Open-Source-Software-Entwicklung und Downloads

Browse Subversion Repository

Diff of /trunk/doc/en/html/macro/command/crc32.html

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3226 by maya, Tue Mar 24 09:37:20 2009 UTC revision 3227 by maya, Tue Mar 24 15:10:33 2009 UTC
# Line 1  Line 1 
1  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2    "http://www.w3.org/TR/html4/strict.dtd">    "http://www.w3.org/TR/html4/strict.dtd">
3  <html>  <html>
4  <head>  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6    <title>crc32</title>    <title>crc32</title>
7    <meta http-equiv="Content-Style-Type" content="text/css">    <meta http-equiv="Content-Style-Type" content="text/css">
8    <link rel="stylesheet" href="../../style.css" type="text/css">    <link rel="stylesheet" href="../../style.css" type="text/css">
9  </head>  </head>
10    
11  <body>  <body>
12    
13    
14  <h1>crc32</h1>  <h1>crc32</h1>
15    
16  <p>  <p>
17  Calculates the CRC-32 of a string or a file.  Calculates the CRC-32 of a string or a file.
18  </p>  </p>
19    
20  <pre class="macro-syntax">  <pre class="macro-syntax">
21  crc32 &lt;intvar&gt; &lt;string&gt;  crc32 &lt;intvar&gt; &lt;string&gt;
22  crc32file &lt;intvar&gt; &lt;filename&gt;  crc32file &lt;intvar&gt; &lt;filename&gt;
23  </pre>  </pre>
24    
25  <h2>Remarks</h2>  <h2>Remarks</h2>
26    
27  <p>  <p>
28  This macro function calculates the CRC(Cyclic Redundancy Checking) of a string or a file. The polynomial expression(right rotation) is as follows:  This macro function calculates the CRC(Cyclic Redundancy Checking) of a string or a file. The polynomial expression(right rotation) is as follows:
29  <br><br>  <br><br>
30    
31  100000100110000010001110110110111 (x<sup>32</sup>+x<sup>26</sup>+x<sup>23</sup>+x<sup>22</sup>+x<sup>16</sup>+x<sup>12</sup>+x<sup>11</sup>+x<sup>10</sup>+x<sup>8</sup>+x<sup>7</sup>+x<sup>5</sup>+x<sup>4</sup>+x<sup>2</sup>+x<sup>1</sup>+x<sup>0</sup>)  100000100110000010001110110110111 (x<sup>32</sup>+x<sup>26</sup>+x<sup>23</sup>+x<sup>22</sup>+x<sup>16</sup>+x<sup>12</sup>+x<sup>11</sup>+x<sup>10</sup>+x<sup>8</sup>+x<sup>7</sup>+x<sup>5</sup>+x<sup>4</sup>+x<sup>2</sup>+x<sup>1</sup>+x<sup>0</sup>)
32  <br><br>  <br><br>
33    
34  The calculated value stores the variable "intvar" as mathematical value.<br>  The calculated value stores the variable "intvar" as mathematical value.<br>
35  </p>  </p>
36    
37  The CRC algorithm implementation by C language is as follows:  The CRC algorithm implementation by C language is as follows:
38  This algorithm is often used as the Ethernet FCS(Frame Check Sequence).  This algorithm is often used as the Ethernet FCS(Frame Check Sequence).
39  <pre>  <pre>
40  <code>  <code>
41  #define CRCPOLY2 0xEDB88320UL  /* left-right reversal */  #define CRCPOLY2 0xEDB88320UL  /* left-right reversal */
42    
43  static unsigned long crc2(int n, unsigned char c[])  static unsigned long crc2(int n, unsigned char c[])
44  {  {
45          int i, j;          int i, j;
46          unsigned long r;          unsigned long r;
47    
48          r = 0xFFFFFFFFUL;          r = 0xFFFFFFFFUL;
49          for (i = 0; i < n; i++) {          for (i = 0; i < n; i++) {
50                  r ^= c[i];                  r ^= c[i];
51                  for (j = 0; j < CHAR_BIT; j++)                  for (j = 0; j < CHAR_BIT; j++)
52                          if (r & 1) r = (r >> 1) ^ CRCPOLY2;                          if (r & 1) r = (r >> 1) ^ CRCPOLY2;
53                          else       r >>= 1;                          else       r >>= 1;
54          }          }
55          return r ^ 0xFFFFFFFFUL;          return r ^ 0xFFFFFFFFUL;
56  }  }
57  </code></pre>  </code></pre>
58    
59    
60  <h2>Example</h2>  <h2>Example</h2>
61    
62  <pre class="macro-example">  <pre class="macro-example">
63  str = 'this is a test string to be CRC32ed'  str = 'this is a test string to be CRC32ed'
64  crc32 crc str  crc32 crc str
65    
66  ; Display CRC32 result asHEX  ; Display CRC32 result asHEX
67  sprintf '0x%08X' crc  sprintf '0x%08X' crc
68  messagebox inputstr 'CRC32 = '  messagebox inputstr 'CRC32 = '
69    
70  crc32file crc 'foo.bin'  crc32file crc 'foo.bin'
71  if result = -1 then  if result = -1 then
72      messagebox 'file open error' 'CRC32 = '      messagebox 'file open error' 'CRC32 = '
73  else  else
74      sprintf '0x%08X' crc      sprintf '0x%08X' crc
75      messagebox inputstr 'CRC32 = '      messagebox inputstr 'CRC32 = '
76  endif  endif
77  </pre>  </pre>
78    
79  </body>  </body>
80  </html>  </html>

Legend:
Removed from v.3226  
changed lines
  Added in v.3227

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26