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