crc.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "io/stream.h"
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. // simple crc function - generates lookup table on first call
  29. static U32 crcTable[256];
  30. static bool crcTableValid;
  31. static void calculateCRCTable()
  32. {
  33. U32 val;
  34. for(S32 i = 0; i < 256; i++)
  35. {
  36. val = i;
  37. for(S32 j = 0; j < 8; j++)
  38. {
  39. if(val & 0x01)
  40. val = 0xedb88320 ^ (val >> 1);
  41. else
  42. val = val >> 1;
  43. }
  44. crcTable[i] = val;
  45. }
  46. crcTableValid = true;
  47. }
  48. //-----------------------------------------------------------------------------
  49. U32 calculateCRC(const void * buffer, S32 len, U32 crcVal )
  50. {
  51. // check if need to generate the crc table
  52. if(!crcTableValid)
  53. calculateCRCTable();
  54. // now calculate the crc
  55. char * buf = (char*)buffer;
  56. for(S32 i = 0; i < len; i++)
  57. crcVal = crcTable[(crcVal ^ buf[i]) & 0xff] ^ (crcVal >> 8);
  58. return(crcVal);
  59. }
  60. U32 calculateCRCStream(Stream *stream, U32 crcVal )
  61. {
  62. // check if need to generate the crc table
  63. if(!crcTableValid)
  64. calculateCRCTable();
  65. // now calculate the crc
  66. stream->setPosition(0);
  67. S32 len = stream->getStreamSize();
  68. U8 buf[4096];
  69. S32 segCount = (len + 4095) / 4096;
  70. for(S32 j = 0; j < segCount; j++)
  71. {
  72. S32 slen = getMin(4096, len - (j * 4096));
  73. stream->read(slen, buf);
  74. crcVal = calculateCRC(buf, slen, crcVal);
  75. }
  76. stream->setPosition(0);
  77. return(crcVal);
  78. }