ascmagic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.81 2011/03/15 22:16:29 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. #include "names.h"
  47. #define MAXLINELEN 300 /* longest sane line length */
  48. #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
  49. || (x) == 0x85 || (x) == '\f')
  50. private int ascmatch(const unsigned char *, const unichar *, size_t);
  51. private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
  52. private size_t trim_nuls(const unsigned char *, size_t);
  53. /*
  54. * Undo the NUL-termination kindly provided by process()
  55. * but leave at least one byte to look at
  56. */
  57. private size_t
  58. trim_nuls(const unsigned char *buf, size_t nbytes)
  59. {
  60. while (nbytes > 1 && buf[nbytes - 1] == '\0')
  61. nbytes--;
  62. return nbytes;
  63. }
  64. protected int
  65. file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
  66. {
  67. unichar *ubuf = NULL;
  68. size_t ulen;
  69. int rv = 1;
  70. const char *code = NULL;
  71. const char *code_mime = NULL;
  72. const char *type = NULL;
  73. if (ms->flags & MAGIC_APPLE)
  74. return 0;
  75. nbytes = trim_nuls(buf, nbytes);
  76. /* If file doesn't look like any sort of text, give up. */
  77. if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
  78. &type) == 0) {
  79. rv = 0;
  80. goto done;
  81. }
  82. rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
  83. type);
  84. done:
  85. if (ubuf)
  86. free(ubuf);
  87. return rv;
  88. }
  89. protected int
  90. file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
  91. size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
  92. const char *type)
  93. {
  94. unsigned char *utf8_buf = NULL, *utf8_end;
  95. size_t mlen, i;
  96. const struct names *p;
  97. int rv = -1;
  98. int mime = ms->flags & MAGIC_MIME;
  99. const char *subtype = NULL;
  100. const char *subtype_mime = NULL;
  101. int has_escapes = 0;
  102. int has_backspace = 0;
  103. int seen_cr = 0;
  104. int n_crlf = 0;
  105. int n_lf = 0;
  106. int n_cr = 0;
  107. int n_nel = 0;
  108. int score, curtype, executable = 0;
  109. size_t last_line_end = (size_t)-1;
  110. int has_long_lines = 0;
  111. if (ms->flags & MAGIC_APPLE)
  112. return 0;
  113. nbytes = trim_nuls(buf, nbytes);
  114. /* If we have fewer than 2 bytes, give up. */
  115. if (nbytes <= 1) {
  116. rv = 0;
  117. goto done;
  118. }
  119. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
  120. /* Convert ubuf to UTF-8 and try text soft magic */
  121. /* malloc size is a conservative overestimate; could be
  122. improved, or at least realloced after conversion. */
  123. mlen = ulen * 6;
  124. if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
  125. file_oomem(ms, mlen);
  126. goto done;
  127. }
  128. if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
  129. == NULL)
  130. goto done;
  131. if ((rv = file_softmagic(ms, utf8_buf,
  132. (size_t)(utf8_end - utf8_buf), TEXTTEST)) != 0)
  133. goto subtype_identified;
  134. else
  135. rv = -1;
  136. }
  137. /* look for tokens from names.h - this is expensive! */
  138. if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
  139. goto subtype_identified;
  140. i = 0;
  141. score = 0;
  142. curtype = -1;
  143. while (i < ulen) {
  144. size_t end;
  145. /* skip past any leading space */
  146. while (i < ulen && ISSPC(ubuf[i]))
  147. i++;
  148. if (i >= ulen)
  149. break;
  150. /* find the next whitespace */
  151. for (end = i + 1; end < nbytes; end++)
  152. if (ISSPC(ubuf[end]))
  153. break;
  154. /* compare the word thus isolated against the token list */
  155. for (p = names; p < names + NNAMES; p++) {
  156. if (ascmatch((const unsigned char *)p->name, ubuf + i,
  157. end - i)) {
  158. if (curtype == -1)
  159. curtype = p->type;
  160. else if (curtype != p->type) {
  161. score = p->score;
  162. curtype = p->type;
  163. } else
  164. score += p->score;
  165. if (score > 1) {
  166. subtype = types[p->type].human;
  167. subtype_mime = types[p->type].mime;
  168. goto subtype_identified;
  169. }
  170. }
  171. }
  172. i = end;
  173. }
  174. subtype_identified:
  175. /* Now try to discover other details about the file. */
  176. for (i = 0; i < ulen; i++) {
  177. if (ubuf[i] == '\n') {
  178. if (seen_cr)
  179. n_crlf++;
  180. else
  181. n_lf++;
  182. last_line_end = i;
  183. } else if (seen_cr)
  184. n_cr++;
  185. seen_cr = (ubuf[i] == '\r');
  186. if (seen_cr)
  187. last_line_end = i;
  188. if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
  189. n_nel++;
  190. last_line_end = i;
  191. }
  192. /* If this line is _longer_ than MAXLINELEN, remember it. */
  193. if (i > last_line_end + MAXLINELEN)
  194. has_long_lines = 1;
  195. if (ubuf[i] == '\033')
  196. has_escapes = 1;
  197. if (ubuf[i] == '\b')
  198. has_backspace = 1;
  199. }
  200. /* Beware, if the data has been truncated, the final CR could have
  201. been followed by a LF. If we have HOWMANY bytes, it indicates
  202. that the data might have been truncated, probably even before
  203. this function was called. */
  204. if (seen_cr && nbytes < HOWMANY)
  205. n_cr++;
  206. if (strcmp(type, "binary") == 0) {
  207. rv = 0;
  208. goto done;
  209. }
  210. if (mime) {
  211. if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
  212. if (subtype_mime) {
  213. if (file_printf(ms, "%s", subtype_mime) == -1)
  214. goto done;
  215. } else {
  216. if (file_printf(ms, "text/plain") == -1)
  217. goto done;
  218. }
  219. }
  220. } else {
  221. if (file_printedlen(ms)) {
  222. switch (file_replace(ms, " text$", ", ")) {
  223. case 0:
  224. switch (file_replace(ms, " text executable$",
  225. ", ")) {
  226. case 0:
  227. if (file_printf(ms, ", ") == -1)
  228. goto done;
  229. case -1:
  230. goto done;
  231. default:
  232. executable = 1;
  233. break;
  234. }
  235. break;
  236. case -1:
  237. goto done;
  238. default:
  239. break;
  240. }
  241. }
  242. if (file_printf(ms, "%s", code) == -1)
  243. goto done;
  244. if (subtype) {
  245. if (file_printf(ms, " %s", subtype) == -1)
  246. goto done;
  247. }
  248. if (file_printf(ms, " %s", type) == -1)
  249. goto done;
  250. if (executable)
  251. if (file_printf(ms, " executable") == -1)
  252. goto done;
  253. if (has_long_lines)
  254. if (file_printf(ms, ", with very long lines") == -1)
  255. goto done;
  256. /*
  257. * Only report line terminators if we find one other than LF,
  258. * or if we find none at all.
  259. */
  260. if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
  261. (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
  262. if (file_printf(ms, ", with") == -1)
  263. goto done;
  264. if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
  265. if (file_printf(ms, " no") == -1)
  266. goto done;
  267. } else {
  268. if (n_crlf) {
  269. if (file_printf(ms, " CRLF") == -1)
  270. goto done;
  271. if (n_cr || n_lf || n_nel)
  272. if (file_printf(ms, ",") == -1)
  273. goto done;
  274. }
  275. if (n_cr) {
  276. if (file_printf(ms, " CR") == -1)
  277. goto done;
  278. if (n_lf || n_nel)
  279. if (file_printf(ms, ",") == -1)
  280. goto done;
  281. }
  282. if (n_lf) {
  283. if (file_printf(ms, " LF") == -1)
  284. goto done;
  285. if (n_nel)
  286. if (file_printf(ms, ",") == -1)
  287. goto done;
  288. }
  289. if (n_nel)
  290. if (file_printf(ms, " NEL") == -1)
  291. goto done;
  292. }
  293. if (file_printf(ms, " line terminators") == -1)
  294. goto done;
  295. }
  296. if (has_escapes)
  297. if (file_printf(ms, ", with escape sequences") == -1)
  298. goto done;
  299. if (has_backspace)
  300. if (file_printf(ms, ", with overstriking") == -1)
  301. goto done;
  302. }
  303. rv = 1;
  304. done:
  305. if (utf8_buf)
  306. free(utf8_buf);
  307. return rv;
  308. }
  309. private int
  310. ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
  311. {
  312. size_t i;
  313. for (i = 0; i < ulen; i++) {
  314. if (s[i] != us[i])
  315. return 0;
  316. }
  317. if (s[i])
  318. return 0;
  319. else
  320. return 1;
  321. }
  322. /*
  323. * Encode Unicode string as UTF-8, returning pointer to character
  324. * after end of string, or NULL if an invalid character is found.
  325. */
  326. private unsigned char *
  327. encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
  328. {
  329. size_t i;
  330. unsigned char *end = buf + len;
  331. for (i = 0; i < ulen; i++) {
  332. if (ubuf[i] <= 0x7f) {
  333. if (end - buf < 1)
  334. return NULL;
  335. *buf++ = (unsigned char)ubuf[i];
  336. } else if (ubuf[i] <= 0x7ff) {
  337. if (end - buf < 2)
  338. return NULL;
  339. *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
  340. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  341. } else if (ubuf[i] <= 0xffff) {
  342. if (end - buf < 3)
  343. return NULL;
  344. *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
  345. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  346. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  347. } else if (ubuf[i] <= 0x1fffff) {
  348. if (end - buf < 4)
  349. return NULL;
  350. *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
  351. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  352. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  353. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  354. } else if (ubuf[i] <= 0x3ffffff) {
  355. if (end - buf < 5)
  356. return NULL;
  357. *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
  358. *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  359. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  360. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  361. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  362. } else if (ubuf[i] <= 0x7fffffff) {
  363. if (end - buf < 6)
  364. return NULL;
  365. *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
  366. *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
  367. *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  368. *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  369. *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  370. *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  371. } else /* Invalid character */
  372. return NULL;
  373. }
  374. return buf;
  375. }