forked from rsenn/qjs-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickjs-magic.c
416 lines (330 loc) · 11.7 KB
/
quickjs-magic.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
#include "defines.h"
#include "buffer-utils.h"
#include "utils.h"
#include <errno.h>
#include <string.h>
#include <magic.h>
/**
* \defgroup quickjs-magic quickjs-magic: libmagic MIME types
* @{
*/
VISIBLE JSClassID js_magic_class_id = 0;
VISIBLE JSValue magic_proto = {{0}, JS_TAG_UNDEFINED}, magic_ctor = {{0}, JS_TAG_UNDEFINED};
enum {
LIBMAGIC_ERROR = 0,
LIBMAGIC_ERRNO,
LIBMAGIC_DESCRIPTOR,
LIBMAGIC_FILE,
LIBMAGIC_BUFFER,
METHOD_GETFLAGS,
METHOD_SETFLAGS,
METHOD_CHECK,
METHOD_COMPILE,
METHOD_LIST,
METHOD_LOAD,
METHOD_GETPARAM,
METHOD_SETPARAM,
METHOD_VERSION,
};
static inline magic_t
js_magic_data(JSContext* ctx, JSValueConst value) {
return JS_GetOpaque(value, js_magic_class_id);
}
static int
js_magic_load(JSContext* ctx, magic_t cookie, int argc, JSValueConst argv[]) {
int n = 1;
if(JS_IsString(argv[0])) {
const char* str;
if((str = JS_ToCString(ctx, argv[0]))) {
magic_load(cookie, str);
JS_FreeCString(ctx, str);
}
} else {
InputBuffer input = js_input_chars(ctx, argv[0]);
if(argc > 1)
n += js_offset_length(ctx, input.size, argc - 1, argv + 1, &input.range);
void* buf[] = {input_buffer_data(&input), NULL};
size_t siz[] = {input_buffer_length(&input), 0};
magic_load_buffers(cookie, buf, siz, 1);
input_buffer_free(&input, ctx);
}
return n;
}
static JSValue
js_magic_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSValue obj = JS_UNDEFINED;
JSValue proto;
magic_t cookie;
int32_t flags = 0;
if(argc > 0 && JS_IsNumber(argv[0])) {
JS_ToInt32(ctx, &flags, argv[0]);
argc--;
argv++;
}
cookie = magic_open(flags);
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
if(!JS_IsObject(proto))
proto = magic_proto;
/* using new_target to get the prototype is necessary when the class is extended. */
obj = JS_NewObjectProtoClass(ctx, proto, js_magic_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
int n;
while(argc > 0 && (n = js_magic_load(ctx, cookie, argc, argv))) {
argc -= n;
argv += n;
}
JS_SetOpaque(obj, cookie);
return obj;
fail:
js_free(ctx, cookie);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue
js_magic_function(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
magic_t cookie;
const char* str = 0;
if(!(cookie = js_magic_data(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case LIBMAGIC_DESCRIPTOR: {
int32_t fd = -1;
JS_ToInt32(ctx, &fd, argv[0]);
str = magic_descriptor(cookie, fd);
break;
}
case LIBMAGIC_FILE: {
const char* filename = JS_IsNull(argv[0]) ? NULL : JS_ToCString(ctx, argv[0]);
str = magic_file(cookie, filename);
if(filename)
JS_FreeCString(ctx, filename);
break;
}
case LIBMAGIC_BUFFER: {
int n = 1;
InputBuffer input = js_input_chars(ctx, argv[0]);
if(argc > 1)
n += js_offset_length(ctx, input.size, argc - 1, argv + 1, &input.range);
str = magic_buffer(cookie, input_buffer_data(&input), input_buffer_length(&input));
input_buffer_free(&input, ctx);
break;
}
}
return str ? JS_NewString(ctx, str) : JS_ThrowInternalError(ctx, "libmagic error: %s", magic_error(cookie));
}
static JSValue
js_magic_method(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
magic_t cookie;
JSValue ret = JS_UNDEFINED;
if(!(cookie = js_magic_data(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case METHOD_GETFLAGS: {
ret = JS_NewInt32(ctx, magic_getflags(cookie));
break;
}
case METHOD_SETFLAGS: {
int32_t flags = -1;
JS_ToInt32(ctx, &flags, argv[0]);
ret = JS_NewInt32(ctx, magic_setflags(cookie, flags));
break;
}
case METHOD_CHECK: {
const char* filename = JS_IsNull(argv[0]) ? NULL : JS_ToCString(ctx, argv[0]);
if(magic_check(cookie, filename))
ret = JS_ThrowInternalError(ctx, "libmagic error: %s", magic_error(cookie));
if(filename)
JS_FreeCString(ctx, filename);
break;
}
case METHOD_COMPILE: {
const char* filename = JS_IsNull(argv[0]) ? NULL : JS_ToCString(ctx, argv[0]);
if(magic_compile(cookie, filename))
ret = JS_ThrowInternalError(ctx, "libmagic error: %s", magic_error(cookie));
if(filename)
JS_FreeCString(ctx, filename);
break;
}
case METHOD_LIST: {
const char* filename = JS_IsNull(argv[0]) ? NULL : JS_ToCString(ctx, argv[0]);
if(magic_list(cookie, filename))
ret = JS_ThrowInternalError(ctx, "libmagic error: %s", magic_error(cookie));
if(filename)
JS_FreeCString(ctx, filename);
break;
}
case METHOD_LOAD: {
int i = 0;
while(argc > 0) {
int n = js_magic_load(ctx, cookie, argc, argv);
if(n == 0)
break;
argc -= n;
argv += n;
i++;
}
ret = JS_NewInt32(ctx, i);
break;
}
case METHOD_GETPARAM: {
int32_t param = -1;
size_t value = 0;
JS_ToInt32(ctx, ¶m, argv[0]);
if(!magic_getparam(cookie, param, &value))
ret = JS_NewInt64(ctx, (int64_t)value);
break;
}
case METHOD_SETPARAM: {
int32_t param = -1;
int64_t value = -1;
size_t sz;
JS_ToInt32(ctx, ¶m, argv[0]);
JS_ToInt64(ctx, &value, argv[1]);
sz = value;
magic_setparam(cookie, param, &sz);
break;
}
}
return ret;
}
static JSValue
js_magic_get(JSContext* ctx, JSValueConst this_val, int magic) {
magic_t cookie;
JSValue ret = JS_UNDEFINED;
if(!(cookie = js_magic_data(ctx, this_val)))
return ret;
switch(magic) {
case LIBMAGIC_ERROR: {
ret = JS_NewString(ctx, magic_error(cookie));
break;
}
case LIBMAGIC_ERRNO: {
ret = JS_NewInt32(ctx, magic_errno(cookie));
break;
}
case METHOD_VERSION: {
ret = JS_NewInt32(ctx, magic_version());
break;
}
}
return ret;
}
static JSValue
js_magic_exec(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
magic_t cookie;
if(!(cookie = js_magic_data(ctx, this_val)))
return JS_EXCEPTION;
if(argc == 0)
return JS_ThrowInternalError(ctx, "Magic(arg)");
return js_magic_function(ctx, this_val, argc, argv, JS_IsNumber(argv[0]) ? LIBMAGIC_DESCRIPTOR : JS_IsString(argv[0]) ? LIBMAGIC_FILE : LIBMAGIC_BUFFER);
}
static JSValue
js_magic_call(JSContext* ctx, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst argv[], int flags) {
return js_magic_exec(ctx, func_obj, argc, argv);
}
static void
js_magic_finalizer(JSRuntime* rt, JSValue val) {
magic_t cookie;
if((cookie = JS_GetOpaque(val, js_magic_class_id))) {
JS_SetOpaque(val, NULL);
magic_close(cookie);
}
}
static JSClassDef js_magic_class = {.class_name = "Magic", .finalizer = js_magic_finalizer, .call = js_magic_call};
static const JSCFunctionListEntry js_magic_funcs[] = {
JS_CGETSET_MAGIC_DEF("error", js_magic_get, 0, LIBMAGIC_ERROR),
JS_CGETSET_MAGIC_DEF("errno", js_magic_get, 0, LIBMAGIC_ERRNO),
JS_CFUNC_MAGIC_DEF("descriptor", 1, js_magic_function, LIBMAGIC_DESCRIPTOR),
JS_CFUNC_MAGIC_DEF("file", 1, js_magic_function, LIBMAGIC_FILE),
JS_CFUNC_MAGIC_DEF("buffer", 1, js_magic_function, LIBMAGIC_BUFFER),
JS_CFUNC_MAGIC_DEF("getflags", 0, js_magic_method, METHOD_GETFLAGS),
JS_CFUNC_MAGIC_DEF("setflags", 1, js_magic_method, METHOD_SETFLAGS),
JS_CFUNC_MAGIC_DEF("check", 1, js_magic_method, METHOD_CHECK),
JS_CFUNC_MAGIC_DEF("compile", 1, js_magic_method, METHOD_COMPILE),
JS_CFUNC_MAGIC_DEF("list", 1, js_magic_method, METHOD_LIST),
JS_CFUNC_MAGIC_DEF("load", 0, js_magic_method, METHOD_LOAD),
JS_CFUNC_MAGIC_DEF("getparam", 2, js_magic_method, METHOD_GETPARAM),
JS_CFUNC_MAGIC_DEF("setparam", 2, js_magic_method, METHOD_SETPARAM),
JS_CGETSET_MAGIC_DEF("version", js_magic_get, 0, METHOD_VERSION),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Magic", JS_PROP_CONFIGURABLE),
};
static const JSCFunctionListEntry js_magic_static[] = {
JS_PROP_INT32_DEF("NONE", MAGIC_NONE, 0),
JS_PROP_INT32_DEF("DEBUG", MAGIC_DEBUG, 0),
JS_PROP_INT32_DEF("SYMLINK", MAGIC_SYMLINK, 0),
JS_PROP_INT32_DEF("COMPRESS", MAGIC_COMPRESS, 0),
JS_PROP_INT32_DEF("DEVICES", MAGIC_DEVICES, 0),
JS_PROP_INT32_DEF("MIME_TYPE", MAGIC_MIME_TYPE, 0),
JS_PROP_INT32_DEF("CONTINUE", MAGIC_CONTINUE, 0),
JS_PROP_INT32_DEF("CHECK", MAGIC_CHECK, 0),
JS_PROP_INT32_DEF("PRESERVE_ATIME", MAGIC_PRESERVE_ATIME, 0),
JS_PROP_INT32_DEF("RAW", MAGIC_RAW, 0),
JS_PROP_INT32_DEF("ERROR", MAGIC_ERROR, 0),
JS_PROP_INT32_DEF("MIME_ENCODING", MAGIC_MIME_ENCODING, 0),
JS_PROP_INT32_DEF("MIME", MAGIC_MIME, 0),
JS_PROP_INT32_DEF("APPLE", MAGIC_APPLE, 0),
JS_PROP_INT32_DEF("EXTENSION", MAGIC_EXTENSION, 0),
JS_PROP_INT32_DEF("COMPRESS_TRANSP", MAGIC_COMPRESS_TRANSP, 0),
JS_PROP_INT32_DEF("NODESC", MAGIC_NODESC, 0),
JS_PROP_INT32_DEF("NO_CHECK_COMPRESS", MAGIC_NO_CHECK_COMPRESS, 0),
JS_PROP_INT32_DEF("NO_CHECK_TAR", MAGIC_NO_CHECK_TAR, 0),
JS_PROP_INT32_DEF("NO_CHECK_SOFT", MAGIC_NO_CHECK_SOFT, 0),
JS_PROP_INT32_DEF("NO_CHECK_APPTYPE", MAGIC_NO_CHECK_APPTYPE, 0),
JS_PROP_INT32_DEF("NO_CHECK_ELF", MAGIC_NO_CHECK_ELF, 0),
JS_PROP_INT32_DEF("NO_CHECK_TEXT", MAGIC_NO_CHECK_TEXT, 0),
JS_PROP_INT32_DEF("NO_CHECK_CDF", MAGIC_NO_CHECK_CDF, 0),
JS_PROP_INT32_DEF("NO_CHECK_CSV", MAGIC_NO_CHECK_CSV, 0),
JS_PROP_INT32_DEF("NO_CHECK_TOKENS", MAGIC_NO_CHECK_TOKENS, 0),
JS_PROP_INT32_DEF("NO_CHECK_ENCODING", MAGIC_NO_CHECK_ENCODING, 0),
JS_PROP_INT32_DEF("NO_CHECK_JSON", MAGIC_NO_CHECK_JSON, 0),
JS_PROP_INT32_DEF("NO_CHECK_BUILTIN", MAGIC_NO_CHECK_BUILTIN, 0),
JS_PROP_INT32_DEF("NO_CHECK_ASCII", MAGIC_NO_CHECK_ASCII, 0),
JS_PROP_INT32_DEF("NO_CHECK_FORTRAN", MAGIC_NO_CHECK_FORTRAN, 0),
JS_PROP_INT32_DEF("NO_CHECK_TROFF", MAGIC_NO_CHECK_TROFF, 0),
JS_PROP_INT32_DEF("VERSION", MAGIC_VERSION, 0),
JS_PROP_INT32_DEF("PARAM_INDIR_MAX", MAGIC_PARAM_INDIR_MAX, 0),
JS_PROP_INT32_DEF("PARAM_NAME_MAX", MAGIC_PARAM_NAME_MAX, 0),
JS_PROP_INT32_DEF("PARAM_ELF_PHNUM_MAX", MAGIC_PARAM_ELF_PHNUM_MAX, 0),
JS_PROP_INT32_DEF("PARAM_ELF_SHNUM_MAX", MAGIC_PARAM_ELF_SHNUM_MAX, 0),
JS_PROP_INT32_DEF("PARAM_ELF_NOTES_MAX", MAGIC_PARAM_ELF_NOTES_MAX, 0),
JS_PROP_INT32_DEF("PARAM_REGEX_MAX", MAGIC_PARAM_REGEX_MAX, 0),
JS_PROP_INT32_DEF("PARAM_BYTES_MAX", MAGIC_PARAM_BYTES_MAX, 0),
JS_PROP_STRING_DEF("DEFAULT_DB", LIBMAGIC_DB, JS_PROP_C_W_E),
};
int
js_magic_init(JSContext* ctx, JSModuleDef* m) {
if(js_magic_class_id == 0) {
JS_NewClassID(&js_magic_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_magic_class_id, &js_magic_class);
magic_ctor = JS_NewCFunction2(ctx, js_magic_constructor, "Magic", 1, JS_CFUNC_constructor, 0);
magic_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, magic_proto, js_magic_funcs, countof(js_magic_funcs));
JS_SetPropertyFunctionList(ctx, magic_ctor, js_magic_static, countof(js_magic_static));
JS_SetClassProto(ctx, js_magic_class_id, magic_proto);
}
if(m) {
JS_SetModuleExport(ctx, m, "Magic", magic_ctor);
}
return 0;
}
#ifdef JS_MAGIC_MODULE
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_magic
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_magic_init))) {
JS_AddModuleExport(ctx, m, "Magic");
}
return m;
}
/**
* @}
*/