Patching a file

This is a discussion on Patching a file within the C++ Programming forums, part of the General Programming Boards category; Hi, can someone help me? I want to write a programm that patch a file at an specific offset, but ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Wink Patching a file

    Hi, can someone help me?
    I want to write a programm that patch a file at an specific offset, but i dont know how to do this. Im very happy when someone can help me!!
    thx

    (sorry for my bad english )

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    A general question like this probably means that you are new to C++ programming. (?)

    If you haven't done any C++ programming yet, then you have to learn the basics before attempting something like this. If you know some C++, then study-up on file I/O and write some code... then if you have problems, post your code here, and someone will help you to get your program working. If you haven't read the board guidelines yet, read them. (Nobody is going to write your program for you.)

    If all you want to do is change a few bytes in a file, then it would be easier to use a hex editor. I found a nice one free on the net, but I've forgotten the name, (and I don't have it with me here).

    And, make sure you have a backup of the original file before you start changing it!

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Patching a file can be done in binary file mode. All you need to know is where to start inserting the code. The location can be found by a careful analysis of your code in an assembly language listing or even a memory dump.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    hmm, your right. Im not an experience programmer, but im also not an "stupid" newbie .
    I dont want to use a HexEditor of course (for what im learning c++ !?), so anything i want to know is how to open a file (in my programm) and patch the file at an specific offset.
    If someone can post a little code i were very glad .

  5. #5
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    The word "an" is reserved for vowel sounds. For example, you can have "an arm", but not "an leg" or "an specific". Note it's vowel sounds, you can't for example have "an european" because it is pronounced with a 'u' sound.

    - Grammar Police.

    btw I'm guessing english isn't your first language, so don't be insulted or anything, it's just 1:14AM and I'm bored.

    also (to make my post relevant) is this kind of patching common? I guess it is for large files, but these days patches can be massive and not visibly change anything.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think this example will demonstrate techniques of overwriting data in a file. If the "patch" involves more than just overwriting, then the read to file/change data/write back to file method offers more flexibility. I have not run program to rule out logic errors or errors of other types.

    Code:
    ////////
    //myFile.dat
    ///////
    abdd
    
    //////////
    //myProgram.cpp
    /////////
    
    //goal---change abdd to abcd
    //assumption--desired position is 2 (zero based counting)
    
    #include <fstream>
    #include <string>
    int main()
    {
      //patch file method
    
      //open file for writing without truncating data.  
      ofstream fout("myFile.dat", ios::in | ios::out | ios::trunc);
    
      //first position ouput pointer to correct position
      fout.seekp(2, ios::beg);
    
      //then overwrite (patch) data at that position
      fout << c;
    
      fout.close();
    
      //change file data in program method
    
      //open stream to write and stream to read
      fout.open("myFile.dat", ios::in | ios::out | ios::trunc);
      ifstream fin("myFile.dat");
    
      string temp;
    
      //read data into program from file
      fin >> temp;
    
      //ovewrite data in program
      temp[2] = 'c';
    
      //return data to file from program
      fout << temp;
    
      fout.close();
      fin.close();
      
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    stupid noobie
    Just a point noobie's aren't stupid just ignorant. I have been studying programming for over a year now and will probably be a newbie for the next ten years if not life.

    btw this is an interesting topice, are the above methods used professionally like in games such as Planetside or AC2?

    Do the patches usually overwrite exisiting exe s or are they rebuilt somwhow without a compiler. I know they are all done via Networking so is there some other special technique.
    Last edited by curlious; 09-18-2003 at 11:34 AM.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I'd assume most patches by game creators are auto-generated; they probably do nothing more than compare the desired .exe with the pre-patched .exe and create a program to change one to the other.

    I've patched files in this manner before, for example, when I feel particularly ambitious I will write patches for games, especially old games that are no longer ever going to be supported by their manufacturers, and if I like the patches, I usually share an .EXE that does the patching.

    I also used to patch my own .EXEs many many years ago, because I always lost my damned manuals so I couldn't know what "word 4 in paragraph 2 on page 11 of the manual" was. In my defense, I legally owned all the games, and never distributed those patches, but I found the patching useful.
    Last edited by Cat; 09-18-2003 at 03:01 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Is there some utilitity that compares two exe files and creates a patch.exe to convert from one to the other if so could you please post a link.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    You could write one

    Is there some utilitity that compares two exe files and creates a patch.exe to convert from one to the other if so could you please post a link.
    I don't know of a program, but it would be fairly simple to write one if the patch only requires overwriting and/or appending. If you have to insert-and-shift... this might be a little more "interesting".

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    hehe, you right... my english is not very good .
    but thx for your help!!

    ----------------------------------------------------------------
    curlious: you looking for a file comparer-patcher?
    try:
    http://www.softnews.ro/public/cat/5/3/5-3-1.shtml

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21