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




; Fast and pretty checksum algorithm :)
; IN: eax - current checksum, esi - data, ecx - data length
; OUT: eax - modified checksum
; Modified Regs: eax, ecx, edx
      
        .code

Adler32 proc uses esi ebx
        xor     edx, edx
        xor     ebx, ebx
        mov     dx, ax  ; dx = s1
        shr     eax, 16 ; ax = s2

@loop:
        mov     bl, [esi]
        add     edx, ebx
        cmp     edx, 65521
        jl      @F
        sub     edx, 65521
@@:
        add     eax, edx
        cmp     eax, 65521
        jl      @F
        sub     eax, 65521
@@:
        inc     esi
        dec     ecx
        jnz     @loop  

        shl     eax, 16
        or      ax, dx

        ret
Adler32 endp