xref: /original-bsd/usr.bin/cksum/cksum.1 (revision de3f5c4e)
1.\" Copyright (c) 1991, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)cksum.1	5.3 (Berkeley) 05/06/91
7.\"
8.Dd
9.Dt CKSUM 1
10.Os
11.Sh NAME
12.Nm cksum
13.Nd display file checksums and block counts
14.Sh SYNOPSIS
15.Nm cksum
16.Op Fl o Op Ar \&1 | Ar \&2
17.Op Ar file ...
18.Sh DESCRIPTION
19The
20.Nm cksum
21utility writes to standard output three whitespace separated fields for each
22input file (or the standard input by default).
23These fields are a checksum
24.Tn CRC ,
25the total number of bytes in the file and
26the file name.
27.Pp
28The options are as follows:
29.Bl -tag -width indent
30.It Fl o
31Use historic algorithms instead of the (superior) default one.
32.Pp
33Algorithm 1 is the algorithm used by historic
34.Bx
35systems as the
36.Xr sum 1
37algorithm and by historic
38.At V
39systems as the
40.Xr sum
41algorithm when using the
42.Fl r
43option.
44This is a 16-bit checksum, with a right rotation before each addition;
45overflow is discarded.
46.Pp
47Algorithm 2 is the algorithm used by historic
48.At V
49systems as the
50default
51.Xr sum
52algorithm.
53This is a 32-bit checksum, and is defined as follows:
54.Bd -unfilled -offset indent
55s = sum of all bytes;
56r = s % 2^16 + (s % 2^32) / 2^16;
57cksum = (r % 2^16) + r / 2^16;
58.Ed
59.Pp
60Both algorithm 1 and 2 write to standard output the same fields as
61the default algorithm except that the size of the file in bytes is
62replaced with the size of the file in blocks.
63For historic reasons, the block size is 1024 for algorithm 1 and 512
64for algorithm 2.
65Partial blocks are rounded up.
66.El
67.Pp
68The default
69.Tn CRC
70used is based on the polynomial used for
71.Tn CRC
72error checking
73in the networking standard
74.St -iso8802-3
75The
76.Tn CRC
77checksum encoding is defined by the generating polynomial:
78.Pp
79.Bd -unfilled -offset indent
80G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
81     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
82.Ed
83.Pp
84Mathematically, the
85.Tn CRC
86value corresponding to a given file is defined by
87the following procedure:
88.Bd -filled -offset indent
89The
90.Ar n
91bits of the file are considered to be the coefficients of a mod 2
92polynomial M(x) of degree
93.Ar n Ns \-1 .
94.Pp
95M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
96G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
97During the division, each time the intermediate remainder is zero, it
98is changed to the next value from a predefined sequence of
9932-bit integers before completing the division.
100This sequence is long and complex -- see the source code for more
101information.
102.Pp
103The coefficients of R(x) are considered to be a 32-bit sequence.
104.Ed
105.Pp
106The calculation used is identical to that given in pseudo-code in
107the
108.Tn ACM
109article referenced below.
110.Pp
111The
112.Nm cksum
113utility exits 0 on success, and >0 if an error occurs.
114.Sh SEE ALSO
115.Rs
116.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
117.%A Dilip V. Sarwate
118.%J "Communications of the \\*(tNACM\\*(sP"
119.%D "August 1988"
120.Re
121.Sh STANDARDS
122.Nm Cksum
123is expected to conform to
124.St -p1003.2 .
125.Sh HISTORY
126The
127.Nm cksum
128utility is
129.Ud .
130