1 /* 2 * Copyright (C) 2011 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 ART_COMPILER_LEB128_ENCODER_H_ 18 #define ART_COMPILER_LEB128_ENCODER_H_ 19 20 #include "base/macros.h" 21 22 namespace art { 23 24 // An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format. 25 class UnsignedLeb128EncodingVector { 26 public: 27 UnsignedLeb128EncodingVector() { 28 } 29 30 void PushBack(uint32_t value) { 31 bool done = false; 32 do { 33 uint8_t out = value & 0x7f; 34 if (out != value) { 35 data_.push_back(out | 0x80); 36 value >>= 7; 37 } else { 38 data_.push_back(out); 39 done = true; 40 } 41 } while (!done); 42 } 43 44 template<typename It> 45 void InsertBack(It cur, It end) { 46 for (; cur != end; ++cur) { 47 PushBack(*cur); 48 } 49 } 50 51 const std::vector<uint8_t>& GetData() const { 52 return data_; 53 } 54 55 private: 56 std::vector<uint8_t> data_; 57 58 DISALLOW_COPY_AND_ASSIGN(UnsignedLeb128EncodingVector); 59 }; 60 61 } // namespace art 62 63 #endif // ART_COMPILER_LEB128_ENCODER_H_ 64