rippled
Loading...
Searching...
No Matches
varint.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_NUDB_VARINT_H_INCLUDED
21#define BEAST_NUDB_VARINT_H_INCLUDED
22
23#include <nudb/detail/stream.hpp>
24
25#include <cstdint>
26#include <type_traits>
27
28namespace ripple {
29namespace NodeStore {
30
31// This is a variant of the base128 varint format from
32// google protocol buffers:
33// https://developers.google.com/protocol-buffers/docs/encoding#varints
34
35// field tag
36struct varint;
37
38// Metafuncton to return largest
39// possible size of T represented as varint.
40// T must be unsigned
41template <class T, bool = std::is_unsigned<T>::value>
43
44template <class T>
45struct varint_traits<T, true>
46{
47 explicit varint_traits() = default;
48
49 static std::size_t constexpr max = (8 * sizeof(T) + 6) / 7;
50};
51
52// Returns: Number of bytes consumed or 0 on error,
53// if the buffer was too small or t overflowed.
54//
55template <class = void>
57read_varint(void const* buf, std::size_t buflen, std::size_t& t)
58{
59 if (buflen == 0)
60 return 0;
61 t = 0;
62 std::uint8_t const* p = reinterpret_cast<std::uint8_t const*>(buf);
63 std::size_t n = 0;
64 while (p[n] & 0x80)
65 if (++n >= buflen)
66 return 0;
67 if (++n > buflen)
68 return 0;
69 // Special case for 0
70 if (n == 1 && *p == 0)
71 {
72 t = 0;
73 return 1;
74 }
75 auto const used = n;
76 while (n--)
77 {
78 auto const d = p[n];
79 auto const t0 = t;
80 t *= 127;
81 t += d & 0x7f;
82 if (t <= t0)
83 return 0; // overflow
84 }
85 return used;
86}
87
88template <class T, std::enable_if_t<std::is_unsigned<T>::value>* = nullptr>
91{
92 std::size_t n = 0;
93 do
94 {
95 v /= 127;
96 ++n;
97 } while (v != 0);
98 return n;
99}
100
101template <class = void>
104{
105 std::uint8_t* p = reinterpret_cast<std::uint8_t*>(p0);
106 do
107 {
108 std::uint8_t d = v % 127;
109 v /= 127;
110 if (v != 0)
111 d |= 0x80;
112 *p++ = d;
113 } while (v != 0);
114 return p - reinterpret_cast<std::uint8_t*>(p0);
115}
116
117// input stream
118
119template <class T, std::enable_if_t<std::is_same<T, varint>::value>* = nullptr>
120void
121read(nudb::detail::istream& is, std::size_t& u)
122{
123 auto p0 = is(1);
124 auto p1 = p0;
125 while (*p1++ & 0x80)
126 is(1);
127 read_varint(p0, p1 - p0, u);
128}
129
130// output stream
131
132template <class T, std::enable_if_t<std::is_same<T, varint>::value>* = nullptr>
133void
134write(nudb::detail::ostream& os, std::size_t t)
135{
136 write_varint(os.data(size_varint(t)), t);
137}
138
139} // namespace NodeStore
140} // namespace ripple
141
142#endif
std::size_t read_varint(void const *buf, std::size_t buflen, std::size_t &t)
Definition: varint.h:57
std::size_t size_varint(T v)
Definition: varint.h:90
void write(nudb::detail::ostream &os, std::size_t t)
Definition: varint.h:134
void read(nudb::detail::istream &is, std::size_t &u)
Definition: varint.h:121
std::size_t write_varint(void *p0, std::size_t v)
Definition: varint.h:103
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26