crc32_wrapper.cc 843 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // A wrapper around ZLib's CRC functions to put them in the rlz_lib namespace
  6. // and use our types.
  7. #include "rlz/lib/assert.h"
  8. #include "rlz/lib/crc32.h"
  9. #include "rlz/lib/string_utils.h"
  10. #include "third_party/zlib/zlib.h"
  11. namespace rlz_lib {
  12. int Crc32(const unsigned char* buf, int length) {
  13. return crc32(0L, buf, length);
  14. }
  15. bool Crc32(const char* text, int* crc) {
  16. if (!crc) {
  17. ASSERT_STRING("Crc32: crc is NULL.");
  18. return false;
  19. }
  20. *crc = 0;
  21. for (int i = 0; text[i]; i++) {
  22. if (!IsAscii(text[i]))
  23. return false;
  24. *crc = crc32(*crc, reinterpret_cast<const unsigned char*>(text + i), 1);
  25. }
  26. return true;
  27. }
  28. } // namespace rlz_lib