1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9 #include "table/block_based/reader_common.h"
10 
11 #include "util/crc32c.h"
12 #include "util/xxhash.h"
13 
14 namespace ROCKSDB_NAMESPACE {
ForceReleaseCachedEntry(void * arg,void * h)15 void ForceReleaseCachedEntry(void* arg, void* h) {
16   Cache* cache = reinterpret_cast<Cache*>(arg);
17   Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
18   cache->Release(handle, true /* force_erase */);
19 }
20 
VerifyChecksum(const ChecksumType type,const char * buf,size_t len,uint32_t expected)21 Status VerifyChecksum(const ChecksumType type, const char* buf, size_t len,
22                       uint32_t expected) {
23   Status s;
24   uint32_t actual = 0;
25   switch (type) {
26     case kNoChecksum:
27       break;
28     case kCRC32c:
29       expected = crc32c::Unmask(expected);
30       actual = crc32c::Value(buf, len);
31       break;
32     case kxxHash:
33       actual = XXH32(buf, static_cast<int>(len), 0);
34       break;
35     case kxxHash64:
36       actual = static_cast<uint32_t>(XXH64(buf, static_cast<int>(len), 0) &
37                                      uint64_t{0xffffffff});
38       break;
39     default:
40       s = Status::Corruption("unknown checksum type");
41   }
42   if (s.ok() && actual != expected) {
43     s = Status::Corruption("properties block checksum mismatched");
44   }
45   return s;
46 }
47 }  // namespace ROCKSDB_NAMESPACE
48