incremental_checksum.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2015 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This code is heavily based on (some might even say stolen from) Mike Shiffman's
  21. * checksumming code from Libnet 1.1.3
  22. */
  23. #include "config.h"
  24. #include "tcpedit.h"
  25. #include "incremental_checksum.h"
  26. static inline unsigned short from32to16(unsigned int x)
  27. {
  28. /* add up 16-bit and 16-bit for 16+c bit */
  29. x = (x & 0xffff) + (x >> 16);
  30. /* add up carry.. */
  31. x = (x & 0xffff) + (x >> 16);
  32. return x;
  33. }
  34. static unsigned int do_csum(const unsigned char *buff, int len)
  35. {
  36. int odd;
  37. unsigned int result = 0;
  38. if (len <= 0)
  39. goto out;
  40. odd = 1 & (unsigned long) buff;
  41. if (odd) {
  42. #ifdef WORDS_BIGENDIAN
  43. result = *buff;
  44. #else
  45. result += (*buff << 8);
  46. #endif
  47. len--;
  48. buff++;
  49. }
  50. if (len >= 2) {
  51. if (2 & (unsigned long) buff) {
  52. result += *(unsigned short *) buff;
  53. len -= 2;
  54. buff += 2;
  55. }
  56. if (len >= 4) {
  57. const unsigned char *end = buff + ((unsigned)len & ~3);
  58. unsigned int carry = 0;
  59. do {
  60. unsigned int w = *(unsigned int *) buff;
  61. buff += 4;
  62. result += carry;
  63. result += w;
  64. carry = (w > result);
  65. } while (buff < end);
  66. result += carry;
  67. result = (result & 0xffff) + (result >> 16);
  68. }
  69. if (len & 2) {
  70. result += *(unsigned short *) buff;
  71. buff += 2;
  72. }
  73. }
  74. if (len & 1)
  75. #ifdef WORDS_BIGENDIAN
  76. result += (*buff << 8);
  77. #else
  78. result += *buff;
  79. #endif
  80. result = from32to16(result);
  81. if (odd)
  82. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  83. out:
  84. return result;
  85. }
  86. /*
  87. * computes the checksum of a memory block at buff, length len,
  88. * and adds in "sum" (32-bit)
  89. *
  90. * returns a 32-bit number suitable for feeding into itself
  91. * or csum_tcpudp_magic
  92. *
  93. * this function must be called with even lengths, except
  94. * for the last fragment, which may be odd
  95. *
  96. * it's best to have buff aligned on a 32-bit boundary
  97. */
  98. __wsum csum_partial(const void *buff, int len, __wsum wsum)
  99. {
  100. unsigned int sum = ( unsigned int)wsum;
  101. unsigned int result = do_csum(buff, len);
  102. /* add in old sum, and carry.. */
  103. result += sum;
  104. if (sum > result)
  105. result += 1;
  106. return (__wsum)result;
  107. }