forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoftmagic.c
2519 lines (2319 loc) · 58.4 KB
/
softmagic.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
* maintained 1995-present by Christos Zoulas and others.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* softmagic - interpret variable magic from MAGIC
*/
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: softmagic.c,v 1.349 2024/11/10 18:33:17 christos Exp $")
#endif /* lint */
#include "magic.h"
#include <assert.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include "der.h"
file_private int match(struct magic_set *, struct magic *, size_t,
const struct buffer *, size_t, int, int, int, uint16_t *,
uint16_t *, int *, int *, int *, int *, int *);
file_private int mget(struct magic_set *, struct magic *, const struct buffer *,
const unsigned char *, size_t,
size_t, unsigned int, int, int, int, uint16_t *,
uint16_t *, int *, int *, int *, int *, int *);
file_private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
const struct buffer *, size_t, unsigned int);
file_private int magiccheck(struct magic_set *, struct magic *);
file_private int mprint(struct magic_set *, struct magic *);
file_private int moffset(struct magic_set *, struct magic *,
const struct buffer *, size_t, int32_t *);
file_private void mdebug(uint32_t, const char *, size_t);
file_private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
const unsigned char *, uint32_t, size_t, struct magic *);
file_private int mconvert(struct magic_set *, struct magic *, int);
file_private int print_sep(struct magic_set *, int);
file_private int handle_annotation(struct magic_set *, struct magic *, int);
file_private int cvt_8(union VALUETYPE *, const struct magic *);
file_private int cvt_16(union VALUETYPE *, const struct magic *);
file_private int cvt_32(union VALUETYPE *, const struct magic *);
file_private int cvt_64(union VALUETYPE *, const struct magic *);
#define OFFSET_OOB(n, o, i) ((n) < CAST(uint32_t, (o)) || (i) > ((n) - (o)))
#define BE64(p) ( \
(CAST(uint64_t, (p)[0])<<56)| \
(CAST(uint64_t, (p)[1])<<48)| \
(CAST(uint64_t, (p)[2])<<40)| \
(CAST(uint64_t, (p)[3])<<32)| \
(CAST(uint64_t, (p)[4])<<24)| \
(CAST(uint64_t, (p)[5])<<16)| \
(CAST(uint64_t, (p)[6])<<8)| \
(CAST(uint64_t, (p)[7])))
#define LE64(p) ( \
(CAST(uint64_t, (p)[7])<<56)| \
(CAST(uint64_t, (p)[6])<<48)| \
(CAST(uint64_t, (p)[5])<<40)| \
(CAST(uint64_t, (p)[4])<<32)| \
(CAST(uint64_t, (p)[3])<<24)| \
(CAST(uint64_t, (p)[2])<<16)| \
(CAST(uint64_t, (p)[1])<<8)| \
(CAST(uint64_t, (p)[0])))
#define LE32(p) ( \
(CAST(uint32_t, (p)[3])<<24)| \
(CAST(uint32_t, (p)[2])<<16)| \
(CAST(uint32_t, (p)[1])<<8)| \
(CAST(uint32_t, (p)[0])))
#define BE32(p) ( \
(CAST(uint32_t, (p)[0])<<24)| \
(CAST(uint32_t, (p)[1])<<16)| \
(CAST(uint32_t, (p)[2])<<8)| \
(CAST(uint32_t, (p)[3])))
#define ME32(p) ( \
(CAST(uint32_t, (p)[1])<<24)| \
(CAST(uint32_t, (p)[0])<<16)| \
(CAST(uint32_t, (p)[3])<<8)| \
(CAST(uint32_t, (p)[2])))
#define BE16(p) ((CAST(uint16_t, (p)[0])<<8)|(CAST(uint16_t, (p)[1])))
#define LE16(p) ((CAST(uint16_t, (p)[1])<<8)|(CAST(uint16_t, (p)[0])))
#define SEXT(s,v,p) ((s) ? \
CAST(intmax_t, CAST(int##v##_t, p)) : \
CAST(intmax_t, CAST(uint##v##_t, p)))
/*
* softmagic - lookup one file in parsed, in-memory copy of database
* Passed the name and FILE * of one file to be typed.
*/
/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
file_protected int
file_softmagic(struct magic_set *ms, const struct buffer *b,
uint16_t *indir_count, uint16_t *name_count, int mode, int text)
{
struct mlist *ml;
int rv = 0, printed_something = 0, need_separator = 0, firstline = 1;
uint16_t nc, ic;
if (name_count == NULL) {
nc = 0;
name_count = &nc;
}
if (indir_count == NULL) {
ic = 0;
indir_count = ⁣
}
for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next) {
int ret = match(ms, ml->magic, ml->nmagic, b,
0, mode, text, 0, indir_count, name_count,
&printed_something, &need_separator, &firstline,
NULL, NULL);
switch (ret) {
case -1:
return ret;
case 0:
continue;
default:
if ((ms->flags & MAGIC_CONTINUE) == 0)
return ret;
rv = ret;
break;
}
}
return rv;
}
#if defined(FILE_FMTDEBUG) && defined(HAVE_FMTCHECK)
#define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
file_private const char * __attribute__((__format_arg__(3)))
file_fmtcheck(struct magic_set *ms, const char *desc, const char *def,
const char *file, size_t line)
{
const char *ptr;
if (strchr(desc, '%') == NULL)
return desc;
ptr = fmtcheck(desc, def);
if (ptr == def)
file_magerror(ms,
"%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
" with `%s'", file, line, desc, def);
return ptr;
}
#elif defined(HAVE_FMTCHECK)
#define F(a, b, c) fmtcheck((b), (c))
#else
#define F(a, b, c) ((b))
#endif
/* NOTE this function has been kept an the state of 5.39 for BC. Observe
* further as the upgrade to 5.41 or above goes. */
/*
* Go through the whole list, stopping if you find a match. Process all
* the continuations of that match before returning.
*
* We support multi-level continuations:
*
* At any time when processing a successful top-level match, there is a
* current continuation level; it represents the level of the last
* successfully matched continuation.
*
* Continuations above that level are skipped as, if we see one, it
* means that the continuation that controls them - i.e, the
* lower-level continuation preceding them - failed to match.
*
* Continuations below that level are processed as, if we see one,
* it means we've finished processing or skipping higher-level
* continuations under the control of a successful or unsuccessful
* lower-level continuation, and are now seeing the next lower-level
* continuation and should process it. The current continuation
* level reverts to the level of the one we're seeing.
*
* Continuations at the current level are processed as, if we see
* one, there's no lower-level continuation that may have failed.
*
* If a continuation matches, we bump the current continuation level
* so that higher-level continuations are processed.
*/
file_private int
match(struct magic_set *ms, struct magic *magic,
size_t nmagic, const struct buffer *b, size_t offset, int mode, int text,
int flip, uint16_t *indir_count, uint16_t *name_count,
int *printed_something, int *need_separator, int *firstline,
int *returnval, int *found_match)
{
uint32_t magindex = 0;
unsigned int cont_level = 0;
int found_matchv = 0; /* if a match is found it is set to 1*/
int returnvalv = 0, e;
struct buffer bb;
int print = (ms->flags & MAGIC_NODESC) == 0;
/*
* returnval can be 0 if a match is found, but there was no
* annotation to be printed.
*/
if (returnval == NULL)
returnval = &returnvalv;
if (found_match == NULL)
found_match = &found_matchv;
if (file_check_mem(ms, cont_level) == -1)
return -1;
for (magindex = 0; magindex < nmagic; magindex++) {
int flush = 0;
struct magic *m = &magic[magindex];
if (m->type != FILE_NAME)
if ((IS_LIBMAGIC_STRING(m->type) &&
#define FLT (STRING_BINTEST | STRING_TEXTTEST)
((text && (m->str_flags & FLT) == STRING_BINTEST) ||
(!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
(m->flag & mode) != mode) {
flush:
/* Skip sub-tests */
while (magindex < nmagic - 1 &&
magic[magindex + 1].cont_level != 0)
magindex++;
cont_level = 0;
continue; /* Skip to next top-level test*/
}
if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
goto flush;
ms->line = m->lineno;
/* if main entry matches, print it... */
switch (mget(ms, m, b, CAST(const unsigned char *, bb.fbuf),
bb.flen, offset, cont_level,
mode, text, flip, indir_count, name_count,
printed_something, need_separator, firstline, returnval,
found_match))
{
case -1:
return -1;
case 0:
flush = m->reln != '!';
break;
default:
if (m->type == FILE_INDIRECT) {
*found_match = 1;
*returnval = 1;
}
switch (magiccheck(ms, m)) {
case -1:
return -1;
case 0:
flush++;
break;
default:
flush = 0;
break;
}
break;
}
if (flush) {
/*
* main entry didn't match,
* flush its continuations
*/
goto flush;
}
if ((e = handle_annotation(ms, m, *firstline)) != 0)
{
*found_match = 1;
*need_separator = 1;
*printed_something = 1;
*returnval = 1;
*firstline = 0;
return e;
}
/*
* If we are going to print something, we'll need to print
* a blank before we print something else.
*/
if (*m->desc) {
*found_match = 1;
if (print) {
*returnval = 1;
*need_separator = 1;
*printed_something = 1;
if (print_sep(ms, *firstline) == -1)
return -1;
if (mprint(ms, m) == -1)
return -1;
}
}
switch (moffset(ms, m, &bb, offset, &ms->c.li[cont_level].off)) {
case -1:
case 0:
goto flush;
default:
break;
}
/* and any continuations that match */
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
while (magindex + 1 < nmagic &&
magic[magindex + 1].cont_level != 0) {
m = &magic[++magindex];
ms->line = m->lineno; /* for messages */
if (cont_level < m->cont_level)
continue;
if (cont_level > m->cont_level) {
/*
* We're at the end of the level
* "cont_level" continuations.
*/
cont_level = m->cont_level;
}
if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
goto flush;
if (m->flag & OFFADD) {
if (cont_level == 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr,
"direct *zero*"
" cont_level\n");
return 0;
}
ms->offset +=
ms->c.li[cont_level - 1].off;
}
#ifdef ENABLE_CONDITIONALS
if (m->cond == COND_ELSE ||
m->cond == COND_ELIF) {
if (ms->c.li[cont_level].last_match == 1)
continue;
}
#endif
switch (mget(ms, m, b, CAST(const unsigned char *,
bb.fbuf), bb.flen, offset,
cont_level, mode, text, flip, indir_count,
name_count, printed_something, need_separator,
firstline, returnval, found_match)) {
case -1:
return -1;
case 0:
if (m->reln != '!')
continue;
flush = 1;
break;
default:
if (m->type == FILE_INDIRECT) {
*found_match = 1;
*returnval = 1;
}
flush = 0;
break;
}
switch (flush ? 1 : magiccheck(ms, m)) {
case -1:
return -1;
case 0:
#ifdef ENABLE_CONDITIONALS
ms->c.li[cont_level].last_match = 0;
#endif
break;
default:
#ifdef ENABLE_CONDITIONALS
ms->c.li[cont_level].last_match = 1;
#endif
if (m->type == FILE_CLEAR)
ms->c.li[cont_level].got_match = 0;
else if (ms->c.li[cont_level].got_match) {
if (m->type == FILE_DEFAULT)
break;
} else
ms->c.li[cont_level].got_match = 1;
if ((e = handle_annotation(ms, m, *firstline))
!= 0) {
*found_match = 1;
*need_separator = 1;
*printed_something = 1;
*returnval = 1;
return e;
}
if (*m->desc) {
*found_match = 1;
}
if (print && *m->desc) {
*returnval = 1;
/*
* This continuation matched. Print
* its message, with a blank before it
* if the previous item printed and
* this item isn't empty.
*/
/*
* If we are going to print something,
* make sure that we have a separator
* first.
*/
if (!*printed_something) {
*printed_something = 1;
if (print_sep(ms, *firstline)
== -1)
return -1;
}
/* space if previous printed */
if (*need_separator
&& (m->flag & NOSPACE) == 0) {
if (file_printf(ms, " ") == -1)
return -1;
}
if (mprint(ms, m) == -1)
return -1;
*need_separator = 1;
}
switch (moffset(ms, m, &bb, offset,
&ms->c.li[cont_level].off)) {
case -1:
case 0:
cont_level--;
break;
default:
break;
}
/*
* If we see any continuations
* at a higher level,
* process them.
*/
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
break;
}
}
if (*printed_something) {
*firstline = 0;
}
if (*found_match) {
if ((ms->flags & MAGIC_CONTINUE) == 0)
return *returnval;
// So that we print a separator
*printed_something = 0;
*firstline = 0;
}
cont_level = 0;
}
return *returnval;
}
file_private int
check_fmt(struct magic_set *ms, const char *fmt)
{
pcre_cache_entry *pce;
int rv = -1;
zend_string *pattern;
if (strchr(fmt, '%') == NULL)
return 0;
pattern = ZSTR_INIT_LITERAL("~%[-0-9\\.]*s~", 0);
if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
rv = -1;
} else {
pcre2_code *re = php_pcre_pce_re(pce);
pcre2_match_data *match_data = php_pcre_create_match_data(0, re);
if (match_data) {
rv = pcre2_match(re, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0;
php_pcre_free_match_data(match_data);
}
}
zend_string_release_ex(pattern, 0);
return rv;
}
#if !defined(HAVE_STRNDUP) || defined(__aiws__) || defined(_AIX)
# if defined(__aiws__) || defined(_AIX)
# define strndup aix_strndup /* aix is broken */
# endif
char *strndup(const char *, size_t);
char *
strndup(const char *str, size_t n)
{
size_t len;
char *copy;
for (len = 0; len < n && str[len]; len++)
continue;
if ((copy = CAST(char *, emalloc(len + 1))) == NULL)
return NULL;
(void)memcpy(copy, str, len);
copy[len] = '\0';
return copy;
}
#endif /* HAVE_STRNDUP */
static int
varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
{
const char *ptr, *sptr, *e, *t, *ee, *et;
size_t l;
for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
l = CAST(size_t, ptr - sptr);
if (l >= len)
return -1;
memcpy(buf, sptr, l);
buf += l;
len -= l;
ptr += 2;
if (!*ptr || ptr[1] != '?')
return -1;
for (et = t = ptr + 2; *et && *et != ':'; et++)
continue;
if (*et != ':')
return -1;
for (ee = e = et + 1; *ee && *ee != '}'; ee++)
continue;
if (*ee != '}')
return -1;
switch (*ptr) {
case 'x':
if (ms->mode & 0111) {
ptr = t;
l = et - t;
} else {
ptr = e;
l = ee - e;
}
break;
default:
return -1;
}
if (l >= len)
return -1;
memcpy(buf, ptr, l);
buf += l;
len -= l;
sptr = ee + 1;
}
l = strlen(sptr);
if (l >= len)
return -1;
memcpy(buf, sptr, l);
buf[l] = '\0';
return 0;
}
file_private int
mprint(struct magic_set *ms, struct magic *m)
{
uint64_t v;
float vf;
double vd;
char buf[128], tbuf[26], sbuf[512], ebuf[512];
const char *desc;
union VALUETYPE *p = &ms->ms_value;
if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
desc = m->desc;
else
desc = ebuf;
#define PRINTER(value, format, stype, utype) \
v = file_signextend(ms, m, CAST(uint64_t, value)); \
switch (check_fmt(ms, desc)) { \
case -1: \
return -1; \
case 1: \
if (m->flag & UNSIGNED) { \
(void)snprintf(buf, sizeof(buf), "%" format "u", \
CAST(utype, v)); \
} else { \
(void)snprintf(buf, sizeof(buf), "%" format "d", \
CAST(stype, v)); \
} \
if (file_printf(ms, F(ms, desc, "%s"), buf) == -1) \
return -1; \
break; \
default: \
if (m->flag & UNSIGNED) { \
if (file_printf(ms, F(ms, desc, "%" format "u"), \
CAST(utype, v)) == -1) \
return -1; \
} else { \
if (file_printf(ms, F(ms, desc, "%" format "d"), \
CAST(stype, v)) == -1) \
return -1; \
} \
break; \
} \
break
switch (m->type) {
case FILE_BYTE:
PRINTER(p->b, "", int8_t, uint8_t);
case FILE_SHORT:
case FILE_BESHORT:
case FILE_LESHORT:
PRINTER(p->h, "", int16_t, uint16_t);
case FILE_LONG:
case FILE_BELONG:
case FILE_LELONG:
case FILE_MELONG:
PRINTER(p->l, "", int32_t, uint32_t);
case FILE_QUAD:
case FILE_BEQUAD:
case FILE_LEQUAD:
case FILE_OFFSET:
PRINTER(p->q, INT64_T_FORMAT, long long, unsigned long long);
case FILE_STRING:
case FILE_PSTRING:
case FILE_BESTRING16:
case FILE_LESTRING16:
if (m->reln == '=' || m->reln == '!') {
if (file_printf(ms, F(ms, desc, "%s"),
file_printable(ms, sbuf, sizeof(sbuf), m->value.s,
sizeof(m->value.s))) == -1)
return -1;
}
else {
char *str = p->s;
/* compute t before we mangle the string? */
if (*m->value.s == '\0')
str[strcspn(str, "\r\n")] = '\0';
if (m->str_flags & STRING_TRIM)
str = file_strtrim(str);
if (file_printf(ms, F(ms, desc, "%s"),
file_printable(ms, sbuf, sizeof(sbuf), str,
sizeof(p->s) - (str - p->s))) == -1)
return -1;
if (m->type == FILE_PSTRING) {
size_t l = file_pstring_length_size(ms, m);
if (l == FILE_BADSIZE)
return -1;
}
}
break;
case FILE_DATE:
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdatetime(tbuf, sizeof(tbuf), p->l, 0)) == -1)
return -1;
break;
case FILE_LDATE:
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdatetime(tbuf, sizeof(tbuf), p->l, FILE_T_LOCAL))
== -1)
return -1;
break;
case FILE_QDATE:
case FILE_BEQDATE:
case FILE_LEQDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdatetime(tbuf, sizeof(tbuf), p->q, 0)) == -1)
return -1;
break;
case FILE_QLDATE:
case FILE_BEQLDATE:
case FILE_LEQLDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdatetime(tbuf, sizeof(tbuf), p->q, FILE_T_LOCAL)) == -1)
return -1;
break;
case FILE_QWDATE:
case FILE_BEQWDATE:
case FILE_LEQWDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdatetime(tbuf, sizeof(tbuf), p->q, FILE_T_WINDOWS))
== -1)
return -1;
break;
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
vf = p->f;
switch (check_fmt(ms, desc)) {
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%g", vf);
if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
return -1;
break;
}
break;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
vd = p->d;
switch (check_fmt(ms, desc)) {
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%g", vd);
if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
return -1;
break;
}
break;
case FILE_SEARCH:
case FILE_REGEX: {
char *cp, *scp;
int rval;
cp = estrndup(RCAST(const char *, ms->search.s),
ms->search.rm_len);
if (cp == NULL) {
file_oomem(ms, ms->search.rm_len);
return -1;
}
scp = (m->str_flags & STRING_TRIM) ? file_strtrim(cp) : cp;
rval = file_printf(ms, F(ms, desc, "%s"), file_printable(ms,
sbuf, sizeof(sbuf), scp, ms->search.rm_len));
efree(cp);
if (rval == -1)
return -1;
break;
}
case FILE_DEFAULT:
case FILE_CLEAR:
if (file_printf(ms, "%s", m->desc) == -1)
return -1;
break;
case FILE_INDIRECT:
case FILE_USE:
case FILE_NAME:
break;
case FILE_DER:
if (file_printf(ms, F(ms, desc, "%s"),
file_printable(ms, sbuf, sizeof(sbuf), ms->ms_value.s,
sizeof(ms->ms_value.s))) == -1)
return -1;
break;
case FILE_GUID:
(void) file_print_guid(buf, sizeof(buf), ms->ms_value.guid);
if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
return -1;
break;
case FILE_MSDOSDATE:
case FILE_BEMSDOSDATE:
case FILE_LEMSDOSDATE:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmtdate(tbuf, sizeof(tbuf), p->h)) == -1)
return -1;
break;
case FILE_MSDOSTIME:
case FILE_BEMSDOSTIME:
case FILE_LEMSDOSTIME:
if (file_printf(ms, F(ms, desc, "%s"),
file_fmttime(tbuf, sizeof(tbuf), p->h)) == -1)
return -1;
break;
case FILE_OCTAL:
file_fmtnum(buf, sizeof(buf), m->value.s, 8);
if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
return -1;
break;
default:
file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
return -1;
}
return 0;
}
file_private int
moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
size_t offset, int32_t *op)
{
size_t nbytes = b->flen;
int32_t o;
switch (m->type) {
case FILE_BYTE:
o = CAST(int32_t, (ms->offset + sizeof(char)));
break;
case FILE_SHORT:
case FILE_BESHORT:
case FILE_LESHORT:
case FILE_MSDOSDATE:
case FILE_LEMSDOSDATE:
case FILE_BEMSDOSDATE:
case FILE_MSDOSTIME:
case FILE_LEMSDOSTIME:
case FILE_BEMSDOSTIME:
o = CAST(int32_t, (ms->offset + sizeof(short)));
break;
case FILE_LONG:
case FILE_BELONG:
case FILE_LELONG:
case FILE_MELONG:
o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
break;
case FILE_QUAD:
case FILE_BEQUAD:
case FILE_LEQUAD:
o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
break;
case FILE_STRING:
case FILE_PSTRING:
case FILE_BESTRING16:
case FILE_LESTRING16:
case FILE_OCTAL:
if (m->reln == '=' || m->reln == '!') {
o = ms->offset + m->vallen;
} else {
union VALUETYPE *p = &ms->ms_value;
if (*m->value.s == '\0')
p->s[strcspn(p->s, "\r\n")] = '\0';
o = CAST(uint32_t, (ms->offset + strlen(p->s)));
if (m->type == FILE_PSTRING) {
size_t l = file_pstring_length_size(ms, m);
if (l == FILE_BADSIZE)
return -1;
o += CAST(uint32_t, l);
}
}
break;
case FILE_DATE:
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
break;
case FILE_LDATE:
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
break;
case FILE_QDATE:
case FILE_BEQDATE:
case FILE_LEQDATE:
o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
break;
case FILE_QLDATE:
case FILE_BEQLDATE:
case FILE_LEQLDATE:
o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
break;
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
o = CAST(int32_t, (ms->offset + sizeof(float)));
break;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
o = CAST(int32_t, (ms->offset + sizeof(double)));
break;
case FILE_REGEX:
if ((m->str_flags & REGEX_OFFSET_START) != 0)
o = CAST(int32_t, ms->search.offset - offset);
else
o = CAST(int32_t,
(ms->search.offset + ms->search.rm_len - offset));
break;
case FILE_SEARCH:
if ((m->str_flags & REGEX_OFFSET_START) != 0)
o = CAST(int32_t, ms->search.offset - offset);
else
o = CAST(int32_t, (ms->search.offset + m->vallen - offset));
break;
case FILE_CLEAR:
case FILE_DEFAULT:
case FILE_INDIRECT:
case FILE_OFFSET:
case FILE_USE:
o = ms->offset;
break;
case FILE_DER:
o = der_offs(ms, m, nbytes);
if (o == -1 || CAST(size_t, o) > nbytes) {
if ((ms->flags & MAGIC_DEBUG) != 0) {
(void)fprintf(stderr,
"Bad DER offset %d nbytes=%"
SIZE_T_FORMAT "u", o, nbytes);
}
*op = 0;
return 0;
}
break;
case FILE_GUID:
o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
break;
default:
o = 0;
break;
}
if (CAST(size_t, o) > nbytes) {
#if 0
file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
"u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
#endif
return -1;
}
*op = o;
return 1;
}
file_private uint32_t
cvt_id3(struct magic_set *ms, uint32_t v)
{
v = ((((v >> 0) & 0x7f) << 0) |
(((v >> 8) & 0x7f) << 7) |
(((v >> 16) & 0x7f) << 14) |