#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
FILE *file,*fileout;
unsigned char data[1];
uint8_t what[] = {0x00, 0xEB, 0x00, 0x00, 0x50, 0xE3, 0x02};
uint8_t repl[] = {0x00, 0xEB, 0x01, 0x00, 0x37, 0xB3, 0x02};
uint8_t *buf_find(uint8_t *p, uint8_t *end, uint8_t *needle, int len)
{
end = end - len + 1;
while (p < end) {
if (memcmp(p, needle, len) == 0) return p;
p++;
}
return NULL;
}
file=fopen("test.bin","rb");
fileout=fopen("test.bin.bak","wb");
while (!feof(file)) {
if (fread(data, 1, 1, file) > 0) {
char *p = data;
char *end = data + 1;
for (;;) {
uint8_t *q = buf_find(p, end, what, sizeof(what));
if (q == NULL) break;
memcpy(q, repl, sizeof(repl));
p = q + sizeof(data[0]);
}
printf("%02X ",data[0]);
fwrite(&data,1,1,fileout);
}
}
fclose(file);
fclose(fileout);
return 0;
}
How to change a binary file hexadecimal character in a binary file? reading hex = "00 EB 00 00 50 E3 02" replace hex = "00 EB 01 00 37 E3 02"
my problem answered but it's wery slowly. Hexadecimal find and replace