Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LINKER_LEB128_H |
| 18 | #define _LINKER_LEB128_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | // Helper classes for decoding LEB128, used in packed relocation data. |
| 23 | // http://en.wikipedia.org/wiki/LEB128 |
| 24 | |
| 25 | class leb128_decoder { |
| 26 | public: |
| 27 | leb128_decoder(const uint8_t* buffer, size_t count) |
| 28 | : current_(buffer), end_(buffer + count) { } |
| 29 | |
| 30 | size_t pop_front() { |
| 31 | size_t value = 0; |
| 32 | |
| 33 | size_t shift = 0; |
| 34 | uint8_t byte; |
| 35 | |
| 36 | do { |
| 37 | if (current_ >= end_) { |
| 38 | __libc_fatal("leb128_decoder ran out of bounds"); |
| 39 | } |
| 40 | byte = *current_++; |
| 41 | value |= static_cast<size_t>(byte & 127) << shift; |
| 42 | shift += 7; |
| 43 | } while (byte & 128); |
| 44 | |
| 45 | return value; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | const uint8_t* current_; |
| 50 | const uint8_t* const end_; |
| 51 | }; |
| 52 | |
| 53 | class sleb128_decoder { |
| 54 | public: |
| 55 | sleb128_decoder(const uint8_t* buffer, size_t count) |
| 56 | : current_(buffer), end_(buffer+count) { } |
| 57 | |
| 58 | size_t pop_front() { |
| 59 | size_t value = 0; |
| 60 | static const size_t size = CHAR_BIT * sizeof(value); |
| 61 | |
| 62 | size_t shift = 0; |
| 63 | uint8_t byte; |
| 64 | |
| 65 | do { |
| 66 | if (current_ >= end_) { |
| 67 | __libc_fatal("leb128_decoder ran out of bounds"); |
| 68 | } |
| 69 | byte = *current_++; |
| 70 | value |= (static_cast<size_t>(byte & 127) << shift); |
| 71 | shift += 7; |
| 72 | } while (byte & 128); |
| 73 | |
| 74 | if (shift < size && (byte & 64)) { |
| 75 | value |= -(static_cast<size_t>(1) << shift); |
| 76 | } |
| 77 | |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | const uint8_t* current_; |
| 83 | const uint8_t* const end_; |
| 84 | }; |
| 85 | |
| 86 | #endif // __LINKER_LEB128_H |
| 87 | |