jscripter
jscripter

Reputation: 840

Edit and add bytes to binary(uint8array)

I want to add a string and some binaries like 0x00 inside my binary file which I loaded via ajax (to get it as binary)

This is my code until now:

var ajax = new XMLHttpRequest();
ajax.open("GET", "test.bin", true);
ajax.responseType = "arraybuffer";

ajax.onload = function () {
    var byteArray = new Uint8Array(ajax.response);
    // What can i do?
};

ajax.send();

Upvotes: 2

Views: 3551

Answers (2)

jscripter
jscripter

Reputation: 840

Well, at last I could resolve it:

var str = 'Injected!!!';

var injAr = [0x21,0xFE,str.length]; 
for (var i = 0; i < str.length; i++) { injAr.push(str.charCodeAt(i));}
injAr.push(0x00,0x3B);

var ajax = new XMLHttpRequest();
ajax.open("GET", "test.gif", true);
ajax.responseType = "arraybuffer";

ajax.onload = function () {
    var bAr = new Uint8Array(ajax.response), len = bAr.length - 1;
    window.newBAr = new Uint8Array(len + injAr.length);
    for (var i = 0; i < len; i++) { newBAr[i] = bAr[i];}
    for (var i = 0; i < injAr.length; i++) { newBAr[len + i] = injAr[i];}
}
ajax.send();

Well, this was intended to add comments blocks to a gif correctly, but it can be use also with jpgs, pngs, for example to modify or add exif metadata

Upvotes: 2

yxre
yxre

Reputation: 3704

You will need to have the reqponse in a format that the javascript will be able to pull out the bytes that are the string and seperate the bytes that are part of the binary. You can do this by positioning the string at the beginning or end with a distinctive byte flag to signify the boundary. I would put the string at the beginning with the flag at the end of it to signify the beginning of the binary since I am unsure of the binary file.

Once you have it seperated, you will need to convert the bytes to numbers and then convert the numbers to characters. Here is a question that goes over how to convert binary to characters.

Upvotes: 1

Related Questions