ascmagic.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.98 2017/11/02 20:25:39 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(b->fbuf, b->flen);
  74. /* If file doesn't look like any sort of text, give up. */
  75. if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
  76. &type) == 0)
  77. rv = 0;
  78. else
  79. rv = file_ascmagic_with_encoding(ms, &bb,
  80. ubuf, ulen, code, type, text);
  81. free(ubuf);
  82. return rv;
  83. }
  84. protected int
  85. file_ascmagic_with_encoding(struct magic_set *ms,
  86. const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
  87. const char *type, int text)
  88. {
  89. struct buffer bb;
  90. const unsigned char *buf = b->fbuf;
  91. size_t nbytes = b->flen;
  92. unsigned char *utf8_buf = NULL, *utf8_end;
  93. size_t mlen, i;
  94. int rv = -1;
  95. int mime = ms->flags & MAGIC_MIME;
  96. const char *subtype = NULL;
  97. const char *subtype_mime = NULL;
  98. int has_escapes = 0;
  99. int has_backspace = 0;
  100. int seen_cr = 0;
  101. int n_crlf = 0;
  102. int n_lf = 0;
  103. int n_cr = 0;
  104. int n_nel = 0;
  105. int executable = 0;
  106. size_t last_line_end = (size_t)-1;
  107. int has_long_lines = 0;
  108. nbytes = trim_nuls(buf, nbytes);
  109. /* If we have fewer than 2 bytes, give up. */
  110. if (nbytes <= 1) {
  111. rv = 0;
  112. goto done;
  113. }
  114. if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
  115. /* Convert ubuf to UTF-8 and try text soft magic */
  116. /* malloc size is a conservative overestimate; could be
  117. improved, or at least realloced after conversion. */
  118. mlen = ulen * 6;
  119. if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
  120. file_oomem(ms, mlen);
  121. goto done;
  122. }
  123. if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
  124. == NULL)
  125. goto done;
  126. buffer_init(&bb, b->fd, utf8_buf,
  127. (size_t)(utf8_end - utf8_buf));
  128. if ((rv = file_softmagic(ms, &bb, NULL, NULL,
  129. TEXTTEST, text)) == 0)
  130. rv = -1;
  131. buffer_fini(&bb);
  132. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
  133. rv = rv == -1 ? 0 : 1;
  134. goto done;
  135. }
  136. }
  137. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
  138. return 0;
  139. /* Now try to discover other details about the file. */
  140. for (i = 0; i < ulen; i++) {
  141. if (ubuf[i] == '\n') {
  142. if (seen_cr)
  143. n_crlf++;
  144. else
  145. n_lf++;
  146. last_line_end = i;
  147. } else if (seen_cr)
  148. n_cr++;
  149. seen_cr = (ubuf[i] == '\r');
  150. if (seen_cr)
  151. last_line_end = i;
  152. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  153. n_nel++;
  154. last_line_end = i;
  155. }
  156. /* If this line is _longer_ than MAXLINELEN, remember it. */
  157. if (i > last_line_end + MAXLINELEN)
  158. has_long_lines = 1;
  159. if (ubuf[i] == '\033')
  160. has_escapes = 1;
  161. if (ubuf[i] == '\b')
  162. has_backspace = 1;
  163. }
  164. /* Beware, if the data has been truncated, the final CR could have
  165. been followed by a LF. If we have ms->bytes_max bytes, it indicates
  166. that the data might have been truncated, probably even before
  167. this function was called. */
  168. if (seen_cr && nbytes < ms->bytes_max)
  169. n_cr++;
  170. if (strcmp(type, "binary") == 0) {
  171. rv = 0;
  172. goto done;
  173. }
  174. if (mime) {
  175. if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
  176. if (subtype_mime) {
  177. if (file_printf(ms, "%s", subtype_mime) == -1)
  178. goto done;
  179. } else {
  180. if (file_printf(ms, "text/plain") == -1)
  181. goto done;
  182. }
  183. }
  184. } else {
  185. if (file_printedlen(ms)) {
  186. switch (file_replace(ms, " text$", ", ")) {
  187. case 0:
  188. switch (file_replace(ms, " text executable$",
  189. ", ")) {
  190. case 0:
  191. if (file_printf(ms, ", ") == -1)
  192. goto done;
  193. break;
  194. case -1:
  195. goto done;
  196. default:
  197. executable = 1;
  198. break;
  199. }
  200. break;
  201. case -1:
  202. goto done;
  203. default:
  204. break;
  205. }
  206. }
  207. if (file_printf(ms, "%s", code) == -1)
  208. goto done;
  209. if (subtype) {
  210. if (file_printf(ms, " %s", subtype) == -1)
  211. goto done;
  212. }
  213. if (file_printf(ms, " %s", type) == -1)
  214. goto done;
  215. if (executable)
  216. if (file_printf(ms, " executable") == -1)
  217. goto done;
  218. if (has_long_lines)
  219. if (file_printf(ms, ", with very long lines") == -1)
  220. goto done;
  221. /*
  222. * Only report line terminators if we find one other than LF,
  223. * or if we find none at all.
  224. */
  225. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  226. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  227. if (file_printf(ms, ", with") == -1)
  228. goto done;
  229. if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
  230. if (file_printf(ms, " no") == -1)
  231. goto done;
  232. } else {
  233. if (n_crlf) {
  234. if (file_printf(ms, " CRLF") == -1)
  235. goto done;
  236. if (n_cr || n_lf || n_nel)
  237. if (file_printf(ms, ",") == -1)
  238. goto done;
  239. }
  240. if (n_cr) {
  241. if (file_printf(ms, " CR") == -1)
  242. goto done;
  243. if (n_lf || n_nel)
  244. if (file_printf(ms, ",") == -1)
  245. goto done;
  246. }
  247. if (n_lf) {
  248. if (file_printf(ms, " LF") == -1)
  249. goto done;
  250. if (n_nel)
  251. if (file_printf(ms, ",") == -1)
  252. goto done;
  253. }
  254. if (n_nel)
  255. if (file_printf(ms, " NEL") == -1)
  256. goto done;
  257. }
  258. if (file_printf(ms, " line terminators") == -1)
  259. goto done;
  260. }
  261. if (has_escapes)
  262. if (file_printf(ms, ", with escape sequences") == -1)
  263. goto done;
  264. if (has_backspace)
  265. if (file_printf(ms, ", with overstriking") == -1)
  266. goto done;
  267. }
  268. rv = 1;
  269. done:
  270. free(utf8_buf);
  271. return rv;
  272. }
  273. /*
  274. * Encode Unicode string as UTF-8, returning pointer to character
  275. * after end of string, or NULL if an invalid character is found.
  276. */
  277. private unsigned char *
  278. encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
  279. {
  280. size_t i;
  281. unsigned char *end = buf + len;
  282. for (i = 0; i < ulen; i++) {
  283. if (ubuf[i] <= 0x7f) {
  284. if (end - buf < 1)
  285. return NULL;
  286. *buf++ = (unsigned char)ubuf[i];
  287. } else if (ubuf[i] <= 0x7ff) {
  288. if (end - buf < 2)
  289. return NULL;
  290. *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
  291. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  292. } else if (ubuf[i] <= 0xffff) {
  293. if (end - buf < 3)
  294. return NULL;
  295. *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
  296. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  297. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  298. } else if (ubuf[i] <= 0x1fffff) {
  299. if (end - buf < 4)
  300. return NULL;
  301. *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
  302. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  303. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  304. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  305. } else if (ubuf[i] <= 0x3ffffff) {
  306. if (end - buf < 5)
  307. return NULL;
  308. *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
  309. *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  310. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  311. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  312. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  313. } else if (ubuf[i] <= 0x7fffffff) {
  314. if (end - buf < 6)
  315. return NULL;
  316. *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
  317. *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
  318. *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  319. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  320. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  321. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  322. } else /* Invalid character */
  323. return NULL;
  324. }
  325. return buf;
  326. }