1872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes/*
2872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
4872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * you may not use this file except in compliance with the License.
6872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * You may obtain a copy of the License at
7872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
8872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes *
10872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * Unless required by applicable law or agreed to in writing, software
11872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * See the License for the specific language governing permissions and
14872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes * limitations under the License.
15872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes */
16872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_JDWP_JDWP_BITS_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_JDWP_JDWP_BITS_H_
19872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
20872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include <stddef.h>
21872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include <stdint.h>
22872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include <stdlib.h>
23872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include <string.h>
24545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes#include <string>
25545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes#include <vector>
26872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
27872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
28872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
29872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace JDWP {
30872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
31f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughesstatic inline uint32_t Get4BE(unsigned char const* pSrc) {
32872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3];
33872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
34872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
35545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstatic inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
36545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(value);
37545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
38545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
39545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstatic inline void Append2BE(std::vector<uint8_t>& bytes, uint16_t value) {
40545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 8));
41545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value));
42545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
43545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
44545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstatic inline void Append4BE(std::vector<uint8_t>& bytes, uint32_t value) {
45545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 24));
46545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 16));
47545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 8));
48545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value));
49545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
50545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
51545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstatic inline void Append8BE(std::vector<uint8_t>& bytes, uint64_t value) {
52545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 56));
53545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 48));
54545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 40));
55545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 32));
56545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 24));
57545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 16));
58545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value >> 8));
59545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  bytes.push_back(static_cast<uint8_t>(value));
60545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
61545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
62545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstatic inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, size_t char_count) {
63545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Append4BE(bytes, char_count);
64545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  for (size_t i = 0; i < char_count; ++i) {
65545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    Append2BE(bytes, chars[i]);
66545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
67545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
68545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
69545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// @deprecated
70f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughesstatic inline void Set1(uint8_t* buf, uint8_t val) {
71277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe  *buf = val;
72872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
73872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
74545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// @deprecated
75f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughesstatic inline void Set2BE(uint8_t* buf, uint16_t val) {
76872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 8);
77872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf = (uint8_t)(val);
78872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
79872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
80545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// @deprecated
81f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughesstatic inline void Set4BE(uint8_t* buf, uint32_t val) {
82872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 24);
83872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 16);
84872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 8);
85872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf = (uint8_t)(val);
86872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
87872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
88545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// @deprecated
89f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughesstatic inline void Set8BE(uint8_t* buf, uint64_t val) {
90872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 56);
91872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 48);
92872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 40);
93872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 32);
94872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 24);
95872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 16);
96872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf++ = (uint8_t)(val >> 8);
97872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  *buf = (uint8_t)(val);
98872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
99872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1007162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesstatic inline void Write1BE(uint8_t** dst, uint8_t value) {
1017162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  Set1(*dst, value);
1027162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  *dst += sizeof(value);
1037162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes}
1047162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
10524437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic inline void Write2BE(uint8_t** dst, uint16_t value) {
10624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  Set2BE(*dst, value);
10724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  *dst += sizeof(value);
10824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
10924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
1107162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesstatic inline void Write4BE(uint8_t** dst, uint32_t value) {
1117162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  Set4BE(*dst, value);
1127162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  *dst += sizeof(value);
1137162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes}
1147162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
11524437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic inline void Write8BE(uint8_t** dst, uint64_t value) {
11624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  Set8BE(*dst, value);
11724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  *dst += sizeof(value);
11824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
11924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
120872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace JDWP
121872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
122872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
123872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
124fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_JDWP_JDWP_BITS_H_
125