• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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_LIBARTBASE_BASE_ENDIAN_UTILS_H_
18 #define ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_
19 
20 #include <stdint.h>
21 #include <endian.h>
22 #include <vector>
23 
24 namespace art {
25 
26 template<typename T>
AppendBytes(std::vector<uint8_t> & bytes,T data)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));
31 }
32 
Append1BE(std::vector<uint8_t> & bytes,uint8_t value)33 inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
34   bytes.push_back(value);
35 }
36 
Append2BE(std::vector<uint8_t> & bytes,uint16_t value)37 inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
38   AppendBytes<uint16_t>(bytes, htobe16(value));
39 }
40 
Append4BE(std::vector<uint8_t> & bytes,uint32_t value)41 inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
42   AppendBytes<uint32_t>(bytes, htobe32(value));
43 }
44 
Append8BE(std::vector<uint8_t> & bytes,uint64_t value)45 inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
46   AppendBytes<uint64_t>(bytes, htobe64(value));
47 }
48 
AppendUtf16BE(std::vector<uint8_t> & bytes,const uint16_t * chars,size_t char_count)49 inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
50   Append4BE(bytes, char_count);
51   for (size_t i = 0; i < char_count; ++i) {
52     Append2BE(bytes, chars[i]);
53   }
54 }
55 
AppendUtf16CompressedBE(std::vector<uint8_t> & bytes,const uint8_t * chars,size_t char_count)56 inline void AppendUtf16CompressedBE(std::vector<uint8_t>& bytes,
57                                     const uint8_t* chars,
58                                     size_t char_count) {
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 
65 template <typename T>
SetBytes(uint8_t * buf,T val)66 inline void SetBytes(uint8_t* buf, T val) {
67   memcpy(buf, &val, sizeof(T));
68 }
69 
Set1(uint8_t * buf,uint8_t val)70 inline void Set1(uint8_t* buf, uint8_t val) {
71   *buf = val;
72 }
73 
Set2BE(uint8_t * buf,uint16_t val)74 inline void Set2BE(uint8_t* buf, uint16_t val) {
75   SetBytes<uint16_t>(buf, htobe16(val));
76 }
77 
Set4BE(uint8_t * buf,uint32_t val)78 inline void Set4BE(uint8_t* buf, uint32_t val) {
79   SetBytes<uint32_t>(buf, htobe32(val));
80 }
81 
Set8BE(uint8_t * buf,uint64_t val)82 inline void Set8BE(uint8_t* buf, uint64_t val) {
83   SetBytes<uint64_t>(buf, htobe64(val));
84 }
85 
Write1BE(uint8_t ** dst,uint8_t value)86 inline void Write1BE(uint8_t** dst, uint8_t value) {
87   Set1(*dst, value);
88   *dst += sizeof(value);
89 }
90 
Write2BE(uint8_t ** dst,uint16_t value)91 inline void Write2BE(uint8_t** dst, uint16_t value) {
92   Set2BE(*dst, value);
93   *dst += sizeof(value);
94 }
95 
Write4BE(uint8_t ** dst,uint32_t value)96 inline void Write4BE(uint8_t** dst, uint32_t value) {
97   Set4BE(*dst, value);
98   *dst += sizeof(value);
99 }
100 
Write8BE(uint8_t ** dst,uint64_t value)101 inline void Write8BE(uint8_t** dst, uint64_t value) {
102   Set8BE(*dst, value);
103   *dst += sizeof(value);
104 }
105 
106 }  // namespace art
107 
108 #endif  // ART_LIBARTBASE_BASE_ENDIAN_UTILS_H_
109