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 | |
| 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 | |
| 25 | namespace art { |
| 26 | |
| 27 | namespace JDWP { |
| 28 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 29 | static inline uint32_t Get4BE(unsigned char const* pSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 30 | return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3]; |
| 31 | } |
| 32 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 33 | static inline uint8_t Read1(unsigned const char** ppSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 34 | return *(*ppSrc)++; |
| 35 | } |
| 36 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 37 | static inline uint16_t Read2BE(unsigned char const** ppSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 38 | const unsigned char* pSrc = *ppSrc; |
| 39 | *ppSrc = pSrc + 2; |
| 40 | return pSrc[0] << 8 | pSrc[1]; |
| 41 | } |
| 42 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 43 | static inline uint32_t Read4BE(unsigned char const** ppSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 44 | 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 Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 53 | static inline uint64_t Read8BE(unsigned char const** ppSrc) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 54 | 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 Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 73 | static inline char* ReadNewUtf8String(unsigned char const** ppSrc, size_t* pLength) { |
| 74 | uint32_t length = Read4BE(ppSrc); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 75 | 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 Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 83 | static inline void Set1(uint8_t* buf, uint8_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 84 | *buf = (uint8_t)(val); |
| 85 | } |
| 86 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 87 | static inline void Set2BE(uint8_t* buf, uint16_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 88 | *buf++ = (uint8_t)(val >> 8); |
| 89 | *buf = (uint8_t)(val); |
| 90 | } |
| 91 | |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 92 | static inline void Set4BE(uint8_t* buf, uint32_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 93 | *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 Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 99 | static inline void Set8BE(uint8_t* buf, uint64_t val) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 100 | *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 Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame^] | 110 | static inline void Write1BE(uint8_t** dst, uint8_t value) { |
| 111 | Set1(*dst, value); |
| 112 | *dst += sizeof(value); |
| 113 | } |
| 114 | |
| 115 | static inline void Write2BE(uint8_t** dst, uint16_t value) { |
| 116 | Set2BE(*dst, value); |
| 117 | *dst += sizeof(value); |
| 118 | } |
| 119 | |
| 120 | static inline void Write4BE(uint8_t** dst, uint32_t value) { |
| 121 | Set4BE(*dst, value); |
| 122 | *dst += sizeof(value); |
| 123 | } |
| 124 | |
| 125 | static inline void Write8BE(uint8_t** dst, uint64_t value) { |
| 126 | Set8BE(*dst, value); |
| 127 | *dst += sizeof(value); |
| 128 | } |
| 129 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 130 | /* |
| 131 | * Stuff a UTF-8 string into the buffer. |
| 132 | */ |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 133 | static inline void SetUtf8String(uint8_t* buf, const uint8_t* str) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 134 | uint32_t strLen = strlen((const char*)str); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 135 | Set4BE(buf, strLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 136 | memcpy(buf + sizeof(uint32_t), str, strLen); |
| 137 | } |
| 138 | |
| 139 | } // namespace JDWP |
| 140 | |
| 141 | } // namespace art |
| 142 | |
| 143 | #endif // ART_JDWP_BITS_H_ |