hhvm: avoid -Werror=nonnull failures in bundled libmagic
[hiphop-php.git] / hphp / runtime / ext / fileinfo / libmagic / magic.cpp
blob649ebe74b830c87742493e81c25265b8faaa7e08
1 /*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
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.
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.
28 #include "file.h"
30 #ifndef lint
31 FILE_RCSID("@(#)$File: magic.c,v 1.78 2013/01/07 18:20:19 christos Exp $")
32 #endif /* lint */
34 #include "hphp/util/assertions.h"
35 #include "magic.h" // @nolint
37 #include <stdlib.h>
38 #include <string.h>
40 #ifdef PHP_WIN32
41 #include <shlwapi.h>
42 #endif
44 #include <limits.h> /* for PIPE_BUF */
46 #include <folly/portability/Unistd.h>
48 #if defined(HAVE_UTIMES)
49 # include <sys/time.h>
50 #elif defined(HAVE_UTIME)
51 # if defined(HAVE_SYS_UTIME_H)
52 # include <sys/utime.h>
53 # elif defined(HAVE_UTIME_H)
54 # include <utime.h>
55 # endif
56 #endif
58 #ifndef PIPE_BUF
59 /* Get the PIPE_BUF from pathconf */
60 #ifdef _PC_PIPE_BUF
61 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
62 #else
63 #define PIPE_BUF 512
64 #endif
65 #endif
67 #ifdef PHP_WIN32
68 # undef S_IFLNK
69 # undef S_IFIFO
70 #endif
72 private void close_and_restore(const struct magic_set *, const char *, int,
73 const struct stat *);
74 private int unreadable_info(struct magic_set *, mode_t, const char *);
75 #if 0
76 private const char* get_default_magic(void);
77 #endif
78 private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
80 #ifndef STDIN_FILENO
81 #define STDIN_FILENO 0
82 #endif
84 /* XXX this functionality is excluded in php, enable it in apprentice.c:340 */
85 #if 0
86 private const char *
87 get_default_magic(void)
89 static const char hmagic[] = "/.magic/magic.mgc";
90 static char *default_magic;
91 char *home, *hmagicpath;
93 #ifndef PHP_WIN32
94 struct stat st;
96 if (default_magic) {
97 free(default_magic);
98 default_magic = NULL;
100 if ((home = getenv("HOME")) == NULL)
101 return MAGIC;
103 if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
104 return MAGIC;
105 if (stat(hmagicpath, &st) == -1) {
106 free(hmagicpath);
107 if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
108 return MAGIC;
109 if (stat(hmagicpath, &st) == -1)
110 goto out;
111 if (S_ISDIR(st.st_mode)) {
112 free(hmagicpath);
113 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
114 return MAGIC;
115 if (access(hmagicpath, R_OK) == -1)
116 goto out;
120 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
121 goto out;
122 free(hmagicpath);
123 return default_magic;
124 out:
125 default_magic = NULL;
126 free(hmagicpath);
127 return MAGIC;
128 #else
129 char *hmagicp = hmagicpath;
130 char *tmppath = NULL;
131 LPTSTR dllpath;
133 #define APPENDPATH() \
134 do { \
135 if (tmppath && access(tmppath, R_OK) != -1) { \
136 if (hmagicpath == NULL) \
137 hmagicpath = tmppath; \
138 else { \
139 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
140 PATHSEP, tmppath) >= 0) { \
141 free(hmagicpath); \
142 hmagicpath = hmagicp; \
144 free(tmppath); \
146 tmppath = NULL; \
148 } while (/*CONSTCOND*/0)
150 if (default_magic) {
151 free(default_magic);
152 default_magic = NULL;
155 /* First, try to get user-specific magic file */
156 if ((home = getenv("LOCALAPPDATA")) == NULL) {
157 if ((home = getenv("USERPROFILE")) != NULL)
158 if (asprintf(&tmppath,
159 "%s/Local Settings/Application Data%s", home,
160 hmagic) < 0)
161 tmppath = NULL;
162 } else {
163 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
164 tmppath = NULL;
167 APPENDPATH();
169 /* Second, try to get a magic file from Common Files */
170 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
171 if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
172 APPENDPATH();
175 /* Third, try to get magic file relative to dll location */
176 dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
177 dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */
178 if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)) {
179 PathRemoveFileSpecA(dllpath);
180 if (strlen(dllpath) > 3 &&
181 stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
182 if (asprintf(&tmppath,
183 "%s/../share/misc/magic.mgc", dllpath) >= 0)
184 APPENDPATH();
185 } else {
186 if (asprintf(&tmppath,
187 "%s/share/misc/magic.mgc", dllpath) >= 0)
188 APPENDPATH();
189 else if (asprintf(&tmppath,
190 "%s/magic.mgc", dllpath) >= 0)
191 APPENDPATH();
195 /* Don't put MAGIC constant - it likely points to a file within MSys
196 tree */
197 default_magic = hmagicpath;
198 return default_magic;
199 #endif
202 public const char *
203 magic_getpath(const char *magicfile, int action)
205 if (magicfile != NULL)
206 return magicfile;
208 magicfile = getenv("MAGIC");
209 if (magicfile != NULL)
210 return magicfile;
212 return action == FILE_LOAD ? get_default_magic() : MAGIC;
214 #endif
216 public struct magic_set *
217 magic_open(int flags)
219 return file_ms_alloc(flags);
222 private int
223 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
225 if (!file) not_reached();
227 /* We cannot open it, but we were able to stat it. */
228 if (access(file, W_OK) == 0)
229 if (file_printf(ms, "writable, ") == -1)
230 return -1;
231 if (access(file, X_OK) == 0)
232 if (file_printf(ms, "executable, ") == -1)
233 return -1;
234 if (S_ISREG(md))
235 if (file_printf(ms, "regular file, ") == -1)
236 return -1;
237 if (file_printf(ms, "no read permission") == -1)
238 return -1;
239 return 0;
242 public void
243 magic_close(struct magic_set *ms)
245 if (ms == NULL)
246 return;
247 file_ms_free(ms);
251 * load a magic file
253 public int
254 magic_load(struct magic_set *ms, const char *magicfile)
256 if (ms == NULL)
257 return -1;
258 return file_apprentice(ms, magicfile, FILE_LOAD);
261 public int
262 magic_compile(struct magic_set *ms, const char *magicfile)
264 if (ms == NULL)
265 return -1;
266 return file_apprentice(ms, magicfile, FILE_COMPILE);
270 public int
271 magic_list(struct magic_set *ms, const char *magicfile)
273 if (ms == NULL)
274 return -1;
275 return file_apprentice(ms, magicfile, FILE_LIST);
278 private
279 void close_and_restore(const struct magic_set* ms, const char* name, int /*fd*/,
280 const struct stat* sb) {
282 if (name == NULL)
283 return;
285 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
287 * Try to restore access, modification times if read it.
288 * This is really *bad* because it will modify the status
289 * time of the file... And of course this will affect
290 * backup programs
292 #ifdef HAVE_UTIMES
293 struct timeval utsbuf[2];
294 (void)memset(utsbuf, 0, sizeof(utsbuf));
295 utsbuf[0].tv_sec = sb->st_atime;
296 utsbuf[1].tv_sec = sb->st_mtime;
298 (void) utimes(name, utsbuf); /* don't care if loses */
299 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
300 struct utimbuf utbuf;
302 (void)memset(&utbuf, 0, sizeof(utbuf));
303 utbuf.actime = sb->st_atime;
304 utbuf.modtime = sb->st_mtime;
305 (void) utime(name, &utbuf); /* don't care if loses */
306 #endif
312 * find type of descriptor
314 public
315 const char* magic_descriptor(struct magic_set* ms, int /*fd*/) {
316 if (ms == NULL)
317 return NULL;
318 return file_or_stream(ms, NULL, NULL);
322 * find type of named file
324 public const char *
325 magic_file(struct magic_set *ms, const char *inname)
327 if (ms == NULL)
328 return NULL;
329 return file_or_stream(ms, inname, NULL);
332 public const char *
333 magic_stream(struct magic_set *ms, php_stream *stream)
335 if (ms == NULL)
336 return NULL;
337 return file_or_stream(ms, NULL, stream);
340 private const char *
341 file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
343 int rv = -1;
344 unsigned char *buf;
345 struct stat sb;
346 ssize_t nbytes = 0; /* number of bytes read from a datafile */
347 HPHP::req::ptr<HPHP::File> file;
349 if (!inname && !stream) {
350 return NULL;
354 * one extra for terminating '\0', and
355 * some overlapping space for matches near EOF
357 #define SLOP (1 + sizeof(union VALUETYPE))
358 buf = (unsigned char*) emalloc(HOWMANY + SLOP);
360 if (file_reset(ms) == -1)
361 goto done;
363 switch (file_fsmagic(ms, inname, &sb, stream)) {
364 case -1: /* error */
365 goto done;
366 case 0: /* nothing found */
367 break;
368 default: /* matched it and printed type */
369 rv = 0;
370 goto done;
373 errno = 0;
375 if (!stream && inname) {
376 auto wrapper = HPHP::Stream::getWrapperFromURI(inname);
377 if (wrapper) file = wrapper->open(inname, "rb", 0, nullptr);
378 stream = file.get();
381 if (!stream) {
382 if (unreadable_info(ms, sb.st_mode, inname) == -1)
383 goto done;
384 rv = 0;
385 goto done;
388 #ifdef O_NONBLOCK
389 /* we should be already be in non blocking mode for network socket */
390 #endif
393 * try looking at the first HOWMANY bytes
395 if ((nbytes = stream->readImpl((char *)buf, HOWMANY)) < 0) {
396 file_error(ms, errno, "cannot read `%s'", inname);
397 goto done;
400 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
401 if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
402 goto done;
403 rv = 0;
404 done:
405 efree(buf);
407 close_and_restore(ms, inname, 0, &sb);
408 return rv == 0 ? file_getbuffer(ms) : NULL;
412 public const char *
413 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
415 if (ms == NULL)
416 return NULL;
417 if (file_reset(ms) == -1)
418 return NULL;
420 * The main work is done here!
421 * We have the file name and/or the data buffer to be identified.
423 if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
424 return NULL;
426 return file_getbuffer(ms);
429 public const char *
430 magic_error(struct magic_set *ms)
432 if (ms == NULL)
433 return "Magic database is not open";
434 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
437 public int
438 magic_errno(struct magic_set *ms)
440 if (ms == NULL)
441 return EINVAL;
442 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
445 public int
446 magic_setflags(struct magic_set *ms, int flags)
448 if (ms == NULL)
449 return -1;
450 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
451 if (flags & MAGIC_PRESERVE_ATIME)
452 return -1;
453 #endif
454 ms->flags = flags;
455 return 0;
458 public int
459 magic_version(void)
461 return MAGIC_VERSION;