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