Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/leb128.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include "wpi/SpanExtras.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | #include "wpi/raw_istream.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | #include "wpi/raw_ostream.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | |
| 11 | namespace wpi { |
| 12 | |
| 13 | uint64_t SizeUleb128(uint64_t val) { |
| 14 | size_t count = 0; |
| 15 | do { |
| 16 | val >>= 7; |
| 17 | ++count; |
| 18 | } while (val != 0); |
| 19 | return count; |
| 20 | } |
| 21 | |
| 22 | uint64_t WriteUleb128(SmallVectorImpl<char>& dest, uint64_t val) { |
| 23 | size_t count = 0; |
| 24 | |
| 25 | do { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 26 | uint8_t byte = val & 0x7f; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | val >>= 7; |
| 28 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | if (val != 0) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | byte |= 0x80; // mark this byte to show that more bytes will follow |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | |
| 33 | dest.push_back(byte); |
| 34 | count++; |
| 35 | } while (val != 0); |
| 36 | |
| 37 | return count; |
| 38 | } |
| 39 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 40 | void WriteUleb128(raw_ostream& os, uint64_t val) { |
| 41 | do { |
| 42 | uint8_t byte = val & 0x7f; |
| 43 | val >>= 7; |
| 44 | |
| 45 | if (val != 0) { |
| 46 | byte |= 0x80; // mark this byte to show that more bytes will follow |
| 47 | } |
| 48 | |
| 49 | os << byte; |
| 50 | } while (val != 0); |
| 51 | } |
| 52 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | uint64_t ReadUleb128(const char* addr, uint64_t* ret) { |
| 54 | uint64_t result = 0; |
| 55 | int shift = 0; |
| 56 | size_t count = 0; |
| 57 | |
| 58 | while (1) { |
| 59 | unsigned char byte = *reinterpret_cast<const unsigned char*>(addr); |
| 60 | addr++; |
| 61 | count++; |
| 62 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 63 | result |= (byte & 0x7fULL) << shift; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | shift += 7; |
| 65 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | if (!(byte & 0x80)) { |
| 67 | break; |
| 68 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | *ret = result; |
| 72 | |
| 73 | return count; |
| 74 | } |
| 75 | |
| 76 | bool ReadUleb128(raw_istream& is, uint64_t* ret) { |
| 77 | uint64_t result = 0; |
| 78 | int shift = 0; |
| 79 | |
| 80 | while (1) { |
| 81 | unsigned char byte; |
| 82 | is.read(reinterpret_cast<char*>(&byte), 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 83 | if (is.has_error()) { |
| 84 | return false; |
| 85 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 87 | result |= (byte & 0x7fULL) << shift; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 88 | shift += 7; |
| 89 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 90 | if (!(byte & 0x80)) { |
| 91 | break; |
| 92 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | *ret = result; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 100 | std::optional<uint64_t> Uleb128Reader::ReadOne(std::span<const uint8_t>* in) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 101 | while (!in->empty()) { |
| 102 | uint8_t byte = in->front(); |
| 103 | *in = wpi::drop_front(*in); |
| 104 | |
| 105 | m_result |= (byte & 0x7fULL) << m_shift; |
| 106 | m_shift += 7; |
| 107 | |
| 108 | if (!(byte & 0x80)) { |
| 109 | uint64_t result = m_result; |
| 110 | m_result = 0; |
| 111 | m_shift = 0; |
| 112 | return result; |
| 113 | } |
| 114 | } |
| 115 | return std::nullopt; |
| 116 | } |
| 117 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 118 | } // namespace wpi |