0

In my .c file I had jpg[4] to check for jpg signature (using memcmp()) in certain file:

static const unsigned __int8 jpg[4] = { 0xFF, 0xD8, 0xFF, 0xDB };

Comparison works great and now I would like to add some more format signatures, for example:

static const unsigned __int8 png[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };

I dont want to copy paste code with different signature variable. How do I create an array of such not changing values and iterate through each signature using for(;;). I don't want to declare them inside methods.

I know it's some basic stuff, but I'm pretty new to C, so it's not so obvious to me.

In pseudo code:

bool isImg(bool * value)
{
for(int index = 0; index < signatures count; i+++) <-- for should iterate through signatures
    {
        // use signature[index] which is array of bytes { 0xFF, Ox... }
        // check signature
    }
}
5
  • Identifiers starting with __ are reserved by the standard. Do not use! Commented Jan 28, 2016 at 15:18
  • Are you asking how to declare a 2 dimensional array? And C does not support methods, only functions Commented Jan 28, 2016 at 15:18
  • Yes. I do know how to do it inside function or constructor, but I have no idea how to declare it outside. Commented Jan 28, 2016 at 15:20
  • Please give more information, it's not clear what you want. Commented Jan 28, 2016 at 15:22
  • 1
    Do not use homebrew types if there are standard ones. For fixed bit-width use int8_t & friends from stdint.h. Commented Jan 28, 2016 at 20:15

1 Answer 1

1

Maybe you want something like this:

static const unsigned __int8 jpg[4] = { 0xFF, 0xD8, 0xFF, 0xDB };
static const unsigned __int8 png[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };

static const unsigned __int8 *signatures[] = { jpg, png };

Now you can iterate through the sigantures array. But then you don't know the length of each signature in the signatures array.

You could get around this by encoding the length in the first element of each signature:

static const unsigned __int8 jpg[] = { 4, 0xFF, 0xD8, 0xFF, 0xDB };
static const unsigned __int8 png[] = { 8, 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };

static const unsigned __int8 *signatures[] = { jpg, png };

bool isImg(bool * value)
{
  for(int i = 0; i < (sizeof (signatures)) / (sizeof (__int8*)); i++)
  {
    const unsigned __int8 *signature = signatures[i];
    int signaturesize = signature[0]; // here: 4 for i==0, 8 for i==1

        // use signature[i] which is array of bytes { 0xFF, Ox... }
        // check signature
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Can I get size of array like this (for jpg): (integer type) sizeof(jpg)/sizeof(jpg[0]) ? Nevermind, you just edited with same suggestion as I though doing to get size. I'll test it now.
You sure signaturesize doesn't display 255 in the first case instead of 4?
@Dancia yes, provided the first element of jpg is 4 as in my example.
Is there a way to get size using only something like int size = (int) sizeof(jpg)/sizeof(jpg[0]) without having 4 inside jpg? Because if not, it makes more sense to make something like static const int signatureSize[2] = { 4, 8 }; and delete counting from actual signature
int size = (int) sizeof(jpg)/sizeof(jpg[0]) works because the compiler know what jpg is, but if you do sizeof(signature)/sizeof(signature[0]) you won't get the number of elements, because at that point signature has decayed into a pointer to __int8 and the length of the array signature points to cannot be deduced. Having int signatureSize[2] = { 4, 8 }; would work, but this is somewhat dangerous, because the order of the signatures in the signatures array must match the order of sizes in the signatureSize array and if it doesn't you will get unexpected behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.