crc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "core/crc.h"
  23. #include "core/stream/stream.h"
  24. //-----------------------------------------------------------------------------
  25. // simple crc function - generates lookup table on first call
  26. static U32 crcTable[256];
  27. static bool crcTableValid;
  28. static void calculateCRCTable()
  29. {
  30. U32 val;
  31. for(S32 i = 0; i < 256; i++)
  32. {
  33. val = i;
  34. for(S32 j = 0; j < 8; j++)
  35. {
  36. if(val & 0x01)
  37. val = 0xedb88320 ^ (val >> 1);
  38. else
  39. val = val >> 1;
  40. }
  41. crcTable[i] = val;
  42. }
  43. crcTableValid = true;
  44. }
  45. //-----------------------------------------------------------------------------
  46. U32 CRC::calculateCRC(const void * buffer, S32 len, U32 crcVal )
  47. {
  48. // check if need to generate the crc table
  49. if(!crcTableValid)
  50. calculateCRCTable();
  51. // now calculate the crc
  52. char * buf = (char*)buffer;
  53. for(S32 i = 0; i < len; i++)
  54. crcVal = crcTable[(crcVal ^ buf[i]) & 0xff] ^ (crcVal >> 8);
  55. return(crcVal);
  56. }
  57. U32 CRC::calculateCRCStream(Stream *stream, U32 crcVal )
  58. {
  59. // check if need to generate the crc table
  60. if(!crcTableValid)
  61. calculateCRCTable();
  62. // now calculate the crc
  63. stream->setPosition(0);
  64. S32 len = stream->getStreamSize();
  65. U8 buf[4096];
  66. S32 segCount = (len + 4095) / 4096;
  67. for(S32 j = 0; j < segCount; j++)
  68. {
  69. S32 slen = getMin(4096, len - (j * 4096));
  70. stream->read(slen, buf);
  71. crcVal = CRC::calculateCRC(buf, slen, crcVal);
  72. }
  73. stream->setPosition(0);
  74. return(crcVal);
  75. }