149209Scael.\" Copyright (c) 1991, 1991 The Regents of the University of California. 247170Sbostic.\" All rights reserved. 347170Sbostic.\" 449209Scael.\" %sccs.include.redist.roff% 547170Sbostic.\" 6*50101Sbostic.\" @(#)cksum.1 5.4 (Berkeley) 06/20/91 747170Sbostic.\" 849209Scael.Dd 949209Scael.Dt CKSUM 1 1049209Scael.Os 1149209Scael.Sh NAME 1249209Scael.Nm cksum 1349209Scael.Nd display file checksums and block counts 1449209Scael.Sh SYNOPSIS 1549209Scael.Nm cksum 16*50101Sbostic.Op Fl o Op \&1 \&| \&2 1749209Scael.Op Ar file ... 1849209Scael.Sh DESCRIPTION 1947170SbosticThe 2049209Scael.Nm cksum 2147801Sbosticutility writes to standard output three whitespace separated fields for each 2247801Sbosticinput file (or the standard input by default). 2349209ScaelThese fields are a checksum 2449209Scael.Tn CRC , 2549209Scaelthe total number of bytes in the file and 2647801Sbosticthe file name. 2749209Scael.Pp 2847801SbosticThe options are as follows: 2949209Scael.Bl -tag -width indent 3049209Scael.It Fl o 3147801SbosticUse historic algorithms instead of the (superior) default one. 3249209Scael.Pp 3349209ScaelAlgorithm 1 is the algorithm used by historic 3449209Scael.Bx 3549209Scaelsystems as the 3649209Scael.Xr sum 1 3749209Scaelalgorithm and by historic 3849209Scael.At V 3949209Scaelsystems as the 4049209Scael.Xr sum 4149209Scaelalgorithm when using the 4249209Scael.Fl r 4349209Scaeloption. 4447801SbosticThis is a 16-bit checksum, with a right rotation before each addition; 4547801Sbosticoverflow is discarded. 4649209Scael.Pp 4749209ScaelAlgorithm 2 is the algorithm used by historic 4849209Scael.At V 4949209Scaelsystems as the 5047801Sbosticdefault 5149209Scael.Xr sum 5247801Sbosticalgorithm. 5347801SbosticThis is a 32-bit checksum, and is defined as follows: 5449209Scael.Bd -unfilled -offset indent 5547801Sbostics = sum of all bytes; 5647801Sbosticr = s % 2^16 + (s % 2^32) / 2^16; 5747801Sbosticcksum = (r % 2^16) + r / 2^16; 5849209Scael.Ed 5949209Scael.Pp 6047801SbosticBoth algorithm 1 and 2 write to standard output the same fields as 6147801Sbosticthe default algorithm except that the size of the file in bytes is 6247801Sbosticreplaced with the size of the file in blocks. 6347801SbosticFor historic reasons, the block size is 1024 for algorithm 1 and 512 6447801Sbosticfor algorithm 2. 6547801SbosticPartial blocks are rounded up. 6649209Scael.El 6749209Scael.Pp 6849209ScaelThe default 6949209Scael.Tn CRC 7049209Scaelused is based on the polynomial used for 7149209Scael.Tn CRC 7249209Scaelerror checking 7349209Scaelin the networking standard 7449209Scael.St -iso8802-3 7549209ScaelThe 7649209Scael.Tn CRC 7749209Scaelchecksum encoding is defined by the generating polynomial: 7849209Scael.Pp 7949209Scael.Bd -unfilled -offset indent 8047801SbosticG(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + 8149209Scael x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 8249209Scael.Ed 8349209Scael.Pp 8449209ScaelMathematically, the 8549209Scael.Tn CRC 8649209Scaelvalue corresponding to a given file is defined by 8747170Sbosticthe following procedure: 8849209Scael.Bd -filled -offset indent 8947170SbosticThe 9049209Scael.Ar n 9147170Sbosticbits of the file are considered to be the coefficients of a mod 2 9247170Sbosticpolynomial M(x) of degree 9349209Scael.Ar n Ns \-1 . 9449209Scael.Pp 9547170SbosticM(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by 9647170SbosticG(x) using mod 2 division, producing a remainder R(x) of degree <= 31. 9747170SbosticDuring the division, each time the intermediate remainder is zero, it 9849209Scaelis changed to the next value from a predefined sequence of 9949209Scael32-bit integers before completing the division. 10047170SbosticThis sequence is long and complex -- see the source code for more 10147170Sbosticinformation. 10249209Scael.Pp 10347170SbosticThe coefficients of R(x) are considered to be a 32-bit sequence. 10449209Scael.Ed 10549209Scael.Pp 10647170SbosticThe calculation used is identical to that given in pseudo-code in 10749209Scaelthe 10849209Scael.Tn ACM 10949209Scaelarticle referenced below. 11049209Scael.Pp 11147170SbosticThe 11249209Scael.Nm cksum 11347170Sbosticutility exits 0 on success, and >0 if an error occurs. 11449209Scael.Sh SEE ALSO 11549209Scael.Rs 11649209Scael.%T "Computation of Cyclic Redundancy Checks Via Table Lookup" 11749209Scael.%A Dilip V. Sarwate 11849209Scael.%J "Communications of the \\*(tNACM\\*(sP" 11949209Scael.%D "August 1988" 12049209Scael.Re 12149209Scael.Sh STANDARDS 12249209Scael.Nm Cksum 12349209Scaelis expected to conform to 12449209Scael.St -p1003.2 . 12549209Scael.Sh HISTORY 12649209ScaelThe 12749209Scael.Nm cksum 12849209Scaelutility is 12949209Scael.Ud . 130