Search code examples
windowsassemblymasmmasm32

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


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

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


Solution

  • Here's a somewhat annotated version of your streamreader2 loop:

    streamreader2:
    .if Byteforstreamreader != 0
        invoke ReadFile, hFile, ADDR Buffer2,1, ADDR BytesWritten, 0
    
        ; check for end-of-line (CR)
        .if Buffer2 == 13
    
            ; open the file named in Buffer2 and 
            ; do a bunch of stuff to it to get the MD5
            ;
            ; ...
    
            mov FileBuffer,0
            mov StringBuffer,0
            dec Byteforstreamreader
            jmp streamreader2
        .endif
    
        ; I don't know what the purpose of these two lines are
        mov eax,offset Buffer2
        mov Buffer3,eax
    
        ; append the character just read from hFile to StringBuffer
        invoke lstrcat,ADDR StringBuffer,addr Buffer2
    
        dec Byteforstreamreader
        jmp streamreader2
    .endif
    

    So no characters read from hFile are handled specially except for 13 (CR). However, I think there's a good chance that the line endings in that file are CRLF so when the next file is opened the name will have a LF control character at the start of it. So the CreateFile or fopen call will fail to open the file.