why is this masm assembly code not working in a loop, code works perfectly the first

patchariadog

Junior Member
Mar 31, 2013
3
0
0
hey I have been using masm for 2 weeks now and I am trying to read from a text file line by line that has paths of files in them

example of text file
C:\a.rar
C:\a.txt
C:\a.png

then I want to read in the whole contents of the file path and get the md5 check sum of the file path

the below code works perfectly for the first time(the first message box is the file path,the second is the contents of the file and the third is the md5 check sum)

but then after the first loop it reads the second files path but can not read the contents of the second file and then crashes because it has nothing to md5 check sum.

it must be a easy mistake of not resetting something or not closing something but I have spent like 20 hours on this and can not get it to work

for example the below code is in a button and when you press the button this is what it is supposed to do

message box C:\a.rar
message box "contents of file"
message box 44644af7515bc4870d44fa688684798
message box C:\a.txt
message box "contents of file"
message box 6057f13c496ecf7fd777ceb9e79ae285
message box C:\a.png
message box "contents of file"
message box 01654ab48d84f484z446ba48453cb48

but this is what happens
message box C:\a.rar
message box "contents of file"
message box 44644af7515bc4870d44fa688684798
message box C:\a.txt
blank contents cant read the contents of the file on loop (this is the problem)
message box blank because it cant md5 no contents

crash

can someone please help
Code:
LOCAL Buffer3  :DWORD

invoke CreateFile, ADDR filestoscan, GENERIC_READ, 0, 0,OPEN_EXISTING, 0, 0
mov hFile, eax

invoke GetFileSize,hFile,NULL
mov Byteforstreamreader,eax
streamreader2:
.if Byteforstreamreader != 0
invoke ReadFile, hFile, ADDR Buffer2,1, ADDR BytesWritten, 0
.if Buffer2 == 13





invoke CreateFile, ADDR StringBuffer, GENERIC_READ, 0, 0,OPEN_EXISTING, 0, 0
mov hFile2, eax

invoke GetFileSize,hFile2,NULL
mov Bytes,eax

invoke ReadFile, hFile2, ADDR FileBuffer,Bytes, ADDR BytesWritten, 0
invoke CloseHandle,hFile2






      invoke MessageBoxA, NULL, addr StringBuffer, offset BoxCaption, NULL 
      invoke MessageBoxA, NULL, addr FileBuffer, offset BoxCaption, NULL 
      
            
                  

                        

            
           invoke MD5_Startup
           invoke MD5_Init, offset ctxt
           invoke MD5_Read, offset ctxt, offset FileBuffer, Bytes
           invoke MD5_Digest, offset ctxt, offset filehash
      invoke MD52String, offset filehash, offset strn, 1
      
      
      invoke MessageBoxA, NULL, addr strn, offset BoxCaption, NULL 

      
      





mov FileBuffer,0 
mov StringBuffer,0
dec Byteforstreamreader
jmp streamreader2
.endif
mov eax,offset Buffer2
mov Buffer3,eax



invoke lstrcat,ADDR StringBuffer,addr Buffer2

dec Byteforstreamreader
jmp streamreader2
.endif
.if Byteforstreamreader == 0
invoke CloseHandle,hFile
.endif

.data
filestoscan      db "myfiles.txt",0
FileBuffer        DB 50000 DUP(?)
Bytes dd ?
Bytes2 dd ?
BytesWritten dd ?
BytesWritten3 dd ?
hFile dd ?
hFile2 dd ?

.data ?
hFile dd ?
Byteforstreamreader dd ?  
BytesWritten2 dd ?
StringBuffer DB 100 DUP(?)
Buffer2  db 500 dup (?)
ctxt db 1000 dup (?)
filehash db 1000 dup (?)
strn db 33 dup(?) ; use dw for unicode
also as a side question if someone could please answer it does not seem right to me to have to reserve 50000 bytes on filebuffer so I can open a 50000 bytes or smaller file. how can I open any file size without reserving all that memory because some of these files may be 100 mb or more

thank you
 
Last edited:

patchariadog

Junior Member
Mar 31, 2013
3
0
0
someone showed me how to fix it just add this line
Code:
invoke ReadFile, hFile, ADDR Buffer2,1, ADDR BytesWritten, 0
under this line

Code:
.if Buffer2 == 13
full code

Code:
invoke CreateFile, ADDR filestoscan, GENERIC_READ, 0, 0,OPEN_EXISTING, 0, 0
    mov hFile, eax
    
    invoke GetFileSize,hFile,NULL
    mov Byteforstreamreader,eax
    streamreader2:
    .if Byteforstreamreader != 0
        invoke ReadFile, hFile, ADDR Buffer2,1, ADDR BytesWritten, 0
    
        .if Buffer2 == 13
        
        invoke ReadFile, hFile, ADDR Buffer2,1, ADDR BytesWritten, 0
    
        
    invoke CreateFile, ADDR StringBuffer, GENERIC_READ, 0, 0,OPEN_EXISTING, 0, 0
    mov hFile2, eax
    
    invoke GetFileSize,hFile2,NULL
    mov Bytes,eax
    
    invoke ReadFile, hFile2, ADDR FileBuffer,Bytes, ADDR BytesWritten, 0
    invoke CloseHandle,hFile2
    
    
    
          invoke MessageBoxA, NULL, addr StringBuffer, offset BoxCaption, NULL 
          invoke MessageBoxA, NULL, addr FileBuffer, offset BoxCaption, NULL 
          
                       
    
                
               invoke MD5_Startup
               invoke MD5_Init, offset ctxt
               invoke MD5_Read, offset ctxt, offset FileBuffer, Bytes
               invoke MD5_Digest, offset ctxt, offset filehash
          invoke MD52String, offset filehash, offset strn, 1
          
          
          invoke MessageBoxA, NULL, addr strn, offset BoxCaption, NULL 
    
          
    
    
    mov FileBuffer,0
            mov StringBuffer,0
            dec Byteforstreamreader
            jmp streamreader2
        .endif
    
    
    
    ; append the character just read from hFile to StringBuffer
        invoke lstrcat,ADDR StringBuffer,addr Buffer2
    
        dec Byteforstreamreader
        jmp streamreader2
    .endif
    
    
    .if Byteforstreamreader == 0
    invoke CloseHandle,hFile
    .endif
now i just need help with the reserve data part of my question

FileBuffer DB 50000 DUP(?)
this will only let me md5 a file under 50000 bytes

because if i have a file that is 20mb big how do I read that into FileBuffer because you cant reserve 20971520 bytes(20mb) of it, and if i split it into chunks how would I join them into one chunk to md5 it. maybe if or anyone has a good way to do this they could post it. thanks!