php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
magic.c
Go to the documentation of this file.
1/*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include "file.h"
29
30#ifndef lint
31FILE_RCSID("@(#)$File: magic.c,v 1.121 2023/02/09 17:45:19 christos Exp $")
32#endif /* lint */
33
34#include "magic.h"
35
36#include <stdlib.h>
37#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
40#include <string.h>
41#ifdef QUICK
42#include <sys/mman.h>
43#endif
44#include <limits.h> /* for PIPE_BUF */
45
46#if defined(HAVE_UTIMES)
47# include <sys/time.h>
48#elif defined(HAVE_UTIME)
49# if defined(HAVE_SYS_UTIME_H)
50# include <sys/utime.h>
51# elif defined(HAVE_UTIME_H)
52# include <utime.h>
53# endif
54#endif
55
56#ifdef HAVE_UNISTD_H
57#include <unistd.h> /* for read() */
58#endif
59
60#ifndef PIPE_BUF
61/* Get the PIPE_BUF from pathconf */
62#ifdef _PC_PIPE_BUF
63#define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
64#else
65#define PIPE_BUF 512
66#endif
67#endif
68
69#ifdef PHP_WIN32
70# undef S_IFLNK
71# undef S_IFIFO
72#endif
73
74file_private int unreadable_info(struct magic_set *, mode_t, const char *);
75file_private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
76
77#ifndef STDIN_FILENO
78#define STDIN_FILENO 0
79#endif
80
83{
84 return file_ms_alloc(flags);
85}
86
88unreadable_info(struct magic_set *ms, mode_t md, const char *file)
89{
90 if (file) {
91 /* We cannot open it, but we were able to stat it. */
92 if (access(file, W_OK) == 0)
93 if (file_printf(ms, "writable, ") == -1)
94 return -1;
95#ifndef WIN32
96 if (access(file, X_OK) == 0)
97 if (file_printf(ms, "executable, ") == -1)
98 return -1;
99#else
100 /* X_OK doesn't work well on MS-Windows */
101 {
102 const char *p = strrchr(file, '.');
103 if (p && (stricmp(p, ".exe")
104 || stricmp(p, ".dll")
105 || stricmp(p, ".bat")
106 || stricmp(p, ".cmd")))
107 if (file_printf(ms, "writable, ") == -1)
108 return -1;
109 }
110#endif
111 }
112 if (S_ISREG(md))
113 if (file_printf(ms, "regular file, ") == -1)
114 return -1;
115 if (file_printf(ms, "no read permission") == -1)
116 return -1;
117 return 0;
118}
119
120file_public void
122{
123 if (ms == NULL)
124 return;
125 file_ms_free(ms);
126}
127
128/*
129 * load a magic file
130 */
131file_public int
132magic_load(struct magic_set *ms, const char *magicfile)
133{
134 if (ms == NULL)
135 return -1;
136 return file_apprentice(ms, magicfile, FILE_LOAD);
137}
138
139file_public int
140magic_compile(struct magic_set *ms, const char *magicfile)
141{
142 if (ms == NULL)
143 return -1;
144 return file_apprentice(ms, magicfile, FILE_COMPILE);
145}
146
147file_public int
148magic_check(struct magic_set *ms, const char *magicfile)
149{
150 if (ms == NULL)
151 return -1;
152 return file_apprentice(ms, magicfile, FILE_CHECK);
153}
154
155file_public int
156magic_list(struct magic_set *ms, const char *magicfile)
157{
158 if (ms == NULL)
159 return -1;
160 return file_apprentice(ms, magicfile, FILE_LIST);
161}
162
163#ifndef COMPILE_ONLY
164
165/*
166 * find type of descriptor
167 */
168file_public const char *
170{
171 if (ms == NULL)
172 return NULL;
173 return file_or_stream(ms, NULL, NULL);
174}
175
176/*
177 * find type of named file
178 */
179file_public const char *
180magic_file(struct magic_set *ms, const char *inname)
181{
182 if (ms == NULL)
183 return NULL;
184 return file_or_stream(ms, inname, NULL);
185}
186
187file_public const char *
189{
190 if (ms == NULL)
191 return NULL;
192 return file_or_stream(ms, NULL, stream);
193}
194
195file_private const char *
196file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
197{
198 int rv = -1;
199 unsigned char *buf;
200 zend_stat_t sb = {0};
201 ssize_t nbytes = 0; /* number of bytes read from a datafile */
202 int no_in_stream = 0;
203
204 if (file_reset(ms, 1) == -1)
205 goto out;
206
207 /*
208 * one extra for terminating '\0', and
209 * some overlapping space for matches near EOF
210 */
211#define SLOP (1 + sizeof(union VALUETYPE))
212 if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
213 return NULL;
214
215 switch (file_fsmagic(ms, inname, &sb)) {
216 case -1: /* error */
217 goto done;
218 case 0: /* nothing found */
219 break;
220 default: /* matched it and printed type */
221 rv = 0;
222 goto done;
223 }
224
225 errno = 0;
226
227 if (inname && !stream) {
228 no_in_stream = 1;
229 stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
230 if (!stream) {
231 if (unreadable_info(ms, sb.st_mode, inname) == -1)
232 goto done;
233 rv = -1;
234 goto done;
235 }
236 }
237
239 if (php_stream_stat(stream, &ssb) < 0) {
240 if (ms->flags & MAGIC_ERROR) {
241 file_error(ms, errno, "cannot stat `%s'", inname);
242 rv = -1;
243 goto done;
244 }
245 }
246 memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
247
248 /*
249 * try looking at the first ms->bytes_max bytes
250 */
251 if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
252 file_error(ms, errno, "cannot read `%s'", inname);
253 goto done;
254 }
255
256 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
257 if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
258 goto done;
259 rv = 0;
260done:
261 efree(buf);
262
263 if (no_in_stream && stream) {
264 php_stream_close(stream);
265 }
266out:
267 return rv == 0 ? file_getbuffer(ms) : NULL;
268}
269
270
271file_public const char *
272magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
273{
274 if (ms == NULL)
275 return NULL;
276 if (file_reset(ms, 1) == -1)
277 return NULL;
278 /*
279 * The main work is done here!
280 * We have the file name and/or the data buffer to be identified.
281 */
282 if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
283 return NULL;
284 }
285 return file_getbuffer(ms);
286}
287#endif
288
289file_public const char *
291{
292 if (ms == NULL)
293 return "Magic database is not open";
294 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
295}
296
297file_public int
299{
300 if (ms == NULL)
301 return EINVAL;
302 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
303}
304
305file_public int
307{
308 if (ms == NULL)
309 return -1;
310
311 return ms->flags;
312}
313
314file_public int
316{
317 if (ms == NULL)
318 return -1;
319#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
321 return -1;
322#endif
323 ms->flags = flags;
324 return 0;
325}
326
327file_public int
329{
330 return MAGIC_VERSION;
331}
332
333file_public int
334magic_setparam(struct magic_set *ms, int param, const void *val)
335{
336 if (ms == NULL)
337 return -1;
338 switch (param) {
340 ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
341 return 0;
343 ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
344 return 0;
346 ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
347 return 0;
349 ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
350 return 0;
352 ms->elf_shsize_max = *CAST(const size_t *, val);
353 return 0;
355 ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
356 return 0;
358 ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
359 return 0;
361 ms->bytes_max = *CAST(const size_t *, val);
362 return 0;
364 ms->encoding_max = *CAST(const size_t *, val);
365 return 0;
366 default:
367 errno = EINVAL;
368 return -1;
369 }
370}
371
372file_public int
373magic_getparam(struct magic_set *ms, int param, void *val)
374{
375 if (ms == NULL)
376 return -1;
377 switch (param) {
379 *CAST(size_t *, val) = ms->indir_max;
380 return 0;
382 *CAST(size_t *, val) = ms->name_max;
383 return 0;
385 *CAST(size_t *, val) = ms->elf_phnum_max;
386 return 0;
388 *CAST(size_t *, val) = ms->elf_shnum_max;
389 return 0;
391 *CAST(size_t *, val) = ms->elf_shsize_max;
392 return 0;
394 *CAST(size_t *, val) = ms->elf_notes_max;
395 return 0;
397 *CAST(size_t *, val) = ms->regex_max;
398 return 0;
400 *CAST(size_t *, val) = ms->bytes_max;
401 return 0;
403 *CAST(size_t *, val) = ms->encoding_max;
404 return 0;
405 default:
406 errno = EINVAL;
407 return -1;
408 }
409}
file_protected struct magic_set * file_ms_alloc(int flags)
Definition apprentice.c:549
file_protected void file_ms_free(struct magic_set *ms)
Definition apprentice.c:526
file_protected int file_apprentice(struct magic_set *ms, const char *fn, int action)
Definition apprentice.c:663
file(string $filename, int $flags=0, $context=null)
strrchr(string $haystack, string $needle, bool $before_needle=false)
memcpy(ptr1, ptr2, size)
memset(ptr, 0, type->size)
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
file_protected int file_printf(struct magic_set *, const char *,...) __attribute__((__format__(__printf__
file_protected const char * file_getbuffer(struct magic_set *)
Definition funcs.c:548
#define FILE_LOAD
Definition file.h:172
#define FILE_LIST
Definition file.h:175
file_protected int file_protected int file_reset(struct magic_set *, int)
Definition funcs.c:523
#define FILE_CHECK
Definition file.h:173
file_protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *)
Definition fsmagic.c:90
#define EVENT_HAD_ERR
Definition file.h:460
file_protected int file_buffer(struct magic_set *, php_stream *, zend_stat_t *, const char *, const void *, size_t)
file_protected void file_error(struct magic_set *, int, const char *,...) __attribute__((__format__(__printf__
#define FILE_COMPILE
Definition file.h:174
#define FILE_RCSID(id)
Definition file.h:654
#define file_private
Definition file.h:101
#define file_public
Definition file.h:109
#define CAST(T, b)
Definition file.h:425
#define NULL
Definition gdcache.h:45
unsigned short mode_t
Definition ioutil.h:72
#define X_OK
Definition ioutil.h:83
#define W_OK
Definition ioutil.h:77
file_public int magic_version(void)
Definition magic.c:328
file_public int magic_load(struct magic_set *ms, const char *magicfile)
Definition magic.c:132
file_public const char * magic_stream(struct magic_set *ms, php_stream *stream)
Definition magic.c:188
file_public int magic_setflags(struct magic_set *ms, int flags)
Definition magic.c:315
file_private int unreadable_info(struct magic_set *, mode_t, const char *)
Definition magic.c:88
file_public int magic_getflags(struct magic_set *ms)
Definition magic.c:306
file_public int magic_errno(struct magic_set *ms)
Definition magic.c:298
file_public int magic_check(struct magic_set *ms, const char *magicfile)
Definition magic.c:148
file_public int magic_list(struct magic_set *ms, const char *magicfile)
Definition magic.c:156
file_private const char * file_or_stream(struct magic_set *, const char *, php_stream *)
Definition magic.c:196
file_public const char * magic_error(struct magic_set *ms)
Definition magic.c:290
file_public const char * magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
Definition magic.c:272
file_public const char * magic_descriptor(struct magic_set *ms, int fd)
Definition magic.c:169
file_public const char * magic_file(struct magic_set *ms, const char *inname)
Definition magic.c:180
file_public int magic_compile(struct magic_set *ms, const char *magicfile)
Definition magic.c:140
#define SLOP
file_public int magic_setparam(struct magic_set *ms, int param, const void *val)
Definition magic.c:334
file_public int magic_getparam(struct magic_set *ms, int param, void *val)
Definition magic.c:373
file_public void magic_close(struct magic_set *ms)
Definition magic.c:121
file_public struct magic_set * magic_open(int flags)
Definition magic.c:82
#define MAGIC_PARAM_INDIR_MAX
Definition magic.h:146
#define MAGIC_PARAM_ELF_NOTES_MAX
Definition magic.h:150
#define MAGIC_ERROR
Definition magic.h:42
#define MAGIC_VERSION
Definition magic.h:116
#define MAGIC_PARAM_BYTES_MAX
Definition magic.h:152
#define MAGIC_PRESERVE_ATIME
Definition magic.h:40
#define MAGIC_PARAM_NAME_MAX
Definition magic.h:147
#define MAGIC_PARAM_ELF_SHNUM_MAX
Definition magic.h:149
#define MAGIC_PARAM_ENCODING_MAX
Definition magic.h:153
#define MAGIC_PARAM_ELF_SHSIZE_MAX
Definition magic.h:154
#define MAGIC_PARAM_REGEX_MAX
Definition magic.h:151
#define MAGIC_PARAM_ELF_PHNUM_MAX
Definition magic.h:148
#define stricmp
Definition php_soap.h:38
struct _php_stream php_stream
Definition php_streams.h:96
#define REPORT_ERRORS
#define php_stream_read(stream, buf, count)
#define php_stream_close(stream)
#define php_stream_open_wrapper(path, mode, options, opened)
#define php_stream_stat(stream, ssb)
struct _php_stream_statbuf php_stream_statbuf
int fd
Definition phpdbg.h:282
zval rv
Definition session.c:1024
p
Definition session.c:1105
char * buf
Definition file.h:450
uint16_t elf_phnum_max
Definition file.h:479
size_t elf_shsize_max
Definition file.h:484
uint16_t indir_max
Definition file.h:476
uint16_t regex_max
Definition file.h:481
struct magic_set::out o
uint16_t elf_shnum_max
Definition file.h:478
int flags
Definition file.h:458
uint16_t elf_notes_max
Definition file.h:480
size_t encoding_max
Definition file.h:483
uint16_t name_max
Definition file.h:477
int error
Definition file.h:457
int event_flags
Definition file.h:459
size_t bytes_max
Definition file.h:482
#define errno
#define efree(ptr)
Definition zend_alloc.h:155
#define emalloc(size)
Definition zend_alloc.h:151
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
struct stat zend_stat_t
Definition zend_stream.h:94
#define S_ISREG(mode)
out($f, $s)