小组成员:10215300402-朱维清 & 10222140408 谷杰
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

106 righe
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. //
  5. // Endian-neutral encoding:
  6. // * Fixed-length numbers are encoded with least-significant byte first
  7. // * In addition we support variable length "varint" encoding
  8. // * Strings are encoded prefixed by their length in varint format
  9. #ifndef STORAGE_LEVELDB_UTIL_CODING_H_
  10. #define STORAGE_LEVELDB_UTIL_CODING_H_
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <string>
  14. #include "leveldb/slice.h"
  15. #include "port/port.h"
  16. namespace leveldb {
  17. // Standard Put... routines append to a string
  18. void PutFixed32(std::string* dst, uint32_t value);
  19. void PutFixed64(std::string* dst, uint64_t value);
  20. void PutVarint32(std::string* dst, uint32_t value);
  21. void PutVarint64(std::string* dst, uint64_t value);
  22. void PutLengthPrefixedSlice(std::string* dst, const Slice& value);
  23. // Standard Get... routines parse a value from the beginning of a Slice
  24. // and advance the slice past the parsed value.
  25. bool GetVarint32(Slice* input, uint32_t* value);
  26. bool GetVarint64(Slice* input, uint64_t* value);
  27. bool GetLengthPrefixedSlice(Slice* input, Slice* result);
  28. // Pointer-based variants of GetVarint... These either store a value
  29. // in *v and return a pointer just past the parsed value, or return
  30. // NULL on error. These routines only look at bytes in the range
  31. // [p..limit-1]
  32. const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* v);
  33. const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* v);
  34. // Returns the length of the varint32 or varint64 encoding of "v"
  35. int VarintLength(uint64_t v);
  36. // Lower-level versions of Put... that write directly into a character buffer
  37. // REQUIRES: dst has enough space for the value being written
  38. void EncodeFixed32(char* dst, uint32_t value);
  39. void EncodeFixed64(char* dst, uint64_t value);
  40. // Lower-level versions of Put... that write directly into a character buffer
  41. // and return a pointer just past the last byte written.
  42. // REQUIRES: dst has enough space for the value being written
  43. char* EncodeVarint32(char* dst, uint32_t value);
  44. char* EncodeVarint64(char* dst, uint64_t value);
  45. // Lower-level versions of Get... that read directly from a character buffer
  46. // without any bounds checking.
  47. inline uint32_t DecodeFixed32(const char* ptr) {
  48. if (port::kLittleEndian) {
  49. // Load the raw bytes
  50. uint32_t result;
  51. memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
  52. return result;
  53. } else {
  54. return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
  55. | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
  56. | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
  57. | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
  58. }
  59. }
  60. inline uint64_t DecodeFixed64(const char* ptr) {
  61. if (port::kLittleEndian) {
  62. // Load the raw bytes
  63. uint64_t result;
  64. memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
  65. return result;
  66. } else {
  67. uint64_t lo = DecodeFixed32(ptr);
  68. uint64_t hi = DecodeFixed32(ptr + 4);
  69. return (hi << 32) | lo;
  70. }
  71. }
  72. // Internal routine for use by fallback path of GetVarint32Ptr
  73. const char* GetVarint32PtrFallback(const char* p,
  74. const char* limit,
  75. uint32_t* value);
  76. inline const char* GetVarint32Ptr(const char* p,
  77. const char* limit,
  78. uint32_t* value) {
  79. if (p < limit) {
  80. uint32_t result = *(reinterpret_cast<const unsigned char*>(p));
  81. if ((result & 128) == 0) {
  82. *value = result;
  83. return p + 1;
  84. }
  85. }
  86. return GetVarint32PtrFallback(p, limit, value);
  87. }
  88. } // namespace leveldb
  89. #endif // STORAGE_LEVELDB_UTIL_CODING_H_