You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
13 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>crc32 - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package crc32</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "hash/crc32"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
  69. checksum. See <a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">http://en.wikipedia.org/wiki/Cyclic_redundancy_check</a> for
  70. information.
  71. </p>
  72. <p>
  73. Polynomials are represented in LSB-first form also known as reversed representation.
  74. </p>
  75. <p>
  76. See <a href="http://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials">http://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials</a>
  77. for information.
  78. </p>
  79. </div>
  80. </div>
  81. <div id="pkg-index" class="toggleVisible">
  82. <div class="collapsed">
  83. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  84. </div>
  85. <div class="expanded">
  86. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  87. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  88. <div id="manual-nav">
  89. <dl>
  90. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  91. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  92. <dd><a href="index.html#Checksum">func Checksum(data []byte, tab *Table) uint32</a></dd>
  93. <dd><a href="index.html#ChecksumIEEE">func ChecksumIEEE(data []byte) uint32</a></dd>
  94. <dd><a href="index.html#New">func New(tab *Table) hash.Hash32</a></dd>
  95. <dd><a href="index.html#NewIEEE">func NewIEEE() hash.Hash32</a></dd>
  96. <dd><a href="index.html#Update">func Update(crc uint32, tab *Table, p []byte) uint32</a></dd>
  97. <dd><a href="index.html#Table">type Table</a></dd>
  98. <dd>&nbsp; &nbsp; <a href="index.html#MakeTable">func MakeTable(poly uint32) *Table</a></dd>
  99. </dl>
  100. </div><!-- #manual-nav -->
  101. <div id="pkg-examples">
  102. <h4>Examples</h4>
  103. <dl>
  104. <dd><a class="exampleLink" href="index.html#example_MakeTable">MakeTable</a></dd>
  105. </dl>
  106. </div>
  107. <h4>Package files</h4>
  108. <p>
  109. <span style="font-size:90%">
  110. <a href="http://localhost:6060/src/hash/crc32/crc32.go">crc32.go</a>
  111. <a href="http://localhost:6060/src/hash/crc32/crc32_amd64.go">crc32_amd64.go</a>
  112. </span>
  113. </p>
  114. </div><!-- .expanded -->
  115. </div><!-- #pkg-index -->
  116. <div id="pkg-callgraph" class="toggle" style="display: none">
  117. <div class="collapsed">
  118. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  119. </div> <!-- .expanded -->
  120. <div class="expanded">
  121. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  122. <p>
  123. In the call graph viewer below, each node
  124. is a function belonging to this package
  125. and its children are the functions it
  126. calls&mdash;perhaps dynamically.
  127. </p>
  128. <p>
  129. The root nodes are the entry points of the
  130. package: functions that may be called from
  131. outside the package.
  132. There may be non-exported or anonymous
  133. functions among them if they are called
  134. dynamically from another package.
  135. </p>
  136. <p>
  137. Click a node to visit that function's source code.
  138. From there you can visit its callers by
  139. clicking its declaring <code>func</code>
  140. token.
  141. </p>
  142. <p>
  143. Functions may be omitted if they were
  144. determined to be unreachable in the
  145. particular programs or tests that were
  146. analyzed.
  147. </p>
  148. <!-- Zero means show all package entry points. -->
  149. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  150. </div>
  151. </div> <!-- #pkg-callgraph -->
  152. <h2 id="pkg-constants">Constants</h2>
  153. <pre>const (
  154. <span class="comment">// IEEE is by far and away the most common CRC-32 polynomial.</span>
  155. <span class="comment">// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...</span>
  156. <span id="IEEE">IEEE</span> = 0xedb88320
  157. <span class="comment">// Castagnoli&#39;s polynomial, used in iSCSI.</span>
  158. <span class="comment">// Has better error detection characteristics than IEEE.</span>
  159. <span class="comment">// http://dx.doi.org/10.1109/26.231911</span>
  160. <span id="Castagnoli">Castagnoli</span> = 0x82f63b78
  161. <span class="comment">// Koopman&#39;s polynomial.</span>
  162. <span class="comment">// Also has better error detection characteristics than IEEE.</span>
  163. <span class="comment">// http://dx.doi.org/10.1109/DSN.2002.1028931</span>
  164. <span id="Koopman">Koopman</span> = 0xeb31d82e
  165. )</pre>
  166. <p>
  167. Predefined polynomials.
  168. </p>
  169. <pre>const <span id="Size">Size</span> = 4</pre>
  170. <p>
  171. The size of a CRC-32 checksum in bytes.
  172. </p>
  173. <h2 id="pkg-variables">Variables</h2>
  174. <pre>var <span id="IEEETable">IEEETable</span> = makeTable(<a href="index.html#IEEE">IEEE</a>)</pre>
  175. <p>
  176. IEEETable is the table for the IEEE polynomial.
  177. </p>
  178. <h2 id="Checksum">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=4825:4870#L167">Checksum</a></h2>
  179. <pre>func Checksum(data []<a href="../../builtin/index.html#byte">byte</a>, tab *<a href="index.html#Table">Table</a>) <a href="../../builtin/index.html#uint32">uint32</a></pre>
  180. <p>
  181. Checksum returns the CRC-32 checksum of data
  182. using the polynomial represented by the Table.
  183. </p>
  184. <h2 id="ChecksumIEEE">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=4986:5023#L171">ChecksumIEEE</a></h2>
  185. <pre>func ChecksumIEEE(data []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#uint32">uint32</a></pre>
  186. <p>
  187. ChecksumIEEE returns the CRC-32 checksum of data
  188. using the IEEE polynomial.
  189. </p>
  190. <h2 id="New">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=3160:3192#L107">New</a></h2>
  191. <pre>func New(tab *<a href="index.html#Table">Table</a>) <a href="../index.html">hash</a>.<a href="../index.html#Hash32">Hash32</a></pre>
  192. <p>
  193. New creates a new hash.Hash32 computing the CRC-32 checksum
  194. using the polynomial represented by the Table.
  195. Its Sum method will lay the value out in big-endian byte order.
  196. </p>
  197. <h2 id="NewIEEE">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=3385:3411#L112">NewIEEE</a></h2>
  198. <pre>func NewIEEE() <a href="../index.html">hash</a>.<a href="../index.html#Hash32">Hash32</a></pre>
  199. <p>
  200. NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
  201. using the IEEE polynomial.
  202. Its Sum method will lay the value out in big-endian byte order.
  203. </p>
  204. <h2 id="Update">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=4236:4288#L143">Update</a></h2>
  205. <pre>func Update(crc <a href="../../builtin/index.html#uint32">uint32</a>, tab *<a href="index.html#Table">Table</a>, p []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#uint32">uint32</a></pre>
  206. <p>
  207. Update returns the result of adding the bytes in p to the crc.
  208. </p>
  209. <h2 id="Table">type <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=1266:1288#L31">Table</a></h2>
  210. <pre>type Table [256]<a href="../../builtin/index.html#uint32">uint32</a></pre>
  211. <p>
  212. Table is a 256-word table representing the polynomial for efficient processing.
  213. </p>
  214. <h3 id="MakeTable">func <a href="http://localhost:6060/src/hash/crc32/crc32.go?s=2047:2081#L56">MakeTable</a></h3>
  215. <pre>func MakeTable(poly <a href="../../builtin/index.html#uint32">uint32</a>) *<a href="index.html#Table">Table</a></pre>
  216. <p>
  217. MakeTable returns a Table constructed from the specified polynomial.
  218. The contents of this Table must not be modified.
  219. </p>
  220. <div id="example_MakeTable" class="toggle">
  221. <div class="collapsed">
  222. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  223. </div>
  224. <div class="expanded">
  225. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  226. <p>Code:</p>
  227. <pre class="code"><span class="comment">// In this package, the CRC polynomial is represented in reversed notation,</span>
  228. <span class="comment">// or LSB-first representation.</span>
  229. <span class="comment">//</span>
  230. <span class="comment">// LSB-first representation is a hexadecimal number with n bits, in which the</span>
  231. <span class="comment">// most significant bit represents the coefficient of x⁰ and the least significant</span>
  232. <span class="comment">// bit represents the coefficient of xⁿ⁻¹ (the coefficient for xⁿ is implicit).</span>
  233. <span class="comment">//</span>
  234. <span class="comment">// For example, CRC32-Q, as defined by the following polynomial,</span>
  235. <span class="comment">// x³²+ x³¹+ x²⁴+ x²²+ x¹⁶+ x¹⁴+ x⁸+ x⁷+ x⁵+ x³+ x¹+ x⁰</span>
  236. <span class="comment">// has the reversed notation 0b11010101100000101000001010000001, so the value</span>
  237. <span class="comment">// that should be passed to MakeTable is 0xD5828281.</span>
  238. crc32q := crc32.MakeTable(0xD5828281)
  239. fmt.Printf(&#34;%08x\n&#34;, crc32.Checksum([]byte(&#34;Hello world&#34;), crc32q))
  240. <span class="comment"></pre>
  241. <p>Output:</p>
  242. <pre class="output">2964d064
  243. </pre>
  244. </div>
  245. </div>
  246. <div id="footer">
  247. Build version go1.6.<br>
  248. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  249. the content of this page is licensed under the
  250. Creative Commons Attribution 3.0 License,
  251. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  252. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  253. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  254. </div>
  255. </div><!-- .container -->
  256. </div><!-- #page -->
  257. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  258. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  259. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  260. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  261. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  262. </body>
  263. </html>