ptclbsum.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "ip.h"
  16. static short endian = 1;
  17. static uint8_t* aendian = (uint8_t*)&endian;
  18. #define LITTLE *aendian
  19. uint16_t
  20. ptclbsum(uint8_t *addr, int len)
  21. {
  22. uint32_t losum, hisum, mdsum, x;
  23. uint32_t t1, t2;
  24. losum = 0;
  25. hisum = 0;
  26. mdsum = 0;
  27. x = 0;
  28. if(PTR2UINT(addr) & 1) {
  29. if(len) {
  30. hisum += addr[0];
  31. len--;
  32. addr++;
  33. }
  34. x = 1;
  35. }
  36. while(len >= 16) {
  37. t1 = *(uint16_t*)(addr+0);
  38. t2 = *(uint16_t*)(addr+2); mdsum += t1;
  39. t1 = *(uint16_t*)(addr+4); mdsum += t2;
  40. t2 = *(uint16_t*)(addr+6); mdsum += t1;
  41. t1 = *(uint16_t*)(addr+8); mdsum += t2;
  42. t2 = *(uint16_t*)(addr+10); mdsum += t1;
  43. t1 = *(uint16_t*)(addr+12); mdsum += t2;
  44. t2 = *(uint16_t*)(addr+14); mdsum += t1;
  45. mdsum += t2;
  46. len -= 16;
  47. addr += 16;
  48. }
  49. while(len >= 2) {
  50. mdsum += *(uint16_t*)addr;
  51. len -= 2;
  52. addr += 2;
  53. }
  54. if(x) {
  55. if(len)
  56. losum += addr[0];
  57. if(LITTLE)
  58. losum += mdsum;
  59. else
  60. hisum += mdsum;
  61. } else {
  62. if(len)
  63. hisum += addr[0];
  64. if(LITTLE)
  65. hisum += mdsum;
  66. else
  67. losum += mdsum;
  68. }
  69. losum += hisum >> 8;
  70. losum += (hisum & 0xff) << 8;
  71. while(hisum = losum>>16)
  72. losum = hisum + (losum & 0xffff);
  73. return losum & 0xffff;
  74. }