Difference between revisions of "Z80 Routines:Security:CRC32"

From WikiTI
Jump to: navigation, search
m
m (Memory Usage)
 
Line 10: Line 10:
 
These routines use RAM that is also used by the built-in MD5 routines.
 
These routines use RAM that is also used by the built-in MD5 routines.
  
The code takes 149 bytes in your program.
+
The code takes 147 bytes in your program.
  
 
== The code ==
 
== The code ==

Latest revision as of 08:31, 29 May 2006


Notes from the author

I (84plusfreak) made this routine on-calc. If you find an error, please tell me (serninpc at gmail dot com).

Memory Usage

These routines use RAM that is also used by the built-in MD5 routines.

The code takes 147 bytes in your program.

The code

CRC32Init

CRC32Init:
 ;Inputs:
 ; A=0 -> Use 'default' Polynomial (the one used in the ZIP Format)
 ; A=1 -> Use custom Polynomial
 ;     -> DEHL=Polynomial
 ;Outputs:
 ; ($8292)=Polynomial
 ; ($8296)=Current CRC (~($8296)=CRC32)
 ;Destroys:
 ; AF,DE,HL
 or a
 jr nz,$+08
 ld de,$EDB8
 ld hl,$8320
 ld a,d
 ld d,e
 ld e,a
 ld a,h
 ld h,l
 ld l,a
 ld ($8292),de
 ld ($8294),hl
 ld hl,$FFFF
 ld ($8296),hl
 ld ($8298),hl
 ret

CRC32Update

CRC32Update:
 ;Inputs:
 ; HL -> Points to data
 ; BC -> Number of bytes
 ;Outputs:
 ; ($8296) = Updated
 ;Destroys:
 ; AF,BC,DE,HL
 ld a,b
 or c
 ret z
 push hl
 push bc
 ld b,(hl)
 call CRC32Update_Routine
 pop bc
 pop hl
 inc hl
 dec bc
 jr CRC32Update
CRC32Update_OneByte:
 ;Inputs:
 ; A = Byte to add to CRC
 ;Destroys:
 ; AF,BC,DE,HL
 ld b,a
CRC32Update_Routine:
 ld a,($8296)
 xor b
 call CRC32TableLookup
 ld a,($8297)
 xor l
 ld ($8296),a
 ld a,($8298)
 xor h
 ld ($8297),a
 ld a,($8299)
 xor e
 ld ($8298),a
 ld a,d
 ld ($8299),a
 ret
CRC32TableLookup:
 ld de,$0000
 ld h,d
 ld l,a
 ld b,8
CRC32TableLookupLoop:
 push bc
 or a
 rr d
 rr e
 rr h
 rr l
 jr nc,CRC32TableLookupLoopEnd
 push hl
 pop bc
 ld hl,$8292
 ld a,d
 xor (hl)
 inc hl
 ld d,a
 ld a,e
 xor (hl)
 inc hl
 ld e,a
 ld a,b
 xor (hl)
 inc hl
 ld b,a
 ld a,c
 xor (hl)
 ld c,a
 push bc
 pop hl
CRC32TableLookupLoopEnd:
 pop bc
 djnz CRC32TableLookupLoop
 ret

CRC32Finalize

CRC32Finalize:
 ;Inputs:
 ; None
 ;Outputs:
 ; DEHL = CRC32
 ;Destroys:
 ; AF,BC,DE,HL
 ld de,($8298)
 ld bc,($8296)
 ld hl,$FFFF
 push hl
 or a
 sbc hl,bc
 ex (sp),hl
 sbc hl,de
 ex de,hl
 pop hl
 ret