blob: 307167e5dc2f12b2f0a7745f16475c8ac345438b [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 * 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
28namespace art {
29
30namespace JDWP {
31
32/*
33 * Data structure used to track buffer use.
34 */
35struct 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 */
46ExpandBuf* 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 */
60void 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 */
72uint8_t* expandBufGetBuffer(ExpandBuf* pBuf) {
73 return pBuf->storage;
74}
75
76/*
77 * Get the amount of data currently in the buffer.
78 */
79size_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 */
88static 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) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -070099 LOG(FATAL) << "realloc(" << pBuf->maxLen << ") failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100 }
101
102 pBuf->storage = newPtr;
103}
104
105/*
106 * Allocate some space in the buffer.
107 */
108uint8_t* expandBufAddSpace(ExpandBuf* pBuf, int gapSize) {
109 uint8_t* gapStart;
110
111 ensureSpace(pBuf, gapSize);
112 gapStart = pBuf->storage + pBuf->curLen;
113 /* do we want to garbage-fill the gap for debugging? */
114 pBuf->curLen += gapSize;
115
116 return gapStart;
117}
118
119/*
120 * Append a byte.
121 */
122void expandBufAdd1(ExpandBuf* pBuf, uint8_t val) {
123 ensureSpace(pBuf, sizeof(val));
124 *(pBuf->storage + pBuf->curLen) = val;
125 pBuf->curLen++;
126}
127
128/*
129 * Append two big-endian bytes.
130 */
131void expandBufAdd2BE(ExpandBuf* pBuf, uint16_t val) {
132 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700133 Set2BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700134 pBuf->curLen += sizeof(val);
135}
136
137/*
138 * Append four big-endian bytes.
139 */
140void expandBufAdd4BE(ExpandBuf* pBuf, uint32_t val) {
141 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700142 Set4BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700143 pBuf->curLen += sizeof(val);
144}
145
146/*
147 * Append eight big-endian bytes.
148 */
149void expandBufAdd8BE(ExpandBuf* pBuf, uint64_t val) {
150 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700151 Set8BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 pBuf->curLen += sizeof(val);
153}
154
Elliott Hughesa2155262011-11-16 16:26:58 -0800155static void SetUtf8String(uint8_t* buf, const char* str, size_t strLen) {
Elliott Hughes21f32d72011-11-09 17:44:13 -0800156 Set4BE(buf, strLen);
157 memcpy(buf + sizeof(uint32_t), str, strLen);
158}
159
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700160/*
161 * Add a UTF8 string as a 4-byte length followed by a non-NULL-terminated
162 * string.
163 *
164 * Because these strings are coming out of the VM, it's safe to assume that
165 * they can be null-terminated (either they don't have null bytes or they
166 * have stored null bytes in a multi-byte encoding).
167 */
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800168void expandBufAddUtf8String(ExpandBuf* pBuf, const char* s) {
169 int strLen = strlen(s);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700170 ensureSpace(pBuf, sizeof(uint32_t) + strLen);
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800171 SetUtf8String(pBuf->storage + pBuf->curLen, s, strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 pBuf->curLen += sizeof(uint32_t) + strLen;
173}
174
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800175void expandBufAddUtf8String(ExpandBuf* pBuf, const std::string& s) {
176 ensureSpace(pBuf, sizeof(uint32_t) + s.size());
177 SetUtf8String(pBuf->storage + pBuf->curLen, s.data(), s.size());
178 pBuf->curLen += sizeof(uint32_t) + s.size();
179}
180
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181} // namespace JDWP
182
183} // namespace art