Solved

Reassemble binary file

Posted on 2001-08-16
17
419 Views
Last Modified: 2008-02-26
Please assist me.
How do I reassemble a file that I red in Binary ?
0
Comment
Question by:HESUS
  • 4
  • 4
  • 3
  • +3
17 Comments
 
LVL 3

Expert Comment

by:Bahnass
ID: 6393493

In Dos Mode
Debug was to read in assembly
is that what U mean
also there is DEBUGGERS to read assembly
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6393671
How did you DISassemble it? Please provide us with more information and/or Program code.. <smile>
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6393702
Disassembly is pretty easy, as Bahnass said, DEBUG in dos has a disassembler - eg
DEBUG c:\windows\mplayer.exe
-u
gives you a very readable disassembly of the first few bytes of the file:

19A8:0000 0E            PUSH    CS
19A8:0001 1F            POP     DS
19A8:0002 BA0E00        MOV     DX,000E
19A8:0005 B409          MOV     AH,09
19A8:0007 CD21          INT     21
19A8:0009 B8014C        MOV     AX,4C01
19A8:000C CD21          INT     21
19A8:000E 54            PUSH    SP
19A8:000F 68            DB      68
19A8:0010 69            DB      69
19A8:0011 7320          JNB     0033
19A8:0013 7072          JO      0087
19A8:0015 6F            DB      6F
19A8:0016 67            DB      67
19A8:0017 7261          JB      007A
19A8:0019 6D            DB      6D
19A8:001A 206361        AND     [BP+DI+61],AH
19A8:001D 6E            DB      6E
19A8:001E 6E            DB      6E
19A8:001F 6F            DB      6F


If you want to get back to the VB source code, then I'm afraid you just can't do this in VB4/5/6. No way. Nada. Never. Not on your nelly. But in VB.NET you can ;)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6393759
i think that HESUS is talking about "restoring".
If you have saved that binary data in an array of byte, do some like this:

Open "c:\yourchoice.???" for binary as #1
    put #1,1,YourByteArray()
close #1
0
 

Author Comment

by:HESUS
ID: 6393765
Thank you all for responding.
I did not explain myself correctly.
The procedure is as follows:
 
  Open 'File' For Binary As #1
 
  Open 'File2' For Binary As #2
   
  THE_LOF = LOF(1)
 
  While Not EOF(1)
    If (THE_LOF  Mod 100) = 0 Then
      N = 100
     Else
      N = (THE_LOF  Mod 100)
    End If
    cr = Input(N, #1)
    Put #2, N, cr
  Wend
 
  Close (1)
  Close (2)

What I get in File2 is a corrupted file.
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6393831
Oh, that's better, we all got the wrong end of the stick!

Still not sure what you're trying to do (a patcher?) - your code is definitely a bit strange.

What you're doing is getting chunks of file1 (based on the file length mod 100, so for a 156 byte file you're processing 56 character chunks). You're then writing the each chunk to the same position in the second file (eg at byte 56 onwards). I guess this isn't what you want!

try the following:


Open "File" For Binary As #1
 
 Open "File2" For Binary As #2

 'Strict typing Please!
 dim N as long
 dim THE_LOF as long
 dim lOffset as long
 THE_LOF = LOF(1)
 
 lOffset=1
 'can't use EOF when using input
 While lOffset<=THE_LOF
   'if there's more than 100 bytes to the end, copy 100 bytes
   If (lOffset <= THE_LOF - 100) = 0 Then
     N = 100
    Else
     'copy remaining bytes
     N = THE_LOF - lOffset + 1
   End If
   'get the bytes (this will increase the file pointer)
   cr = Input(N, #1)
   'put the bytes at the current offset
   Put #2, lOffset, cr
   lOffset=lOffset+N
 Wend
 
 Close (1)
 Close (2)


Note this is untested, but should give you some pointers.
I personally wouldn't use input (use GET), and I'd use a byte array as the intermediary rather than a string to save problems with double-byte character sets in NT. I'd also use freefile with variables rather than hardcoding file numbers.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6393844
Use this:
cr = InputB(N, #1)

instead of this:
cr = Input(N, #1)

0
 

Author Comment

by:HESUS
ID: 6393901
To andyclap thank you veru much.
The file that I am trying to copy is a .MDB file.
I am using your code,and when I am trying to read the file in Access, I get error message that the file is not an MDB
file.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6393967
HESUS, please, explain if it comes from a modem connection from other question of you. So, Experts could figure out entire picture.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6393968
The main problem I think is the use of input instead of inputb which will cause translation problems with certain binaray characters. By the way, why do you not just use FileCopy?
0
 

Author Comment

by:HESUS
ID: 6394000
Richie Simonetti is right.
I am transferring the file between two PCs by using a
modem connection.That is why I can not use FileCopy.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6394016
Using CHUNKS to process through the file (Chunks are very efficient when dealing with VERY large files 1mb+)

<----- Code Begin ----->

   Const CHUNK_SIZE = 1000 ' Size of chunks to process
   
   Dim lngFileGet As Long: lngFileGet = FreeFile
   Dim strFileGet As String: strFileGet = "c:\MyFileINPUT.mdb" ' <-- CHANGE
   Dim lngFilePut As Long: lngFilePut = FreeFile
   Dim strFilePut As String: strFilePut = "c:\MyFileOUTPUT.mdb" ' <-- CHANGE
   
'  Open Input File
   Open strFileGet For Binary As #lngFileGet
   
'  Open Output File
   If Not Dir(strFileGet) = "" Then
      Kill strFileGet
   End If
   Open strFilePut For Binary As #lngFilePut
   
'  Copy File in CHUNK_SIZE bites
   Dim lngBuffer As Long
   Dim strBuffer As String
   Dim lngFileLOF As Long: lngFileLOF = LOF(1)
   Dim lngFilePOS As Long: lngFilePOS = 1
   Do Until lngFilePOS > lngFileLOF
      If (lngFilePOS + CHUNK_SIZE) > lngFileLOF Then
         lngBuffer = lngFileLOF - lngFilePOS + 1
      Else
         lngBuffer = CHUNK_SIZE
      End If
      strBuffer = Space$(lngBuffer)
      Get #lngFileGet, lngFilePOS, strBuffer
      Put #lngFilePut, lngFilePOS, strBuffer
      lngFilePOS = lngFilePOS + lngBuffer
   Loop
 
'  Close Files
   Close (lngFileGet)
   Close (lngFilePut)

<----- Code End ----->

====================================
For smaller files.. all you need is:
====================================

<----- Code Begin ----->

   Dim lngFileGet As Long: lngFileGet = FreeFile
   Dim strFileGet As String: strFileGet = "c:\MyFileINPUT.mdb" ' <-- CHANGE
   Dim lngFilePut As Long: lngFilePut = FreeFile
   Dim strFilePut As String: strFilePut = "c:\MyFileOUTPUT.mdb" ' <-- CHANGE
   
'  Open Input File
   Open strFileGet For Binary As #lngFileGet
   
'  Open Output File
   If Not Dir(strFileGet) = "" Then
      Kill strFileGet
   End If
   Open strFilePut For Binary As #lngFilePut

'  Copy File
   Dim strBuffer As String: strBuffer = Space$(LOF(lngFileGet))
   Get #lngFileGet, , strBuffer
   Put #lngFilePut, , strBuffer
 
'  Close Files
   Close (lngFileGet)
   Close (lngFilePut)

<----- Code End ----->

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 50 total points
ID: 6394017
All binary methods, fixed and tested:

Public Sub sCopyFileByByte(strFileIn As String, strFileOut As String)

    Const ChunkSize = 4096
    Dim lngNumChunks As Long
    Dim bytbuffer() As Byte
    Dim hFileIn As Integer
    Dim hFileOut As Integer
    Dim lngLOF As Long
    Dim i As Long
   
    ReDim bytbuffer(1 To ChunkSize) As Byte
   
    hFileIn = FreeFile
    Open strFileIn For Binary As #hFileIn
    hFileOut = FreeFile
    Open strFileOut For Binary As #hFileOut
   
   
    lngLOF = LOF(hFileIn)
   
    For i = 1 To lngLOF \ ChunkSize + IIf(lngLOF Mod ChunkSize, 1, 0)
        If i = lngLOF \ ChunkSize + 1 Then
            ReDim bytbuffer(1 To lngLOF Mod ChunkSize)
        End If
        Get #hFileIn, , bytbuffer
        Put #hFileOut, , bytbuffer

    Next i
    Close hFileIn, hFileOut
   
End Sub
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6394026
Oooops... corrected:

====================================
For Larger files
====================================

<----- Code Begin ----->

  Const CHUNK_SIZE = 1000 ' Size of chunks to process
 
  Dim lngFileGet As Long: lngFileGet = FreeFile
  Dim strFileGet As String: strFileGet = "c:\MyFileINPUT.mdb" ' <-- CHANGE
  Dim lngFilePut As Long: lngFilePut = FreeFile
  Dim strFilePut As String: strFilePut = "c:\MyFileOUTPUT.mdb" ' <-- CHANGE
 
'  Open Input File
  Open strFileGet For Binary As #lngFileGet
 
'  Open Output File
  If Not Dir(strFilePut) = "" Then ' <-- Corrected
     Kill strFilePut ' <-- Corrected
  End If
  Open strFilePut For Binary As #lngFilePut
 
'  Copy File in CHUNK_SIZE bites
  Dim lngBuffer As Long
  Dim strBuffer As String
  Dim lngFileLOF As Long: lngFileLOF = LOF(1)
  Dim lngFilePOS As Long: lngFilePOS = 1
  Do Until lngFilePOS > lngFileLOF
     If (lngFilePOS + CHUNK_SIZE) > lngFileLOF Then
        lngBuffer = lngFileLOF - lngFilePOS + 1
     Else
        lngBuffer = CHUNK_SIZE
     End If
     strBuffer = Space$(lngBuffer)
     Get #lngFileGet, lngFilePOS, strBuffer
     Put #lngFilePut, lngFilePOS, strBuffer
     lngFilePOS = lngFilePOS + lngBuffer
  Loop

'  Close Files
  Close (lngFileGet)
  Close (lngFilePut)

<----- Code End ----->

====================================
For smaller files.. all you need is:
====================================

<----- Code Begin ----->

  Dim lngFileGet As Long: lngFileGet = FreeFile
  Dim strFileGet As String: strFileGet = "c:\MyFileINPUT.mdb" ' <-- CHANGE
  Dim lngFilePut As Long: lngFilePut = FreeFile
  Dim strFilePut As String: strFilePut = "c:\MyFileOUTPUT.mdb" ' <-- CHANGE
 
'  Open Input File
  Open strFileGet For Binary As #lngFileGet
 
'  Open Output File
  If Not Dir(strFilePut) = "" Then ' <-- Corrected
     Kill strFilePut ' <-- Corrected
  End If
  Open strFilePut For Binary As #lngFilePut

'  Copy File
  Dim strBuffer As String: strBuffer = Space$(LOF(lngFileGet))
  Get #lngFileGet, , strBuffer
  Put #lngFilePut, , strBuffer

'  Close Files
  Close (lngFileGet)
  Close (lngFilePut)

<----- Code End ----->

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6398938
HESUS, try this:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=26185
it could be useful to not break your head reading binaries.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6398946
0
 

Author Comment

by:HESUS
ID: 6405264
To all the experts,thank you very much for your help,and please accept my apologies for the delay.
I will accept PaulHews answer because it is the first one
I tested and it is doing the work.However,my problem is not completed ,because I need to transfare the file through
a Modem. But I will open a new question for that.(It is about time to pay somebody for his trouble,isn't it ?)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now