1 /* $NetBSD: ascmagic.c,v 1.1.1.7 2015/01/02 20:34:27 christos Exp $ */
2
3 /*
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30 /*
31 * ASCII magic -- try to detect text encoding.
32 *
33 * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
34 * to handle character codes other than ASCII on a unified basis.
35 */
36
37 #include "file.h"
38
39 #ifndef lint
40 #if 0
41 FILE_RCSID("@(#)$File: ascmagic.c,v 1.91 2014/11/28 02:46:39 christos Exp $")
42 #else
43 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.7 2015/01/02 20:34:27 christos Exp $");
44 #endif
45 #endif /* lint */
46
47 #include "magic.h"
48 #include <string.h>
49 #include <memory.h>
50 #include <ctype.h>
51 #include <stdlib.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55
56 #define MAXLINELEN 300 /* longest sane line length */
57 #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
58 || (x) == 0x85 || (x) == '\f')
59
60 private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
61 private size_t trim_nuls(const unsigned char *, size_t);
62
63 /*
64 * Undo the NUL-termination kindly provided by process()
65 * but leave at least one byte to look at
66 */
67 private size_t
trim_nuls(const unsigned char * buf,size_t nbytes)68 trim_nuls(const unsigned char *buf, size_t nbytes)
69 {
70 while (nbytes > 1 && buf[nbytes - 1] == '\0')
71 nbytes--;
72
73 return nbytes;
74 }
75
76 protected int
file_ascmagic(struct magic_set * ms,const unsigned char * buf,size_t nbytes,int text)77 file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
78 int text)
79 {
80 unichar *ubuf = NULL;
81 size_t ulen = 0;
82 int rv = 1;
83
84 const char *code = NULL;
85 const char *code_mime = NULL;
86 const char *type = NULL;
87
88 if (ms->flags & MAGIC_APPLE)
89 return 0;
90
91 nbytes = trim_nuls(buf, nbytes);
92
93 /* If file doesn't look like any sort of text, give up. */
94 if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
95 &type) == 0)
96 rv = 0;
97 else
98 rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
99 type, text);
100
101 free(ubuf);
102
103 return rv;
104 }
105
106 protected int
file_ascmagic_with_encoding(struct magic_set * ms,const unsigned char * buf,size_t nbytes,unichar * ubuf,size_t ulen,const char * code,const char * type,int text)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, int text)
110 {
111 unsigned char *utf8_buf = NULL, *utf8_end;
112 size_t mlen, i;
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 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 (ulen > 0 && (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), 0, NULL,
157 TEXTTEST, text)) == 0)
158 rv = -1;
159 }
160
161 /* Now try to discover other details about the file. */
162 for (i = 0; i < ulen; i++) {
163 if (ubuf[i] == '\n') {
164 if (seen_cr)
165 n_crlf++;
166 else
167 n_lf++;
168 last_line_end = i;
169 } else if (seen_cr)
170 n_cr++;
171
172 seen_cr = (ubuf[i] == '\r');
173 if (seen_cr)
174 last_line_end = i;
175
176 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */
177 n_nel++;
178 last_line_end = i;
179 }
180
181 /* If this line is _longer_ than MAXLINELEN, remember it. */
182 if (i > last_line_end + MAXLINELEN)
183 has_long_lines = 1;
184
185 if (ubuf[i] == '\033')
186 has_escapes = 1;
187 if (ubuf[i] == '\b')
188 has_backspace = 1;
189 }
190
191 /* Beware, if the data has been truncated, the final CR could have
192 been followed by a LF. If we have HOWMANY bytes, it indicates
193 that the data might have been truncated, probably even before
194 this function was called. */
195 if (seen_cr && nbytes < HOWMANY)
196 n_cr++;
197
198 if (strcmp(type, "binary") == 0) {
199 rv = 0;
200 goto done;
201 }
202 if (mime) {
203 if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
204 if (subtype_mime) {
205 if (file_printf(ms, "%s", subtype_mime) == -1)
206 goto done;
207 } else {
208 if (file_printf(ms, "text/plain") == -1)
209 goto done;
210 }
211 }
212 } else {
213 if (file_printedlen(ms)) {
214 switch (file_replace(ms, " text$", ", ")) {
215 case 0:
216 switch (file_replace(ms, " text executable$",
217 ", ")) {
218 case 0:
219 if (file_printf(ms, ", ") == -1)
220 goto done;
221 break;
222 case -1:
223 goto done;
224 default:
225 executable = 1;
226 break;
227 }
228 break;
229 case -1:
230 goto done;
231 default:
232 break;
233 }
234 }
235
236 if (file_printf(ms, "%s", code) == -1)
237 goto done;
238
239 if (subtype) {
240 if (file_printf(ms, " %s", subtype) == -1)
241 goto done;
242 }
243
244 if (file_printf(ms, " %s", type) == -1)
245 goto done;
246
247 if (executable)
248 if (file_printf(ms, " executable") == -1)
249 goto done;
250
251 if (has_long_lines)
252 if (file_printf(ms, ", with very long lines") == -1)
253 goto done;
254
255 /*
256 * Only report line terminators if we find one other than LF,
257 * or if we find none at all.
258 */
259 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) ||
260 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) {
261 if (file_printf(ms, ", with") == -1)
262 goto done;
263
264 if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) {
265 if (file_printf(ms, " no") == -1)
266 goto done;
267 } else {
268 if (n_crlf) {
269 if (file_printf(ms, " CRLF") == -1)
270 goto done;
271 if (n_cr || n_lf || n_nel)
272 if (file_printf(ms, ",") == -1)
273 goto done;
274 }
275 if (n_cr) {
276 if (file_printf(ms, " CR") == -1)
277 goto done;
278 if (n_lf || n_nel)
279 if (file_printf(ms, ",") == -1)
280 goto done;
281 }
282 if (n_lf) {
283 if (file_printf(ms, " LF") == -1)
284 goto done;
285 if (n_nel)
286 if (file_printf(ms, ",") == -1)
287 goto done;
288 }
289 if (n_nel)
290 if (file_printf(ms, " NEL") == -1)
291 goto done;
292 }
293
294 if (file_printf(ms, " line terminators") == -1)
295 goto done;
296 }
297
298 if (has_escapes)
299 if (file_printf(ms, ", with escape sequences") == -1)
300 goto done;
301 if (has_backspace)
302 if (file_printf(ms, ", with overstriking") == -1)
303 goto done;
304 }
305 rv = 1;
306 done:
307 free(utf8_buf);
308
309 return rv;
310 }
311
312 /*
313 * Encode Unicode string as UTF-8, returning pointer to character
314 * after end of string, or NULL if an invalid character is found.
315 */
316 private unsigned char *
encode_utf8(unsigned char * buf,size_t len,unichar * ubuf,size_t ulen)317 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
318 {
319 size_t i;
320 unsigned char *end = buf + len;
321
322 for (i = 0; i < ulen; i++) {
323 if (ubuf[i] <= 0x7f) {
324 if (end - buf < 1)
325 return NULL;
326 *buf++ = (unsigned char)ubuf[i];
327 } else if (ubuf[i] <= 0x7ff) {
328 if (end - buf < 2)
329 return NULL;
330 *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
331 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
332 } else if (ubuf[i] <= 0xffff) {
333 if (end - buf < 3)
334 return NULL;
335 *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
336 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
337 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
338 } else if (ubuf[i] <= 0x1fffff) {
339 if (end - buf < 4)
340 return NULL;
341 *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
342 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
343 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
344 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
345 } else if (ubuf[i] <= 0x3ffffff) {
346 if (end - buf < 5)
347 return NULL;
348 *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
349 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
350 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
351 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
352 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
353 } else if (ubuf[i] <= 0x7fffffff) {
354 if (end - buf < 6)
355 return NULL;
356 *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
357 *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
358 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
359 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
360 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
361 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
362 } else /* Invalid character */
363 return NULL;
364 }
365
366 return buf;
367 }
368