none
C# reading binary file or byte array to EOF

    Question

  • How can this be done to read a executable file with a binary reader to the EOF (end of file) marker:

    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("resource.exe");
    BinaryReader reader = new BinaryReader(stream);
    byte[] buffer = reader.ReadBytes((int)stream.Length);
    reader.Close();


    I believe this reads everything but not to the EOF of the executable file
    Wednesday, March 03, 2010 9:54 AM

Answers

  • Hi,

    I have had this problem reading binary files in a number of languages. I'm not sure there is going to be a complete solution, typically readers do not read the EOF, because they will write it when you save the file, and in it's strictest sense the EOF marker at the end of the file is not actually part of the file (if it did read in the EOF character, then when you wrote the file back to disk it would get two EOF characters)

    Why do you need the file complete with the EOF? have you tried just adding it to the end of your buffer? (0x0000)

    Cheers,

    Simon.
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Thursday, March 04, 2010 6:34 AM
  • Hi,

    You can use PeekChar method to check the next available character, or -1 if no more characters are available.

    Something like:
      while (br.PeekChar() != -1)
          {
                // Read and process the information.
          }

          br.Close();

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Tuesday, March 09, 2010 7:00 AM
  • Sorry, but why you want to read a thing wheras you know what is that? is there a reasonable issue which we don't know?
    You know that if you read it then it will be EOF signature so you can simply skip it and add it yourself as SimonRB mentioned.
    Or please let us know if we aren't right.
    Thanks.
    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Tuesday, March 09, 2010 7:20 AM

All replies

  • Here is my problem:
    Some binary files have "PADDINGXXXPADDING...etc" or other empty useless bytes in the file that are not actually executed when the file is run via double clicking it.

    From my understanding it used to increase the memory space for the program should it later need use of it but it causes a problem for me, if I read a binary file into an array then execute it directly into memory it causes the process to crash since the extra bytes create a problem.

    Here is what I have so far:
    I can successfully take a binary file and read the whole contents into a binary array then execute the binary array into a memory allocation using the following :

    //Resource is read into a stream
     Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("someinternalprogram.exe");
                        BinaryReader reader = new BinaryReader(stream);
    
                        byte[] buffer = reader.ReadBytes((int)stream.Length);
                        reader.Close();
    
    //byte array is read exactly from file then is executed:
    
    MemExec(buffer, Application.ExecutablePath);
    /* 
    MemExec is a method that will execute the binary array
    using the following:
    ReadProcessMemory (from win32 api)
    WriteProcessMemory (win32)
    VirtualAllocEx
    VirtualProtectEx
    
    Writing the binary array into a process simulating the actions of the file as if it were double clicked
    */
    

    Note: This seems to work for most executables but appears to have problems with some that have PaddingXXX etc. interestingly enough notepad has padding at EOF but still works fine with it.
    • Merged by Harry Zhu Wednesday, March 10, 2010 2:51 AM
    Thursday, March 04, 2010 3:14 AM
  • Hi,

    I have had this problem reading binary files in a number of languages. I'm not sure there is going to be a complete solution, typically readers do not read the EOF, because they will write it when you save the file, and in it's strictest sense the EOF marker at the end of the file is not actually part of the file (if it did read in the EOF character, then when you wrote the file back to disk it would get two EOF characters)

    Why do you need the file complete with the EOF? have you tried just adding it to the end of your buffer? (0x0000)

    Cheers,

    Simon.
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Thursday, March 04, 2010 6:34 AM
  • Did you put an EOF character in the file you're reading?
    Thursday, March 04, 2010 8:21 AM
  • Hi,

    You can use PeekChar method to check the next available character, or -1 if no more characters are available.

    Something like:
      while (br.PeekChar() != -1)
          {
                // Read and process the information.
          }

          br.Close();

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Tuesday, March 09, 2010 7:00 AM
  • Sorry, but why you want to read a thing wheras you know what is that? is there a reasonable issue which we don't know?
    You know that if you read it then it will be EOF signature so you can simply skip it and add it yourself as SimonRB mentioned.
    Or please let us know if we aren't right.
    Thanks.
    My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010
    • Marked as answer by Bin-ze Zhao Thursday, March 11, 2010 2:11 AM
    Tuesday, March 09, 2010 7:20 AM
  • Maybe the problem is that you're expecting an EOF character to be in the file without having put it there.

    Tuesday, March 09, 2010 8:46 AM
  • So, basically, you want to write a virus.
    Friday, October 22, 2010 1:49 PM