/
binary-engine.cc
298 lines (226 loc) · 7.27 KB
/
binary-engine.cc
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
#include <narwhal.h>
#include <stdlib.h>
#include <string.h>
#include <iconv.h>
#include <errno.h>
#include <binary-engine.h>
DESTRUCTOR(Bytes_finalize)
{
GET_INTERNAL(BytesPrivate *, data, object);
DEBUG("freeing bytes=[%p]\n", data);
if (data) {
if (data->buffer)
free(data->buffer);
data->buffer = NULL;
free(data);
}
}
END
#ifdef NARWHAL_JSC
JSClassRef Bytes_class(JSContextRef _context)
{
static JSClassRef jsClass;
if (!jsClass)
{
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.finalize = Bytes_finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
JSObjectRef Bytes_new(JSContextRef _context, JSValueRef *_exception, char* buffer, int length)
{
BytesPrivate *data = (BytesPrivate*)malloc(sizeof(BytesPrivate));
if (!data) return NULL;
data->length = length;
data->buffer = buffer;
return JSObjectMake(_context, Bytes_class(_context), data);
}
#elif NARWHAL_V8
OBJECT(Bytes_new, 2, char* buffer, size_t length)
{
BytesPrivate *data = (BytesPrivate*)malloc(sizeof(BytesPrivate));
data->length = length;
data->buffer = buffer;
SET_INTERNAL(self, data);
Persistent<Object> _weak_handle = Persistent<Object>::New(self);
_weak_handle.MakeWeak(NULL, &Bytes_finalize);
return self;
}
END
#endif
FUNCTION(BINARY_B_ALLOC, ARG_INT(length))
{
char *buffer = (char*)calloc(length, sizeof(char));
if (!buffer)
THROW("B_ALLOC: Couldn't alloc buffer");
return CALL(Bytes_new, buffer, length);
}
END
FUNCTION(BINARY_B_LENGTH, ARG_OBJ(bytes))
{
GET_INTERNAL(BytesPrivate*, byte_data, bytes);
return JS_int(byte_data->length);
}
END
FUNCTION(BINARY_B_FILL, ARG_OBJ(bytes), ARG_INT(from), ARG_INT(to), ARG_INT(value))
{
GET_INTERNAL(BytesPrivate*, byte_data, bytes);
if (!byte_data->buffer)
THROW("B_FILL: NULL buffer");
if (from > to || from < 0 || to >= byte_data->length)
THROW("B_FILL: tried to fill beyond bounds");
memset(byte_data->buffer + from, value, to - from);
return JS_undefined;
}
END
FUNCTION(BINARY_B_COPY, ARG_OBJ(src), ARG_INT(srcOffset), ARG_OBJ(dst), ARG_INT(dstOffset), ARG_INT(length))
{
GET_INTERNAL(BytesPrivate*, src_data, src);
GET_INTERNAL(BytesPrivate*, dst_data, dst);
if (srcOffset + length > src_data->length || dstOffset + length > dst_data->length)
THROW("B_COPY: tried to copy beyond bounds");
memcpy(dst_data->buffer + dstOffset, src_data->buffer + srcOffset, length);
return JS_undefined;
}
END
FUNCTION(BINARY_B_GET, ARG_OBJ(bytes), ARG_INT(index))
{
GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
if (index >= bytes_data->length)
THROW("B_GET: tried get beyond bounds");
unsigned char b = bytes_data->buffer[index];
return JS_int((int)b);
}
END
FUNCTION(BINARY_B_SET, ARG_OBJ(bytes), ARG_INT(index), ARG_INT(value))
{
GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
if (index >= bytes_data->length)
THROW("B_SET: tried set beyond bounds");
if (value < 0 || value >= 256)
THROW("B_SET: tried to set out of byte range");
bytes_data->buffer[index] = (unsigned char)value;
return JS_undefined;
}
END
FUNCTION(BINARY_B_DECODE_DEFAULT, ARG_OBJ(bytes), ARG_INT(offset), ARG_INT(length))
{
GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
return JS_str_utf8((char *)(bytes_data->buffer + offset), length);
}
END
FUNCTION(BINARY_B_ENCODE_DEFAULT, ARG_UTF8(string))
{
int length = strlen(string);
char* buffer = (char*)calloc(length, sizeof(char));
if (!buffer)
THROW("B_ENCODE_DEFAULT: Couldn't alloc buffer");
memcpy(buffer, string, length);
return CALL(Bytes_new, buffer, length);
}
END
int transcode(char *src, size_t srcLength, char **dstOut, size_t *dstLengthOut, const char *srcCodec, const char *dstCodec)
{
/*
printf("src=%p srcLength=%d srcCodec=%s dstCodec=%s\n", src, srcLength, srcCodec, dstCodec);
for (size_t i = 0; i < srcLength; i++)
printf("%02x ",(unsigned char)src[i]);
printf("\n");
//*/
int dst_length = srcLength*10; // FIXME!!!!!!
char *dst = (char*)calloc(dst_length, 1);
if (!dst) {
perror("transcode");
return 0;
}
iconv_t cd = iconv_open(dstCodec, srcCodec);
if (cd == (iconv_t)-1) {
perror("transcode");
return 0;
}
char *src_buf = src, *dst_buf = dst;
#ifdef ICONV_REQUIRES_CONST_POINTER
const char **src_buf_ptr = &src_buf;
#else
char **src_buf_ptr = &src_buf;
#endif
size_t src_bytes_left = (size_t)srcLength, dst_bytes_left = (size_t)dst_length, converted=0;
while (dst_bytes_left > 0)
{
if (src_bytes_left == 0)
break;
if ((converted = iconv(cd, src_buf_ptr, &src_bytes_left, &dst_buf, &dst_bytes_left)) == (size_t)-1)
{
if (errno != EINVAL)
{
perror("transcode");
return 0;
}
}
}
if (dst_bytes_left >= sizeof (wchar_t))
*((wchar_t *) dst_buf) = L'\0';
if (iconv_close(cd)) {
perror("transcode");
return 0;
}
if (src_bytes_left > 0) {
perror("transcode");
return 0;
}
*dstOut = dst;
*dstLengthOut = (dst_length - dst_bytes_left);
return 1;
}
FUNCTION(BINARY_B_DECODE, ARG_OBJ(bytes), ARG_INT(offset), ARG_INT(srcLength), ARG_UTF8(codec))
{
GET_INTERNAL(BytesPrivate*, src_data, bytes);
char *dst;
size_t dstLength;
if (!transcode((char *)(src_data->buffer + offset), srcLength, &dst, &dstLength, codec, UTF_16_ENCODING))
THROW("B_DECODE: iconv error");
NWString string = JS_str_utf16(dst, dstLength);
free(dst);
return string;
}
END
FUNCTION(BINARY_B_ENCODE, ARG_STR(string), ARG_UTF8(codec))
{
char *src, *dst;
size_t srcLength, dstLength;
if (!GET_UTF16(string, &src, &srcLength))
THROW("B_ENCODE: GET_UTF16 error");
if (!transcode(src, srcLength, &dst, &dstLength, UTF_16_ENCODING, codec))
THROW("B_ENCODE: iconv error");
free(src);
return CALL(Bytes_new, dst, dstLength);
}
END
FUNCTION(BINARY_B_TRANSCODE, ARG_OBJ(srcBytes), ARG_INT(srcOffset), ARG_INT(srcLength), ARG_UTF8(srcCodec), ARG_UTF8(dstCodec))
{
GET_INTERNAL(BytesPrivate*, src_data, srcBytes);
char *dst;
size_t dstLength;
char *src = (char *)(src_data->buffer + srcOffset);
if (!transcode(src, srcLength, &dst, &dstLength, srcCodec, dstCodec))
THROW("B_TRANSCODE: iconv error");
return CALL(Bytes_new, dst, dstLength);
}
END
NARWHAL_MODULE(binary_engine)
{
EXPORTS("B_LENGTH", JS_fn(BINARY_B_LENGTH));
EXPORTS("B_ALLOC", JS_fn(BINARY_B_ALLOC));
EXPORTS("B_FILL", JS_fn(BINARY_B_FILL));
EXPORTS("B_COPY", JS_fn(BINARY_B_COPY));
EXPORTS("B_GET", JS_fn(BINARY_B_GET));
EXPORTS("B_SET", JS_fn(BINARY_B_SET));
EXPORTS("B_DECODE", JS_fn(BINARY_B_DECODE));
EXPORTS("B_ENCODE", JS_fn(BINARY_B_ENCODE));
EXPORTS("B_DECODE_DEFAULT", JS_fn(BINARY_B_DECODE_DEFAULT));
EXPORTS("B_ENCODE_DEFAULT", JS_fn(BINARY_B_ENCODE_DEFAULT));
EXPORTS("B_TRANSCODE", JS_fn(BINARY_B_TRANSCODE));
EXPORTS("DEFAULT_CODEC", JS_str_utf8("UTF-8", strlen("UTF-8")));
}
END_NARWHAL_MODULE