25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash.cpp 1.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. namespace lol
  15. {
  16. /*
  17. * Hash implementation class
  18. */
  19. static class HashData
  20. {
  21. friend class Hash;
  22. public:
  23. HashData()
  24. {
  25. /* Initialise CRC32 table */
  26. for (int i = 0; i < 256; i++)
  27. {
  28. uint32_t tmp = i;
  29. for (int j = 8; j--; )
  30. tmp = (tmp >> 1) ^ ((tmp & 1) ? 0xedb88320 : 0);
  31. crc32_table[i] = tmp;
  32. }
  33. }
  34. private:
  35. uint32_t crc32_table[256];
  36. }
  37. hashdata;
  38. static HashData * const data = &hashdata;
  39. /*
  40. * Public Hash class
  41. */
  42. uint32_t Hash::Crc32(char const *str)
  43. {
  44. uint32_t ret = 0xffffffff, ch;
  45. while ((ch = (uint8_t)*str++))
  46. ret = data->crc32_table[(uint8_t)(ret ^ ch)] ^ (ret >> 8);
  47. return ret ^ 0xffffffff;
  48. }
  49. } /* namespace lol */