r/C_Programming • • 25d 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...).

22 Upvotes

13 comments sorted by

•

u/AutoModerator 25d ago

Hi /u/NeedleworkerClean445,

Your submission in r/C_Programming was filtered because it links to a git project.

You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.

While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.

You can read more at How do I post my Project?


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

→ More replies (2)

5

u/mikeblas 24d ago

I think you want to work on a couple of things:

  • use functions. Your main() function has a ton of code. split it out
  • use functions. You nest if/else logic deeply, when it could just be a function call.

1

u/NeedleworkerClean445 24d ago

Thanks, that's really helpful. I'm working on a new version now; I'll make sure to cut down main function and will try to reduce the nesting by enclosing the logic into functions.
btw I am thinking of splitting main into different files also instead of keeping all in one, is it a good practice or is it not required??

1

u/mikeblas 24d ago

It's not required -- it's mostly a matter of taste. I usually split files when they get to about 1000 lines. Or, if they have semantically, contextually different content.

Maybe one way to think of it is that, if you split the files, you'll have to make the declarations and interfaces so the definitions in one file are visible to the to other file. Is splitting the file worth that work?

1

u/NeedleworkerClean445 24d ago

Thanks, that makes sense. Mine is past 1000 lines and has many different functionalities, for future versions I'm thinking of adding png too and maybe a gui version which would show each marker in one block with which you can interact with (this i will do a bit later when i become more comfortable with c), so i think it would be nice to split it now or else it might become a headache later on. what do you think?

7

u/AmanBabuHemant 24d ago

I have doubt that AI only wrote your --help option text, are you sure while making this statement ?

3

u/NeedleworkerClean445 24d ago

i can totally see how u might see that way coming that this is my first major project, but i can assure u that i wrote it myself apart form --help (print usage) and read me which were written by AI, there are a lot of mistakes i made along the way but i did get the code reviewed by ai and picked up many good practices along the way.
my commit history on github shows how over the span of 2-3 months I improved the project and my coding skills improved over time. This is the best proof I have to support my project was made by me.
You may visit my github to see the full code and if any part of my code raises suspicion that it might not be written by me I would be happy to provide my insight on how I came up with the specific logic and wrote the code. Still thank you for your comment. :)

1

u/FedUp233 23d ago

One area of confusion might be, when you said you had AI review the code, did you let it do the editing to clean up issues or did you just look at the AI’s comments and then do the changes manually yourself? If the former I could see where that might confuse people to think the cud was more AI generated than it was.

2

u/NeedleworkerClean445 22d ago

oh, thnx for clarifying the cause for confusion. Guess i should have made it clear in the post i will edit it now. What i meant was I took the AI's comments and then did all the changes manually, not a single line apart from what i mentioned was written by AI. Like When i was done writing a particular function i let AI review it and point me my mistakes and any bugs then based on the comments i manually fixed each and everything on my own without any assistance. This includes no help about the logic itself, i came up with the logic myself for each problem. Thnx for letting me know :)

1

u/floatfloatjam 23d ago

Thank you for disclosing your AI usage. Valid use case imo

1

u/NeedleworkerClean445 23d ago

Thanks, I appreciate that. I wanted to be upfront about it.