I have run the debug50 and it seems the seg fault occurs after trying to free memory here:
// if not start of a new jpeg and we HAVE NOT already found one before
if (block_buffer[0] != 0xff && jpegcounter == 0)
{
//free buffer
free(block_buffer);
}
I did first try setting my buffer on the heap using:
unsigned char* block_buffer = malloc(sizeof(unsigned char) * 512);
However I got memory leaks so am proceeding with array for now.
I have copied my code below any hints or ideas on what might be causing problems would be much appreciated. I expect there a few things wrong as it usually takes me a few drafts, but really stuck with this seg fault.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
//declare variables
int jpegcounter = 0;
int zerocounter = 0;//used to find the EOF (if a block contains all zeros then this should = 512)
int i;
char filename[8]; //stores 000.jpeg format
FILE* img;
// ensure proper usage (can only accept one commant line argument)
if (argc != 2)
{
fprintf(stderr, "Enter 1 argument only\n");
return 1;
}
//give filename to input
char* card_raw = argv[1];
//open card file
FILE *cardfile = fopen(card_raw, "r");
//ensure that file can be openned for reading
if (cardfile == NULL)
{
fprintf(stderr, "Could not open %s.\n", card_raw);
return 2;
}
//repeat until end of card (logic being that a block containing all zeros will neither be new jpeg or part of an already open one)
while(zerocounter != 512)
{
//allocate array for block buffer
unsigned char block_buffer[512];
//read 512 bytes into a buffer
fread(block_buffer, sizeof(unsigned char), 512, cardfile);
//start of a new JPEG? - YES and not found one before
if (block_buffer[0] == 0xff &&
block_buffer[1] == 0xd8 &&
block_buffer[2] == 0xff &&
(block_buffer[3] & 0xf0) == 0xe0
&& jpegcounter == 0)
{
//send formatted filename to array (filname[8])
sprintf(filename, "%03i.jpeg", jpegcounter);
img = fopen(filename, "w");
//write bytes to img file until a new jpeg is detected
fwrite(block_buffer, sizeof(unsigned char), 512, img);
//free buffer
free(block_buffer);
}
// if not start of a new jpeg and we HAVE already found one before
if (block_buffer[0] != 0xff && jpegcounter > 0)
{
sprintf(filename, "%03i.jpeg", jpegcounter);
img = fopen(filename, "w");
//write bytes to img as part of same filename
fwrite(block_buffer, sizeof(unsigned char), 512, img);
//free buffer
free(block_buffer);
}
//if new jpeg and have found one before
if (block_buffer[0] == 0xff &&
block_buffer[1] == 0xd8 &&
block_buffer[2] == 0xff &&
(block_buffer[3] & 0xf0) == 0xe0
&& jpegcounter > 0)
{
jpegcounter ++;
//Close last img file
fclose(img);
sprintf(filename, "%03i.jpeg", jpegcounter);
img = fopen(filename, "w");
//write bytes to img as part of same filename
fwrite(block_buffer, sizeof(unsigned char), 512, img);
//free buffer
free(block_buffer);
}
// if not start of a new jpeg and we HAVE NOT already found one before
if (block_buffer[0] != 0xff && jpegcounter == 0)
{
//free buffer
free(block_buffer);
}
// if not start of a jpeg and contains zeros
else
{
for (i = 0; i < 512; i++)
//initialise zerocounter to 0 each time condition checked
zerocounter = 0;
{
if (block_buffer[i] == 0x00)
{
zerocounter ++;
}
}
if (zerocounter != 512)
//write bytes to img as part of same filename
{
fwrite(block_buffer, sizeof(unsigned char), 512, img);
//free buffer
free(block_buffer);
}
}
}//end while loop
}//end main