Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_JDWP_JDWP_BITS_H_ |
| 18 | #define ART_RUNTIME_JDWP_JDWP_BITS_H_ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace JDWP { |
| 30 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 31 | static inline uint32_t Get4BE(unsigned char const* pSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 32 | return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3]; |
| 33 | } |
| 34 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 35 | static inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) { |
| 36 | bytes.push_back(value); |
| 37 | } |
| 38 | |
| 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 | |
| 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 | |
| 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 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 62 | static inline void AppendUtf16BE(std::vector<uint8_t>& bytes, const uint16_t* chars, |
| 63 | size_t char_count) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 64 | Append4BE(bytes, char_count); |
| 65 | for (size_t i = 0; i < char_count; ++i) { |
| 66 | Append2BE(bytes, chars[i]); |
| 67 | } |
| 68 | } |
| 69 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 70 | static inline void AppendUtf16CompressedBE(std::vector<uint8_t>& bytes, |
| 71 | const uint8_t* chars, size_t char_count) { |
| 72 | Append4BE(bytes, char_count); |
| 73 | for (size_t i = 0; i < char_count; ++i) { |
| 74 | Append2BE(bytes, static_cast<uint16_t>(chars[i])); |
| 75 | } |
| 76 | } |
| 77 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 78 | // @deprecated |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 79 | static inline void Set1(uint8_t* buf, uint8_t val) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 80 | *buf = val; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 83 | // @deprecated |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 84 | static inline void Set2BE(uint8_t* buf, uint16_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 85 | *buf++ = (uint8_t)(val >> 8); |
| 86 | *buf = (uint8_t)(val); |
| 87 | } |
| 88 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 89 | // @deprecated |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 90 | static inline void Set4BE(uint8_t* buf, uint32_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 91 | *buf++ = (uint8_t)(val >> 24); |
| 92 | *buf++ = (uint8_t)(val >> 16); |
| 93 | *buf++ = (uint8_t)(val >> 8); |
| 94 | *buf = (uint8_t)(val); |
| 95 | } |
| 96 | |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 97 | // @deprecated |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 98 | static inline void Set8BE(uint8_t* buf, uint64_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 99 | *buf++ = (uint8_t)(val >> 56); |
| 100 | *buf++ = (uint8_t)(val >> 48); |
| 101 | *buf++ = (uint8_t)(val >> 40); |
| 102 | *buf++ = (uint8_t)(val >> 32); |
| 103 | *buf++ = (uint8_t)(val >> 24); |
| 104 | *buf++ = (uint8_t)(val >> 16); |
| 105 | *buf++ = (uint8_t)(val >> 8); |
| 106 | *buf = (uint8_t)(val); |
| 107 | } |
| 108 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 109 | static inline void Write1BE(uint8_t** dst, uint8_t value) { |
| 110 | Set1(*dst, value); |
| 111 | *dst += sizeof(value); |
| 112 | } |
| 113 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 114 | static inline void Write2BE(uint8_t** dst, uint16_t value) { |
| 115 | Set2BE(*dst, value); |
| 116 | *dst += sizeof(value); |
| 117 | } |
| 118 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 119 | static inline void Write4BE(uint8_t** dst, uint32_t value) { |
| 120 | Set4BE(*dst, value); |
| 121 | *dst += sizeof(value); |
| 122 | } |
| 123 | |
Elliott Hughes | 2443799 | 2011-11-30 14:49:33 -0800 | [diff] [blame] | 124 | static inline void Write8BE(uint8_t** dst, uint64_t value) { |
| 125 | Set8BE(*dst, value); |
| 126 | *dst += sizeof(value); |
| 127 | } |
| 128 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 129 | } // namespace JDWP |
| 130 | |
| 131 | } // namespace art |
| 132 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 133 | #endif // ART_RUNTIME_JDWP_JDWP_BITS_H_ |