I'm trying to copy 50 jpegs, one by one from a large .raw file, however currently I get a segmentation fault error. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
//SOI - 0xFF 0xD8
//EOI - 0xFF 0xD9
//APPn - 0xFF 0xEn
int main(void)
{
//FAT - 512 bytes per block
BYTE block[512];
//open file containing pictures
FILE* card_file = fopen("card.raw", "rd");
FILE* jpeg_file;
//make sure the file opened without errors
if (card_file == NULL)
{
printf("something went wrong and file could not be opened");
return 1;
}
int i = 0;
while (fread(&block, sizeof(BYTE), 512, card_file) != 0)
{
//jpeg start signature
if(block[0] == 0xFF && block[1] == 0xD8)
{
i++;
if(jpeg_file != NULL)
fclose(jpeg_file);
//create a new jpeg file to copy bytes to
jpeg_file = fopen((char*)i, "w+");
}
//write 512 bytes to a jpeg file
if(jpeg_file != NULL)
fwrite(block, sizeof(block), 1, jpeg_file);
}
fclose(card_file);
return 0;
}
when I run it through GDB, my code gets all the way to if(block[0] == 0xFF && block1 == 0xD8), then it skips the condition and segmentation fault occurs. I don't see what might be causing this.
Here's a screenshot:
Code updated:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
typedef uint8_t BYTE;
/*struct jpg*/
/*{*/
/* BYTE soi[2] = { 0xFF, 0xD8 };*/
/* BYTE eoi[2] = { 0xFF, 0xD9 };*/
/*};*/
//SOI - 0xFF 0xD8
//EOI - 0xFF 0xD9
//APPn - 0xFF 0xEn
int main(void)
{
//FAT - 512 bytes per block
BYTE block[512];
//jpeg name
char name[6];
bool is_open = false;
//JPEG
//struct jpg image;
//open file containing pictures
FILE* card_file = fopen("card.raw", "r");
FILE* jpeg_file;
//make sure the file opened without errors
if (card_file == NULL)
{
printf("something went wrong and file could not be opened");
return 1;
}
int i = 0;
while (fread(block, sizeof(BYTE), 512, card_file) != 0)
{
//jpeg start signature
if ((block[0] == 0xFF) && (block[1] == 0xD8) && (block[2] == 0xFF) && ((block[3] == 0xe1) || (block[3] == 0xe0)))
{
//assign jpeg name
sprintf(name, "%d.jpg", i++);
if(is_open)
fclose(jpeg_file);
//create a new jpeg file to copy bytes to
jpeg_file = fopen(name, "a+");
is_open = true;
}
//write 512 bytes to a jpeg file
if(is_open)
fwrite(block, sizeof(block), 1, jpeg_file);
}
fclose(jpeg_file);
fclose(card_file);
return 0;
}
Now it doesn't crash, however only 9 out of 50 jpegs are properly recovered. cs50.h is there just so I have access to bool type. What's a better way to write 50 files? I seem to have a logical flaw with my booleans.
fopen((char*)i, "w+"); is completely invalid. You are casting an integer as a pointer, which is going to crash.
You need to format the number as a filename:
char path[PATH_MAX];
sprintf(path, "%d", i);
fopen(path, "w+");
You are also not initializing jpeg_file -- if the condition fails, jpeg_file will be a wild pointer, which also crashes. You should initialize jpeg_file to NULL.
In your fread call, you should pass the address of the array. Hence, the statement should be fread(block, sizeof(BYTE), 512, card_file).
Postscript:
In your code, there is assumption that the size of the input file is an integral multiple of 512, which needn't the case for JPEG files. The last fread might return a number less than 512 which needs to be handled in your implementation logic. Hence, the number of elements to write should be determined by the return value of fread
You would need to close the jpeg_file pointer after the loop terminates.
Last, because you are working with JPEG, you may want to handle a case for EXIF files with thumbnails. In this case, you would get 2 SOI (start of image) markers.
Related
I am working on problem set 4 "Memory" and trying to understand fread() function and how using fread() inside of a while loop of fread() works. I am trying to read a file until end of file, which is what my while loop is for, then when I find the JPEG file signature, I would like to read from that file until I find the next JPEG signature. My question is how does this work for the two separate calls to fread() function? Once I find the file signature and start reading from file using fread() inside the while loop then iterate over that while loop after exiting my if conditional, does the while(fread()) pickup where fread() left off inside the while loop? Please see code below:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// using keyword typedef to give the uint8_t a new name of BYTE (capitalized by convention)
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// must be two arguments or program not run correctly
if(argc != 2)
{
printf("Usage: ./recover filename\n");
return 1;
}
// open file and store its location in a pointer called infile
FILE *infile = fopen(argv[1], "r");
if(infile == NULL)
{
printf("file cannot be opened or doesnt exist...\n");
return 1;
}
// read using fread(), each 512 byte block into a buffer
//need a buffer of size 512 BYTEs
BYTE buffer[512];
while(fread(&buffer, sizeof(BYTE), 512, infile))
{
// create buffer to store a filename with a formatted string of ###.jpg starting at 000.jpg
int number = 0;
char filename[8];
sprintf(filename, "%03i.jpg", number);
// this demarks a JPEG using bitwise logical & for last buffer bit of signature
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// this will be the first signature read and start of a JPEG
if(number == 0)
{
// open new file
FILE *img = fopen(filename, "w");
// write what is currently in buffer into file
fwrite(&buffer, sizeof(BYTE), 512, img);
// continue reading from file where left off and write it to img file until a new file signature?
while(fread(&buffer, sizeof(BYTE), 512, infile))
{
fwrite(&buffer, sizeof(BYTE), 512, img)
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
break;
}
}
My question is about the second call to fread() while inside the while loop and how that call to fread() affects the call to fread() for the initial while loop. Hope this made sense.
I think you have figured out the solution in the comments.
You can also use functions and recursion here.
I have been trying to do this problem for at least a week now, and can't seem to understand where is the problem, I already checked everything in google, and dont know any programmer in real life to ask them personaly, so if anyone can help me it would be great.
None of the images generated load, and it doesnt recover 50 as it is suposed to, it recovers 986.
I get this results in check50:
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
recovered image does not match
:( recovers middle images correctly
recovered image does not match
:( recovers 049.jpg correctly
recovered image does not match
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
typedef uint8_t BYTE;
#define BLOCK_SIZE 512
int main(int argc, char *argv[])
{
//it only accepts one comand argument in the name of an image
if (argc != 2)
{
printf("Usage: ./recover IMAGE");
return 1;
}
//check if it can open the image
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("The image cannot be opened");
return 1;
}
bool jpg_before = false;
int counter = 0;
FILE *image = NULL;
char name[8];
unsigned char buffer[BLOCK_SIZE];
//while there is still jpegs in the file
while (fread(buffer, BLOCK_SIZE, 1, file) == 1)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0)
{
jpg_before = true;
}
if(jpg_before == true)
{
sprintf(name, "%03i.jpg", counter);
counter++;
image = fopen(name, "a");
fwrite(buffer, BLOCK_SIZE, 1, image);
fclose(image);
}
}
fclose(file);
}
(also please keep in mind I'm new to programming, 16 years old and english is not my first lenguage)
When you detect a header in the input, you set jpg_before. But, you never clear it.
Once the flag is set, each block will be put into a different file.
Every output file should consist of a header, followed by the associated data blocks.
name[8] is a bit too small. The compiler will complain because the int could [in theory] be 10 or so digits, so the sprintf could overflow. Don't be stingy--use (e.g.): char name[20];
Output file should be opened with "w" instead of "a". If the program is run twice, the second time, the output file(s) will be incorrect.
Here is the refactored code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//#include <cs50.h>
typedef uint8_t BYTE;
#define BLOCK_SIZE 512
int
main(int argc, char *argv[])
{
// it only accepts one comand argument in the name of an image
if (argc != 2) {
printf("Usage: ./recover IMAGE");
return 1;
}
// check if it can open the image
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
printf("The image cannot be opened");
return 1;
}
int counter = 0;
FILE *image = NULL;
char name[20];
unsigned char buffer[BLOCK_SIZE];
// while there is still jpegs in the file
while (fread(buffer, BLOCK_SIZE, 1, file) == 1) {
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0) {
if (image != NULL)
fclose(image);
sprintf(name, "%03i.jpg", counter);
counter++;
image = fopen(name, "w");
}
fwrite(buffer, BLOCK_SIZE, 1, image);
}
if (image != NULL)
fclose(image);
fclose(file);
}
UPDATE:
From the comments below:
Points 2 and 3 look to be handled by the file being opened for appending. Leaving the file open is probably a better idea, though. Faster and handles point 6. –
user4581301
If H is header and D is data, for an input of (e.g): H1,D1,D2,D3,D4,H2,D5,D6,D7:
Instead of two output files: F0:H1,D1,D2,D3,D4 and F1:H2,D5,D6,D7
We'd have: F0:H1, F1:D1, F2:D2, F3:D3, F4:D4, F5:H2, F6:D5, F7:D6, F8:D7
Although my refactored code was correct, the top section of my answer had an incorrect analysis of what OP's code was actually doing.
I've fixed that. But, to make user4581301's make sense, here is the original analysis:
When you detect a header in the input, you set jpg_before. But, you never clear it.
You only write to the output stream for the header block, so any data is not copied. So, each output file will only be 512 bytes
You immediately close the output stream after writing the header. It should be left open.
Every block must go to a given output file, not just the header.
name[8] is a bit too small. The compiler will complain because the int could [in theory] be 10 or so digits, so the sprintf could overflow. Don't be stingy--use (e.g.): char name[20];
Output file should be opened with "w" instead of "a". If the program is run twice, the second time, the output file(s) will be incorrect.
UPDATE #2:
First of all thanks! But it is giving me a segmentation fault, do you have any idea why? because everything seems correct –
Isa M
From code inspection, the only place that could segfault is the fwrite call (i.e. image is NULL).
I confirmed this by running the program under gdb [I have the cs50 recover input file]. When the program faults, just do tb to get a stack traceback.
image could be NULL for the following reasons:
The fopen for output file could fail (due to permissions, space, etc.) and return NULL. There was no check after the call as there was for opening the input file.
image starts out being NULL. If there is some sort of extra file data/file header before the first jpg header (e.g. before FF/D8/FF/E0) the if will not match on the first block read. The fwrite will be called even with a NULL in image.
Option (2) is what actually occurred because cs50's file has an extra header at the top of the file. You can see this by examining the file with a hex editor/dumper (e.g.) od or xxd:
00000000: 00000000 00000000 00000000 00000000 ................
*
00000200: 63733530 2E6C792F 73757270 72697365 cs50.ly/surprise
00000210: 00000000 00000000 00000000 00000000 ................
*
00000400: FFD8FFE0 00104A46 49460001 01000001 ......JFIF......
The code will not see a valid header (i.e. the if matches) until offset 400. So, there are two extraneous fread calls at the start until things sync up.
The fix is to change:
fwrite(buffer, BLOCK_SIZE, 1, image);
Into:
if (image != NULL)
fwrite(buffer, BLOCK_SIZE, 1, image);
I've written a few answers on this problem before. However, I forgot to include this. I just wrote the code but did not test it ;-)
To round things out, I've added more return code checking and added "rb" and "wb" to the fopen calls, just in case you're running on Windoze.
Here is the updated/fixed code (I've tested it this time ;-):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
//#include <cs50.h>
typedef uint8_t BYTE;
#define BLOCK_SIZE 512
void
onerr(const char *action,const char *file)
{
printf("%s -- %s -- %s\n",action,file,strerror(errno));
exit(1);
}
int
main(int argc, char *argv[])
{
// it only accepts one comand argument in the name of an image
if (argc != 2) {
printf("Usage: ./recover IMAGE");
return 1;
}
// check if it can open the image
FILE *file = fopen(argv[1], "rb");
if (file == NULL)
onerr("The image cannot be opened",argv[1]);
int counter = 0;
FILE *image = NULL;
char name[20];
unsigned char buffer[BLOCK_SIZE];
// while there is still jpegs in the file
while (fread(buffer, BLOCK_SIZE, 1, file) == 1) {
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xe0) == 0xe0) {
if (image != NULL)
fclose(image);
sprintf(name, "%03i.jpg", counter);
counter++;
image = fopen(name, "wb");
if (image == NULL)
onerr("unable to open output file",name);
}
#if 0
fwrite(buffer, BLOCK_SIZE, 1, image);
#else
if (image != NULL)
fwrite(buffer, BLOCK_SIZE, 1, image);
#endif
}
if (image != NULL)
fclose(image);
fclose(file);
return 0;
}
I am writing a code that reads information from a memory card (card.raw is the one we are provided but the code uses user input) and extracts the jpegs from it using the signatures that jpegs have of (0xff,0xd8,0xff,0x00 - 0xff). I am getting a segmentation fault because i am using malloc, but i dont see where i went wrong. I am pasting my code here any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
//check terminal usage
if (argc != 2)
{
printf("Usage: ./recover image");
return 1;
}
//open inputted file and check for valid file
FILE *file = fopen(argv[1], "r");
if (!file)
{
printf("Invalid or missing file.");
return 1;
}
BYTE *buff = malloc(512 * sizeof(BYTE));
int counter = 0;
FILE *image = NULL;
char *name = malloc(8 * sizeof(char));
//loop till end of file reached and read a block of input
while(fread(buff, sizeof(BYTE), 512, file) == 1 && !feof(file))
{
bool foundJPEG = buff[0] == 0xff && buff[1] == 0xd8 && buff[2] == 0xff && ((buff[3] & 0xf0) == 0xe0);
//check if found jpeg, and open file for writing
if (foundJPEG)
{
sprintf(name, "%03i.jpg", counter);
image = fopen(name, "w");
}
//if image file open, write to it
if (image != NULL)
{
fwrite(buff, sizeof(BYTE), 512, image);
}
//if found a jpeg already, close it so new one can be written
if (foundJPEG && image != NULL)
{
fclose(image);
counter++;
}
}
free(name);
free(buff);
fclose(image);
fclose(file);
return 0;
}
There are three issues with the code above which are not mentioned in the comments:
The return value of fread is not 1 but 512, upon successful read. You exchanged the parameters for the blocksize and the blockcount -> fread definition. Therefore the while loop is not entered.
Don't try to save space with packing to much code into one statement. If would be more clever to separate the checks for the fread return value and the EOF and use a do ... while() loop, instead. Then you had the chance of seeing this issue in the debugger. This was exactly what i have done and how i found this out.
The second issue is that you close the image after rescuing the first 512 bytes, but you do not reset the file pointer image back to NULL along with the fclose statement.
As a consequence, the code would repeatedly write to an a file which is closed until a new block with a jpg header is found.
The third issue is that you only rescue the first 512 bytes of the jpg but not the whole jpg. You need to scan the input stream for the jpg end indicator FF D9 and copy bytes until it is found. ->jpg format
I am learning how to code and I have no experience with that at all. I've successful got to PSET4 and stuck on recover. I've read everything online about this problem and i found out that many people have similar code as I do and it works. Does not work for me whatsoever. Please have a look and give me a hint what did I do wrong and how to correct it.
Here is everything about the pset4 recover i downloaded their card.raw from here card.raw
/** recovering JPEG files from a memory card
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr,
"Usage: ./recover infile (the name of a forensic image from which to recover JPEGs)\n");
return 1;
}
// open input file (forensic image)
FILE* inptr = fopen(argv[1], "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
FILE* outptr = NULL;
// create a pointer array of 512 elements to store 512 bytes from the memory card
BYTE* buffer = malloc(sizeof(BYTE) * 512);
if (buffer == NULL)
{
return 3;
}
// count amount of jpeg files found
int jpeg = 0;
// string for a file name using sprintf
char filename[8] = { 0 };
// read memory card untill the end of file
while (fread(buffer, sizeof(BYTE) * 512, 1, inptr) != 0)
{
// check if jpeg is found
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff
&& (buffer[3] >= 0xe0 || buffer[3] <= 0xef))
{
if (jpeg > 0)
{
fclose(outptr);
}
sprintf(filename, "%03d.JPEG", jpeg);
outptr = fopen(filename, "w");
jpeg++;
}
if (jpeg > 0)
{
fwrite(buffer, sizeof(BYTE) * 512, 1, outptr);
}
}
// free memory
free(buffer);
// close filename
fclose(outptr);
// close input file (forensic image)
fclose(inptr);
return 0;
}
The main problem is that you invoke undefined behavior because filename is not enough big. sprintf() need be 9 and 17 bytes with your code but you only has 8. So you have a buffer overflow.
Just change:
char filename[8] = { 0 };
to
char filename[17] = { 0 };
Because, you use an int, this value is implemented defined but in many system has an int with 32 bits. So the value possible are between -2^31 and 2^31 - 1 that make a maximum of 11 chars (-2147483648). We add the number of chars in ".JPEG", 5. We have 16 but you forget the null terminate byte of a c-string. So we are 17 maximum.
Modern compiler warning you: gcc version 7.1.1 20170516 (GCC):
In function ‘main’:
warning: ‘sprintf’ writing a terminating nul past the end of the destination [-Wformat-overflow ]
sprintf(filename, "%03d.JPEG", jpeg++);
^
note: ‘sprintf’ output between 9 and 17 bytes into a destination of size 8
sprintf(filename, "%03d.JPEG", jpeg++);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Plus, your typedef is useless because a char world be always a byte in C. More than that you don't need a byte but an octet so like char, uint8_t would be always an octet in C. So you don't need typedef.
Again one thing, you allocate your buffer but it's useless because your buffer has a constant size. So just create an array is more simple.
#include <stdint.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: ./recover infile (the name of a forensic image "
"from which to recover JPEGs)\n");
return 1;
}
FILE *inptr = fopen(argv[1], "r");
if (inptr == NULL) {
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
FILE *outptr = NULL;
uint8_t buffer[512];
size_t const buffer_size = sizeof buffer / sizeof *buffer;
size_t jpeg = 0;
while (fread(buffer, sizeof *buffer, buffer_size, inptr) == buffer_size) {
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
buffer[3] == 0xe0) {
if (outptr != NULL) {
fclose(outptr);
}
char filename[26];
sprintf(filename, "%03zu.JPEG", jpeg++);
outptr = fopen(filename, "w");
}
if (outptr != NULL) {
fwrite(buffer, sizeof *buffer, buffer_size, outptr);
}
}
if (outptr != NULL) {
fwrite(buffer, sizeof *buffer, buffer_size, outptr);
}
if (outptr != NULL) {
fclose(outptr);
}
fclose(inptr);
}
Note: This example is clearly not perfect, this will be better to make a true parser for jpeg file to have a better control flow. Here we suppose that all gonna be right.
how do you know that an instance of a JPEG image will always end with '\n'? Or better, how do you know that a JPEG image will be an exact multiple of 512?
You dont know.
So the posted code needs to calculate the actual value OR use some method to have the last call to fread() for any specific JPEG instance, to stop reading at the end of that image,
Then the check for the ID bytes of the next JPEG image will find the next image.
Otherwise, the start of the next image is already written to the prior output file and the check for a new image will fail.
In general this will result in the last created file containing more than one image.
This link: 'https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format' is a web page that describes the format of a JPEG file.
On every digital camera that I have used, the SD card has a directory of all the files.
Suggest using that directory and the info in the linked web page to find each JPEG image and to determine when the end of that image has been encountered. (I.E. the 0xFF 0xD9)
I’m a very novice programmer and I’ve encountered an issue whose nature I don’t understand, whilst working on a problem set of the excellent cs50 course. I have implemented a program to recover JPEG pictures from an image of a memory card and am implementing a break at End of File as follows:
if(file > 1)
{
if (fread(&buffer, 1, 512, in_pointer) != 512)
{
free(filename);
return 0;
}
else
fseek(in_pointer, -512, SEEK_CUR);
}
(the pictures are filling up the card in 512 byte blocks). When I first implemented this it broke my first picture (it was recognizable but distorted) so I excluded it by means of the first if statement. Now however the middle files of the set are slightly off– they still open as Jpegs but I can’t get their thumbnails to work. My hypothesis is that I am corrupting the JPEG file format header. The beginning (including first and last images of the set work perfectly).
My questions are:
What is an elegant way to implement an EOF break since my getto solution is causing trouble?
What is the likely nature of the problem I’ve created (in layman’s terms)?
Thank you very much,
Tikhon
ps here is the whole thing
#include <cs50.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "enter exactly two command line arguments: ./recover and destination of disc to scan\n");
return 1;
}
//name the file
char *infile = argv[1];
//open card file and ensure proper format
FILE *in_pointer = fopen(infile, "r");
if (in_pointer == NULL)
{
fprintf(stderr, "could not open %s\n", infile);
return 2;
}
typedef uint8_t BYTE;
BYTE buffer[512];
bool new_jpeg = false;
int block = 0;
int file = 0;
char *filename = malloc(3);
//sprintf(filename, "%03i.jpg",1);
do
{
//read a 512 block of a jpeg
fread(&buffer, 512, 1, in_pointer);
//check for new jpeg
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) // took me a while to figure this out
{
new_jpeg = true;
//printf("jpeg found, block %i\n", block);
}
block++;
} while(new_jpeg == false);
do
{
//set name of file to write to
sprintf(filename, "%03i.jpg",file);
file++;
new_jpeg = false;
// open output file
FILE *img = fopen(filename, "w");
if (img == NULL)
{
fprintf(stderr, "Could not create %s.\n", filename);
return 3;
}
//add blocks to file while before we reach the nea JPEG.
do
{
fwrite(&buffer, 1, 512, img);
//read the next block
fread(&buffer, 1, 512, in_pointer);
//There MUST be a better way... Anyhow this checks for end of file but backtracks becouse the act of checking moved the file coursor forward...
if(file > 1)
{
if (fread(&buffer, 1, 512, in_pointer) != 512)
{
free(filename);
return 0;
}
else
fseek(in_pointer, -512, SEEK_CUR);
}
block++; //we are reading off teh next block
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) // took me a while to figure this out
{
new_jpeg = true;
//printf("jpeg %i found, block %i\n", file, block);
}
}while(new_jpeg == false);
}while(!feof(in_pointer));
free(filename);
//ran valgrind no probs detected.
}
OK, I fixed it.
Instead of having a whole separate section of code to check eof I killed two birds in one stone and fread the file WHILE checking for eof:
//read the next block
int k = fread(&buffer, 1, 512, in_pointer);
if(k != 512)
{
free(filename);
return 0;
}
I still have no idea why my previous method didn't work, I would be extremly grateful for suggestions...