1*63678Sbostic.\" Copyright (c) 1991, 1993 2*63678Sbostic.\" 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*63678Sbostic.\" @(#)cksum.1 8.1 (Berkeley) 06/29/93 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 ... 2149209Scael.Sh DESCRIPTION 2247170SbosticThe 2349209Scael.Nm cksum 2463677Smckusickutility writes to the standard output three whitespace separated 2563677Smckusickfields for each input file. 2649209ScaelThese fields are a checksum 2749209Scael.Tn CRC , 2851821Sbosticthe total number of octets in the file and the file name. 2951821SbosticIf no file name is specified, the standard input is used and no file name 3051821Sbosticis 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 6463677SmckusickBoth algorithm 1 and 2 write to the 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 9551821Sbosticbits to be evaluated are considered to be the coefficients of a mod 2 9647170Sbosticpolynomial M(x) of degree 9749209Scael.Ar n Ns \-1 . 9851821SbosticThese 9951821Sbostic.Ar n 10051821Sbosticbits are the bits from the file, with the most significant bit being the most 10151821Sbosticsignificant bit of the first octet of the file and the last bit being the least 10251821Sbosticsignificant bit of the last octet, padded with zero bits (if necessary) to 10351821Sbosticachieve an integral number of octets, followed by one or more octets 10451821Sbosticrepresenting the length of the file as a binary value, least significant octet 10551821Sbosticfirst. 10651821SbosticThe 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. 11251821Sbostic.Pp 11351821SbosticThe 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 12051821SbosticThe default calculation is identical to that given in pseudo-code 12151821Sbosticin the following 12251821Sbostic.Tn ACM 12351821Sbosticarticle. 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 13163677SmckusickThe 13263677Smckusick.Nm cksum 13363677Smckusickutility is expected to be POSIX 1003.2 compatible. 13449209Scael.Sh HISTORY 13549209ScaelThe 13649209Scael.Nm cksum 13758795Scaelutility appears in 13858795Scael.Bx 4.4 . 139