PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/fileinfo/libmagic/magic.c

http://github.com/php/php-src
C | 424 lines | 324 code | 46 blank | 54 comment | 78 complexity | 12c3f8f31df97ada08b0c57632315eb7 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. #include "file.h"
  28. #ifndef lint
  29. FILE_RCSID("@(#)$File: magic.c,v 1.111 2019/05/07 02:27:11 christos Exp $")
  30. #endif /* lint */
  31. #include "magic.h"
  32. #include <stdlib.h>
  33. #ifdef PHP_WIN32
  34. #include "win32/unistd.h"
  35. #else
  36. #include <unistd.h>
  37. #endif
  38. #include <string.h>
  39. #include "config.h"
  40. #ifdef PHP_WIN32
  41. #include <shlwapi.h>
  42. #endif
  43. #include <limits.h> /* for PIPE_BUF */
  44. #if defined(HAVE_UTIMES)
  45. # include <sys/time.h>
  46. #elif defined(HAVE_UTIME)
  47. # if defined(HAVE_SYS_UTIME_H)
  48. # include <sys/utime.h>
  49. # elif defined(HAVE_UTIME_H)
  50. # include <utime.h>
  51. # endif
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h> /* for read() */
  55. #endif
  56. #ifndef PIPE_BUF
  57. /* Get the PIPE_BUF from pathconf */
  58. #ifdef _PC_PIPE_BUF
  59. #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
  60. #else
  61. #define PIPE_BUF 512
  62. #endif
  63. #endif
  64. #ifdef PHP_WIN32
  65. # undef S_IFLNK
  66. # undef S_IFIFO
  67. #endif
  68. private int unreadable_info(struct magic_set *, mode_t, const char *);
  69. #if 0
  70. private const char* get_default_magic(void);
  71. #endif
  72. private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
  73. #ifndef STDIN_FILENO
  74. #define STDIN_FILENO 0
  75. #endif
  76. public struct magic_set *
  77. magic_open(int flags)
  78. {
  79. return file_ms_alloc(flags);
  80. }
  81. private int
  82. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  83. {
  84. if (file) {
  85. /* We cannot open it, but we were able to stat it. */
  86. if (access(file, W_OK) == 0)
  87. if (file_printf(ms, "writable, ") == -1)
  88. return -1;
  89. if (access(file, X_OK) == 0)
  90. if (file_printf(ms, "executable, ") == -1)
  91. return -1;
  92. }
  93. if (S_ISREG(md))
  94. if (file_printf(ms, "regular file, ") == -1)
  95. return -1;
  96. if (file_printf(ms, "no read permission") == -1)
  97. return -1;
  98. return 0;
  99. }
  100. public void
  101. magic_close(struct magic_set *ms)
  102. {
  103. if (ms == NULL)
  104. return;
  105. file_ms_free(ms);
  106. }
  107. /*
  108. * load a magic file
  109. */
  110. public int
  111. magic_load(struct magic_set *ms, const char *magicfile)
  112. {
  113. if (ms == NULL)
  114. return -1;
  115. return file_apprentice(ms, magicfile, FILE_LOAD);
  116. }
  117. public int
  118. magic_compile(struct magic_set *ms, const char *magicfile)
  119. {
  120. if (ms == NULL)
  121. return -1;
  122. return file_apprentice(ms, magicfile, FILE_COMPILE);
  123. }
  124. public int
  125. magic_check(struct magic_set *ms, const char *magicfile)
  126. {
  127. if (ms == NULL)
  128. return -1;
  129. return file_apprentice(ms, magicfile, FILE_CHECK);
  130. }
  131. public int
  132. magic_list(struct magic_set *ms, const char *magicfile)
  133. {
  134. if (ms == NULL)
  135. return -1;
  136. return file_apprentice(ms, magicfile, FILE_LIST);
  137. }
  138. #if 0
  139. private void
  140. close_and_restore(const struct magic_set *ms, const char *name, int fd,
  141. const zend_stat_t *sb)
  142. {
  143. if (fd == STDIN_FILENO || name == NULL)
  144. return;
  145. (void) close(fd);
  146. if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
  147. /*
  148. * Try to restore access, modification times if read it.
  149. * This is really *bad* because it will modify the status
  150. * time of the file... And of course this will affect
  151. * backup programs
  152. */
  153. #ifdef HAVE_UTIMES
  154. struct timeval utsbuf[2];
  155. (void)memset(utsbuf, 0, sizeof(utsbuf));
  156. utsbuf[0].tv_sec = sb->st_atime;
  157. utsbuf[1].tv_sec = sb->st_mtime;
  158. (void) utimes(name, utsbuf); /* don't care if loses */
  159. #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
  160. struct utimbuf utbuf;
  161. (void)memset(&utbuf, 0, sizeof(utbuf));
  162. utbuf.actime = sb->st_atime;
  163. utbuf.modtime = sb->st_mtime;
  164. (void) utime(name, &utbuf); /* don't care if loses */
  165. #endif
  166. }
  167. }
  168. #endif
  169. /*
  170. * find type of descriptor
  171. */
  172. public const char *
  173. magic_descriptor(struct magic_set *ms, int fd)
  174. {
  175. if (ms == NULL)
  176. return NULL;
  177. return file_or_stream(ms, NULL, NULL);
  178. }
  179. /*
  180. * find type of named file
  181. */
  182. public const char *
  183. magic_file(struct magic_set *ms, const char *inname)
  184. {
  185. if (ms == NULL)
  186. return NULL;
  187. return file_or_stream(ms, inname, NULL);
  188. }
  189. public const char *
  190. magic_stream(struct magic_set *ms, php_stream *stream)
  191. {
  192. if (ms == NULL)
  193. return NULL;
  194. return file_or_stream(ms, NULL, stream);
  195. }
  196. private const char *
  197. file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
  198. {
  199. int rv = -1;
  200. unsigned char *buf;
  201. zend_stat_t sb;
  202. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  203. int no_in_stream = 0;
  204. if (file_reset(ms, 1) == -1)
  205. goto out;
  206. /*
  207. * one extra for terminating '\0', and
  208. * some overlapping space for matches near EOF
  209. */
  210. #define SLOP (1 + sizeof(union VALUETYPE))
  211. if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
  212. return NULL;
  213. switch (file_fsmagic(ms, inname, &sb)) {
  214. case -1: /* error */
  215. goto done;
  216. case 0: /* nothing found */
  217. break;
  218. default: /* matched it and printed type */
  219. rv = 0;
  220. goto done;
  221. }
  222. errno = 0;
  223. if (inname && !stream) {
  224. no_in_stream = 1;
  225. stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
  226. if (!stream) {
  227. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  228. goto done;
  229. rv = -1;
  230. goto done;
  231. }
  232. }
  233. php_stream_statbuf ssb;
  234. if (php_stream_stat(stream, &ssb) < 0) {
  235. if (ms->flags & MAGIC_ERROR) {
  236. file_error(ms, errno, "cannot stat `%s'", inname);
  237. rv = -1;
  238. goto done;
  239. }
  240. }
  241. memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
  242. /*
  243. * try looking at the first ms->bytes_max bytes
  244. */
  245. if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
  246. file_error(ms, errno, "cannot read `%s'", inname);
  247. goto done;
  248. }
  249. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  250. if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
  251. goto done;
  252. rv = 0;
  253. done:
  254. efree(buf);
  255. if (no_in_stream && stream) {
  256. php_stream_close(stream);
  257. }
  258. out:
  259. return rv == 0 ? file_getbuffer(ms) : NULL;
  260. }
  261. public const char *
  262. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  263. {
  264. if (ms == NULL)
  265. return NULL;
  266. if (file_reset(ms, 1) == -1)
  267. return NULL;
  268. /*
  269. * The main work is done here!
  270. * We have the file name and/or the data buffer to be identified.
  271. */
  272. if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
  273. return NULL;
  274. }
  275. return file_getbuffer(ms);
  276. }
  277. public const char *
  278. magic_error(struct magic_set *ms)
  279. {
  280. if (ms == NULL)
  281. return "Magic database is not open";
  282. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  283. }
  284. public int
  285. magic_errno(struct magic_set *ms)
  286. {
  287. if (ms == NULL)
  288. return EINVAL;
  289. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  290. }
  291. public int
  292. magic_getflags(struct magic_set *ms)
  293. {
  294. if (ms == NULL)
  295. return -1;
  296. return ms->flags;
  297. }
  298. public int
  299. magic_setflags(struct magic_set *ms, int flags)
  300. {
  301. if (ms == NULL)
  302. return -1;
  303. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  304. if (flags & MAGIC_PRESERVE_ATIME)
  305. return -1;
  306. #endif
  307. ms->flags = flags;
  308. return 0;
  309. }
  310. public int
  311. magic_version(void)
  312. {
  313. return MAGIC_VERSION;
  314. }
  315. public int
  316. magic_setparam(struct magic_set *ms, int param, const void *val)
  317. {
  318. if (ms == NULL)
  319. return -1;
  320. switch (param) {
  321. case MAGIC_PARAM_INDIR_MAX:
  322. ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
  323. return 0;
  324. case MAGIC_PARAM_NAME_MAX:
  325. ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
  326. return 0;
  327. case MAGIC_PARAM_ELF_PHNUM_MAX:
  328. ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  329. return 0;
  330. case MAGIC_PARAM_ELF_SHNUM_MAX:
  331. ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  332. return 0;
  333. case MAGIC_PARAM_ELF_NOTES_MAX:
  334. ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
  335. return 0;
  336. case MAGIC_PARAM_REGEX_MAX:
  337. ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
  338. return 0;
  339. case MAGIC_PARAM_BYTES_MAX:
  340. ms->bytes_max = *CAST(const size_t *, val);
  341. return 0;
  342. default:
  343. errno = EINVAL;
  344. return -1;
  345. }
  346. }
  347. public int
  348. magic_getparam(struct magic_set *ms, int param, void *val)
  349. {
  350. if (ms == NULL)
  351. return -1;
  352. switch (param) {
  353. case MAGIC_PARAM_INDIR_MAX:
  354. *CAST(size_t *, val) = ms->indir_max;
  355. return 0;
  356. case MAGIC_PARAM_NAME_MAX:
  357. *CAST(size_t *, val) = ms->name_max;
  358. return 0;
  359. case MAGIC_PARAM_ELF_PHNUM_MAX:
  360. *CAST(size_t *, val) = ms->elf_phnum_max;
  361. return 0;
  362. case MAGIC_PARAM_ELF_SHNUM_MAX:
  363. *CAST(size_t *, val) = ms->elf_shnum_max;
  364. return 0;
  365. case MAGIC_PARAM_ELF_NOTES_MAX:
  366. *CAST(size_t *, val) = ms->elf_notes_max;
  367. return 0;
  368. case MAGIC_PARAM_REGEX_MAX:
  369. *CAST(size_t *, val) = ms->regex_max;
  370. return 0;
  371. case MAGIC_PARAM_BYTES_MAX:
  372. *CAST(size_t *, val) = ms->bytes_max;
  373. return 0;
  374. default:
  375. errno = EINVAL;
  376. return -1;
  377. }
  378. }