0

So right now, my code can open the compact flash card and look for jpg headers byte by byte. It tells me that it has found 16 jpg files (which is what I had anticipated).

However, my concern is detecting parts of jpg files after the initial head has been found and writing it to each jpg file. My problem is that every time I put another if statement (or else if or else) statement below the head detection if statement, the program goes into infinite loop and I'm not sure why.

I've taken steps in eliminating all the unnecessary code and only use printf statements to build a prototype for what I will do, but it just won't work. Even a simple printf statement below the said if statement (line 36) will cause my program to loop non-stop printing that same message!

Can someone help?

Code is posted at http://pastebin.ca/3077969

1
  • Try writing some pseudocode. – i_am_david Jul 27 '15 at 1:43
1

It's not an infinite loop, it's just taking an extraordinarily long time to process. Look at your code to check for the signature:

    for (int i = 0, n = sizeof(buffer); i < n; i++)
    {  
        if(buffer[i] == 0xff && buffer[i + 1] == 0xd8 &&
          buffer[i + 2] == 0xff && 
          (buffer[i + 3] == 0xe0 || buffer[i + 3] ==  0xe1))

The signature will ONLY appear in the first 4 bytes of any 512 byte block. Your for loop causes the check to be done across EVERY byte of the buffer. In other words, it checks the 4 bytes starting at byte 0, then the 4 bytes starting at byte 1, then byte 2, and so on until it makes 512 checks on EVERY block! In short, you're making a check for every byte in the file. Given the input file, that's about 20 million checks. It takes time.

You only need to check the first 4 bytes of any given block. If the signature doesn't start on the first byte, it isn't a signature. Note that you could potentially have real data inside the jpg that is equal to the signature, so your checks after the first 4 bytes could potentially find a false signature.

As for the rest, the spec says that once you find the first block with a signature, assume everything that follows is part of the file until you hit another signature in the first 4 bytes of a block.

If this answers your question, please accept this answer to close the question. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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