1 /* $NetBSD: ascmagic.c,v 1.1.1.5 2013/12/01 19:28:16 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.87 2013/09/17 15:51:22 christos Exp $") 42 #else 43 __RCSID("$NetBSD: ascmagic.c,v 1.1.1.5 2013/12/01 19:28:16 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 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 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 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), TEXTTEST, text)) == 0) 157 rv = -1; 158 } 159 160 /* Now try to discover other details about the file. */ 161 for (i = 0; i < ulen; i++) { 162 if (ubuf[i] == '\n') { 163 if (seen_cr) 164 n_crlf++; 165 else 166 n_lf++; 167 last_line_end = i; 168 } else if (seen_cr) 169 n_cr++; 170 171 seen_cr = (ubuf[i] == '\r'); 172 if (seen_cr) 173 last_line_end = i; 174 175 if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */ 176 n_nel++; 177 last_line_end = i; 178 } 179 180 /* If this line is _longer_ than MAXLINELEN, remember it. */ 181 if (i > last_line_end + MAXLINELEN) 182 has_long_lines = 1; 183 184 if (ubuf[i] == '\033') 185 has_escapes = 1; 186 if (ubuf[i] == '\b') 187 has_backspace = 1; 188 } 189 190 /* Beware, if the data has been truncated, the final CR could have 191 been followed by a LF. If we have HOWMANY bytes, it indicates 192 that the data might have been truncated, probably even before 193 this function was called. */ 194 if (seen_cr && nbytes < HOWMANY) 195 n_cr++; 196 197 if (strcmp(type, "binary") == 0) { 198 rv = 0; 199 goto done; 200 } 201 if (mime) { 202 if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) { 203 if (subtype_mime) { 204 if (file_printf(ms, "%s", subtype_mime) == -1) 205 goto done; 206 } else { 207 if (file_printf(ms, "text/plain") == -1) 208 goto done; 209 } 210 } 211 } else { 212 if (file_printedlen(ms)) { 213 switch (file_replace(ms, " text$", ", ")) { 214 case 0: 215 switch (file_replace(ms, " text executable$", 216 ", ")) { 217 case 0: 218 if (file_printf(ms, ", ") == -1) 219 goto done; 220 break; 221 case -1: 222 goto done; 223 default: 224 executable = 1; 225 break; 226 } 227 break; 228 case -1: 229 goto done; 230 default: 231 break; 232 } 233 } 234 235 if (file_printf(ms, "%s", code) == -1) 236 goto done; 237 238 if (subtype) { 239 if (file_printf(ms, " %s", subtype) == -1) 240 goto done; 241 } 242 243 if (file_printf(ms, " %s", type) == -1) 244 goto done; 245 246 if (executable) 247 if (file_printf(ms, " executable") == -1) 248 goto done; 249 250 if (has_long_lines) 251 if (file_printf(ms, ", with very long lines") == -1) 252 goto done; 253 254 /* 255 * Only report line terminators if we find one other than LF, 256 * or if we find none at all. 257 */ 258 if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) || 259 (n_crlf != 0 || n_cr != 0 || n_nel != 0)) { 260 if (file_printf(ms, ", with") == -1) 261 goto done; 262 263 if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) { 264 if (file_printf(ms, " no") == -1) 265 goto done; 266 } else { 267 if (n_crlf) { 268 if (file_printf(ms, " CRLF") == -1) 269 goto done; 270 if (n_cr || n_lf || n_nel) 271 if (file_printf(ms, ",") == -1) 272 goto done; 273 } 274 if (n_cr) { 275 if (file_printf(ms, " CR") == -1) 276 goto done; 277 if (n_lf || n_nel) 278 if (file_printf(ms, ",") == -1) 279 goto done; 280 } 281 if (n_lf) { 282 if (file_printf(ms, " LF") == -1) 283 goto done; 284 if (n_nel) 285 if (file_printf(ms, ",") == -1) 286 goto done; 287 } 288 if (n_nel) 289 if (file_printf(ms, " NEL") == -1) 290 goto done; 291 } 292 293 if (file_printf(ms, " line terminators") == -1) 294 goto done; 295 } 296 297 if (has_escapes) 298 if (file_printf(ms, ", with escape sequences") == -1) 299 goto done; 300 if (has_backspace) 301 if (file_printf(ms, ", with overstriking") == -1) 302 goto done; 303 } 304 rv = 1; 305 done: 306 free(utf8_buf); 307 308 return rv; 309 } 310 311 /* 312 * Encode Unicode string as UTF-8, returning pointer to character 313 * after end of string, or NULL if an invalid character is found. 314 */ 315 private unsigned char * 316 encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen) 317 { 318 size_t i; 319 unsigned char *end = buf + len; 320 321 for (i = 0; i < ulen; i++) { 322 if (ubuf[i] <= 0x7f) { 323 if (end - buf < 1) 324 return NULL; 325 *buf++ = (unsigned char)ubuf[i]; 326 } else if (ubuf[i] <= 0x7ff) { 327 if (end - buf < 2) 328 return NULL; 329 *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0); 330 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 331 } else if (ubuf[i] <= 0xffff) { 332 if (end - buf < 3) 333 return NULL; 334 *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0); 335 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 336 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 337 } else if (ubuf[i] <= 0x1fffff) { 338 if (end - buf < 4) 339 return NULL; 340 *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0); 341 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 342 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 343 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 344 } else if (ubuf[i] <= 0x3ffffff) { 345 if (end - buf < 5) 346 return NULL; 347 *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8); 348 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); 349 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 350 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 351 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 352 } else if (ubuf[i] <= 0x7fffffff) { 353 if (end - buf < 6) 354 return NULL; 355 *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc); 356 *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80); 357 *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); 358 *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 359 *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 360 *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 361 } else /* Invalid character */ 362 return NULL; 363 } 364 365 return buf; 366 } 367