This source file includes following definitions.
- get_default_magic
- magic_getpath
- magic_open
- unreadable_info
- magic_close
- magic_load
- magic_compile
- magic_list
- close_and_restore
- magic_descriptor
- magic_file
- magic_stream
- file_or_stream
- magic_buffer
- magic_error
- magic_errno
- magic_setflags
- magic_version
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 #include "file.h"
29
30 #ifndef lint
31 FILE_RCSID("@(#)$File: magic.c,v 1.81 2013/11/29 15:42:51 christos Exp $")
32 #endif
33
34 #include "magic.h"
35
36 #include <stdlib.h>
37 #ifdef PHP_WIN32
38 #include "win32/unistd.h"
39 #else
40 #include <unistd.h>
41 #endif
42 #include <string.h>
43 #ifdef PHP_WIN32
44 # include "config.w32.h"
45 #else
46 # include "php_config.h"
47 #endif
48
49 #ifdef PHP_WIN32
50 #include <shlwapi.h>
51 #endif
52
53 #include <limits.h>
54
55 #if defined(HAVE_UTIMES)
56 # include <sys/time.h>
57 #elif defined(HAVE_UTIME)
58 # if defined(HAVE_SYS_UTIME_H)
59 # include <sys/utime.h>
60 # elif defined(HAVE_UTIME_H)
61 # include <utime.h>
62 # endif
63 #endif
64
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #endif
68
69 #ifndef PIPE_BUF
70
71 #ifdef _PC_PIPE_BUF
72 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
73 #else
74 #define PIPE_BUF 512
75 #endif
76 #endif
77
78 #ifdef PHP_WIN32
79 # undef S_IFLNK
80 # undef S_IFIFO
81 #endif
82
83 private void close_and_restore(const struct magic_set *, const char *, int,
84 const struct stat *);
85 private int unreadable_info(struct magic_set *, mode_t, const char *);
86 #if 0
87 private const char* get_default_magic(void);
88 #endif
89 private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
90
91 #ifndef STDIN_FILENO
92 #define STDIN_FILENO 0
93 #endif
94
95
96 #if 0
97 private const char *
98 get_default_magic(void)
99 {
100 static const char hmagic[] = "/.magic/magic.mgc";
101 static char *default_magic;
102 char *home, *hmagicpath;
103
104 #ifndef PHP_WIN32
105 struct stat st;
106
107 if (default_magic) {
108 free(default_magic);
109 default_magic = NULL;
110 }
111 if ((home = getenv("HOME")) == NULL)
112 return MAGIC;
113
114 if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
115 return MAGIC;
116 if (stat(hmagicpath, &st) == -1) {
117 free(hmagicpath);
118 if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
119 return MAGIC;
120 if (stat(hmagicpath, &st) == -1)
121 goto out;
122 if (S_ISDIR(st.st_mode)) {
123 free(hmagicpath);
124 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
125 return MAGIC;
126 if (access(hmagicpath, R_OK) == -1)
127 goto out;
128 }
129 }
130
131 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
132 goto out;
133 free(hmagicpath);
134 return default_magic;
135 out:
136 default_magic = NULL;
137 free(hmagicpath);
138 return MAGIC;
139 #else
140 char *hmagicp = hmagicpath;
141 char *tmppath = NULL;
142 LPTSTR dllpath;
143
144 #define APPENDPATH() \
145 do { \
146 if (tmppath && access(tmppath, R_OK) != -1) { \
147 if (hmagicpath == NULL) \
148 hmagicpath = tmppath; \
149 else { \
150 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
151 PATHSEP, tmppath) >= 0) { \
152 free(hmagicpath); \
153 hmagicpath = hmagicp; \
154 } \
155 free(tmppath); \
156 } \
157 tmppath = NULL; \
158 } \
159 } while (0)
160
161 if (default_magic) {
162 free(default_magic);
163 default_magic = NULL;
164 }
165
166
167 if ((home = getenv("LOCALAPPDATA")) == NULL) {
168 if ((home = getenv("USERPROFILE")) != NULL)
169 if (asprintf(&tmppath,
170 "%s/Local Settings/Application Data%s", home,
171 hmagic) < 0)
172 tmppath = NULL;
173 } else {
174 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
175 tmppath = NULL;
176 }
177
178 APPENDPATH();
179
180
181 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
182 if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
183 APPENDPATH();
184 }
185
186
187 dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
188 dllpath[MAX_PATH] = 0;
189 if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
190 PathRemoveFileSpecA(dllpath);
191 if (strlen(dllpath) > 3 &&
192 stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
193 if (asprintf(&tmppath,
194 "%s/../share/misc/magic.mgc", dllpath) >= 0)
195 APPENDPATH();
196 } else {
197 if (asprintf(&tmppath,
198 "%s/share/misc/magic.mgc", dllpath) >= 0)
199 APPENDPATH();
200 else if (asprintf(&tmppath,
201 "%s/magic.mgc", dllpath) >= 0)
202 APPENDPATH();
203 }
204 }
205
206
207
208 default_magic = hmagicpath;
209 return default_magic;
210 #endif
211 }
212
213 public const char *
214 magic_getpath(const char *magicfile, int action)
215 {
216 if (magicfile != NULL)
217 return magicfile;
218
219 magicfile = getenv("MAGIC");
220 if (magicfile != NULL)
221 return magicfile;
222
223 return action == FILE_LOAD ? get_default_magic() : MAGIC;
224 }
225 #endif
226
227 public struct magic_set *
228 magic_open(int flags)
229 {
230 return file_ms_alloc(flags);
231 }
232
233 private int
234 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
235 {
236
237 if (access(file, W_OK) == 0)
238 if (file_printf(ms, "writable, ") == -1)
239 return -1;
240 if (access(file, X_OK) == 0)
241 if (file_printf(ms, "executable, ") == -1)
242 return -1;
243 if (S_ISREG(md))
244 if (file_printf(ms, "regular file, ") == -1)
245 return -1;
246 if (file_printf(ms, "no read permission") == -1)
247 return -1;
248 return 0;
249 }
250
251 public void
252 magic_close(struct magic_set *ms)
253 {
254 if (ms == NULL)
255 return;
256 file_ms_free(ms);
257 }
258
259
260
261
262 public int
263 magic_load(struct magic_set *ms, const char *magicfile)
264 {
265 if (ms == NULL)
266 return -1;
267 return file_apprentice(ms, magicfile, FILE_LOAD);
268 }
269
270 public int
271 magic_compile(struct magic_set *ms, const char *magicfile)
272 {
273 if (ms == NULL)
274 return -1;
275 return file_apprentice(ms, magicfile, FILE_COMPILE);
276 }
277
278
279 public int
280 magic_list(struct magic_set *ms, const char *magicfile)
281 {
282 if (ms == NULL)
283 return -1;
284 return file_apprentice(ms, magicfile, FILE_LIST);
285 }
286
287 private void
288 close_and_restore(const struct magic_set *ms, const char *name, int fd,
289 const struct stat *sb)
290 {
291
292 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
293
294
295
296
297
298
299 #ifdef HAVE_UTIMES
300 struct timeval utsbuf[2];
301 (void)memset(utsbuf, 0, sizeof(utsbuf));
302 utsbuf[0].tv_sec = sb->st_atime;
303 utsbuf[1].tv_sec = sb->st_mtime;
304
305 (void) utimes(name, utsbuf);
306 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
307 struct utimbuf utbuf;
308
309 (void)memset(&utbuf, 0, sizeof(utbuf));
310 utbuf.actime = sb->st_atime;
311 utbuf.modtime = sb->st_mtime;
312 (void) utime(name, &utbuf);
313 #endif
314 }
315 }
316
317
318
319
320
321 public const char *
322 magic_descriptor(struct magic_set *ms, int fd)
323 {
324 if (ms == NULL)
325 return NULL;
326 return file_or_stream(ms, NULL, NULL);
327 }
328
329
330
331
332 public const char *
333 magic_file(struct magic_set *ms, const char *inname)
334 {
335 if (ms == NULL)
336 return NULL;
337 return file_or_stream(ms, inname, NULL);
338 }
339
340 public const char *
341 magic_stream(struct magic_set *ms, php_stream *stream)
342 {
343 if (ms == NULL)
344 return NULL;
345 return file_or_stream(ms, NULL, stream);
346 }
347
348 private const char *
349 file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
350 {
351 int rv = -1;
352 unsigned char *buf;
353 struct stat sb;
354 ssize_t nbytes = 0;
355 int no_in_stream = 0;
356 TSRMLS_FETCH();
357
358 if (!inname && !stream) {
359 return NULL;
360 }
361
362
363
364
365
366 #define SLOP (1 + sizeof(union VALUETYPE))
367 buf = emalloc(HOWMANY + SLOP);
368
369 if (file_reset(ms) == -1)
370 goto done;
371
372 switch (file_fsmagic(ms, inname, &sb, stream)) {
373 case -1:
374 goto done;
375 case 0:
376 break;
377 default:
378 rv = 0;
379 goto done;
380 }
381
382 errno = 0;
383
384 if (!stream && inname) {
385 no_in_stream = 1;
386 stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
387 }
388
389 if (!stream) {
390 if (unreadable_info(ms, sb.st_mode, inname) == -1)
391 goto done;
392 rv = 0;
393 goto done;
394 }
395
396 #ifdef O_NONBLOCK
397
398 #endif
399
400
401
402
403 if ((nbytes = php_stream_read(stream, (char *)buf, HOWMANY)) < 0) {
404 file_error(ms, errno, "cannot read `%s'", inname);
405 goto done;
406 }
407
408 (void)memset(buf + nbytes, 0, SLOP);
409 if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
410 goto done;
411 rv = 0;
412 done:
413 efree(buf);
414
415 if (no_in_stream && stream) {
416 php_stream_close(stream);
417 }
418
419 close_and_restore(ms, inname, 0, &sb);
420 return rv == 0 ? file_getbuffer(ms) : NULL;
421 }
422
423
424 public const char *
425 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
426 {
427 if (ms == NULL)
428 return NULL;
429 if (file_reset(ms) == -1)
430 return NULL;
431
432
433
434
435 if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
436 return NULL;
437 }
438 return file_getbuffer(ms);
439 }
440
441 public const char *
442 magic_error(struct magic_set *ms)
443 {
444 if (ms == NULL)
445 return "Magic database is not open";
446 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
447 }
448
449 public int
450 magic_errno(struct magic_set *ms)
451 {
452 if (ms == NULL)
453 return EINVAL;
454 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
455 }
456
457 public int
458 magic_setflags(struct magic_set *ms, int flags)
459 {
460 if (ms == NULL)
461 return -1;
462 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
463 if (flags & MAGIC_PRESERVE_ATIME)
464 return -1;
465 #endif
466 ms->flags = flags;
467 return 0;
468 }
469
470 public int
471 magic_version(void)
472 {
473 return MAGIC_VERSION;
474 }