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