1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be 3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors. 4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_UTIL_CRC32C_H_ 6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_UTIL_CRC32C_H_ 7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stddef.h> 9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stdint.h> 10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb { 12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace crc32c { 13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Return the crc32c of concat(A, data[0,n-1]) where init_crc is the 15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// crc32c of some string A. Extend() is often used to maintain the 16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// crc32c of a stream of data. 17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgextern uint32_t Extend(uint32_t init_crc, const char* data, size_t n); 18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Return the crc32c of data[0,n-1] 20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orginline uint32_t Value(const char* data, size_t n) { 21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return Extend(0, data, n); 22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstatic const uint32_t kMaskDelta = 0xa282ead8ul; 25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Return a masked representation of crc. 27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// 28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Motivation: it is problematic to compute the CRC of a string that 29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// contains embedded CRCs. Therefore we recommend that CRCs stored 30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// somewhere (e.g., in files) should be masked before being stored. 31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orginline uint32_t Mask(uint32_t crc) { 32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org // Rotate right by 15 bits and add a constant. 33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return ((crc >> 15) | (crc << 17)) + kMaskDelta; 34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Return the crc whose masked representation is masked_crc. 37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orginline uint32_t Unmask(uint32_t masked_crc) { 38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org uint32_t rot = masked_crc - kMaskDelta; 39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return ((rot >> 17) | (rot << 15)); 40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 4245b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace crc32c 4345b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace leveldb 44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif // STORAGE_LEVELDB_UTIL_CRC32C_H_ 46