Go through a confusing list of binary entries in memory with pointers

I have the following .bin file

1f ac 00 78 00 3f 00 c3 00 83....

and I have to go through it using pointer arithmetic. I have to capture the first byte, which will tell me how many “words” I will process, then every two bytes will tell me the offset, where should I start reading. My problem is that I get the first byte without any problems, but now all I am trying to do is increase my pointer so that it points to ac, drops it to uint16_t, prints this value, do some procedures, and now I want him to point to 78. Here is what I have written so far:

 Pre: Buffer points to a region of memory formatted as specified.
      Log points to an opened text file. 
 Post: The target of Buffer has been parsed and report written as specified
uint8_t doStuff(uint8_t *Buffer, FILE *Log) // given function parameters
{
    int wordsToProcess = *(Buffer); // get that first byte
    uint16_t offset = 0;
    bool firstTime = true;

    for  (int i = 0; i < wordsToProcess; i++)
    {
        if (firstTime)
        {
            Buffer++; // I've tried Buffer += 1;
            offset = *((uint16_t*)Buffer); // casting turns into little endian.
                                           // I want 00 ac but I'm not getting that

            fprintf(Log, "Looking for %0X words, starting at %0X\n",
                    wordsToProcess, offset);

            firstTime = false;
        }
        else
        {
            Buffer += 2;
            offset = *((uint16_t*)Buffer);
        }
    }
}

hexdump, , 66. , Buffer, , , , , Buffer , . - , ?

+4
1

, , , , , . .

, , mantain , , ( ).

uint8_t
doStuff(uint8_t *buffer, FILE *log)
{
    int wordCount = *buffer++;
    uint16_t *pointer = (uint16_t *) buffer;
    uint16_t offset = *pointer++;
    pointer += offset;
    for  (int i = 0; i < wordCount; i++)
        fprintf(log, "0x%04X ", *pointer++);
    return 0 // I don't know what you want to return;
}

, , Undefined .

+1

Source: https://habr.com/ru/post/1618060/


All Articles