1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| ; Encodes data to base64 format
Base64Encode proc uses esi edi ebx lpSrc, lpDst, dwSrcLen: DWORD
mov esi, lpSrc
mov edi, lpDst
mov ecx, dwSrcLen
xor ebx, ebx
@l:
jecxz @b64_ret
lodsb
shl eax, 16
cmp ecx, 1
jz @work
lodsb
shl ax, 8
cmp ecx, 2
jz @work
lodsb
@work:
; Output b64 quad
mov edx, eax
ror edx, 24
push ecx
push 4
pop ecx
@l2:
call @b64_write
loop @l2
pop ecx
inc ebx
.IF ebx == 18
xor ebx, ebx
mov ax, 0a0dh
stosw
.ENDIF
sub ecx, 3
jns @l
; Pad
neg ecx
sub edi, ecx
mov al, '='
rep stosb
@b64_ret:
ret
@b64_write:
rol edx, 6
mov eax, edx
and al, 00111111b
cmp al, 62
jae @write_spec
cmp al, 52
jae @write_number
; Uppercase
add al, 'A'
cmp al, 'A'+26
jb @write
; Lowercase
add al, 6
jmp @write
; Number
@write_number:
add al, '0'-52
jmp @write
; Special: +/
@write_spec:
sub al, 62
shl al, 2
add al, 43
@write:
stosb
retn
Base64Encode endp |
Partager