checksum32.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*====================================================================*
  2. *
  3. * uint32_t checksum32 (void const * memory, size_t extent, uint32_t checksum);
  4. *
  5. * memory.h
  6. *
  7. * compute the 32 bit checksum of a memory segment; the calculated
  8. * checksum is the one's complement of the XOR of all 32-bit words;
  9. * this means that extent should be an even multiple of 4-bytes or
  10. * trailing bytes will be excluded from the calculation;
  11. *
  12. * set checksum argument to 0 when the memory region does not
  13. * include the previous checksum value;
  14. *
  15. * set checksum argument to the previous checksum value when the
  16. * memory region includes the previous checksum value; this will
  17. * effectively cancel-out the previous checksum value;
  18. *
  19. * Motley Tools by Charles Maier;
  20. * Copyright (c) 2001-2006 by Charles Maier Associates;
  21. * Licensed under the Internet Software Consortium License;
  22. *
  23. *--------------------------------------------------------------------*/
  24. #ifndef CHECKSUM32_SOURCE
  25. #define CHECKSUM32_SOURCE
  26. #include <string.h>
  27. #include <stdint.h>
  28. #include <memory.h>
  29. #include "../tools/memory.h"
  30. uint32_t checksum32 (void const * vmemory, size_t extent, uint32_t checksum)
  31. {
  32. uint32_t temp;
  33. const uint8_t *memory = vmemory;
  34. while (extent >= sizeof (checksum))
  35. {
  36. /* Since 'memory' might point to an unaligned address,
  37. * we use memcpy to copy the data to 'temp' which is
  38. * aligned. Otherwise, the checksum would be wrong in the
  39. * best case, or worst-case: program is killed with SIGBUS.
  40. */
  41. memcpy (&temp, memory, sizeof (temp));
  42. checksum ^= temp;
  43. memory += sizeof (checksum);
  44. extent -= sizeof (checksum);
  45. }
  46. return (~checksum);
  47. }
  48. /*====================================================================*
  49. * demo/test program;
  50. *--------------------------------------------------------------------*/
  51. #if 0
  52. #include <stdio.h>
  53. int main (int argc, char const * argv [])
  54. {
  55. uint32_t data [100];
  56. read (0, data, sizeof (data));
  57. data [10] = 0;
  58. data [10] = checksum32 (data, sizeof (data), data [10]);
  59. printf ("data [10] = 0x%08x\n", data [10]);
  60. data [10] = checksum32 (data, sizeof (data), data [10]);
  61. printf ("data [10] = 0x%08x\n", data [10]);
  62. data [10] = checksum32 (data, sizeof (data), 0);
  63. printf ("data [10] = 0x%08x\n", data [10]);
  64. data [10] = checksum32 (data, sizeof (data), 0);
  65. printf ("data [10] = 0x%08x\n", data [10]);
  66. return (0);
  67. }
  68. #endif
  69. /*====================================================================*
  70. *
  71. *--------------------------------------------------------------------*/
  72. #endif