forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmagic.patch
3397 lines (3219 loc) · 84.7 KB
/
libmagic.patch
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
diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
--- libmagic.orig/apprentice.c 2013-03-21 18:45:14.000000000 +0100
+++ libmagic/apprentice.c 2013-04-27 13:53:32.175250261 +0200
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
+#include "php.h"
+
#include "file.h"
#ifndef lint
@@ -36,18 +38,30 @@
#endif /* lint */
#include "magic.h"
+#include "patchlevel.h"
#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
+
+#if defined(__hpux) && !defined(HAVE_STRTOULL)
+#if SIZEOF_LONG == 8
+# define strtoull strtoul
+#else
+# define strtoull __strtoull
+#endif
+#endif
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#if _MSC_VER <= 1300
+# include "win32/php_strtoi64.h"
+#endif
+#define strtoull _strtoui64
+#else
#include <unistd.h>
#endif
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
-#ifdef QUICK
-#include <sys/mman.h>
-#endif
-#include <dirent.h>
#define EATAB {while (isascii((unsigned char) *l) && \
isspace((unsigned char) *l)) ++l;}
@@ -143,38 +157,7 @@
{ NULL, 0, NULL }
};
-#ifdef COMPILE_ONLY
-
-int main(int, char *[]);
-
-int
-main(int argc, char *argv[])
-{
- int ret;
- struct magic_set *ms;
- char *progname;
-
- if ((progname = strrchr(argv[0], '/')) != NULL)
- progname++;
- else
- progname = argv[0];
-
- if (argc != 2) {
- (void)fprintf(stderr, "Usage: %s file\n", progname);
- return 1;
- }
-
- if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
- (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return 1;
- }
- ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
- if (ret == 1)
- (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
- magic_close(ms);
- return ret;
-}
-#endif /* COMPILE_ONLY */
+#include "../data_file.c"
struct type_tbl_s {
const char name[16];
@@ -255,6 +238,10 @@
# undef XX
# undef XX_NULL
+#ifndef S_ISDIR
+#define S_ISDIR(mode) ((mode) & _S_IFDIR)
+#endif
+
private int
get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
{
@@ -378,7 +365,7 @@
{
struct mlist *ml;
- if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
+ if ((ml = CAST(struct mlist *, emalloc(sizeof(*ml)))) == NULL)
return -1;
ml->map = idx == 0 ? map : NULL;
@@ -416,12 +403,13 @@
return apprentice_compile(ms, map, fn);
}
-#ifndef COMPILE_ONLY
map = apprentice_map(ms, fn);
if (map == NULL) {
- if (ms->flags & MAGIC_CHECK)
- file_magwarn(ms, "using regular magic file `%s'", fn);
- map = apprentice_load(ms, fn, action);
+ if (fn) {
+ if (ms->flags & MAGIC_CHECK)
+ file_magwarn(ms, "using regular magic file `%s'", fn);
+ map = apprentice_load(ms, fn, action);
+ }
if (map == NULL)
return -1;
}
@@ -444,7 +432,6 @@
}
return 0;
-#endif /* COMPILE_ONLY */
}
protected void
@@ -455,10 +442,16 @@
return;
for (i = 0; i < MAGIC_SETS; i++)
mlist_free(ms->mlist[i]);
- free(ms->o.pbuf);
- free(ms->o.buf);
- free(ms->c.li);
- free(ms);
+ if (ms->o.pbuf) {
+ efree(ms->o.pbuf);
+ }
+ if (ms->o.buf) {
+ efree(ms->o.buf);
+ }
+ if (ms->c.li) {
+ efree(ms->c.li);
+ }
+ efree(ms);
}
protected struct magic_set *
@@ -467,7 +460,7 @@
struct magic_set *ms;
size_t i, len;
- if ((ms = CAST(struct magic_set *, calloc((size_t)1,
+ if ((ms = CAST(struct magic_set *, ecalloc((size_t)1,
sizeof(struct magic_set)))) == NULL)
return NULL;
@@ -479,7 +472,7 @@
ms->o.buf = ms->o.pbuf = NULL;
len = (ms->c.len = 10) * sizeof(*ms->c.li);
- if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
+ if ((ms->c.li = CAST(struct level_info *, emalloc(len))) == NULL)
goto free;
ms->event_flags = 0;
@@ -490,7 +483,7 @@
ms->line = 0;
return ms;
free:
- free(ms);
+ efree(ms);
return NULL;
}
@@ -499,22 +492,24 @@
{
if (map == NULL)
return;
- if (map->p == NULL)
- return;
-#ifdef QUICK
- if (map->len)
- (void)munmap(map->p, map->len);
- else
-#endif
- free(map->p);
- free(map);
+ if (map->p != php_magic_database) {
+ int j;
+ for (j = 0; j < MAGIC_SETS; j++) {
+ if (map->magic[j])
+ efree(map->magic[j]);
+ }
+ if (map->p != NULL) {
+ efree(map->p);
+ }
+ }
+ efree(map);
}
private struct mlist *
mlist_alloc(void)
{
struct mlist *mlist;
- if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
+ if ((mlist = CAST(struct mlist *, ecalloc(1, sizeof(*mlist)))) == NULL) {
return NULL;
}
mlist->next = mlist->prev = mlist;
@@ -533,10 +528,10 @@
struct mlist *next = ml->next;
if (ml->map)
apprentice_unmap(ml->map);
- free(ml);
+ efree(ml);
ml = next;
}
- free(ml);
+ efree(ml);
}
/* const char *fn: list of magic files and directories */
@@ -546,13 +541,28 @@
char *p, *mfn;
int file_err, errs = -1;
size_t i;
-
+/* XXX disabling default magic loading so the compiled in data is used */
+#if 0
if ((fn = magic_getpath(fn, action)) == NULL)
return -1;
+#endif
init_file_tables();
- if ((mfn = strdup(fn)) == NULL) {
+ if (fn == NULL)
+ fn = getenv("MAGIC");
+ if (fn == NULL) {
+ for (i = 0; i < MAGIC_SETS; i++) {
+ mlist_free(ms->mlist[i]);
+ if ((ms->mlist[i] = mlist_alloc()) == NULL) {
+ file_oomem(ms, sizeof(*ms->mlist[i]));
+ return -1;
+ }
+ }
+ return apprentice_1(ms, fn, action);
+ }
+
+ if ((mfn = estrdup(fn)) == NULL) {
file_oomem(ms, strlen(fn));
return -1;
}
@@ -567,7 +577,7 @@
mlist_free(ms->mlist[i]);
while (i != 0);
}
- free(mfn);
+ efree(mfn);
return -1;
}
}
@@ -584,7 +594,7 @@
fn = p;
}
- free(mfn);
+ efree(mfn);
if (errs == -1) {
for (i = 0; i < MAGIC_SETS; i++) {
@@ -904,7 +914,7 @@
maxmagic[i] += ALLOC_INCR;
if ((mp = CAST(struct magic_entry *,
- realloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
+ erealloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
NULL) {
file_oomem(ms, sizeof(*mp) * maxmagic[i]);
return -1;
@@ -925,13 +935,24 @@
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
struct magic_entry **mentry, uint32_t *mentrycount)
{
- size_t lineno = 0, llen = 0;
+ char buffer[BUFSIZ + 1];
char *line = NULL;
- ssize_t len;
+ size_t len;
+ size_t lineno = 0;
struct magic_entry me;
- FILE *f = fopen(ms->file = fn, "r");
- if (f == NULL) {
+ php_stream *stream;
+
+ TSRMLS_FETCH();
+
+ ms->file = fn;
+#if PHP_API_VERSION < 20100412
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
+#else
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
+#endif
+
+ if (stream == NULL) {
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
fn);
@@ -941,8 +962,7 @@
memset(&me, 0, sizeof(me));
/* read and parse this file */
- for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
- ms->line++) {
+ for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; ms->line++) {
if (len == 0) /* null line, garbage, etc */
continue;
if (line[len - 1] == '\n') {
@@ -994,14 +1014,13 @@
goto again;
default:
(*errs)++;
- break;
- }
+ break;
}
}
+ }
if (me.mp)
(void)addentry(ms, &me, mentry, mentrycount);
- free(line);
- (void)fclose(f);
+ php_stream_close(stream);
}
/*
@@ -1080,7 +1099,7 @@
mentrycount += me[i].cont_count;
slen = sizeof(**ma) * mentrycount;
- if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
+ if ((*ma = CAST(struct magic *, emalloc(slen))) == NULL) {
file_oomem(ms, slen);
return -1;
}
@@ -1102,27 +1121,29 @@
if (me == NULL)
return;
for (i = 0; i < nme; i++)
- free(me[i].mp);
- free(me);
+ efree(me[i].mp);
+ efree(me);
}
private struct magic_map *
apprentice_load(struct magic_set *ms, const char *fn, int action)
{
- int errs = 0;
+ int errs = 0;
struct magic_entry *mentry[MAGIC_SETS] = { NULL };
uint32_t mentrycount[MAGIC_SETS] = { 0 };
uint32_t i, j;
size_t files = 0, maxfiles = 0;
- char **filearr = NULL, *mfn;
+ char **filearr = NULL;
struct stat st;
struct magic_map *map;
- DIR *dir;
- struct dirent *d;
+ php_stream *dir;
+ php_stream_dirent d;
+
+ TSRMLS_FETCH();
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
file_oomem(ms, sizeof(*map));
return NULL;
}
@@ -1131,23 +1152,37 @@
if (action == FILE_CHECK)
(void)fprintf(stderr, "%s\n", usg_hdr);
+ {
+ /* XXX the maxmagic has to be reset each time we load some new magic file.
+ Where file commando is used it's not essential as the CLI process
+ ends, multiple loading within the same process wouldn't work. */
+ int k;
+ for (k = 0; k < MAGIC_SETS; k++) {
+ maxmagic[k] = 0;
+ }
+ }
+
/* load directory or file */
- if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
- dir = opendir(fn);
+ /* FIXME: Read file names and sort them to prevent
+ non-determinism. See Debian bug #488562. */
+ if (php_sys_stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
+ int mflen;
+ char mfn[MAXPATHLEN];
+
+ dir = php_stream_opendir((char *)fn, REPORT_ERRORS, NULL);
if (!dir) {
errs++;
goto out;
}
- while ((d = readdir(dir)) != NULL) {
- if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
+ while (php_stream_readdir(dir, &d)) {
+ if ((mflen = snprintf(mfn, sizeof(mfn), "%s/%s", fn, d.d_name)) < 0) {
file_oomem(ms,
- strlen(fn) + strlen(d->d_name) + 2);
+ strlen(fn) + strlen(d.d_name) + 2);
errs++;
- closedir(dir);
+ php_stream_closedir(dir);
goto out;
}
if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
- free(mfn);
continue;
}
if (files >= maxfiles) {
@@ -1155,24 +1190,23 @@
maxfiles = (maxfiles + 1) * 2;
mlen = maxfiles * sizeof(*filearr);
if ((filearr = CAST(char **,
- realloc(filearr, mlen))) == NULL) {
+ erealloc(filearr, mlen))) == NULL) {
file_oomem(ms, mlen);
- free(mfn);
- closedir(dir);
+ php_stream_closedir(dir);
errs++;
goto out;
}
}
- filearr[files++] = mfn;
+ filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen);
}
- closedir(dir);
+ php_stream_closedir(dir);
qsort(filearr, files, sizeof(*filearr), cmpstrp);
for (i = 0; i < files; i++) {
load_1(ms, action, filearr[i], &errs, mentry,
mentrycount);
- free(filearr[i]);
+ efree(filearr[i]);
}
- free(filearr);
+ efree(filearr);
} else
load_1(ms, action, fn, &errs, mentry, mentrycount);
if (errs)
@@ -1211,9 +1245,9 @@
if (errs) {
for (j = 0; j < MAGIC_SETS; j++) {
if (map->magic[j])
- free(map->magic[j]);
+ efree(map->magic[j]);
}
- free(map);
+ efree(map);
return NULL;
}
return map;
@@ -1500,7 +1534,7 @@
if (me->cont_count == me->max_count) {
struct magic *nm;
size_t cnt = me->max_count + ALLOC_CHUNK;
- if ((nm = CAST(struct magic *, realloc(me->mp,
+ if ((nm = CAST(struct magic *, erealloc(me->mp,
sizeof(*nm) * cnt))) == NULL) {
file_oomem(ms, sizeof(*nm) * cnt);
return -1;
@@ -1515,7 +1549,7 @@
static const size_t len = sizeof(*m) * ALLOC_CHUNK;
if (me->mp != NULL)
return 1;
- if ((m = CAST(struct magic *, malloc(len))) == NULL) {
+ if ((m = CAST(struct magic *, emalloc(len))) == NULL) {
file_oomem(ms, len);
return -1;
}
@@ -1688,7 +1722,7 @@
m->type = get_standard_integer_type(l, &l);
else if (*l == 's' && !isalpha((unsigned char)l[1])) {
m->type = FILE_STRING;
- ++l;
+ ++l;
}
}
}
@@ -1701,6 +1735,10 @@
if (m->type == FILE_INVALID) {
if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "type `%s' invalid", l);
+ if (me->mp) {
+ efree(me->mp);
+ me->mp = NULL;
+ }
return -1;
}
@@ -1709,7 +1747,7 @@
m->mask_op = 0;
if (*l == '~') {
- if (!IS_STRING(m->type))
+ if (!IS_LIBMAGIC_STRING(m->type))
m->mask_op |= FILE_OPINVERSE;
else if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "'~' invalid for string types");
@@ -1718,7 +1756,7 @@
m->str_range = 0;
m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
if ((op = get_op(*l)) != -1) {
- if (!IS_STRING(m->type)) {
+ if (!IS_LIBMAGIC_STRING(m->type)) {
uint64_t val;
++l;
m->mask_op |= op;
@@ -1909,11 +1947,6 @@
if (check_format(ms, m) == -1)
return -1;
}
-#ifndef COMPILE_ONLY
- if (action == FILE_CHECK) {
- file_mdump(m);
- }
-#endif
m->mimetype[0] = '\0'; /* initialise MIME type to none */
return 0;
}
@@ -2554,59 +2587,80 @@
private struct magic_map *
apprentice_map(struct magic_set *ms, const char *fn)
{
- int fd;
- struct stat st;
uint32_t *ptr;
uint32_t version, entries, nentries;
int needsbyteswap;
char *dbname = NULL;
struct magic_map *map;
size_t i;
+ php_stream *stream = NULL;
+ php_stream_statbuf st;
+
- fd = -1;
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
+ TSRMLS_FETCH();
+
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
file_oomem(ms, sizeof(*map));
+ efree(map);
goto error;
}
+ if (fn == NULL) {
+ map->p = (void *)&php_magic_database;
+ goto internal_loaded;
+ }
+
+#ifdef PHP_WIN32
+ /* Don't bother on windows with php_stream_open_wrapper,
+ return to give apprentice_load() a chance. */
+ if (php_stream_stat_path_ex((char *)fn, 0, &st, NULL) == SUCCESS) {
+ if (st.sb.st_mode & S_IFDIR) {
+ goto error;
+ }
+ }
+#endif
+
dbname = mkdbname(ms, fn, 0);
if (dbname == NULL)
goto error;
- if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
+#if PHP_API_VERSION < 20100412
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
+#else
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
+#endif
+
+ if (!stream) {
goto error;
+ }
- if (fstat(fd, &st) == -1) {
+ if (php_stream_stat(stream, &st) < 0) {
file_error(ms, errno, "cannot stat `%s'", dbname);
goto error;
}
- if (st.st_size < 8) {
+
+ if (st.sb.st_size < 8) {
file_error(ms, 0, "file `%s' is too small", dbname);
goto error;
}
- map->len = (size_t)st.st_size;
-#ifdef QUICK
- if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
- file_error(ms, errno, "cannot map `%s'", dbname);
- goto error;
- }
-#else
- if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
+ map->len = (size_t)st.sb.st_size;
+ if ((map->p = CAST(void *, emalloc(map->len))) == NULL) {
file_oomem(ms, map->len);
goto error;
}
- if (read(fd, map->p, map->len) != (ssize_t)map->len) {
+ if (php_stream_read(stream, map->p, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) {
file_badread(ms);
goto error;
}
map->len = 0;
#define RET 1
-#endif
- (void)close(fd);
- fd = -1;
- ptr = CAST(uint32_t *, map->p);
+
+ php_stream_close(stream);
+ stream = NULL;
+
+internal_loaded:
+ ptr = (uint32_t *)(void *)map->p;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
file_error(ms, 0, "bad magic in `%s'", dbname);
@@ -2620,17 +2674,29 @@
else
version = ptr[1];
if (version != VERSIONNO) {
- file_error(ms, 0, "File %s supports only version %d magic "
- "files. `%s' is version %d", VERSION,
+ file_error(ms, 0, "File %d.%d supports only version %d magic "
+ "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
VERSIONNO, dbname, version);
goto error;
}
- entries = (uint32_t)(st.st_size / sizeof(struct magic));
- if ((off_t)(entries * sizeof(struct magic)) != st.st_size) {
- file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
- dbname, (unsigned long long)st.st_size,
- sizeof(struct magic));
- goto error;
+
+ /* php_magic_database is a const, performing writes will segfault. This is for big-endian
+ machines only, PPC and Sparc specifically. Consider static variable or MINIT in
+ future. */
+ if (needsbyteswap && fn == NULL) {
+ map->p = emalloc(sizeof(php_magic_database));
+ map->p = memcpy(map->p, php_magic_database, sizeof(php_magic_database));
+ }
+
+ if (NULL != fn) {
+ nentries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
+ entries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
+ if ((off_t)(entries * sizeof(struct magic)) != st.sb.st_size) {
+ file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
+ dbname, (unsigned long long)st.sb.st_size,
+ sizeof(struct magic));
+ goto error;
+ }
}
map->magic[0] = CAST(struct magic *, map->p) + 1;
nentries = 0;
@@ -2643,22 +2709,29 @@
map->magic[i + 1] = map->magic[i] + map->nmagic[i];
nentries += map->nmagic[i];
}
- if (entries != nentries + 1) {
+ if (NULL != fn && entries != nentries + 1) {
file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
dbname, entries, nentries + 1);
goto error;
}
+
if (needsbyteswap)
for (i = 0; i < MAGIC_SETS; i++)
byteswap(map->magic[i], map->nmagic[i]);
- free(dbname);
+
+ if (dbname) {
+ efree(dbname);
+ }
return map;
error:
- if (fd != -1)
- (void)close(fd);
+ if (stream) {
+ php_stream_close(stream);
+ }
apprentice_unmap(map);
- free(dbname);
+ if (dbname) {
+ efree(dbname);
+ }
return NULL;
}
@@ -2679,14 +2752,23 @@
char *dbname;
int rv = -1;
uint32_t i;
+ php_stream *stream;
+
+ TSRMLS_FETCH();
- dbname = mkdbname(ms, fn, 1);
+ dbname = mkdbname(ms, fn, 0);
if (dbname == NULL)
goto out;
- if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
- {
+/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
+#if PHP_API_VERSION < 20100412
+ stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
+#else
+ stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL);
+#endif
+
+ if (!stream) {
file_error(ms, errno, "cannot open `%s'", dbname);
goto out;
}
@@ -2696,31 +2778,33 @@
goto out;
}
- if (write(fd, map->nmagic, nm) != (ssize_t)nm) {
+ if (php_stream_write(stream, (const char *)map->nmagic, nm) != (ssize_t)nm) {
file_error(ms, errno, "error writing `%s'", dbname);
goto out;
}
assert(nm + sizeof(ar) < m);
- if (lseek(fd, (off_t)m, SEEK_SET) != (off_t)m) {
+ if (php_stream_seek(stream,(off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) {
file_error(ms, errno, "error seeking `%s'", dbname);
goto out;
}
for (i = 0; i < MAGIC_SETS; i++) {
len = m * map->nmagic[i];
- if (write(fd, map->magic[i], len) != (ssize_t)len) {
+ if (php_stream_write(stream, (const char *)map->magic[i], len) != (ssize_t)len) {
file_error(ms, errno, "error writing `%s'", dbname);
goto out;
}
}
- if (fd != -1)
- (void)close(fd);
+ if (stream) {
+ php_stream_close(stream);
+ }
+
rv = 0;
out:
- free(dbname);
+ efree(dbname);
return rv;
}
@@ -2733,6 +2817,7 @@
{
const char *p, *q;
char *buf;
+ TSRMLS_FETCH();
if (strip) {
if ((p = strrchr(fn, '/')) != NULL)
@@ -2754,16 +2839,18 @@
q++;
/* Compatibility with old code that looked in .mime */
if (ms->flags & MAGIC_MIME) {
- if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
- if (access(buf, R_OK) != -1) {
+ spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", (int)(q - fn), fn, ext);
+#ifdef PHP_WIN32
+ if (VCWD_ACCESS(buf, R_OK) == 0) {
+#else
+ if (VCWD_ACCESS(buf, R_OK) != -1) {
+#endif
ms->flags &= MAGIC_MIME_TYPE;
return buf;
}
- free(buf);
+ efree(buf);
}
- if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
+ spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext);
/* Compatibility with old code that looked in .mime */
if (strstr(p, ".mime") != NULL)
@@ -2853,7 +2940,7 @@
m->offset = swap4((uint32_t)m->offset);
m->in_offset = swap4((uint32_t)m->in_offset);
m->lineno = swap4((uint32_t)m->lineno);
- if (IS_STRING(m->type)) {
+ if (IS_LIBMAGIC_STRING(m->type)) {
m->str_range = swap4(m->str_range);
m->str_flags = swap4(m->str_flags);
}
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
--- libmagic.orig/ascmagic.c 2012-10-31 18:03:01.000000000 +0100
+++ libmagic/ascmagic.c 2013-04-08 15:42:57.328298809 +0200
@@ -139,7 +139,7 @@
/* malloc size is a conservative overestimate; could be
improved, or at least realloced after conversion. */
mlen = ulen * 6;
- if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
+ if ((utf8_buf = CAST(unsigned char *, emalloc(mlen))) == NULL) {
file_oomem(ms, mlen);
goto done;
}
@@ -211,6 +211,7 @@
case 0:
if (file_printf(ms, ", ") == -1)
goto done;
+ break;
case -1:
goto done;
default:
@@ -296,7 +297,8 @@
}
rv = 1;
done:
- free(utf8_buf);
+ if (utf8_buf)
+ efree(utf8_buf);
return rv;
}
diff -u libmagic.orig/cdf.c libmagic/cdf.c
--- libmagic.orig/cdf.c 2013-03-21 18:45:14.000000000 +0100
+++ libmagic/cdf.c 2013-04-08 15:42:57.328298809 +0200
@@ -43,7 +43,17 @@
#include <err.h>
#endif
#include <stdlib.h>
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
#include <unistd.h>
+#endif
+
+#ifndef UINT32_MAX
+# define UINT32_MAX (0xffffffff)
+#endif
+
#include <string.h>
#include <time.h>
#include <ctype.h>
@@ -296,7 +306,10 @@
if (info->i_fd == -1)
return -1;
- if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
+ if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (off_t)-1)
+ return -1;
+
+ if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
return -1;
return (ssize_t)len;
@@ -1132,7 +1145,7 @@
cdf_directory_t *d;
char name[__arraycount(d->d_name)];
cdf_stream_t scn;
- struct timespec ts;
+ struct timeval ts;
static const char *types[] = { "empty", "user storage",
"user stream", "lockbytes", "property", "root storage" };
@@ -1185,7 +1198,7 @@
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
{
cdf_timestamp_t tp;
- struct timespec ts;
+ struct timeval ts;
char buf[64];
size_t i, j;
@@ -1229,7 +1242,11 @@
break;
case CDF_FILETIME:
tp = info[i].pi_tp;
+#if defined(PHP_WIN32) && _MSC_VER <= 1500
+ if (tp < 1000000000000000i64) {
+#else
if (tp < 1000000000000000LL) {
+#endif
cdf_print_elapsed_time(buf, sizeof(buf), tp);
(void)fprintf(stderr, "timestamp %s\n", buf);
} else {
diff -u libmagic.orig/cdf.h libmagic/cdf.h
--- libmagic.orig/cdf.h 2012-10-31 18:03:01.000000000 +0100
+++ libmagic/cdf.h 2013-04-08 15:42:57.328298809 +0200
@@ -35,10 +35,12 @@
#ifndef _H_CDF_
#define _H_CDF_
-#ifdef WIN32
+#ifdef PHP_WIN32
#include <winsock2.h>
#define timespec timeval
#define tv_nsec tv_usec
+#define asctime_r php_asctime_r
+#define ctime_r php_ctime_r
#endif
#ifdef __DJGPP__
#define timespec timeval
@@ -57,7 +59,11 @@
typedef struct {
uint64_t h_magic;
-#define CDF_MAGIC 0xE11AB1A1E011CFD0LL
+#if defined(PHP_WIN32) && _MSC_VER <= 1500
+# define CDF_MAGIC 0xE11AB1A1E011CFD0i64
+#else
+# define CDF_MAGIC 0xE11AB1A1E011CFD0LL
+#endif
uint64_t h_uuid[2];
uint16_t h_revision;
uint16_t h_version;
@@ -267,9 +273,9 @@
size_t i_len;
} cdf_info_t;
-struct timespec;
-int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
-int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
+struct timeval;
+int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t);
+int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *);
int cdf_read_header(const cdf_info_t *, cdf_header_t *);
void cdf_swap_header(cdf_header_t *);
void cdf_unpack_header(cdf_header_t *, char *);
diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
--- libmagic.orig/cdf_time.c 2012-10-31 18:03:01.000000000 +0100
+++ libmagic/cdf_time.c 2013-04-08 15:42:57.328298809 +0200
@@ -96,7 +96,7 @@
}
int
-cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
+cdf_timestamp_to_timespec(struct timeval *ts, cdf_timestamp_t t)
{
struct tm tm;
#ifdef HAVE_STRUCT_TM_TM_ZONE
@@ -104,8 +104,9 @@
#endif
int rdays;
- /* Unit is 100's of nanoseconds */
- ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
+ /* XXX 5.14 at least introdced 100 ns intervals, this is to do */
+ /* Time interval, in microseconds */
+ ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC;
t /= CDF_TIME_PREC;
tm.tm_sec = (int)(t % 60);
@@ -117,7 +118,7 @@
tm.tm_hour = (int)(t % 24);
t /= 24;
- // XXX: Approx
+ /* XXX: Approx */
tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
rdays = cdf_getdays(tm.tm_year);