]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/file/ascmagic.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / file / 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 -- file types that we know based on keywords
30  * that can appear anywhere in the file.
31  *
32  * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
33  * to handle character codes other than ASCII on a unified basis.
34  */
35
36 #include "file.h"
37
38 #ifndef lint
39 FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
40 #endif  /* lint */
41
42 #include "magic.h"
43 #include <string.h>
44 #include <memory.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #include "names.h"
51
52 #define MAXLINELEN 300  /* longest sane line length */
53 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
54                   || (x) == 0x85 || (x) == '\f')
55
56 private int ascmatch(const unsigned char *, const unichar *, size_t);
57 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
58 private size_t trim_nuls(const unsigned char *, size_t);
59
60 /*
61  * Undo the NUL-termination kindly provided by process()
62  * but leave at least one byte to look at
63  */
64 private size_t
65 trim_nuls(const unsigned char *buf, size_t nbytes)
66 {
67         while (nbytes > 1 && buf[nbytes - 1] == '\0')
68                 nbytes--;
69
70         return nbytes;
71 }
72
73 protected int
74 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
75 {
76         unichar *ubuf = NULL;
77         size_t ulen;
78         int rv = 1;
79
80         const char *code = NULL;
81         const char *code_mime = NULL;
82         const char *type = NULL;
83
84         if (ms->flags & MAGIC_APPLE)
85                 return 0;
86
87         nbytes = trim_nuls(buf, nbytes);
88
89         /* If file doesn't look like any sort of text, give up. */
90         if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
91             &type) == 0) {
92                 rv = 0;
93                 goto done;
94         }
95
96         rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, 
97             type);
98
99  done:
100         if (ubuf)
101                 free(ubuf);
102
103         return rv;
104 }
105
106 protected int
107 file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
108     size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
109     const char *type)
110 {
111         unsigned char *utf8_buf = NULL, *utf8_end;
112         size_t mlen, i;
113         const struct names *p;
114         int rv = -1;
115         int mime = ms->flags & MAGIC_MIME;
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
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         /* Convert ubuf to UTF-8 and try text soft magic */
144         /* malloc size is a conservative overestimate; could be
145            improved, or at least realloced after conversion. */
146         mlen = ulen * 6;
147         if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
148                 file_oomem(ms, mlen);
149                 goto done;
150         }
151         if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL)
152                 goto done;
153         if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf),
154             TEXTTEST)) != 0)
155                 goto done;
156         else
157                 rv = -1;
158
159         /* look for tokens from names.h - this is expensive! */
160         if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
161                 goto subtype_identified;
162
163         i = 0;
164         while (i < ulen) {
165                 size_t end;
166
167                 /* skip past any leading space */
168                 while (i < ulen && ISSPC(ubuf[i]))
169                         i++;
170                 if (i >= ulen)
171                         break;
172
173                 /* find the next whitespace */
174                 for (end = i + 1; end < nbytes; end++)
175                         if (ISSPC(ubuf[end]))
176                                 break;
177
178                 /* compare the word thus isolated against the token list */
179                 for (p = names; p < names + NNAMES; p++) {
180                         if (ascmatch((const unsigned char *)p->name, ubuf + i,
181                             end - i)) {
182                                 subtype = types[p->type].human;
183                                 subtype_mime = types[p->type].mime;
184                                 goto subtype_identified;
185                         }
186                 }
187
188                 i = end;
189         }
190
191 subtype_identified:
192
193         /* Now try to discover other details about the file. */
194         for (i = 0; i < ulen; i++) {
195                 if (ubuf[i] == '\n') {
196                         if (seen_cr)
197                                 n_crlf++;
198                         else
199                                 n_lf++;
200                         last_line_end = i;
201                 } else if (seen_cr)
202                         n_cr++;
203
204                 seen_cr = (ubuf[i] == '\r');
205                 if (seen_cr)
206                         last_line_end = i;
207
208                 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
209                         n_nel++;
210                         last_line_end = i;
211                 }
212
213                 /* If this line is _longer_ than MAXLINELEN, remember it. */
214                 if (i > last_line_end + MAXLINELEN)
215                         has_long_lines = 1;
216
217                 if (ubuf[i] == '\033')
218                         has_escapes = 1;
219                 if (ubuf[i] == '\b')
220                         has_backspace = 1;
221         }
222
223         /* Beware, if the data has been truncated, the final CR could have
224            been followed by a LF.  If we have HOWMANY bytes, it indicates
225            that the data might have been truncated, probably even before
226            this function was called. */
227         if (seen_cr && nbytes < HOWMANY)
228                 n_cr++;
229
230         if (strcmp(type, "binary") == 0) {
231                 rv = 0;
232                 goto done;
233         }
234         if (mime) {
235                 if ((mime & MAGIC_MIME_TYPE) != 0) {
236                         if (subtype_mime) {
237                                 if (file_printf(ms, "%s", subtype_mime) == -1)
238                                         goto done;
239                         } else {
240                                 if (file_printf(ms, "text/plain") == -1)
241                                         goto done;
242                         }
243                 }
244         } else {
245                 if (file_printf(ms, "%s", code) == -1)
246                         goto done;
247
248                 if (subtype) {
249                         if (file_printf(ms, " %s", subtype) == -1)
250                                 goto done;
251                 }
252
253                 if (file_printf(ms, " %s", type) == -1)
254                         goto done;
255
256                 if (has_long_lines)
257                         if (file_printf(ms, ", with very long lines") == -1)
258                                 goto done;
259
260                 /*
261                  * Only report line terminators if we find one other than LF,
262                  * or if we find none at all.
263                  */
264                 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
265                     (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
266                         if (file_printf(ms, ", with") == -1)
267                                 goto done;
268
269                         if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
270                                 if (file_printf(ms, " no") == -1)
271                                         goto done;
272                         } else {
273                                 if (n_crlf) {
274                                         if (file_printf(ms, " CRLF") == -1)
275                                                 goto done;
276                                         if (n_cr || n_lf || n_nel)
277                                                 if (file_printf(ms, ",") == -1)
278                                                         goto done;
279                                 }
280                                 if (n_cr) {
281                                         if (file_printf(ms, " CR") == -1)
282                                                 goto done;
283                                         if (n_lf || n_nel)
284                                                 if (file_printf(ms, ",") == -1)
285                                                         goto done;
286                                 }
287                                 if (n_lf) {
288                                         if (file_printf(ms, " LF") == -1)
289                                                 goto done;
290                                         if (n_nel)
291                                                 if (file_printf(ms, ",") == -1)
292                                                         goto done;
293                                 }
294                                 if (n_nel)
295                                         if (file_printf(ms, " NEL") == -1)
296                                                 goto done;
297                         }
298
299                         if (file_printf(ms, " line terminators") == -1)
300                                 goto done;
301                 }
302
303                 if (has_escapes)
304                         if (file_printf(ms, ", with escape sequences") == -1)
305                                 goto done;
306                 if (has_backspace)
307                         if (file_printf(ms, ", with overstriking") == -1)
308                                 goto done;
309         }
310         rv = 1;
311 done:
312         if (utf8_buf)
313                 free(utf8_buf);
314
315         return rv;
316 }
317
318 private int
319 ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
320 {
321         size_t i;
322
323         for (i = 0; i < ulen; i++) {
324                 if (s[i] != us[i])
325                         return 0;
326         }
327
328         if (s[i])
329                 return 0;
330         else
331                 return 1;
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++ = (unsigned char)ubuf[i];
349                 } else if (ubuf[i] <= 0x7ff) {
350                         if (end - buf < 2)
351                                 return NULL;
352                         *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
353                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
354                 } else if (ubuf[i] <= 0xffff) {
355                         if (end - buf < 3)
356                                 return NULL;
357                         *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
358                         *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
359                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
360                 } else if (ubuf[i] <= 0x1fffff) {
361                         if (end - buf < 4)
362                                 return NULL;
363                         *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
364                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
365                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
366                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
367                 } else if (ubuf[i] <= 0x3ffffff) {
368                         if (end - buf < 5)
369                                 return NULL;
370                         *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
371                         *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
372                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
373                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
374                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
375                 } else if (ubuf[i] <= 0x7fffffff) {
376                         if (end - buf < 6)
377                                 return NULL;
378                         *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
379                         *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
380                         *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
381                         *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
382                         *buf++ = (unsigned char)(((ubuf[i] >>  6) & 0x3f) + 0x80);
383                         *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
384                 } else /* Invalid character */
385                         return NULL;
386         }
387
388         return buf;
389 }