csum_partial_copy.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Network Checksum & Copy routine
  4. *
  5. * Copyright (C) 1999, 2003-2004 Hewlett-Packard Co
  6. * Stephane Eranian <[email protected]>
  7. *
  8. * Most of the code has been imported from Linux/Alpha
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <net/checksum.h>
  14. /*
  15. * XXX Fixme: those 2 inlines are meant for debugging and will go away
  16. */
  17. static inline unsigned
  18. short from64to16(unsigned long x)
  19. {
  20. /* add up 32-bit words for 33 bits */
  21. x = (x & 0xffffffff) + (x >> 32);
  22. /* add up 16-bit and 17-bit words for 17+c bits */
  23. x = (x & 0xffff) + (x >> 16);
  24. /* add up 16-bit and 2-bit for 16+c bit */
  25. x = (x & 0xffff) + (x >> 16);
  26. /* add up carry.. */
  27. x = (x & 0xffff) + (x >> 16);
  28. return x;
  29. }
  30. static inline
  31. unsigned long do_csum_c(const unsigned char * buff, int len, unsigned int psum)
  32. {
  33. int odd, count;
  34. unsigned long result = (unsigned long)psum;
  35. if (len <= 0)
  36. goto out;
  37. odd = 1 & (unsigned long) buff;
  38. if (odd) {
  39. result = *buff << 8;
  40. len--;
  41. buff++;
  42. }
  43. count = len >> 1; /* nr of 16-bit words.. */
  44. if (count) {
  45. if (2 & (unsigned long) buff) {
  46. result += *(unsigned short *) buff;
  47. count--;
  48. len -= 2;
  49. buff += 2;
  50. }
  51. count >>= 1; /* nr of 32-bit words.. */
  52. if (count) {
  53. if (4 & (unsigned long) buff) {
  54. result += *(unsigned int *) buff;
  55. count--;
  56. len -= 4;
  57. buff += 4;
  58. }
  59. count >>= 1; /* nr of 64-bit words.. */
  60. if (count) {
  61. unsigned long carry = 0;
  62. do {
  63. unsigned long w = *(unsigned long *) buff;
  64. count--;
  65. buff += 8;
  66. result += carry;
  67. result += w;
  68. carry = (w > result);
  69. } while (count);
  70. result += carry;
  71. result = (result & 0xffffffff) + (result >> 32);
  72. }
  73. if (len & 4) {
  74. result += *(unsigned int *) buff;
  75. buff += 4;
  76. }
  77. }
  78. if (len & 2) {
  79. result += *(unsigned short *) buff;
  80. buff += 2;
  81. }
  82. }
  83. if (len & 1)
  84. result += *buff;
  85. result = from64to16(result);
  86. if (odd)
  87. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  88. out:
  89. return result;
  90. }