0

The code compiles and using GDB I can look at the block data and see the jpeg pattern matches for the first jpeg(I'm assuming it's the first.) The second do loop to pack the jpeg file with data until it sees the next jpeg never stops. Cant understand why it would work for the first do loop and not the second. It is really difficult to use gdb because card.raw is soo big. Thank you for any help I've spent lots of hours debugging and I'm really stuck.


#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
#include <string.h>


void make_file_Name(char jpeg_str[], int file_num);

int main(int argc, char* argv[])
{

// Jpeg pattern 0xff 0xd8 0xff 0xe1
// Jpeg pattern 0xff 0xd8 0xff 0xe1
uint8_t pattern_a[] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t pattern_b[] = {0xff, 0xd8, 0xff, 0xe1};
BYTE block[512];
int match_a;
int match_b;   
int c;
int file_num = 0;

// open input file 
FILE* rawfile = fopen("card.raw", "r");     
if (rawfile == NULL)
{
    fclose(rawfile);
}

//look through beginning of file till find first jpeg
do
{
    fread(&block, sizeof(block), 1, rawfile);
    //just check the first 4 bytes of block
    match_a = memcmp(pattern_a, block, sizeof(pattern_a));
    match_b = memcmp(pattern_b, block, sizeof(pattern_b));
}while(match_a != 0 && match_b != 0);

while((c = fgetc(rawfile)) != 1)
{ 
    char jpeg_name[9];

    file_num++;
    make_file_Name(jpeg_name, file_num);

     // open output file 
     FILE* jpeg = fopen(jpeg_name, "w");
     if (jpeg == NULL)
     {
        fclose(jpeg);
     }

   //keep adding blocks of data (512) till we find the beginning    
  //the next jpeg
    do
    {                                   
        fwrite(&block, sizeof(block), 1, jpeg);   
        fread(&block, sizeof(block), 1, rawfile); 
        //memcmp returns a 0 if bytes are the same                      
        match_a = memcmp(pattern_a, block, sizeof(pattern_a));
        match_b = memcmp(pattern_b, block, sizeof   
              (pattern_b));                         
    }while(match_a != 0 && match_b != 0);

 // close jpeg particular instance of jpeg
 fclose(jpeg);    
}

 //found all jpgs in raw file so close it
 fclose(rawfile);
 }   


void make_file_Name(char jpeg_str[], int file_num)
{   
    char pic_num_str[2] = {0, 0};
    memset(jpeg_str,0,strlen(jpeg_str));

    if( file_num > 9)
    {
        strcat(jpeg_str, "0");                     
    }
    else
    {
        strcat(jpeg_str, "00");      
    }       
sprintf(pic_num_str, "%i", file_num);
strcat(jpeg_str, pic_num_str );    
strcat(jpeg_str, ".jpg"); 
printf("%s\n",jpeg_str);
}   
6
  • Is your while condition is stealing a character from you? (c = fgetc(rawfile)) != 1)? I think that will advance the cursor by one byte, meaning any subsequent headers will come up as the last byte of a block and the first three bytes of the next block instead of as the first four of the next? – Sam Gerber Oct 13 '15 at 21:37
  • Sam you are right, I looked at fgetc and did not realize it moved the pointer. For the moment I decrement the cursor one byte following while statement, I can now see 12 images. I have more work to do but that helps a lot. Thank you. – Laurie Oct 13 '15 at 22:23
  • If I'm not mistaken 12 jpegs end with 0xe0 and the others with 0xe1? I can't see any issue with how you check for them, though, so maybe I'm wrong or it's just coincidence? – Sam Gerber Oct 13 '15 at 23:10
  • Sam, can you please repost as an answer so that Laurie can accept it and get it off the unanswered question pool? Thanks. – Cliff B Oct 14 '15 at 0:08
  • Cliff, it doesn't sound like the issue is resolved yet. – Sam Gerber Oct 14 '15 at 4:06

You must log in to answer this question.

Browse other questions tagged .