xref: /freebsd/usr.bin/cksum/cksum.1 (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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.\" 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	8.1 (Berkeley) 6/29/93
36.\"
37.Dd June 29, 1993
38.Dt CKSUM 1
39.Os BSD 4.4
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 the standard output three whitespace separated
51fields for each input file.
52These fields are a checksum
53.Tn CRC ,
54the total number of octets in the file and the file name.
55If no file name is specified, the standard input is used and no file name
56is written.
57.Pp
58The options are as follows:
59.Bl -tag -width indent
60.It Fl o
61Use historic algorithms instead of the (superior) default one.
62.Pp
63Algorithm 1 is the algorithm used by historic
64.Bx
65systems as the
66.Xr sum 1
67algorithm and by historic
68.At V
69systems as the
70.Xr sum
71algorithm when using the
72.Fl r
73option.
74This is a 16-bit checksum, with a right rotation before each addition;
75overflow is discarded.
76.Pp
77Algorithm 2 is the algorithm used by historic
78.At V
79systems as the
80default
81.Xr sum
82algorithm.
83This is a 32-bit checksum, and is defined as follows:
84.Bd -unfilled -offset indent
85s = sum of all bytes;
86r = s % 2^16 + (s % 2^32) / 2^16;
87cksum = (r % 2^16) + r / 2^16;
88.Ed
89.Pp
90Both algorithm 1 and 2 write to the standard output the same fields as
91the default algorithm except that the size of the file in bytes is
92replaced with the size of the file in blocks.
93For historic reasons, the block size is 1024 for algorithm 1 and 512
94for algorithm 2.
95Partial blocks are rounded up.
96.El
97.Pp
98The default
99.Tn CRC
100used is based on the polynomial used for
101.Tn CRC
102error checking
103in the networking standard
104.St -iso8802-3
105The
106.Tn CRC
107checksum encoding is defined by the generating polynomial:
108.Pp
109.Bd -unfilled -offset indent
110G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
111     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
112.Ed
113.Pp
114Mathematically, the
115.Tn CRC
116value corresponding to a given file is defined by
117the following procedure:
118.Bd -filled -offset indent
119The
120.Ar n
121bits to be evaluated are considered to be the coefficients of a mod 2
122polynomial M(x) of degree
123.Ar n Ns \-1 .
124These
125.Ar n
126bits are the bits from the file, with the most significant bit being the most
127significant bit of the first octet of the file and the last bit being the least
128significant bit of the last octet, padded with zero bits (if necessary) to
129achieve an integral number of octets, followed by one or more octets
130representing the length of the file as a binary value, least significant octet
131first.
132The smallest number of octets capable of representing this integer are used.
133.Pp
134M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
135G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
136.Pp
137The coefficients of R(x) are considered to be a 32-bit sequence.
138.Pp
139The bit sequence is complemented and the result is the CRC.
140.Ed
141.Pp
142The
143.Nm cksum
144utility exits 0 on success, and >0 if an error occurs.
145.Sh SEE ALSO
146The default calculation is identical to that given in pseudo-code
147in the following
148.Tn ACM
149article.
150.Rs
151.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
152.%A Dilip V. Sarwate
153.%J "Communications of the \\*(tNACM\\*(sP"
154.%D "August 1988"
155.Re
156.Sh STANDARDS
157The
158.Nm cksum
159utility is expected to be POSIX 1003.2 compatible.
160.Sh HISTORY
161The
162.Nm cksum
163utility appears in
164.Bx 4.4 .
165