Import file-5.06.
[dragonfly.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.81 2011/03/15 22:16:29 christos Exp $")
39 #endif  /* lint */
40
41 #include "magic.h"
42 #include <string.h>
43 #include <memory.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include "names.h"
50
51 #define MAXLINELEN 300  /* longest sane line length */
52 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
53                   || (x) == 0x85 || (x) == '\f')
54
55 private int ascmatch(const unsigned char *, const unichar *, size_t);
56 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
57 private size_t trim_nuls(const unsigned char *, size_t);
58
59 /*
60  * Undo the NUL-termination kindly provided by process()
61  * but leave at least one byte to look at
62  */
63 private size_t
64 trim_nuls(const unsigned char *buf, size_t nbytes)
65 {
66         while (nbytes > 1 && buf[nbytes - 1] == '\0')
67                 nbytes--;
68
69         return nbytes;
70 }
71
72 protected int
73 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
74 {
75         unichar *ubuf = NULL;
76         size_t ulen;
77         int rv = 1;
78
79         const char *code = NULL;
80         const char *code_mime = NULL;
81         const char *type = NULL;
82
83         if (ms->flags & MAGIC_APPLE)
84                 return 0;
85
86         nbytes = trim_nuls(buf, nbytes);
87
88         /* If file doesn't look like any sort of text, give up. */
89         if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
90             &type) == 0) {
91                 rv = 0;
92                 goto done;
93         }
94
95         rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
96             type);
97
98  done:
99         if (ubuf)
100                 free(ubuf);
101
102         return rv;
103 }
104
105 protected int
106 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
107     size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
108     const char *type)
109 {
110         unsigned char *utf8_buf = NULL, *utf8_end;
111         size_t mlen, i;
112         const struct names *p;
113         int rv = -1;
114         int mime = ms->flags & MAGIC_MIME;
115
116         const char *subtype = NULL;
117         const char *subtype_mime = NULL;
118
119         int has_escapes = 0;
120         int has_backspace = 0;
121         int seen_cr = 0;
122
123         int n_crlf = 0;
124         int n_lf = 0;
125         int n_cr = 0;
126         int n_nel = 0;
127         int score, curtype, executable = 0;
128
129         size_t last_line_end = (size_t)-1;
130         int has_long_lines = 0;
131
132         if (ms->flags & MAGIC_APPLE)
133                 return 0;
134
135         nbytes = trim_nuls(buf, nbytes);
136
137         /* If we have fewer than 2 bytes, give up. */
138         if (nbytes <= 1) {
139                 rv = 0;
140                 goto done;
141         }
142
143         if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
144                 /* Convert ubuf to UTF-8 and try text soft magic */
145                 /* malloc size is a conservative overestimate; could be
146                    improved, or at least realloced after conversion. */
147                 mlen = ulen * 6;
148                 if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
149                         file_oomem(ms, mlen);
150                         goto done;
151                 }
152                 if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
153                     == NULL)
154                         goto done;
155                 if ((rv = file_softmagic(ms, utf8_buf,
156                     (size_t)(utf8_end - utf8_buf), TEXTTEST)) != 0)
157                         goto subtype_identified;
158                 else
159                         rv = -1;
160         }
161
162         /* look for tokens from names.h - this is expensive! */
163         if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
164                 goto subtype_identified;
165
166         i = 0;
167         score = 0;
168         curtype = -1;
169         while (i < ulen) {
170                 size_t end;
171
172                 /* skip past any leading space */
173                 while (i < ulen && ISSPC(ubuf[i]))
174                         i++;
175                 if (i >= ulen)
176                         break;
177
178                 /* find the next whitespace */
179                 for (end = i + 1; end < nbytes; end++)
180                         if (ISSPC(ubuf[end]))
181                                 break;
182
183                 /* compare the word thus isolated against the token list */
184                 for (p = names; p < names + NNAMES; p++) {
185                         if (ascmatch((const unsigned char *)p->name, ubuf + i,
186                             end - i)) {
187                                 if (curtype == -1)
188                                         curtype = p->type;
189                                 else if (curtype != p->type) {
190                                         score = p->score;
191                                         curtype = p->type;
192                                 } else
193                                         score += p->score;
194                                 if (score > 1) {
195                                         subtype = types[p->type].human;
196                                         subtype_mime = types[p->type].mime;
197                                         goto subtype_identified;
198                                 }
199                         }
200                 }
201
202                 i = end;
203         }
204
205 subtype_identified:
206
207         /* Now try to discover other details about the file. */
208         for (i = 0; i < ulen; i++) {
209                 if (ubuf[i] == '\n') {
210                         if (seen_cr)
211                                 n_crlf++;
212                         else
213                                 n_lf++;
214                         last_line_end = i;
215                 } else if (seen_cr)
216                         n_cr++;
217
218                 seen_cr = (ubuf[i] == '\r');
219                 if (seen_cr)
220                         last_line_end = i;
221
222                 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
223                         n_nel++;
224                         last_line_end = i;
225                 }
226
227                 /* If this line is _longer_ than MAXLINELEN, remember it. */
228                 if (i > last_line_end + MAXLINELEN)
229                         has_long_lines = 1;
230
231                 if (ubuf[i] == '\033')
232                         has_escapes = 1;
233                 if (ubuf[i] == '\b')
234                         has_backspace = 1;
235         }
236
237         /* Beware, if the data has been truncated, the final CR could have
238            been followed by a LF.  If we have HOWMANY bytes, it indicates
239            that the data might have been truncated, probably even before
240            this function was called. */
241         if (seen_cr && nbytes < HOWMANY)
242                 n_cr++;
243
244         if (strcmp(type, "binary") == 0) {
245                 rv = 0;
246                 goto done;
247         }
248         if (mime) {
249                 if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
250                         if (subtype_mime) {
251                                 if (file_printf(ms, "%s", subtype_mime) == -1)
252                                         goto done;
253                         } else {
254                                 if (file_printf(ms, "text/plain") == -1)
255                                         goto done;
256                         }
257                 }
258         } else {
259                 if (file_printedlen(ms)) {
260                         switch (file_replace(ms, " text$", ", ")) {
261                         case 0:
262                                 switch (file_replace(ms, " text executable$",
263                                     ", ")) {
264                                 case 0:
265                                         if (file_printf(ms, ", ") == -1)
266                                                 goto done;
267                                 case -1:
268                                         goto done;
269                                 default:
270                                         executable = 1;
271                                         break;
272                                 }
273                                 break;
274                         case -1:
275                                 goto done;
276                         default:
277                                 break;
278                         }
279                 }
280
281                 if (file_printf(ms, "%s", code) == -1)
282                         goto done;
283
284                 if (subtype) {
285                         if (file_printf(ms, " %s", subtype) == -1)
286                                 goto done;
287                 }
288
289                 if (file_printf(ms, " %s", type) == -1)
290                         goto done;
291
292                 if (executable)
293                         if (file_printf(ms, " executable") == -1)
294                                 goto done;
295
296                 if (has_long_lines)
297                         if (file_printf(ms, ", with very long lines") == -1)
298                                 goto done;
299
300                 /*
301                  * Only report line terminators if we find one other than LF,
302                  * or if we find none at all.
303                  */
304                 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
305                     (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
306                         if (file_printf(ms, ", with") == -1)
307                                 goto done;
308
309                         if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
310                                 if (file_printf(ms, " no") == -1)
311                                         goto done;
312                         } else {
313                                 if (n_crlf) {
314                                         if (file_printf(ms, " CRLF") == -1)
315                                                 goto done;
316                                         if (n_cr || n_lf || n_nel)
317                                                 if (file_printf(ms, ",") == -1)
318                                                         goto done;
319                                 }
320                                 if (n_cr) {
321                                         if (file_printf(ms, " CR") == -1)
322                                                 goto done;
323                                         if (n_lf || n_nel)
324                                                 if (file_printf(ms, ",") == -1)
325                                                         goto done;
326                                 }
327                                 if (n_lf) {
328                                         if (file_printf(ms, " LF") == -1)
329                                                 goto done;
330                                         if (n_nel)
331                                                 if (file_printf(ms, ",") == -1)
332                                                         goto done;
333                                 }
334                                 if (n_nel)
335                                         if (file_printf(ms, " NEL") == -1)
336                                                 goto done;
337                         }
338
339                         if (file_printf(ms, " line terminators") == -1)
340                                 goto done;
341                 }
342
343                 if (has_escapes)
344                         if (file_printf(ms, ", with escape sequences") == -1)
345                                 goto done;
346                 if (has_backspace)
347                         if (file_printf(ms, ", with overstriking") == -1)
348                                 goto done;
349         }
350         rv = 1;
351 done:
352         if (utf8_buf)
353                 free(utf8_buf);
354
355         return rv;
356 }
357
358 private int
359 ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
360 {
361         size_t i;
362
363         for (i = 0; i < ulen; i++) {
364                 if (s[i] != us[i])
365                         return 0;
366         }
367
368         if (s[i])
369                 return 0;
370         else
371                 return 1;
372 }
373
374 /*
375  * Encode Unicode string as UTF-8, returning pointer to character
376  * after end of string, or NULL if an invalid character is found.
377  */
378 private unsigned char *
379 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
380 {
381         size_t i;
382         unsigned char *end = buf + len;
383
384         for (i = 0; i < ulen; i++) {
385                 if (ubuf[i] <= 0x7f) {
386                         if (end - buf < 1)
387                                 return NULL;
388                         *buf++ = (unsigned char)ubuf[i];
389                 } else if (ubuf[i] <= 0x7ff) {
390                         if (end - buf < 2)
391                                 return NULL;
392                         *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
393                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
394                 } else if (ubuf[i] <= 0xffff) {
395                         if (end - buf < 3)
396                                 return NULL;
397                         *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
398                         *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
399                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
400                 } else if (ubuf[i] <= 0x1fffff) {
401                         if (end - buf < 4)
402                                 return NULL;
403                         *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
404                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
405                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
406                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
407                 } else if (ubuf[i] <= 0x3ffffff) {
408                         if (end - buf < 5)
409                                 return NULL;
410                         *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
411                         *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
412                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
413                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
414                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
415                 } else if (ubuf[i] <= 0x7fffffff) {
416                         if (end - buf < 6)
417                                 return NULL;
418                         *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
419                         *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
420                         *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
421                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
422                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
423                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
424                 } else /* Invalid character */
425                         return NULL;
426         }
427
428         return buf;
429 }