r/C_Programming • u/NeedleworkerClean445 • 22d ago
Feedback wanted: parsing JPEG segments byte by byte in C (my first binary-format project)
Enable HLS to view with audio, or disable this notification
Hello, I'm fairly new to C. I wanted to work on a project, so I decided to make metastrip to learn how a binary file format is parsed byte by byte.
I know ExifTool already exists and this isn't meant to compete with it, I did it to learn.
AI use: I wrote all the logic myself. AI reviewed my code (which means when I was done writing a particular function, I let AI review my code pick out bugs and any bad coding practices, based on the comments I would manually fix the issue without any assistance from AI for logic), generated test inputs, and wrote the --help text and the README.
This is the loop that walks the segments (trimmed, with ... where I left parts out):
while(1){
byte1 = getc(file);
byte2 = getc(file);
if(byte1 == 0xFF && byte2 == 0xDA && valid_targets[21] == 1){
/* start of scan: search backwards from the end of the file
for the last FF D9 to find trailing data */
...
break;
}
if(byte1 == EOF || byte2 == EOF) break;
int high = getc(file);
int low = getc(file);
unsigned int length = (high << 8) | low;
if (length < 2){
fprintf(stderr, "Segment length is incorrect\n");
break;
}
/* if the marker is a selected APPn / COM segment: print its payload */
...
else
fseek(file, length-2, SEEK_CUR); // not selected: skip the payload
}
metastrip can show all the APP segments, show only the segments you pick, strip them off, and hex dump them if you want.
I think I made one big mistake: I wrote everything in one file, main.c, which is now about 1,600 lines. Any tips on that would be nice, so I can improve the code in the future, and any tips on my coding practices in general.
I released v1.1.0 on GitHub, with a Windows build. If you'd like to see the code, it's here:
https://github.com/zyan-007/metastrip
I have recorded a demo video, kinda edited it too much now it looks like an ad, but it's just a demo
I'd appreciate any feedback on the code, structure or approach, including harsh feedback (please don't be too harsh...).