• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Theoretical CRC Computer</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../crc.html" title="Chapter 12. Boost.CRC 1.5">
10<link rel="prev" href="introduction.html" title="Introduction">
11<link rel="next" href="crc_optimal.html" title="Optimized CRC Computer">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="introduction.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../crc.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="crc_optimal.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="crc.crc_basic"></a><a class="link" href="crc_basic.html" title="Theoretical CRC Computer">Theoretical CRC Computer</a>
29</h2></div></div></div>
30<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cstddef</span><span class="special">&gt;</span>  <span class="comment">// for std::size_t</span>
31
32<span class="keyword">namespace</span> <span class="identifier">boost</span>
33<span class="special">{</span>
34    <span class="keyword">template</span> <span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Bits</span> <span class="special">&gt;</span>
35    <span class="keyword">class</span> <span class="identifier">crc_basic</span><span class="special">;</span>
36<span class="special">}</span>
37</pre>
38<p>
39      The <span class="bold"><strong><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_basic</span></code></strong></span>
40      class template acts as an unaugmented-CRC processor that can accept input at
41      the bit-level. It only takes one Rocksoft™ Model CRC Algorithm parameter
42      as a template parameter, the <span class="emphasis"><em>WIDTH</em></span>, which determines the
43      built-in unsigned integer type used for division registers. The other Rocksoft™
44      Model CRC Algorithm parameters can be passed on through the constructor. (Most
45      of the parameters have defaults.)
46    </p>
47<p>
48      The integer type used for registers is published as <code class="computeroutput"><span class="identifier">value_type</span></code>,
49      while the Rocksoft™ Model CRC Algorithm attributes can be discovered
50      as:
51    </p>
52<div class="table">
53<a name="crc.crc_basic.crc_basic_rmca"></a><p class="title"><b>Table 12.1. RMCA Parameters in boost::crc_basic</b></p>
54<div class="table-contents"><table class="table" summary="RMCA Parameters in boost::crc_basic">
55<colgroup>
56<col>
57<col>
58<col>
59<col>
60</colgroup>
61<thead><tr>
62<th>
63              <p>
64                Parameter
65              </p>
66            </th>
67<th>
68              <p>
69                Member Name
70              </p>
71            </th>
72<th>
73              <p>
74                Kind
75              </p>
76            </th>
77<th>
78              <p>
79                Expression Type
80              </p>
81            </th>
82</tr></thead>
83<tbody>
84<tr>
85<td>
86              <p>
87                <span class="emphasis"><em>WIDTH</em></span>
88              </p>
89            </td>
90<td>
91              <p>
92                <code class="computeroutput"><span class="identifier">bit_count</span></code>
93              </p>
94            </td>
95<td>
96              <p>
97                class-static immutable data member
98              </p>
99            </td>
100<td>
101              <p>
102                <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>
103              </p>
104            </td>
105</tr>
106<tr>
107<td>
108              <p>
109                <span class="emphasis"><em>POLY</em></span>
110              </p>
111            </td>
112<td>
113              <p>
114                <code class="computeroutput"><span class="identifier">get_truncated_polynominal</span></code>
115              </p>
116            </td>
117<td>
118              <p>
119                <code class="computeroutput"><span class="keyword">const</span></code> member function
120              </p>
121            </td>
122<td>
123              <p>
124                <code class="computeroutput"><span class="identifier">value_type</span></code>
125              </p>
126            </td>
127</tr>
128<tr>
129<td>
130              <p>
131                <span class="emphasis"><em>INIT</em></span>
132              </p>
133            </td>
134<td>
135              <p>
136                <code class="computeroutput"><span class="identifier">get_initial_remainder</span></code>
137              </p>
138            </td>
139<td>
140              <p>
141                <code class="computeroutput"><span class="keyword">const</span></code> member function
142              </p>
143            </td>
144<td>
145              <p>
146                <code class="computeroutput"><span class="identifier">value_type</span></code>
147              </p>
148            </td>
149</tr>
150<tr>
151<td>
152              <p>
153                <span class="emphasis"><em>REFIN</em></span>
154              </p>
155            </td>
156<td>
157              <p>
158                <code class="computeroutput"><span class="identifier">get_reflect_input</span></code>
159              </p>
160            </td>
161<td>
162              <p>
163                <code class="computeroutput"><span class="keyword">const</span></code> member function
164              </p>
165            </td>
166<td>
167              <p>
168                <code class="computeroutput"><span class="keyword">bool</span></code>
169              </p>
170            </td>
171</tr>
172<tr>
173<td>
174              <p>
175                <span class="emphasis"><em>REFOUT</em></span>
176              </p>
177            </td>
178<td>
179              <p>
180                <code class="computeroutput"><span class="identifier">get_reflect_remainder</span></code>
181              </p>
182            </td>
183<td>
184              <p>
185                <code class="computeroutput"><span class="keyword">const</span></code> member function
186              </p>
187            </td>
188<td>
189              <p>
190                <code class="computeroutput"><span class="keyword">bool</span></code>
191              </p>
192            </td>
193</tr>
194<tr>
195<td>
196              <p>
197                <span class="emphasis"><em>XOROUT</em></span>
198              </p>
199            </td>
200<td>
201              <p>
202                <code class="computeroutput"><span class="identifier">get_final_xor_value</span></code>
203              </p>
204            </td>
205<td>
206              <p>
207                <code class="computeroutput"><span class="keyword">const</span></code> member function
208              </p>
209            </td>
210<td>
211              <p>
212                <code class="computeroutput"><span class="identifier">value_type</span></code>
213              </p>
214            </td>
215</tr>
216</tbody>
217</table></div>
218</div>
219<br class="table-break"><p>
220      Since most of the parameters are specified at run-time, you can reuse a single
221      computer object for separate runs with differing parameters. The type uses
222      the automatically-defined copy/move/assign and destruction routines.
223    </p>
224<p>
225      Here's an example of reuse:
226    </p>
227<p>
228</p>
229<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">unsigned</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">&gt;</span> <span class="identifier">crc_16_and_xmodem</span><span class="special">(</span> <span class="keyword">void</span> <span class="keyword">const</span> <span class="special">*</span><span class="identifier">b</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">l</span> <span class="special">)</span>
230<span class="special">{</span>
231    <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">unsigned</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">&gt;</span>  <span class="identifier">result</span><span class="special">;</span>
232    <a class="co" name="crc.crc_basic.c0" href="crc_basic.html#crc.crc_basic.c1"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_basic</span><span class="special">&lt;</span><span class="number">16</span><span class="special">&gt;</span>           <span class="identifier">crc1</span><span class="special">(</span> <span class="number">0x8005u</span><span class="special">,</span> <span class="number">0u</span><span class="special">,</span> <span class="number">0u</span><span class="special">,</span> <span class="keyword">true</span><span class="special">,</span> <span class="keyword">true</span> <span class="special">);</span>
233
234    <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">process_bytes</span><span class="special">(</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">l</span> <span class="special">);</span>
235    <span class="identifier">result</span><span class="special">.</span><span class="identifier">first</span> <span class="special">=</span> <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">checksum</span><span class="special">();</span>
236    <a class="co" name="crc.crc_basic.c2" href="crc_basic.html#crc.crc_basic.c3"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="identifier">crc1</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_basic</span><span class="special">&lt;</span><span class="number">16</span><span class="special">&gt;(</span> <span class="number">0x8408u</span><span class="special">,</span> <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">get_initial_remainder</span><span class="special">(),</span>
237     <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">get_final_xor_value</span><span class="special">(),</span> <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">get_reflect_input</span><span class="special">(),</span>
238     <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">get_reflect_remainder</span><span class="special">()</span> <span class="special">);</span>
239    <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">process_bytes</span><span class="special">(</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">l</span> <span class="special">);</span>
240    <span class="identifier">result</span><span class="special">.</span><span class="identifier">second</span> <span class="special">=</span> <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">checksum</span><span class="special">();</span>
241
242    <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
243<span class="special">}</span>
244</pre>
245<p>
246    </p>
247<p>
248      For now, most Rocksoft™ Model CRC Algorithm parameters can only be changed
249      through assignment to the entire object.
250    </p>
251<div class="calloutlist"><table border="0" summary="Callout list">
252<tr>
253<td width="5%" valign="top" align="left"><p><a name="crc.crc_basic.c1"></a><a href="#crc.crc_basic.c0"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
254<td valign="top" align="left"><p>
255          The parameters are based on <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_16_type</span></code>.
256        </p></td>
257</tr>
258<tr>
259<td width="5%" valign="top" align="left"><p><a name="crc.crc_basic.c3"></a><a href="#crc.crc_basic.c2"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
260<td valign="top" align="left"><p>
261          Change the parameters to match <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_xmodem_type</span></code>.
262        </p></td>
263</tr>
264</table></div>
265<p>
266      This example necessarily demonstrates input and output. There is one output
267      member function, <code class="computeroutput"><span class="identifier">checksum</span></code> that
268      returns the remainder from the CRC steps plus any post-processing reflection
269      and/or XOR-masking. There are several options to submit input data for processing:
270    </p>
271<div class="table">
272<a name="crc.crc_basic.crc_basic_input"></a><p class="title"><b>Table 12.2. Member Functions for Input in boost::crc_basic</b></p>
273<div class="table-contents"><table class="table" summary="Member Functions for Input in boost::crc_basic">
274<colgroup>
275<col>
276<col>
277</colgroup>
278<thead><tr>
279<th>
280              <p>
281                Member Function
282              </p>
283            </th>
284<th>
285              <p>
286                Input Type/Style
287              </p>
288            </th>
289</tr></thead>
290<tbody>
291<tr>
292<td>
293              <p>
294                <code class="computeroutput"><span class="identifier">process_bit</span></code>
295              </p>
296            </td>
297<td>
298              <p>
299                A single bit.
300              </p>
301            </td>
302</tr>
303<tr>
304<td>
305              <p>
306                <code class="computeroutput"><span class="identifier">process_bits</span></code>
307              </p>
308            </td>
309<td>
310              <p>
311                A specified number of bits within the given byte. Reading starts
312                from the highest-order desired bit.
313              </p>
314            </td>
315</tr>
316<tr>
317<td>
318              <p>
319                <code class="computeroutput"><span class="identifier">process_byte</span></code>
320              </p>
321            </td>
322<td>
323              <p>
324                An entire byte. The bits within the byte are read in an order consistent
325                with <code class="computeroutput"><span class="identifier">get_reflect_input</span></code>.
326              </p>
327            </td>
328</tr>
329<tr>
330<td>
331              <p>
332                <code class="computeroutput"><span class="identifier">process_block</span></code>
333              </p>
334            </td>
335<td>
336              <p>
337                All bytes in the range. The range is defined by a pointer to the
338                first byte and another pointer to one (byte) past-the-end.
339              </p>
340            </td>
341</tr>
342<tr>
343<td>
344              <p>
345                <code class="computeroutput"><span class="identifier">process_bytes</span></code>
346              </p>
347            </td>
348<td>
349              <p>
350                All bytes in the range. The range is defined by a pointer to the
351                first byte and a count of number of bytes to read.
352              </p>
353            </td>
354</tr>
355</tbody>
356</table></div>
357</div>
358<br class="table-break"><p>
359      The input functions currently do not return anything.
360    </p>
361<p>
362      Persistent objects mean that the data doesn't have to be in one block:
363    </p>
364<p>
365</p>
366<pre class="programlisting"><span class="keyword">unsigned</span>  <span class="identifier">combined_crc_16</span><span class="special">(</span> <span class="keyword">unsigned</span> <span class="identifier">block_count</span><span class="special">,</span> <span class="special">...</span> <span class="special">)</span>
367<span class="special">{</span>
368    <a class="co" name="crc.crc_basic.c4" href="crc_basic.html#crc.crc_basic.c5"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
369
370    <a class="co" name="crc.crc_basic.c6" href="crc_basic.html#crc.crc_basic.c7"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_basic</span><span class="special">&lt;</span><span class="number">16</span><span class="special">&gt;</span>  <span class="identifier">crc1</span><span class="special">(</span> <span class="number">0x8005u</span><span class="special">,</span> <span class="number">0u</span><span class="special">,</span> <span class="number">0u</span><span class="special">,</span> <span class="keyword">true</span><span class="special">,</span> <span class="keyword">true</span> <span class="special">);</span>
371    <span class="identifier">va_list</span>               <span class="identifier">ap</span><span class="special">;</span>
372
373    <span class="identifier">va_start</span><span class="special">(</span> <span class="identifier">ap</span><span class="special">,</span> <span class="identifier">block_count</span> <span class="special">);</span>
374    <span class="keyword">while</span> <span class="special">(</span> <span class="identifier">block_count</span><span class="special">--</span> <span class="special">)</span>
375    <span class="special">{</span>
376        <span class="keyword">void</span> <span class="keyword">const</span> <span class="special">*</span> <span class="keyword">const</span>  <span class="identifier">bs</span> <span class="special">=</span> <span class="identifier">va_arg</span><span class="special">(</span> <span class="identifier">ap</span><span class="special">,</span> <span class="keyword">void</span> <span class="keyword">const</span> <span class="special">*</span> <span class="special">);</span>
377        <span class="identifier">size_t</span> <span class="keyword">const</span>        <span class="identifier">bl</span> <span class="special">=</span> <span class="identifier">va_arg</span><span class="special">(</span> <span class="identifier">ap</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="special">);</span>
378
379        <a class="co" name="crc.crc_basic.c8" href="crc_basic.html#crc.crc_basic.c9"><img src="../../../doc/src/images/callouts/3.png" alt="3" border="0"></a><span class="identifier">crc1</span><span class="special">.</span><span class="identifier">process_bytes</span><span class="special">(</span> <span class="identifier">bs</span><span class="special">,</span> <span class="identifier">bl</span> <span class="special">);</span>
380    <span class="special">}</span>
381    <span class="identifier">va_end</span><span class="special">(</span> <span class="identifier">ap</span> <span class="special">);</span>
382
383    <span class="keyword">return</span> <span class="identifier">crc1</span><span class="special">.</span><span class="identifier">checksum</span><span class="special">();</span>
384<span class="special">}</span>
385</pre>
386<p>
387    </p>
388<p>
389      No CRC operation throws, so there is no need for extra protection between the
390      varargs macro calls.
391    </p>
392<div class="calloutlist"><table border="0" summary="Callout list">
393<tr>
394<td width="5%" valign="top" align="left"><p><a name="crc.crc_basic.c5"></a><a href="#crc.crc_basic.c4"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
395<td valign="top" align="left"><p>
396          C-style variable-argument routines are or may be macros.
397        </p></td>
398</tr>
399<tr>
400<td width="5%" valign="top" align="left"><p><a name="crc.crc_basic.c7"></a><a href="#crc.crc_basic.c6"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
401<td valign="top" align="left"><p>
402          The parameters are based on <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">crc_16_type</span></code>.
403        </p></td>
404</tr>
405<tr>
406<td width="5%" valign="top" align="left"><p><a name="crc.crc_basic.c9"></a><a href="#crc.crc_basic.c8"><img src="../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td>
407<td valign="top" align="left"><p>
408          The <code class="computeroutput"><span class="identifier">va_arg</span></code> calls were within
409          the <code class="computeroutput"><span class="identifier">process_bytes</span></code> call,
410          but I remembered that calling order is not guaranteed among function arguments,
411          so I need explicit object declarations to enforce the extraction order.
412        </p></td>
413</tr>
414</table></div>
415<p>
416      The input-influenced state of a computer object may be read with the <code class="computeroutput"><span class="identifier">get_interim_remainder</span></code> member function. An
417      object may be loaded with the same kind of state with the one-argument version
418      of the <code class="computeroutput"><span class="identifier">reset</span></code> member function.
419      The state is the remainder from the CRC operations performed on all the already-entered
420      input, without any output post-processing.
421    </p>
422<p>
423      The two functions can be used together to save the state to a persistent medium
424      and restore it later. Note that the Rocksoft™ Model CRC Algorithm parameters
425      have to saved/restored via other means, if they're not fixed within a program.
426    </p>
427<p>
428      Calling <code class="computeroutput"><span class="identifier">reset</span></code> with no arguments
429      sets the interim remainder to what <span class="emphasis"><em>INIT</em></span> was set at object
430      construction. This lets one computer object be used for independent runs with
431      separate data messages using the same CRC standard.
432    </p>
433</div>
434<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
435<td align="left"></td>
436<td align="right"><div class="copyright-footer">Copyright © 2001, 2003, 2012 Daryle Walker<p>
437        Distributed under the Boost Software License, Version 1.0. (See the accompanying
438        file LICENSE_1_0.txt or a copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
439      </p>
440</div></td>
441</tr></table>
442<hr>
443<div class="spirit-nav">
444<a accesskey="p" href="introduction.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../crc.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="crc_optimal.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
445</div>
446</body>
447</html>
448