-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidris_buffer.c
156 lines (134 loc) · 3.42 KB
/
idris_buffer.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
#include <string.h>
#include "idris_buffer.h"
typedef struct {
int size;
uint8_t data[0];
} Buffer;
void *idris_newBuffer(int bytes) {
Buffer *buf = malloc(sizeof(Buffer) + bytes * sizeof(uint8_t));
if (buf == NULL) {
return NULL;
}
buf->size = bytes;
memset(buf->data, 0, bytes);
return buf;
}
void idris_copyBuffer(void *from, int start, int len, void *to, int loc) {
Buffer *bfrom = from;
Buffer *bto = to;
if (loc >= 0 && loc + len <= bto->size) {
memcpy(bto->data + loc, bfrom->data + start, len);
}
}
int idris_getBufferSize(void *buffer) { return ((Buffer *)buffer)->size; }
void idris_setBufferByte(void *buffer, int loc, uint8_t byte) {
Buffer *b = buffer;
if (loc >= 0 && loc < b->size) {
b->data[loc] = byte;
}
}
void idris_setBufferInt(void *buffer, int loc, int val) {
Buffer *b = buffer;
if (loc >= 0 && loc + 3 < b->size) {
b->data[loc] = val & 0xff;
b->data[loc + 1] = (val >> 8) & 0xff;
b->data[loc + 2] = (val >> 16) & 0xff;
b->data[loc + 3] = (val >> 24) & 0xff;
}
}
void idris_setBufferDouble(void *buffer, int loc, double val) {
Buffer *b = buffer;
// I am not proud of this
if (loc >= 0 && loc + sizeof(double) <= b->size) {
unsigned char *c = (unsigned char *)(&val);
int i;
for (i = 0; i < sizeof(double); ++i) {
b->data[loc + i] = c[i];
}
}
}
void idris_setBufferString(void *buffer, int loc, char *str) {
Buffer *b = buffer;
int len = strlen(str);
if (loc >= 0 && loc + len <= b->size) {
memcpy((b->data) + loc, str, len);
}
}
uint8_t idris_getBufferByte(void *buffer, int loc) {
Buffer *b = buffer;
if (loc >= 0 && loc < b->size) {
return b->data[loc];
} else {
return 0;
}
}
int idris_getBufferInt(void *buffer, int loc) {
Buffer *b = buffer;
if (loc >= 0 && loc + 3 < b->size) {
return b->data[loc] + (b->data[loc + 1] << 8) + (b->data[loc + 2] << 16) +
(b->data[loc + 3] << 24);
} else {
return 0;
}
}
double idris_getBufferDouble(void *buffer, int loc) {
Buffer *b = buffer;
double d;
// I am even less proud of this
unsigned char *c = (unsigned char *)(&d);
if (loc >= 0 && loc + sizeof(double) <= b->size) {
int i;
for (i = 0; i < sizeof(double); ++i) {
c[i] = b->data[loc + i];
}
return d;
} else {
return 0;
}
}
int idris_readBufferStr(char *from, void *buffer, int loc, int max) {
FILE *h = fopen(from, "r");
Buffer *b = buffer;
size_t len;
if (loc >= 0 && loc < b->size) {
if (loc + max > b->size) {
max = b->size - loc;
}
len = fread((b->data) + loc, sizeof(uint8_t), (size_t)max, h);
fclose(h);
return len;
} else {
fclose(h);
return 0;
}
}
void *_fileOpen(char *name, char *mode) {
#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
FILE *f = win32_u8fopen(name, mode);
#else
FILE *f = fopen(name, mode);
#endif
return (void *)f;
}
int idris_readBuffer(FILE *h, void *buffer, int loc, int max) {
Buffer *b = buffer;
size_t len;
if (loc >= 0 && loc < b->size) {
if (loc + max > b->size) {
max = b->size - loc;
}
len = fread((b->data) + loc, sizeof(uint8_t), (size_t)max, h);
return len;
} else {
return 0;
}
}
void idris_writeBuffer(FILE *h, void *buffer, int loc, int len) {
Buffer *b = buffer;
if (loc >= 0 && loc < b->size) {
if (loc + len > b->size) {
len = b->size - loc;
}
fwrite((b->data) + loc, sizeof(uint8_t), len, h);
}
}