udfCrc32Calculate (hbbcrctable, hbbdata)
;------------------------------------------------------------------------------------------------------------------------------------------
; CRC32 calculation for WinBatch, returns CRC32 hexstr identical to PKZIP or WINZIP calculations.
; Based on some free C source code, translated by Marty Williams.
; WindowWare Tech Database article 'CRC32 Computations', File Created: 2001:12:11:13:05:14
; Modified by Detlev Dalitz.20011212.20020626
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
; udfCrc32CreateTable ()
; udfCrc32SaveTable (hbbcrctable, filename)
; udfCrc32LoadTable (filename)
; udfCrc32LoadFile (filename)
; udfCrc32LoadStr (str)
; udfCrc32Calculate (hbbcrctable, hbbdata)
; udfDecToHex (dec, padlength, uppercaseflag)
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32createtable",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32createtable

#DefineFunction udfCrc32CreateTable ()
hbb = BinaryAlloc(1024)
BinaryPoke4(hbb,0,0)               ; 00000000
BinaryPoke4(hbb,4,1996959894)      ; 77073096
BinaryPoke4(hbb,8,-301047508)      ; ee0e612c
BinaryPoke4(hbb,12,-1727442502)    ; 990951ba
BinaryPoke4(hbb,16,124634137)      ; 076dc419
BinaryPoke4(hbb,20,1886057615)     ; 706af48f
BinaryPoke4(hbb,24,-379345611)     ; e963a535
BinaryPoke4(hbb,28,-1637575261)    ; 9e6495a3
BinaryPoke4(hbb,32,249268274)      ; 0edb8832
BinaryPoke4(hbb,36,2044508324)     ; 79dcb8a4
BinaryPoke4(hbb,40,-522852066)     ; e0d5e91e
BinaryPoke4(hbb,44,-1747789432)    ; 97d2d988
BinaryPoke4(hbb,48,162941995)      ; 09b64c2b
BinaryPoke4(hbb,52,2125561021)     ; 7eb17cbd
BinaryPoke4(hbb,56,-407360249)     ; e7b82d07
BinaryPoke4(hbb,60,-1866523247)    ; 90bf1d91
BinaryPoke4(hbb,64,498536548)      ; 1db71064
BinaryPoke4(hbb,68,1789927666)     ; 6ab020f2
BinaryPoke4(hbb,72,-205950648)     ; f3b97148
BinaryPoke4(hbb,76,-2067906082)    ; 84be41de
BinaryPoke4(hbb,80,450548861)      ; 1adad47d
BinaryPoke4(hbb,84,1843258603)     ; 6ddde4eb
BinaryPoke4(hbb,88,-187386543)     ; f4d4b551
BinaryPoke4(hbb,92,-2083289657)    ; 83d385c7
BinaryPoke4(hbb,96,325883990)      ; 136c9856
BinaryPoke4(hbb,100,1684777152)    ; 646ba8c0
BinaryPoke4(hbb,104,-43845254)     ; fd62f97a
BinaryPoke4(hbb,108,-1973040660)   ; 8a65c9ec
BinaryPoke4(hbb,112,335633487)     ; 14015c4f
BinaryPoke4(hbb,116,1661365465)    ; 63066cd9
BinaryPoke4(hbb,120,-99664541)     ; fa0f3d63
BinaryPoke4(hbb,124,-1928851979)   ; 8d080df5
BinaryPoke4(hbb,128,997073096)     ; 3b6e20c8
BinaryPoke4(hbb,132,1281953886)    ; 4c69105e
BinaryPoke4(hbb,136,-715111964)    ; d56041e4
BinaryPoke4(hbb,140,-1570279054)   ; a2677172
BinaryPoke4(hbb,144,1006888145)    ; 3c03e4d1
BinaryPoke4(hbb,148,1258607687)    ; 4b04d447
BinaryPoke4(hbb,152,-770865667)    ; d20d85fd
BinaryPoke4(hbb,156,-1526024853)   ; a50ab56b
BinaryPoke4(hbb,160,901097722)     ; 35b5a8fa
BinaryPoke4(hbb,164,1119000684)    ; 42b2986c
BinaryPoke4(hbb,168,-608450090)    ; dbbbc9d6
BinaryPoke4(hbb,172,-1396901568)   ; acbcf940
BinaryPoke4(hbb,176,853044451)     ; 32d86ce3
BinaryPoke4(hbb,180,1172266101)    ; 45df5c75
BinaryPoke4(hbb,184,-589951537)    ; dcd60dcf
BinaryPoke4(hbb,188,-1412350631)   ; abd13d59
BinaryPoke4(hbb,192,651767980)     ; 26d930ac
BinaryPoke4(hbb,196,1373503546)    ; 51de003a
BinaryPoke4(hbb,200,-925412992)    ; c8d75180
BinaryPoke4(hbb,204,-1076862698)   ; bfd06116
BinaryPoke4(hbb,208,565507253)     ; 21b4f4b5
BinaryPoke4(hbb,212,1454621731)    ; 56b3c423
BinaryPoke4(hbb,216,-809855591)    ; cfba9599
BinaryPoke4(hbb,220,-1195530993)   ; b8bda50f
BinaryPoke4(hbb,224,671266974)     ; 2802b89e
BinaryPoke4(hbb,228,1594198024)    ; 5f058808
BinaryPoke4(hbb,232,-972236366)    ; c60cd9b2
BinaryPoke4(hbb,236,-1324619484)   ; b10be924
BinaryPoke4(hbb,240,795835527)     ; 2f6f7c87
BinaryPoke4(hbb,244,1483230225)    ; 58684c11
BinaryPoke4(hbb,248,-1050600021)   ; c1611dab
BinaryPoke4(hbb,252,-1234817731)   ; b6662d3d
BinaryPoke4(hbb,256,1994146192)    ; 76dc4190
BinaryPoke4(hbb,260,31158534)      ; 01db7106
BinaryPoke4(hbb,264,-1731059524)   ; 98d220bc
BinaryPoke4(hbb,268,-271249366)    ; efd5102a
BinaryPoke4(hbb,272,1907459465)    ; 71b18589
BinaryPoke4(hbb,276,112637215)     ; 06b6b51f
BinaryPoke4(hbb,280,-1614814043)   ; 9fbfe4a5
BinaryPoke4(hbb,284,-390540237)    ; e8b8d433
BinaryPoke4(hbb,288,2013776290)    ; 7807c9a2
BinaryPoke4(hbb,292,251722036)     ; 0f00f934
BinaryPoke4(hbb,296,-1777751922)   ; 9609a88e
BinaryPoke4(hbb,300,-519137256)    ; e10e9818
BinaryPoke4(hbb,304,2137656763)    ; 7f6a0dbb
BinaryPoke4(hbb,308,141376813)     ; 086d3d2d
BinaryPoke4(hbb,312,-1855689577)   ; 91646c97
BinaryPoke4(hbb,316,-429695999)    ; e6635c01
BinaryPoke4(hbb,320,1802195444)    ; 6b6b51f4
BinaryPoke4(hbb,324,476864866)     ; 1c6c6162
BinaryPoke4(hbb,328,-2056965928)   ; 856530d8
BinaryPoke4(hbb,332,-228458418)    ; f262004e
BinaryPoke4(hbb,336,1812370925)    ; 6c0695ed
BinaryPoke4(hbb,340,453092731)     ; 1b01a57b
BinaryPoke4(hbb,344,-2113342271)   ; 8208f4c1
BinaryPoke4(hbb,348,-183516073)    ; f50fc457
BinaryPoke4(hbb,352,1706088902)    ; 65b0d9c6
BinaryPoke4(hbb,356,314042704)     ; 12b7e950
BinaryPoke4(hbb,360,-1950435094)   ; 8bbeb8ea
BinaryPoke4(hbb,364,-54949764)     ; fcb9887c
BinaryPoke4(hbb,368,1658658271)    ; 62dd1ddf
BinaryPoke4(hbb,372,366619977)     ; 15da2d49
BinaryPoke4(hbb,376,-1932296973)   ; 8cd37cf3
BinaryPoke4(hbb,380,-69972891)     ; fbd44c65
BinaryPoke4(hbb,384,1303535960)    ; 4db26158
BinaryPoke4(hbb,388,984961486)     ; 3ab551ce
BinaryPoke4(hbb,392,-1547960204)   ; a3bc0074
BinaryPoke4(hbb,396,-725929758)    ; d4bb30e2
BinaryPoke4(hbb,400,1256170817)    ; 4adfa541
BinaryPoke4(hbb,404,1037604311)    ; 3dd895d7
BinaryPoke4(hbb,408,-1529756563)   ; a4d1c46d
BinaryPoke4(hbb,412,-740887301)    ; d3d6f4fb
BinaryPoke4(hbb,416,1131014506)    ; 4369e96a
BinaryPoke4(hbb,420,879679996)     ; 346ed9fc
BinaryPoke4(hbb,424,-1385723834)   ; ad678846
BinaryPoke4(hbb,428,-631195440)    ; da60b8d0
BinaryPoke4(hbb,432,1141124467)    ; 44042d73
BinaryPoke4(hbb,436,855842277)     ; 33031de5
BinaryPoke4(hbb,440,-1442165665)   ; aa0a4c5f
BinaryPoke4(hbb,444,-586318647)    ; dd0d7cc9
BinaryPoke4(hbb,448,1342533948)    ; 5005713c
BinaryPoke4(hbb,452,654459306)     ; 270241aa
BinaryPoke4(hbb,456,-1106571248)   ; be0b1010
BinaryPoke4(hbb,460,-921952122)    ; c90c2086
BinaryPoke4(hbb,464,1466479909)    ; 5768b525
BinaryPoke4(hbb,468,544179635)     ; 206f85b3
BinaryPoke4(hbb,472,-1184443383)   ; b966d409
BinaryPoke4(hbb,476,-832445281)    ; ce61e49f
BinaryPoke4(hbb,480,1591671054)    ; 5edef90e
BinaryPoke4(hbb,484,702138776)     ; 29d9c998
BinaryPoke4(hbb,488,-1328506846)   ; b0d09822
BinaryPoke4(hbb,492,-942167884)    ; c7d7a8b4
BinaryPoke4(hbb,496,1504918807)    ; 59b33d17
BinaryPoke4(hbb,500,783551873)     ; 2eb40d81
BinaryPoke4(hbb,504,-1212326853)   ; b7bd5c3b
BinaryPoke4(hbb,508,-1061524307)   ; c0ba6cad
BinaryPoke4(hbb,512,-306674912)    ; edb88320
BinaryPoke4(hbb,516,-1698712650)   ; 9abfb3b6
BinaryPoke4(hbb,520,62317068)      ; 03b6e20c
BinaryPoke4(hbb,524,1957810842)    ; 74b1d29a
BinaryPoke4(hbb,528,-355121351)    ; ead54739
BinaryPoke4(hbb,532,-1647151185)   ; 9dd277af
BinaryPoke4(hbb,536,81470997)      ; 04db2615
BinaryPoke4(hbb,540,1943803523)    ; 73dc1683
BinaryPoke4(hbb,544,-480048366)    ; e3630b12
BinaryPoke4(hbb,548,-1805370492)   ; 94643b84
BinaryPoke4(hbb,552,225274430)     ; 0d6d6a3e
BinaryPoke4(hbb,556,2053790376)    ; 7a6a5aa8
BinaryPoke4(hbb,560,-468791541)    ; e40ecf0b
BinaryPoke4(hbb,564,-1828061283)   ; 9309ff9d
BinaryPoke4(hbb,568,167816743)     ; 0a00ae27
BinaryPoke4(hbb,572,2097651377)    ; 7d079eb1
BinaryPoke4(hbb,576,-267414716)    ; f00f9344
BinaryPoke4(hbb,580,-2029476910)   ; 8708a3d2
BinaryPoke4(hbb,584,503444072)     ; 1e01f268
BinaryPoke4(hbb,588,1762050814)    ; 6906c2fe
BinaryPoke4(hbb,592,-144550051)    ; f762575d
BinaryPoke4(hbb,596,-2140837941)   ; 806567cb
BinaryPoke4(hbb,600,426522225)     ; 196c3671
BinaryPoke4(hbb,604,1852507879)    ; 6e6b06e7
BinaryPoke4(hbb,608,-19653770)     ; fed41b76
BinaryPoke4(hbb,612,-1982649376)   ; 89d32be0
BinaryPoke4(hbb,616,282753626)     ; 10da7a5a
BinaryPoke4(hbb,620,1742555852)    ; 67dd4acc
BinaryPoke4(hbb,624,-105259153)    ; f9b9df6f
BinaryPoke4(hbb,628,-1900089351)   ; 8ebeeff9
BinaryPoke4(hbb,632,397917763)     ; 17b7be43
BinaryPoke4(hbb,636,1622183637)    ; 60b08ed5
BinaryPoke4(hbb,640,-690576408)    ; d6d6a3e8
BinaryPoke4(hbb,644,-1580100738)   ; a1d1937e
BinaryPoke4(hbb,648,953729732)     ; 38d8c2c4
BinaryPoke4(hbb,652,1340076626)    ; 4fdff252
BinaryPoke4(hbb,656,-776247311)    ; d1bb67f1
BinaryPoke4(hbb,660,-1497606297)   ; a6bc5767
BinaryPoke4(hbb,664,1068828381)    ; 3fb506dd
BinaryPoke4(hbb,668,1219638859)    ; 48b2364b
BinaryPoke4(hbb,672,-670225446)    ; d80d2bda
BinaryPoke4(hbb,676,-1358292148)   ; af0a1b4c
BinaryPoke4(hbb,680,906185462)     ; 36034af6
BinaryPoke4(hbb,684,1090812512)    ; 41047a60
BinaryPoke4(hbb,688,-547295293)    ; df60efc3
BinaryPoke4(hbb,692,-1469587627)   ; a867df55
BinaryPoke4(hbb,696,829329135)     ; 316e8eef
BinaryPoke4(hbb,700,1181335161)    ; 4669be79
BinaryPoke4(hbb,704,-882789492)    ; cb61b38c
BinaryPoke4(hbb,708,-1134132454)   ; bc66831a
BinaryPoke4(hbb,712,628085408)     ; 256fd2a0
BinaryPoke4(hbb,716,1382605366)    ; 5268e236
BinaryPoke4(hbb,720,-871598187)    ; cc0c7795
BinaryPoke4(hbb,724,-1156888829)   ; bb0b4703
BinaryPoke4(hbb,728,570562233)     ; 220216b9
BinaryPoke4(hbb,732,1426400815)    ; 5505262f
BinaryPoke4(hbb,736,-977650754)    ; c5ba3bbe
BinaryPoke4(hbb,740,-1296233688)   ; b2bd0b28
BinaryPoke4(hbb,744,733239954)     ; 2bb45a92
BinaryPoke4(hbb,748,1555261956)    ; 5cb36a04
BinaryPoke4(hbb,752,-1026031705)   ; c2d7ffa7
BinaryPoke4(hbb,756,-1244606671)   ; b5d0cf31
BinaryPoke4(hbb,760,752459403)     ; 2cd99e8b
BinaryPoke4(hbb,764,1541320221)    ; 5bdeae1d
BinaryPoke4(hbb,768,-1687895376)   ; 9b64c2b0
BinaryPoke4(hbb,772,-328994266)    ; ec63f226
BinaryPoke4(hbb,776,1969922972)    ; 756aa39c
BinaryPoke4(hbb,780,40735498)      ; 026d930a
BinaryPoke4(hbb,784,-1677130071)   ; 9c0906a9
BinaryPoke4(hbb,788,-351390145)    ; eb0e363f
BinaryPoke4(hbb,792,1913087877)    ; 72076785
BinaryPoke4(hbb,796,83908371)      ; 05005713
BinaryPoke4(hbb,800,-1782625662)   ; 95bf4a82
BinaryPoke4(hbb,804,-491226604)    ; e2b87a14
BinaryPoke4(hbb,808,2075208622)    ; 7bb12bae
BinaryPoke4(hbb,812,213261112)     ; 0cb61b38
BinaryPoke4(hbb,816,-1831694693)   ; 92d28e9b
BinaryPoke4(hbb,820,-438977011)    ; e5d5be0d
BinaryPoke4(hbb,824,2094854071)    ; 7cdcefb7
BinaryPoke4(hbb,828,198958881)     ; 0bdbdf21
BinaryPoke4(hbb,832,-2032938284)   ; 86d3d2d4
BinaryPoke4(hbb,836,-237706686)    ; f1d4e242
BinaryPoke4(hbb,840,1759359992)    ; 68ddb3f8
BinaryPoke4(hbb,844,534414190)     ; 1fda836e
BinaryPoke4(hbb,848,-2118248755)   ; 81be16cd
BinaryPoke4(hbb,852,-155638181)    ; f6b9265b
BinaryPoke4(hbb,856,1873836001)    ; 6fb077e1
BinaryPoke4(hbb,860,414664567)     ; 18b74777
BinaryPoke4(hbb,864,-2012718362)   ; 88085ae6
BinaryPoke4(hbb,868,-15766928)     ; ff0f6a70
BinaryPoke4(hbb,872,1711684554)    ; 66063bca
BinaryPoke4(hbb,876,285281116)     ; 11010b5c
BinaryPoke4(hbb,880,-1889165569)   ; 8f659eff
BinaryPoke4(hbb,884,-127750551)    ; f862ae69
BinaryPoke4(hbb,888,1634467795)    ; 616bffd3
BinaryPoke4(hbb,892,376229701)     ; 166ccf45
BinaryPoke4(hbb,896,-1609899400)   ; a00ae278
BinaryPoke4(hbb,900,-686959890)    ; d70dd2ee
BinaryPoke4(hbb,904,1308918612)    ; 4e048354
BinaryPoke4(hbb,908,956543938)     ; 3903b3c2
BinaryPoke4(hbb,912,-1486412191)   ; a7672661
BinaryPoke4(hbb,916,-799009033)    ; d06016f7
BinaryPoke4(hbb,920,1231636301)    ; 4969474d
BinaryPoke4(hbb,924,1047427035)    ; 3e6e77db
BinaryPoke4(hbb,928,-1362007478)   ; aed16a4a
BinaryPoke4(hbb,932,-640263460)    ; d9d65adc
BinaryPoke4(hbb,936,1088359270)    ; 40df0b66
BinaryPoke4(hbb,940,936918000)     ; 37d83bf0
BinaryPoke4(hbb,944,-1447252397)   ; a9bcae53
BinaryPoke4(hbb,948,-558129467)    ; debb9ec5
BinaryPoke4(hbb,952,1202900863)    ; 47b2cf7f
BinaryPoke4(hbb,956,817233897)     ; 30b5ffe9
BinaryPoke4(hbb,960,-1111625188)   ; bdbdf21c
BinaryPoke4(hbb,964,-893730166)    ; cabac28a
BinaryPoke4(hbb,968,1404277552)    ; 53b39330
BinaryPoke4(hbb,972,615818150)     ; 24b4a3a6
BinaryPoke4(hbb,976,-1160759803)   ; bad03605
BinaryPoke4(hbb,980,-841546093)    ; cdd70693
BinaryPoke4(hbb,984,1423857449)    ; 54de5729
BinaryPoke4(hbb,988,601450431)     ; 23d967bf
BinaryPoke4(hbb,992,-1285129682)   ; b3667a2e
BinaryPoke4(hbb,996,-1000256840)   ; c4614ab8
BinaryPoke4(hbb,1000,1567103746)   ; 5d681b02
BinaryPoke4(hbb,1004,711928724)    ; 2a6f2b94
BinaryPoke4(hbb,1008,-1274298825)  ; b40bbe37
BinaryPoke4(hbb,1012,-1022587231)  ; c30c8ea1
BinaryPoke4(hbb,1016,1510334235)   ; 5a05df1b
BinaryPoke4(hbb,1020,755167117)    ; 2d02ef8d
Return (hbb)
; Returns a handle to a binary buffer which contains 256 crc32 data numbers.
#EndFunction

:skip_udfcrc32createtable
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32savetable",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32savetable

#DefineFunction udfCrc32SaveTable (hbbcrctable, filename)
Return (1024 == BinaryWrite(hbbcrctable,filename))
; Returns @true if 1024 byte are written otherwise @false.
#EndFunction

:skip_udfcrc32savetable
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32loadtable",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32loadtable

#DefineFunction udfCrc32LoadTable (filename)
fsize = FileSize(filename)
hbbcrcdata = 0
If (fsize == 1024)
   hbbcrcdata = BinaryAlloc(fsize)
   num = BinaryRead(hbbcrcdata,filename)
   If (num <> 1024)
      BinaryFree(hbbcrcdata)
      hbbcrcdata=0
   EndIf
EndIf
Return (hbbcrcdata)
;..........................................................................................................................................
; If fileread was successful, this udf returns a handle to a binary buffer,
; which contains the crc32 data lookup table, otherwise it returns 0.
; Make sure to read the proper data file.
;..........................................................................................................................................
#EndFunction

:skip_udfcrc32loadtable
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32loadfile",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32loadfile

#DefineFunction udfCrc32LoadFile (filename)
If !FileExist(filename) Then Return (0)
fsize = FileSize(filename)
hbb = BinaryAlloc(fsize)
BinaryRead(hbb,filename)
Return (hbb)
;..........................................................................................................................................
; Filename is the file to checksum.
; Returns a handle to a binary buffer which covers the input data.
; Binary buffer is used as input parameter for udfCrc32Calculate (hbbcrctable, hbbdata).
;..........................................................................................................................................
#EndFunction

:skip_udfcrc32loadfile
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32loadstr",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32loadstr

#DefineFunction udfCrc32LoadStr (str)
If (str == "") Then Return (0)
strsize = StrLen(str)
hbb = BinaryAlloc(strsize)
BinaryPokeStr(hbb,0,str)
Return (hbb)
;..........................................................................................................................................
; Str is the stream of characters to checksum.
; Returns a handle to a binary buffer which covers the input data.
; Binary buffer is used as input parameter for udfCrc32Calculate (hbbcrctable, hbbdata).
;..........................................................................................................................................
#EndFunction

:skip_udfcrc32loadstr
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcrc32calculate",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcrc32calculate

#DefineFunction udfCrc32Calculate (hbbcrctable, hbbdata)
datasize = BinaryEodGet(hbbdata)
crc = -1
i=0
While (i<datasize)
   crc = ((crc >> 8) & 16777215) ^ BinaryPeek4(hbbcrctable,4*((crc ^ BinaryPeek(hbbdata,i)) & 255))
   i=i+1
EndWhile
crc = crc ^ -1

BinaryFree(hbbdata)

Return (crc)
;..........................................................................................................................................
; (FFFFFFFF = -1; 00FFFFFF = 16777215; FF = 255)
;
; hbbcrctable is a handle to a binary buffer which contains the crc32 data lookup table.
; hbbdata is a handle to a binary buffer which contains the data to checksum.
; Returns CRC32 checksum as decimal number.
; Note: This udf destroys resp. frees the hbbdata binary buffer as default.
;..........................................................................................................................................
#EndFunction

:skip_udfcrc32calculate
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfdectohex",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfdectohex

#DefineFunction udfDecToHex (iDecimal, iPadLength, iCaseMode)
iPadLength = Min(8,Max(1,iPadLength))
If Max(0,Min(1,iCaseMode)) Then sHexChars = "0123456789ABCDEF"
   Else sHexChars = "0123456789abcdef"
sHex = ""
iZ = 1
For i=7 To 0 By -1
   iN = (iDecimal>>(i*4))&15
   If !iN Then If iZ Then Continue
   iZ = 0
   sHex = StrCat(sHex,StrSub(sHexChars,iN+1,1))
Next
sHex = StrFixLeft(sHex,"0",iPadLength)
Return (sHex)
#EndFunction

:skip_udfdectohex
;------------------------------------------------------------------------------------------------------------------------------------------



;--- test ---


:test1
; Calculate crc32 checksum on string data.
str = "DD"
crc32 = udfCrc32Calculate(udfCrc32CreateTable(),udfCrc32LoadStr(str))
; Should be crc32 = "-1535304329".
crc32hex = udfDecToHex(crc32,8,1)
; Should be crc32hex = "A47D1D77".


:test2
; Create crc32 table and save it to diskfile.
; Create proper tempfilename.
TempFolder = Environment("TEMP")

TempFolder = StrCat(TempFolder,StrSub("\",(StrSub(TempFolder,StrLen(TempFolder),1)<>"\"),1)) ; add backslash
crctablefile = StrCat(TempFolder,"CRC32.BIN")

; Create crc32 data lookup table.
hbbcrctable = udfCrc32CreateTable()

; Write crc table binary buffer to diskfile.
bool = udfCrc32SaveTable(hbbcrctable,crctablefile)
BinaryFree(hbbcrctable)

; Note:
; For checksumming many files in a loop
; the binary buffer which contains the crc data lookup table
; has to be created only once.


:test3
; Read crc table from diskfile and return binary buffer handle.
If FileExist(crctablefile) Then hbbcrctable = udfCrc32LoadTable(crctablefile)
If !IsDefined(hbbcrctable) Then Goto CANCEL
If !hbbcrctable Then Goto CANCEL


:test4
; Simple test, build checksum on existing tempfile.
; Use table from diskfile, loaded above in test3.
testfile = crctablefile
crc32    = udfCrc32Calculate(hbbcrctable,udfCrc32LoadFile(testfile))
; Should be crc32 = "1875877395".
crc32hex = udfDecToHex(crc32,8,1)
; Should be crc32hex = "6FCF9E13".
Pause("Demo Calculate CRC32",StrCat(testfile,@CRLF,crc32,@CRLF,crc32hex))


:test5
; ... time for a cup of tea.
testfile = StrCat(DirHome(),"BROWSER.EXE")
crc32