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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * ASCII magic -- try to detect text encoding.
31 * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
32 * to handle character codes other than ASCII on a unified basis.
38 FILE_RCSID("@(#)$File: ascmagic.c,v 1.110 2021/12/06 15:33:00 christos Exp $")
49 #define MAXLINELEN 300 /* longest sane line length */
50 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
51 || (x) == 0x85 || (x) == '\f')
53 private unsigned char *encode_utf8(unsigned char *, size_t, file_unichar_t *,
55 private size_t trim_nuls(const unsigned char *, size_t);
58 * Undo the NUL-termination kindly provided by process()
59 * but leave at least one byte to look at
62 trim_nuls(const unsigned char *buf, size_t nbytes)
64 while (nbytes > 1 && buf[nbytes - 1] == '\0')
71 file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
73 file_unichar_t *ubuf = NULL;
78 const char *code = NULL;
79 const char *code_mime = NULL;
80 const char *type = NULL;
83 bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
85 * Avoid trimming at an odd byte if the original buffer was evenly
86 * sized; this avoids losing the last character on UTF-16 LE text
88 if ((bb.flen & 1) && !(b->flen & 1))
91 /* If file doesn't look like any sort of text, give up. */
92 if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
96 rv = file_ascmagic_with_encoding(ms, &bb,
97 ubuf, ulen, code, type, text);
105 file_ascmagic_with_encoding(struct magic_set *ms, const struct buffer *b,
106 file_unichar_t *ubuf, size_t ulen, const char *code, const char *type,
110 const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
111 size_t nbytes = b->flen;
112 unsigned char *utf8_buf = NULL, *utf8_end;
115 int mime = ms->flags & MAGIC_MIME;
116 int need_separator = 0;
118 const char *subtype = NULL;
121 int has_backspace = 0;
130 size_t last_line_end = CAST(size_t, -1);
131 size_t has_long_lines = 0;
133 nbytes = trim_nuls(buf, nbytes);
135 /* If we have fewer than 2 bytes, give up. */
141 if (ulen > 0 && (ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
142 /* Convert ubuf to UTF-8 and try text soft magic */
143 /* malloc size is a conservative overestimate; could be
144 improved, or at least realloced after conversion. */
146 if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
147 file_oomem(ms, mlen);
150 if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
153 buffer_init(&bb, b->fd, &b->st, utf8_buf,
154 CAST(size_t, utf8_end - utf8_buf));
156 if ((rv = file_softmagic(ms, &bb, NULL, NULL,
157 TEXTTEST, text)) == 0)
162 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
163 rv = rv == -1 ? 0 : 1;
168 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
173 /* Now try to discover other details about the file. */
174 for (i = 0; i < ulen; i++) {
175 if (ubuf[i] == '\n') {
184 seen_cr = (ubuf[i] == '\r');
188 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
193 /* If this line is _longer_ than MAXLINELEN, remember it. */
194 if (i > last_line_end + MAXLINELEN) {
195 size_t ll = i - last_line_end;
196 if (ll > has_long_lines)
200 if (ubuf[i] == '\033')
206 /* Beware, if the data has been truncated, the final CR could have
207 been followed by a LF. If we have ms->bytes_max bytes, it indicates
208 that the data might have been truncated, probably even before
209 this function was called. */
210 if (seen_cr && nbytes < ms->bytes_max)
213 if (strcmp(type, "binary") == 0) {
217 len = file_printedlen(ms);
219 if ((mime & MAGIC_MIME_TYPE) != 0) {
222 * Softmagic printed something, we
223 * are either done, or we need a separator
225 if ((ms->flags & MAGIC_CONTINUE) == 0) {
229 if (need_separator && file_separator(ms) == -1)
232 if (file_printf(ms, "text/plain") == -1)
238 switch (file_replace(ms, " text$", ", ")) {
240 switch (file_replace(ms, " text executable$",
243 if (file_printf(ms, ", ") == -1)
260 if (file_printf(ms, "%s", code) == -1)
264 if (file_printf(ms, " %s", subtype) == -1)
268 if (file_printf(ms, " %s", type) == -1)
272 if (file_printf(ms, " executable") == -1)
276 if (file_printf(ms, ", with very long lines (%"
277 SIZE_T_FORMAT "u)", has_long_lines) == -1)
281 * Only report line terminators if we find one other than LF,
282 * or if we find none at all.
284 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
285 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
286 if (file_printf(ms, ", with") == -1)
289 if (n_crlf == 0 && n_cr == 0 &&
290 n_nel == 0 && n_lf == 0) {
291 if (file_printf(ms, " no") == -1)
295 if (file_printf(ms, " CRLF") == -1)
297 if (n_cr || n_lf || n_nel)
298 if (file_printf(ms, ",") == -1)
302 if (file_printf(ms, " CR") == -1)
305 if (file_printf(ms, ",") == -1)
309 if (file_printf(ms, " LF") == -1)
312 if (file_printf(ms, ",") == -1)
316 if (file_printf(ms, " NEL") == -1)
320 if (file_printf(ms, " line terminators") == -1)
325 if (file_printf(ms, ", with escape sequences") == -1)
328 if (file_printf(ms, ", with overstriking") == -1)
339 * Encode Unicode string as UTF-8, returning pointer to character
340 * after end of string, or NULL if an invalid character is found.
342 private unsigned char *
343 encode_utf8(unsigned char *buf, size_t len, file_unichar_t *ubuf, size_t ulen)
346 unsigned char *end = buf + len;
348 for (i = 0; i < ulen; i++) {
349 if (ubuf[i] <= 0x7f) {
352 *buf++ = CAST(unsigned char, ubuf[i]);
355 if (ubuf[i] <= 0x7ff) {
358 *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
361 if (ubuf[i] <= 0xffff) {
364 *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
367 if (ubuf[i] <= 0x1fffff) {
370 *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
373 if (ubuf[i] <= 0x3ffffff) {
376 *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
379 if (ubuf[i] <= 0x7fffffff) {
382 *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
385 /* Invalid character */
387 out5: *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
388 out4: *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
389 out3: *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
390 out2: *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
391 out1: *buf++ = CAST(unsigned char, ((ubuf[i] >> 0) & 0x3f) + 0x80);