-
Notifications
You must be signed in to change notification settings - Fork 1
/
scratchpad.cpp
executable file
·362 lines (293 loc) · 6.66 KB
/
scratchpad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***
scratchpad.cpp
Scratchpad objects.
Need a dynamic memory buffer to hold string input from untrusted sources (file, user input)? Don't know what
size you need ahead of time? Create a scratchpad object to handle it for you.
These are used throughout libcodehappy for various purposes (keyboard buffers, ramfile data, etc.)
Copyright (c) 2014-2022 Chris Street
***/
#include "libcodehappy.h"
#define MIN_ALLOC 32UL
Scratchpad::Scratchpad() {
ialloc = 0;
buf = cend = bend = nullptr;
}
Scratchpad::Scratchpad(u32 nbytes) {
ialloc = 0;
buf = cend = bend = nullptr;
if (nbytes <= 0)
return;
if (nbytes < MIN_ALLOC)
nbytes = MIN_ALLOC;
buf = new u8 [nbytes];
if (is_null(buf))
return;
ialloc = nbytes;
cend = buf;
bend = buf + nbytes;
}
Scratchpad::~Scratchpad() {
free();
}
void Scratchpad::free(void) {
if (buf != nullptr) {
delete [] buf;
}
ialloc = 0;
cend = bend = buf = nullptr;
}
int Scratchpad::addc(u8 ch) {
if (is_null(buf)) {
if (realloc(MIN_ALLOC))
return(1);
} else if (cend + 1 >= bend) {
u32 newc = smallest_pow2_greater_than(ialloc);
if (newc < MIN_ALLOC)
newc = MIN_ALLOC;
if (realloc(newc))
return(1);
}
*cend = ch;
++cend;
*cend = '\000';
return(0);
}
void Scratchpad::addle_u16(u16 val) {
u16 v = CPU_TO_LE16(val);
this->memcat((u8*)&v, (u32)sizeof(u16));
}
void Scratchpad::addle_u32(u32 val) {
u32 v = CPU_TO_LE32(val);
this->memcat((u8*)&v, (u32)sizeof(u32));
}
void Scratchpad::addle_u64(u64 val) {
u64 v = CPU_TO_LE64(val);
this->memcat((u8*)&v, (u32)sizeof(u64));
}
void Scratchpad::addle_16(i16 val) {
i16 v = CPU_TO_LE16(val);
this->memcat((u8*)&v, (u32)sizeof(i16));
}
void Scratchpad::addle_32(i32 val) {
i32 v = CPU_TO_LE32(val);
this->memcat((u8*)&v, (u32)sizeof(i32));
}
void Scratchpad::addle_64(i64 val) {
i64 v = CPU_TO_LE64(val);
this->memcat((u8*)&v, (u32)sizeof(i64));
}
int Scratchpad::insertch(u8 ch, u32 pos) {
realloc(MIN_ALLOC);
NOT_NULL_OR_RETURN(buf, 1);
// ensure that we have enough room
if (addc(0))
return(1);
if (pos > cend - buf)
pos = cend - buf;
if ((cend - (buf + pos)) + 1 > 0)
memmove(buf + pos + 1, buf + pos, (cend - (buf + pos)) + 1);
buf[pos] = ch;
return(0);
}
int Scratchpad::delbytes(u32 nb, u32 pos) {
NOT_NULL_OR_RETURN(buf, 1);
if (iszero(nb))
return 0;
u8* from, *end;
from = buf + pos;
end = from + nb;
if (from > cend)
return 1;
if (end > cend)
return 1;
memmove(from, end, (cend - end) + 1);
cend -= nb;
return 0;
}
int Scratchpad::delfromend(u32 nb) {
NOT_NULL_OR_RETURN(buf, 1);
if (iszero(nb))
return 0;
cend -= nb;
if (cend < buf)
cend = buf;
*cend = '\000';
return 0;
}
int Scratchpad::doublestr(void) {
u32 sz = cend - buf;
u8* savbuf;
if (sz + sz + 1 < ialloc) {
// don't need a realloc
::strncpy((char *)cend, (char *)buf, sz);
cend += sz;
*cend = '\000';
return(0);
}
savbuf = new u8 [ialloc + ialloc];
if (is_null(savbuf))
return(1);
::strncpy((char*)savbuf, (char*)buf, sz);
::strncpy((char*)savbuf + sz, (char*)buf, sz);
savbuf[sz + sz] = '\000';
delete [] buf;
ialloc += ialloc;
buf = savbuf;
bend = savbuf + ialloc;
cend = savbuf + sz + sz;
return(0);
}
int Scratchpad::strcat(const char* str, u32 len) {
u32 newc;
if (is_null(buf)) {
newc = smallest_pow2_greater_than(len);
if (realloc(newc))
return(1);
} else if ((const u8*)str == buf) {
return doublestr();
} else if (cend + len + 1 >= bend) {
newc = smallest_pow2_greater_than(ialloc + len + 1);
if (realloc(newc))
return(1);
}
::strcpy((char *)cend, str);
cend += len;
return(0);
}
int Scratchpad::strcat(const char* str) {
return strcat(str, strlen(str));
}
int Scratchpad::memcat(const u8* mem, u32 nbytes) {
u32 newc;
if (is_null(buf)) {
newc = smallest_pow2_greater_than(nbytes);
if (realloc(newc))
return(1);
memcpy(buf, mem, nbytes);
cend += nbytes;
return(0);
}
if (cend + nbytes >= bend) {
newc = smallest_pow2_greater_than(ialloc + nbytes);
if (realloc(newc))
return(1);
}
memcpy(cend, mem, nbytes);
cend += nbytes;
*cend = 0;
return(0);
}
int Scratchpad::concat(const Scratchpad* sp) {
if (nullptr == sp)
return(0); /* no-op */
return this->memcat(sp->buffer(), sp->length());
}
void Scratchpad::update_str(void) {
cend = buf + strlen((char *)buf);
}
int Scratchpad::realloc(u32 nbytes) {
u32 offs;
u8* newbuf;
if (nbytes <= ialloc)
return(0);
if (is_null(buf))
offs = 0;
else
offs = cend - buf;
if (nbytes < MIN_ALLOC)
nbytes = MIN_ALLOC;
newbuf = new u8 [nbytes];
NOT_NULL_OR_RETURN(newbuf, 1);
if (!is_null(buf))
memcpy(newbuf, buf, length());
newbuf[length()] = '\000';
if (!is_null(buf))
delete [] buf;
buf = newbuf;
cend = newbuf + offs;
bend = newbuf + nbytes;
ialloc = nbytes;
return(0);
}
int Scratchpad::compact(void) {
u32 nc = length();
u8 *newbuf;
if (0 == nc) {
cend = bend = nullptr;
ialloc = 0;
if (buf)
delete [] buf;
buf = nullptr;
return(0);
}
newbuf = new u8 [nc + 1];
memcpy(newbuf, buf, nc + 1);
delete [] buf;
buf = newbuf;
ialloc = nc + 1;
cend = newbuf + nc;
bend = newbuf + ialloc;
return(0);
}
int Scratchpad::strcpy(const char* str) {
const u32 needed = strlen(str) + 1;
/* this is a no-op if we already have enough memory */
if (realloc(needed) != 0)
return(1);
::strcpy((char *)buf, str);
cend = buf + needed - 1;
return(0);
}
int Scratchpad::strncpy(const char* str, u32 nchar) {
if (realloc(nchar + 1) != 0)
return(1);
::strncpy((char *)buf, str, nchar);
buf[nchar] = '\000';
cend = buf + strlen((char *)buf);
return(0);
}
int Scratchpad::getch(void) {
int ret;
int cpysize;
NOT_NULL_OR_RETURN(buf, -1);
if (cend == buf)
return(-1);
ret = buf[0];
cpysize = (cend - buf) - 1;
if (cpysize > 0)
memcpy(buf, buf + 1, cpysize);
--cend;
return(ret);
}
void Scratchpad::swap(Scratchpad* sp) {
u32 ia = ialloc;
u8* b = buf;
u8* ce = cend;
u8* be = bend;
if (is_null(sp))
return;
ialloc = sp->ialloc;
buf = sp->buf;
cend = sp->cend;
bend = sp->bend;
sp->ialloc = ia;
sp->buf = b;
sp->cend = ce;
sp->bend = be;
}
u8* Scratchpad::relinquish_buffer(void) {
u8* ret = buf;
ialloc = 0;
buf = cend = bend = nullptr;
return(ret);
}
void Scratchpad::clear() {
cend = buf;
}
void Scratchpad::give_static_buffer(u8* buf_in, u32 buflen) {
buf = buf_in;
ialloc = buflen;
cend = buf;
bend = buf + buflen;
}
/* end scratchpad.cpp */