blob: 414668c0de518dd0fa94074dbfb4874f7d13d1fc [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
Alex Lightfc588092020-01-23 15:39:08 -08002 * Copyright 2020 The Android Open Source Project
Elliott Hughes872d4ec2011-10-21 17:07:15 -07003 *
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 Lightfc588092020-01-23 15:39:08 -080017#ifndef ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_
18#define ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_
Elliott Hughes872d4ec2011-10-21 17:07:15 -070019
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <stdint.h>
Alex Lightfc588092020-01-23 15:39:08 -080021#include <endian.h>
Elliott Hughes545a0642011-11-08 19:10:03 -080022#include <vector>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070023
24namespace art {
25
Alex Light78815ee2020-01-28 11:09:42 -080026template<typename T>
27inline 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 Hughes872d4ec2011-10-21 17:07:15 -070031}
32
Alex Lightfc588092020-01-23 15:39:08 -080033inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
Elliott Hughes545a0642011-11-08 19:10:03 -080034 bytes.push_back(value);
35}
36
Alex Lightfc588092020-01-23 15:39:08 -080037inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
Alex Light78815ee2020-01-28 11:09:42 -080038 AppendBytes<uint16_t>(bytes, htobe16(value));
Elliott Hughes545a0642011-11-08 19:10:03 -080039}
40
Alex Lightfc588092020-01-23 15:39:08 -080041inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
Alex Light78815ee2020-01-28 11:09:42 -080042 AppendBytes<uint32_t>(bytes, htobe32(value));
Elliott Hughes545a0642011-11-08 19:10:03 -080043}
44
Alex Lightfc588092020-01-23 15:39:08 -080045inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
Alex Light78815ee2020-01-28 11:09:42 -080046 AppendBytes<uint64_t>(bytes, htobe64(value));
Elliott Hughes545a0642011-11-08 19:10:03 -080047}
48
Alex Light78815ee2020-01-28 11:09:42 -080049inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
Elliott Hughes545a0642011-11-08 19:10:03 -080050 Append4BE(bytes, char_count);
51 for (size_t i = 0; i < char_count; ++i) {
52 Append2BE(bytes, chars[i]);
53 }
54}
55
Alex Lightfc588092020-01-23 15:39:08 -080056inline void AppendUtf16CompressedBE(std::vector<uint8_t>& bytes,
Alex Light78815ee2020-01-28 11:09:42 -080057 const uint8_t* chars,
58 size_t char_count) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070059 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 Light78815ee2020-01-28 11:09:42 -080065template <typename T>
66inline void SetBytes(uint8_t* buf, T val) {
67 memcpy(buf, &val, sizeof(T));
68}
69
Alex Lightfc588092020-01-23 15:39:08 -080070inline void Set1(uint8_t* buf, uint8_t val) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -080071 *buf = val;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072}
73
Alex Lightfc588092020-01-23 15:39:08 -080074inline void Set2BE(uint8_t* buf, uint16_t val) {
Alex Light78815ee2020-01-28 11:09:42 -080075 SetBytes<uint16_t>(buf, htobe16(val));
Elliott Hughes872d4ec2011-10-21 17:07:15 -070076}
77
Alex Lightfc588092020-01-23 15:39:08 -080078inline void Set4BE(uint8_t* buf, uint32_t val) {
Alex Light78815ee2020-01-28 11:09:42 -080079 SetBytes<uint32_t>(buf, htobe32(val));
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080}
81
Alex Lightfc588092020-01-23 15:39:08 -080082inline void Set8BE(uint8_t* buf, uint64_t val) {
Alex Light78815ee2020-01-28 11:09:42 -080083 SetBytes<uint64_t>(buf, htobe64(val));
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084}
85
Alex Lightfc588092020-01-23 15:39:08 -080086inline void Write1BE(uint8_t** dst, uint8_t value) {
Elliott Hughes7162ad92011-10-27 14:08:42 -070087 Set1(*dst, value);
88 *dst += sizeof(value);
89}
90
Alex Lightfc588092020-01-23 15:39:08 -080091inline void Write2BE(uint8_t** dst, uint16_t value) {
Elliott Hughes24437992011-11-30 14:49:33 -080092 Set2BE(*dst, value);
93 *dst += sizeof(value);
94}
95
Alex Lightfc588092020-01-23 15:39:08 -080096inline void Write4BE(uint8_t** dst, uint32_t value) {
Elliott Hughes7162ad92011-10-27 14:08:42 -070097 Set4BE(*dst, value);
98 *dst += sizeof(value);
99}
100
Alex Lightfc588092020-01-23 15:39:08 -0800101inline void Write8BE(uint8_t** dst, uint64_t value) {
Elliott Hughes24437992011-11-30 14:49:33 -0800102 Set8BE(*dst, value);
103 *dst += sizeof(value);
104}
Alex Light78815ee2020-01-28 11:09:42 -0800105
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700106} // namespace art
107
Alex Lightfc588092020-01-23 15:39:08 -0800108#endif // ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_