How do I edit binary files using C3?

  • I'm working on an NWjs windows app that can edit the metadata in large batches of files such as JPGs and others. My app searches and replaces certain words or phrases according to very specific needs. The problem is these files contain characters that get corrupted when my app loads them into a text string via Ajax or whatever. So basically the app does everything it's supposed to but corrupts all the files it changes because of this issue.

    I tried loading and saving the files using the binary plugin but I have no idea how to actually make any changes in their content. I hope I'm missing something here because everything I tried seemed to go nowhere. So, does anyone have any ideas about how to edit these "binary" files without ruining them? Thanks.

  • You already mentioned the right answer - use the Binary Data object.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I also mentioned I have no idea what to do other than load and save the files using the Binary Data Object. When I load them as text I put the data into a string and use find, replace and such to make the edits I need. How do I do that with the data inside the Binary object? I tried converting it to text but it brought me back to the same problem.

  • The manual link actually covers it pretty well.

    In order to manipulate the data in binary form, you first have to "tell" what value is inside the data, hence the expressions for setting and getting different types of data.

    I doubt that text manipulation will help here.

    If you read the file and convert it to UTF 8 text, then do your search and replace and save it back, it has already been read wrongly due to the text conversion, then you'll write the corrupted data back and the file is then broken.

    Also you mentioned "files such as JPGs and others", I assume you are referring to EXIF data of the image file.

    Maybe this will help on your journey: https://www.npmjs.com/package/node-exiftool

  • It's theoretically possible to do any binary manipulation with Binary Data, since it has expressions to read data and actions to set data. It can be awkward to do this kind of binary transformation within the event system though. Perhaps we should find a way to expose it to JavaScript so it can be done from code instead.

  • RageByte the basic issue is that the data your trying to read/write isn't text. It contains text, but reading and writing the non-text parts as text is wrong. Most file formats use explicit offset and length values to separate the components of the data, so unless the text was exactly the same length as the piece you were replacing it would make any surrounding offsets and lengths incorrect.

    If you want to modify JPEG data you will have to read the data according to the JPEG specification. This is perfectly possible with the BinaryData plugin, but it's going to be hard work. I've written encoders and decoders for several file formats in the past and most took several days of research and coding to implement. If you know even a little JavaScript it will be a lot easier for you to find a library to do this for you.

  • I am currently working on manipulating the metadata in a format called EPS. At it's origins it's all text but because I save the files in a custom Adobe format they call EPS10 (which also allows the format to have metadata at all) there is a portion of it that is encoded as binary data.

    What I am trying to do is separate the text from the binary without touching the latter. I've managed to do this in a small test project, on a txt file with special characters. I load the file into a BinaryData with Ajax, then I copy the binary into a string as text, solely for the purpose of figuring out where the binary data starts. Within the string I look for a certain phrase/tag that signals the begging of the binary stuff, which, thankfully, is all at the end of the file. In this case I choose an arbitrary point where I break the file in half. Now I have the ByteOffset by which I can copy the binary data from the BinaryData object into a new temporary BinaryData object, for safekeeping. Now I cut off the corrupted binary data from the text string and do my edits on the meta part of it. Now I rewrite the BinaryData object by loading the text string into it, aka the first half of the file and then I attach the actual binary data I was safekeeping, aka the second half of the file. Now the file is successfully altered without corrupting the binary information. And just to clarify, I indeed have no intention of altering that information, only to work around it without breaking it.

    My main confusion was about how to use the data manipulation actions on the BinaryData object since I never worked with binary before. I think I got it to a degree. I am yet to implement this technique into my actual software but the test project seems to have worked because in the txt file it alters, the first half has it's special characters all screwed-up while the second part doesn't.

    As for JPGs, I see that they keep all their metadata right at the beginning of the file so it would probably work the same.

  • The latest release lets you access the Binary Data's ArrayBuffer in script. So I think the most flexible solution is to write the binary modification you want to do in JavaScript.

    I have to say though, if you've never done binary modification before, it sounds like you're going to have to learn a few things about that before you can get this working.

  • I finally managed to implement my solution in the app I was talking about. Just to confirm, the whole point was to edit metadata that consists exclusively of text without corrupting the binary parts of the files. The app loads each file inside a specified folder as binary data, then copies the data into a text string and uses the string to determine where the text is and where the actual binary data is in order to separate them. Then it edits the text, converts it back to binary and sticks the real binary data to it to re-assemble the file.

    I now was able to mass edit 400+ files in a matter of minutes and save loads of time and effort so thank you all for your help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)