checksum32.c.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. checksum32.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='checkfilename.c.html' title=' checkfilename.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='chipset.c.html' title=' chipset.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * uint32_t checksum32 (void const * memory, size_t extent, uint32_t checksum);
  24. *
  25. * memory.h
  26. *
  27. * compute the 32 bit checksum of a memory segment; the calculated
  28. * checksum is the one's complement of the XOR of all 32-bit words;
  29. * this means that extent should be an even multiple of 4-bytes or
  30. * trailing bytes will be excluded from the calculation;
  31. *
  32. * set checksum argument to 0 when the memory region does not
  33. * include the previous checksum value;
  34. *
  35. * set checksum argument to the previous checksum value when the
  36. * memory region includes the previous checksum value; this will
  37. * effectively cancel-out the previous checksum value;
  38. *
  39. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  40. * Copyright (c) 2001-2006 by Charles Maier Associates;
  41. * Licensed under the Internet Software Consortium License;
  42. *
  43. *--------------------------------------------------------------------*/
  44. #ifndef CHECKSUM32_SOURCE
  45. #define CHECKSUM32_SOURCE
  46. #include &lt;stdint.h&gt;
  47. #include &lt;memory.h&gt;
  48. #include &quot;../tools/memory.h&quot;
  49. uint32_t checksum32 (register void const * memory, register size_t extent, register uint32_t checksum)
  50. {
  51. #ifdef __GNUC__
  52. while (extent &gt;= sizeof (checksum))
  53. {
  54. checksum ^= *(typeof (checksum) *)(memory);
  55. memory += sizeof (checksum);
  56. extent -= sizeof (checksum);
  57. }
  58. #else
  59. uint32_t * offset = (uint32_t *)(memory);
  60. while (extent &gt;= sizeof (* offset))
  61. {
  62. checksum ^= *offset++;
  63. extent -= sizeof (* offset);
  64. }
  65. #endif
  66. return (~checksum);
  67. }
  68. /*====================================================================*
  69. * demo/test program;
  70. *--------------------------------------------------------------------*/
  71. #if 0
  72. #include &lt;stdio.h&gt;
  73. int main (int argc, char const * argv [])
  74. {
  75. uint32_t data [100];
  76. read (0, data, sizeof (data));
  77. data [10] = 0;
  78. data [10] = checksum32 (data, sizeof (data), data [10]);
  79. printf (&quot;data [10] = 0x%08x\n&quot;, data [10]);
  80. data [10] = checksum32 (data, sizeof (data), data [10]);
  81. printf (&quot;data [10] = 0x%08x\n&quot;, data [10]);
  82. data [10] = checksum32 (data, sizeof (data), 0);
  83. printf (&quot;data [10] = 0x%08x\n&quot;, data [10]);
  84. data [10] = checksum32 (data, sizeof (data), 0);
  85. printf (&quot;data [10] = 0x%08x\n&quot;, data [10]);
  86. return (0);
  87. }
  88. #endif
  89. /*====================================================================*
  90. *
  91. *--------------------------------------------------------------------*/
  92. #endif
  93. </pre>
  94. <div class='footerlink'>
  95. [<a href='checkfilename.c.html' title=' checkfilename.c '>PREV</a>]
  96. [<a href='toolkit.html' title=' Index '>HOME</a>]
  97. [<a href='chipset.c.html' title=' chipset.c '>NEXT</a>]
  98. </div>
  99. </body>
  100. </html>