Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 2 | * Copyright 2020 The Android Open Source Project |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 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 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 21 | #include <endian.h> |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 22 | #include <vector> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 26 | template<typename T> |
| 27 | inline void AppendBytes(std::vector<uint8_t>& bytes, T data) { |
| 28 | size_t size = bytes.size(); |
| 29 | bytes.resize(size + sizeof(T)); |
| 30 | memcpy(bytes.data() + size, &data, sizeof(T)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 33 | inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 34 | bytes.push_back(value); |
| 35 | } |
| 36 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 37 | inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 38 | AppendBytes<uint16_t>(bytes, htobe16(value)); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 41 | inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 42 | AppendBytes<uint32_t>(bytes, htobe32(value)); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 45 | inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 46 | AppendBytes<uint64_t>(bytes, htobe64(value)); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 49 | inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 50 | Append4BE(bytes, char_count); |
| 51 | for (size_t i = 0; i < char_count; ++i) { |
| 52 | Append2BE(bytes, chars[i]); |
| 53 | } |
| 54 | } |
| 55 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 56 | inline void AppendUtf16CompressedBE(std::vector<uint8_t>& bytes, |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 57 | const uint8_t* chars, |
| 58 | size_t char_count) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 59 | Append4BE(bytes, char_count); |
| 60 | for (size_t i = 0; i < char_count; ++i) { |
| 61 | Append2BE(bytes, static_cast<uint16_t>(chars[i])); |
| 62 | } |
| 63 | } |
| 64 | |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 65 | template <typename T> |
| 66 | inline void SetBytes(uint8_t* buf, T val) { |
| 67 | memcpy(buf, &val, sizeof(T)); |
| 68 | } |
| 69 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 70 | inline void Set1(uint8_t* buf, uint8_t val) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 71 | *buf = val; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 74 | inline void Set2BE(uint8_t* buf, uint16_t val) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 75 | SetBytes<uint16_t>(buf, htobe16(val)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 78 | inline void Set4BE(uint8_t* buf, uint32_t val) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 79 | SetBytes<uint32_t>(buf, htobe32(val)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 82 | inline void Set8BE(uint8_t* buf, uint64_t val) { |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 83 | SetBytes<uint64_t>(buf, htobe64(val)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 86 | inline void Write1BE(uint8_t** dst, uint8_t value) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 87 | Set1(*dst, value); |
| 88 | *dst += sizeof(value); |
| 89 | } |
| 90 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 91 | inline void Write2BE(uint8_t** dst, uint16_t value) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 92 | Set2BE(*dst, value); |
| 93 | *dst += sizeof(value); |
| 94 | } |
| 95 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 96 | inline void Write4BE(uint8_t** dst, uint32_t value) { |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 97 | Set4BE(*dst, value); |
| 98 | *dst += sizeof(value); |
| 99 | } |
| 100 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 101 | inline void Write8BE(uint8_t** dst, uint64_t value) { |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 102 | Set8BE(*dst, value); |
| 103 | *dst += sizeof(value); |
| 104 | } |
Alex Light | 78815ee | 2020-01-28 11:09:42 -0800 | [diff] [blame] | 105 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 106 | } // namespace art |
| 107 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 108 | #endif // ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_ |