blob: 3b951f9a8c1a865b59e94b6198667911c2c63255 [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>
24
25namespace art {
26
27namespace JDWP {
28
Elliott Hughesf7c3b662011-10-27 12:04:56 -070029static inline uint32_t Get4BE(unsigned char const* pSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070030 return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3];
31}
32
Elliott Hughesf7c3b662011-10-27 12:04:56 -070033static inline uint8_t Read1(unsigned const char** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070034 return *(*ppSrc)++;
35}
36
Elliott Hughesf7c3b662011-10-27 12:04:56 -070037static inline uint16_t Read2BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070038 const unsigned char* pSrc = *ppSrc;
39 *ppSrc = pSrc + 2;
40 return pSrc[0] << 8 | pSrc[1];
41}
42
Elliott Hughesf7c3b662011-10-27 12:04:56 -070043static inline uint32_t Read4BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070044 const unsigned char* pSrc = *ppSrc;
45 uint32_t result = pSrc[0] << 24;
46 result |= pSrc[1] << 16;
47 result |= pSrc[2] << 8;
48 result |= pSrc[3];
49 *ppSrc = pSrc + 4;
50 return result;
51}
52
Elliott Hughesf7c3b662011-10-27 12:04:56 -070053static inline uint64_t Read8BE(unsigned char const** ppSrc) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070054 const unsigned char* pSrc = *ppSrc;
55 uint32_t high = pSrc[0];
56 high = (high << 8) | pSrc[1];
57 high = (high << 8) | pSrc[2];
58 high = (high << 8) | pSrc[3];
59 uint32_t low = pSrc[4];
60 low = (low << 8) | pSrc[5];
61 low = (low << 8) | pSrc[6];
62 low = (low << 8) | pSrc[7];
63 *ppSrc = pSrc + 8;
64 return ((uint64_t) high << 32) | (uint64_t) low;
65}
66
67/*
68 * Read a UTF-8 string into newly-allocated storage, and null-terminate it.
69 *
70 * Returns the string and its length. (The latter is probably unnecessary
71 * for the way we're using UTF8.)
72 */
Elliott Hughesf7c3b662011-10-27 12:04:56 -070073static inline char* ReadNewUtf8String(unsigned char const** ppSrc, size_t* pLength) {
74 uint32_t length = Read4BE(ppSrc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070075 char* buf = (char*) malloc(length+1);
76 memcpy(buf, *ppSrc, length);
77 buf[length] = '\0';
78 (*ppSrc) += length;
79 *pLength = length;
80 return buf;
81}
82
Elliott Hughesf7c3b662011-10-27 12:04:56 -070083static inline void Set1(uint8_t* buf, uint8_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084 *buf = (uint8_t)(val);
85}
86
Elliott Hughesf7c3b662011-10-27 12:04:56 -070087static inline void Set2BE(uint8_t* buf, uint16_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070088 *buf++ = (uint8_t)(val >> 8);
89 *buf = (uint8_t)(val);
90}
91
Elliott Hughesf7c3b662011-10-27 12:04:56 -070092static inline void Set4BE(uint8_t* buf, uint32_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093 *buf++ = (uint8_t)(val >> 24);
94 *buf++ = (uint8_t)(val >> 16);
95 *buf++ = (uint8_t)(val >> 8);
96 *buf = (uint8_t)(val);
97}
98
Elliott Hughesf7c3b662011-10-27 12:04:56 -070099static inline void Set8BE(uint8_t* buf, uint64_t val) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100 *buf++ = (uint8_t)(val >> 56);
101 *buf++ = (uint8_t)(val >> 48);
102 *buf++ = (uint8_t)(val >> 40);
103 *buf++ = (uint8_t)(val >> 32);
104 *buf++ = (uint8_t)(val >> 24);
105 *buf++ = (uint8_t)(val >> 16);
106 *buf++ = (uint8_t)(val >> 8);
107 *buf = (uint8_t)(val);
108}
109
Elliott Hughes7162ad92011-10-27 14:08:42 -0700110static inline void Write1BE(uint8_t** dst, uint8_t value) {
111 Set1(*dst, value);
112 *dst += sizeof(value);
113}
114
115static inline void Write2BE(uint8_t** dst, uint16_t value) {
116 Set2BE(*dst, value);
117 *dst += sizeof(value);
118}
119
120static inline void Write4BE(uint8_t** dst, uint32_t value) {
121 Set4BE(*dst, value);
122 *dst += sizeof(value);
123}
124
125static inline void Write8BE(uint8_t** dst, uint64_t value) {
126 Set8BE(*dst, value);
127 *dst += sizeof(value);
128}
129
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130/*
131 * Stuff a UTF-8 string into the buffer.
132 */
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700133static inline void SetUtf8String(uint8_t* buf, const uint8_t* str) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134 uint32_t strLen = strlen((const char*)str);
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700135 Set4BE(buf, strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700136 memcpy(buf + sizeof(uint32_t), str, strLen);
137}
138
139} // namespace JDWP
140
141} // namespace art
142
143#endif // ART_JDWP_BITS_H_