I'm having roblem with a base64 encodin procedure . I found the code for it here http://www.asmcommunity.net/board/viewtopic.php?t=10311&highlight=base64

But when I encode a simple txt file saying :
HELLO WORLD I'm here !!!!!

the output is wrong :

SEVMTE7gVz8STEQgSSdsIGhlcmUgICEhISEh wrong one
!!!!!!!!!!!|!!!!||!!!!!!!!!!!!!!!!|!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SEVMTE8gV09STEQgSSdtIGhlcmUgICEhISEh right one tested it

and i can't find the error

here is the whole code :


.data
f2 db "enc.txt",0
f db "s.txt",0
nbr dd ?
ofs OFSTRUCT<>


szLut db "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr stuvwxyz0123456789+/"

.data?
dest db 100000 dup(?)
maph dd ?
MapV dd ?
MapV2 dd ?
ensize dd ?
fileh dd ?
fsize dd ?
.code
start:
invoke OpenFile,addr f,addr ofs,OF_READWRITE
mov fileh, eax
invoke GetFileSize,eax,fsize
invoke CreateFileMapping,fileh,0,PAGE_READWRITE,0,fsize,0
mov maph,eax
invoke MapViewOfFile,maph,FILE_MAP_READ,0,0,fsize
mov MapV,eax
invoke Base64EncodeFile,MapV,27,addr dest
invoke lstrlen,addr dest
mov ensize,eax
invoke CreateFile,addr f2,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
invoke WriteFile,eax,addr dest,ensize,addr nbr,0
invoke ExitProcess,0


Base64EncodeFile PROC USES EBX ESI EDI pFile:DWORD, dwLen:DWORD, pBase64:DWORD

xor eax, eax
mov edi, pBase64
mov esi, pFile
mov ecx, dwLen
mov edx, 19
xor ebx, ebx
cld

_loop:
mov al, byte ptr
and eax, 0FCh
sar eax, 2
stosb

lodsb
and eax, 3h
sal eax, 4
stosb
dec ecx
jz _done

dec edi
mov al, byte ptr
and eax, 0F0h
sar eax, 4
or byte ptr , al
inc edi

lodsb
and eax, 0Fh
sal eax, 2
stosb
dec ecx
jz _done

dec edi
mov al, byte ptr
and eax, 0C0h
sar eax, 6
or byte ptr , al
inc edi

lodsb
and eax, 03Fh
stosb

dec edx
jnz @F
mov edx, 19
mov ax, 0a0dh
stosw
xor eax, eax
add ebx, 2

@@:

dec ecx
jnz _loop

_done:

mov eax, edi
sub eax, pBase64
mov esi, eax ; esi = length of base64-encoded file

; table lookups


push ebx
push edi

mov edi, pBase64
mov ecx, esi
lea ebx, szLut
xor eax, eax
mov edx, 76

_lutLoop:
mov al, byte ptr
xlatb
stosb

dec edx
jnz @F ; every 76:th char is a 'CRLF', skip those
add edi, 2
dec ecx
jz _dooone
dec ecx
jz _dooone
mov edx, 76
@@:

dec ecx ; fara= ecx skulle kunna bli -1
jnz _lutLoop
_dooone:
pop edi
pop ebx


; Check if padding is needed

mov eax, esi
sub eax, ebx

mov eax, esi
xor edx, edx
mov ecx, 4
div ecx
test edx, edx
jz _noPad
sub ecx, edx
add esi, ecx ; add the nr. of pad-bytes to length of base64-encoded file
mov al, '='

rep stosb ; add ecx-nr. of "=" pad-bytes

_noPad:

mov eax, esi ; return length of base64-encoded file
ret

Base64EncodeFile ENDP

end start
Posted on 2004-09-24 03:43:46 by Gangleri
Going for the obvious..

due to the space between 'r' and 's' in the szLut string?
Posted on 2004-09-24 06:48:06 by drarem
Guess the saying "Even a peace of rubble can overturn the waggon" is right here .

Thanks I would never look for an error there .
Posted on 2004-09-24 06:56:07 by Gangleri