1 /*
2  * Copyright (C) 2008 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_RUNTIME_JDWP_JDWP_BITS_H_
18 #define ART_RUNTIME_JDWP_JDWP_BITS_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <string>
25 #include <vector>
26 
27 namespace art {
28 
29 namespace JDWP {
30 
Get4BE(unsigned char const * pSrc)31 static inline uint32_t Get4BE(unsigned char const* pSrc) {
32   return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3];
33 }
34 
Append1BE(std::vector<uint8_t> & bytes,uint8_t value)35 static inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
36   bytes.push_back(value);
37 }
38 
Append2BE(std::vector<uint8_t> & bytes,uint16_t value)39 static inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
40   bytes.push_back(static_cast<uint8_t>(value >> 8));
41   bytes.push_back(static_cast<uint8_t>(value));
42 }
43 
Append4BE(std::vector<uint8_t> & bytes,uint32_t value)44 static inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
45   bytes.push_back(static_cast<uint8_t>(value >> 24));
46   bytes.push_back(static_cast<uint8_t>(value >> 16));
47   bytes.push_back(static_cast<uint8_t>(value >> 8));
48   bytes.push_back(static_cast<uint8_t>(value));
49 }
50 
Append8BE(std::vector<uint8_t> & bytes,uint64_t value)51 static inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
52   bytes.push_back(static_cast<uint8_t>(value >> 56));
53   bytes.push_back(static_cast<uint8_t>(value >> 48));
54   bytes.push_back(static_cast<uint8_t>(value >> 40));
55   bytes.push_back(static_cast<uint8_t>(value >> 32));
56   bytes.push_back(static_cast<uint8_t>(value >> 24));
57   bytes.push_back(static_cast<uint8_t>(value >> 16));
58   bytes.push_back(static_cast<uint8_t>(value >> 8));
59   bytes.push_back(static_cast<uint8_t>(value));
60 }
61 
AppendUtf16BE(std::vector<uint8_t> & bytes,const uint16_t * chars,size_t char_count)62 static inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
63   Append4BE(bytes, char_count);
64   for (size_t i = 0; i < char_count; ++i) {
65     Append2BE(bytes, chars[i]);
66   }
67 }
68 
69 // @deprecated
Set1(uint8_t * buf,uint8_t val)70 static inline void Set1(uint8_t* buf, uint8_t val) {
71   *buf = val;
72 }
73 
74 // @deprecated
Set2BE(uint8_t * buf,uint16_t val)75 static inline void Set2BE(uint8_t* buf, uint16_t val) {
76   *buf++ = (uint8_t)(val >> 8);
77   *buf = (uint8_t)(val);
78 }
79 
80 // @deprecated
Set4BE(uint8_t * buf,uint32_t val)81 static inline void Set4BE(uint8_t* buf, uint32_t val) {
82   *buf++ = (uint8_t)(val >> 24);
83   *buf++ = (uint8_t)(val >> 16);
84   *buf++ = (uint8_t)(val >> 8);
85   *buf = (uint8_t)(val);
86 }
87 
88 // @deprecated
Set8BE(uint8_t * buf,uint64_t val)89 static inline void Set8BE(uint8_t* buf, uint64_t val) {
90   *buf++ = (uint8_t)(val >> 56);
91   *buf++ = (uint8_t)(val >> 48);
92   *buf++ = (uint8_t)(val >> 40);
93   *buf++ = (uint8_t)(val >> 32);
94   *buf++ = (uint8_t)(val >> 24);
95   *buf++ = (uint8_t)(val >> 16);
96   *buf++ = (uint8_t)(val >> 8);
97   *buf = (uint8_t)(val);
98 }
99 
Write1BE(uint8_t ** dst,uint8_t value)100 static inline void Write1BE(uint8_t** dst, uint8_t value) {
101   Set1(*dst, value);
102   *dst += sizeof(value);
103 }
104 
Write2BE(uint8_t ** dst,uint16_t value)105 static inline void Write2BE(uint8_t** dst, uint16_t value) {
106   Set2BE(*dst, value);
107   *dst += sizeof(value);
108 }
109 
Write4BE(uint8_t ** dst,uint32_t value)110 static inline void Write4BE(uint8_t** dst, uint32_t value) {
111   Set4BE(*dst, value);
112   *dst += sizeof(value);
113 }
114 
Write8BE(uint8_t ** dst,uint64_t value)115 static inline void Write8BE(uint8_t** dst, uint64_t value) {
116   Set8BE(*dst, value);
117   *dst += sizeof(value);
118 }
119 
120 }  // namespace JDWP
121 
122 }  // namespace art
123 
124 #endif  // ART_RUNTIME_JDWP_JDWP_BITS_H_
125