Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

156 Zeilen
3.8 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "util/coding.h"
  5. namespace leveldb {
  6. void PutFixed32(std::string* dst, uint32_t value) {
  7. char buf[sizeof(value)];
  8. EncodeFixed32(buf, value);
  9. dst->append(buf, sizeof(buf));
  10. }
  11. void PutFixed64(std::string* dst, uint64_t value) {
  12. char buf[sizeof(value)];
  13. EncodeFixed64(buf, value);
  14. dst->append(buf, sizeof(buf));
  15. }
  16. char* EncodeVarint32(char* dst, uint32_t v) {
  17. // Operate on characters as unsigneds
  18. uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
  19. static const int B = 128;
  20. if (v < (1 << 7)) {
  21. *(ptr++) = v;
  22. } else if (v < (1 << 14)) {
  23. *(ptr++) = v | B;
  24. *(ptr++) = v >> 7;
  25. } else if (v < (1 << 21)) {
  26. *(ptr++) = v | B;
  27. *(ptr++) = (v >> 7) | B;
  28. *(ptr++) = v >> 14;
  29. } else if (v < (1 << 28)) {
  30. *(ptr++) = v | B;
  31. *(ptr++) = (v >> 7) | B;
  32. *(ptr++) = (v >> 14) | B;
  33. *(ptr++) = v >> 21;
  34. } else {
  35. *(ptr++) = v | B;
  36. *(ptr++) = (v >> 7) | B;
  37. *(ptr++) = (v >> 14) | B;
  38. *(ptr++) = (v >> 21) | B;
  39. *(ptr++) = v >> 28;
  40. }
  41. return reinterpret_cast<char*>(ptr);
  42. }
  43. void PutVarint32(std::string* dst, uint32_t v) {
  44. char buf[5];
  45. char* ptr = EncodeVarint32(buf, v);
  46. dst->append(buf, ptr - buf);
  47. }
  48. char* EncodeVarint64(char* dst, uint64_t v) {
  49. static const int B = 128;
  50. uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
  51. while (v >= B) {
  52. *(ptr++) = v | B;
  53. v >>= 7;
  54. }
  55. *(ptr++) = static_cast<uint8_t>(v);
  56. return reinterpret_cast<char*>(ptr);
  57. }
  58. void PutVarint64(std::string* dst, uint64_t v) {
  59. char buf[10];
  60. char* ptr = EncodeVarint64(buf, v);
  61. dst->append(buf, ptr - buf);
  62. }
  63. void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
  64. PutVarint32(dst, value.size());
  65. dst->append(value.data(), value.size());
  66. }
  67. int VarintLength(uint64_t v) {
  68. int len = 1;
  69. while (v >= 128) {
  70. v >>= 7;
  71. len++;
  72. }
  73. return len;
  74. }
  75. const char* GetVarint32PtrFallback(const char* p, const char* limit,
  76. uint32_t* value) {
  77. uint32_t result = 0;
  78. for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
  79. uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
  80. p++;
  81. if (byte & 128) {
  82. // More bytes are present
  83. result |= ((byte & 127) << shift);
  84. } else {
  85. result |= (byte << shift);
  86. *value = result;
  87. return reinterpret_cast<const char*>(p);
  88. }
  89. }
  90. return nullptr;
  91. }
  92. bool GetVarint32(Slice* input, uint32_t* value) {
  93. const char* p = input->data();
  94. const char* limit = p + input->size();
  95. const char* q = GetVarint32Ptr(p, limit, value);
  96. if (q == nullptr) {
  97. return false;
  98. } else {
  99. *input = Slice(q, limit - q);
  100. return true;
  101. }
  102. }
  103. const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
  104. uint64_t result = 0;
  105. for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
  106. uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
  107. p++;
  108. if (byte & 128) {
  109. // More bytes are present
  110. result |= ((byte & 127) << shift);
  111. } else {
  112. result |= (byte << shift);
  113. *value = result;
  114. return reinterpret_cast<const char*>(p);
  115. }
  116. }
  117. return nullptr;
  118. }
  119. bool GetVarint64(Slice* input, uint64_t* value) {
  120. const char* p = input->data();
  121. const char* limit = p + input->size();
  122. const char* q = GetVarint64Ptr(p, limit, value);
  123. if (q == nullptr) {
  124. return false;
  125. } else {
  126. *input = Slice(q, limit - q);
  127. return true;
  128. }
  129. }
  130. bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
  131. uint32_t len;
  132. if (GetVarint32(input, &len) && input->size() >= len) {
  133. *result = Slice(input->data(), len);
  134. input->remove_prefix(len);
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. } // namespace leveldb