POSIX 1003.2, D11.2 modified the CRC! Bring up to date.
[unix-history] / usr / src / usr.bin / cksum / cksum.1
CommitLineData
05e12ae3 1.\" Copyright (c) 1991, 1991 The Regents of the University of California.
bd2a8a16
KB
2.\" All rights reserved.
3.\"
cf136d2c
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
05e12ae3 7.\" %sccs.include.redist.roff%
bd2a8a16 8.\"
040b0e93 9.\" @(#)cksum.1 5.6 (Berkeley) %G%
bd2a8a16 10.\"
05e12ae3
CL
11.Dd
12.Dt CKSUM 1
13.Os
14.Sh NAME
15.Nm cksum
16.Nd display file checksums and block counts
17.Sh SYNOPSIS
18.Nm cksum
87c9f085 19.Op Fl o Op \&1 \&| \&2
05e12ae3
CL
20.Op Ar file ...
21.Sh DESCRIPTION
bd2a8a16 22The
05e12ae3 23.Nm cksum
27de4b45 24utility writes to standard output three whitespace separated fields for each
040b0e93 25input file.
05e12ae3
CL
26These fields are a checksum
27.Tn CRC ,
040b0e93
KB
28the total number of octets in the file and the file name.
29If no file name is specified, the standard input is used and no file name
30is written.
05e12ae3 31.Pp
27de4b45 32The options are as follows:
05e12ae3
CL
33.Bl -tag -width indent
34.It Fl o
27de4b45 35Use historic algorithms instead of the (superior) default one.
05e12ae3
CL
36.Pp
37Algorithm 1 is the algorithm used by historic
38.Bx
39systems as the
40.Xr sum 1
41algorithm and by historic
42.At V
43systems as the
44.Xr sum
45algorithm when using the
46.Fl r
47option.
27de4b45
KB
48This is a 16-bit checksum, with a right rotation before each addition;
49overflow is discarded.
05e12ae3
CL
50.Pp
51Algorithm 2 is the algorithm used by historic
52.At V
53systems as the
27de4b45 54default
05e12ae3 55.Xr sum
27de4b45
KB
56algorithm.
57This is a 32-bit checksum, and is defined as follows:
05e12ae3 58.Bd -unfilled -offset indent
27de4b45
KB
59s = sum of all bytes;
60r = s % 2^16 + (s % 2^32) / 2^16;
61cksum = (r % 2^16) + r / 2^16;
05e12ae3
CL
62.Ed
63.Pp
27de4b45
KB
64Both algorithm 1 and 2 write to standard output the same fields as
65the default algorithm except that the size of the file in bytes is
66replaced with the size of the file in blocks.
67For historic reasons, the block size is 1024 for algorithm 1 and 512
68for algorithm 2.
69Partial blocks are rounded up.
05e12ae3
CL
70.El
71.Pp
72The default
73.Tn CRC
74used is based on the polynomial used for
75.Tn CRC
76error checking
77in the networking standard
78.St -iso8802-3
79The
80.Tn CRC
81checksum encoding is defined by the generating polynomial:
82.Pp
83.Bd -unfilled -offset indent
27de4b45 84G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
05e12ae3
CL
85 x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
86.Ed
87.Pp
88Mathematically, the
89.Tn CRC
90value corresponding to a given file is defined by
bd2a8a16 91the following procedure:
05e12ae3 92.Bd -filled -offset indent
bd2a8a16 93The
05e12ae3 94.Ar n
040b0e93 95bits to be evaluated are considered to be the coefficients of a mod 2
bd2a8a16 96polynomial M(x) of degree
05e12ae3 97.Ar n Ns \-1 .
040b0e93
KB
98These
99.Ar n
100bits are the bits from the file, with the most significant bit being the most
101significant bit of the first octet of the file and the last bit being the least
102significant bit of the last octet, padded with zero bits (if necessary) to
103achieve an integral number of octets, followed by one or more octets
104representing the length of the file as a binary value, least significant octet
105first.
106The smallest number of octets capable of representing this integer are used.
05e12ae3 107.Pp
bd2a8a16
KB
108M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
109G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
05e12ae3 110.Pp
bd2a8a16 111The coefficients of R(x) are considered to be a 32-bit sequence.
05e12ae3 112.Pp
040b0e93
KB
113The bit sequence is complemented and the result is the CRC.
114.Ed
05e12ae3 115.Pp
bd2a8a16 116The
05e12ae3 117.Nm cksum
bd2a8a16 118utility exits 0 on success, and >0 if an error occurs.
05e12ae3 119.Sh SEE ALSO
040b0e93
KB
120The default calculation is identical to that given in pseudo-code
121in the following
122.Tn ACM
123article.
05e12ae3
CL
124.Rs
125.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
126.%A Dilip V. Sarwate
127.%J "Communications of the \\*(tNACM\\*(sP"
128.%D "August 1988"
129.Re
130.Sh STANDARDS
131.Nm Cksum
132is expected to conform to
133.St -p1003.2 .
134.Sh HISTORY
135The
136.Nm cksum
137utility is
138.Ud .