WPILibC++ 2024.3.2
leb128.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPIUTIL_WPI_LEB128_H_
6#define WPIUTIL_WPI_LEB128_H_
7
8#include <stdint.h>
9
10#include <optional>
11#include <span>
12
13namespace wpi {
14
15template <typename T>
16class SmallVectorImpl;
17class raw_istream;
18class raw_ostream;
19
20/**
21 * Get size of unsigned LEB128 data.
22 *
23 * Determine the number of bytes required to encode an unsigned LEB128 datum.
24 * The algorithm is taken from Appendix C of the DWARF 3 spec. For information
25 * on the encodings refer to section "7.6 - Variable Length Data". Return
26 * the number of bytes required.
27 *
28 * @param val LEB128 data.
29 */
30uint64_t SizeUleb128(uint64_t val);
31
32/**
33 * Write unsigned LEB128 data.
34 *
35 * Encode an unsigned LEB128 encoded datum. The algorithm is taken
36 * from Appendix C of the DWARF 3 spec. For information on the
37 * encodings refer to section "7.6 - Variable Length Data". Return
38 * the number of bytes written.
39 *
40 * @param dest The address where the ULEB128 data is to be stored.
41 * @param val Value to be stored.
42 */
43uint64_t WriteUleb128(SmallVectorImpl<char>& dest, uint64_t val);
44
45/**
46 * Write unsigned LEB128 data.
47 *
48 * Encode an unsigned LEB128 encoded datum. The algorithm is taken
49 * from Appendix C of the DWARF 3 spec. For information on the
50 * encodings refer to section "7.6 - Variable Length Data".
51 *
52 * @param os Output stream.
53 * @param val Value to be stored.
54 */
55void WriteUleb128(raw_ostream& os, uint64_t val);
56
57/**
58 * Read unsigned LEB128 data.
59 *
60 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
61 * from Appendix C of the DWARF 3 spec. For information on the
62 * encodings refer to section "7.6 - Variable Length Data". Return
63 * the number of bytes read.
64 *
65 * @param addr The address where the ULEB128 data is stored.
66 * @param ret Address to store the result.
67 */
68uint64_t ReadUleb128(const char* addr, uint64_t* ret);
69
70/**
71 * Read unsigned LEB128 data from a stream.
72 *
73 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
74 * from Appendix C of the DWARF 3 spec. For information on the
75 * encodings refer to section "7.6 - Variable Length Data". Return
76 * false on stream error, true on success.
77 *
78 * @param is The input stream where the ULEB128 data is to be read from.
79 * @param ret Address to store the result.
80 */
81bool ReadUleb128(raw_istream& is, uint64_t* ret);
82
83/**
84 * Unsigned LEB128 streaming reader.
85 *
86 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
87 * from Appendix C of the DWARF 3 spec. For information on the
88 * encodings refer to section "7.6 - Variable Length Data".
89 */
91 public:
92 /**
93 * Decode a single ULEB128 value. Returns after a single ULEB128 value has
94 * been read or insufficient input (call in a loop to get multiple values).
95 * If a value is returned, internal state is reset so it's safe to immediately
96 * call this function again to decode another value.
97 *
98 * @param in Input data; modified as data is consumed (any unconsumed data
99 * is left when function returns).
100 * @return value (in std::optional)
101 */
102 std::optional<uint64_t> ReadOne(std::span<const uint8_t>* in);
103
104 private:
105 uint64_t m_result = 0;
106 int m_shift = 0;
107};
108
109} // namespace wpi
110
111#endif // WPIUTIL_WPI_LEB128_H_
Unsigned LEB128 streaming reader.
Definition: leb128.h:90
std::optional< uint64_t > ReadOne(std::span< const uint8_t > *in)
Decode a single ULEB128 value.
Definition: raw_istream.h:22
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:43
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
Definition: ntcore_cpp.h:26
uint64_t SizeUleb128(uint64_t val)
Get size of unsigned LEB128 data.
uint64_t ReadUleb128(const char *addr, uint64_t *ret)
Read unsigned LEB128 data.
uint64_t WriteUleb128(SmallVectorImpl< char > &dest, uint64_t val)
Write unsigned LEB128 data.