blob: 5536c52f69245e72e8a8f6ca2e5a0aa4fe1a672e [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/*
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -080070 * Reads a UTF-8 string into a std::string.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070071 */
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -080072static inline std::string ReadNewUtf8String(unsigned char const** ppSrc) {
Elliott Hughesf7c3b662011-10-27 12:04:56 -070073 uint32_t length = Read4BE(ppSrc);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -080074 std::string s;
75 s.resize(length);
76 memcpy(&s[0], *ppSrc, length);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 (*ppSrc) += length;
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -080078 return s;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070079}
80
Elliott Hughes545a0642011-11-08 19:10:03 -080081static inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
82 bytes.push_back(value);
83}
84
85static inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
86 bytes.push_back(static_cast<uint8_t>(value >> 8));
87 bytes.push_back(static_cast<uint8_t>(value));
88}
89
90static inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
91 bytes.push_back(static_cast<uint8_t>(value >> 24));
92 bytes.push_back(static_cast<uint8_t>(value >> 16));
93 bytes.push_back(static_cast<uint8_t>(value >> 8));
94 bytes.push_back(static_cast<uint8_t>(value));
95}
96
97static inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
98 bytes.push_back(static_cast<uint8_t>(value >> 56));
99 bytes.push_back(static_cast<uint8_t>(value >> 48));
100 bytes.push_back(static_cast<uint8_t>(value >> 40));
101 bytes.push_back(static_cast<uint8_t>(value >> 32));
102 bytes.push_back(static_cast<uint8_t>(value >> 24));
103 bytes.push_back(static_cast<uint8_t>(value >> 16));
104 bytes.push_back(static_cast<uint8_t>(value >> 8));
105 bytes.push_back(static_cast<uint8_t>(value));
106}
107
108static inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
109 Append4BE(bytes, char_count);
110 for (size_t i = 0; i < char_count; ++i) {
111 Append2BE(bytes, chars[i]);
112 }
113}
114
115// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700116static inline void Set1(uint8_t* buf, uint8_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 *buf = (uint8_t)(val);
118}
119
Elliott Hughes545a0642011-11-08 19:10:03 -0800120// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700121static inline void Set2BE(uint8_t* buf, uint16_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 *buf++ = (uint8_t)(val >> 8);
123 *buf = (uint8_t)(val);
124}
125
Elliott Hughes545a0642011-11-08 19:10:03 -0800126// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700127static inline void Set4BE(uint8_t* buf, uint32_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700128 *buf++ = (uint8_t)(val >> 24);
129 *buf++ = (uint8_t)(val >> 16);
130 *buf++ = (uint8_t)(val >> 8);
131 *buf = (uint8_t)(val);
132}
133
Elliott Hughes545a0642011-11-08 19:10:03 -0800134// @deprecated
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700135static inline void Set8BE(uint8_t* buf, uint64_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136 *buf++ = (uint8_t)(val >> 56);
137 *buf++ = (uint8_t)(val >> 48);
138 *buf++ = (uint8_t)(val >> 40);
139 *buf++ = (uint8_t)(val >> 32);
140 *buf++ = (uint8_t)(val >> 24);
141 *buf++ = (uint8_t)(val >> 16);
142 *buf++ = (uint8_t)(val >> 8);
143 *buf = (uint8_t)(val);
144}
145
Elliott Hughes7162ad92011-10-27 14:08:42 -0700146static inline void Write1BE(uint8_t** dst, uint8_t value) {
147 Set1(*dst, value);
148 *dst += sizeof(value);
149}
150
Elliott Hughes24437992011-11-30 14:49:33 -0800151static inline void Write2BE(uint8_t** dst, uint16_t value) {
152 Set2BE(*dst, value);
153 *dst += sizeof(value);
154}
155
Elliott Hughes7162ad92011-10-27 14:08:42 -0700156static inline void Write4BE(uint8_t** dst, uint32_t value) {
157 Set4BE(*dst, value);
158 *dst += sizeof(value);
159}
160
Elliott Hughes24437992011-11-30 14:49:33 -0800161static inline void Write8BE(uint8_t** dst, uint64_t value) {
162 Set8BE(*dst, value);
163 *dst += sizeof(value);
164}
165
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166} // namespace JDWP
167
168} // namespace art
169
170#endif // ART_JDWP_BITS_H_