jcrc.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // If anyone wants to try and understand crcs especially combining them, try reading
  14. // http://www.repairfaq.org/filipg/LINK/F_LINK_IN.html if you want to stand any chance!
  15. #ifndef __JCRC__
  16. #define __JCRC__
  17. #include "platform.h"
  18. #include <stdio.h>
  19. #include "jiface.hpp"
  20. #include "jio.hpp"
  21. jlib_decl unsigned crc32(const char *buf, unsigned len, unsigned crc);
  22. jlib_decl unsigned cxc32(unsigned * buf, unsigned numWords, unsigned cxc32);
  23. jlib_decl unsigned short crc16(const void *buf,size32_t len,unsigned short crc);
  24. class jlib_decl CRC32
  25. {
  26. public:
  27. CRC32(unsigned crc = ~0U) { reset(crc); }
  28. inline void reset(unsigned _crc = ~0U) { crc = _crc; }
  29. inline unsigned get() const { return ~crc; }
  30. void skip(offset_t length);
  31. void tally(unsigned len, const void * buf);
  32. protected:
  33. unsigned crc;
  34. };
  35. class jlib_decl CRC32Merger
  36. {
  37. public:
  38. CRC32Merger();
  39. void addChildCRC(offset_t nextLength, unsigned nextCRC, bool fullCRC=false); // length = length of chunk CRC'd fullCRC=true if seed was ~0 and results was ~'d (e.g. from CRC32 class).
  40. void clear();
  41. unsigned get() { return ~crc; }
  42. protected:
  43. unsigned crc;
  44. };
  45. interface ICrcIOStream : extends IIOStream
  46. {
  47. virtual unsigned queryCrc() = 0;
  48. };
  49. jlib_decl ICrcIOStream *createCrcPipeStream(IIOStream *stream);
  50. jlib_decl unsigned getFileCRC(const char * name); // correctly uses ~0 for seed and ~ on result.
  51. //Legacy...
  52. jlib_decl unsigned crc_file(const char * name); // NB: Does not correctly use 0xffffffff
  53. // Fast inline 16 bit sumcheck
  54. inline unsigned short chksum16(const void *ptr,size32_t sz)
  55. {
  56. const unsigned short *p = (const unsigned short *)ptr;
  57. unsigned sum =0;
  58. while (sz>1) {
  59. sum += *p++;
  60. sz -= 2;
  61. }
  62. if (sz)
  63. sum += *(const byte *)p;
  64. sum = (sum >> 16) + (sum & 0xffff); // add in carrys
  65. sum += (sum >> 16); // and again
  66. return (unsigned short)(~sum);
  67. }
  68. #endif