change needed in _MemCopy used by BitmapFromMemory (m32lib)

Started by ToutEnMasm, July 31, 2006, 03:04:33 PM

Previous topic - Next topic

ToutEnMasm

The function is as follow


_MemCopy proc public uses esi edi Source:PTR BYTE,Dest:PTR BYTE,ln:DWORD

    ; ---------------------------------------------------------
    ; Copy ln bytes of memory from Source buffer to Dest buffer
    ;      ~~                      ~~~~~~           ~~~~
    ; USAGE:
    ; invoke _MemCopy,ADDR Source,ADDR Dest,4096
    ;
    ; NOTE: Dest buffer must be at least as large as the source
    ;       buffer otherwise a page fault will be generated.
    ; ---------------------------------------------------------

    cld
    mov esi, [Source]
    mov edi, [Dest]
    mov ecx, [ln]

    shr ecx, 2
    rep movsd

    mov ecx, [ln]
    and ecx, 3
    rep movsb

    ret

_MemCopy endp



IF the file size is a multiple of 4,there is a crash because ecx == 0 in "rep movsb"

the needed change is


_MemCopy proc public uses esi edi Source:PTR BYTE,Dest:PTR BYTE,ln:DWORD

    ; ---------------------------------------------------------
    ; Copy ln bytes of memory from Source buffer to Dest buffer
    ;      ~~                      ~~~~~~           ~~~~
    ; USAGE:
    ; invoke _MemCopy,ADDR Source,ADDR Dest,4096
    ;
    ; NOTE: Dest buffer must be at least as large as the source
    ;       buffer otherwise a page fault will be generated.
    ; ---------------------------------------------------------

    cld
    mov esi, [Source]
    mov edi, [Dest]
    mov ecx, [ln]

    shr ecx, 2
    rep movsd

    mov ecx, [ln]
    and ecx, 3
.if ecx != 0
    rep movsb
.endif

    ret

_MemCopy endp









MichaelW

I cannot detect any problem with the function. The Intel documents represent the logic of the REP prefix as a WHILE loop that starts with WHILE countReg != 0, and stepping through the instruction I can verify that ECX, ESI, and EDI are not altered, and that no data is moved, when ECX == 0 on entry.
eschew obfuscation