ascmagic.c 9.5 KB

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