ascmagic.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.116 2023/05/21 16:08:50 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. file_private unsigned char *encode_utf8(unsigned char *, size_t, file_unichar_t *,
  49. size_t);
  50. file_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. file_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. file_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. file_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. size_t n_crlf = 0;
  108. size_t n_lf = 0;
  109. size_t n_cr = 0;
  110. size_t 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. rv = 0;
  132. goto done;
  133. }
  134. buffer_init(&bb, b->fd, &b->st, utf8_buf,
  135. CAST(size_t, utf8_end - utf8_buf));
  136. if ((rv = file_softmagic(ms, &bb, NULL, NULL,
  137. TEXTTEST, text)) == 0)
  138. rv = -1;
  139. else
  140. need_separator = 1;
  141. buffer_fini(&bb);
  142. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  143. rv = rv == -1 ? 0 : 1;
  144. goto done;
  145. }
  146. }
  147. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  148. rv = 0;
  149. goto done;
  150. }
  151. /* Now try to discover other details about the file. */
  152. for (i = 0; i < ulen; i++) {
  153. if (ubuf[i] == '\n') {
  154. if (seen_cr)
  155. n_crlf++;
  156. else
  157. n_lf++;
  158. last_line_end = i;
  159. } else if (seen_cr)
  160. n_cr++;
  161. seen_cr = (ubuf[i] == '\r');
  162. if (seen_cr)
  163. last_line_end = i;
  164. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  165. n_nel++;
  166. last_line_end = i;
  167. }
  168. /* If this line is _longer_ than MAXLINELEN, remember it. */
  169. if (i > last_line_end + MAXLINELEN) {
  170. size_t ll = i - last_line_end;
  171. if (ll > has_long_lines)
  172. has_long_lines = ll;
  173. }
  174. if (ubuf[i] == '\033')
  175. has_escapes = 1;
  176. if (ubuf[i] == '\b')
  177. has_backspace = 1;
  178. }
  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 (file_printf(ms, "text/plain") == -1)
  199. goto done;
  200. }
  201. } else {
  202. if (len) {
  203. switch (file_replace(ms, " text$", ", ")) {
  204. case 0:
  205. switch (file_replace(ms, " text executable$",
  206. ", ")) {
  207. case 0:
  208. if (file_printf(ms, ", ") == -1)
  209. goto done;
  210. break;
  211. case -1:
  212. goto done;
  213. default:
  214. executable = 1;
  215. break;
  216. }
  217. break;
  218. case -1:
  219. goto done;
  220. default:
  221. break;
  222. }
  223. }
  224. if (file_printf(ms, "%s", code) == -1)
  225. goto done;
  226. if (subtype) {
  227. if (file_printf(ms, " %s", subtype) == -1)
  228. goto done;
  229. }
  230. if (file_printf(ms, " %s", type) == -1)
  231. goto done;
  232. if (executable)
  233. if (file_printf(ms, " executable") == -1)
  234. goto done;
  235. if (has_long_lines)
  236. if (file_printf(ms, ", with very long lines (%"
  237. SIZE_T_FORMAT "u)", has_long_lines) == -1)
  238. goto done;
  239. /*
  240. * Only report line terminators if we find one other than LF,
  241. * or if we find none at all.
  242. */
  243. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  244. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  245. if (file_printf(ms, ", with") == -1)
  246. goto done;
  247. if (n_crlf == 0 && n_cr == 0 &&
  248. n_nel == 0 && n_lf == 0) {
  249. if (file_printf(ms, " no") == -1)
  250. goto done;
  251. } else {
  252. if (n_crlf) {
  253. if (file_printf(ms, " CRLF") == -1)
  254. goto done;
  255. if (n_cr || n_lf || n_nel)
  256. if (file_printf(ms, ",") == -1)
  257. goto done;
  258. }
  259. if (n_cr) {
  260. if (file_printf(ms, " CR") == -1)
  261. goto done;
  262. if (n_lf || n_nel)
  263. if (file_printf(ms, ",") == -1)
  264. goto done;
  265. }
  266. if (n_lf) {
  267. if (file_printf(ms, " LF") == -1)
  268. goto done;
  269. if (n_nel)
  270. if (file_printf(ms, ",") == -1)
  271. goto done;
  272. }
  273. if (n_nel)
  274. if (file_printf(ms, " NEL") == -1)
  275. goto done;
  276. }
  277. if (file_printf(ms, " line terminators") == -1)
  278. goto done;
  279. }
  280. if (has_escapes)
  281. if (file_printf(ms, ", with escape sequences") == -1)
  282. goto done;
  283. if (has_backspace)
  284. if (file_printf(ms, ", with overstriking") == -1)
  285. goto done;
  286. }
  287. rv = 1;
  288. done:
  289. free(utf8_buf);
  290. return rv;
  291. }
  292. /*
  293. * Encode Unicode string as UTF-8, returning pointer to character
  294. * after end of string, or NULL if an invalid character is found.
  295. */
  296. file_private unsigned char *
  297. encode_utf8(unsigned char *buf, size_t len, file_unichar_t *ubuf, size_t ulen)
  298. {
  299. size_t i;
  300. unsigned char *end = buf + len;
  301. for (i = 0; i < ulen; i++) {
  302. if (ubuf[i] <= 0x7f) {
  303. if (end - buf < 1)
  304. return NULL;
  305. *buf++ = CAST(unsigned char, ubuf[i]);
  306. continue;
  307. }
  308. if (ubuf[i] <= 0x7ff) {
  309. if (end - buf < 2)
  310. return NULL;
  311. *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
  312. goto out1;
  313. }
  314. if (ubuf[i] <= 0xffff) {
  315. if (end - buf < 3)
  316. return NULL;
  317. *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
  318. goto out2;
  319. }
  320. if (ubuf[i] <= 0x1fffff) {
  321. if (end - buf < 4)
  322. return NULL;
  323. *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
  324. goto out3;
  325. }
  326. if (ubuf[i] <= 0x3ffffff) {
  327. if (end - buf < 5)
  328. return NULL;
  329. *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
  330. goto out4;
  331. }
  332. if (ubuf[i] <= 0x7fffffff) {
  333. if (end - buf < 6)
  334. return NULL;
  335. *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
  336. goto out5;
  337. }
  338. /* Invalid character */
  339. return NULL;
  340. out5: *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
  341. out4: *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  342. out3: *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  343. out2: *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  344. out1: *buf++ = CAST(unsigned char, ((ubuf[i] >> 0) & 0x3f) + 0x80);
  345. }
  346. return buf;
  347. }