-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary.cpp
396 lines (297 loc) · 9.47 KB
/
Binary.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/******************************************************************************/
/* */
/* Copyright (c) 2010, 2014 Sylwester Wysocki <sw143@wp.pl> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a */
/* copy of this software and associated documentation files (the "Software"), */
/* to deal in the Software without restriction, including without limitation */
/* the rights to use, copy, modify, merge, publish, distribute, sublicense, */
/* and/or sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
/* */
/******************************************************************************/
//
// Purpose: Edit raw byte buffers stored in pure C-style buffer i.e.
// {buf, len} pair.
//
#include "Str.h"
namespace Tegenaria
{
//
// ----------------------------------------------------------------------------
//
// Pop data from C-style {buf, len} buffer
// All below functions do NOT destroy input buffer.
//
// ----------------------------------------------------------------------------
//
//
// Pop <rawSize> bytes from begin of buffer.
//
// raw - buffer, where to write popped data (OUT).
// rawSize - how much bytes to pop (IN).
// it - pointer inside raw buffer, where to read data from (IN/OUT).
// bytesLeft - number of bytes left in buffer (IN/OUT).
//
// RETURNS: 0 if all bytes popped,
// -1 if error.
//
int StrPopRaw(void *raw, int rawSize, char **it, int *bytesLeft)
{
DBG_ENTER3("StrPopRaw");
int exitCode = -1;
//
// Check args.
//
FAILEX(it == NULL, "ERROR: 'it' cannot be NULL in StrPopRaw.");
FAILEX(*it == NULL, "ERROR: '*it' cannot be NULL in StrPopRaw.");
FAILEX(raw == NULL, "ERROR: 'raw' cannot be NULL in StrPopRaw.");
FAILEX(bytesLeft == NULL, "ERROR: 'bytesLeft' cannot be NULL in StrPopRaw.");
FAILEX(rawSize > *bytesLeft, "ERROR: Buffer too small in StrPopRaw.");
//
// Pop raw bytes from buffer.
//
memcpy(raw, *it, rawSize);
DEBUG3("StrPopRaw : Popped [%d] bytes from buffer.\n", rawSize);
//
// Move pointer by rawSize bytes.
//
(*it) += rawSize;
(*bytesLeft) -= rawSize;
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("StrPopRaw");
return exitCode;
}
//
// Pop QWORD from begin of string.
//
// value - buffer, where to write popped QWORD (OUT).
// it - pointer inside raw buffer, where to read data from (IN/OUT).
// bytesLeft - number of bytes left in buffer (IN/OUT).
// flags - set to STR_BIG_ENDIAN or STR_LITTLE_ENDIAN (IN).
//
// RETURNS: 0 if all 8 bytes popped,
// -1 if error.
//
int StrPopQword(uint64_t *value, char **it, int *bytesLeft, int flags)
{
DBG_ENTER3("StrPopQword");
int exitCode = -1;
//
// Check args.
//
FAILEX(it == NULL, "ERROR: 'it' cannot be NULL in StrPopQword.");
FAILEX(*it == NULL, "ERROR: '*it' cannot be NULL in StrPopQword.");
FAILEX(value == NULL, "ERROR: Null pointer passed to StrPopQword.");
FAILEX(bytesLeft == NULL, "ERROR: 'bytesLeft' cannot be NULL in StrPopQword.");
FAILEX(*bytesLeft < 8, "ERROR: Buffer too small in StrPopQword.");
//
// Pop big endian qword (motorola 68k like).
//
if (flags & STR_BIG_ENDIAN)
{
uint8_t *dst = (uint8_t *) value;
uint8_t *src = (uint8_t *) *it;
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
DEBUG3("StrPopQword : Popped big endian [0x%"PRIx64"].\n", *value);
}
//
// Pop little endian dword (intel 86 like).
//
else
{
*value = *(uint64_t *)(*it);
DEBUG3("StrPopQword : Popped little endian [0x%"PRIx64"].\n", *value);
}
//
// Move pointer 8 bytes.
//
(*it) += 8;
(*bytesLeft) -= 8;
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("StrPopQword");
return exitCode;
}
//
// Pop DWORD from begin of string.
//
// value - buffer, where to write popped DWORD (OUT).
// it - pointer inside raw buffer, where to read data from (IN/OUT).
// bytesLeft - number of bytes left in buffer (IN/OUT).
// flags - set to STR_BIG_ENDIAN or STR_LITTLE_ENDIAN (IN).
//
// RETURNS: 0 if all 4 bytes popped,
// -1 if error.
//
int StrPopDword(uint32_t *value, char **it, int *bytesLeft, int flags)
{
DBG_ENTER3("StrPopDword");
int exitCode = -1;
//
// Check args.
//
FAILEX(it == NULL, "ERROR: 'it' cannot be NULL in StrPopDword.");
FAILEX(*it == NULL, "ERROR: '*it' cannot be NULL in StrPopDword.");
FAILEX(bytesLeft == NULL, "ERROR: 'bytesLeft' cannot be NULL in StrPopDword.");
FAILEX(value == NULL, "ERROR: Null pointer passed to StrPopDword.");
FAILEX(*bytesLeft < 4, "ERROR: Buffer too small in StrPopDword.");
//
// Pop big endian dword (motorola 68k like).
//
if (flags & STR_BIG_ENDIAN)
{
uint8_t *dst = (uint8_t *) value;
uint8_t *src = (uint8_t *) *it;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
DEBUG3("StrPopDword : Popped big endian [0x%x].\n", *value);
}
//
// Pop little endian dword (intel 86 like).
//
else
{
*value = *(uint32_t *)(*it);
DEBUG3("StrPopDword : Popped little endian [0x%x].\n", *value);
}
//
// Move pointer 4 bytes.
//
(*it) += 4;
(*bytesLeft) -= 4;
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("StrPopDword");
return exitCode;
}
//
// Pop one byte from begin of string.
//
// value - buffer, where to write popped byte (OUT).
// it - pointer inside raw buffer, where to read data from (IN/OUT).
// bytesLeft - number of bytes left in buffer (IN/OUT).
//
// RETURNS: 0 if byte popped,
// -1 if error.
//
int StrPopByte(uint8_t *value, char **it, int *bytesLeft)
{
DBG_ENTER3("StrPopByte");
int exitCode = -1;
//
// Check args.
//
FAILEX(it == NULL, "ERROR: 'it' cannot be NULL in StrPopDword.");
FAILEX(*it == NULL, "ERROR: '*it' cannot be NULL in StrPopDword.");
FAILEX(bytesLeft == NULL, "ERROR: 'bytseLeft' cannot be NULL in StrPopDword.");
FAILEX(value == NULL, "ERROR: 'value' cannot be NULL in StrPopDword.");
FAILEX(*bytesLeft < 1, "ERROR: Buffer too small in StrPopDword.");
//
// Pop byte from buffer.
//
*value = **it;
DEBUG3("StrPopByte : Popped byte [0x%x].\n", *value);
//
// Move pointer one byte.
//
(*it) ++;
(*bytesLeft) --;
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("StrPopByte");
return exitCode;
}
//
// Pop C string from buffer.
//
// str - pointer to begin of string (OUT).
// len - length of string in bytes, can be NULL if not needed (OUT/OPT).
// it - pointer inside raw buffer, where to read data from (IN/OUT).
// bytesLeft - number of bytes left in buffer (IN/OUT).
//
// RETURNS: 0 if byte popped,
// -1 if error.
//
int StrPopString(const char **str, int *strLen, char **it, int *bytesLeft)
{
DBG_ENTER3("StrPopString");
int exitCode = -1;
uint32_t len = 0;
//
// Check args.
//
FAILEX(str == NULL, "ERROR: 'str' cannot be NULL in StrPopString.\n");
//
// Pop size header.
//
*str = "";
FAIL(StrPopDword(&len, it, bytesLeft));
DEBUG3("StrPopString : Popped length [%d].\n", len);
FAILEX(*bytesLeft < len, "ERROR: Buffer too small in StrPopString.\n");
//
// Move pointer by <len> bytes.
//
*str = *it;
(*it) += len;
(*bytesLeft) -= len;
//
// Make sure there is zero terminator at the end of string.
//
if (len > 0)
{
(*it)[-1] = 0;
}
else
{
*str = "";
}
//
// Give length to user if needed.
//
if (strLen)
{
*strLen = len;
}
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("StrPopString");
return exitCode;
}
} /* namespace Tegenaria */