magic.c 16 KB

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