ascmagic.c 9.4 KB

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