1
0

incremental_checksum.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #pragma once
  20. #include "defines.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
  41. csum_fold(__wsum csum)
  42. {
  43. uint32_t sum = (uint32_t)csum;
  44. sum = (sum & 0xffff) + (sum >> 16);
  45. sum = (sum & 0xffff) + (sum >> 16);
  46. return (__sum16)~sum;
  47. }
  48. static inline __wsum
  49. csum_add(__wsum csum, __wsum addend)
  50. {
  51. uint32_t res = (uint32_t)csum;
  52. res += (uint32_t)addend;
  53. return (__wsum)(res + (res < (uint32_t)addend));
  54. }
  55. static inline __wsum
  56. csum_sub(__wsum csum, __wsum addend)
  57. {
  58. return csum_add(csum, ~addend);
  59. }
  60. static inline __sum16
  61. csum16_add(__sum16 csum, __be16 addend)
  62. {
  63. uint16_t res = (uint16_t)csum;
  64. res += (uint16_t)addend;
  65. return (__sum16)(res + (res < (uint16_t)addend));
  66. }
  67. static inline __sum16
  68. csum16_sub(__sum16 csum, __be16 addend)
  69. {
  70. return csum16_add(csum, ~addend);
  71. }
  72. static inline __wsum
  73. csum_unfold(__sum16 n)
  74. {
  75. return (__wsum)n;
  76. }
  77. __wsum csum_partial(const void *buff, int len, __wsum wsum);
  78. static inline void
  79. csum_replace16(__sum16 *sum, const __be32 *from, const __be32 *to)
  80. {
  81. __be32 diff[] = {
  82. ~from[0],
  83. ~from[1],
  84. ~from[2],
  85. ~from[3],
  86. to[0],
  87. to[1],
  88. to[2],
  89. to[3],
  90. };
  91. *sum = csum_fold(csum_partial(diff, sizeof(diff), ~csum_unfold(*sum)));
  92. }
  93. static inline void
  94. csum_replace4(__sum16 *sum, __be32 from, __be32 to)
  95. {
  96. *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), from), to));
  97. }
  98. static inline void
  99. csum_replace2(__sum16 *sum, __be16 from, __be16 to)
  100. {
  101. *sum = ~csum16_add(csum16_sub(~(*sum), from), to);
  102. }