41 magic_t MagicInitContext(
void)
44 const char *filename = NULL;
53 (void)
ConfGet(
"magic-file", &filename);
56 if (filename != NULL) {
57 if (strlen(filename) == 0) {
66 if ( (fd = fopen(filename,
"r")) == NULL) {
67 SCLogWarning(
"Error opening file: \"%s\": %s", filename, strerror(errno));
74 if (magic_load(
ctx, filename) != 0) {
89 void MagicDeinitContext(magic_t
ctx)
103 char *MagicThreadLookup(magic_t *
ctx,
const uint8_t *
buf, uint32_t buflen)
105 const char *result = NULL;
108 if (
buf != NULL && buflen > 0) {
109 result = magic_buffer(*
ctx, (
void *)
buf, (
size_t)buflen);
110 if (result != NULL) {
124 static int MagicInitTest01(
void)
129 magic_ctx = magic_open(0);
130 if (magic_ctx == NULL) {
131 printf(
"failure retrieving magic_ctx\n");
135 if (magic_load(magic_ctx, NULL) == -1) {
136 printf(
"failure magic_load\n");
142 magic_close(magic_ctx);
147 static int MagicDetectTest01(
void)
151 char buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
152 size_t buffer_len =
sizeof(buffer);
155 magic_ctx = magic_open(0);
156 if (magic_ctx == NULL) {
157 printf(
"failure retrieving magic_ctx\n");
161 if (magic_load(magic_ctx, NULL) == -1) {
162 printf(
"magic_load failure\n");
166 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
167 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
168 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
174 magic_close(magic_ctx);
179 static int MagicDetectTest02(
void)
185 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
190 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
192 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
195 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
196 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
197 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
198 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
200 size_t buffer_len =
sizeof(buffer);
203 magic_ctx = magic_open(0);
204 if (magic_ctx == NULL) {
205 printf(
"failure retrieving magic_ctx\n");
209 if (magic_load(magic_ctx, NULL) == -1) {
210 printf(
"magic_load failure\n");
214 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
215 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
216 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
222 magic_close(magic_ctx);
227 static int MagicDetectTest03(
void)
230 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
231 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
232 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
233 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
235 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
236 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
237 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
238 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
240 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
241 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
242 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
243 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
246 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
247 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
248 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
250 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
251 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
253 size_t buffer_len =
sizeof(buffer);
255 magic_t magic_ctx = magic_open(0);
258 FAIL_IF(magic_load(magic_ctx, NULL) == -1);
260 char *result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
263 char *
str = strstr(result,
"OpenDocument Text");
266 magic_close(magic_ctx);
271 static int MagicDetectTest04(
void)
277 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
278 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
279 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
280 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
282 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
283 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
284 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
285 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
287 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
288 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
292 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
293 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
294 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
295 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
297 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
298 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
299 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
300 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
302 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
303 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
304 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
306 size_t buffer_len =
sizeof(buffer);
309 magic_ctx = magic_open(0);
310 if (magic_ctx == NULL) {
311 printf(
"failure retrieving magic_ctx\n");
315 if (magic_load(magic_ctx, NULL) == -1) {
316 printf(
"magic_load failure\n");
320 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
321 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
322 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
328 magic_close(magic_ctx);
334 static int MagicDetectTest05(
void)
336 const char *result = NULL;
338 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
339 size_t buffer_len =
sizeof(buffer);
343 ctx = MagicInitContext();
346 result = MagicThreadLookup(&
ctx, buffer, buffer_len);
347 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
348 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
354 MagicDeinitContext(
ctx);
360 static int MagicDetectTest06(
void)
362 const char *result = NULL;
364 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
367 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
369 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
371 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
374 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
375 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
376 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
377 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
379 size_t buffer_len =
sizeof(buffer);
382 if (MagicInit() < 0) {
383 printf(
"MagicInit() failure\n");
387 result = MagicGlobalLookup(buffer, buffer_len);
388 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
389 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
401 static int MagicDetectTest07(
void)
403 const char *result = NULL;
406 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
407 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
408 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
409 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
411 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
412 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
413 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
414 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
416 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
417 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
418 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
419 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
421 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
422 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
423 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
424 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
426 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
427 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
429 size_t buffer_len =
sizeof(buffer);
431 ctx = MagicInitContext();
434 result = MagicThreadLookup(&
ctx, buffer, buffer_len);
437 char *
str = strstr(result,
"OpenDocument Text");
440 MagicDeinitContext(
ctx);
445 static int MagicDetectTest08(
void)
447 const char *result = NULL;
450 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
451 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
452 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
453 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
455 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
456 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
457 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
458 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
460 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
461 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
463 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
465 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
466 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
467 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
468 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
470 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
471 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
472 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
474 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
475 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
476 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
477 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
479 size_t buffer_len =
sizeof(buffer);
482 ctx = MagicInitContext();
485 result = MagicThreadLookup(&
ctx, buffer, buffer_len);
486 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
487 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
493 MagicDeinitContext(
ctx);
498 static int MagicDetectTest09(
void)
500 const char *result1 = NULL;
501 const char *result2 = NULL;
502 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
503 size_t buffer_len =
sizeof(buffer);
506 if (MagicInit() < 0) {
507 printf(
"MagicInit() failure\n");
511 result1 = MagicGlobalLookup(buffer, buffer_len);
512 if (result1 == NULL || strncmp(result1,
"PDF document", 12) != 0) {
513 printf(
"result %p:%s, not \"PDF document\": ", result1,result1?result1:
"(null)");
517 result2 = MagicGlobalLookup(buffer, buffer_len);
518 if (result2 == NULL || strncmp(result2,
"PDF document", 12) != 0) {
519 printf(
"result %p:%s, not \"PDF document\": ", result2,result2?result2:
"(null)");
523 if (result1 != result2) {
524 printf(
"pointers not equal, weird... %p != %p: ", result1, result2);
536 static int MagicDetectTest10ValgrindError(
void)
538 const char *result = NULL;
541 0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2C,
542 0x01,0x2C,0x00,0x00,0xFF,0xFE,0x00,0x4C,0x53,0x69,0x67,0x6E,0x61,0x74,0x75,0x72,
543 0x65,0x3A,0x34,0x31,0x31,0x65,0x33,0x38,0x61,0x61,0x61,0x31,0x37,0x65,0x33,0x30,
544 0x66,0x30,0x32,0x38,0x62,0x61,0x30,0x31,0x36,0x32,0x36,0x37,0x66,0x66,0x30,0x31,
545 0x36,0x36,0x61,0x65,0x35,0x39,0x65,0x38,0x31,0x39,0x62,0x61,0x32,0x34,0x63,0x39,
546 0x62,0x31,0x33,0x37,0x33,0x62,0x31,0x61,0x35,0x61,0x38,0x65,0x64,0x63,0x36,0x30,
547 0x65,0x37,0xFF,0xE2,0x02,0x2C,0x49,0x43,0x43,0x5F,0x50,0x52,0x4F,0x46,0x49,0x4C,
548 0x45,0x00,0x01,0x01,0x00,0x00,0x02,0x1C,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
549 0x6D,0x6E,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5A,0x20,0x07,0xCF,0x00,0x05,
550 0x00,0x09,0x00,0x15,0x00,0x0B,0x00,0x21,0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4C,
551 0x00,0x00,0x00,0x00,0x6E,0x6F,0x6E,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
553 size_t buffer_len =
sizeof(buffer);
557 ctx = MagicInitContext();
560 result = MagicThreadLookup(&
ctx, buffer, buffer_len);
561 if (result == NULL || strncmp(result,
"JPEG", 4) != 0) {
562 printf(
"result %p:%s, not \"JPEG\": ", result,result?result:
"(null)");
568 MagicDeinitContext(
ctx);
592 MagicDetectTest10ValgrindError);