xref: /386bsd/usr/src/usr.bin/cksum/cksum.1 (revision a2142627)
1.\" Copyright (c) 1991, 1991 The Regents of the University of California.
2.\" 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.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"	@(#)cksum.1	5.5 (Berkeley) 6/27/91
36.\"
37.Dd June 27, 1991
38.Dt CKSUM 1
39.Os
40.Sh NAME
41.Nm cksum
42.Nd display file checksums and block counts
43.Sh SYNOPSIS
44.Nm cksum
45.Op Fl o Op \&1 \&| \&2
46.Op Ar file ...
47.Sh DESCRIPTION
48The
49.Nm cksum
50utility writes to standard output three whitespace separated fields for each
51input file (or the standard input by default).
52These fields are a checksum
53.Tn CRC ,
54the total number of bytes in the file and
55the file name.
56.Pp
57The options are as follows:
58.Bl -tag -width indent
59.It Fl o
60Use historic algorithms instead of the (superior) default one.
61.Pp
62Algorithm 1 is the algorithm used by historic
63.Bx
64systems as the
65.Xr sum 1
66algorithm and by historic
67.At V
68systems as the
69.Xr sum
70algorithm when using the
71.Fl r
72option.
73This is a 16-bit checksum, with a right rotation before each addition;
74overflow is discarded.
75.Pp
76Algorithm 2 is the algorithm used by historic
77.At V
78systems as the
79default
80.Xr sum
81algorithm.
82This is a 32-bit checksum, and is defined as follows:
83.Bd -unfilled -offset indent
84s = sum of all bytes;
85r = s % 2^16 + (s % 2^32) / 2^16;
86cksum = (r % 2^16) + r / 2^16;
87.Ed
88.Pp
89Both algorithm 1 and 2 write to standard output the same fields as
90the default algorithm except that the size of the file in bytes is
91replaced with the size of the file in blocks.
92For historic reasons, the block size is 1024 for algorithm 1 and 512
93for algorithm 2.
94Partial blocks are rounded up.
95.El
96.Pp
97The default
98.Tn CRC
99used is based on the polynomial used for
100.Tn CRC
101error checking
102in the networking standard
103.St -iso8802-3
104The
105.Tn CRC
106checksum encoding is defined by the generating polynomial:
107.Pp
108.Bd -unfilled -offset indent
109G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
110     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
111.Ed
112.Pp
113Mathematically, the
114.Tn CRC
115value corresponding to a given file is defined by
116the following procedure:
117.Bd -filled -offset indent
118The
119.Ar n
120bits of the file are considered to be the coefficients of a mod 2
121polynomial M(x) of degree
122.Ar n Ns \-1 .
123.Pp
124M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
125G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
126During the division, each time the intermediate remainder is zero, it
127is changed to the next value from a predefined sequence of
12832-bit integers before completing the division.
129This sequence is long and complex -- see the source code for more
130information.
131.Pp
132The coefficients of R(x) are considered to be a 32-bit sequence.
133.Ed
134.Pp
135The calculation used is identical to that given in pseudo-code in
136the
137.Tn ACM
138article referenced below.
139.Pp
140The
141.Nm cksum
142utility exits 0 on success, and >0 if an error occurs.
143.Sh SEE ALSO
144.Rs
145.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
146.%A Dilip V. Sarwate
147.%J "Communications of the \\*(tNACM\\*(sP"
148.%D "August 1988"
149.Re
150.Sh STANDARDS
151.Nm Cksum
152is expected to conform to
153.St -p1003.2 .
154.Sh HISTORY
155The
156.Nm cksum
157utility is
158.Ud .
159