Line | Count | Source (jump to first uncovered line) |
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 | | #ifdef WIN32 |
29 | | #include <windows.h> |
30 | | #include <shlwapi.h> |
31 | | #endif |
32 | | |
33 | | #include "file.h" |
34 | | |
35 | | #ifndef lint |
36 | | FILE_RCSID("@(#)$File: magic.c,v 1.121 2023/02/09 17:45:19 christos Exp $") |
37 | | #endif /* lint */ |
38 | | |
39 | | #include "magic.h" |
40 | | |
41 | | #include <stdlib.h> |
42 | | #include <unistd.h> |
43 | | #include <string.h> |
44 | | #ifdef QUICK |
45 | | #include <sys/mman.h> |
46 | | #endif |
47 | | #include <limits.h> /* for PIPE_BUF */ |
48 | | |
49 | | #if defined(HAVE_UTIMES) |
50 | | # include <sys/time.h> |
51 | | #elif defined(HAVE_UTIME) |
52 | | # if defined(HAVE_SYS_UTIME_H) |
53 | | # include <sys/utime.h> |
54 | | # elif defined(HAVE_UTIME_H) |
55 | | # include <utime.h> |
56 | | # endif |
57 | | #endif |
58 | | |
59 | | #ifdef HAVE_UNISTD_H |
60 | | #include <unistd.h> /* for read() */ |
61 | | #endif |
62 | | |
63 | | #ifndef PIPE_BUF |
64 | | /* Get the PIPE_BUF from pathconf */ |
65 | | #ifdef _PC_PIPE_BUF |
66 | | #define PIPE_BUF pathconf(".", _PC_PIPE_BUF) |
67 | | #else |
68 | | #define PIPE_BUF 512 |
69 | | #endif |
70 | | #endif |
71 | | |
72 | | file_private void close_and_restore(const struct magic_set *, const char *, int, |
73 | | const struct stat *); |
74 | | file_private int unreadable_info(struct magic_set *, mode_t, const char *); |
75 | | file_private const char* get_default_magic(void); |
76 | | #ifndef COMPILE_ONLY |
77 | | file_private const char *file_or_fd(struct magic_set *, const char *, int); |
78 | | #endif |
79 | | |
80 | | #ifndef STDIN_FILENO |
81 | | #define STDIN_FILENO 0 |
82 | | #endif |
83 | | |
84 | | #ifdef WIN32 |
85 | | /* HINSTANCE of this shared library. Needed for get_default_magic() */ |
86 | | static HINSTANCE _w32_dll_instance = NULL; |
87 | | |
88 | | static void |
89 | | _w32_append_path(char **hmagicpath, const char *fmt, ...) |
90 | | { |
91 | | char *tmppath; |
92 | | char *newpath; |
93 | | va_list ap; |
94 | | |
95 | | va_start(ap, fmt); |
96 | | if (vasprintf(&tmppath, fmt, ap) < 0) { |
97 | | va_end(ap); |
98 | | return; |
99 | | } |
100 | | va_end(ap); |
101 | | |
102 | | if (access(tmppath, R_OK) == -1) |
103 | | goto out; |
104 | | |
105 | | if (*hmagicpath == NULL) { |
106 | | *hmagicpath = tmppath; |
107 | | return; |
108 | | } |
109 | | |
110 | | if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0) |
111 | | goto out; |
112 | | |
113 | | free(*hmagicpath); |
114 | | free(tmppath); |
115 | | *hmagicpath = newpath; |
116 | | return; |
117 | | out: |
118 | | free(tmppath); |
119 | | } |
120 | | |
121 | | static void |
122 | | _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module) |
123 | | { |
124 | | static const char *trypaths[] = { |
125 | | "%s/share/misc/magic.mgc", |
126 | | "%s/magic.mgc", |
127 | | }; |
128 | | LPSTR dllpath; |
129 | | size_t sp; |
130 | | |
131 | | dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath)); |
132 | | |
133 | | if (!GetModuleFileNameA(module, dllpath, MAX_PATH)) |
134 | | goto out; |
135 | | |
136 | | PathRemoveFileSpecA(dllpath); |
137 | | |
138 | | if (module) { |
139 | | char exepath[MAX_PATH]; |
140 | | GetModuleFileNameA(NULL, exepath, MAX_PATH); |
141 | | PathRemoveFileSpecA(exepath); |
142 | | if (stricmp(exepath, dllpath) == 0) |
143 | | goto out; |
144 | | } |
145 | | |
146 | | sp = strlen(dllpath); |
147 | | if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) { |
148 | | _w32_append_path(hmagicpath, |
149 | | "%s/../share/misc/magic.mgc", dllpath); |
150 | | goto out; |
151 | | } |
152 | | |
153 | | for (sp = 0; sp < __arraycount(trypaths); sp++) |
154 | | _w32_append_path(hmagicpath, trypaths[sp], dllpath); |
155 | | out: |
156 | | free(dllpath); |
157 | | } |
158 | | |
159 | | #ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY |
160 | | /* Placate GCC by offering a sacrificial previous prototype */ |
161 | | BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); |
162 | | |
163 | | BOOL WINAPI |
164 | | DllMain(HINSTANCE hinstDLL, DWORD fdwReason, |
165 | | LPVOID lpvReserved __attribute__((__unused__))) |
166 | | { |
167 | | if (fdwReason == DLL_PROCESS_ATTACH) |
168 | | _w32_dll_instance = hinstDLL; |
169 | | return 1; |
170 | | } |
171 | | #endif |
172 | | #endif |
173 | | |
174 | | file_private const char * |
175 | | get_default_magic(void) |
176 | 0 | { |
177 | 0 | static const char hmagic[] = "/.magic/magic.mgc"; |
178 | 0 | static char *default_magic; |
179 | 0 | char *home, *hmagicpath; |
180 | |
|
181 | 0 | #ifndef WIN32 |
182 | 0 | struct stat st; |
183 | |
|
184 | 0 | if (default_magic) { |
185 | 0 | free(default_magic); |
186 | 0 | default_magic = NULL; |
187 | 0 | } |
188 | 0 | if ((home = getenv("HOME")) == NULL) |
189 | 0 | return MAGIC; |
190 | | |
191 | 0 | if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) |
192 | 0 | return MAGIC; |
193 | 0 | if (stat(hmagicpath, &st) == -1) { |
194 | 0 | free(hmagicpath); |
195 | 0 | if (asprintf(&hmagicpath, "%s/.magic", home) < 0) |
196 | 0 | return MAGIC; |
197 | 0 | if (stat(hmagicpath, &st) == -1) |
198 | 0 | goto out; |
199 | 0 | if (S_ISDIR(st.st_mode)) { |
200 | 0 | free(hmagicpath); |
201 | 0 | if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0) |
202 | 0 | return MAGIC; |
203 | 0 | if (access(hmagicpath, R_OK) == -1) |
204 | 0 | goto out; |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 0 | if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) |
209 | 0 | goto out; |
210 | 0 | free(hmagicpath); |
211 | 0 | return default_magic; |
212 | 0 | out: |
213 | 0 | default_magic = NULL; |
214 | 0 | free(hmagicpath); |
215 | 0 | return MAGIC; |
216 | | #else |
217 | | hmagicpath = NULL; |
218 | | |
219 | | if (default_magic) { |
220 | | free(default_magic); |
221 | | default_magic = NULL; |
222 | | } |
223 | | |
224 | | /* Before anything else, try to get a magic file from user HOME */ |
225 | | if ((home = getenv("HOME")) != NULL) |
226 | | _w32_append_path(&hmagicpath, "%s%s", home, hmagic); |
227 | | |
228 | | /* First, try to get a magic file from user-application data */ |
229 | | if ((home = getenv("LOCALAPPDATA")) != NULL) |
230 | | _w32_append_path(&hmagicpath, "%s%s", home, hmagic); |
231 | | |
232 | | /* Second, try to get a magic file from the user profile data */ |
233 | | if ((home = getenv("USERPROFILE")) != NULL) |
234 | | _w32_append_path(&hmagicpath, |
235 | | "%s/Local Settings/Application Data%s", home, hmagic); |
236 | | |
237 | | /* Third, try to get a magic file from Common Files */ |
238 | | if ((home = getenv("COMMONPROGRAMFILES")) != NULL) |
239 | | _w32_append_path(&hmagicpath, "%s%s", home, hmagic); |
240 | | |
241 | | /* Fourth, try to get magic file relative to exe location */ |
242 | | _w32_get_magic_relative_to(&hmagicpath, NULL); |
243 | | |
244 | | /* Fifth, try to get magic file relative to dll location */ |
245 | | _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance); |
246 | | |
247 | | /* Avoid MAGIC constant - it likely points to a file within MSys tree */ |
248 | | default_magic = hmagicpath; |
249 | | return default_magic; |
250 | | #endif |
251 | 0 | } |
252 | | |
253 | | file_public const char * |
254 | | magic_getpath(const char *magicfile, int action) |
255 | 13.4k | { |
256 | 13.4k | if (magicfile != NULL) |
257 | 13.4k | return magicfile; |
258 | | |
259 | 0 | magicfile = getenv("MAGIC"); |
260 | 0 | if (magicfile != NULL) |
261 | 0 | return magicfile; |
262 | | |
263 | 0 | return action == FILE_LOAD ? get_default_magic() : MAGIC; |
264 | 0 | } |
265 | | |
266 | | file_public struct magic_set * |
267 | | magic_open(int flags) |
268 | 6.73k | { |
269 | 6.73k | return file_ms_alloc(flags); |
270 | 6.73k | } |
271 | | |
272 | | file_private int |
273 | | unreadable_info(struct magic_set *ms, mode_t md, const char *file) |
274 | 0 | { |
275 | 0 | if (file) { |
276 | | /* We cannot open it, but we were able to stat it. */ |
277 | 0 | if (access(file, W_OK) == 0) |
278 | 0 | if (file_printf(ms, "writable, ") == -1) |
279 | 0 | return -1; |
280 | 0 | #ifndef WIN32 |
281 | 0 | if (access(file, X_OK) == 0) |
282 | 0 | if (file_printf(ms, "executable, ") == -1) |
283 | 0 | return -1; |
284 | | #else |
285 | | /* X_OK doesn't work well on MS-Windows */ |
286 | | { |
287 | | const char *p = strrchr(file, '.'); |
288 | | if (p && (stricmp(p, ".exe") |
289 | | || stricmp(p, ".dll") |
290 | | || stricmp(p, ".bat") |
291 | | || stricmp(p, ".cmd"))) |
292 | | if (file_printf(ms, "writable, ") == -1) |
293 | | return -1; |
294 | | } |
295 | | #endif |
296 | 0 | } |
297 | 0 | if (S_ISREG(md)) |
298 | 0 | if (file_printf(ms, "regular file, ") == -1) |
299 | 0 | return -1; |
300 | 0 | if (file_printf(ms, "no read permission") == -1) |
301 | 0 | return -1; |
302 | 0 | return 0; |
303 | 0 | } |
304 | | |
305 | | file_public void |
306 | | magic_close(struct magic_set *ms) |
307 | 6.73k | { |
308 | 6.73k | if (ms == NULL) |
309 | 0 | return; |
310 | 6.73k | file_ms_free(ms); |
311 | 6.73k | } |
312 | | |
313 | | /* |
314 | | * load a magic file |
315 | | */ |
316 | | file_public int |
317 | | magic_load(struct magic_set *ms, const char *magicfile) |
318 | 0 | { |
319 | 0 | if (ms == NULL) |
320 | 0 | return -1; |
321 | 0 | return file_apprentice(ms, magicfile, FILE_LOAD); |
322 | 0 | } |
323 | | |
324 | | #ifndef COMPILE_ONLY |
325 | | /* |
326 | | * Install a set of compiled magic buffers. |
327 | | */ |
328 | | file_public int |
329 | | magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, |
330 | | size_t nbufs) |
331 | 0 | { |
332 | 0 | if (ms == NULL) |
333 | 0 | return -1; |
334 | 0 | return buffer_apprentice(ms, RCAST(struct magic **, bufs), |
335 | 0 | sizes, nbufs); |
336 | 0 | } |
337 | | #endif |
338 | | |
339 | | file_public int |
340 | | magic_compile(struct magic_set *ms, const char *magicfile) |
341 | 6.73k | { |
342 | 6.73k | if (ms == NULL) |
343 | 0 | return -1; |
344 | 6.73k | return file_apprentice(ms, magicfile, FILE_COMPILE); |
345 | 6.73k | } |
346 | | |
347 | | file_public int |
348 | | magic_check(struct magic_set *ms, const char *magicfile) |
349 | 6.73k | { |
350 | 6.73k | if (ms == NULL) |
351 | 0 | return -1; |
352 | 6.73k | return file_apprentice(ms, magicfile, FILE_CHECK); |
353 | 6.73k | } |
354 | | |
355 | | file_public int |
356 | | magic_list(struct magic_set *ms, const char *magicfile) |
357 | 0 | { |
358 | 0 | if (ms == NULL) |
359 | 0 | return -1; |
360 | 0 | return file_apprentice(ms, magicfile, FILE_LIST); |
361 | 0 | } |
362 | | |
363 | | file_private void |
364 | | close_and_restore(const struct magic_set *ms, const char *name, int fd, |
365 | | const struct stat *sb) |
366 | 0 | { |
367 | 0 | if (fd == STDIN_FILENO || name == NULL) |
368 | 0 | return; |
369 | 0 | (void) close(fd); |
370 | |
|
371 | 0 | if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { |
372 | | /* |
373 | | * Try to restore access, modification times if read it. |
374 | | * This is really *bad* because it will modify the status |
375 | | * time of the file... And of course this will affect |
376 | | * backup programs |
377 | | */ |
378 | 0 | #ifdef HAVE_UTIMES |
379 | 0 | struct timeval utsbuf[2]; |
380 | 0 | (void)memset(utsbuf, 0, sizeof(utsbuf)); |
381 | 0 | utsbuf[0].tv_sec = sb->st_atime; |
382 | 0 | utsbuf[1].tv_sec = sb->st_mtime; |
383 | |
|
384 | 0 | (void) utimes(name, utsbuf); /* don't care if loses */ |
385 | | #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) |
386 | | struct utimbuf utbuf; |
387 | | |
388 | | (void)memset(&utbuf, 0, sizeof(utbuf)); |
389 | | utbuf.actime = sb->st_atime; |
390 | | utbuf.modtime = sb->st_mtime; |
391 | | (void) utime(name, &utbuf); /* don't care if loses */ |
392 | | #endif |
393 | 0 | } |
394 | 0 | } |
395 | | |
396 | | #ifndef COMPILE_ONLY |
397 | | |
398 | | /* |
399 | | * find type of descriptor |
400 | | */ |
401 | | file_public const char * |
402 | | magic_descriptor(struct magic_set *ms, int fd) |
403 | 0 | { |
404 | 0 | if (ms == NULL) |
405 | 0 | return NULL; |
406 | 0 | return file_or_fd(ms, NULL, fd); |
407 | 0 | } |
408 | | |
409 | | /* |
410 | | * find type of named file |
411 | | */ |
412 | | file_public const char * |
413 | | magic_file(struct magic_set *ms, const char *inname) |
414 | 0 | { |
415 | 0 | if (ms == NULL) |
416 | 0 | return NULL; |
417 | 0 | return file_or_fd(ms, inname, STDIN_FILENO); |
418 | 0 | } |
419 | | |
420 | | file_private const char * |
421 | | file_or_fd(struct magic_set *ms, const char *inname, int fd) |
422 | 0 | { |
423 | 0 | int rv = -1; |
424 | 0 | unsigned char *buf; |
425 | 0 | struct stat sb; |
426 | 0 | ssize_t nbytes = 0; /* number of bytes read from a datafile */ |
427 | 0 | int ispipe = 0; |
428 | 0 | int okstat = 0; |
429 | 0 | off_t pos = CAST(off_t, -1); |
430 | |
|
431 | 0 | if (file_reset(ms, 1) == -1) |
432 | 0 | goto out; |
433 | | |
434 | | /* |
435 | | * one extra for terminating '\0', and |
436 | | * some overlapping space for matches near EOF |
437 | | */ |
438 | 0 | #define SLOP (1 + sizeof(union VALUETYPE)) |
439 | 0 | if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL) |
440 | 0 | return NULL; |
441 | | |
442 | 0 | switch (file_fsmagic(ms, inname, &sb)) { |
443 | 0 | case -1: /* error */ |
444 | 0 | goto done; |
445 | 0 | case 0: /* nothing found */ |
446 | 0 | break; |
447 | 0 | default: /* matched it and printed type */ |
448 | 0 | rv = 0; |
449 | 0 | goto done; |
450 | 0 | } |
451 | | |
452 | | #ifdef WIN32 |
453 | | /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */ |
454 | | if (fd == STDIN_FILENO) |
455 | | _setmode(STDIN_FILENO, O_BINARY); |
456 | | #endif |
457 | 0 | if (inname != NULL) { |
458 | 0 | int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC; |
459 | 0 | errno = 0; |
460 | 0 | if ((fd = open(inname, flags)) < 0) { |
461 | 0 | okstat = stat(inname, &sb) == 0; |
462 | | #ifdef WIN32 |
463 | | /* |
464 | | * Can't stat, can't open. It may have been opened in |
465 | | * fsmagic, so if the user doesn't have read permission, |
466 | | * allow it to say so; otherwise an error was probably |
467 | | * displayed in fsmagic. |
468 | | */ |
469 | | if (!okstat && errno == EACCES) { |
470 | | sb.st_mode = S_IFBLK; |
471 | | okstat = 1; |
472 | | } |
473 | | #endif |
474 | 0 | if (okstat && |
475 | 0 | unreadable_info(ms, sb.st_mode, inname) == -1) |
476 | 0 | goto done; |
477 | 0 | rv = 0; |
478 | 0 | goto done; |
479 | 0 | } |
480 | | #if O_CLOEXEC == 0 && defined(F_SETFD) |
481 | | (void)fcntl(fd, F_SETFD, FD_CLOEXEC); |
482 | | #endif |
483 | 0 | } |
484 | | |
485 | 0 | if (fd != -1) { |
486 | 0 | okstat = fstat(fd, &sb) == 0; |
487 | 0 | if (okstat && S_ISFIFO(sb.st_mode)) |
488 | 0 | ispipe = 1; |
489 | 0 | if (inname == NULL) |
490 | 0 | pos = lseek(fd, CAST(off_t, 0), SEEK_CUR); |
491 | 0 | } |
492 | | |
493 | | /* |
494 | | * try looking at the first ms->bytes_max bytes |
495 | | */ |
496 | 0 | if (ispipe) { |
497 | 0 | if (fd != -1) { |
498 | 0 | ssize_t r = 0; |
499 | |
|
500 | 0 | while ((r = sread(fd, RCAST(void *, &buf[nbytes]), |
501 | 0 | CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { |
502 | 0 | nbytes += r; |
503 | 0 | if (r < PIPE_BUF) break; |
504 | 0 | } |
505 | 0 | } |
506 | |
|
507 | 0 | if (nbytes == 0 && inname) { |
508 | | /* We can not read it, but we were able to stat it. */ |
509 | 0 | if (unreadable_info(ms, sb.st_mode, inname) == -1) |
510 | 0 | goto done; |
511 | 0 | rv = 0; |
512 | 0 | goto done; |
513 | 0 | } |
514 | |
|
515 | 0 | } else if (fd != -1) { |
516 | | /* Windows refuses to read from a big console buffer. */ |
517 | 0 | size_t howmany = |
518 | | #ifdef WIN32 |
519 | | _isatty(fd) ? 8 * 1024 : |
520 | | #endif |
521 | 0 | ms->bytes_max; |
522 | 0 | if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) { |
523 | 0 | if (inname == NULL && fd != STDIN_FILENO) |
524 | 0 | file_error(ms, errno, "cannot read fd %d", fd); |
525 | 0 | else |
526 | 0 | file_error(ms, errno, "cannot read `%s'", |
527 | 0 | inname == NULL ? "/dev/stdin" : inname); |
528 | 0 | goto done; |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | 0 | (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ |
533 | 0 | if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1) |
534 | 0 | goto done; |
535 | 0 | rv = 0; |
536 | 0 | done: |
537 | 0 | free(buf); |
538 | 0 | if (fd != -1) { |
539 | 0 | if (pos != CAST(off_t, -1)) |
540 | 0 | (void)lseek(fd, pos, SEEK_SET); |
541 | 0 | close_and_restore(ms, inname, fd, &sb); |
542 | 0 | } |
543 | 0 | out: |
544 | 0 | return rv == 0 ? file_getbuffer(ms) : NULL; |
545 | 0 | } |
546 | | |
547 | | |
548 | | file_public const char * |
549 | | magic_buffer(struct magic_set *ms, const void *buf, size_t nb) |
550 | 0 | { |
551 | 0 | if (ms == NULL) |
552 | 0 | return NULL; |
553 | 0 | if (file_reset(ms, 1) == -1) |
554 | 0 | return NULL; |
555 | | /* |
556 | | * The main work is done here! |
557 | | * We have the file name and/or the data buffer to be identified. |
558 | | */ |
559 | 0 | if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) { |
560 | 0 | return NULL; |
561 | 0 | } |
562 | 0 | return file_getbuffer(ms); |
563 | 0 | } |
564 | | #endif |
565 | | |
566 | | file_public const char * |
567 | | magic_error(struct magic_set *ms) |
568 | 0 | { |
569 | 0 | if (ms == NULL) |
570 | 0 | return "Magic database is not open"; |
571 | 0 | return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; |
572 | 0 | } |
573 | | |
574 | | file_public int |
575 | | magic_errno(struct magic_set *ms) |
576 | 0 | { |
577 | 0 | if (ms == NULL) |
578 | 0 | return EINVAL; |
579 | 0 | return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; |
580 | 0 | } |
581 | | |
582 | | file_public int |
583 | | magic_getflags(struct magic_set *ms) |
584 | 0 | { |
585 | 0 | if (ms == NULL) |
586 | 0 | return -1; |
587 | | |
588 | 0 | return ms->flags; |
589 | 0 | } |
590 | | |
591 | | file_public int |
592 | | magic_setflags(struct magic_set *ms, int flags) |
593 | 6.73k | { |
594 | 6.73k | if (ms == NULL) |
595 | 0 | return -1; |
596 | | #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) |
597 | | if (flags & MAGIC_PRESERVE_ATIME) |
598 | | return -1; |
599 | | #endif |
600 | 6.73k | ms->flags = flags; |
601 | 6.73k | return 0; |
602 | 6.73k | } |
603 | | |
604 | | file_public int |
605 | | magic_version(void) |
606 | 0 | { |
607 | 0 | return MAGIC_VERSION; |
608 | 0 | } |
609 | | |
610 | | file_public int |
611 | | magic_setparam(struct magic_set *ms, int param, const void *val) |
612 | 0 | { |
613 | 0 | if (ms == NULL) |
614 | 0 | return -1; |
615 | 0 | switch (param) { |
616 | 0 | case MAGIC_PARAM_INDIR_MAX: |
617 | 0 | ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val)); |
618 | 0 | return 0; |
619 | 0 | case MAGIC_PARAM_NAME_MAX: |
620 | 0 | ms->name_max = CAST(uint16_t, *CAST(const size_t *, val)); |
621 | 0 | return 0; |
622 | 0 | case MAGIC_PARAM_ELF_PHNUM_MAX: |
623 | 0 | ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val)); |
624 | 0 | return 0; |
625 | 0 | case MAGIC_PARAM_ELF_SHNUM_MAX: |
626 | 0 | ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val)); |
627 | 0 | return 0; |
628 | 0 | case MAGIC_PARAM_ELF_SHSIZE_MAX: |
629 | 0 | ms->elf_shsize_max = *CAST(const size_t *, val); |
630 | 0 | return 0; |
631 | 0 | case MAGIC_PARAM_ELF_NOTES_MAX: |
632 | 0 | ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val)); |
633 | 0 | return 0; |
634 | 0 | case MAGIC_PARAM_REGEX_MAX: |
635 | 0 | ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val)); |
636 | 0 | return 0; |
637 | 0 | case MAGIC_PARAM_BYTES_MAX: |
638 | 0 | ms->bytes_max = *CAST(const size_t *, val); |
639 | 0 | return 0; |
640 | 0 | case MAGIC_PARAM_ENCODING_MAX: |
641 | 0 | ms->encoding_max = *CAST(const size_t *, val); |
642 | 0 | return 0; |
643 | 0 | default: |
644 | 0 | errno = EINVAL; |
645 | 0 | return -1; |
646 | 0 | } |
647 | 0 | } |
648 | | |
649 | | file_public int |
650 | | magic_getparam(struct magic_set *ms, int param, void *val) |
651 | 0 | { |
652 | 0 | if (ms == NULL) |
653 | 0 | return -1; |
654 | 0 | switch (param) { |
655 | 0 | case MAGIC_PARAM_INDIR_MAX: |
656 | 0 | *CAST(size_t *, val) = ms->indir_max; |
657 | 0 | return 0; |
658 | 0 | case MAGIC_PARAM_NAME_MAX: |
659 | 0 | *CAST(size_t *, val) = ms->name_max; |
660 | 0 | return 0; |
661 | 0 | case MAGIC_PARAM_ELF_PHNUM_MAX: |
662 | 0 | *CAST(size_t *, val) = ms->elf_phnum_max; |
663 | 0 | return 0; |
664 | 0 | case MAGIC_PARAM_ELF_SHNUM_MAX: |
665 | 0 | *CAST(size_t *, val) = ms->elf_shnum_max; |
666 | 0 | return 0; |
667 | 0 | case MAGIC_PARAM_ELF_SHSIZE_MAX: |
668 | 0 | *CAST(size_t *, val) = ms->elf_shsize_max; |
669 | 0 | return 0; |
670 | 0 | case MAGIC_PARAM_ELF_NOTES_MAX: |
671 | 0 | *CAST(size_t *, val) = ms->elf_notes_max; |
672 | 0 | return 0; |
673 | 0 | case MAGIC_PARAM_REGEX_MAX: |
674 | 0 | *CAST(size_t *, val) = ms->regex_max; |
675 | 0 | return 0; |
676 | 0 | case MAGIC_PARAM_BYTES_MAX: |
677 | 0 | *CAST(size_t *, val) = ms->bytes_max; |
678 | 0 | return 0; |
679 | 0 | case MAGIC_PARAM_ENCODING_MAX: |
680 | 0 | *CAST(size_t *, val) = ms->encoding_max; |
681 | 0 | return 0; |
682 | 0 | default: |
683 | 0 | errno = EINVAL; |
684 | 0 | return -1; |
685 | 0 | } |
686 | 0 | } |