coding.cc 2.5 KB

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