0

I'm not sure if I use correctly the hexadecimal notation to verify if there is a .jpeg signature in the beginning of my BLOCK of 512 bytes. My program compile well but no photograph is revealed. May be someone can help me with that? Here a part of my code. Great thanks for your time.

int main(int argc, char* argv[])
{    
    typedef uint8_t BYTE;
    typedef uint32_t SIGN;
    SIGN buffer[BLOCK];
    int count = 0;
    FILE* pic;
    char picname[8];


    // open file
    FILE* inptr = fopen("card.raw", "r");
    char* name = "card.raw";

    if (inptr == NULL)
    {
        printf("Could not open %s.\n", name);
        return 1;
    }

    // until end of data is reached    
    while (fread(&buffer, BLOCK, 1, inptr) == 1)
    {
        // read 512 bytes
        // fread(&buffer, BLOCK, 1, inptr);

        printf("%0x\n", buffer[0]);
        printf("%0x\n", 0xffd8ffe0);

        if (buffer[0] == 0xffd8ffe0 || buffer[0] == 0xffd8ffe1)
        {
            if (count > 0)
        {
2
  • I resolved all my problems with my code!! If you have any question related to this one or want to know what I did to recover correctly, it'll be my pleasure to help you. Great thanks to @cliffb
    – CharlesD89
    Commented Nov 5, 2015 at 2:21
  • ... and thanks to @Irene too!
    – CharlesD89
    Commented Nov 5, 2015 at 2:22

2 Answers 2

1

1) What's the second argument of fread supposed to be? Are you sure passing BLOCK is correct?

2) Why are you reading only 4 bytes (I guess that's your intention) at a time? You know the file is formatted in chunks of 512 bytes, take advantage of that so that you don't loop so many times.

3) The problem with what you're doing (once you fix what i mentioned in the first point) is one of endianness. You can read about it here: http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

The safest and easiest way would be to store each byte separately and evaluate them in the same way.

2
  • BLOCK corresponds to a memory bloc of 512 bytes. I'm trying to read a single element (third arg = 1) of 512 bytes size at each pass, so I think it is the right way to proceed with the fread function... Now, with help of the second answer, I changed my code to only compare one byte at time to find out if the first four bytes of the BLOCK correspond to the signature. I don't really understand which statement your talking about when you said that I only read 4 bytes at a time?!
    – CharlesD89
    Commented Nov 5, 2015 at 1:26
  • Yes, for some reason i assumed that BLOCK was the actual block read (the array) and not a define for 512. I don't really know why you have a SIGN array of size 512 though. That's an array of size 2048 bytes (sign is 4 bytes, you have 512 elements of 4 bytes). If you wanted to still use a 32 bit integer and compare the 4 bytes at a time, you would maybe want to use an array of size 128 and reverse the order of the bytes compared, to account for endianness. Using bytes for storing and comparing is clearer and safer. Sorry for any confusion :)
    – Irene
    Commented Nov 5, 2015 at 2:20
1

To answer your specific question, no, this isn't the way to check for a signature in the beginning of the buffer. The following line,

        if (buffer[0] == 0xffd8ffe0 || buffer[0] == 0xffd8ffe1)

doesn't work because you can't compare strings of bytes like this. You need to compare single bytes to single bytes, similar to this:

    if ( buffer[0] == 0xff && buffer[1] == 0xd8 ....)

I'll let you figure out how the rest of it should look, but that's the general idea. There are other techniques, but that becomes a larger conversation.

If this answers your question, please click the check mark and accept. Let's keep up on forum maintenance. ;-)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .