CheckSum.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /**
  11. * @file
  12. * @brief CheckSum implementation from http://www.flounder.com/checksum.htm
  13. *
  14. */
  15. #include "CheckSum.h"
  16. /****************************************************************************
  17. * CheckSum::add
  18. * Inputs:
  19. * unsigned int d: word to add
  20. * Result: void
  21. *
  22. * Effect:
  23. * Adds the bytes of the unsigned int to the CheckSum
  24. ****************************************************************************/
  25. void CheckSum::Add ( unsigned int value )
  26. {
  27. union
  28. {
  29. unsigned int value;
  30. unsigned char bytes[ 4 ];
  31. }
  32. data;
  33. data.value = value;
  34. for ( unsigned int i = 0; i < sizeof( data.bytes ); i++ )
  35. Add ( data.bytes[ i ] )
  36. ;
  37. } // CheckSum::add(unsigned int)
  38. /****************************************************************************
  39. * CheckSum::add
  40. * Inputs:
  41. * unsigned short value:
  42. * Result: void
  43. *
  44. * Effect:
  45. * Adds the bytes of the unsigned short value to the CheckSum
  46. ****************************************************************************/
  47. void CheckSum::Add ( unsigned short value )
  48. {
  49. union
  50. {
  51. unsigned short value;
  52. unsigned char bytes[ 2 ];
  53. }
  54. data;
  55. data.value = value;
  56. for ( unsigned int i = 0; i < sizeof( data.bytes ); i++ )
  57. Add ( data.bytes[ i ] )
  58. ;
  59. } // CheckSum::add(unsigned short)
  60. /****************************************************************************
  61. * CheckSum::add
  62. * Inputs:
  63. * unsigned char value:
  64. * Result: void
  65. *
  66. * Effect:
  67. * Adds the byte to the CheckSum
  68. ****************************************************************************/
  69. void CheckSum::Add ( unsigned char value )
  70. {
  71. unsigned char cipher = (unsigned char)( value ^ ( r >> 8 ) );
  72. r = ( cipher + r ) * c1 + c2;
  73. sum += cipher;
  74. } // CheckSum::add(unsigned char)
  75. /****************************************************************************
  76. * CheckSum::add
  77. * Inputs:
  78. * LPunsigned char b: pointer to byte array
  79. * unsigned int length: count
  80. * Result: void
  81. *
  82. * Effect:
  83. * Adds the bytes to the CheckSum
  84. ****************************************************************************/
  85. void CheckSum::Add ( unsigned char *b, unsigned int length )
  86. {
  87. for ( unsigned int i = 0; i < length; i++ )
  88. Add ( b[ i ] )
  89. ;
  90. } // CheckSum::add(LPunsigned char, unsigned int)
粤ICP备19079148号