ascmagic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * ASCII magic -- try to detect text encoding.
  30. *
  31. * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
  32. * to handle character codes other than ASCII on a unified basis.
  33. */
  34. #include "file.h"
  35. #ifndef lint
  36. FILE_RCSID("@(#)$File: ascmagic.c,v 1.104 2019/05/07 02:27:11 christos Exp $")
  37. #endif /* lint */
  38. #include "magic.h"
  39. #include <string.h>
  40. #include <memory.h>
  41. #include <ctype.h>
  42. #include <stdlib.h>
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif
  46. #define MAXLINELEN 300 /* longest sane line length */
  47. #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
  48. || (x) == 0x85 || (x) == '\f')
  49. private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
  50. private size_t trim_nuls(const unsigned char *, size_t);
  51. /*
  52. * Undo the NUL-termination kindly provided by process()
  53. * but leave at least one byte to look at
  54. */
  55. private size_t
  56. trim_nuls(const unsigned char *buf, size_t nbytes)
  57. {
  58. while (nbytes > 1 && buf[nbytes - 1] == '\0')
  59. nbytes--;
  60. return nbytes;
  61. }
  62. protected int
  63. file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
  64. {
  65. unichar *ubuf = NULL;
  66. size_t ulen = 0;
  67. int rv = 1;
  68. struct buffer bb;
  69. const char *code = NULL;
  70. const char *code_mime = NULL;
  71. const char *type = NULL;
  72. bb = *b;
  73. bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
  74. /*
  75. * Avoid trimming at an odd byte if the original buffer was evenly
  76. * sized; this avoids losing the last character on UTF-16 LE text
  77. */
  78. if ((bb.flen & 1) && !(b->flen & 1))
  79. bb.flen++;
  80. /* If file doesn't look like any sort of text, give up. */
  81. if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
  82. &type) == 0)
  83. rv = 0;
  84. else
  85. rv = file_ascmagic_with_encoding(ms, &bb,
  86. ubuf, ulen, code, type, text);
  87. free(ubuf);
  88. return rv;
  89. }
  90. protected int
  91. file_ascmagic_with_encoding(struct magic_set *ms,
  92. const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
  93. const char *type, int text)
  94. {
  95. struct buffer bb;
  96. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  97. size_t nbytes = b->flen;
  98. unsigned char *utf8_buf = NULL, *utf8_end;
  99. size_t mlen, i, len;
  100. int rv = -1;
  101. int mime = ms->flags & MAGIC_MIME;
  102. int need_separator = 0;
  103. const char *subtype = NULL;
  104. const char *subtype_mime = NULL;
  105. int has_escapes = 0;
  106. int has_backspace = 0;
  107. int seen_cr = 0;
  108. int n_crlf = 0;
  109. int n_lf = 0;
  110. int n_cr = 0;
  111. int n_nel = 0;
  112. int executable = 0;
  113. size_t last_line_end = CAST(size_t, -1);
  114. int has_long_lines = 0;
  115. nbytes = trim_nuls(buf, nbytes);
  116. /* If we have fewer than 2 bytes, give up. */
  117. if (nbytes <= 1) {
  118. rv = 0;
  119. goto done;
  120. }
  121. if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
  122. /* Convert ubuf to UTF-8 and try text soft magic */
  123. /* malloc size is a conservative overestimate; could be
  124. improved, or at least realloced after conversion. */
  125. mlen = ulen * 6;
  126. if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
  127. file_oomem(ms, mlen);
  128. goto done;
  129. }
  130. if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
  131. == NULL)
  132. goto done;
  133. buffer_init(&bb, b->fd, &b->st, utf8_buf,
  134. CAST(size_t, utf8_end - utf8_buf));
  135. if ((rv = file_softmagic(ms, &bb, NULL, NULL,
  136. TEXTTEST, text)) == 0)
  137. rv = -1;
  138. else
  139. need_separator = 1;
  140. buffer_fini(&bb);
  141. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  142. rv = rv == -1 ? 0 : 1;
  143. goto done;
  144. }
  145. }
  146. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
  147. return 0;
  148. /* Now try to discover other details about the file. */
  149. for (i = 0; i < ulen; i++) {
  150. if (ubuf[i] == '\n') {
  151. if (seen_cr)
  152. n_crlf++;
  153. else
  154. n_lf++;
  155. last_line_end = i;
  156. } else if (seen_cr)
  157. n_cr++;
  158. seen_cr = (ubuf[i] == '\r');
  159. if (seen_cr)
  160. last_line_end = i;
  161. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  162. n_nel++;
  163. last_line_end = i;
  164. }
  165. /* If this line is _longer_ than MAXLINELEN, remember it. */
  166. if (i > last_line_end + MAXLINELEN)
  167. has_long_lines = 1;
  168. if (ubuf[i] == '\033')
  169. has_escapes = 1;
  170. if (ubuf[i] == '\b')
  171. has_backspace = 1;
  172. }
  173. /* Beware, if the data has been truncated, the final CR could have
  174. been followed by a LF. If we have ms->bytes_max bytes, it indicates
  175. that the data might have been truncated, probably even before
  176. this function was called. */
  177. if (seen_cr && nbytes < ms->bytes_max)
  178. n_cr++;
  179. if (strcmp(type, "binary") == 0) {
  180. rv = 0;
  181. goto done;
  182. }
  183. len = file_printedlen(ms);
  184. if (mime) {
  185. if ((mime & MAGIC_MIME_TYPE) != 0) {
  186. if (len) {
  187. /*
  188. * Softmagic printed something, we
  189. * are either done, or we need a separator
  190. */
  191. if ((ms->flags & MAGIC_CONTINUE) == 0) {
  192. rv = 1;
  193. goto done;
  194. }
  195. if (need_separator && file_separator(ms) == -1)
  196. goto done;
  197. }
  198. if (subtype_mime) {
  199. if (file_printf(ms, "%s", subtype_mime) == -1)
  200. goto done;
  201. } else {
  202. if (file_printf(ms, "text/plain") == -1)
  203. goto done;
  204. }
  205. }
  206. } else {
  207. if (len) {
  208. switch (file_replace(ms, " text$", ", ")) {
  209. case 0:
  210. switch (file_replace(ms, " text executable$",
  211. ", ")) {
  212. case 0:
  213. if (file_printf(ms, ", ") == -1)
  214. goto done;
  215. break;
  216. case -1:
  217. goto done;
  218. default:
  219. executable = 1;
  220. break;
  221. }
  222. break;
  223. case -1:
  224. goto done;
  225. default:
  226. break;
  227. }
  228. }
  229. if (file_printf(ms, "%s", code) == -1)
  230. goto done;
  231. if (subtype) {
  232. if (file_printf(ms, " %s", subtype) == -1)
  233. goto done;
  234. }
  235. if (file_printf(ms, " %s", type) == -1)
  236. goto done;
  237. if (executable)
  238. if (file_printf(ms, " executable") == -1)
  239. goto done;
  240. if (has_long_lines)
  241. if (file_printf(ms, ", with very long lines") == -1)
  242. goto done;
  243. /*
  244. * Only report line terminators if we find one other than LF,
  245. * or if we find none at all.
  246. */
  247. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  248. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  249. if (file_printf(ms, ", with") == -1)
  250. goto done;
  251. if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
  252. if (file_printf(ms, " no") == -1)
  253. goto done;
  254. } else {
  255. if (n_crlf) {
  256. if (file_printf(ms, " CRLF") == -1)
  257. goto done;
  258. if (n_cr || n_lf || n_nel)
  259. if (file_printf(ms, ",") == -1)
  260. goto done;
  261. }
  262. if (n_cr) {
  263. if (file_printf(ms, " CR") == -1)
  264. goto done;
  265. if (n_lf || n_nel)
  266. if (file_printf(ms, ",") == -1)
  267. goto done;
  268. }
  269. if (n_lf) {
  270. if (file_printf(ms, " LF") == -1)
  271. goto done;
  272. if (n_nel)
  273. if (file_printf(ms, ",") == -1)
  274. goto done;
  275. }
  276. if (n_nel)
  277. if (file_printf(ms, " NEL") == -1)
  278. goto done;
  279. }
  280. if (file_printf(ms, " line terminators") == -1)
  281. goto done;
  282. }
  283. if (has_escapes)
  284. if (file_printf(ms, ", with escape sequences") == -1)
  285. goto done;
  286. if (has_backspace)
  287. if (file_printf(ms, ", with overstriking") == -1)
  288. goto done;
  289. }
  290. rv = 1;
  291. done:
  292. free(utf8_buf);
  293. return rv;
  294. }
  295. /*
  296. * Encode Unicode string as UTF-8, returning pointer to character
  297. * after end of string, or NULL if an invalid character is found.
  298. */
  299. private unsigned char *
  300. encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
  301. {
  302. size_t i;
  303. unsigned char *end = buf + len;
  304. for (i = 0; i < ulen; i++) {
  305. if (ubuf[i] <= 0x7f) {
  306. if (end - buf < 1)
  307. return NULL;
  308. *buf++ = CAST(unsigned char, ubuf[i]);
  309. } else if (ubuf[i] <= 0x7ff) {
  310. if (end - buf < 2)
  311. return NULL;
  312. *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
  313. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  314. } else if (ubuf[i] <= 0xffff) {
  315. if (end - buf < 3)
  316. return NULL;
  317. *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
  318. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  319. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  320. } else if (ubuf[i] <= 0x1fffff) {
  321. if (end - buf < 4)
  322. return NULL;
  323. *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
  324. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  325. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  326. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  327. } else if (ubuf[i] <= 0x3ffffff) {
  328. if (end - buf < 5)
  329. return NULL;
  330. *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
  331. *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  332. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  333. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  334. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  335. } else if (ubuf[i] <= 0x7fffffff) {
  336. if (end - buf < 6)
  337. return NULL;
  338. *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
  339. *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
  340. *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  341. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  342. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  343. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  344. } else /* Invalid character */
  345. return NULL;
  346. }
  347. return buf;
  348. }