This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblob.c
265 lines (200 loc) · 5.16 KB
/
blob.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
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "object.h"
pic_value
pic_blob_value(pic_state *pic, const unsigned char *buf, int len)
{
struct blob *bv;
bv = (struct blob *)pic_obj_alloc(pic, PIC_TYPE_BLOB);
bv->data = pic_malloc(pic, len);
bv->len = len;
if (buf) {
memcpy(bv->data, buf, len);
}
return obj_value(pic, bv);
}
unsigned char *
pic_blob(pic_state *pic, pic_value blob, int *len)
{
struct blob *bv = blob_ptr(pic, blob);
if (len) {
*len = bv->len;
}
return bv->data;
}
static pic_value
pic_blob_bytevector_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_blob_p(pic, v));
}
static pic_value
pic_blob_bytevector(pic_state *pic)
{
pic_value *argv, blob;
int argc, i;
unsigned char *data;
pic_get_args(pic, "*", &argc, &argv);
blob = pic_blob_value(pic, 0, argc);
data = pic_blob(pic, blob, NULL);
for (i = 0; i < argc; ++i) {
TYPE_CHECK(pic, argv[i], int);
if (pic_int(pic, argv[i]) < 0 || pic_int(pic, argv[i]) > 255) {
pic_error(pic, "byte out of range", 0);
}
*data++ = (unsigned char)pic_int(pic, argv[i]);
}
return blob;
}
static pic_value
pic_blob_make_bytevector(pic_state *pic)
{
pic_value blob;
int k, b = 0;
pic_get_args(pic, "i|i", &k, &b);
if (b < 0 || b > 255)
pic_error(pic, "byte out of range", 0);
if (k < 0) {
pic_error(pic, "make-bytevector: negative length given", 1, pic_int_value(pic, k));
}
blob = pic_blob_value(pic, 0, k);
memset(pic_blob(pic, blob, NULL), (unsigned char)b, k);
return blob;
}
static pic_value
pic_blob_bytevector_length(pic_state *pic)
{
int len;
pic_get_args(pic, "b", NULL, &len);
return pic_int_value(pic, len);
}
static pic_value
pic_blob_bytevector_u8_ref(pic_state *pic)
{
unsigned char *buf;
int len, k;
pic_get_args(pic, "bi", &buf, &len, &k);
VALID_INDEX(pic, len, k);
return pic_int_value(pic, buf[k]);
}
static pic_value
pic_blob_bytevector_u8_set(pic_state *pic)
{
unsigned char *buf;
int len, k, v;
pic_get_args(pic, "bii", &buf, &len, &k, &v);
if (v < 0 || v > 255)
pic_error(pic, "byte out of range", 0);
VALID_INDEX(pic, len, k);
buf[k] = (unsigned char)v;
return pic_undef_value(pic);
}
static pic_value
pic_blob_bytevector_copy_i(pic_state *pic)
{
unsigned char *to, *from;
int n, at, start, end, tolen, fromlen;
n = pic_get_args(pic, "bib|ii", &to, &tolen, &at, &from, &fromlen, &start, &end);
switch (n) {
case 3:
start = 0;
case 4:
end = fromlen;
}
VALID_ATRANGE(pic, tolen, at, fromlen, start, end);
memmove(to + at, from + start, end - start);
return pic_undef_value(pic);
}
static pic_value
pic_blob_bytevector_copy(pic_state *pic)
{
unsigned char *buf;
int n, start, end, len;
n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end);
switch (n) {
case 1:
start = 0;
case 2:
end = len;
}
VALID_RANGE(pic, len, start, end);
return pic_blob_value(pic, buf + start, end - start);
}
static pic_value
pic_blob_bytevector_append(pic_state *pic)
{
int argc, i, l, len;
unsigned char *buf, *dst;
pic_value *argv, blob;
pic_get_args(pic, "*", &argc, &argv);
len = 0;
for (i = 0; i < argc; ++i) {
TYPE_CHECK(pic, argv[i], blob);
pic_blob(pic, argv[i], &l);
len += l;
}
blob = pic_blob_value(pic, NULL, len);
dst = pic_blob(pic, blob, NULL);
len = 0;
for (i = 0; i < argc; ++i) {
buf = pic_blob(pic, argv[i], &l);
memcpy(dst + len, buf, l);
len += l;
}
return blob;
}
static pic_value
pic_blob_list_to_bytevector(pic_state *pic)
{
pic_value blob;
unsigned char *data;
pic_value list, e, it;
pic_get_args(pic, "o", &list);
blob = pic_blob_value(pic, 0, pic_length(pic, list));
data = pic_blob(pic, blob, NULL);
pic_for_each (e, list, it) {
TYPE_CHECK(pic, e, int);
if (pic_int(pic, e) < 0 || pic_int(pic, e) > 255)
pic_error(pic, "byte out of range", 0);
*data++ = (unsigned char)pic_int(pic, e);
}
return blob;
}
static pic_value
pic_blob_bytevector_to_list(pic_state *pic)
{
pic_value list;
unsigned char *buf;
int n, len, start, end, i;
n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end);
switch (n) {
case 1:
start = 0;
case 2:
end = len;
}
VALID_RANGE(pic, len, start, end);
list = pic_nil_value(pic);
for (i = start; i < end; ++i) {
pic_push(pic, pic_int_value(pic, buf[i]), list);
}
return pic_reverse(pic, list);
}
void
pic_init_blob(pic_state *pic)
{
pic_defun(pic, "bytevector?", pic_blob_bytevector_p);
pic_defun(pic, "bytevector", pic_blob_bytevector);
pic_defun(pic, "make-bytevector", pic_blob_make_bytevector);
pic_defun(pic, "bytevector-length", pic_blob_bytevector_length);
pic_defun(pic, "bytevector-u8-ref", pic_blob_bytevector_u8_ref);
pic_defun(pic, "bytevector-u8-set!", pic_blob_bytevector_u8_set);
pic_defun(pic, "bytevector-copy!", pic_blob_bytevector_copy_i);
pic_defun(pic, "bytevector-copy", pic_blob_bytevector_copy);
pic_defun(pic, "bytevector-append", pic_blob_bytevector_append);
pic_defun(pic, "bytevector->list", pic_blob_bytevector_to_list);
pic_defun(pic, "list->bytevector", pic_blob_list_to_bytevector);
}