blob: 96c2cfed0db56e034dd070eaa76fad1b5450ef25 [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) {
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 */
109uint8_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 */
123void 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 */
132void expandBufAdd2BE(ExpandBuf* pBuf, uint16_t val) {
133 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700134 Set2BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700135 pBuf->curLen += sizeof(val);
136}
137
138/*
139 * Append four big-endian bytes.
140 */
141void expandBufAdd4BE(ExpandBuf* pBuf, uint32_t val) {
142 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700143 Set4BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700144 pBuf->curLen += sizeof(val);
145}
146
147/*
148 * Append eight big-endian bytes.
149 */
150void expandBufAdd8BE(ExpandBuf* pBuf, uint64_t val) {
151 ensureSpace(pBuf, sizeof(val));
Elliott Hughesf7c3b662011-10-27 12:04:56 -0700152 Set8BE(pBuf->storage + pBuf->curLen, val);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153 pBuf->curLen += sizeof(val);
154}
155
Elliott Hughesa2155262011-11-16 16:26:58 -0800156static void SetUtf8String(uint8_t* buf, const char* str, size_t strLen) {
Elliott Hughes21f32d72011-11-09 17:44:13 -0800157 Set4BE(buf, strLen);
158 memcpy(buf + sizeof(uint32_t), str, strLen);
159}
160
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161/*
162 * Add a UTF8 string as a 4-byte length followed by a non-NULL-terminated
163 * string.
164 *
165 * Because these strings are coming out of the VM, it's safe to assume that
166 * they can be null-terminated (either they don't have null bytes or they
167 * have stored null bytes in a multi-byte encoding).
168 */
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800169void expandBufAddUtf8String(ExpandBuf* pBuf, const char* s) {
170 int strLen = strlen(s);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700171 ensureSpace(pBuf, sizeof(uint32_t) + strLen);
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800172 SetUtf8String(pBuf->storage + pBuf->curLen, s, strLen);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 pBuf->curLen += sizeof(uint32_t) + strLen;
174}
175
Elliott Hughes4740cdf2011-12-07 14:07:12 -0800176void expandBufAddUtf8String(ExpandBuf* pBuf, const std::string& s) {
177 ensureSpace(pBuf, sizeof(uint32_t) + s.size());
178 SetUtf8String(pBuf->storage + pBuf->curLen, s.data(), s.size());
179 pBuf->curLen += sizeof(uint32_t) + s.size();
180}
181
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182} // namespace JDWP
183
184} // namespace art