]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/file/src/ascmagic.c
MFV r357712: file 5.38.
[FreeBSD/FreeBSD.git] / contrib / file / src / ascmagic.c
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.105 2019/06/08 20:49:14 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, unichar *, size_t);
54 private size_t trim_nuls(const unsigned char *, size_t);
55
56 /*
57  * Undo the NUL-termination kindly provided by process()
58  * but leave at least one byte to look at
59  */
60 private size_t
61 trim_nuls(const unsigned char *buf, size_t nbytes)
62 {
63         while (nbytes > 1 && buf[nbytes - 1] == '\0')
64                 nbytes--;
65
66         return nbytes;
67 }
68
69 protected int
70 file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
71 {
72         unichar *ubuf = NULL;
73         size_t ulen = 0;
74         int rv = 1;
75         struct buffer bb;
76
77         const char *code = NULL;
78         const char *code_mime = NULL;
79         const char *type = NULL;
80
81         bb = *b;
82         bb.flen = trim_nuls(CAST(const unsigned char *, b->fbuf), b->flen);
83         /*
84          * Avoid trimming at an odd byte if the original buffer was evenly
85          * sized; this avoids losing the last character on UTF-16 LE text
86          */
87         if ((bb.flen & 1) && !(b->flen & 1))
88                 bb.flen++;
89
90         /* If file doesn't look like any sort of text, give up. */
91         if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime,
92             &type) == 0)
93                 rv = 0;
94         else
95                 rv = file_ascmagic_with_encoding(ms, &bb,
96                     ubuf, ulen, code, type, text);
97
98         free(ubuf);
99
100         return rv;
101 }
102
103 protected int
104 file_ascmagic_with_encoding(struct magic_set *ms,
105     const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
106     const char *type, int text)
107 {
108         struct buffer bb;
109         const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
110         size_t nbytes = b->flen;
111         unsigned char *utf8_buf = NULL, *utf8_end;
112         size_t mlen, i, len;
113         int rv = -1;
114         int mime = ms->flags & MAGIC_MIME;
115         int need_separator = 0;
116
117         const char *subtype = NULL;
118         const char *subtype_mime = 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         int 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 *, malloc(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                         goto done;
153                 buffer_init(&bb, b->fd, &b->st, utf8_buf,
154                     CAST(size_t, utf8_end - utf8_buf));
155
156                 if ((rv = file_softmagic(ms, &bb, NULL, NULL,
157                     TEXTTEST, text)) == 0)
158                         rv = -1;
159                 else
160                         need_separator = 1;
161                 buffer_fini(&bb);
162                 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
163                         rv = rv == -1 ? 0 : 1;
164                         goto done;
165                 }
166         }
167         if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
168                 return 0;
169
170         /* Now try to discover other details about the file. */
171         for (i = 0; i < ulen; i++) {
172                 if (ubuf[i] == '\n') {
173                         if (seen_cr)
174                                 n_crlf++;
175                         else
176                                 n_lf++;
177                         last_line_end = i;
178                 } else if (seen_cr)
179                         n_cr++;
180
181                 seen_cr = (ubuf[i] == '\r');
182                 if (seen_cr)
183                         last_line_end = i;
184
185                 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
186                         n_nel++;
187                         last_line_end = i;
188                 }
189
190                 /* If this line is _longer_ than MAXLINELEN, remember it. */
191                 if (i > last_line_end + MAXLINELEN)
192                         has_long_lines = 1;
193
194                 if (ubuf[i] == '\033')
195                         has_escapes = 1;
196                 if (ubuf[i] == '\b')
197                         has_backspace = 1;
198         }
199
200         /* Beware, if the data has been truncated, the final CR could have
201            been followed by a LF.  If we have ms->bytes_max bytes, it indicates
202            that the data might have been truncated, probably even before
203            this function was called. */
204         if (seen_cr && nbytes < ms->bytes_max)
205                 n_cr++;
206
207         if (strcmp(type, "binary") == 0) {
208                 rv = 0;
209                 goto done;
210         }
211         len = file_printedlen(ms);
212         if (mime) {
213                 if ((mime & MAGIC_MIME_TYPE) != 0) {
214                         if (len) {
215                                 /*
216                                  * Softmagic printed something, we
217                                  * are either done, or we need a separator
218                                  */
219                                 if ((ms->flags & MAGIC_CONTINUE) == 0) {
220                                         rv = 1;
221                                         goto done;
222                                 }
223                                 if (need_separator && file_separator(ms) == -1)
224                                         goto done;
225                         }
226                         if (subtype_mime) {
227                                 if (file_printf(ms, "%s", subtype_mime) == -1)
228                                         goto done;
229                         } else {
230                                 if (file_printf(ms, "text/plain") == -1)
231                                         goto done;
232                         }
233                 }
234         } else {
235                 if (len) {
236                         switch (file_replace(ms, " text$", ", ")) {
237                         case 0:
238                                 switch (file_replace(ms, " text executable$",
239                                     ", ")) {
240                                 case 0:
241                                         if (file_printf(ms, ", ") == -1)
242                                                 goto done;
243                                         break;
244                                 case -1:
245                                         goto done;
246                                 default:
247                                         executable = 1;
248                                         break;
249                                 }
250                                 break;
251                         case -1:
252                                 goto done;
253                         default:
254                                 break;
255                         }
256                 }
257
258                 if (file_printf(ms, "%s", code) == -1)
259                         goto done;
260
261                 if (subtype) {
262                         if (file_printf(ms, " %s", subtype) == -1)
263                                 goto done;
264                 }
265
266                 if (file_printf(ms, " %s", type) == -1)
267                         goto done;
268
269                 if (executable)
270                         if (file_printf(ms, " executable") == -1)
271                                 goto done;
272
273                 if (has_long_lines)
274                         if (file_printf(ms, ", with very long lines") == -1)
275                                 goto done;
276
277                 /*
278                  * Only report line terminators if we find one other than LF,
279                  * or if we find none at all.
280                  */
281                 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
282                     (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
283                         if (file_printf(ms, ", with") == -1)
284                                 goto done;
285
286                         if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
287                                 if (file_printf(ms, " no") == -1)
288                                         goto done;
289                         } else {
290                                 if (n_crlf) {
291                                         if (file_printf(ms, " CRLF") == -1)
292                                                 goto done;
293                                         if (n_cr || n_lf || n_nel)
294                                                 if (file_printf(ms, ",") == -1)
295                                                         goto done;
296                                 }
297                                 if (n_cr) {
298                                         if (file_printf(ms, " CR") == -1)
299                                                 goto done;
300                                         if (n_lf || n_nel)
301                                                 if (file_printf(ms, ",") == -1)
302                                                         goto done;
303                                 }
304                                 if (n_lf) {
305                                         if (file_printf(ms, " LF") == -1)
306                                                 goto done;
307                                         if (n_nel)
308                                                 if (file_printf(ms, ",") == -1)
309                                                         goto done;
310                                 }
311                                 if (n_nel)
312                                         if (file_printf(ms, " NEL") == -1)
313                                                 goto done;
314                         }
315
316                         if (file_printf(ms, " line terminators") == -1)
317                                 goto done;
318                 }
319
320                 if (has_escapes)
321                         if (file_printf(ms, ", with escape sequences") == -1)
322                                 goto done;
323                 if (has_backspace)
324                         if (file_printf(ms, ", with overstriking") == -1)
325                                 goto done;
326         }
327         rv = 1;
328 done:
329         free(utf8_buf);
330
331         return rv;
332 }
333
334 /*
335  * Encode Unicode string as UTF-8, returning pointer to character
336  * after end of string, or NULL if an invalid character is found.
337  */
338 private unsigned char *
339 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
340 {
341         size_t i;
342         unsigned char *end = buf + len;
343
344         for (i = 0; i < ulen; i++) {
345                 if (ubuf[i] <= 0x7f) {
346                         if (end - buf < 1)
347                                 return NULL;
348                         *buf++ = CAST(unsigned char, ubuf[i]);
349                 } else if (ubuf[i] <= 0x7ff) {
350                         if (end - buf < 2)
351                                 return NULL;
352                         *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
353                         *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
354                 } else if (ubuf[i] <= 0xffff) {
355                         if (end - buf < 3)
356                                 return NULL;
357                         *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
358                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
359                         *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
360                 } else if (ubuf[i] <= 0x1fffff) {
361                         if (end - buf < 4)
362                                 return NULL;
363                         *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
364                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
365                         *buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
366                         *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
367                 } else if (ubuf[i] <= 0x3ffffff) {
368                         if (end - buf < 5)
369                                 return NULL;
370                         *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
371                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
372                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
373                         *buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
374                         *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
375                 } else if (ubuf[i] <= 0x7fffffff) {
376                         if (end - buf < 6)
377                                 return NULL;
378                         *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
379                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
380                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
381                         *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
382                         *buf++ = CAST(unsigned char, ((ubuf[i] >>  6) & 0x3f) + 0x80);
383                         *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
384                 } else /* Invalid character */
385                         return NULL;
386         }
387
388         return buf;
389 }