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