xref: /csrg-svn/usr.bin/cksum/cksum.1 (revision 51821)
149209Scael.\" Copyright (c) 1991, 1991 The Regents of the University of California.
247170Sbostic.\" All rights reserved.
347170Sbostic.\"
450182Sbostic.\" This code is derived from software contributed to Berkeley by
550182Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
650182Sbostic.\"
749209Scael.\" %sccs.include.redist.roff%
847170Sbostic.\"
9*51821Sbostic.\"	@(#)cksum.1	5.6 (Berkeley) 11/27/91
1047170Sbostic.\"
1149209Scael.Dd
1249209Scael.Dt CKSUM 1
1349209Scael.Os
1449209Scael.Sh NAME
1549209Scael.Nm cksum
1649209Scael.Nd display file checksums and block counts
1749209Scael.Sh SYNOPSIS
1849209Scael.Nm cksum
1950101Sbostic.Op Fl o Op \&1 \&| \&2
2049209Scael.Op Ar file ...
2149209Scael.Sh DESCRIPTION
2247170SbosticThe
2349209Scael.Nm cksum
2447801Sbosticutility writes to standard output three whitespace separated fields for each
25*51821Sbosticinput file.
2649209ScaelThese fields are a checksum
2749209Scael.Tn CRC ,
28*51821Sbosticthe total number of octets in the file and the file name.
29*51821SbosticIf no file name is specified, the standard input is used and no file name
30*51821Sbosticis written.
3149209Scael.Pp
3247801SbosticThe options are as follows:
3349209Scael.Bl -tag -width indent
3449209Scael.It Fl o
3547801SbosticUse historic algorithms instead of the (superior) default one.
3649209Scael.Pp
3749209ScaelAlgorithm 1 is the algorithm used by historic
3849209Scael.Bx
3949209Scaelsystems as the
4049209Scael.Xr sum 1
4149209Scaelalgorithm and by historic
4249209Scael.At V
4349209Scaelsystems as the
4449209Scael.Xr sum
4549209Scaelalgorithm when using the
4649209Scael.Fl r
4749209Scaeloption.
4847801SbosticThis is a 16-bit checksum, with a right rotation before each addition;
4947801Sbosticoverflow is discarded.
5049209Scael.Pp
5149209ScaelAlgorithm 2 is the algorithm used by historic
5249209Scael.At V
5349209Scaelsystems as the
5447801Sbosticdefault
5549209Scael.Xr sum
5647801Sbosticalgorithm.
5747801SbosticThis is a 32-bit checksum, and is defined as follows:
5849209Scael.Bd -unfilled -offset indent
5947801Sbostics = sum of all bytes;
6047801Sbosticr = s % 2^16 + (s % 2^32) / 2^16;
6147801Sbosticcksum = (r % 2^16) + r / 2^16;
6249209Scael.Ed
6349209Scael.Pp
6447801SbosticBoth algorithm 1 and 2 write to standard output the same fields as
6547801Sbosticthe default algorithm except that the size of the file in bytes is
6647801Sbosticreplaced with the size of the file in blocks.
6747801SbosticFor historic reasons, the block size is 1024 for algorithm 1 and 512
6847801Sbosticfor algorithm 2.
6947801SbosticPartial blocks are rounded up.
7049209Scael.El
7149209Scael.Pp
7249209ScaelThe default
7349209Scael.Tn CRC
7449209Scaelused is based on the polynomial used for
7549209Scael.Tn CRC
7649209Scaelerror checking
7749209Scaelin the networking standard
7849209Scael.St -iso8802-3
7949209ScaelThe
8049209Scael.Tn CRC
8149209Scaelchecksum encoding is defined by the generating polynomial:
8249209Scael.Pp
8349209Scael.Bd -unfilled -offset indent
8447801SbosticG(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
8549209Scael     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
8649209Scael.Ed
8749209Scael.Pp
8849209ScaelMathematically, the
8949209Scael.Tn CRC
9049209Scaelvalue corresponding to a given file is defined by
9147170Sbosticthe following procedure:
9249209Scael.Bd -filled -offset indent
9347170SbosticThe
9449209Scael.Ar n
95*51821Sbosticbits to be evaluated are considered to be the coefficients of a mod 2
9647170Sbosticpolynomial M(x) of degree
9749209Scael.Ar n Ns \-1 .
98*51821SbosticThese
99*51821Sbostic.Ar n
100*51821Sbosticbits are the bits from the file, with the most significant bit being the most
101*51821Sbosticsignificant bit of the first octet of the file and the last bit being the least
102*51821Sbosticsignificant bit of the last octet, padded with zero bits (if necessary) to
103*51821Sbosticachieve an integral number of octets, followed by one or more octets
104*51821Sbosticrepresenting the length of the file as a binary value, least significant octet
105*51821Sbosticfirst.
106*51821SbosticThe smallest number of octets capable of representing this integer are used.
10749209Scael.Pp
10847170SbosticM(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
10947170SbosticG(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
11049209Scael.Pp
11147170SbosticThe coefficients of R(x) are considered to be a 32-bit sequence.
112*51821Sbostic.Pp
113*51821SbosticThe bit sequence is complemented and the result is the CRC.
11449209Scael.Ed
11549209Scael.Pp
11647170SbosticThe
11749209Scael.Nm cksum
11847170Sbosticutility exits 0 on success, and >0 if an error occurs.
11949209Scael.Sh SEE ALSO
120*51821SbosticThe default calculation is identical to that given in pseudo-code
121*51821Sbosticin the following
122*51821Sbostic.Tn ACM
123*51821Sbosticarticle.
12449209Scael.Rs
12549209Scael.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
12649209Scael.%A Dilip V. Sarwate
12749209Scael.%J "Communications of the \\*(tNACM\\*(sP"
12849209Scael.%D "August 1988"
12949209Scael.Re
13049209Scael.Sh STANDARDS
13149209Scael.Nm Cksum
13249209Scaelis expected to conform to
13349209Scael.St -p1003.2 .
13449209Scael.Sh HISTORY
13549209ScaelThe
13649209Scael.Nm cksum
13749209Scaelutility is
13849209Scael.Ud .
139