MS Access: VBA CRC32 Function

Here’s a CRC32 function based on the work at: cCRC32.

The main difference is that this is a function, and the crc32 table is not recalculated each time. If there’s a way to do constant arrays, I’d like to know. I haven’t found anything online.

Function CRC32(str As String)
    Dim crc32Table(256) As Long
    crc32Table(0) = 0
    crc32Table(1) = 1996959894
    crc32Table(2) = -301047508
    crc32Table(3) = -1727442502
    crc32Table(4) = 124634137
    crc32Table(5) = 1886057615
    crc32Table(6) = -379345611
    crc32Table(7) = -1637575261
    crc32Table(8) = 249268274
    crc32Table(9) = 2044508324
    crc32Table(10) = -522852066
    crc32Table(11) = -1747789432
    crc32Table(12) = 162941995
    crc32Table(13) = 2125561021
    crc32Table(14) = -407360249
    crc32Table(15) = -1866523247
    crc32Table(16) = 498536548
    crc32Table(17) = 1789927666
    crc32Table(18) = -205950648
    crc32Table(19) = -2067906082
    crc32Table(20) = 450548861
    crc32Table(21) = 1843258603
    crc32Table(22) = -187386543
    crc32Table(23) = -2083289657
    crc32Table(24) = 325883990
    crc32Table(25) = 1684777152
    crc32Table(26) = -43845254
    crc32Table(27) = -1973040660
    crc32Table(28) = 335633487
    crc32Table(29) = 1661365465
    crc32Table(30) = -99664541
    crc32Table(31) = -1928851979
    crc32Table(32) = 997073096
    crc32Table(33) = 1281953886
    crc32Table(34) = -715111964
    crc32Table(35) = -1570279054
    crc32Table(36) = 1006888145
    crc32Table(37) = 1258607687
    crc32Table(38) = -770865667
    crc32Table(39) = -1526024853
    crc32Table(40) = 901097722
    crc32Table(41) = 1119000684
    crc32Table(42) = -608450090
    crc32Table(43) = -1396901568
    crc32Table(44) = 853044451
    crc32Table(45) = 1172266101
    crc32Table(46) = -589951537
    crc32Table(47) = -1412350631
    crc32Table(48) = 651767980
    crc32Table(49) = 1373503546
    crc32Table(50) = -925412992
    crc32Table(51) = -1076862698
    crc32Table(52) = 565507253
    crc32Table(53) = 1454621731
    crc32Table(54) = -809855591
    crc32Table(55) = -1195530993
    crc32Table(56) = 671266974
    crc32Table(57) = 1594198024
    crc32Table(58) = -972236366
    crc32Table(59) = -1324619484
    crc32Table(60) = 795835527
    crc32Table(61) = 1483230225
    crc32Table(62) = -1050600021
    crc32Table(63) = -1234817731
    crc32Table(64) = 1994146192
    crc32Table(65) = 31158534
    crc32Table(66) = -1731059524
    crc32Table(67) = -271249366
    crc32Table(68) = 1907459465
    crc32Table(69) = 112637215
    crc32Table(70) = -1614814043
    crc32Table(71) = -390540237
    crc32Table(72) = 2013776290
    crc32Table(73) = 251722036
    crc32Table(74) = -1777751922
    crc32Table(75) = -519137256
    crc32Table(76) = 2137656763
    crc32Table(77) = 141376813
    crc32Table(78) = -1855689577
    crc32Table(79) = -429695999
    crc32Table(80) = 1802195444
    crc32Table(81) = 476864866
    crc32Table(82) = -2056965928
    crc32Table(83) = -228458418
    crc32Table(84) = 1812370925
    crc32Table(85) = 453092731
    crc32Table(86) = -2113342271
    crc32Table(87) = -183516073
    crc32Table(88) = 1706088902
    crc32Table(89) = 314042704
    crc32Table(90) = -1950435094
    crc32Table(91) = -54949764
    crc32Table(92) = 1658658271
    crc32Table(93) = 366619977
    crc32Table(94) = -1932296973
    crc32Table(95) = -69972891
    crc32Table(96) = 1303535960
    crc32Table(97) = 984961486
    crc32Table(98) = -1547960204
    crc32Table(99) = -725929758
    crc32Table(100) = 1256170817
    crc32Table(101) = 1037604311
    crc32Table(102) = -1529756563
    crc32Table(103) = -740887301
    crc32Table(104) = 1131014506
    crc32Table(105) = 879679996
    crc32Table(106) = -1385723834
    crc32Table(107) = -631195440
    crc32Table(108) = 1141124467
    crc32Table(109) = 855842277
    crc32Table(110) = -1442165665
    crc32Table(111) = -586318647
    crc32Table(112) = 1342533948
    crc32Table(113) = 654459306
    crc32Table(114) = -1106571248
    crc32Table(115) = -921952122
    crc32Table(116) = 1466479909
    crc32Table(117) = 544179635
    crc32Table(118) = -1184443383
    crc32Table(119) = -832445281
    crc32Table(120) = 1591671054
    crc32Table(121) = 702138776
    crc32Table(122) = -1328506846
    crc32Table(123) = -942167884
    crc32Table(124) = 1504918807
    crc32Table(125) = 783551873
    crc32Table(126) = -1212326853
    crc32Table(127) = -1061524307
    crc32Table(128) = -306674912
    crc32Table(129) = -1698712650
    crc32Table(130) = 62317068
    crc32Table(131) = 1957810842
    crc32Table(132) = -355121351
    crc32Table(133) = -1647151185
    crc32Table(134) = 81470997
    crc32Table(135) = 1943803523
    crc32Table(136) = -480048366
    crc32Table(137) = -1805370492
    crc32Table(138) = 225274430
    crc32Table(139) = 2053790376
    crc32Table(140) = -468791541
    crc32Table(141) = -1828061283
    crc32Table(142) = 167816743
    crc32Table(143) = 2097651377
    crc32Table(144) = -267414716
    crc32Table(145) = -2029476910
    crc32Table(146) = 503444072
    crc32Table(147) = 1762050814
    crc32Table(148) = -144550051
    crc32Table(149) = -2140837941
    crc32Table(150) = 426522225
    crc32Table(151) = 1852507879
    crc32Table(152) = -19653770
    crc32Table(153) = -1982649376
    crc32Table(154) = 282753626
    crc32Table(155) = 1742555852
    crc32Table(156) = -105259153
    crc32Table(157) = -1900089351
    crc32Table(158) = 397917763
    crc32Table(159) = 1622183637
    crc32Table(160) = -690576408
    crc32Table(161) = -1580100738
    crc32Table(162) = 953729732
    crc32Table(163) = 1340076626
    crc32Table(164) = -776247311
    crc32Table(165) = -1497606297
    crc32Table(166) = 1068828381
    crc32Table(167) = 1219638859
    crc32Table(168) = -670225446
    crc32Table(169) = -1358292148
    crc32Table(170) = 906185462
    crc32Table(171) = 1090812512
    crc32Table(172) = -547295293
    crc32Table(173) = -1469587627
    crc32Table(174) = 829329135
    crc32Table(175) = 1181335161
    crc32Table(176) = -882789492
    crc32Table(177) = -1134132454
    crc32Table(178) = 628085408
    crc32Table(179) = 1382605366
    crc32Table(180) = -871598187
    crc32Table(181) = -1156888829
    crc32Table(182) = 570562233
    crc32Table(183) = 1426400815
    crc32Table(184) = -977650754
    crc32Table(185) = -1296233688
    crc32Table(186) = 733239954
    crc32Table(187) = 1555261956
    crc32Table(188) = -1026031705
    crc32Table(189) = -1244606671
    crc32Table(190) = 752459403
    crc32Table(191) = 1541320221
    crc32Table(192) = -1687895376
    crc32Table(193) = -328994266
    crc32Table(194) = 1969922972
    crc32Table(195) = 40735498
    crc32Table(196) = -1677130071
    crc32Table(197) = -351390145
    crc32Table(198) = 1913087877
    crc32Table(199) = 83908371
    crc32Table(200) = -1782625662
    crc32Table(201) = -491226604
    crc32Table(202) = 2075208622
    crc32Table(203) = 213261112
    crc32Table(204) = -1831694693
    crc32Table(205) = -438977011
    crc32Table(206) = 2094854071
    crc32Table(207) = 198958881
    crc32Table(208) = -2032938284
    crc32Table(209) = -237706686
    crc32Table(210) = 1759359992
    crc32Table(211) = 534414190
    crc32Table(212) = -2118248755
    crc32Table(213) = -155638181
    crc32Table(214) = 1873836001
    crc32Table(215) = 414664567
    crc32Table(216) = -2012718362
    crc32Table(217) = -15766928
    crc32Table(218) = 1711684554
    crc32Table(219) = 285281116
    crc32Table(220) = -1889165569
    crc32Table(221) = -127750551
    crc32Table(222) = 1634467795
    crc32Table(223) = 376229701
    crc32Table(224) = -1609899400
    crc32Table(225) = -686959890
    crc32Table(226) = 1308918612
    crc32Table(227) = 956543938
    crc32Table(228) = -1486412191
    crc32Table(229) = -799009033
    crc32Table(230) = 1231636301
    crc32Table(231) = 1047427035
    crc32Table(232) = -1362007478
    crc32Table(233) = -640263460
    crc32Table(234) = 1088359270
    crc32Table(235) = 936918000
    crc32Table(236) = -1447252397
    crc32Table(237) = -558129467
    crc32Table(238) = 1202900863
    crc32Table(239) = 817233897
    crc32Table(240) = -1111625188
    crc32Table(241) = -893730166
    crc32Table(242) = 1404277552
    crc32Table(243) = 615818150
    crc32Table(244) = -1160759803
    crc32Table(245) = -841546093
    crc32Table(246) = 1423857449
    crc32Table(247) = 601450431
    crc32Table(248) = -1285129682
    crc32Table(249) = -1000256840
    crc32Table(250) = 1567103746
    crc32Table(251) = 711928724
    crc32Table(252) = -1274298825
    crc32Table(253) = -1022587231
    crc32Table(254) = 1510334235
    crc32Table(255) = 755167117

   Dim crc32Result As Long
   crc32Result = &HFFFFFFFF
      
   Dim i As Integer
   Dim iLookup As Integer
   Dim buffer() As Byte
   buffer = StrConv(str, vbFromUnicode)
   
   For i = LBound(buffer) To UBound(buffer)
      iLookup = (crc32Result And &HFF) Xor buffer(i)
      crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And 16777215
      ' nasty shr 8 with vb :/
      crc32Result = crc32Result Xor crc32Table(iLookup)
   Next i
   
   CRC32 = Not (crc32Result)

End Function

Leave a Reply