163678Sbostic.\" Copyright (c) 1991, 1993 263678Sbostic.\" The Regents of the University of California. 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*69080Sbostic.\" @(#)cksum.1 8.2 (Berkeley) 04/28/95 1047170Sbostic.\" 1149209Scael.Dd 1249209Scael.Dt CKSUM 1 1358795Scael.Os BSD 4.4 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 ... 21*69080Sbostic.Nm sum 22*69080Sbostic.Op Ar file ... 2349209Scael.Sh DESCRIPTION 2447170SbosticThe 2549209Scael.Nm cksum 2663677Smckusickutility writes to the standard output three whitespace separated 2763677Smckusickfields for each input file. 2849209ScaelThese fields are a checksum 2949209Scael.Tn CRC , 3051821Sbosticthe total number of octets in the file and the file name. 3151821SbosticIf no file name is specified, the standard input is used and no file name 3251821Sbosticis written. 3349209Scael.Pp 34*69080SbosticThe 35*69080Sbostic.Nm sum 36*69080Sbosticutility is identical to the 37*69080Sbostic.Nm cksum 38*69080Sbosticutility, except that it defaults to using historic algorithm 1, as 39*69080Sbosticdescribed below. 40*69080SbosticIt is provided for compatibility only. 41*69080Sbostic.Pp 4247801SbosticThe options are as follows: 4349209Scael.Bl -tag -width indent 4449209Scael.It Fl o 4547801SbosticUse historic algorithms instead of the (superior) default one. 4649209Scael.Pp 4749209ScaelAlgorithm 1 is the algorithm used by historic 4849209Scael.Bx 4949209Scaelsystems as the 5049209Scael.Xr sum 1 5149209Scaelalgorithm and by historic 5249209Scael.At V 5349209Scaelsystems as the 5449209Scael.Xr sum 5549209Scaelalgorithm when using the 5649209Scael.Fl r 5749209Scaeloption. 5847801SbosticThis is a 16-bit checksum, with a right rotation before each addition; 5947801Sbosticoverflow is discarded. 6049209Scael.Pp 6149209ScaelAlgorithm 2 is the algorithm used by historic 6249209Scael.At V 6349209Scaelsystems as the 6447801Sbosticdefault 6549209Scael.Xr sum 6647801Sbosticalgorithm. 6747801SbosticThis is a 32-bit checksum, and is defined as follows: 6849209Scael.Bd -unfilled -offset indent 6947801Sbostics = sum of all bytes; 7047801Sbosticr = s % 2^16 + (s % 2^32) / 2^16; 7147801Sbosticcksum = (r % 2^16) + r / 2^16; 7249209Scael.Ed 7349209Scael.Pp 7463677SmckusickBoth algorithm 1 and 2 write to the standard output the same fields as 7547801Sbosticthe default algorithm except that the size of the file in bytes is 7647801Sbosticreplaced with the size of the file in blocks. 7747801SbosticFor historic reasons, the block size is 1024 for algorithm 1 and 512 7847801Sbosticfor algorithm 2. 7947801SbosticPartial blocks are rounded up. 8049209Scael.El 8149209Scael.Pp 8249209ScaelThe default 8349209Scael.Tn CRC 8449209Scaelused is based on the polynomial used for 8549209Scael.Tn CRC 8649209Scaelerror checking 8749209Scaelin the networking standard 8849209Scael.St -iso8802-3 8949209ScaelThe 9049209Scael.Tn CRC 9149209Scaelchecksum encoding is defined by the generating polynomial: 9249209Scael.Pp 9349209Scael.Bd -unfilled -offset indent 9447801SbosticG(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + 9549209Scael x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 9649209Scael.Ed 9749209Scael.Pp 9849209ScaelMathematically, the 9949209Scael.Tn CRC 10049209Scaelvalue corresponding to a given file is defined by 10147170Sbosticthe following procedure: 10249209Scael.Bd -filled -offset indent 10347170SbosticThe 10449209Scael.Ar n 10551821Sbosticbits to be evaluated are considered to be the coefficients of a mod 2 10647170Sbosticpolynomial M(x) of degree 10749209Scael.Ar n Ns \-1 . 10851821SbosticThese 10951821Sbostic.Ar n 11051821Sbosticbits are the bits from the file, with the most significant bit being the most 11151821Sbosticsignificant bit of the first octet of the file and the last bit being the least 11251821Sbosticsignificant bit of the last octet, padded with zero bits (if necessary) to 11351821Sbosticachieve an integral number of octets, followed by one or more octets 11451821Sbosticrepresenting the length of the file as a binary value, least significant octet 11551821Sbosticfirst. 11651821SbosticThe smallest number of octets capable of representing this integer are used. 11749209Scael.Pp 11847170SbosticM(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by 11947170SbosticG(x) using mod 2 division, producing a remainder R(x) of degree <= 31. 12049209Scael.Pp 12147170SbosticThe coefficients of R(x) are considered to be a 32-bit sequence. 12251821Sbostic.Pp 12351821SbosticThe bit sequence is complemented and the result is the CRC. 12449209Scael.Ed 12549209Scael.Pp 12647170SbosticThe 12749209Scael.Nm cksum 128*69080Sbosticand 129*69080Sbostic.Nm sum 130*69080Sbosticutilities exit 0 on success, and >0 if an error occurs. 13149209Scael.Sh SEE ALSO 13251821SbosticThe default calculation is identical to that given in pseudo-code 13351821Sbosticin the following 13451821Sbostic.Tn ACM 13551821Sbosticarticle. 13649209Scael.Rs 13749209Scael.%T "Computation of Cyclic Redundancy Checks Via Table Lookup" 13849209Scael.%A Dilip V. Sarwate 13949209Scael.%J "Communications of the \\*(tNACM\\*(sP" 14049209Scael.%D "August 1988" 14149209Scael.Re 14249209Scael.Sh STANDARDS 14363677SmckusickThe 14463677Smckusick.Nm cksum 14563677Smckusickutility is expected to be POSIX 1003.2 compatible. 14649209Scael.Sh HISTORY 14749209ScaelThe 14849209Scael.Nm cksum 14958795Scaelutility appears in 15058795Scael.Bx 4.4 . 151