0
#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

Community
  • 1
  • 1
Mehmet_
  • 5
  • 4
  • Mehmet, you read the file _byte by byte_ and try to replace the string after rading each byte? Of course it will be slow. Read the whole file or big chunks (as you did in your original question) and then run `buf_find` on the chunk. Or use the stream method in the answer below. – M Oehm Jan 17 '14 at 17:23

1 Answers1

1

try this

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


size_t replace(FILE *fi, FILE *fo, uint8_t *what, uint8_t *repl, size_t size){
    size_t i, index = 0, count = 0;
    int ch;
    while(EOF!=(ch=fgetc(fi))){
        if(ch == what[index]){
            if(++index == size){
                for(i = 0; i < size ; ++i){
                    fputc(repl[i], fo);
                }
                index = 0;
                ++count;
            }
        } else {
            for(i = 0; i < index ; ++i){
                fputc(what[i], fo);
            }
            index =0;
            fputc(ch, fo);
        }
    }
    for(i = 0; i < index ; ++i){
        fputc(what[i], fo);
    }

    return count;
}

int main(void){
    FILE *file,*fileout;
    uint8_t what[] = {0x00, 0xEB, 0x00, 0x00, 0x50, 0xE3, 0x02};
    uint8_t repl[] = {0x00, 0xEB, 0x01, 0x00, 0x37, 0xB3, 0x02};
    size_t count;

    file=fopen("test.bin","rb");
    fileout=fopen("test.bin.bak","wb");
    count = replace(file, fileout, what, repl, sizeof(what));
    printf("number of replace count is %zu\n", count);
    fclose(fileout);
    fclose(file);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • yess very well big thanks it's ok, well two what and two repl send function but what1 and repl1 ok what2 and repl2 failure:( – Mehmet_ Jan 17 '14 at 17:52
  • @Mehmet_ I'm not sure what you're saying. – BLUEPIXY Jan 17 '14 at 17:54
  • Hi BLUEPIXY I have this hex code "01 2C A0 E3 05 10 A0 E1 73 39 00 EB 00 00 50 E3 02" but "01 2C A0 E3 05 10" and "00 EB 00 00 50 E3 02" numbers are constant but " A0 E1 73 39 " numbers are variable. I want to replace with "A1 2C A0 E3 05 10 E0 00 73 38 00 EB 01 00 37 E3 02". – Mehmet_ Jan 22 '14 at 15:00
  • @Mehmet_ replace("01 2C A0 E3 05 10 A0 E1 73 39 00 EB 00 00 50 E3 02","A1 2C A0 E3 05 10 E0 00 73 38 00 EB 01 00 37 E3 02") – BLUEPIXY Jan 22 '14 at 21:23
  • you aren't understand me.. "A0 E1 73 39 " Hex adress are variable(changing) ETC : "01 2C A0 E3 05 10 00 EB 00 00 50 E3 02 ", "01 2C A0 E3 05 10 00 EB 00 00 50 E3 02 " , "01 2C A0 E3 05 10 00 EB 00 00 50 E3 02 " bla bla bla "A0 E1 73 39 " are changing in each file whatever the range of values ​​that I gave it my be "A1 2C A0 E3 05 10 E0 00 73 38 00 EB 01 00 37 E3 02". – Mehmet_ Jan 23 '14 at 01:09
  • @Mehmet_ I'm not sure what you're saying. – BLUEPIXY Jan 23 '14 at 01:16