-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathByteArray.c
473 lines (417 loc) · 12.8 KB
/
ByteArray.c
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* ByteArray (C) 2012 rofl0r
*
* licensed under the LGPL 2.1+ */
#include "ByteArray.h"
#include "debug.h"
#include "endianness.h"
#ifndef _WIN32
#define O_BINARY 0
#endif
#include <errno.h>
#include <stdlib.h>
#ifndef NO_MMAN /* lame OS like winblows */
#include <sys/mman.h>
#define BLOWS 0
#else
#define mmap(...) (void*)0xffffffff
#define MAP_FAILED (void*)0xffffffff
#define BLOWS 1
#endif
void ByteArray_defaults(struct ByteArray* self) {
memset(self, 0, sizeof(*self));
}
void ByteArray_ctor(struct ByteArray* self) {
ByteArray_defaults(self);
// original constructor code goes here
self->readMultiByte = ByteArray_readMultiByte;
self->readByte = ByteArray_readByte;
self->readUnsignedByte = ByteArray_readUnsignedByte;
self->readShort = ByteArray_readShort;
self->readUnsignedShort = ByteArray_readUnsignedShort;
self->readInt = ByteArray_readInt;
self->readUnsignedInt = ByteArray_readUnsignedInt;
self->readUnsignedLongLong = ByteArray_readUnsignedLongLong;
self->readBytes = ByteArray_readBytes;
self->writeInt = ByteArray_writeInt;
self->writeUnsignedInt = ByteArray_writeUnsignedInt;
self->writeShort = ByteArray_writeShort;
self->writeUnsignedShort = ByteArray_writeUnsignedShort;
self->writeByte = ByteArray_writeByte;
self->writeUnsignedByte = ByteArray_writeUnsignedByte;;
self->writeMem = ByteArray_writeMem;
self->writeUTFBytes = ByteArray_writeUTFBytes;
self->writeBytes = ByteArray_writeBytes;
self->writeFloat = ByteArray_writeFloat;
self->set_position = ByteArray_set_position;
self->set_position_rel = ByteArray_set_position_rel;
self->get_position = ByteArray_get_position;
self->bytesAvailable = ByteArray_bytesAvailable;
self->sys_endian = ENDIANNESS_BE ? BAE_BIG : BAE_LITTLE;
}
struct ByteArray* ByteArray_new(void) {
struct ByteArray* self = (struct ByteArray*) malloc(sizeof(*self));
if(self) ByteArray_ctor(self);
return self;
}
void ByteArray_set_endian(struct ByteArray* self, enum ByteArray_Endianess endian) {
self->endian = endian;
}
void ByteArray_set_flags(struct ByteArray *self, int flags) {
self->flags = flags;
}
enum ByteArray_Endianess ByteArray_get_endian(struct ByteArray* self) {
return self->endian;
}
// a real byte array clears the mem and resets
// "len" and pos to 0
// where len is equivalent to the bytes written into it
//void* mem_getptr(MG* mem, size_t offset, size_t byteswanted);
void ByteArray_clear(struct ByteArray* self) {
fprintf(stderr, "clear called\n");
assert_op(self->type, ==, BAT_MEMSTREAM);
void *p = mem_getptr(&self->source_mem, 0, self->size);
if(p) memset(p, 0, self->size);
}
void ByteArray_close(struct ByteArray* self) {
assert_op(self->type, ==, BAT_MEMSTREAM);
mem_free(&self->source_mem);
}
off_t ByteArray_get_position(struct ByteArray* self) {
return self->pos;
}
static void seek_error() {
perror("seek error!\n");
assert_dbg(0);
}
static void neg_off() {
fprintf(stderr, "negative seek attempted\n");
assert_dbg(0);
}
static void oob(const char *fn, off_t want, off_t have) {
fprintf(stderr, "%s: oob access attempted (want: %jd, have %jd)\n", fn ? fn : "<unknown>", (intmax_t) want, (intmax_t) have);
fflush(stderr);
assert_dbg(0);
}
void ByteArray_set_length(struct ByteArray* self, off_t len) {
if(len > self->size) {
oob(self->filename, len, self->size);
return;
}
self->size = len;
}
off_t ByteArray_get_length(struct ByteArray* self) {
return self->size;
}
int ByteArray_set_position_rel(struct ByteArray* self, int rel) {
if((int) self->pos + rel < 0) {
neg_off();
rel = -self->pos;
}
return ByteArray_set_position(self, self->pos + rel);
}
int ByteArray_set_position(struct ByteArray* self, off_t pos) {
if(pos == self->pos) return 1;
if(pos > self->size) {
oob(self->filename, pos, self->size);
return 0;
}
if(self->type == BAT_FILESTREAM) {
off_t ret = lseek(self->source_fd, pos, SEEK_SET);
if(ret == (off_t) -1) {
seek_error();
return 0;
}
}
self->pos = pos;
return 1;
}
static void read_error() {
perror("read error!\n");
assert_dbg(0);
}
static void read_error_short() {
perror("read error (short)!\n");
assert_dbg(0);
}
int ByteArray_open_file(struct ByteArray* self, const char* filename) {
struct stat st;
self->type = BAT_FILESTREAM;
self->pos = 0;
self->size = 0;
if(stat(filename, &st) == -1) return 0;
self->size = st.st_size;
self->filename = filename;
self->source_fd = open(filename, O_RDONLY|O_BINARY);
if (self->source_fd == -1) return 0;
void *addr = mmap(NULL, self->size, PROT_READ, MAP_PRIVATE, self->source_fd, 0);
if(addr == MAP_FAILED) {
if(!BLOWS)
fprintf(stderr, "mmap %s failed (%s) - fd %d, size %llu\n", filename, strerror(errno), self->source_fd, (long long) self->size);
#ifdef __POCC__
if(sizeof(off_t) < 8 && self->size < 0)
fprintf(stderr, "sorry, the PellesC compiled 32 bit windows binaries do not support files > 2GB at this point.\nplease compile from source in a linux environment, WSL or cygwin to access this file.\n");
#endif
return 1; // fall back to traditional non-mmaped BAT_FILESTREAM
}
return ByteArray_open_mem(self, addr, self->size);
}
void ByteArray_close_file(struct ByteArray *self) {
if(self->type == BAT_MEMSTREAM) {
munmap(self->source_mem.mem, self->size);
self->source_mem.mem = 0;
}
close(self->source_fd);
self->source_fd = -1;
}
int ByteArray_open_mem(struct ByteArray* self, char* data, size_t size) {
self->size = size;
self->type = BAT_MEMSTREAM;
mem_set(&self->source_mem, data, size, size);
return 1;
}
void* ByteArray_get_mem(struct ByteArray* self, size_t offset, size_t byteswanted) {
assert_dbg(self->type == BAT_MEMSTREAM);
return mem_getptr(&self->source_mem, offset, byteswanted);
}
ssize_t ByteArray_readMultiByte(struct ByteArray* self, char* buffer, size_t len) {
if(self->type == BAT_MEMSTREAM) {
assert_op((size_t) self->pos + len, <=, (size_t) self->size);
void *p = mem_getptr(&self->source_mem, self->pos, len);
if(p) memcpy(buffer, p, len);
else return -1;
} else {
ssize_t ret = read(self->source_fd, buffer, len);
if(ret == -1) {
read_error();
return -1;
} else if((size_t) ret != len) {
read_error_short();
self->pos += len;
return -1;
}
}
self->pos += len;
return len;
}
// write contents of self into dest
// if len == 0 all available bytes are used.
// self->pos is considered the start offset to use for self.
// the position in dest will not be advanced.
// the position in source will be advanced len bytes.
// returns the number of bytes written
off_t ByteArray_readBytes(struct ByteArray* self, struct ByteArray *dest, off_t start, off_t len) {
off_t left = self->size - self->pos;
if(len == 0) len = left;
else if(len > left) {
oob(self->filename, len, left);
len = left;
}
if(len == 0) return 0;
else if (len > start + dest->size) {
oob(self->filename, len, start + dest->size);
len = start + dest->size;
if(len == 0) return 0;
}
if(dest->type != BAT_MEMSTREAM) {
assert_dbg(0);
}
void *p = mem_getptr(&dest->source_mem, start, len);
if(p) self->readMultiByte(self, p, len);
else return 0;
return len;
}
off_t ByteArray_bytesAvailable(struct ByteArray* self) {
if(self->pos < self->size) return self->size - self->pos;
return 0;
}
unsigned long long ByteArray_readUnsignedLongLong(struct ByteArray* self) {
union {
unsigned long long intval;
unsigned char charval[sizeof(unsigned long long)];
} buf;
self->readMultiByte(self, (char*) buf.charval, 8);
if(self->endian != self->sys_endian) {
buf.intval = end_bswap64(buf.intval);
}
return buf.intval;
}
unsigned int ByteArray_readUnsignedInt(struct ByteArray* self) {
union {
unsigned int intval;
unsigned char charval[sizeof(unsigned int)];
} buf;
self->readMultiByte(self, (char*) buf.charval, 4);
if(self->endian != self->sys_endian) {
buf.intval = end_bswap32(buf.intval);
}
return buf.intval;
}
int ByteArray_readInt(struct ByteArray* self) {
union {
unsigned int intval;
unsigned char charval[sizeof(unsigned int)];
} buf;
self->readMultiByte(self, (char*) buf.charval, 4);
if(self->endian != self->sys_endian) {
buf.intval = end_bswap32(buf.intval);
}
return buf.intval;
}
unsigned short ByteArray_readUnsignedShort(struct ByteArray* self) {
union {
unsigned short intval;
unsigned char charval[sizeof(unsigned short)];
} buf;
self->readMultiByte(self, (char*) buf.charval, 2);
if(self->endian != self->sys_endian) {
buf.intval = end_bswap16(buf.intval);
}
return buf.intval;
}
short ByteArray_readShort(struct ByteArray* self) {
union {
unsigned short intval;
unsigned char charval[sizeof(unsigned short)];
} buf;
self->readMultiByte(self, (char*) buf.charval, 2);
if(self->endian != self->sys_endian) {
buf.intval = end_bswap16(buf.intval);
}
return buf.intval;
}
unsigned char ByteArray_readUnsignedByte(struct ByteArray* self) {
union {
unsigned char intval;
} buf;
self->readMultiByte(self, (char*) &buf.intval, 1);
return buf.intval;
}
signed char ByteArray_readByte(struct ByteArray* self) {
union {
signed char intval;
} buf;
self->readMultiByte(self, (char*) &buf.intval, 1);
return buf.intval;
}
/* equivalent to foo = self[x]; (pos stays unchanged) */
unsigned char ByteArray_getUnsignedByte(struct ByteArray* self, off_t index) {
//assert_op(self->type, ==, BAT_MEMSTREAM);
assert_op(index, <, self->size);
off_t save = self->pos;
unsigned char res;
ByteArray_set_position(self, index);
res = ByteArray_readUnsignedByte(self);
ByteArray_set_position(self, save);
return res;
}
/* equivalent to self[x] = what (pos stays unchanged) */
void ByteArray_setUnsignedByte(struct ByteArray* self, off_t index, unsigned char what) {
off_t save = self->pos;
if(ByteArray_set_position(self, index)) {
ByteArray_writeUnsignedByte(self, what);
self->pos = save;
}
}
off_t ByteArray_writeByte(struct ByteArray* self, signed char what) {
return ByteArray_writeMem(self, (unsigned char*) &what, 1);
}
off_t ByteArray_writeUnsignedByte(struct ByteArray* self, unsigned char what) {
return ByteArray_writeMem(self, (unsigned char*) &what, 1);
}
off_t ByteArray_writeShort(struct ByteArray* self, signed short what) {
union {
short intval;
unsigned char charval[sizeof(what)];
} u;
u.intval = what;
if(self->sys_endian != self->endian) {
u.intval = end_bswap16(u.intval);
}
return ByteArray_writeMem(self, u.charval, sizeof(what));
}
off_t ByteArray_writeUnsignedShort(struct ByteArray* self, unsigned short what) {
union {
unsigned short intval;
unsigned char charval[sizeof(what)];
} u;
u.intval = what;
if(self->sys_endian != self->endian) {
u.intval = end_bswap16(u.intval);
}
return ByteArray_writeMem(self, u.charval, sizeof(what));
}
off_t ByteArray_writeInt(struct ByteArray* self, signed int what) {
union {
int intval;
unsigned char charval[sizeof(what)];
} u;
u.intval = what;
if(self->sys_endian != self->endian) {
u.intval = end_bswap32(u.intval);
}
return ByteArray_writeMem(self, u.charval, sizeof(what));
}
off_t ByteArray_writeUnsignedInt(struct ByteArray* self, unsigned int what) {
union {
unsigned int intval;
unsigned char charval[sizeof(what)];
} u;
u.intval = what;
if(self->sys_endian != self->endian) {
u.intval = end_bswap32(u.intval);
}
return ByteArray_writeMem(self, u.charval, sizeof(what));
}
off_t ByteArray_writeMem(struct ByteArray* self, unsigned char* what, size_t len) {
if(self->type == BAT_FILESTREAM) {
fprintf(stderr, "tried to write to file!\n");
assert_dbg(0);
return 0;
}
if(!(self->flags & BAF_CANGROW) && (size_t) self->pos + len > (size_t) self->size) {
fprintf(stderr, "oob write attempted");
assert_dbg(0);
return 0;
}
if(mem_write(&self->source_mem, self->pos, what, len)) {
self->pos += len;
if(self->pos > self->size) self->size = self->pos; /* apparently CANGROW was used */
}
return len;
}
off_t ByteArray_writeUTFBytes(struct ByteArray* self, char* what) {
return ByteArray_writeMem(self, (unsigned char*) what, strlen(what));
}
// write contents of what into self
off_t ByteArray_writeBytes(struct ByteArray* self, struct ByteArray* what) {
if(what->type == BAT_FILESTREAM) {
fprintf(stderr, "tried to write from non-memory stream\n");
assert_dbg(0);
return 0;
} else {
unsigned char* p = mem_getptr(&what->source_mem, what->pos, what->size - what->pos);
if(p)
return ByteArray_writeMem(self, p, what->size - what->pos);
return -1;
}
}
off_t ByteArray_writeFloat(struct ByteArray* self, float what) {
union {
float floatval;
unsigned int intval;
unsigned char charval[sizeof(what)];
} u;
u.floatval = what;
if(self->sys_endian != self->endian) {
u.intval = end_bswap32(u.intval);
}
return ByteArray_writeMem(self, u.charval, sizeof(what));
}
void ByteArray_dump_to_stream(struct ByteArray* self, FILE *out) {
assert_op(self->type, ==, BAT_MEMSTREAM);
mem_write_stream(&self->source_mem, out);
}
void ByteArray_dump_to_file(struct ByteArray* self, char* filename) {
assert_op(self->type, ==, BAT_MEMSTREAM);
mem_write_file(&self->source_mem, filename);
}