This is ofcourse the opposite of the encoding algo found in my base64 encoding topic: Step 1. Grab 4 bytes from the in-stream Step 2. Convert each byte into a 6bit value using a table Step 3. Merge the four 6bit vaules into 3 bytes Step 4. Write to out-stream Step 5. Repeat Note: the = padding chars should be ignored or decoded to nulls like in my version of the algo.

.486
.model flat, stdcall
.data
        ; ASCII -> BASE64 mapping table (do not modify)
        base64table     db 43 dup (255)
                        db 62,255,255,255,63,52,53,54,55,56,57,58,59,60,61,255
                        db 255,255,0,255,255,255,0,1,2,3,4,5,6,7,8,9,10,11,12,13
                        db 14,15,16,17,18,19,20,21,22,23,24,25,255,255,255,255
                        db 255,255,26,27,28,29,30,31,32,33,34,35,36,37,38
                        db 39,40,41,42,43,44,45,46,47,48,49,50,51
                        db 132 dup (255)

.code
base64decode PROC source:DWORD, destination:DWORD, sourcelen:DWORD
        push   esi
        push   edi
        push   ebx
        
        mov    esi, source              ; esi <- source
        mov    edi, destination         ; edi <- destination
        mov    ecx, sourcelen
        shr    ecx, 2                   ; ecx = # of quadruples in in-stream
        cld
        
        ;----------------------------
        
        xor    edx, edx
@@outer_loop:
        push   ecx
        mov    ecx, 4                   ; loop 4 times, once for each byte in quadruple
        xor    ebx, ebx
        lodsd                           ; fetch the 4 bytes
@@inner_loop:
        push   eax
        and    eax, 0ffh                ; mask out all except one char
        cmp    al, "="                  ; is it a padding char?
        jne    @F                       ; no, continue with decode
        inc    edx                      ; yes, so we decrease the outbyte counter by 1
@@:
        mov    al, byte ptr 
        cmp    al, 255                  ; is the char invalid?
        je     @@invalid_char           ; yes, exit
        shl    ebx, 6                   ; no, decode the char
        or     bl, al                   ; "      "    "    "
        pop    eax
        shr    eax, 8                   ; prepare next byte in quadruple
        dec    ecx
        jnz    @@inner_loop             ; loop
        mov    eax, ebx
        shl    eax, 8                   ; eax now contains up to 3 decoded chars
        bswap  eax
        stosd
        dec    edi
        pop    ecx
        dec    ecx
        jnz    @@outer_loop             ; loop and get next quadruple
        mov    eax, sourcelen
        mov    ecx, edx
        shr    eax, 2
        mov    ebx, 3
        mul    ebx
        sub    eax, ecx
        jmp    @@decode_done
        
        ;-------------------------------------------
        
@@invalid_char:
        mov    eax, -1
@@decode_done:
        pop    ebx
        pop    edi
        pop    esi
        ret
base64decode ENDP
Comments? My version vill return the lenght of the decoded data on sucess or -1 if an invalid character was found in the stream. This message was edited by Zynaps, on 5/1/2001 12:18:26 PM
Posted on 2001-05-01 12:15:00 by Zynaps
Just curious... what is base64 used for?
Posted on 2001-05-09 14:47:00 by asm_happy
It's an encoding algo (not crypting). Tell me if i'm wrong, but it's used in uuencode/mails (attached files i think)...
Posted on 2001-05-09 15:42:00 by Readiosys
That's correct. It is also used by browsers in some cases. Im sure you have all seen the gray IE/NS login window. The username and password you enter in it is sent encoded with base64. The real point with these encoding algos is ofcourse to encode binary data into printable characters only so that the data does not interfere with text based protocols like SMTP, POP3, NNTP and HTTP to name a few :)
Posted on 2001-05-10 05:47:00 by Zynaps
mov eax, sourcelen ; mov ecx, edx shr eax, 2 lea eax, ; mov ebx, 3 ; mul ebx ; sub eax, ecx sub eax,edx jmp @@decode_done
Posted on 2001-05-13 16:54:00 by The Svin