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 | * Implementation of an expandable byte buffer. Designed for serializing |
| 18 | * primitive values, e.g. JDWP replies. |
| 19 | */ |
| 20 | |
| 21 | #include "jdwp/jdwp_bits.h" |
| 22 | #include "jdwp/jdwp_expand_buf.h" |
| 23 | #include "logging.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | namespace JDWP { |
| 31 | |
| 32 | /* |
| 33 | * Data structure used to track buffer use. |
| 34 | */ |
| 35 | struct ExpandBuf { |
| 36 | uint8_t* storage; |
| 37 | int curLen; |
| 38 | int maxLen; |
| 39 | }; |
| 40 | |
| 41 | #define kInitialStorage 64 |
| 42 | |
| 43 | /* |
| 44 | * Allocate a JdwpBuf and some initial storage. |
| 45 | */ |
| 46 | ExpandBuf* expandBufAlloc() { |
| 47 | ExpandBuf* newBuf; |
| 48 | |
| 49 | newBuf = (ExpandBuf*) malloc(sizeof(*newBuf)); |
| 50 | newBuf->storage = (uint8_t*) malloc(kInitialStorage); |
| 51 | newBuf->curLen = 0; |
| 52 | newBuf->maxLen = kInitialStorage; |
| 53 | |
| 54 | return newBuf; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Free a JdwpBuf and associated storage. |
| 59 | */ |
| 60 | void expandBufFree(ExpandBuf* pBuf) { |
| 61 | if (pBuf == NULL) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | free(pBuf->storage); |
| 66 | free(pBuf); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Get a pointer to the start of the buffer. |
| 71 | */ |
| 72 | uint8_t* expandBufGetBuffer(ExpandBuf* pBuf) { |
| 73 | return pBuf->storage; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Get the amount of data currently in the buffer. |
| 78 | */ |
| 79 | size_t expandBufGetLength(ExpandBuf* pBuf) { |
| 80 | return pBuf->curLen; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * Ensure that the buffer has enough space to hold incoming data. If it |
| 86 | * doesn't, resize the buffer. |
| 87 | */ |
| 88 | static void ensureSpace(ExpandBuf* pBuf, int newCount) { |
| 89 | if (pBuf->curLen + newCount <= pBuf->maxLen) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | while (pBuf->curLen + newCount > pBuf->maxLen) { |
| 94 | pBuf->maxLen *= 2; |
| 95 | } |
| 96 | |
| 97 | uint8_t* newPtr = (uint8_t*) realloc(pBuf->storage, pBuf->maxLen); |
| 98 | if (newPtr == NULL) { |
| 99 | LOG(ERROR) << "realloc(" << pBuf->maxLen << ") failed"; |
| 100 | abort(); |
| 101 | } |
| 102 | |
| 103 | pBuf->storage = newPtr; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Allocate some space in the buffer. |
| 108 | */ |
| 109 | uint8_t* expandBufAddSpace(ExpandBuf* pBuf, int gapSize) { |
| 110 | uint8_t* gapStart; |
| 111 | |
| 112 | ensureSpace(pBuf, gapSize); |
| 113 | gapStart = pBuf->storage + pBuf->curLen; |
| 114 | /* do we want to garbage-fill the gap for debugging? */ |
| 115 | pBuf->curLen += gapSize; |
| 116 | |
| 117 | return gapStart; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Append a byte. |
| 122 | */ |
| 123 | void expandBufAdd1(ExpandBuf* pBuf, uint8_t val) { |
| 124 | ensureSpace(pBuf, sizeof(val)); |
| 125 | *(pBuf->storage + pBuf->curLen) = val; |
| 126 | pBuf->curLen++; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Append two big-endian bytes. |
| 131 | */ |
| 132 | void expandBufAdd2BE(ExpandBuf* pBuf, uint16_t val) { |
| 133 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame^] | 134 | Set2BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 135 | pBuf->curLen += sizeof(val); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Append four big-endian bytes. |
| 140 | */ |
| 141 | void expandBufAdd4BE(ExpandBuf* pBuf, uint32_t val) { |
| 142 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame^] | 143 | Set4BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 144 | pBuf->curLen += sizeof(val); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Append eight big-endian bytes. |
| 149 | */ |
| 150 | void expandBufAdd8BE(ExpandBuf* pBuf, uint64_t val) { |
| 151 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame^] | 152 | Set8BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 153 | pBuf->curLen += sizeof(val); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Add a UTF8 string as a 4-byte length followed by a non-NULL-terminated |
| 158 | * string. |
| 159 | * |
| 160 | * Because these strings are coming out of the VM, it's safe to assume that |
| 161 | * they can be null-terminated (either they don't have null bytes or they |
| 162 | * have stored null bytes in a multi-byte encoding). |
| 163 | */ |
| 164 | void expandBufAddUtf8String(ExpandBuf* pBuf, const uint8_t* str) { |
| 165 | int strLen = strlen((const char*)str); |
| 166 | |
| 167 | ensureSpace(pBuf, sizeof(uint32_t) + strLen); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame^] | 168 | SetUtf8String(pBuf->storage + pBuf->curLen, str); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 169 | pBuf->curLen += sizeof(uint32_t) + strLen; |
| 170 | } |
| 171 | |
| 172 | } // namespace JDWP |
| 173 | |
| 174 | } // namespace art |