]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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 | ||
10 | #include "util/coding.h" | |
11 | ||
12 | #include <algorithm> | |
1e59de90 | 13 | |
7c673cae FG |
14 | #include "rocksdb/slice.h" |
15 | #include "rocksdb/slice_transform.h" | |
16 | ||
f67539c2 | 17 | namespace ROCKSDB_NAMESPACE { |
7c673cae | 18 | |
11fdf7f2 TL |
19 | // conversion' conversion from 'type1' to 'type2', possible loss of data |
20 | #if defined(_MSC_VER) | |
21 | #pragma warning(push) | |
22 | #pragma warning(disable : 4244) | |
23 | #endif | |
7c673cae FG |
24 | char* EncodeVarint32(char* dst, uint32_t v) { |
25 | // Operate on characters as unsigneds | |
26 | unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); | |
27 | static const int B = 128; | |
28 | if (v < (1 << 7)) { | |
29 | *(ptr++) = v; | |
30 | } else if (v < (1 << 14)) { | |
31 | *(ptr++) = v | B; | |
32 | *(ptr++) = v >> 7; | |
33 | } else if (v < (1 << 21)) { | |
34 | *(ptr++) = v | B; | |
35 | *(ptr++) = (v >> 7) | B; | |
36 | *(ptr++) = v >> 14; | |
37 | } else if (v < (1 << 28)) { | |
38 | *(ptr++) = v | B; | |
39 | *(ptr++) = (v >> 7) | B; | |
40 | *(ptr++) = (v >> 14) | B; | |
41 | *(ptr++) = v >> 21; | |
42 | } else { | |
43 | *(ptr++) = v | B; | |
44 | *(ptr++) = (v >> 7) | B; | |
45 | *(ptr++) = (v >> 14) | B; | |
46 | *(ptr++) = (v >> 21) | B; | |
47 | *(ptr++) = v >> 28; | |
48 | } | |
49 | return reinterpret_cast<char*>(ptr); | |
50 | } | |
11fdf7f2 TL |
51 | #if defined(_MSC_VER) |
52 | #pragma warning(pop) | |
53 | #endif | |
7c673cae FG |
54 | |
55 | const char* GetVarint32PtrFallback(const char* p, const char* limit, | |
56 | uint32_t* value) { | |
57 | uint32_t result = 0; | |
58 | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { | |
59 | uint32_t byte = *(reinterpret_cast<const unsigned char*>(p)); | |
60 | p++; | |
61 | if (byte & 128) { | |
62 | // More bytes are present | |
63 | result |= ((byte & 127) << shift); | |
64 | } else { | |
65 | result |= (byte << shift); | |
66 | *value = result; | |
67 | return reinterpret_cast<const char*>(p); | |
68 | } | |
69 | } | |
70 | return nullptr; | |
71 | } | |
72 | ||
73 | const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) { | |
74 | uint64_t result = 0; | |
75 | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { | |
76 | uint64_t byte = *(reinterpret_cast<const unsigned char*>(p)); | |
77 | p++; | |
78 | if (byte & 128) { | |
79 | // More bytes are present | |
80 | result |= ((byte & 127) << shift); | |
81 | } else { | |
82 | result |= (byte << shift); | |
83 | *value = result; | |
84 | return reinterpret_cast<const char*>(p); | |
85 | } | |
86 | } | |
87 | return nullptr; | |
88 | } | |
89 | ||
f67539c2 | 90 | } // namespace ROCKSDB_NAMESPACE |