ascmagic.c 10 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.105 2019/06/08 20:49:14 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, unichar *, size_t);
  49. private size_t trim_nuls(const unsigned char *, size_t);
  50. /*
  51. * Undo the NUL-termination kindly provided by process()
  52. * but leave at least one byte to look at
  53. */
  54. private size_t
  55. trim_nuls(const unsigned char *buf, size_t nbytes)
  56. {
  57. while (nbytes > 1 && buf[nbytes - 1] == '\0')
  58. nbytes--;
  59. return nbytes;
  60. }
  61. protected int
  62. file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
  63. {
  64. unichar *ubuf = NULL;
  65. size_t ulen = 0;
  66. int rv = 1;
  67. struct buffer bb;
  68. const char *code = NULL;
  69. const char *code_mime = NULL;
  70. const char *type = NULL;
  71. bb = *b;
  72. bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
  73. /*
  74. * Avoid trimming at an odd byte if the original buffer was evenly
  75. * sized; this avoids losing the last character on UTF-16 LE text
  76. */
  77. if ((bb.flen & 1) && !(b->flen & 1))
  78. bb.flen++;
  79. /* If file doesn't look like any sort of text, give up. */
  80. if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
  81. &type) == 0)
  82. rv = 0;
  83. else
  84. rv = file_ascmagic_with_encoding(ms, &bb,
  85. ubuf, ulen, code, type, text);
  86. free(ubuf);
  87. return rv;
  88. }
  89. protected int
  90. file_ascmagic_with_encoding(struct magic_set *ms,
  91. const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
  92. const char *type, int text)
  93. {
  94. struct buffer bb;
  95. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  96. size_t nbytes = b->flen;
  97. unsigned char *utf8_buf = NULL, *utf8_end;
  98. size_t mlen, i, len;
  99. int rv = -1;
  100. int mime = ms->flags & MAGIC_MIME;
  101. int need_separator = 0;
  102. const char *subtype = NULL;
  103. const char *subtype_mime = 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. int 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. return 0;
  147. /* Now try to discover other details about the file. */
  148. for (i = 0; i < ulen; i++) {
  149. if (ubuf[i] == '\n') {
  150. if (seen_cr)
  151. n_crlf++;
  152. else
  153. n_lf++;
  154. last_line_end = i;
  155. } else if (seen_cr)
  156. n_cr++;
  157. seen_cr = (ubuf[i] == '\r');
  158. if (seen_cr)
  159. last_line_end = i;
  160. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  161. n_nel++;
  162. last_line_end = i;
  163. }
  164. /* If this line is _longer_ than MAXLINELEN, remember it. */
  165. if (i > last_line_end + MAXLINELEN)
  166. has_long_lines = 1;
  167. if (ubuf[i] == '\033')
  168. has_escapes = 1;
  169. if (ubuf[i] == '\b')
  170. has_backspace = 1;
  171. }
  172. /* Beware, if the data has been truncated, the final CR could have
  173. been followed by a LF. If we have ms->bytes_max bytes, it indicates
  174. that the data might have been truncated, probably even before
  175. this function was called. */
  176. if (seen_cr && nbytes < ms->bytes_max)
  177. n_cr++;
  178. if (strcmp(type, "binary") == 0) {
  179. rv = 0;
  180. goto done;
  181. }
  182. len = file_printedlen(ms);
  183. if (mime) {
  184. if ((mime & MAGIC_MIME_TYPE) != 0) {
  185. if (len) {
  186. /*
  187. * Softmagic printed something, we
  188. * are either done, or we need a separator
  189. */
  190. if ((ms->flags & MAGIC_CONTINUE) == 0) {
  191. rv = 1;
  192. goto done;
  193. }
  194. if (need_separator && file_separator(ms) == -1)
  195. goto done;
  196. }
  197. if (subtype_mime) {
  198. if (file_printf(ms, "%s", subtype_mime) == -1)
  199. goto done;
  200. } else {
  201. if (file_printf(ms, "text/plain") == -1)
  202. goto done;
  203. }
  204. }
  205. } else {
  206. if (len) {
  207. switch (file_replace(ms, " text$", ", ")) {
  208. case 0:
  209. switch (file_replace(ms, " text executable$",
  210. ", ")) {
  211. case 0:
  212. if (file_printf(ms, ", ") == -1)
  213. goto done;
  214. break;
  215. case -1:
  216. goto done;
  217. default:
  218. executable = 1;
  219. break;
  220. }
  221. break;
  222. case -1:
  223. goto done;
  224. default:
  225. break;
  226. }
  227. }
  228. if (file_printf(ms, "%s", code) == -1)
  229. goto done;
  230. if (subtype) {
  231. if (file_printf(ms, " %s", subtype) == -1)
  232. goto done;
  233. }
  234. if (file_printf(ms, " %s", type) == -1)
  235. goto done;
  236. if (executable)
  237. if (file_printf(ms, " executable") == -1)
  238. goto done;
  239. if (has_long_lines)
  240. if (file_printf(ms, ", with very long lines") == -1)
  241. goto done;
  242. /*
  243. * Only report line terminators if we find one other than LF,
  244. * or if we find none at all.
  245. */
  246. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  247. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  248. if (file_printf(ms, ", with") == -1)
  249. goto done;
  250. if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
  251. if (file_printf(ms, " no") == -1)
  252. goto done;
  253. } else {
  254. if (n_crlf) {
  255. if (file_printf(ms, " CRLF") == -1)
  256. goto done;
  257. if (n_cr || n_lf || n_nel)
  258. if (file_printf(ms, ",") == -1)
  259. goto done;
  260. }
  261. if (n_cr) {
  262. if (file_printf(ms, " CR") == -1)
  263. goto done;
  264. if (n_lf || n_nel)
  265. if (file_printf(ms, ",") == -1)
  266. goto done;
  267. }
  268. if (n_lf) {
  269. if (file_printf(ms, " LF") == -1)
  270. goto done;
  271. if (n_nel)
  272. if (file_printf(ms, ",") == -1)
  273. goto done;
  274. }
  275. if (n_nel)
  276. if (file_printf(ms, " NEL") == -1)
  277. goto done;
  278. }
  279. if (file_printf(ms, " line terminators") == -1)
  280. goto done;
  281. }
  282. if (has_escapes)
  283. if (file_printf(ms, ", with escape sequences") == -1)
  284. goto done;
  285. if (has_backspace)
  286. if (file_printf(ms, ", with overstriking") == -1)
  287. goto done;
  288. }
  289. rv = 1;
  290. done:
  291. free(utf8_buf);
  292. return rv;
  293. }
  294. /*
  295. * Encode Unicode string as UTF-8, returning pointer to character
  296. * after end of string, or NULL if an invalid character is found.
  297. */
  298. private unsigned char *
  299. encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
  300. {
  301. size_t i;
  302. unsigned char *end = buf + len;
  303. for (i = 0; i < ulen; i++) {
  304. if (ubuf[i] <= 0x7f) {
  305. if (end - buf < 1)
  306. return NULL;
  307. *buf++ = CAST(unsigned char, ubuf[i]);
  308. } else if (ubuf[i] <= 0x7ff) {
  309. if (end - buf < 2)
  310. return NULL;
  311. *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
  312. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  313. } else if (ubuf[i] <= 0xffff) {
  314. if (end - buf < 3)
  315. return NULL;
  316. *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
  317. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  318. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  319. } else if (ubuf[i] <= 0x1fffff) {
  320. if (end - buf < 4)
  321. return NULL;
  322. *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
  323. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  324. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  325. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  326. } else if (ubuf[i] <= 0x3ffffff) {
  327. if (end - buf < 5)
  328. return NULL;
  329. *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
  330. *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  331. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  332. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  333. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  334. } else if (ubuf[i] <= 0x7fffffff) {
  335. if (end - buf < 6)
  336. return NULL;
  337. *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
  338. *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
  339. *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  340. *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  341. *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  342. *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  343. } else /* Invalid character */
  344. return NULL;
  345. }
  346. return buf;
  347. }