incremental_checksum.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifndef _INCREMENTAL_CHECKSUM_H_
  20. #define _INCREMENTAL_CHECKSUM_H_
  21. #include "config.h"
  22. /*
  23. * shamelessly stolen from Linux kernel
  24. *
  25. * Implements RFC 1624 (Incremental Internet Checksum)
  26. * 3. Discussion states :
  27. * HC' = ~(~HC + ~m + m')
  28. * m : old value of a 16bit field
  29. * m' : new value of a 16bit field
  30. */
  31. typedef uint16_t __le16;
  32. typedef uint16_t __be16;
  33. typedef uint32_t __le32;
  34. typedef uint32_t __be32;
  35. typedef uint16_t __sum16;
  36. typedef uint32_t __wsum;
  37. /*
  38. * Fold a partial checksum
  39. */
  40. static inline __sum16 csum_fold(__wsum csum)
  41. {
  42. uint32_t sum = (uint32_t)csum;
  43. sum = (sum & 0xffff) + (sum >> 16);
  44. sum = (sum & 0xffff) + (sum >> 16);
  45. return (__sum16)~sum;
  46. }
  47. static inline __wsum csum_add(__wsum csum, __wsum addend)
  48. {
  49. uint32_t res = (uint32_t)csum;
  50. res += (uint32_t)addend;
  51. return (__wsum)(res + (res < (uint32_t)addend));
  52. }
  53. static inline __wsum csum_sub(__wsum csum, __wsum addend)
  54. {
  55. return csum_add(csum, ~addend);
  56. }
  57. static inline __sum16 csum16_add(__sum16 csum, __be16 addend)
  58. {
  59. uint16_t res = (uint16_t)csum;
  60. res += (uint16_t)addend;
  61. return (__sum16)(res + (res < (uint16_t)addend));
  62. }
  63. static inline __sum16 csum16_sub(__sum16 csum, __be16 addend)
  64. {
  65. return csum16_add(csum, ~addend);
  66. }
  67. static inline __wsum csum_unfold(__sum16 n)
  68. {
  69. return (__wsum)n;
  70. }
  71. __wsum csum_partial(const void *buff, int len, __wsum wsum);
  72. static inline void csum_replace16(__sum16 *sum, const __be32 *from, const __be32 *to)
  73. {
  74. __be32 diff[] = {
  75. ~from[0], ~from[1], ~from[2], ~from[3],
  76. to[0], to[1], to[2], to[3],
  77. };
  78. *sum = csum_fold(csum_partial(diff, sizeof(diff), ~csum_unfold(*sum)));
  79. }
  80. static inline void csum_replace4(__sum16 *sum, __be32 from, __be32 to)
  81. {
  82. *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), from), to));
  83. }
  84. static inline void csum_replace2(__sum16 *sum, __be16 from, __be16 to)
  85. {
  86. *sum = ~csum16_add(csum16_sub(~(*sum), from), to);
  87. }
  88. #endif /* _INCREMENTAL_CHECKSUM_H_ */