summaryrefslogtreecommitdiff
path: root/ext/fileinfo/libmagic.patch
blob: 522bbcc71b8def5e179fdead914d2a1fb420c86e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
--- libmagic.orig/apprentice.c	2018-03-11 01:46:42.000000000 +0100
+++ libmagic/apprentice.c	2019-04-15 10:57:47.115181746 +0200
@@ -2,7 +2,7 @@
  * 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:
@@ -12,7 +12,7 @@
  * 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
@@ -29,6 +29,8 @@
  * apprentice - make one pass through /etc/magic, learning its secrets.
  */
 
+#include "php.h"
+
 #include "file.h"
 
 #ifndef	lint
@@ -36,24 +38,27 @@
 #endif	/* lint */
 
 #include "magic.h"
+#include "patchlevel.h"
 #include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+
+#if defined(__hpux) && !defined(HAVE_STRTOULL)
+#if SIZEOF_LONG == 8
+# define strtoull strtoul
+#else
+# define strtoull __strtoull
 #endif
-#ifdef HAVE_STDDEF_H
-#include <stddef.h>
+#endif
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#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>
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
-#endif
 
 #ifndef SSIZE_MAX
 #define MAXMAGIC_SIZE        ((ssize_t)0x7fffffff)
@@ -75,6 +80,10 @@
 #endif
 #endif
 
+#ifndef offsetof
+#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
+#endif
+
 #ifndef MAP_FAILED
 #define MAP_FAILED (void *) -1
 #endif
@@ -91,7 +100,7 @@
 #define MAP_TYPE_MMAP	2
 
 struct magic_entry {
-	struct magic *mp;	
+	struct magic *mp;
 	uint32_t cont_count;
 	uint32_t max_count;
 };
@@ -126,7 +135,7 @@
 private size_t apprentice_magic_strength(const struct magic *);
 private int apprentice_sort(const void *, const void *);
 private void apprentice_list(struct mlist *, int );
-private struct magic_map *apprentice_load(struct magic_set *, 
+private struct magic_map *apprentice_load(struct magic_set *,
     const char *, int);
 private struct mlist *mlist_alloc(void);
 private void mlist_free(struct mlist *);
@@ -136,10 +145,7 @@
 private uint32_t swap4(uint32_t);
 private uint64_t swap8(uint64_t);
 private char *mkdbname(struct magic_set *, const char *, int);
-private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
-    size_t);
 private struct magic_map *apprentice_map(struct magic_set *, const char *);
-private int check_buffer(struct magic_set *, struct magic_map *, const char *);
 private void apprentice_unmap(struct magic_map *);
 private int apprentice_compile(struct magic_set *, struct magic_map *,
     const char *);
@@ -170,38 +176,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];
@@ -409,7 +384,7 @@
 	struct mlist *ml;
 
 	mlp->map = NULL;
-	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;
@@ -430,10 +405,8 @@
 apprentice_1(struct magic_set *ms, const char *fn, int action)
 {
 	struct magic_map *map;
-#ifndef COMPILE_ONLY
 	struct mlist *ml;
 	size_t i;
-#endif
 
 	if (magicsize != FILE_MAGICSIZE) {
 		file_error(ms, 0, "magic element size %lu != %lu",
@@ -449,14 +422,15 @@
 		return apprentice_compile(ms, map, fn);
 	}
 
-#ifndef COMPILE_ONLY
 	map = apprentice_map(ms, fn);
 	if (map == (struct magic_map *)-1)
 		return -1;
 	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;
 	}
@@ -478,9 +452,6 @@
 		}
 	}
 	return 0;
-#else
-	return 0;
-#endif /* COMPILE_ONLY */
 }
 
 protected void
@@ -491,10 +462,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 *
@@ -503,7 +480,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;
 
@@ -515,7 +492,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;
@@ -533,48 +510,35 @@
 	ms->bytes_max = FILE_BYTES_MAX;
 	return ms;
 free:
-	free(ms);
+	efree(ms);
 	return NULL;
 }
 
 private void
 apprentice_unmap(struct magic_map *map)
 {
-	size_t i;
 	if (map == NULL)
 		return;
-
-	switch (map->type) {
-	case MAP_TYPE_USER:
-		break;
-	case MAP_TYPE_MALLOC:
-		for (i = 0; i < MAGIC_SETS; i++) {
-			void *b = map->magic[i];
-			void *p = map->p;
-			if (CAST(char *, b) >= CAST(char *, p) &&
-			    CAST(char *, b) <= CAST(char *, p) + map->len)
-				continue;
-			free(map->magic[i]);
+	if (map->p != php_magic_database) {
+		if (map->p == NULL) {
+			int j;
+			for (j = 0; j < MAGIC_SETS; j++) {
+				if (map->magic[j]) {
+					efree(map->magic[j]);
+				}
+			}
+		} else {
+			efree(map->p);
 		}
-		free(map->p);
-		break;
-#ifdef QUICK
-	case MAP_TYPE_MMAP:
-		if (map->p && map->p != MAP_FAILED)
-			(void)munmap(map->p, map->len);
-		break;
-#endif
-	default:
-		abort();
 	}
-	free(map);
+	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;
@@ -593,60 +557,12 @@
 	for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
 		if (ml->map)
 			apprentice_unmap(CAST(struct magic_map *, ml->map));
-		free(ml);
+		efree(ml);
 		if (ml == mlist)
 			break;
 	}
 }
 
-#ifndef COMPILE_ONLY
-/* void **bufs: an array of compiled magic files */
-protected int
-buffer_apprentice(struct magic_set *ms, struct magic **bufs,
-    size_t *sizes, size_t nbufs)
-{
-	size_t i, j;
-	struct mlist *ml;
-	struct magic_map *map;
-
-	if (nbufs == 0)
-		return -1;
-
-	(void)file_reset(ms, 0);
-
-	init_file_tables();
-
-	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]));
-			goto fail;
-		}
-	}
-
-	for (i = 0; i < nbufs; i++) {
-		map = apprentice_buf(ms, bufs[i], sizes[i]);
-		if (map == NULL)
-			goto fail;
-
-		for (j = 0; j < MAGIC_SETS; j++) {
-			if (add_mlist(ms->mlist[j], map, j) == -1) {
-				file_oomem(ms, sizeof(*ml));
-				goto fail;
-			}
-		}
-	}
-
-	return 0;
-fail:
-	for (i = 0; i < MAGIC_SETS; i++) {
-		mlist_free(ms->mlist[i]);
-		ms->mlist[i] = NULL;
-	}
-	return -1;
-}
-#endif
-
 /* const char *fn: list of magic files and directories */
 protected int
 file_apprentice(struct magic_set *ms, const char *fn, int action)
@@ -655,14 +571,31 @@
 	int fileerr, errs = -1;
 	size_t i;
 
-	(void)file_reset(ms, 0);
+	if (ms->mlist[0] != NULL)
+		(void)file_reset(ms, 0);
 
+/* 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;
 	}
@@ -675,7 +608,7 @@
 				mlist_free(ms->mlist[i]);
 				ms->mlist[i] = NULL;
 			}
-			free(mfn);
+			efree(mfn);
 			return -1;
 		}
 	}
@@ -692,7 +625,7 @@
 		fn = p;
 	}
 
-	free(mfn);
+	efree(mfn);
 
 	if (errs == -1) {
 		for (i = 0; i < MAGIC_SETS; i++) {
@@ -974,7 +907,7 @@
 	return val;
 }
 
-/*  
+/*
  * Sort callback for sorting entries by "strength" (basically length)
  */
 private int
@@ -992,7 +925,7 @@
 		return 1;
 }
 
-/*  
+/*
  * Shows sorted patterns list in the order which is used for the matching
  */
 private void
@@ -1088,7 +1021,7 @@
 			mstart->flag |= BINTEST;
 		if (mstart->str_flags & STRING_TEXTTEST)
 			mstart->flag |= TEXTTEST;
-		    
+
 		if (mstart->flag & (TEXTTEST|BINTEST))
 			break;
 
@@ -1120,7 +1053,7 @@
 
 		mset[i].max += ALLOC_INCR;
 		if ((mp = CAST(struct magic_entry *,
-		    realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
+		    erealloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
 		    NULL) {
 			file_oomem(ms, sizeof(*mp) * mset[i].max);
 			return -1;
@@ -1141,13 +1074,19 @@
 load_1(struct magic_set *ms, int action, const char *fn, int *errs,
    struct magic_entry_set *mset)
 {
-	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;
+
+
+	ms->file = fn;
+	stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
+
+	if (stream == NULL) {
 		if (errno != ENOENT)
 			file_error(ms, errno, "cannot read magic file `%s'",
 				   fn);
@@ -1157,8 +1096,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') {
@@ -1216,8 +1154,8 @@
 	}
 	if (me.mp)
 		(void)addentry(ms, &me, mset);
-	free(line);
-	(void)fclose(f);
+    efree(line);
+	php_stream_close(stream);
 }
 
 /*
@@ -1280,7 +1218,7 @@
 				file_magwarn(ms,
 				    "level 0 \"default\" did not sort last");
 			}
-			return;					    
+			return;
 		}
 	}
 }
@@ -1296,7 +1234,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;
 	}
@@ -1318,8 +1256,8 @@
 	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 *
@@ -1328,18 +1266,19 @@
 	int errs = 0;
 	uint32_t i, j;
 	size_t files = 0, maxfiles = 0;
-	char **filearr = NULL, *mfn;
-	struct stat st;
+	char **filearr = NULL;
+	zend_stat_t st;
 	struct magic_map *map;
 	struct magic_entry_set mset[MAGIC_SETS];
-	DIR *dir;
-	struct dirent *d;
+	php_stream *dir;
+	php_stream_dirent d;
+
 
 	memset(mset, 0, sizeof(mset));
 	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;
@@ -1351,24 +1290,26 @@
 		(void)fprintf(stderr, "%s\n", usg_hdr);
 
 	/* 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 (d->d_name[0] == '.')
-				continue;
-			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);
+			if (zend_stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
 				continue;
 			}
 			if (files >= maxfiles) {
@@ -1376,23 +1317,22 @@
 				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, mset);
-			free(filearr[i]);
+			efree(filearr[i]);
 		}
-		free(filearr);
+		efree(filearr);
 	} else
 		load_1(ms, action, fn, &errs, mset);
 	if (errs)
@@ -1833,7 +1773,7 @@
 	 */
 	while (*l == '>') {
 		++l;		/* step over */
-		cont_level++; 
+		cont_level++;
 	}
 #ifdef ENABLE_CONDITIONALS
 	if (cont_level == 0 || cont_level > last_cont_level)
@@ -1859,7 +1799,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;
@@ -1874,7 +1814,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;
 		}
@@ -1916,17 +1856,6 @@
 			file_magwarn(ms, "offset `%s' invalid", l);
 		return -1;
 	}
-#if 0
-        if (m->offset < 0 && cont_level != 0 &&
-	    (m->flag & (OFFADD | INDIROFFADD)) == 0) {
-		if (ms->flags & MAGIC_CHECK) {
-			file_magwarn(ms,
-			    "negative direct offset `%s' at level %u",
-			    l, cont_level);
-		}
-		return -1;
-	}
-#endif
         l = t;
 
 	if (m->flag & INDIR) {
@@ -2012,7 +1941,7 @@
 			}
 			l = t;
 		}
-		if (*l++ != ')' || 
+		if (*l++ != ')' ||
 		    ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) {
 			if (ms->flags & MAGIC_CHECK)
 				file_magwarn(ms,
@@ -2037,7 +1966,7 @@
 		/*
 		 * Try it as a keyword type prefixed by "u"; match what
 		 * follows the "u".  If that fails, try it as an SUS
-		 * integer type. 
+		 * integer type.
 		 */
 		m->type = get_type(type_tbl, l + 1, &l);
 		if (m->type == FILE_INVALID) {
@@ -2077,7 +2006,7 @@
 		/* Not found - try it as a special keyword. */
 		m->type = get_type(special_tbl, l, &l);
 	}
-			
+
 	if (m->type == FILE_INVALID) {
 		if (ms->flags & MAGIC_CHECK)
 			file_magwarn(ms, "type `%s' invalid", l);
@@ -2089,7 +2018,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");
@@ -2098,7 +2027,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)) {
 			int r;
 
 			if (op != FILE_OPDIVIDE) {
@@ -2124,7 +2053,7 @@
 	 * anything if mask = 0 (unless you have a better idea)
 	 */
 	EATAB;
-  
+
 	switch (*l) {
 	case '>':
 	case '<':
@@ -2156,7 +2085,7 @@
 		break;
 	default:
   		m->reln = '=';	/* the default relation */
-		if (*l == 'x' && ((isascii((unsigned char)l[1]) && 
+		if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
 		    isspace((unsigned char)l[1])) || !l[1])) {
 			m->reln = *l;
 			++l;
@@ -2171,7 +2100,7 @@
 
 	/*
 	 * TODO finish this macro and start using it!
-	 * #define offsetcheck {if (offset > ms->bytes_max -1) 
+	 * #define offsetcheck {if (offset > ms->bytes_max -1)
 	 *	magwarn("offset too big"); }
 	 */
 
@@ -2203,11 +2132,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;
 }
@@ -2279,7 +2203,7 @@
 
 private int
 parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
-    off_t off, size_t len, const char *name, const char *extra, int nt)
+    zend_off_t off, size_t len, const char *name, const char *extra, int nt)
 {
 	size_t i;
 	const char *l = line;
@@ -2291,7 +2215,7 @@
 		file_magwarn(ms, "Current entry already has a %s type "
 		    "`%.*s', new type `%s'", name, (int)len, buf, l);
 		return -1;
-	}	
+	}
 
 	if (*m->desc == '\0') {
 		file_magwarn(ms, "Current entry does not yet have a "
@@ -2361,7 +2285,7 @@
 	struct magic *m = &me->mp[0];
 
 	return parse_extra(ms, me, line,
-	    CAST(off_t, offsetof(struct magic, mimetype)),
+	    CAST(zend_off_t, offsetof(struct magic, mimetype)),
 	    sizeof(m->mimetype), "MIME", "+-/.$?:{}", 1);
 }
 
@@ -2428,7 +2352,7 @@
 			if (*ptr++ != 'l')
 				goto invalid;
 		}
-	
+
 		switch (*ptr++) {
 #ifdef STRICT_FORMAT 	/* "long" formats are int formats for us */
 		/* so don't accept the 'l' modifier */
@@ -2446,7 +2370,7 @@
 			default:
 				goto invalid;
 			}
-		
+
 		/*
 		 * Don't accept h and hh modifiers. They make writing
 		 * magic entries more complicated, for very little benefit
@@ -2502,7 +2426,7 @@
 		default:
 			goto invalid;
 		}
-		
+
 	case FILE_FMT_FLOAT:
 	case FILE_FMT_DOUBLE:
 		if (*ptr == '-')
@@ -2521,11 +2445,11 @@
 		case 'g':
 		case 'G':
 			return 0;
-			
+
 		default:
 			goto invalid;
 		}
-		
+
 
 	case FILE_FMT_STR:
 		if (*ptr == '-')
@@ -2537,14 +2461,14 @@
 			while (isdigit((unsigned char )*ptr))
 				ptr++;
 		}
-		
+
 		switch (*ptr++) {
 		case 's':
 			return 0;
 		default:
 			goto invalid;
 		}
-		
+
 	default:
 		/* internal error */
 		abort();
@@ -2555,7 +2479,7 @@
 	*estr = "too long";
 	return -1;
 }
-	
+
 /*
  * Check that the optional printf format in description matches
  * the type of the magic.
@@ -2578,7 +2502,7 @@
 
 	if (m->type >= file_nformats) {
 		file_magwarn(ms, "Internal error inconsistency between "
-		    "m->type and format strings");		
+		    "m->type and format strings");
 		return -1;
 	}
 	if (file_formats[m->type] == FILE_FMT_NONE) {
@@ -2598,7 +2522,7 @@
 		    file_names[m->type], m->desc);
 		return -1;
 	}
-	
+
 	for (; *ptr; ptr++) {
 		if (*ptr == '%') {
 			file_magwarn(ms,
@@ -2611,9 +2535,9 @@
 	return 0;
 }
 
-/* 
- * Read a numeric value from a pointer, into the value union of a magic 
- * pointer, according to the magic type.  Update the string pointer to point 
+/*
+ * Read a numeric value from a pointer, into the value union of a magic
+ * pointer, according to the magic type.  Update the string pointer to point
  * just after the number read.  Return 0 for success, non-zero for failure.
  */
 private int
@@ -2640,14 +2564,19 @@
 			return -1;
 		}
 		if (m->type == FILE_REGEX) {
-			file_regex_t rx;
-			int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
-			if (rc) {
-				if (ms->flags & MAGIC_CHECK)
-					file_regerror(&rx, rc, ms);
+			zval pattern;
+			int options = 0;
+			pcre_cache_entry *pce;
+
+			convert_libmagic_pattern(&pattern, m->value.s, strlen(m->value.s), options);
+
+			if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
+				zval_dtor(&pattern);
+				return -1;
 			}
-			file_regfree(&rx);
-			return rc ? -1 : 0;
+			zval_dtor(&pattern);
+
+			return 0;
 		}
 		return 0;
 	default:
@@ -2770,7 +2699,7 @@
 			default:
 				if (warn) {
 					if (isprint((unsigned char)c)) {
-						/* Allow escaping of 
+						/* Allow escaping of
 						 * ``relations'' */
 						if (strchr("<>&^=!", c) == NULL
 						    && (m->type != FILE_REGEX ||
@@ -2975,7 +2904,7 @@
 {
 	const char *l = *p;
 
-	if (LOWCASE(*l) == 'u') 
+	if (LOWCASE(*l) == 'u')
 		l++;
 
 	switch (LOWCASE(*l)) {
@@ -2993,6 +2922,7 @@
 	*p = l;
 }
 
+#if 0
 /*
  * handle a buffer containing a compiled file.
  */
@@ -3001,7 +2931,7 @@
 {
 	struct magic_map *map;
 
-	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;
 	}
@@ -3014,6 +2944,7 @@
 	}
 	return map;
 }
+#endif
 
 /*
  * handle a compiled file.
@@ -3022,81 +2953,148 @@
 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 = 0, nentries;
+	int needsbyteswap;
 	char *dbname = NULL;
 	struct magic_map *map;
-	struct magic_map *rv = NULL;
+	size_t i;
+	php_stream *stream = NULL;
+	php_stream_statbuf st;
 
-	fd = -1;
-	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));
-		goto error;
+		return NULL;
+	}
+
+	if (fn == NULL) {
+		map->p = (void *)&php_magic_database;
+		goto internal_loaded;
 	}
-	map->type = MAP_TYPE_USER;	/* unspecified */
+
+#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) {
+                       return NULL;
+               }
+       }
+#endif
 
 	dbname = mkdbname(ms, fn, 0);
 	if (dbname == NULL)
 		goto error;
 
-	if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
+	stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
+
+	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 || st.st_size > MAXMAGIC_SIZE) {
+	if (st.sb.st_size < 8 || st.sb.st_size > MAXMAGIC_SIZE) {
 		file_error(ms, 0, "file `%s' is too %s", dbname,
-		    st.st_size < 8 ? "small" : "large");
+		    st.sb.st_size < 8 ? "small" : "large");
 		goto error;
 	}
 
-	map->len = (size_t)st.st_size;
-#ifdef QUICK
-	map->type = MAP_TYPE_MMAP;
-	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
-	map->type = MAP_TYPE_MALLOC;
-	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;
 
-	if (check_buffer(ms, map, dbname) != 0) {
-		rv = (struct magic_map *)-1;
+	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);
+			goto error;
+		}
+		needsbyteswap = 1;
+	} else
+		needsbyteswap = 0;
+	if (needsbyteswap)
+		version = swap4(ptr[1]);
+	else
+		version = ptr[1];
+	if (version != VERSIONNO) {
+		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;
 	}
-#ifdef QUICK
-	if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) {
-		file_error(ms, errno, "cannot mprotect `%s'", dbname);
+
+	/* 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 ((zend_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;
+	for (i = 0; i < MAGIC_SETS; i++) {
+		if (needsbyteswap)
+			map->nmagic[i] = swap4(ptr[i + 2]);
+		else
+			map->nmagic[i] = ptr[i + 2];
+		if (i != MAGIC_SETS - 1)
+			map->magic[i + 1] = map->magic[i] + map->nmagic[i];
+		nentries += map->nmagic[i];
+	}
+	if (NULL != fn && entries != nentries + 1) {
+		file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
+		    dbname, entries, nentries + 1);
 		goto error;
 	}
-#endif
+	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);
-	return rv;
+	if (dbname) {
+		efree(dbname);
+	}
+	return NULL;
 }
 
+#if 0
 private int
 check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
 {
@@ -3120,7 +3118,7 @@
 		version = ptr[1];
 	if (version != VERSIONNO) {
 		file_error(ms, 0, "File %s supports only version %d magic "
-		    "files. `%s' is version %d", VERSION,
+		    "files. `%s' is version %d", FILE_VERSION_MAJOR,
 		    VERSIONNO, dbname, version);
 		return -1;
 	}
@@ -3152,6 +3150,7 @@
 			byteswap(map->magic[i], map->nmagic[i]);
 	return 0;
 }
+#endif
 
 /*
  * handle an mmaped file.
@@ -3161,7 +3160,6 @@
 {
 	static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
 	static const size_t m = sizeof(**map->magic);
-	int fd = -1;
 	size_t len;
 	char *dbname;
 	int rv = -1;
@@ -3170,14 +3168,17 @@
 		struct magic m;
 		uint32_t h[2 + MAGIC_SETS];
 	} hdr;
+	php_stream *stream;
 
 	dbname = mkdbname(ms, fn, 1);
 
-	if (dbname == NULL) 
+	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 */
+	stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL);
+
+	if (!stream) {
 		file_error(ms, errno, "cannot open `%s'", dbname);
 		goto out;
 	}
@@ -3186,26 +3187,25 @@
 	hdr.h[1] = VERSIONNO;
 	memcpy(hdr.h + 2, map->nmagic, nm);
 
-	if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
+	if (php_stream_write(stream,(const char *)&hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
 		file_error(ms, errno, "error writing `%s'", dbname);
-		goto out2;
+		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 out2;
+			goto out;
 		}
 	}
 
+	if (stream) {
+		php_stream_close(stream);
+	}
 	rv = 0;
-out2:
-	if (fd != -1)
-		(void)close(fd);
 out:
-	apprentice_unmap(map);
-	free(dbname);
+	efree(dbname);
 	return rv;
 }
 
@@ -3239,16 +3239,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(fn, ".mime") != NULL)
@@ -3274,8 +3276,8 @@
 swap2(uint16_t sv)
 {
 	uint16_t rv;
-	uint8_t *s = (uint8_t *)(void *)&sv; 
-	uint8_t *d = (uint8_t *)(void *)&rv; 
+	uint8_t *s = (uint8_t *)(void *)&sv;
+	uint8_t *d = (uint8_t *)(void *)&rv;
 	d[0] = s[1];
 	d[1] = s[0];
 	return rv;
@@ -3288,8 +3290,8 @@
 swap4(uint32_t sv)
 {
 	uint32_t rv;
-	uint8_t *s = (uint8_t *)(void *)&sv; 
-	uint8_t *d = (uint8_t *)(void *)&rv; 
+	uint8_t *s = (uint8_t *)(void *)&sv;
+	uint8_t *d = (uint8_t *)(void *)&rv;
 	d[0] = s[3];
 	d[1] = s[2];
 	d[2] = s[1];
@@ -3304,8 +3306,8 @@
 swap8(uint64_t sv)
 {
 	uint64_t rv;
-	uint8_t *s = (uint8_t *)(void *)&sv; 
-	uint8_t *d = (uint8_t *)(void *)&rv; 
+	uint8_t *s = (uint8_t *)(void *)&sv;
+	uint8_t *d = (uint8_t *)(void *)&rv;
 #if 0
 	d[0] = s[3];
 	d[1] = s[2];
@@ -3338,7 +3340,7 @@
 	m->offset = swap4((int32_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);
 	}
@@ -3348,7 +3350,7 @@
 	}
 }
 
-protected size_t 
+protected size_t
 file_pstring_length_size(const struct magic *m)
 {
 	switch (m->str_flags & PSTRING_LEN) {
diff -u libmagic.orig/apptype.c libmagic/apptype.c
--- libmagic.orig/apptype.c	2011-09-07 23:57:15.000000000 +0200
+++ libmagic/apptype.c	2019-03-08 09:31:16.392796494 +0100
@@ -1,15 +1,15 @@
 /*
  * Adapted from: apptype.c, Written by Eberhard Mattes and put into the
  * public domain
- * 
+ *
  * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
  * searches.
- * 
+ *
  * 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com"
  * (other than an OS/2 exe or Win exe with this name). Eberhard Mattes
  * remarks Tue, 6 Apr 93: Moreover, it reports the type of the (new and very
  * bug ridden) Win Emacs as "OS/2 executable".
- * 
+ *
  * 3. apptype() uses the filename if given, otherwise a tmp file is created with
  * the contents of buf. If buf is not the complete file, apptype can
  * incorrectly identify the exe type. The "-z" option of "file" is the reason
@@ -18,10 +18,10 @@
 
 /*
  * amai: Darrel Hankerson did the changes described here.
- * 
+ *
  * It remains to check the validity of comments (2.) since it's referred to an
  * "old" OS/2 version.
- * 
+ *
  */
 
 #include "file.h"
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
--- libmagic.orig/ascmagic.c	2017-11-02 21:25:39.000000000 +0100
+++ libmagic/ascmagic.c	2019-03-08 09:31:16.392796494 +0100
@@ -90,7 +90,7 @@
 		rv = file_ascmagic_with_encoding(ms, &bb,
 		    ubuf, ulen, code, type, text);
 
-	free(ubuf);
+	efree(ubuf);
 
 	return rv;
 }
@@ -137,7 +137,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;
 		}
@@ -305,7 +305,8 @@
 	}
 	rv = 1;
 done:
-	free(utf8_buf);
+	if (utf8_buf)
+		efree(utf8_buf);
 
 	return rv;
 }
diff -u libmagic.orig/buffer.c libmagic/buffer.c
--- libmagic.orig/buffer.c	2018-03-11 01:46:42.000000000 +0100
+++ libmagic/buffer.c	2019-03-08 09:31:16.392796494 +0100
@@ -31,7 +31,11 @@
 #endif	/* lint */
 
 #include "magic.h"
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
 #include <unistd.h>
+#endif
 #include <string.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -40,7 +44,7 @@
 buffer_init(struct buffer *b, int fd, const void *data, size_t len)
 {
 	b->fd = fd;
-	if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
+	if (b->fd == -1 || zend_fstat(b->fd, &b->st) == -1)
 		memset(&b->st, 0, sizeof(b->st));
 	b->fbuf = data;
 	b->flen = len;
@@ -52,7 +56,7 @@
 void
 buffer_fini(struct buffer *b)
 {
-	free(b->ebuf);
+	efree(b->ebuf);
 }
 
 int
@@ -68,12 +72,14 @@
 
 	b->elen =  (size_t)b->st.st_size < b->flen ?
 	    (size_t)b->st.st_size : b->flen;
-	if ((b->ebuf = malloc(b->elen)) == NULL)
+	if ((b->ebuf = emalloc(b->elen)) == NULL)
 		goto out;
 
 	b->eoff = b->st.st_size - b->elen;
-	if (pread(b->fd, b->ebuf, b->elen, b->eoff) == -1) {
-		free(b->ebuf);
+	if (FINFO_LSEEK_FUNC(b->fd, b->eoff, SEEK_SET) == (zend_off_t)-1 ||
+		FINFO_READ_FUNC(b->fd, b->ebuf, b->elen) != (ssize_t)b->elen)
+	{
+		efree(b->ebuf);
 		goto out;
 	}
 
diff -u libmagic.orig/cdf.c libmagic/cdf.c
--- libmagic.orig/cdf.c	2018-03-11 01:46:42.000000000 +0100
+++ libmagic/cdf.c	2019-04-12 12:02:54.279893504 +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>
@@ -80,37 +90,9 @@
 			    CDF_TOLE8(CAST(uint64_t, x))))
 #define CDF_GETUINT32(x, y)	cdf_getuint32(x, y)
 
-#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
-#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
-#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
-
-
-/*ARGSUSED*/
-static void *
-cdf_malloc(const char *file __attribute__((__unused__)),
-    size_t line __attribute__((__unused__)), size_t n)
-{
-	DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
-	return malloc(n);
-}
-
-/*ARGSUSED*/
-static void *
-cdf_realloc(const char *file __attribute__((__unused__)),
-    size_t line __attribute__((__unused__)), void *p, size_t n)
-{
-	DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
-	return realloc(p, n);
-}
-
-/*ARGSUSED*/
-static void *
-cdf_calloc(const char *file __attribute__((__unused__)),
-    size_t line __attribute__((__unused__)), size_t n, size_t u)
-{
-	DPRINTF(("%s,%zu: %s %zu %zu\n", file, line, __func__, n, u));
-	return calloc(n, u);
-}
+#define CDF_MALLOC(n) emalloc(n)
+#define CDF_REALLOC(p, n) erealloc(p, n)
+#define CDF_CALLOC(n, u) ecalloc(n, u)
 
 /*
  * swap a short
@@ -303,7 +285,7 @@
 	scn->sst_len = 0;
 	scn->sst_dirlen = 0;
 	scn->sst_ss = 0;
-	free(scn->sst_tab);
+	efree(scn->sst_tab);
 	scn->sst_tab = NULL;
 	return -1;
 }
@@ -311,9 +293,11 @@
 static size_t
 cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h)
 {
+#ifndef NDEBUG
 	size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
 	    CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
 	assert(ss == sst->sst_ss);
+#endif
 	return sst->sst_ss;
 }
 
@@ -336,12 +320,13 @@
 }
 
 static ssize_t
-cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
+cdf_read(const cdf_info_t *info, zend_off_t off, void *buf, size_t len)
 {
 	size_t siz = (size_t)off + len;
 
-	if ((off_t)(off + len) != (off_t)siz)
+	if ((zend_off_t)(off + len) != (zend_off_t)siz) {
 		goto out;
+	}
 
 	if (info->i_buf != NULL && info->i_len >= siz) {
 		(void)memcpy(buf, &info->i_buf[off], len);
@@ -351,7 +336,10 @@
 	if (info->i_fd == -1)
 		goto out;
 
-	if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
+	if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (zend_off_t)-1)
+		return -1;
+
+	if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
 		return -1;
 
 	return (ssize_t)len;
@@ -366,7 +354,7 @@
 	char buf[512];
 
 	(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
-	if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
+	if (cdf_read(info, (zend_off_t)0, buf, sizeof(buf)) == -1)
 		return -1;
 	cdf_unpack_header(h, buf);
 	cdf_swap_header(h);
@@ -397,19 +385,17 @@
 cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
     const cdf_header_t *h, cdf_secid_t id)
 {
-	size_t ss = CDF_SEC_SIZE(h);
 	size_t pos = CDF_SEC_POS(h, id);
-	assert(ss == len);
-	return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len);
+	assert(CDF_SEC_SIZE(h) == len);
+	return cdf_read(info, (zend_off_t)pos, ((char *)buf) + offs, len);
 }
 
 ssize_t
 cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
     size_t len, const cdf_header_t *h, cdf_secid_t id)
 {
-	size_t ss = CDF_SHORT_SEC_SIZE(h);
 	size_t pos = CDF_SHORT_SEC_POS(h, id);
-	assert(ss == len);
+	assert(CDF_SHORT_SEC_SIZE(h) == len);
 	if (pos + len > CDF_SEC_SIZE(h) * sst->sst_len) {
 		DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
 		    SIZE_T_FORMAT "u\n",
@@ -501,14 +487,14 @@
 	}
 out:
 	sat->sat_len = i;
-	free(msa);
+	efree(msa);
 	return 0;
 out3:
 	errno = EFTYPE;
 out2:
-	free(msa);
+	efree(msa);
 out1:
-	free(sat->sat_tab);
+	efree(sat->sat_tab);
 	return -1;
 }
 
@@ -676,7 +662,7 @@
 		return -1;
 
 	if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
-		free(dir->dir_tab);
+		efree(dir->dir_tab);
 		return -1;
 	}
 
@@ -698,11 +684,11 @@
 	if (NEED_SWAP)
 		for (i = 0; i < dir->dir_len; i++)
 			cdf_swap_dir(&dir->dir_tab[i]);
-	free(buf);
+	efree(buf);
 	return 0;
 out:
-	free(dir->dir_tab);
-	free(buf);
+	efree(dir->dir_tab);
+	efree(buf);
 	errno = EFTYPE;
 	return -1;
 }
@@ -747,7 +733,7 @@
 out:
 	errno = EFTYPE;
 out1:
-	free(ssat->sat_tab);
+	efree(ssat->sat_tab);
 	return -1;
 }
 
@@ -859,7 +845,7 @@
 }
 
 static const uint8_t *
-cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h, 
+cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
     const uint8_t *p, const uint8_t *e, size_t i)
 {
 	size_t tail = (i << 1) + 1;
@@ -874,7 +860,7 @@
 	    __LINE__) == -1)
 		return NULL;
 	ofs = CDF_GETUINT32(p, tail);
-	q = CAST(const uint8_t *, cdf_offset(CAST(const void *, p), 
+	q = CAST(const uint8_t *, cdf_offset(CAST(const void *, p),
 	    ofs - 2 * sizeof(uint32_t)));
 
 	if (q < p) {
@@ -896,7 +882,7 @@
 	size_t newcount = *maxcount + incr;
 
 	if (newcount > CDF_PROP_LIMIT) {
-		DPRINTF(("exceeded property limit %zu > %zu\n", 
+		DPRINTF(("exceeded property limit %zu > %zu\n",
 		    newcount, CDF_PROP_LIMIT));
 		goto out;
 	}
@@ -909,7 +895,7 @@
 	*maxcount = newcount;
 	return inp;
 out:
-	free(*info);
+	efree(*info);
 	*maxcount = 0;
 	*info = NULL;
 	return NULL;
@@ -1092,7 +1078,7 @@
 	}
 	return 0;
 out:
-	free(*info);
+	efree(*info);
 	*info = NULL;
 	*count = 0;
 	*maxcount = 0;
@@ -1383,7 +1369,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" };
@@ -1425,7 +1411,7 @@
 				break;
 			}
 			cdf_dump_stream(&scn);
-			free(scn.sst_tab);
+			efree(scn.sst_tab);
 			break;
 		default:
 			break;
@@ -1438,7 +1424,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;
 
@@ -1523,7 +1509,7 @@
 	(void)fprintf(stderr, "Class %s\n", buf);
 	(void)fprintf(stderr, "Count %d\n", ssi.si_count);
 	cdf_dump_property_info(info, count);
-	free(info);
+	efree(info);
 }
 
 
@@ -1544,7 +1530,7 @@
 		    cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
 		    cdf_ctime(&ts.tv_sec, tbuf));
 	}
-	free(cat);
+	efree(cat);
 }
 
 #endif
diff -u libmagic.orig/cdf.h libmagic/cdf.h
--- libmagic.orig/cdf.h	2017-03-09 17:57:17.000000000 +0100
+++ libmagic/cdf.h	2019-03-08 09:31:16.392796494 +0100
@@ -35,10 +35,10 @@
 #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
@@ -272,7 +272,7 @@
 typedef struct {
 	uint16_t ce_namlen;
 	uint32_t ce_num;
-	uint64_t ce_timestamp; 
+	uint64_t ce_timestamp;
 	uint16_t ce_name[256];
 } cdf_catalog_entry_t;
 
diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
--- libmagic.orig/cdf_time.c	2017-03-29 17:57:48.000000000 +0200
+++ libmagic/cdf_time.c	2019-03-22 11:57:57.251021738 +0100
@@ -23,6 +23,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+#include "php.h"
 
 #include "file.h"
 
@@ -56,7 +57,7 @@
 
 	for (y = CDF_BASE_YEAR; y < year; y++)
 		days += isleap(y) + 365;
-		
+
 	return days;
 }
 
@@ -77,7 +78,7 @@
 	return days;
 }
 
-/* 
+/*
  * Return the 0...11 month number.
  */
 static int
@@ -152,7 +153,7 @@
 #endif
 #ifdef notyet
 	struct tm tm;
-	if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
+	if (php_gmtime_r(&ts->ts_sec, &tm) == NULL) {
 		errno = EINVAL;
 		return -1;
 	}
@@ -168,7 +169,7 @@
 char *
 cdf_ctime(const time_t *sec, char *buf)
 {
-	char *ptr = ctime_r(sec, buf);
+	char *ptr = php_ctime_r(sec, buf);
 	if (ptr != NULL)
 		return buf;
 	(void)snprintf(buf, 26, "*Bad* %#16.16" INT64_T_FORMAT "x\n",
diff -u libmagic.orig/compress.c libmagic/compress.c
--- libmagic.orig/compress.c	2017-11-02 21:25:39.000000000 +0100
+++ libmagic/compress.c	2019-04-12 10:04:15.721646341 +0200
@@ -2,7 +2,7 @@
  * 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:
@@ -12,7 +12,7 @@
  * 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
@@ -29,13 +29,13 @@
  * compress routines:
  *	zmagic() - returns 0 if not recognized, uncompresses and prints
  *		   information if recognized
- *	uncompress(method, old, n, newch) - uncompress old into new, 
+ *	uncompress(method, old, n, newch) - uncompress old into new,
  *					    using method, return sizeof new
  */
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: compress.c,v 1.106 2017/11/02 20:25:39 christos Exp $")
+FILE_RCSID("@(#)$File: compress.c,v 1.104 2017/03/29 15:57:48 christos Exp $")
 #endif
 
 #include "magic.h"
@@ -45,15 +45,13 @@
 #endif
 #include <string.h>
 #include <errno.h>
-#include <ctype.h>
-#include <stdarg.h>
 #ifdef HAVE_SIGNAL_H
 #include <signal.h>
 # ifndef HAVE_SIG_T
 typedef void (*sig_t)(int);
 # endif /* HAVE_SIG_T */
-#endif 
-#if !defined(__MINGW32__) && !defined(WIN32)
+#endif
+#ifndef PHP_WIN32
 #include <sys/ioctl.h>
 #endif
 #ifdef HAVE_SYS_WAIT_H
@@ -62,56 +60,18 @@
 #if defined(HAVE_SYS_TIME_H)
 #include <sys/time.h>
 #endif
-#if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
+#if defined(HAVE_ZLIB_H) && defined(PHP_FILEINFO_UNCOMPRESS)
 #define BUILTIN_DECOMPRESS
 #include <zlib.h>
 #endif
-#ifdef DEBUG
-int tty = -1;
-#define DPRINTF(...)	do { \
-	if (tty == -1) \
-		tty = open("/dev/tty", O_RDWR); \
-	if (tty == -1) \
-		abort(); \
-	dprintf(tty, __VA_ARGS__); \
-} while (/*CONSTCOND*/0)
-#else
-#define DPRINTF(...)
-#endif
-
-#ifdef ZLIBSUPPORT
-/*
- * The following python code is not really used because ZLIBSUPPORT is only
- * defined if we have a built-in zlib, and the built-in zlib handles that.
- * That is not true for android where we have zlib.h and not -lz.
- */
-static const char zlibcode[] =
-    "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
-
-static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
 
-static int
-zlibcmp(const unsigned char *buf)
-{
-	unsigned short x = 1;
-	unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
-
-	if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
-		return 0;
-	if (s[0] != 1)	/* endianness test */
-		x = buf[0] | (buf[1] << 8);
-	else
-		x = buf[1] | (buf[0] << 8);
-	if (x % 31)
-		return 0;
-	return 1;
-}
-#endif
+#undef FIONREAD
 
 #define gzip_flags "-cd"
 #define lrzip_flags "-do"
 #define lzip_flags gzip_flags
 
+#ifdef PHP_FILEINFO_UNCOMPRESS
 static const char *gzip_args[] = {
 	"gzip", gzip_flags, NULL
 };
@@ -163,13 +123,14 @@
 	{ RCAST(const void *, zlibcmp),	0, zlib_args },		/* zlib */
 #endif
 };
+#endif
 
 #define OKDATA 	0
 #define NODATA	1
 #define ERRDATA	2
 
 private ssize_t swrite(int, const void *, size_t);
-#if HAVE_FORK
+#ifdef PHP_FILEINFO_UNCOMPRESS
 private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
 private int uncompressbuf(int, size_t, size_t, const unsigned char *,
     unsigned char **, size_t *);
@@ -179,12 +140,12 @@
 private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
     size_t *);
 #endif
-static int makeerror(unsigned char **, size_t *, const char *, ...)
-    __attribute__((__format__(__printf__, 3, 4)));
+static int makeerror(unsigned char **, size_t *, const char *, ...);
 private const char *methodname(size_t);
 
 protected int
-file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
+file_zmagic(struct magic_set *ms, int fd, const char *name,
+    const unsigned char *buf, size_t nbytes)
 {
 	unsigned char *newbuf = NULL;
 	size_t i, nsz;
@@ -192,9 +153,6 @@
 	file_pushbuf_t *pb;
 	int urv, prv, rv = 0;
 	int mime = ms->flags & MAGIC_MIME;
-	int fd = b->fd;
-	const unsigned char *buf = b->fbuf;
-	size_t nbytes = b->flen;
 #ifdef HAVE_SIGNAL_H
 	sig_t osigpipe;
 #endif
@@ -226,7 +184,7 @@
 		switch (urv) {
 		case OKDATA:
 		case ERRDATA:
-			
+
 			ms->flags &= ~MAGIC_COMPRESS;
 			if (urv == ERRDATA)
 				prv = file_printf(ms, "%s ERROR: %s",
@@ -253,10 +211,10 @@
 				goto error;
 			if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
 				if (file_printf(ms, "%s", rbuf) == -1) {
-					free(rbuf);
+					efree(rbuf);
 					goto error;
 				}
-				free(rbuf);
+				efree(rbuf);
 			}
 			if (!mime && file_printf(ms, ")") == -1)
 				goto error;
@@ -277,7 +235,8 @@
 #ifdef HAVE_SIGNAL_H
 	(void)signal(SIGPIPE, osigpipe);
 #endif
-	free(newbuf);
+	if (newbuf)
+		efree(newbuf);
 	ms->flags |= MAGIC_COMPRESS;
 	DPRINTF("Zmagic returns %d\n", rv);
 	return rv;
@@ -312,7 +271,7 @@
  * `safe' read for sockets and pipes.
  */
 protected ssize_t
-sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
+sread(int fd, void *buf, size_t n, int canbepipe)
 {
 	ssize_t rv;
 #ifdef FIONREAD
@@ -360,7 +319,7 @@
 
 nocheck:
 	do
-		switch ((rv = read(fd, buf, n))) {
+		switch ((rv = FINFO_READ_FUNC(fd, buf, n))) {
 		case -1:
 			if (errno == EINTR)
 				continue;
@@ -437,13 +396,14 @@
 		return -1;
 	}
 	(void)close(tfd);
-	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
+	if (FINFO_LSEEK_FUNC(fd, (zend_off_t)0, SEEK_SET) == (zend_off_t)-1) {
 		file_badseek(ms);
 		return -1;
 	}
 	return fd;
 }
-#if HAVE_FORK
+
+#ifdef PHP_FILEINFO_UNCOMPRESS
 #ifdef BUILTIN_DECOMPRESS
 
 #define FHCRC		(1 << 1)
@@ -494,7 +454,7 @@
 	int rc;
 	z_stream z;
 
-	if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL) 
+	if ((*newch = CAST(unsigned char *, emalloc(bytes_max + 1))) == NULL)
 		return makeerror(newch, n, "No buffer, %s", strerror(errno));
 
 	z.next_in = CCAST(Bytef *, old);
@@ -518,7 +478,7 @@
 	rc = inflateEnd(&z);
 	if (rc != Z_OK)
 		goto err;
-	
+
 	/* let's keep the nul-terminate tradition */
 	(*newch)[*n] = '\0';
 
@@ -586,7 +546,7 @@
 	int status;
 
 	closefd(fdp[STDIN_FILENO], 0);
-	/* 
+	/*
 	 * fork again, to avoid blocking because both
 	 * pipes filled
 	 */
@@ -689,13 +649,13 @@
 			fdp[STDIN_FILENO][0] = fd;
 			(void) lseek(fd, (off_t)0, SEEK_SET);
 		}
-		
+
 		for (i = 0; i < __arraycount(fdp); i++)
 			copydesc(CAST(int, i), fdp[i]);
 
 		(void)execvp(compr[method].argv[0],
 		    (char *const *)(intptr_t)compr[method].argv);
-		dprintf(STDERR_FILENO, "exec `%s' failed, %s", 
+		dprintf(STDERR_FILENO, "exec `%s' failed, %s",
 		    compr[method].argv[0], strerror(errno));
 		exit(1);
 		/*NOTREACHED*/
@@ -711,7 +671,7 @@
 		if (fd == -1)
 			writechild(fdp, old, *n);
 
-		*newch = CAST(unsigned char *, malloc(bytes_max + 1));
+		*newch = CAST(unsigned char *, emalloc(bytes_max + 1));
 		if (*newch == NULL) {
 			rv = makeerror(newch, n, "No buffer, %s",
 			    strerror(errno));
@@ -730,7 +690,7 @@
 			r = filter_error(*newch, r);
 			break;
 		}
-		free(*newch);
+		efree(*newch);
 		if  (r == 0)
 			rv = makeerror(newch, n, "Read failed, %s",
 			    strerror(errno));
@@ -738,27 +698,5 @@
 			rv = makeerror(newch, n, "No data");
 		goto err;
 	}
-
-	*n = r;
-	/* NUL terminate, as every buffer is handled here. */
-	(*newch)[*n] = '\0';
-err:
-	closefd(fdp[STDIN_FILENO], 1);
-	closefd(fdp[STDOUT_FILENO], 0);
-	closefd(fdp[STDERR_FILENO], 0);
-	if (wait(&status) == -1) {
-		free(*newch);
-		rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
-		DPRINTF("Child wait return %#x\n", status);
-	} else if (!WIFEXITED(status)) {
-		DPRINTF("Child not exited (%#x)\n", status);
-	} else if (WEXITSTATUS(status) != 0) {
-		DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
-	}
-
-	closefd(fdp[STDIN_FILENO], 0);
-	DPRINTF("Returning %p n=%zu rv=%d\n", *newch, *n, rv);
-    
-	return rv;
 }
-#endif
+#endif /* if PHP_FILEINFO_UNCOMPRESS */
diff -u libmagic.orig/der.c libmagic/der.c
--- libmagic.orig/der.c	2017-02-10 19:14:01.000000000 +0100
+++ libmagic/der.c	2019-03-08 09:31:16.392796494 +0100
@@ -51,7 +51,9 @@
 #include "magic.h"
 #include "der.h"
 #else
+#ifndef PHP_WIN32
 #include <sys/mman.h>
+#endif
 #include <sys/stat.h>
 #include <err.h>
 #endif
@@ -207,7 +209,7 @@
 static const char *
 der_tag(char *buf, size_t len, uint32_t tag)
 {
-	if (tag < DER_TAG_LONG) 
+	if (tag < DER_TAG_LONG)
 		strlcpy(buf, der__tag[tag], len);
 	else
 		snprintf(buf, len, "%#x", tag);
@@ -218,6 +220,7 @@
 static int
 der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
 {
+	uint32_t i = 0;
 	const uint8_t *d = CAST(const uint8_t *, q);
 	switch (tag) {
 	case DER_TAG_PRINTABLE_STRING:
@@ -229,7 +232,7 @@
 		break;
 	}
 		
-	for (uint32_t i = 0; i < len; i++) {
+	for (; i < len; i++) {
 		uint32_t z = i << 1;
 		if (z < blen - 2)
 			snprintf(buf + z, blen - z, "%.2x", d[i]);
@@ -343,7 +346,7 @@
 	default:
 		break;
 	}
-		
+
 	for (uint32_t i = 0; i < len; i++)
 		printf("%.2x", d[i]);
 	printf("\n");
@@ -367,7 +370,7 @@
 		if (p + x >= ep)
 			break;
 		uint32_t len = getlength(p, &x, ep - p + x);
-		
+
 		printf("%zu %zu-%zu %c,%c,%s,%u:", level, ox, x,
 		    der_class[c], der_type[t],
 		    der_tag(buf, sizeof(buf), tag), len);
diff -u libmagic.orig/elfclass.h libmagic/elfclass.h
--- libmagic.orig/elfclass.h	2014-12-17 00:18:40.000000000 +0100
+++ libmagic/elfclass.h	2019-03-08 09:31:16.392796494 +0100
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) Christos Zoulas 2008.
  * All Rights Reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -11,7 +11,7 @@
  * 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
@@ -41,7 +41,7 @@
 			return toomany(ms, "program headers", phnum);
 		flags |= FLAGS_IS_CORE;
 		if (dophn_core(ms, clazz, swap, fd,
-		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
+		    (zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
 		    fsize, &flags, &notecount) == -1)
 			return -1;
@@ -56,7 +56,7 @@
 		if (shnum > ms->elf_shnum_max)
 			return toomany(ms, "section", shnum);
 		if (dophn_exec(ms, clazz, swap, fd,
-		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
+		    (zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
 		    fsize, shnum, &flags, &notecount) == -1)
 			return -1;
@@ -66,7 +66,7 @@
 		if (shnum > ms->elf_shnum_max)
 			return toomany(ms, "section headers", shnum);
 		if (doshn(ms, clazz, swap, fd,
-		    (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
+		    (zend_off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
 		    (size_t)elf_getu16(swap, elfhdr.e_shentsize),
 		    fsize, elf_getu16(swap, elfhdr.e_machine),
 		    (int)elf_getu16(swap, elfhdr.e_shstrndx),
diff -u libmagic.orig/encoding.c libmagic/encoding.c
--- libmagic.orig/encoding.c	2017-11-02 21:25:39.000000000 +0100
+++ libmagic/encoding.c	2019-03-08 09:31:16.392796494 +0100
@@ -88,12 +88,12 @@
 	*code_mime = "binary";
 
 	mlen = (nbytes + 1) * sizeof((*ubuf)[0]);
-	if ((*ubuf = CAST(unichar *, calloc((size_t)1, mlen))) == NULL) {
+	if ((*ubuf = CAST(unichar *, ecalloc((size_t)1, mlen))) == NULL) {
 		file_oomem(ms, mlen);
 		goto done;
 	}
 	mlen = (nbytes + 1) * sizeof(nbuf[0]);
-	if ((nbuf = CAST(unsigned char *, calloc((size_t)1, mlen))) == NULL) {
+	if ((nbuf = CAST(unsigned char *, ecalloc((size_t)1, mlen))) == NULL) {
 		file_oomem(ms, mlen);
 		goto done;
 	}
@@ -153,9 +153,9 @@
 	}
 
  done:
-	free(nbuf);
+	efree(nbuf);
 	if (ubuf == &udefbuf)
-		free(udefbuf);
+		efree(udefbuf);
 
 	return rv;
 }
diff -u libmagic.orig/file.h libmagic/file.h
--- libmagic.orig/file.h	2018-03-11 01:46:42.000000000 +0100
+++ libmagic/file.h	2019-03-08 09:31:16.392796494 +0100
@@ -27,21 +27,15 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.191 2018/02/21 21:26:00 christos Exp $
+ * @(#)$File: file.h,v 1.182 2017/04/07 19:46:44 christos Exp $
  */
 
 #ifndef __file_h__
 #define __file_h__
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#ifdef HAVE_STDINT_H
-#ifndef __STDC_LIMIT_MACROS
-#define __STDC_LIMIT_MACROS
-#endif
+#include "config.h"
 
-#ifdef WIN32
+#ifdef PHP_WIN32
   #ifdef _WIN64
     #define SIZE_T_FORMAT "I64"
   #else
@@ -54,19 +48,31 @@
   #define INT64_T_FORMAT "ll"
   #define INTMAX_T_FORMAT "j"
 #endif
-#include <stdint.h>
-#endif
 
 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
 #include <errno.h>
 #include <fcntl.h>	/* For open and flags */
+#ifdef HAVE_STDINT_H
+#ifndef __STDC_LIMIT_MACROS
+#define __STDC_LIMIT_MACROS
+#endif
+#include <stdint.h>
+#endif
 #ifdef HAVE_INTTYPES_H
 #include <inttypes.h>
 #endif
-#include <regex.h>
-#include <time.h>
+#ifdef PHP_WIN32
+#include "win32/php_stdint.h"
+#endif
+
+#include "php.h"
+#include "ext/standard/php_string.h"
+#include "ext/pcre/php_pcre.h"
+
 #include <sys/types.h>
-#ifndef WIN32
+#ifdef PHP_WIN32
+#include "win32/param.h"
+#else
 #include <sys/param.h>
 #endif
 /* Do this here and now, because struct stat gets re-defined on solaris */
@@ -79,7 +85,7 @@
 #define MAGIC "/etc/magic"
 #endif
 
-#if defined(__EMX__) || defined (WIN32)
+#if defined(__EMX__) || defined(PHP_WIN32)
 #define PATHSEP	';'
 #else
 #define PATHSEP	':'
@@ -113,12 +119,6 @@
 #endif
 #endif
 
-#ifndef __GNUC__
-#ifndef __attribute__
-#define __attribute__(a)
-#endif
-#endif
-
 #ifndef MIN
 #define	MIN(a,b)	(((a) < (b)) ? (a) : (b))
 #endif
@@ -147,10 +147,10 @@
 
 struct buffer {
 	int fd;
-	struct stat st;
+	zend_stat_t st;
 	const void *fbuf;
 	size_t flen;
-	off_t eoff;
+	zend_off_t eoff;
 	void *ebuf;
 	size_t elen;
 };
@@ -240,7 +240,7 @@
 #define				FILE_DER	48
 #define				FILE_NAMES_SIZE	49 /* size of array to contain all names */
 
-#define IS_STRING(t) \
+#define IS_LIBMAGIC_STRING(t) \
 	((t) == FILE_STRING || \
 	 (t) == FILE_PSTRING || \
 	 (t) == FILE_BESTRING16 || \
@@ -443,26 +443,22 @@
 /* Type for Unicode characters */
 typedef unsigned long unichar;
 
-struct stat;
 #define FILE_T_LOCAL	1
 #define FILE_T_WINDOWS	2
 protected const char *file_fmttime(uint64_t, int, char *);
 protected struct magic_set *file_ms_alloc(int);
 protected void file_ms_free(struct magic_set *);
-protected int file_buffer(struct magic_set *, int, const char *, const void *,
+protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *,
     size_t);
-protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
+protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *, php_stream *);
 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
-protected int file_vprintf(struct magic_set *, const char *, va_list)
-    __attribute__((__format__(__printf__, 2, 0)));
 protected size_t file_printedlen(const struct magic_set *);
 protected int file_replace(struct magic_set *, const char *, const char *);
-protected int file_printf(struct magic_set *, const char *, ...)
-    __attribute__((__format__(__printf__, 2, 3)));
+protected int file_printf(struct magic_set *, const char *, ...);
 protected int file_reset(struct magic_set *, int);
 protected int file_tryelf(struct magic_set *, const struct buffer *);
 protected int file_trycdf(struct magic_set *, const struct buffer *);
-#if HAVE_FORK
+#ifdef PHP_FILEINFO_UNCOMPRESS
 protected int file_zmagic(struct magic_set *, const struct buffer *,
     const char *);
 #endif
@@ -484,13 +480,9 @@
 protected void file_badread(struct magic_set *);
 protected void file_badseek(struct magic_set *);
 protected void file_oomem(struct magic_set *, size_t);
-protected void file_error(struct magic_set *, int, const char *, ...)
-    __attribute__((__format__(__printf__, 3, 4)));
-protected void file_magerror(struct magic_set *, const char *, ...)
-    __attribute__((__format__(__printf__, 2, 3)));
-protected void file_magwarn(struct magic_set *, const char *, ...)
-    __attribute__((__format__(__printf__, 2, 3)));
-protected void file_mdump(struct magic *);
+protected void file_error(struct magic_set *, int, const char *, ...);
+protected void file_magerror(struct magic_set *, const char *, ...);
+protected void file_magwarn(struct magic_set *, const char *, ...);
 protected void file_showstr(FILE *, const char *, size_t);
 protected size_t file_mbswidth(const char *);
 protected const char *file_getbuffer(struct magic_set *);
@@ -510,31 +502,8 @@
 protected void buffer_fini(struct buffer *);
 protected int buffer_fill(const struct buffer *);
 
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
-#if defined(HAVE_XLOCALE_H)
-#include <xlocale.h>
-#endif
-
-typedef struct {
-	const char *pat;
-#if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
-#define USE_C_LOCALE
-	locale_t old_lc_ctype;
-	locale_t c_lc_ctype;
-#else
-	char *old_lc_ctype;
-#endif
-	int rc;
-	regex_t rx;
-} file_regex_t;
-
-protected int file_regcomp(file_regex_t *, const char *, int);
-protected int file_regexec(file_regex_t *, const char *, size_t, regmatch_t *,
-    int);
-protected void file_regfree(file_regex_t *);
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
+public void
+convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options);
 
 typedef struct {
 	char *buf;
@@ -544,10 +513,8 @@
 protected file_pushbuf_t *file_push_buffer(struct magic_set *);
 protected char  *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
 
-#ifndef COMPILE_ONLY
 extern const char *file_names[];
 extern const size_t file_nnames;
-#endif
 
 #ifndef HAVE_STRERROR
 extern int sys_nerr;
@@ -560,23 +527,10 @@
 #define strtoul(a, b, c)	strtol(a, b, c)
 #endif
 
-#ifndef HAVE_PREAD
-ssize_t pread(int, void *, size_t, off_t);
-#endif
-#ifndef HAVE_VASPRINTF
-int vasprintf(char **, const char *, va_list);
-#endif
-#ifndef HAVE_ASPRINTF
-int asprintf(char **, const char *, ...);
-#endif
-#ifndef HAVE_DPRINTF
-int dprintf(int, const char *, ...);
-#endif
-
-#ifndef HAVE_STRLCPY
+#ifndef strlcpy
 size_t strlcpy(char *, const char *, size_t);
 #endif
-#ifndef HAVE_STRLCAT
+#ifndef strlcat
 size_t strlcat(char *, const char *, size_t);
 #endif
 #ifndef HAVE_STRCASESTR
@@ -592,39 +546,6 @@
 #ifndef HAVE_ASCTIME_R
 char   *asctime_r(const struct tm *, char *);
 #endif
-#ifndef HAVE_GMTIME_R
-struct tm *gmtime_r(const time_t *, struct tm *);
-#endif
-#ifndef HAVE_LOCALTIME_R
-struct tm *localtime_r(const time_t *, struct tm *);
-#endif
-#ifndef HAVE_FMTCHECK
-const char *fmtcheck(const char *, const char *) 
-     __attribute__((__format_arg__(2)));
-#endif
-
-#ifdef HAVE_LIBSECCOMP
-// basic filter 
-// this mode should not interfere with normal operations
-// only some dangerous syscalls are blacklisted
-int enable_sandbox_basic(void);
-
-// enhanced filter 
-// this mode allows only the necessary syscalls used during normal operation
-// extensive testing required !!!
-int enable_sandbox_full(void);
-#endif
-
-protected const char *file_getprogname(void);
-protected void file_setprogname(const char *);
-protected void file_err(int, const char *, ...)
-    __attribute__((__format__(__printf__, 2, 3)));
-protected void file_errx(int, const char *, ...)
-    __attribute__((__format__(__printf__, 2, 3)));
-protected void file_warn(const char *, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
-protected void file_warnx(const char *, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
 
 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
 #define QUICK
@@ -647,6 +568,18 @@
 #else
 #define FILE_RCSID(id)
 #endif
+
+#ifdef PHP_WIN32
+#ifdef _WIN64
+#define FINFO_LSEEK_FUNC _lseeki64
+#else
+#define FINFO_LSEEK_FUNC _lseek
+#endif
+#define FINFO_READ_FUNC _read
+#else
+#define FINFO_LSEEK_FUNC lseek
+#define FINFO_READ_FUNC read
+#endif
 #ifndef __RCSID
 #define __RCSID(a)
 #endif
diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
--- libmagic.orig/fsmagic.c	2017-05-24 21:17:50.000000000 +0200
+++ libmagic/fsmagic.c	2019-03-08 09:31:16.392796494 +0100
@@ -2,7 +2,7 @@
  * 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:
@@ -12,7 +12,7 @@
  * 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
@@ -63,26 +63,10 @@
 # define minor(dev)  ((dev) & 0xff)
 #endif
 #undef HAVE_MAJOR
-#ifdef	S_IFLNK
-private int
-bad_link(struct magic_set *ms, int err, char *buf)
-{
-	int mime = ms->flags & MAGIC_MIME;
-	if ((mime & MAGIC_MIME_TYPE) &&
-	    file_printf(ms, "inode/symlink")
-	    == -1)
-		return -1;
-	else if (!mime) {
-		if (ms->flags & MAGIC_ERROR) {
-			file_error(ms, err,
-				   "broken symbolic link to %s", buf);
-			return -1;
-		} 
-		if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
-			return -1;
-	}
-	return 1;
-}
+
+#ifdef PHP_WIN32
+
+# undef S_IFIFO
 #endif
 private int
 handle_mime(struct magic_set *ms, int mime, const char *str)
@@ -100,70 +84,39 @@
 }
 
 protected int
-file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
+file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb, php_stream *stream)
 {
 	int ret, did = 0;
 	int mime = ms->flags & MAGIC_MIME;
 	int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
-#ifdef	S_IFLNK
-	char buf[BUFSIZ+4];
-	ssize_t nch;
-	struct stat tstatbuf;
-#endif
 
-	if (fn == NULL)
+	if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
+		return 0;
+
+	if (fn == NULL && !stream) {
 		return 0;
+	}
 
 #define COMMA	(did++ ? ", " : "")
-	/*
-	 * Fstat is cheaper but fails for files you don't have read perms on.
-	 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
-	 */
-#ifdef	S_IFLNK
-	if ((ms->flags & MAGIC_SYMLINK) == 0)
-		ret = lstat(fn, sb);
-	else
-#endif
-	ret = stat(fn, sb);	/* don't merge into if; see "ret =" above */
 
-#ifdef WIN32
-	{
-		HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
-		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
-		    NULL);
-		if (hFile != INVALID_HANDLE_VALUE) {
-			/*
-			 * Stat failed, but we can still open it - assume it's
-			 * a block device, if nothing else.
-			 */
-			if (ret) {
-				sb->st_mode = S_IFBLK;
-				ret = 0;
-			}
-			switch (GetFileType(hFile)) {
-			case FILE_TYPE_CHAR:
-				sb->st_mode |= S_IFCHR;
-				sb->st_mode &= ~S_IFREG;
-				break;
-			case FILE_TYPE_PIPE:
-				sb->st_mode |= S_IFIFO;
-				sb->st_mode &= ~S_IFREG;
-				break;
+	if (stream) {
+		php_stream_statbuf ssb;
+		if (php_stream_stat(stream, &ssb) < 0) {
+			if (ms->flags & MAGIC_ERROR) {
+				file_error(ms, errno, "cannot stat `%s'", fn);
+				return -1;
 			}
-			CloseHandle(hFile);
+			return 0;
 		}
-	}
-#endif
-
-	if (ret) {
-		if (ms->flags & MAGIC_ERROR) {
-			file_error(ms, errno, "cannot stat `%s'", fn);
-			return -1;
+		memcpy(sb, &ssb.sb, sizeof(struct stat));
+	} else {
+		if (php_sys_stat(fn, sb) != 0) {
+			if (ms->flags & MAGIC_ERROR) {
+				file_error(ms, errno, "cannot stat `%s'", fn);
+				return -1;
+			}
+			return 0;
 		}
-		if (file_printf(ms, "cannot open `%s' (%s)",
-		    fn, strerror(errno)) == -1)
-			return -1;
-		return 0;
 	}
 
 	ret = 1;
@@ -174,44 +127,36 @@
 				return -1;
 #endif
 #ifdef S_ISGID
-		if (sb->st_mode & S_ISGID) 
+		if (sb->st_mode & S_ISGID)
 			if (file_printf(ms, "%ssetgid", COMMA) == -1)
 				return -1;
 #endif
 #ifdef S_ISVTX
-		if (sb->st_mode & S_ISVTX) 
+		if (sb->st_mode & S_ISVTX)
 			if (file_printf(ms, "%ssticky", COMMA) == -1)
 				return -1;
 #endif
 	}
-	
+
 	switch (sb->st_mode & S_IFMT) {
-	case S_IFDIR:
-		if (mime) {
-			if (handle_mime(ms, mime, "directory") == -1)
-				return -1;
-		} else if (silent) {
-		} else if (file_printf(ms, "%sdirectory", COMMA) == -1)
-			return -1;
-		break;
-#ifdef S_IFCHR
-	case S_IFCHR:
-		/* 
-		 * If -s has been specified, treat character special files
-		 * like ordinary files.  Otherwise, just report that they
-		 * are block special files and go on to the next file.
-		 */
-		if ((ms->flags & MAGIC_DEVICES) != 0) {
-			ret = 0;
-			break;
-		}
-		if (mime) {
-			if (handle_mime(ms, mime, "chardevice") == -1)
-				return -1;
-		} else if (silent) {
-		} else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
+#ifndef PHP_WIN32
+# ifdef S_IFCHR
+		case S_IFCHR:
+			/*
+			 * If -s has been specified, treat character special files
+			 * like ordinary files.  Otherwise, just report that they
+			 * are block special files and go on to the next file.
+			 */
+			if ((ms->flags & MAGIC_DEVICES) != 0) {
+				ret = 0;
+				break;
+			}
+			if (mime) {
+				if (handle_mime(ms, mime, "chardevice") == -1)
+					return -1;
+			} else {
+#  ifdef HAVE_STAT_ST_RDEV
+#   ifdef dv_unit
 			if (file_printf(ms, "%scharacter special (%d/%d/%d)",
 			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
 					dv_subunit(sb->st_rdev)) == -1)
@@ -226,45 +171,11 @@
 			if (file_printf(ms, "%scharacter special", COMMA) == -1)
 				return -1;
 #endif
-		}
-		break;
-#endif
-#ifdef S_IFBLK
-	case S_IFBLK:
-		/* 
-		 * If -s has been specified, treat block special files
-		 * like ordinary files.  Otherwise, just report that they
-		 * are block special files and go on to the next file.
-		 */
-		if ((ms->flags & MAGIC_DEVICES) != 0) {
-			ret = 0;
-			break;
-		}
-		if (mime) {
-			if (handle_mime(ms, mime, "blockdevice") == -1)
-				return -1;
-		} else if (silent) {
-		} else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
-			if (file_printf(ms, "%sblock special (%d/%d/%d)",
-			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
-			    dv_subunit(sb->st_rdev)) == -1)
-				return -1;
-# else
-			if (file_printf(ms, "%sblock special (%ld/%ld)",
-			    COMMA, (long)major(sb->st_rdev),
-			    (long)minor(sb->st_rdev)) == -1)
-				return -1;
+	}
+			return 1;
 # endif
-#else
-			if (file_printf(ms, "%sblock special", COMMA) == -1)
-				return -1;
 #endif
-		}
-		break;
-#endif
-	/* TODO add code to handle V7 MUX and Blit MUX files */
+
 #ifdef	S_IFIFO
 	case S_IFIFO:
 		if((ms->flags & MAGIC_DEVICES) != 0)
@@ -272,7 +183,6 @@
 		if (mime) {
 			if (handle_mime(ms, mime, "fifo") == -1)
 				return -1;
-		} else if (silent) {
 		} else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
 			return -1;
 		break;
@@ -282,89 +192,20 @@
 		if (mime) {
 			if (handle_mime(ms, mime, "door") == -1)
 				return -1;
-		} else if (silent) {
 		} else if (file_printf(ms, "%sdoor", COMMA) == -1)
 			return -1;
 		break;
 #endif
 #ifdef	S_IFLNK
 	case S_IFLNK:
-		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
+		/* stat is used, if it made here then the link is broken */
 			if (ms->flags & MAGIC_ERROR) {
-			    file_error(ms, errno, "unreadable symlink `%s'",
-				fn);
+			    file_error(ms, errno, "unreadable symlink `%s'", fn);
 			    return -1;
 			}
-			if (mime) {
-				if (handle_mime(ms, mime, "symlink") == -1)
-					return -1;
-			} else if (silent) {
-			} else if (file_printf(ms,
-			    "%sunreadable symlink `%s' (%s)", COMMA, fn,
-			    strerror(errno)) == -1)
-				return -1;
-			break;
-		}
-		buf[nch] = '\0';	/* readlink(2) does not do this */
-
-		/* If broken symlink, say so and quit early. */
-		if (*buf == '/') {
-			if (stat(buf, &tstatbuf) < 0)
-				return bad_link(ms, errno, buf);
-		} else {
-			char *tmp;
-			char buf2[BUFSIZ+BUFSIZ+4];
-
-			if ((tmp = strrchr(fn,  '/')) == NULL) {
-				tmp = buf; /* in current directory anyway */
-			} else {
-				if (tmp - fn + 1 > BUFSIZ) {
-					if (ms->flags & MAGIC_ERROR) {
-						file_error(ms, 0, 
-						    "path too long: `%s'", buf);
-						return -1;
-					}
-					if (mime) {
-						if (handle_mime(ms, mime,
-						    "x-path-too-long") == -1)
-							return -1;
-					} else if (silent) {
-					} else if (file_printf(ms,
-					    "%spath too long: `%s'", COMMA,
-					    fn) == -1)
-						return -1;
-					break;
-				}
-				/* take dir part */
-				(void)strlcpy(buf2, fn, sizeof buf2);
-				buf2[tmp - fn + 1] = '\0';
-				/* plus (rel) link */
-				(void)strlcat(buf2, buf, sizeof buf2);
-				tmp = buf2;
-			}
-			if (stat(tmp, &tstatbuf) < 0)
-				return bad_link(ms, errno, buf);
-		}
-
-		/* Otherwise, handle it. */
-		if ((ms->flags & MAGIC_SYMLINK) != 0) {
-			const char *p;
-			ms->flags &= MAGIC_SYMLINK;
-			p = magic_file(ms, buf);
-			ms->flags |= MAGIC_SYMLINK;
-			if (p == NULL)
-				return -1;
-		} else { /* just print what it points to */
-			if (mime) {
-				if (handle_mime(ms, mime, "symlink") == -1)
-					return -1;
-			} else if (silent) {
-			} else if (file_printf(ms, "%ssymbolic link to %s",
-			    COMMA, buf) == -1)
-				return -1;
-		}
-		break;
+	return 1;
 #endif
+
 #ifdef	S_IFSOCK
 #ifndef __COHERENT__
 	case S_IFSOCK:
diff -u libmagic.orig/funcs.c libmagic/funcs.c
--- libmagic.orig/funcs.c	2017-11-02 21:25:39.000000000 +0100
+++ libmagic/funcs.c	2019-03-25 16:13:59.574598917 +0100
@@ -31,7 +31,6 @@
 #endif	/* lint */
 
 #include "magic.h"
-#include <assert.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -42,78 +41,76 @@
 #if defined(HAVE_WCTYPE_H)
 #include <wctype.h>
 #endif
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
+#if defined(HAVE_LOCALE_H)
+#include <locale.h>
 #endif
 
 #ifndef SIZE_MAX
 #define SIZE_MAX	((size_t)~0)
 #endif
 
-/*
- * Like printf, only we append to a buffer.
- */
-protected int
-file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
-{
-	int len;
-	char *buf, *newstr;
+#include "php.h"
+#include "main/php_network.h"
 
-	if (ms->event_flags & EVENT_HAD_ERR)
-		return 0;
-	len = vasprintf(&buf, fmt, ap);
-	if (len < 0)
-		goto out;
-
-	if (ms->o.buf != NULL) {
-		len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
-		free(buf);
-		if (len < 0)
-			goto out;
-		free(ms->o.buf);
-		buf = newstr;
-	}
-	ms->o.buf = buf;
-	return 0;
-out:
-	fprintf(stderr, "vasprintf failed (%s)", strerror(errno));
-	return -1;
-}
+#ifndef PREG_OFFSET_CAPTURE
+# define PREG_OFFSET_CAPTURE                 (1<<8)
+#endif
 
 protected int
 file_printf(struct magic_set *ms, const char *fmt, ...)
 {
-	int rv;
 	va_list ap;
+	char *buf = NULL, *newstr;
 
 	va_start(ap, fmt);
-	rv = file_vprintf(ms, fmt, ap);
+	vspprintf(&buf, 0, fmt, ap);
 	va_end(ap);
-	return rv;
+
+	if (ms->o.buf != NULL) {
+		spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
+		if (buf) {
+			efree(buf);
+		}
+		efree(ms->o.buf);
+		ms->o.buf = newstr;
+	} else {
+		ms->o.buf = buf;
+	}
+	return 0;
 }
 
 /*
  * error - print best error message possible
  */
 /*VARARGS*/
-__attribute__((__format__(__printf__, 3, 0)))
 private void
 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
     size_t lineno)
 {
+	char *buf = NULL;
+
 	/* Only the first error is ok */
 	if (ms->event_flags & EVENT_HAD_ERR)
 		return;
 	if (lineno != 0) {
-		free(ms->o.buf);
+		efree(ms->o.buf);
 		ms->o.buf = NULL;
 		file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
 	}
-	if (ms->o.buf && *ms->o.buf)
-		file_printf(ms, " ");
-	file_vprintf(ms, f, va);
-	if (error > 0)
-		file_printf(ms, " (%s)", strerror(error));
+
+	vspprintf(&buf, 0, f, va);
+	va_end(va);
+
+	if (error > 0) {
+		file_printf(ms, "%s (%s)", (*buf ? buf : ""), strerror(error));
+	} else if (*buf) {
+		file_printf(ms, "%s", buf);
+	}
+
+	if (buf) {
+		efree(buf);
+	}
+
 	ms->event_flags |= EVENT_HAD_ERR;
 	ms->error = error;
 }
@@ -160,7 +157,6 @@
 	file_error(ms, errno, "error reading");
 }
 
-#ifndef COMPILE_ONLY
 
 static int
 checkdone(struct magic_set *ms, int *rv)
@@ -174,8 +170,8 @@
 
 /*ARGSUSED*/
 protected int
-file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
-    const void *buf, size_t nb)
+file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const void *buf,
+    size_t nb)
 {
 	int m = 0, rv = 0, looks_text = 0;
 	const char *code = NULL;
@@ -184,7 +180,8 @@
 	const char *def = "data";
 	const char *ftype = NULL;
 	struct buffer b;
-
+	int fd = -1;
+	
 	buffer_init(&b, fd, buf, nb);
 
 	if (nb == 0) {
@@ -216,8 +213,8 @@
 		}
 	}
 #endif
-#if HAVE_FORK
-	/* try compression stuff */
+
+#if PHP_FILEINFO_UNCOMPRESS
 	if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
 		m = file_zmagic(ms, &b, inname);
 		if ((ms->flags & MAGIC_DEBUG) != 0)
@@ -239,13 +236,22 @@
 	}
 
 	/* Check if we have a CDF file */
-	if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
-		m = file_trycdf(ms, &b);
-		if ((ms->flags & MAGIC_DEBUG) != 0)
-			(void)fprintf(stderr, "[try cdf %d]\n", m);
-		if (m) {
-			if (checkdone(ms, &rv))
-				goto done;
+	if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0 && stream) {
+#ifdef _WIN64
+		php_socket_t _fd = fd;
+		int _ret = php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&_fd, 0);
+		fd = (int)_fd;
+#else
+		int _ret = php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, 0);
+#endif
+		if (SUCCESS == _ret) {
+			m = file_trycdf(ms, &b);
+			if ((ms->flags & MAGIC_DEBUG) != 0)
+				(void)fprintf(stderr, "[try cdf %d]\n", m);
+			if (m) {
+				if (checkdone(ms, &rv))
+					goto done;
+			}
 		}
 	}
 
@@ -315,7 +321,7 @@
 		if (file_printf(ms, "%s", code_mime) == -1)
 			rv = -1;
 	}
-#if HAVE_FORK
+#if PHP_FILEINFO_UNCOMPRESS
  done_encoding:
 #endif
 	buffer_fini(&b);
@@ -324,7 +330,6 @@
 
 	return m;
 }
-#endif
 
 protected int
 file_reset(struct magic_set *ms, int checkloaded)
@@ -334,11 +339,11 @@
 		return -1;
 	}
 	if (ms->o.buf) {
-		free(ms->o.buf);
+		efree(ms->o.buf);
 		ms->o.buf = NULL;
 	}
 	if (ms->o.pbuf) {
-		free(ms->o.pbuf);
+		efree(ms->o.pbuf);
 		ms->o.pbuf = NULL;
 	}
 	ms->event_flags &= ~EVENT_HAD_ERR;
@@ -376,7 +381,7 @@
 		return NULL;
 	}
 	psize = len * 4 + 1;
-	if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
+	if ((pbuf = CAST(char *, erealloc(ms->o.pbuf, psize))) == NULL) {
 		file_oomem(ms, psize);
 		return NULL;
 	}
@@ -440,8 +445,8 @@
 	if (level >= ms->c.len) {
 		len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
 		ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
-		    malloc(len) :
-		    realloc(ms->c.li, len));
+		    emalloc(len) :
+		    erealloc(ms->c.li, len));
 		if (ms->c.li == NULL) {
 			file_oomem(ms, len);
 			return -1;
@@ -464,76 +469,41 @@
 protected int
 file_replace(struct magic_set *ms, const char *pat, const char *rep)
 {
-	file_regex_t rx;
-	int rc, rv = -1;
-
-	rc = file_regcomp(&rx, pat, REG_EXTENDED);
-	if (rc) {
-		file_regerror(&rx, rc, ms);
-	} else {
-		regmatch_t rm;
-		int nm = 0;
-		while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
-			ms->o.buf[rm.rm_so] = '\0';
-			if (file_printf(ms, "%s%s", rep,
-			    rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
-				goto out;
-			nm++;
-		}
-		rv = nm;
+	zval patt;
+	uint32_t opts = 0;
+	pcre_cache_entry *pce;
+	zend_string *res;
+	zend_string *repl;
+	size_t rep_cnt = 0;
+
+	(void)setlocale(LC_CTYPE, "C");
+
+	opts |= PCRE2_MULTILINE;
+	convert_libmagic_pattern(&patt, (char*)pat, strlen(pat), opts);
+	if ((pce = pcre_get_compiled_regex_cache(Z_STR(patt))) == NULL) {
+		zval_ptr_dtor(&patt);
+		rep_cnt = -1;
+		goto out;
 	}
-out:
-	file_regfree(&rx);
-	return rv;
-}
+	zval_ptr_dtor(&patt);
 
-protected int
-file_regcomp(file_regex_t *rx, const char *pat, int flags)
-{
-#ifdef USE_C_LOCALE
-	rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
-	assert(rx->c_lc_ctype != NULL);
-	rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
-	assert(rx->old_lc_ctype != NULL);
-#else
-	rx->old_lc_ctype = setlocale(LC_CTYPE, "C");
-#endif
-	rx->pat = pat;
+	repl = zend_string_init(rep, strlen(rep), 0);
+	res = php_pcre_replace_impl(pce, NULL, ms->o.buf, strlen(ms->o.buf), repl, -1, &rep_cnt);
 
-	return rx->rc = regcomp(&rx->rx, pat, flags);
-}
+	zend_string_release_ex(repl, 0);
+	if (NULL == res) {
+		rep_cnt = -1;
+		goto out;
+	}
 
-protected int
-file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
-    regmatch_t* pmatch, int eflags)
-{
-	assert(rx->rc == 0);
-	/* XXX: force initialization because glibc does not always do this */
-	memset(pmatch, 0, nmatch * sizeof(*pmatch));
-	return regexec(&rx->rx, str, nmatch, pmatch, eflags);
-}
+	strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
+	ms->o.buf[ZSTR_LEN(res)] = '\0';
 
-protected void
-file_regfree(file_regex_t *rx)
-{
-	if (rx->rc == 0)
-		regfree(&rx->rx);
-#ifdef USE_C_LOCALE
-	(void)uselocale(rx->old_lc_ctype);
-	freelocale(rx->c_lc_ctype);
-#else
-	(void)setlocale(LC_CTYPE, rx->old_lc_ctype);
-#endif
-}
+	zend_string_release_ex(res, 0);
 
-protected void
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
-{
-	char errmsg[512];
-
-	(void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
-	file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
-	    errmsg);
+out:
+	(void)setlocale(LC_CTYPE, "");
+	return rep_cnt;
 }
 
 protected file_pushbuf_t *
@@ -544,7 +514,7 @@
 	if (ms->event_flags & EVENT_HAD_ERR)
 		return NULL;
 
-	if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
+	if ((pb = (CAST(file_pushbuf_t *, emalloc(sizeof(*pb))))) == NULL)
 		return NULL;
 
 	pb->buf = ms->o.buf;
@@ -562,8 +532,8 @@
 	char *rbuf;
 
 	if (ms->event_flags & EVENT_HAD_ERR) {
-		free(pb->buf);
-		free(pb);
+		efree(pb->buf);
+		efree(pb);
 		return NULL;
 	}
 
@@ -572,7 +542,7 @@
 	ms->o.buf = pb->buf;
 	ms->offset = pb->offset;
 
-	free(pb);
+	efree(pb);
 	return rbuf;
 }
 
diff -u libmagic.orig/magic.c libmagic/magic.c
--- libmagic.orig/magic.c	2017-08-28 15:39:18.000000000 +0200
+++ libmagic/magic.c	2019-04-12 10:04:15.721646341 +0200
@@ -25,11 +25,6 @@
  * SUCH DAMAGE.
  */
 
-#ifdef WIN32
-#include <windows.h>
-#include <shlwapi.h>
-#endif
-
 #include "file.h"
 
 #ifndef	lint
@@ -39,14 +34,19 @@
 #include "magic.h"
 
 #include <stdlib.h>
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
 #include <unistd.h>
+#endif
 #include <string.h>
-#ifdef QUICK
-#include <sys/mman.h>
+#include "config.h"
+
+#ifdef PHP_WIN32
+#include <shlwapi.h>
 #endif
-#ifdef HAVE_LIMITS_H
+
 #include <limits.h>	/* for PIPE_BUF */
-#endif
 
 #if defined(HAVE_UTIMES)
 # include <sys/time.h>
@@ -71,194 +71,21 @@
 #endif
 #endif
 
-private void close_and_restore(const struct magic_set *, const char *, int,
-    const struct stat *);
+#ifdef PHP_WIN32
+# undef S_IFLNK
+# undef S_IFIFO
+#endif
+
 private int unreadable_info(struct magic_set *, mode_t, const char *);
+#if 0
 private const char* get_default_magic(void);
-#ifndef COMPILE_ONLY
-private const char *file_or_fd(struct magic_set *, const char *, int);
 #endif
+private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
 
 #ifndef	STDIN_FILENO
 #define	STDIN_FILENO	0
 #endif
 
-#ifdef WIN32
-/* HINSTANCE of this shared library. Needed for get_default_magic() */
-static HINSTANCE _w32_dll_instance = NULL;
-
-static void
-_w32_append_path(char **hmagicpath, const char *fmt, ...)
-{
-	char *tmppath;
-        char *newpath;
-	va_list ap;
-
-	va_start(ap, fmt);
-	if (vasprintf(&tmppath, fmt, ap) < 0) {
-		va_end(ap);
-		return;
-	}
-	va_end(ap);
-
-	if (access(tmppath, R_OK) == -1)
-		goto out;
-
-	if (*hmagicpath == NULL) {
-		*hmagicpath = tmppath;
-		return;
-	}
-
-	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
-		goto out;
-
-	free(*hmagicpath);
-	free(tmppath);
-	*hmagicpath = newpath;
-	return;
-out:
-	free(tmppath);
-}
-
-static void
-_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
-{
-	static const char *trypaths[] = {
-		"%s/share/misc/magic.mgc",
-		"%s/magic.mgc",
-	};
-	LPSTR dllpath;
-	size_t sp;
-
-	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
-
-	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
-		goto out;
-
-	PathRemoveFileSpecA(dllpath);
-
-	if (module) {
-		char exepath[MAX_PATH];
-		GetModuleFileNameA(NULL, exepath, MAX_PATH);
-		PathRemoveFileSpecA(exepath);
-		if (stricmp(exepath, dllpath) == 0)
-			goto out;
-	}
-
-	sp = strlen(dllpath);
-	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
-		_w32_append_path(hmagicpath,
-		    "%s/../share/misc/magic.mgc", dllpath);
-		goto out;
-	}
-
-	for (sp = 0; sp < __arraycount(trypaths); sp++)
-		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
-out:
-	free(dllpath);
-}
-
-/* Placate GCC by offering a sacrificial previous prototype */
-BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
-
-BOOL WINAPI
-DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
-    LPVOID lpvReserved __attribute__((__unused__)))
-{
-	if (fdwReason == DLL_PROCESS_ATTACH)
-		_w32_dll_instance = hinstDLL;
-	return 1;
-}
-#endif
-
-private const char *
-get_default_magic(void)
-{
-	static const char hmagic[] = "/.magic/magic.mgc";
-	static char *default_magic;
-	char *home, *hmagicpath;
-
-#ifndef WIN32
-	struct stat st;
-
-	if (default_magic) {
-		free(default_magic);
-		default_magic = NULL;
-	}
-	if ((home = getenv("HOME")) == NULL)
-		return MAGIC;
-
-	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
-		return MAGIC;
-	if (stat(hmagicpath, &st) == -1) {
-		free(hmagicpath);
-		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
-			return MAGIC;
-		if (stat(hmagicpath, &st) == -1)
-			goto out;
-		if (S_ISDIR(st.st_mode)) {
-			free(hmagicpath);
-			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
-				return MAGIC;
-			if (access(hmagicpath, R_OK) == -1)
-				goto out;
-		}
-	}
-
-	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
-		goto out;
-	free(hmagicpath);
-	return default_magic;
-out:
-	default_magic = NULL;
-	free(hmagicpath);
-	return MAGIC;
-#else
-	hmagicpath = NULL;
-
-	if (default_magic) {
-		free(default_magic);
-		default_magic = NULL;
-	}
-
-	/* First, try to get a magic file from user-application data */
-	if ((home = getenv("LOCALAPPDATA")) != NULL)
-		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
-
-	/* Second, try to get a magic file from the user profile data */
-	if ((home = getenv("USERPROFILE")) != NULL)
-		_w32_append_path(&hmagicpath,
-		    "%s/Local Settings/Application Data%s", home, hmagic);
-
-	/* Third, try to get a magic file from Common Files */
-	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
-		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
-
-	/* Fourth, try to get magic file relative to exe location */
-        _w32_get_magic_relative_to(&hmagicpath, NULL);
-
-	/* Fifth, try to get magic file relative to dll location */
-        _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
-
-	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
-	default_magic = hmagicpath;
-	return default_magic;
-#endif
-}
-
-public const char *
-magic_getpath(const char *magicfile, int action)
-{
-	if (magicfile != NULL)
-		return magicfile;
-
-	magicfile = getenv("MAGIC");
-	if (magicfile != NULL)
-		return magicfile;
-
-	return action == FILE_LOAD ? get_default_magic() : MAGIC;
-}
-
 public struct magic_set *
 magic_open(int flags)
 {
@@ -304,20 +131,6 @@
 	return file_apprentice(ms, magicfile, FILE_LOAD);
 }
 
-#ifndef COMPILE_ONLY
-/*
- * Install a set of compiled magic buffers.
- */
-public int
-magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
-    size_t nbufs)
-{
-	if (ms == NULL)
-		return -1;
-	return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
-}
-#endif
-
 public int
 magic_compile(struct magic_set *ms, const char *magicfile)
 {
@@ -342,9 +155,10 @@
 	return file_apprentice(ms, magicfile, FILE_LIST);
 }
 
+#if 0
 private void
 close_and_restore(const struct magic_set *ms, const char *name, int fd,
-    const struct stat *sb)
+    const zend_stat_t *sb)
 {
 	if (fd == STDIN_FILENO || name == NULL)
 		return;
@@ -374,8 +188,8 @@
 #endif
 	}
 }
+#endif
 
-#ifndef COMPILE_ONLY
 
 /*
  * find type of descriptor
@@ -385,7 +199,7 @@
 {
 	if (ms == NULL)
 		return NULL;
-	return file_or_fd(ms, NULL, fd);
+	return file_or_stream(ms, NULL, NULL);
 }
 
 /*
@@ -396,31 +210,42 @@
 {
 	if (ms == NULL)
 		return NULL;
-	return file_or_fd(ms, inname, STDIN_FILENO);
+	return file_or_stream(ms, inname, NULL);
+}
+
+public const char *
+magic_stream(struct magic_set *ms, php_stream *stream)
+{
+	if (ms == NULL)
+		return NULL;
+	return file_or_stream(ms, NULL, stream);
 }
 
 private const char *
-file_or_fd(struct magic_set *ms, const char *inname, int fd)
+file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
 {
 	int	rv = -1;
 	unsigned char *buf;
-	struct stat	sb;
+	zend_stat_t   sb;
 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
-	int	ispipe = 0;
-	off_t	pos = (off_t)-1;
+	int no_in_stream = 0;
 
 	if (file_reset(ms, 1) == -1)
 		goto out;
 
+	if (!inname && !stream) {
+		return NULL;
+	}
+
 	/*
 	 * one extra for terminating '\0', and
 	 * some overlapping space for matches near EOF
 	 */
 #define SLOP (1 + sizeof(union VALUETYPE))
-	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
+	if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
 		return NULL;
 
-	switch (file_fsmagic(ms, inname, &sb)) {
+	switch (file_fsmagic(ms, inname, &sb, stream)) {
 	case -1:		/* error */
 		goto done;
 	case 0:			/* nothing found */
@@ -430,103 +255,41 @@
 		goto done;
 	}
 
-#ifdef WIN32
-	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
-	if (fd == STDIN_FILENO)
-		_setmode(STDIN_FILENO, O_BINARY);
-#endif
-
-	if (inname == NULL) {
-		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
-			ispipe = 1;
-		else
-			pos = lseek(fd, (off_t)0, SEEK_CUR);
-	} else {
-		int flags = O_RDONLY|O_BINARY;
-		int okstat = stat(inname, &sb) == 0;
+	errno = 0;
 
-		if (okstat && S_ISFIFO(sb.st_mode)) {
-#ifdef O_NONBLOCK
-			flags |= O_NONBLOCK;
-#endif
-			ispipe = 1;
-		}
+	if (!stream && inname) {
+		no_in_stream = 1;
+		stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
+	}
 
-		errno = 0;
-		if ((fd = open(inname, flags)) < 0) {
-#ifdef WIN32
-			/*
-			 * Can't stat, can't open.  It may have been opened in
-			 * fsmagic, so if the user doesn't have read permission,
-			 * allow it to say so; otherwise an error was probably
-			 * displayed in fsmagic.
-			 */
-			if (!okstat && errno == EACCES) {
-				sb.st_mode = S_IFBLK;
-				okstat = 1;
-			}
-#endif
-			if (okstat &&
-			    unreadable_info(ms, sb.st_mode, inname) == -1)
-				goto done;
-			rv = 0;
+	if (!stream) {
+		if (unreadable_info(ms, sb.st_mode, inname) == -1)
 			goto done;
-		}
+		rv = 0;
+		goto done;
+	}
+
 #ifdef O_NONBLOCK
-		if ((flags = fcntl(fd, F_GETFL)) != -1) {
-			flags &= ~O_NONBLOCK;
-			(void)fcntl(fd, F_SETFL, flags);
-		}
+/* we should be already be in non blocking mode for network socket */
 #endif
-	}
 
 	/*
 	 * try looking at the first ms->bytes_max bytes
 	 */
-	if (ispipe) {
-		ssize_t r = 0;
-
-		while ((r = sread(fd, (void *)&buf[nbytes],
-		    (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
-			nbytes += r;
-			if (r < PIPE_BUF) break;
-		}
-
-		if (nbytes == 0 && inname) {
-			/* We can not read it, but we were able to stat it. */
-			if (unreadable_info(ms, sb.st_mode, inname) == -1)
-				goto done;
-			rv = 0;
-			goto done;
-		}
-
-	} else {
-		/* Windows refuses to read from a big console buffer. */
-		size_t howmany =
-#if defined(WIN32)
-				_isatty(fd) ? 8 * 1024 :
-#endif
-				ms->bytes_max;
-		if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
-			if (inname == NULL && fd != STDIN_FILENO)
-				file_error(ms, errno, "cannot read fd %d", fd);
-			else
-				file_error(ms, errno, "cannot read `%s'",
-				    inname == NULL ? "/dev/stdin" : inname);
-			goto done;
-		}
+	if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
+		file_error(ms, errno, "cannot read `%s'", inname);
+		goto done;
 	}
 
 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
-	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
+	if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
 		goto done;
 	rv = 0;
 done:
-	free(buf);
-	if (fd != -1) {
-		if (pos != (off_t)-1)
-			(void)lseek(fd, pos, SEEK_SET);
-		close_and_restore(ms, inname, fd, &sb);
+	efree(buf);
+
+	if (no_in_stream && stream) {
+		php_stream_close(stream);
 	}
 out:
 	return rv == 0 ? file_getbuffer(ms) : NULL;
@@ -544,12 +307,11 @@
 	 * The main work is done here!
 	 * We have the file name and/or the data buffer to be identified.
 	 */
-	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
+	if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
 		return NULL;
 	}
 	return file_getbuffer(ms);
 }
-#endif
 
 public const char *
 magic_error(struct magic_set *ms)
diff -u libmagic.orig/print.c libmagic/print.c
--- libmagic.orig/print.c	2017-02-10 19:14:01.000000000 +0100
+++ libmagic/print.c	2019-03-22 11:57:57.251021738 +0100
@@ -2,7 +2,7 @@
  * 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:
@@ -12,7 +12,7 @@
  * 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
@@ -28,6 +28,7 @@
 /*
  * print.c - debugging printout routines
  */
+#include "php.h"
 
 #include "file.h"
 
@@ -75,7 +76,7 @@
 	if (m->mask_op & FILE_OPINVERSE)
 		(void) fputc('~', stderr);
 
-	if (IS_STRING(m->type)) {
+	if (IS_LIBMAGIC_STRING(m->type)) {
 		if (m->str_flags) {
 			(void) fputc('/', stderr);
 			if (m->str_flags & STRING_COMPACT_WHITESPACE) 
@@ -215,18 +216,18 @@
 file_magwarn(struct magic_set *ms, const char *f, ...)
 {
 	va_list va;
+	char *expanded_format = NULL;
+	int expanded_len;
 
-	/* cuz we use stdout for most, stderr here */
-	(void) fflush(stdout); 
-
-	if (ms->file)
-		(void) fprintf(stderr, "%s, %lu: ", ms->file,
-		    (unsigned long)ms->line);
-	(void) fprintf(stderr, "Warning: ");
 	va_start(va, f);
-	(void) vfprintf(stderr, f, va);
+	expanded_len = vasprintf(&expanded_format, f, va);
 	va_end(va);
-	(void) fputc('\n', stderr);
+
+	if (expanded_len >= 0 && expanded_format) {
+		php_error_docref(NULL, E_NOTICE, "Warning: %s", expanded_format);
+
+		free(expanded_format);
+	}
 }
 
 protected const char *
@@ -247,13 +248,13 @@
 	}
 
 	if (flags & FILE_T_LOCAL) {
-		tm = localtime_r(&t, &tmz);
+		tm = php_localtime_r(&t, &tmz);
 	} else {
-		tm = gmtime_r(&t, &tmz);
+		tm = php_gmtime_r(&t, &tmz);
 	}
 	if (tm == NULL)
 		goto out;
-	pp = asctime_r(tm, buf);
+	pp = php_asctime_r(tm, buf);
 
 	if (pp == NULL)
 		goto out;
diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
--- libmagic.orig/readcdf.c	2017-11-02 21:25:39.000000000 +0100
+++ libmagic/readcdf.c	2019-03-08 09:31:16.396796480 +0100
@@ -31,7 +31,11 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
 #include <unistd.h>
+#endif
 #include <string.h>
 #include <time.h>
 #include <ctype.h>
@@ -104,10 +108,6 @@
 		if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
 			return cv[i].mime;
 	}
-#ifdef CDF_DEBUG
-	fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
-	    clsid[1]);
-#endif
 	return NULL;
 }
 
@@ -116,30 +116,14 @@
 {
 	size_t i;
 	const char *rv = NULL;
-#ifdef USE_C_LOCALE
-	locale_t old_lc_ctype, c_lc_ctype;
 
-	c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
-	assert(c_lc_ctype != NULL);
-	old_lc_ctype = uselocale(c_lc_ctype);
-	assert(old_lc_ctype != NULL);
-#else
-	char *old_lc_ctype = setlocale(LC_CTYPE, "C");
-#endif
+	(void)setlocale(LC_CTYPE, "C");
 	for (i = 0; nv[i].pattern != NULL; i++)
 		if (strcasestr(vbuf, nv[i].pattern) != NULL) {
 			rv = nv[i].mime;
 			break;
 		}
-#ifdef CDF_DEBUG
-	fprintf(stderr, "unknown app %s\n", vbuf);
-#endif
-#ifdef USE_C_LOCALE
-	(void)uselocale(old_lc_ctype);
-	freelocale(c_lc_ctype);
-#else
-	setlocale(LC_CTYPE, old_lc_ctype);
-#endif
+	(void)setlocale(LC_CTYPE, "");
 	return rv;
 }
 
@@ -155,6 +139,8 @@
         const char *s, *e;
         int len;
 
+	memset(&ts, 0, sizeof(ts));
+
         if (!NOTMIME(ms) && root_storage)
 		str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
 		    clsid2mime);
@@ -281,10 +267,10 @@
 			if (file_printf(ms, "%s%s",
 			    cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
 			    i == cat->cat_num - 1 ? "]" : ", ") == -1) {
-				free(cat);
+				efree(cat);
 				return -1;
 			}
-		free(cat);
+		efree(cat);
 	} else {
 		if (file_printf(ms, "application/CDFV2") == -1)
 			return -1;
@@ -345,7 +331,7 @@
 	}
 
         m = cdf_file_property_info(ms, info, count, root_storage);
-        free(info);
+        efree(info);
 
         return m == -1 ? -2 : m;
 }
@@ -353,11 +339,11 @@
 #ifdef notdef
 private char *
 format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
-	snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" 
+	snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
 	    PRIx64 "-%.12" PRIx64,
 	    (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
 	    (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
-	    (uuid[0] >>  0) & (uint64_t)0x0000000000000ffffULL, 
+	    (uuid[0] >>  0) & (uint64_t)0x0000000000000ffffULL,
 	    (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
 	    (uuid[1] >>  0) & (uint64_t)0x0000fffffffffffffULL);
 	return buf;
@@ -436,7 +422,7 @@
 	const char *sections[5];
 	const int  types[5];
 } sectioninfo[] = {
-	{ "Encrypted", "encrypted", 
+	{ "Encrypted", "encrypted",
 		{
 			"EncryptedPackage", "EncryptedSummary",
 			NULL, NULL, NULL,
@@ -448,7 +434,7 @@
 
 		},
 	},
-	{ "QuickBooks", "quickbooks", 
+	{ "QuickBooks", "quickbooks",
 		{
 #if 0
 			"TaxForms", "PDFTaxForms", "modulesInBackup",
@@ -655,11 +641,11 @@
 	cdf_zero_stream(&scn);
 	cdf_zero_stream(&sst);
 out3:
-        free(dir.dir_tab);
+        efree(dir.dir_tab);
 out2:
-        free(ssat.sat_tab);
+        efree(ssat.sat_tab);
 out1:
-        free(sat.sat_tab);
+        efree(sat.sat_tab);
 out0:
 	if (i == -1) {
 	    if (NOTMIME(ms)) {
diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
--- libmagic.orig/softmagic.c	2018-04-15 20:49:15.000000000 +0200
+++ libmagic/softmagic.c	2019-03-25 16:13:59.574598917 +0100
@@ -43,6 +43,10 @@
 #include <time.h>
 #include "der.h"
 
+#ifndef PREG_OFFSET_CAPTURE
+# define PREG_OFFSET_CAPTURE                 (1<<8)
+#endif
+
 private int match(struct magic_set *, struct magic *, uint32_t,
     const struct buffer *, size_t, int, int, int, uint16_t *,
     uint16_t *, int *, int *, int *);
@@ -119,8 +123,8 @@
 	return 0;
 }
 
-#define FILE_FMTDEBUG
-#ifdef FILE_FMTDEBUG
+
+#if defined(FILE_FMTDEBUG) && defined(HAVE_FMTCHECK)
 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
 
 private const char * __attribute__((__format_arg__(3)))
@@ -134,8 +138,10 @@
 		    " with `%s'", file, line, desc, def);
 	return ptr;
 }
-#else
+#elif defined(HAVE_FMTCHECK)
 #define F(a, b, c) fmtcheck((b), (c))
+#else
+#define F(a, b, c) ((b))
 #endif
 
 /*
@@ -189,7 +195,7 @@
 		struct magic *m = &magic[magindex];
 
 		if (m->type != FILE_NAME)
-		if ((IS_STRING(m->type) &&
+		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))) ||
@@ -416,42 +422,30 @@
 private int
 check_fmt(struct magic_set *ms, const char *fmt)
 {
-	file_regex_t rx;
-	int rc, rv = -1;
+	pcre2_code *pce;
+	uint32_t capture_count;
+	int rv = -1;
+	zend_string *pattern;
 
 	if (strchr(fmt, '%') == NULL)
 		return 0;
 
-	rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
-	if (rc) {
-		file_regerror(&rx, rc, ms);
+	(void)setlocale(LC_CTYPE, "C");
+	pattern = zend_string_init("~%[-0-9\\.]*s~", sizeof("~%[-0-9\\.]*s~") - 1, 0);
+	if ((pce = pcre_get_compiled_regex(pattern, &capture_count)) == NULL) {
+		rv = -1;
 	} else {
-		rc = file_regexec(&rx, fmt, 0, 0, 0);
-		rv = !rc;
+		pcre2_match_data *match_data = php_pcre_create_match_data(capture_count, pce);
+		if (match_data) {
+			rv = pcre2_match(pce, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0;
+			php_pcre_free_match_data(match_data);
+		}
 	}
-	file_regfree(&rx);
+	zend_string_release_ex(pattern, 0);
+	(void)setlocale(LC_CTYPE, "");
 	return rv;
 }
 
-#ifndef HAVE_STRNDUP
-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 = malloc(len + 1)) == NULL)
-		return NULL;
-	(void)memcpy(copy, str, len);
-	copy[len] = '\0';
-	return copy;
-}
-#endif /* HAVE_STRNDUP */
-
 static int
 varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
 {
@@ -743,14 +737,10 @@
 		char *cp;
 		int rval;
 
-		cp = strndup((const char *)ms->search.s, ms->search.rm_len);
-		if (cp == NULL) {
-			file_oomem(ms, ms->search.rm_len);
-			return -1;
-		}
+		cp = estrndup((const char *)ms->search.s, ms->search.rm_len);
 		rval = file_printf(ms, F(ms, desc, "%s"),
 		    file_printable(sbuf, sizeof(sbuf), cp));
-		free(cp);
+		efree(cp);
 
 		if (rval == -1)
 			return -1;
@@ -1135,7 +1125,7 @@
 			 * string by p->s, so we need to deduct sz.
 			 * Because we can use one of the bytes of the length
 			 * after we shifted as NUL termination.
-			 */ 
+			 */
 			len = sz;
 		}
 		while (len--)
@@ -1209,7 +1199,7 @@
 			goto out;
 		return 1;
 	case FILE_BEDOUBLE:
-		p->q = BE64(p); 
+		p->q = BE64(p);
 		if (cvt_double(p, m) == -1)
 			goto out;
 		return 1;
@@ -1481,8 +1471,6 @@
 		return -1;
 	}
 
-
-
 	if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
 	    (uint32_t)nbytes, m) == -1)
 		return -1;
@@ -1494,9 +1482,6 @@
 		    m->type, m->flag, offset, o, nbytes,
 		    *indir_count, *name_count);
 		mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
-#ifndef COMPILE_ONLY
-		file_mdump(m);
-#endif
 	}
 
 	if (m->flag & INDIR) {
@@ -1609,9 +1594,6 @@
 		if ((ms->flags & MAGIC_DEBUG) != 0) {
 			mdebug(offset, (char *)(void *)p,
 			    sizeof(union VALUETYPE));
-#ifndef COMPILE_ONLY
-			file_mdump(m);
-#endif
 		}
 	}
 
@@ -1696,15 +1678,15 @@
 		if (rv == 1) {
 			if ((ms->flags & MAGIC_NODESC) == 0 &&
 			    file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) {
-				free(rbuf);
+				if (rbuf) efree(rbuf);
 				return -1;
 			}
 			if (file_printf(ms, "%s", rbuf) == -1) {
-				free(rbuf);
+				if (rbuf) efree(rbuf);
 				return -1;
 			}
 		}
-		free(rbuf);
+		if (rbuf) efree(rbuf);
 		return rv;
 
 	case FILE_USE:
@@ -1827,6 +1809,41 @@
 	return file_strncmp(a, b, len, flags);
 }
 
+public void
+convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options)
+{
+	int i, j=0;
+	zend_string *t;
+
+	t = zend_string_alloc(len * 2 + 4, 0);
+
+	ZSTR_VAL(t)[j++] = '~';
+
+	for (i = 0; i < len; i++, j++) {
+		switch (val[i]) {
+			case '~':
+				ZSTR_VAL(t)[j++] = '\\';
+				ZSTR_VAL(t)[j] = '~';
+				break;
+			default:
+				ZSTR_VAL(t)[j] = val[i];
+				break;
+		}
+	}
+	ZSTR_VAL(t)[j++] = '~';
+
+	if (options & PCRE2_CASELESS)
+		ZSTR_VAL(t)[j++] = 'i';
+
+	if (options & PCRE2_MULTILINE)
+		ZSTR_VAL(t)[j++] = 'm';
+
+	ZSTR_VAL(t)[j]='\0';
+	ZSTR_LEN(t) = j;
+
+	ZVAL_NEW_STR(pattern, t);
+}
+
 private int
 magiccheck(struct magic_set *ms, struct magic *m)
 {
@@ -1987,65 +2004,77 @@
 		break;
 	}
 	case FILE_REGEX: {
-		int rc;
-		file_regex_t rx;
-		const char *search;
+		zval pattern;
+		uint32_t options = 0;
+		pcre_cache_entry *pce;
 
-		if (ms->search.s == NULL)
-			return 0;
+		options |= PCRE2_MULTILINE;
 
-		l = 0;
-		rc = file_regcomp(&rx, m->value.s,
-		    REG_EXTENDED|REG_NEWLINE|
-		    ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
-		if (rc) {
-			file_regerror(&rx, rc, ms);
-			v = (uint64_t)-1;
+		if (m->str_flags & STRING_IGNORE_CASE) {
+			options |= PCRE2_CASELESS;
+		}
+
+		convert_libmagic_pattern(&pattern, (char *)m->value.s, m->vallen, options);
+
+		l = v = 0;
+		if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
+			zval_ptr_dtor(&pattern);
+			return -1;
 		} else {
-			regmatch_t pmatch;
-			size_t slen = ms->search.s_len;
-			char *copy;
-			if (slen != 0) {
-			    copy = CAST(char *, malloc(slen));
-			    if (copy == NULL)  {
-				file_regfree(&rx);
-				file_error(ms, errno,
-				    "can't allocate %" SIZE_T_FORMAT "u bytes",
-				    slen);
-				return -1;
-			    }
-			    memcpy(copy, ms->search.s, slen);
-			    copy[--slen] = '\0';
-			    search = copy;
+			/* pce now contains the compiled regex */
+			zval retval;
+			zval subpats;
+			zend_string *haystack;
+
+			ZVAL_NULL(&retval);
+			ZVAL_NULL(&subpats);
+
+			/* Cut the search len from haystack, equals to REG_STARTEND */
+			haystack = zend_string_init(ms->search.s, ms->search.s_len, 0);
+
+			/* match v = 0, no match v = 1 */
+			php_pcre_match_impl(pce, haystack, &retval, &subpats, 0, 1, PREG_OFFSET_CAPTURE, 0);
+			/* Free haystack */
+			zend_string_release(haystack);
+
+			if (Z_LVAL(retval) < 0) {
+				zval_ptr_dtor(&subpats);
+				zval_ptr_dtor(&pattern);
+				return -1;
+			} else if ((Z_LVAL(retval) > 0) && (Z_TYPE(subpats) == IS_ARRAY)) {
+				/* Need to fetch global match which equals pmatch[0] */
+				zval *pzval;
+				HashTable *ht = Z_ARRVAL(subpats);
+				if ((pzval = zend_hash_index_find(ht, 0)) != NULL && Z_TYPE_P(pzval) == IS_ARRAY) {
+					/* If everything goes according to the master plan
+					   tmpcopy now contains two elements:
+					   0 = the match
+					   1 = starting position of the match */
+					zval *match, *offset;
+					if ((match = zend_hash_index_find(Z_ARRVAL_P(pzval), 0)) &&
+							(offset = zend_hash_index_find(Z_ARRVAL_P(pzval), 1))) {
+						if (Z_TYPE_P(match) != IS_STRING && Z_TYPE_P(offset) != IS_LONG) {
+							goto error_out;
+						}
+						ms->search.s += Z_LVAL_P(offset); /* this is where the match starts */
+						ms->search.offset += Z_LVAL_P(offset); /* this is where the match starts as size_t */
+						ms->search.rm_len = Z_STRLEN_P(match) /* This is the length of the matched pattern */;
+						v = 0;
+					} else {
+						goto error_out;
+					}
+				} else {
+error_out:
+					zval_ptr_dtor(&subpats);
+					zval_ptr_dtor(&pattern);
+					return -1;
+				}
 			} else {
-			    search = CCAST(char *, "");
-			    copy = NULL;
-			}
-			rc = file_regexec(&rx, (const char *)search,
-			    1, &pmatch, 0);
-			free(copy);
-			switch (rc) {
-			case 0:
-				ms->search.s += (int)pmatch.rm_so;
-				ms->search.offset += (size_t)pmatch.rm_so;
-				ms->search.rm_len =
-				    (size_t)(pmatch.rm_eo - pmatch.rm_so);
-				v = 0;
-				break;
-
-			case REG_NOMATCH:
 				v = 1;
-				break;
-
-			default:
-				file_regerror(&rx, rc, ms);
-				v = (uint64_t)-1;
-				break;
 			}
+			zval_ptr_dtor(&subpats);
+			zval_ptr_dtor(&pattern);
 		}
-		file_regfree(&rx);
-		if (v == (uint64_t)-1)
-			return -1;
 		break;
 	}
 	case FILE_INDIRECT:
diff -u libmagic.orig/strcasestr.c libmagic/strcasestr.c
--- libmagic.orig/strcasestr.c	2014-05-13 18:48:12.000000000 +0200
+++ libmagic/strcasestr.c	2019-03-08 09:31:16.396796480 +0100
@@ -39,6 +39,8 @@
 
 #include "file.h"
 
+#include "php_stdint.h"
+
 #include <assert.h>
 #include <ctype.h>
 #include <string.h>