ascmagic.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.110 2021/12/06 15:33:00 christos Exp $")
  37. #endif /* lint */
  38. #include "magic.h"
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <stdlib.h>
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #define MAXLINELEN 300 /* longest sane line length */
  46. #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
  47. || (x) == 0x85 || (x) == '\f')
  48. private unsigned char *encode_utf8(unsigned char *, size_t, file_unichar_t *,
  49. 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. file_unichar_t *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, const struct buffer *b,
  92. file_unichar_t *ubuf, size_t ulen, const char *code, const char *type,
  93. 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. int has_escapes = 0;
  105. int has_backspace = 0;
  106. int seen_cr = 0;
  107. int n_crlf = 0;
  108. int n_lf = 0;
  109. int n_cr = 0;
  110. int n_nel = 0;
  111. int executable = 0;
  112. size_t last_line_end = CAST(size_t, -1);
  113. size_t has_long_lines = 0;
  114. nbytes = trim_nuls(buf, nbytes);
  115. /* If we have fewer than 2 bytes, give up. */
  116. if (nbytes <= 1) {
  117. rv = 0;
  118. goto done;
  119. }
  120. if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
  121. /* Convert ubuf to UTF-8 and try text soft magic */
  122. /* malloc size is a conservative overestimate; could be
  123. improved, or at least realloced after conversion. */
  124. mlen = ulen * 6;
  125. if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
  126. file_oomem(ms, mlen);
  127. goto done;
  128. }
  129. if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
  130. == NULL)
  131. goto done;
  132. buffer_init(&bb, b->fd, &b->st, utf8_buf,
  133. CAST(size_t, utf8_end - utf8_buf));
  134. if ((rv = file_softmagic(ms, &bb, NULL, NULL,
  135. TEXTTEST, text)) == 0)
  136. rv = -1;
  137. else
  138. need_separator = 1;
  139. buffer_fini(&bb);
  140. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  141. rv = rv == -1 ? 0 : 1;
  142. goto done;
  143. }
  144. }
  145. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  146. rv = 0;
  147. goto done;
  148. }
  149. /* Now try to discover other details about the file. */
  150. for (i = 0; i < ulen; i++) {
  151. if (ubuf[i] == '\n') {
  152. if (seen_cr)
  153. n_crlf++;
  154. else
  155. n_lf++;
  156. last_line_end = i;
  157. } else if (seen_cr)
  158. n_cr++;
  159. seen_cr = (ubuf[i] == '\r');
  160. if (seen_cr)
  161. last_line_end = i;
  162. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  163. n_nel++;
  164. last_line_end = i;
  165. }
  166. /* If this line is _longer_ than MAXLINELEN, remember it. */
  167. if (i > last_line_end + MAXLINELEN) {
  168. size_t ll = i - last_line_end;
  169. if (ll > has_long_lines)
  170. has_long_lines = ll;
  171. }
  172. if (ubuf[i] == '\033')
  173. has_escapes = 1;
  174. if (ubuf[i] == '\b')
  175. has_backspace = 1;
  176. }
  177. /* Beware, if the data has been truncated, the final CR could have
  178. been followed by a LF. If we have ms->bytes_max bytes, it indicates
  179. that the data might have been truncated, probably even before
  180. this function was called. */
  181. if (seen_cr && nbytes < ms->bytes_max)
  182. n_cr++;
  183. if (strcmp(type, "binary") == 0) {
  184. rv = 0;
  185. goto done;
  186. }
  187. len = file_printedlen(ms);
  188. if (mime) {
  189. if ((mime & MAGIC_MIME_TYPE) != 0) {
  190. if (len) {
  191. /*
  192. * Softmagic printed something, we
  193. * are either done, or we need a separator
  194. */
  195. if ((ms->flags & MAGIC_CONTINUE) == 0) {
  196. rv = 1;
  197. goto done;
  198. }
  199. if (need_separator && file_separator(ms) == -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 (%"
  242. SIZE_T_FORMAT "u)", has_long_lines) == -1)
  243. goto done;
  244. /*
  245. * Only report line terminators if we find one other than LF,
  246. * or if we find none at all.
  247. */
  248. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  249. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  250. if (file_printf(ms, ", with") == -1)
  251. goto done;
  252. if (n_crlf == 0 && n_cr == 0 &&
  253. n_nel == 0 && n_lf == 0) {
  254. if (file_printf(ms, " no") == -1)
  255. goto done;
  256. } else {
  257. if (n_crlf) {
  258. if (file_printf(ms, " CRLF") == -1)
  259. goto done;
  260. if (n_cr || n_lf || n_nel)
  261. if (file_printf(ms, ",") == -1)
  262. goto done;
  263. }
  264. if (n_cr) {
  265. if (file_printf(ms, " CR") == -1)
  266. goto done;
  267. if (n_lf || n_nel)
  268. if (file_printf(ms, ",") == -1)
  269. goto done;
  270. }
  271. if (n_lf) {
  272. if (file_printf(ms, " LF") == -1)
  273. goto done;
  274. if (n_nel)
  275. if (file_printf(ms, ",") == -1)
  276. goto done;
  277. }
  278. if (n_nel)
  279. if (file_printf(ms, " NEL") == -1)
  280. goto done;
  281. }
  282. if (file_printf(ms, " line terminators") == -1)
  283. goto done;
  284. }
  285. if (has_escapes)
  286. if (file_printf(ms, ", with escape sequences") == -1)
  287. goto done;
  288. if (has_backspace)
  289. if (file_printf(ms, ", with overstriking") == -1)
  290. goto done;
  291. }
  292. rv = 1;
  293. done:
  294. free(utf8_buf);
  295. return rv;
  296. }
  297. /*
  298. * Encode Unicode string as UTF-8, returning pointer to character
  299. * after end of string, or NULL if an invalid character is found.
  300. */
  301. private unsigned char *
  302. encode_utf8(unsigned char *buf, size_t len, file_unichar_t *ubuf, size_t ulen)
  303. {
  304. size_t i;
  305. unsigned char *end = buf + len;
  306. for (i = 0; i < ulen; i++) {
  307. if (ubuf[i] <= 0x7f) {
  308. if (end - buf < 1)
  309. return NULL;
  310. *buf++ = CAST(unsigned char, ubuf[i]);
  311. continue;
  312. }
  313. if (ubuf[i] <= 0x7ff) {
  314. if (end - buf < 2)
  315. return NULL;
  316. *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
  317. goto out1;
  318. }
  319. if (ubuf[i] <= 0xffff) {
  320. if (end - buf < 3)
  321. return NULL;
  322. *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
  323. goto out2;
  324. }
  325. if (ubuf[i] <= 0x1fffff) {
  326. if (end - buf < 4)
  327. return NULL;
  328. *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
  329. goto out3;
  330. }
  331. if (ubuf[i] <= 0x3ffffff) {
  332. if (end - buf < 5)
  333. return NULL;
  334. *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
  335. goto out4;
  336. }
  337. if (ubuf[i] <= 0x7fffffff) {
  338. if (end - buf < 6)
  339. return NULL;
  340. *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
  341. goto out5;
  342. }
  343. /* Invalid character */
  344. return NULL;
  345. out5: *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
  346. out4: *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  347. out3: *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  348. out2: *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  349. out1: *buf++ = CAST(unsigned char, ((ubuf[i] >> 0) & 0x3f) + 0x80);
  350. }
  351. return buf;
  352. }