OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / tools / sg-cksum / cksum.1
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.2 (Berkeley) 4/28/95
36 .\"     $Id: cksum.1,v 1.1 2006-07-27 23:37:04 damion Exp $
37 .\"
38 .Dd April 28, 1995
39 .Dt CKSUM 1
40 .Os BSD 4.4
41 .Sh NAME
42 .Nm cksum
43 .Nd display file checksums and block counts
44 .Sh SYNOPSIS
45 .Nm cksum
46 .Oo 
47 .Fl o Ar \&1 Xo
48 .No \&|
49 .Ar \&2
50 .No \&|
51 .Ar \&3
52 .Xc
53 .Oc
54 .Op Ar file ...
55 .Nm sum
56 .Op Ar file ...
57 .Sh DESCRIPTION
58 The
59 .Nm cksum
60 utility writes to the standard output three whitespace separated
61 fields for each input file.
62 These fields are a checksum
63 .Tn CRC ,
64 the total number of octets in the file and the file name.
65 If no file name is specified, the standard input is used and no file name
66 is written.
67 .Pp
68 The
69 .Nm sum
70 utility is identical to the
71 .Nm cksum
72 utility, except that it defaults to using historic algorithm 1, as
73 described below.
74 It is provided for compatibility only.
75 .Pp
76 The options are as follows:
77 .Bl -tag -width indent
78 .It Fl o
79 Use historic algorithms instead of the (superior) default one.
80 .Pp
81 Algorithm 1 is the algorithm used by historic
82 .Bx
83 systems as the 
84 .Xr sum 1
85 algorithm and by historic
86 .At V
87 systems as the
88 .Xr sum
89 algorithm when using the
90 .Fl r
91 option.
92 This is a 16-bit checksum, with a right rotation before each addition;
93 overflow is discarded.
94 .Pp
95 Algorithm 2 is the algorithm used by historic
96 .At V
97 systems as the
98 default
99 .Xr sum
100 algorithm.
101 This is a 32-bit checksum, and is defined as follows:
102 .Bd -unfilled -offset indent
103 s = sum of all bytes;
104 r = s % 2^16 + (s % 2^32) / 2^16;
105 cksum = (r % 2^16) + r / 2^16;
106 .Ed
107 .Pp
108 Algorithm 3 is what is commonly called the
109 .Ql 32bit CRC
110 algorithm.
111 This is a 32-bit checksum.
112 .Pp
113 Both algorithm 1 and 2 write to the standard output the same fields as
114 the default algorithm except that the size of the file in bytes is
115 replaced with the size of the file in blocks.
116 For historic reasons, the block size is 1024 for algorithm 1 and 512
117 for algorithm 2.
118 Partial blocks are rounded up.
119 .El
120 .Pp
121 The default
122 .Tn CRC
123 used is based on the polynomial used for
124 .Tn CRC
125 error checking
126 in the networking standard
127 .St -iso8802-3
128 The
129 .Tn CRC
130 checksum encoding is defined by the generating polynomial:
131 .Pp
132 .Bd -unfilled -offset indent
133 G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
134      x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
135 .Ed
136 .Pp
137 Mathematically, the
138 .Tn CRC
139 value corresponding to a given file is defined by
140 the following procedure:
141 .Bd -filled -offset indent
142 The
143 .Ar n
144 bits to be evaluated are considered to be the coefficients of a mod 2
145 polynomial M(x) of degree
146 .Ar n Ns \-1 .
147 These
148 .Ar n
149 bits are the bits from the file, with the most significant bit being the most
150 significant bit of the first octet of the file and the last bit being the least
151 significant bit of the last octet, padded with zero bits (if necessary) to
152 achieve an integral number of octets, followed by one or more octets
153 representing the length of the file as a binary value, least significant octet
154 first.
155 The smallest number of octets capable of representing this integer are used.
156 .Pp
157 M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
158 G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
159 .Pp
160 The coefficients of R(x) are considered to be a 32-bit sequence.
161 .Pp
162 The bit sequence is complemented and the result is the CRC.
163 .Ed
164 .Pp
165 The
166 .Nm cksum
167 and
168 .Nm sum
169 utilities exit 0 on success, and >0 if an error occurs.
170 .Sh SEE ALSO
171 .Xr md5 1
172 .Rs
173 The default calculation is identical to that given in pseudo-code
174 in the following
175 .Tn ACM
176 article.
177 .Rs
178 .%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
179 .%A Dilip V. Sarwate
180 .%J "Communications of the \\*(tNACM\\*(sP"
181 .%D "August 1988"
182 .Re
183 .Sh STANDARDS
184 The
185 .Nm cksum
186 utility is expected to conform to
187 .St -p1003.2-92 .
188 .Sh HISTORY
189 The
190 .Nm cksum
191 utility appeared in
192 .Bx 4.4 .