forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRC32.Mod
75 lines (65 loc) · 1.85 KB
/
CRC32.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE CRC32; (* ejz *)
IMPORT SYSTEM;
CONST
CRCInit* = {};
VAR
crcTable: ARRAY 256 OF SET;
PROCEDURE CRC32*(crc: SET; VAR buf: ARRAY OF BYTE; len: LONGINT): SET;
VAR i, j: LONGINT;
BEGIN
i := 0; crc := crc / {0 .. 31};
WHILE len >= 8 DO
j := 0;
WHILE j < 8 DO
crc := crcTable[ORD( (crc / BITS( buf[i])) * {0 .. 7})] / SYSTEM.LSH(crc, -8);
INC(i); INC(j)
END;
DEC(len, 8)
END;
IF len > 0 THEN
REPEAT
crc := crcTable[ORD( (crc / BITS( buf[i])) * {0 .. 7})] / SYSTEM.LSH(crc, -8);
INC(i); DEC(len)
UNTIL len <= 0
END;
RETURN crc / {0 .. 31}
END CRC32;
PROCEDURE InitCRCTable();
VAR
p: ARRAY 14 OF LONGINT;
poly, c: SET;
k, n: LONGINT;
BEGIN
p[0] := 0; p[1] := 1; p[2] := 2; p[3] := 4; p[4] := 5; p[5] := 7; p[6] := 8; p[7] := 10;
p[8] := 11; p[9] := 12; p[10] := 16; p[11] := 22; p[12] := 23; p[13] := 26;
poly := {}; n := 0;
WHILE n < 14 DO
poly := poly + SYSTEM.LSH({0}, (31-p[n])); INC(n)
END;
n := 0;
WHILE n < 256 DO
c := BITS( n); k := 0;
WHILE k < 8 DO
IF 0 IN c THEN
c := poly / SYSTEM.LSH(c, -1)
ELSE
c := SYSTEM.LSH(c, -1)
END;
INC(k)
END;
crcTable[n] := c; INC(n)
END
END InitCRCTable;
BEGIN
InitCRCTable()
END CRC32.