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
35 #include "file.h"
36
37 #ifndef lint
38 FILE_RCSID("@(#)$File: ascmagic.c,v 1.109 2021/02/05 23:01:40 christos Exp $")
39 #endif /* lint */
40
41 #include "magic.h"
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdlib.h>
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48
49 #define MAXLINELEN 300 /* longest sane line length */
50 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
51 || (x) == 0x85 || (x) == '\f')
52
53 private unsigned char *encode_utf8(unsigned char *, size_t, file_unichar_t *,
54 size_t);
55 private size_t trim_nuls(const unsigned char *, size_t);
56
57 /*
58 * Undo the NUL-termination kindly provided by process()
59 * but leave at least one byte to look at
60 */
61 private size_t
trim_nuls(const unsigned char * buf,size_t nbytes)62 trim_nuls(const unsigned char *buf, size_t nbytes)
63 {
64 while (nbytes > 1 && buf[nbytes - 1] == '\0')
65 nbytes--;
66
67 return nbytes;
68 }
69
70 protected int
file_ascmagic(struct magic_set * ms,const struct buffer * b,int text)71 file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
72 {
73 file_unichar_t *ubuf = NULL;
74 size_t ulen = 0;
75 int rv = 1;
76 struct buffer bb;
77
78 const char *code = NULL;
79 const char *code_mime = NULL;
80 const char *type = NULL;
81
82 bb = *b;
83 bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
84 /*
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
87 */
88 if ((bb.flen & 1) && !(b->flen & 1))
89 bb.flen++;
90
91 /* If file doesn't look like any sort of text, give up. */
92 if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
93 &type) == 0)
94 rv = 0;
95 else
96 rv = file_ascmagic_with_encoding(ms, &bb,
97 ubuf, ulen, code, type, text);
98
99 efree(ubuf);
100
101 return rv;
102 }
103
104 protected int
file_ascmagic_with_encoding(struct magic_set * ms,const struct buffer * b,file_unichar_t * ubuf,size_t ulen,const char * code,const char * type,int 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,
107 int text)
108 {
109 struct buffer bb;
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;
113 size_t mlen, i, len;
114 int rv = -1;
115 int mime = ms->flags & MAGIC_MIME;
116 int need_separator = 0;
117
118 const char *subtype = NULL;
119
120 int has_escapes = 0;
121 int has_backspace = 0;
122 int seen_cr = 0;
123
124 int n_crlf = 0;
125 int n_lf = 0;
126 int n_cr = 0;
127 int n_nel = 0;
128 int executable = 0;
129
130 size_t last_line_end = CAST(size_t, -1);
131 size_t has_long_lines = 0;
132
133 nbytes = trim_nuls(buf, nbytes);
134
135 /* If we have fewer than 2 bytes, give up. */
136 if (nbytes <= 1) {
137 rv = 0;
138 goto done;
139 }
140
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. */
145 mlen = ulen * 6;
146 if ((utf8_buf = CAST(unsigned char *, emalloc(mlen))) == NULL) {
147 file_oomem(ms, mlen);
148 goto done;
149 }
150 if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
151 == NULL) {
152 rv = 0;
153 goto done;
154 }
155 buffer_init(&bb, b->fd, &b->st, utf8_buf,
156 CAST(size_t, utf8_end - utf8_buf));
157
158 if ((rv = file_softmagic(ms, &bb, NULL, NULL,
159 TEXTTEST, text)) == 0)
160 rv = -1;
161 else
162 need_separator = 1;
163 buffer_fini(&bb);
164 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
165 rv = rv == -1 ? 0 : 1;
166 goto done;
167 }
168 }
169
170 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
171 rv = 0;
172 goto done;
173 }
174
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
186 seen_cr = (ubuf[i] == '\r');
187 if (seen_cr)
188 last_line_end = i;
189
190 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
191 n_nel++;
192 last_line_end = i;
193 }
194
195 /* If this line is _longer_ than MAXLINELEN, remember it. */
196 if (i > last_line_end + MAXLINELEN) {
197 size_t ll = i - last_line_end;
198 if (ll > has_long_lines)
199 has_long_lines = ll;
200 }
201
202 if (ubuf[i] == '\033')
203 has_escapes = 1;
204 if (ubuf[i] == '\b')
205 has_backspace = 1;
206 }
207
208 /* Beware, if the data has been truncated, the final CR could have
209 been followed by a LF. If we have ms->bytes_max bytes, it indicates
210 that the data might have been truncated, probably even before
211 this function was called. */
212 if (seen_cr && nbytes < ms->bytes_max)
213 n_cr++;
214
215 if (strcmp(type, "binary") == 0) {
216 rv = 0;
217 goto done;
218 }
219 len = file_printedlen(ms);
220 if (mime) {
221 if ((mime & MAGIC_MIME_TYPE) != 0) {
222 if (len) {
223 /*
224 * Softmagic printed something, we
225 * are either done, or we need a separator
226 */
227 if ((ms->flags & MAGIC_CONTINUE) == 0) {
228 rv = 1;
229 goto done;
230 }
231 if (need_separator && file_separator(ms) == -1)
232 goto done;
233 } else {
234 if (file_printf(ms, "text/plain") == -1)
235 goto done;
236 }
237 }
238 } else {
239 if (len) {
240 switch (file_replace(ms, " text$", ", ")) {
241 case 0:
242 switch (file_replace(ms, " text executable$",
243 ", ")) {
244 case 0:
245 if (file_printf(ms, ", ") == -1)
246 goto done;
247 break;
248 case -1:
249 goto done;
250 default:
251 executable = 1;
252 break;
253 }
254 break;
255 case -1:
256 goto done;
257 default:
258 break;
259 }
260 }
261
262 if (file_printf(ms, "%s", code) == -1)
263 goto done;
264
265 if (subtype) {
266 if (file_printf(ms, " %s", subtype) == -1)
267 goto done;
268 }
269
270 if (file_printf(ms, " %s", type) == -1)
271 goto done;
272
273 if (executable)
274 if (file_printf(ms, " executable") == -1)
275 goto done;
276
277 if (has_long_lines)
278 if (file_printf(ms, ", with very long lines (%zu)",
279 has_long_lines) == -1)
280 goto done;
281
282 /*
283 * Only report line terminators if we find one other than LF,
284 * or if we find none at all.
285 */
286 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
287 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
288 if (file_printf(ms, ", with") == -1)
289 goto done;
290
291 if (n_crlf == 0 && n_cr == 0 &&
292 n_nel == 0 && n_lf == 0) {
293 if (file_printf(ms, " no") == -1)
294 goto done;
295 } else {
296 if (n_crlf) {
297 if (file_printf(ms, " CRLF") == -1)
298 goto done;
299 if (n_cr || n_lf || n_nel)
300 if (file_printf(ms, ",") == -1)
301 goto done;
302 }
303 if (n_cr) {
304 if (file_printf(ms, " CR") == -1)
305 goto done;
306 if (n_lf || n_nel)
307 if (file_printf(ms, ",") == -1)
308 goto done;
309 }
310 if (n_lf) {
311 if (file_printf(ms, " LF") == -1)
312 goto done;
313 if (n_nel)
314 if (file_printf(ms, ",") == -1)
315 goto done;
316 }
317 if (n_nel)
318 if (file_printf(ms, " NEL") == -1)
319 goto done;
320 }
321
322 if (file_printf(ms, " line terminators") == -1)
323 goto done;
324 }
325
326 if (has_escapes)
327 if (file_printf(ms, ", with escape sequences") == -1)
328 goto done;
329 if (has_backspace)
330 if (file_printf(ms, ", with overstriking") == -1)
331 goto done;
332 }
333 rv = 1;
334 done:
335 if (utf8_buf)
336 efree(utf8_buf);
337
338 return rv;
339 }
340
341 /*
342 * Encode Unicode string as UTF-8, returning pointer to character
343 * after end of string, or NULL if an invalid character is found.
344 */
345 private unsigned char *
encode_utf8(unsigned char * buf,size_t len,file_unichar_t * ubuf,size_t ulen)346 encode_utf8(unsigned char *buf, size_t len, file_unichar_t *ubuf, size_t ulen)
347 {
348 size_t i;
349 unsigned char *end = buf + len;
350
351 for (i = 0; i < ulen; i++) {
352 if (ubuf[i] <= 0x7f) {
353 if (end - buf < 1)
354 return NULL;
355 *buf++ = CAST(unsigned char, ubuf[i]);
356 continue;
357 }
358 if (ubuf[i] <= 0x7ff) {
359 if (end - buf < 2)
360 return NULL;
361 *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
362 goto out1;
363 }
364 if (ubuf[i] <= 0xffff) {
365 if (end - buf < 3)
366 return NULL;
367 *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
368 goto out2;
369 }
370 if (ubuf[i] <= 0x1fffff) {
371 if (end - buf < 4)
372 return NULL;
373 *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
374 goto out3;
375 }
376 if (ubuf[i] <= 0x3ffffff) {
377 if (end - buf < 5)
378 return NULL;
379 *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
380 goto out4;
381 }
382 if (ubuf[i] <= 0x7fffffff) {
383 if (end - buf < 6)
384 return NULL;
385 *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
386 goto out5;
387 }
388 /* Invalid character */
389 return NULL;
390 out5: *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
391 out4: *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
392 out3: *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
393 out2: *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
394 out1: *buf++ = CAST(unsigned char, ((ubuf[i] >> 0) & 0x3f) + 0x80);
395 }
396
397 return buf;
398 }
399