summaryrefslogtreecommitdiff
path: root/appl/lib/crc.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/crc.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/lib/crc.b')
-rw-r--r--appl/lib/crc.b45
1 files changed, 45 insertions, 0 deletions
diff --git a/appl/lib/crc.b b/appl/lib/crc.b
new file mode 100644
index 00000000..d25eeec2
--- /dev/null
+++ b/appl/lib/crc.b
@@ -0,0 +1,45 @@
+implement Crc;
+
+include "crc.m";
+
+init(poly : int, reg : int) : ref CRCstate
+{
+ if (poly == 0)
+ poly = int 16redb88320;
+ tab := array[256] of int;
+ for(i := 0; i < 256; i++){
+ crc := i;
+ for(j := 0; j < 8; j++){
+ c := crc & 1;
+ crc = (crc >> 1) & 16r7fffffff;
+ if(c)
+ crc ^= poly;
+ }
+ tab[i] = crc;
+ }
+ crcs := ref CRCstate;
+ crcs.crc = 0;
+ crcs.crctab = tab;
+ crcs.reg = reg;
+ return crcs;
+}
+
+crc(crcs : ref CRCstate, buf : array of byte, nb : int) : int
+{
+ n := nb;
+ if (n > len buf)
+ n = len buf;
+ crc := crcs.crc;
+ tab := crcs.crctab;
+ crc ^= crcs.reg;
+ for (i := 0; i < n; i++)
+ crc = tab[int(byte crc ^ buf[i])] ^ ((crc >> 8) & 16r00ffffff);
+ crc ^= crcs.reg;
+ crcs.crc = crc;
+ return crc;
+}
+
+reset(crcs : ref CRCstate)
+{
+ crcs.crc = 0;
+}