blob: 344d2de9f32f9e3773f3b7c0aec2dd7d9b7dc928 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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_JDWP_BITS_H_
18#define ART_JDWP_BITS_H_
19
20#include <stddef.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
Elliott Hughes545a0642011-11-08 19:10:03 -080024#include <string>
25#include <vector>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070026
27namespace art {
28
29namespace JDWP {
30
Elliott Hughesf7c3b662011-10-27 12:04:56 -070031static inline uint32_t Get4BE(unsigned char const* pSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070032 return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3];
33}
34
Elliott Hughesf7c3b662011-10-27 12:04:56 -070035static inline uint8_t Read1(unsigned const char** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036 return *(*ppSrc)++;
37}
38
Elliott Hughesf7c3b662011-10-27 12:04:56 -070039static inline uint16_t Read2BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040 const unsigned char* pSrc = *ppSrc;
41 *ppSrc = pSrc + 2;
42 return pSrc[0] << 8 | pSrc[1];
43}
44
Elliott Hughesf7c3b662011-10-27 12:04:56 -070045static inline uint32_t Read4BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070046 const unsigned char* pSrc = *ppSrc;
47 uint32_t result = pSrc[0] << 24;
48 result |= pSrc[1] << 16;
49 result |= pSrc[2] << 8;
50 result |= pSrc[3];
51 *ppSrc = pSrc + 4;
52 return result;
53}
54
Elliott Hughesf7c3b662011-10-27 12:04:56 -070055static inline uint64_t Read8BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056 const unsigned char* pSrc = *ppSrc;
57 uint32_t high = pSrc[0];
58 high = (high << 8) | pSrc[1];
59 high = (high << 8) | pSrc[2];
60 high = (high << 8) | pSrc[3];
61 uint32_t low = pSrc[4];
62 low = (low << 8) | pSrc[5];
63 low = (low << 8) | pSrc[6];
64 low = (low << 8) | pSrc[7];
65 *ppSrc = pSrc + 8;
66 return ((uint64_t) high << 32) | (uint64_t) low;
67}
68
69/*
70 * Read a UTF-8 string into newly-allocated storage, and null-terminate it.
71 *
72 * Returns the string and its length. (The latter is probably unnecessary
73 * for the way we're using UTF8.)
74 */
Elliott Hughesf7c3b662011-10-27 12:04:56 -070075static inline char* ReadNewUtf8String(unsigned char const** ppSrc, size_t* pLength) {
76 uint32_t length = Read4BE(ppSrc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 char* buf = (char*) malloc(length+1);
78 memcpy(buf, *ppSrc, length);
79 buf[length] = '\0';
80 (*ppSrc) += length;
81 *pLength = length;
82 return buf;
83}
84
Elliott Hughes545a0642011-11-08 19:10:03 -080085static inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
86 bytes.push_back(value);
87}
88
89static inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
90 bytes.push_back(static_cast<uint8_t>(value >> 8));
91 bytes.push_back(static_cast<uint8_t>(value));
92}
93
94static inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
95 bytes.push_back(static_cast<uint8_t>(value >> 24));
96 bytes.push_back(static_cast<uint8_t>(value >> 16));
97 bytes.push_back(static_cast<uint8_t>(value >> 8));
98 bytes.push_back(static_cast<uint8_t>(value));
99}
100
101static inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
102 bytes.push_back(static_cast<uint8_t>(value >> 56));
103 bytes.push_back(static_cast<uint8_t>(value >> 48));
104 bytes.push_back(static_cast<uint8_t>(value >> 40));
105 bytes.push_back(static_cast<uint8_t>(value >> 32));
106 bytes.push_back(static_cast<uint8_t>(value >> 24));
107 bytes.push_back(static_cast<uint8_t>(value >> 16));
108 bytes.push_back(static_cast<uint8_t>(value >> 8));
109 bytes.push_back(static_cast<uint8_t>(value));
110}
111
112static inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
113 Append4BE(bytes, char_count);
114 for (size_t i = 0; i < char_count; ++i) {
115 Append2BE(bytes, chars[i]);
116 }
117}
118
119// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700120static inline void Set1(uint8_t* buf, uint8_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 *buf = (uint8_t)(val);
122}
123
Elliott Hughes545a0642011-11-08 19:10:03 -0800124// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700125static inline void Set2BE(uint8_t* buf, uint16_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 *buf++ = (uint8_t)(val >> 8);
127 *buf = (uint8_t)(val);
128}
129
Elliott Hughes545a0642011-11-08 19:10:03 -0800130// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700131static inline void Set4BE(uint8_t* buf, uint32_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700132 *buf++ = (uint8_t)(val >> 24);
133 *buf++ = (uint8_t)(val >> 16);
134 *buf++ = (uint8_t)(val >> 8);
135 *buf = (uint8_t)(val);
136}
137
Elliott Hughes545a0642011-11-08 19:10:03 -0800138// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700139static inline void Set8BE(uint8_t* buf, uint64_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700140 *buf++ = (uint8_t)(val >> 56);
141 *buf++ = (uint8_t)(val >> 48);
142 *buf++ = (uint8_t)(val >> 40);
143 *buf++ = (uint8_t)(val >> 32);
144 *buf++ = (uint8_t)(val >> 24);
145 *buf++ = (uint8_t)(val >> 16);
146 *buf++ = (uint8_t)(val >> 8);
147 *buf = (uint8_t)(val);
148}
149
Elliott Hughes7162ad92011-10-27 14:08:42 -0700150static inline void Write1BE(uint8_t** dst, uint8_t value) {
151 Set1(*dst, value);
152 *dst += sizeof(value);
153}
154
Elliott Hughes24437992011-11-30 14:49:33 -0800155static inline void Write2BE(uint8_t** dst, uint16_t value) {
156 Set2BE(*dst, value);
157 *dst += sizeof(value);
158}
159
Elliott Hughes7162ad92011-10-27 14:08:42 -0700160static inline void Write4BE(uint8_t** dst, uint32_t value) {
161 Set4BE(*dst, value);
162 *dst += sizeof(value);
163}
164
Elliott Hughes24437992011-11-30 14:49:33 -0800165static inline void Write8BE(uint8_t** dst, uint64_t value) {
166 Set8BE(*dst, value);
167 *dst += sizeof(value);
168}
169
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170} // namespace JDWP
171
172} // namespace art
173
174#endif // ART_JDWP_BITS_H_