42 magic_t MagicInitContext(
void)
45 const char *filename = NULL;
55 (void)
ConfGet(
"magic-file", &filename);
58 if (filename != NULL) {
59 if (strlen(filename) == 0) {
68 if ( (fd = fopen(filename,
"r")) == NULL) {
70 filename, strerror(errno));
77 if (magic_load(ctx, filename) != 0) {
93 void MagicDeinitContext(magic_t ctx)
107 char *MagicThreadLookup(magic_t *ctx,
const uint8_t *
buf, uint32_t buflen)
109 const char *result = NULL;
112 if (
buf != NULL && buflen > 0) {
113 result = magic_buffer(*ctx, (
void *)
buf, (
size_t)buflen);
114 if (result != NULL) {
127 #if defined OS_FREEBSD || defined OS_DARWIN
128 #define MICROSOFT_OFFICE_DOC "OLE 2 Compound Document"
130 #define MICROSOFT_OFFICE_DOC "Microsoft Office Document"
134 static int MagicInitTest01(
void)
139 magic_ctx = magic_open(0);
140 if (magic_ctx == NULL) {
141 printf(
"failure retrieving magic_ctx\n");
145 if (magic_load(magic_ctx, NULL) == -1) {
146 printf(
"failure magic_load\n");
152 magic_close(magic_ctx);
157 static int MagicDetectTest01(
void)
161 char buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
162 size_t buffer_len =
sizeof(buffer);
165 magic_ctx = magic_open(0);
166 if (magic_ctx == NULL) {
167 printf(
"failure retrieving magic_ctx\n");
171 if (magic_load(magic_ctx, NULL) == -1) {
172 printf(
"magic_load failure\n");
176 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
177 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
178 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
184 magic_close(magic_ctx);
189 static int MagicDetectTest02(
void)
195 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
196 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
200 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
202 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
205 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
206 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
207 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
208 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
210 size_t buffer_len =
sizeof(buffer);
213 magic_ctx = magic_open(0);
214 if (magic_ctx == NULL) {
215 printf(
"failure retrieving magic_ctx\n");
219 if (magic_load(magic_ctx, NULL) == -1) {
220 printf(
"magic_load failure\n");
224 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
225 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
226 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
232 magic_close(magic_ctx);
237 static int MagicDetectTest03(
void)
240 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
242 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
243 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
245 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
246 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
247 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
248 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
250 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
251 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
252 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
253 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
256 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
257 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
258 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
260 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
261 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
263 size_t buffer_len =
sizeof(buffer);
265 magic_t magic_ctx = magic_open(0);
268 FAIL_IF(magic_load(magic_ctx, NULL) == -1);
270 char *result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
273 char *
str = strstr(result,
"OpenDocument Text");
276 magic_close(magic_ctx);
281 static int MagicDetectTest04(
void)
287 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
288 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
289 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
290 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
292 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
293 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
294 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
295 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
297 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
298 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
302 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
303 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
304 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
305 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
307 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
308 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
309 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
310 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
312 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
313 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
314 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
316 size_t buffer_len =
sizeof(buffer);
319 magic_ctx = magic_open(0);
320 if (magic_ctx == NULL) {
321 printf(
"failure retrieving magic_ctx\n");
325 if (magic_load(magic_ctx, NULL) == -1) {
326 printf(
"magic_load failure\n");
330 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
331 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
332 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
338 magic_close(magic_ctx);
344 static int MagicDetectTest05(
void)
346 const char *result = NULL;
348 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
349 size_t buffer_len =
sizeof(buffer);
353 ctx = MagicInitContext();
356 result = MagicThreadLookup(&ctx, buffer, buffer_len);
357 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
358 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
364 MagicDeinitContext(ctx);
370 static int MagicDetectTest06(
void)
372 const char *result = NULL;
374 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
379 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
381 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
384 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
385 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
386 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
387 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
389 size_t buffer_len =
sizeof(buffer);
392 if (MagicInit() < 0) {
393 printf(
"MagicInit() failure\n");
397 result = MagicGlobalLookup(buffer, buffer_len);
398 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
399 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
411 static int MagicDetectTest07(
void)
413 const char *result = NULL;
416 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
417 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
418 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
419 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
421 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
422 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
423 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
424 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
426 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
427 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
428 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
429 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
431 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
432 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
433 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
434 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
436 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
437 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
439 size_t buffer_len =
sizeof(buffer);
441 ctx = MagicInitContext();
444 result = MagicThreadLookup(&ctx, buffer, buffer_len);
447 char *
str = strstr(result,
"OpenDocument Text");
450 MagicDeinitContext(ctx);
455 static int MagicDetectTest08(
void)
457 const char *result = NULL;
460 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
461 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
462 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
463 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
465 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
466 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
467 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
468 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
470 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
471 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
472 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
475 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
476 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
477 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
478 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
480 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
481 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
482 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
484 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
485 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
486 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
487 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
489 size_t buffer_len =
sizeof(buffer);
492 ctx = MagicInitContext();
495 result = MagicThreadLookup(&ctx, buffer, buffer_len);
496 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
497 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
503 MagicDeinitContext(ctx);
508 static int MagicDetectTest09(
void)
510 const char *result1 = NULL;
511 const char *result2 = NULL;
512 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
513 size_t buffer_len =
sizeof(buffer);
516 if (MagicInit() < 0) {
517 printf(
"MagicInit() failure\n");
521 result1 = MagicGlobalLookup(buffer, buffer_len);
522 if (result1 == NULL || strncmp(result1,
"PDF document", 12) != 0) {
523 printf(
"result %p:%s, not \"PDF document\": ", result1,result1?result1:
"(null)");
527 result2 = MagicGlobalLookup(buffer, buffer_len);
528 if (result2 == NULL || strncmp(result2,
"PDF document", 12) != 0) {
529 printf(
"result %p:%s, not \"PDF document\": ", result2,result2?result2:
"(null)");
533 if (result1 != result2) {
534 printf(
"pointers not equal, weird... %p != %p: ", result1, result2);
546 static int MagicDetectTest10ValgrindError(
void)
548 const char *result = NULL;
551 0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2C,
552 0x01,0x2C,0x00,0x00,0xFF,0xFE,0x00,0x4C,0x53,0x69,0x67,0x6E,0x61,0x74,0x75,0x72,
553 0x65,0x3A,0x34,0x31,0x31,0x65,0x33,0x38,0x61,0x61,0x61,0x31,0x37,0x65,0x33,0x30,
554 0x66,0x30,0x32,0x38,0x62,0x61,0x30,0x31,0x36,0x32,0x36,0x37,0x66,0x66,0x30,0x31,
555 0x36,0x36,0x61,0x65,0x35,0x39,0x65,0x38,0x31,0x39,0x62,0x61,0x32,0x34,0x63,0x39,
556 0x62,0x31,0x33,0x37,0x33,0x62,0x31,0x61,0x35,0x61,0x38,0x65,0x64,0x63,0x36,0x30,
557 0x65,0x37,0xFF,0xE2,0x02,0x2C,0x49,0x43,0x43,0x5F,0x50,0x52,0x4F,0x46,0x49,0x4C,
558 0x45,0x00,0x01,0x01,0x00,0x00,0x02,0x1C,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
559 0x6D,0x6E,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5A,0x20,0x07,0xCF,0x00,0x05,
560 0x00,0x09,0x00,0x15,0x00,0x0B,0x00,0x21,0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4C,
561 0x00,0x00,0x00,0x00,0x6E,0x6F,0x6E,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
563 size_t buffer_len =
sizeof(buffer);
567 ctx = MagicInitContext();
570 result = MagicThreadLookup(&ctx, buffer, buffer_len);
571 if (result == NULL || strncmp(result,
"JPEG", 4) != 0) {
572 printf(
"result %p:%s, not \"JPEG\": ", result,result?result:
"(null)");
578 MagicDeinitContext(ctx);
602 MagicDetectTest10ValgrindError);