Skip to content
Advertisement

JAVA – How to replace string in a binary file (.exe) using regex pattern [closed]

I need to read a file named “chromedriver.exe” and replace all the occurrences of a string that starts with “cdc_” and is 26 characters long. So, my regex is “cdc_.{22}”. That matches a string that starts with “cdc_” and after that has 22 characters. (example of the string -> cdc_kwjeorialeksjeiwRTkwjr)

My replacement will be this string of 26 characters “plp_roepstdlwoeproslPOweos”.

I have this code in python (not mine) that does what I described above, but I need to convert it in Java. So the question is: how do I do this in Java? Please help.

import io
import re
import string

replacement = "plp_roepstdlwoeproslPOweos".encode()

with io.open("chromedriver.exe", "r+b") as fh:
    for line in iter(lambda: fh.readline(), b""):
        if b"cdc_" in line:
            fh.seek(-len(line), 1)
            newline = re.sub(b"cdc_.{22}", replacement, line)
            fh.write(newline)

Advertisement

Answer

This is a quick and dirty approach. Read the file into a byte array, search for the text, replace if found, then write back to the file.

private void replace(String filename) throws Exception
{
    File file=new File(filename);
    int length=(int)file.length();
    byte[] data;
    try(FileInputStream in = new FileInputStream(file); 
        ByteArrayOutputStream bs=new ByteArrayOutputStream(length))
    {
        byte[] buffer=new byte[128_000];
        int len=0;
        while((len=in.read(buffer))>0)
            bs.write(buffer,0,len);
        in.close();
        bs.close();
        data=bs.toByteArray();
    }
    searchAndReplace(data);
    
    try(FileOutputStream out=new FileOutputStream(file);
        ByteArrayInputStream bs=new ByteArrayInputStream(data))
    {
        byte[] buffer=new byte[128_000];
        int len=0;
        while((len=bs.read(buffer))>0)
            out.write(buffer,0,len);
        bs.close();
        out.flush();
        out.close();
    }
}

private void searchAndReplace(byte[] data)
{
    byte[] replacements="plp_roepstdlwoeproslPOweos".getBytes(StandardCharsets.US_ASCII);
    byte[] first="cdc_".getBytes(StandardCharsets.US_ASCII);
    Pattern test=Pattern.compile("cdc_.{22}");
    
    for(int i=0;i<data.length-replacements.length;i++)
    {
        if(data[i]==first[0] && data[i+1]==first[1] && data[i+2]==first[2] && data[i+3]==first[3]) // check for consecutive bytes 
        {
            String text=new String(data, i, replacements.length, StandardCharsets.US_ASCII);
            if(test.matcher(text).matches()) // found it
            {
                System.arraycopy(replacements, 0, data, i, replacements.length);
                i+=replacements.length;
            }
        }
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement