-
Notifications
You must be signed in to change notification settings - Fork 3
/
memorystream.c
92 lines (68 loc) · 1.97 KB
/
memorystream.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
#include <string.h>
#include "memorystream.h"
void memorystream_init(memorystream *pMS, void* data, size_t datalen) {
pMS->data = data;
pMS->len = datalen;
pMS->pos = data;
}
size_t memorystream_bytesavailable(memorystream *pMS) {
size_t bytesAvailable = pMS->len - (pMS->pos - (uint8_t*)pMS->data);
return bytesAvailable;
}
long memorystream_getposition(memorystream *pMS) {
return (pMS->pos - (uint8_t*)pMS->data);
}
void memorystream_setposition(memorystream *pMS, long position) {
pMS->pos = ((uint8_t*)pMS->data) + position;
}
void* memorystream_getpointer(memorystream *pMS) {
return pMS->pos;
}
bool memorystream_write8(memorystream *pMS, uint8_t val) {
if(memorystream_bytesavailable(pMS) < sizeof(uint8_t)) {
return false;
}
*pMS->pos = val;
pMS->pos++;
return true;
}
bool memorystream_write16(memorystream *pMS, uint16_t val) {
if(memorystream_bytesavailable(pMS) < sizeof(uint16_t)) {
return false;
}
*((uint16_t*)pMS->pos) = val;
pMS->pos += sizeof(uint16_t);
return true;
}
bool memorystream_writebytes(memorystream *pMS, void* data, size_t len) {
if(memorystream_bytesavailable(pMS) < len) {
return false;
}
memcpy(pMS->pos, data, len);
pMS->pos += len;
return true;
}
bool memorystream_read8(memorystream *pMS, uint8_t *val) {
if(memorystream_bytesavailable(pMS) < sizeof(uint8_t)) {
return false;
}
*val = *((uint8_t*)pMS->pos);
pMS->pos += sizeof(uint8_t);
return true;
}
bool memorystream_read16(memorystream *pMS, uint16_t *val) {
if(memorystream_bytesavailable(pMS) < sizeof(uint16_t)) {
return false;
}
*val = *((uint16_t*)pMS->pos);
pMS->pos += sizeof(uint16_t);
return true;
}
bool memorystream_readbytes(memorystream *pMS, void* data, size_t len) {
if(memorystream_bytesavailable(pMS) < len) {
return false;
}
memcpy(data, pMS->pos, len);
pMS->pos += len;
return true;
}