ascmagic.c revision 234250
168349Sobrien/* 2133359Sobrien * Copyright (c) Ian F. Darwin 1986-1995. 3133359Sobrien * Software written by Ian F. Darwin and others; 4133359Sobrien * maintained 1995-present by Christos Zoulas and others. 5191736Sobrien * 6133359Sobrien * Redistribution and use in source and binary forms, with or without 7133359Sobrien * modification, are permitted provided that the following conditions 8133359Sobrien * are met: 9133359Sobrien * 1. Redistributions of source code must retain the above copyright 10133359Sobrien * notice immediately at the beginning of the file, without modification, 11133359Sobrien * this list of conditions, and the following disclaimer. 12133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright 13133359Sobrien * notice, this list of conditions and the following disclaimer in the 14133359Sobrien * documentation and/or other materials provided with the distribution. 15191736Sobrien * 16133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26133359Sobrien * SUCH DAMAGE. 27133359Sobrien */ 28133359Sobrien/* 29226048Sobrien * ASCII magic -- try to detect text encoding. 3068349Sobrien * 3168349Sobrien * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000, 3268349Sobrien * to handle character codes other than ASCII on a unified basis. 3368349Sobrien */ 3468349Sobrien 3568349Sobrien#include "file.h" 36191736Sobrien 37191736Sobrien#ifndef lint 38234250SobrienFILE_RCSID("@(#)$File: ascmagic.c,v 1.84 2011/12/08 12:38:24 rrt Exp $") 39191736Sobrien#endif /* lint */ 40191736Sobrien 41133359Sobrien#include "magic.h" 4268349Sobrien#include <string.h> 4368349Sobrien#include <memory.h> 4468349Sobrien#include <ctype.h> 4568349Sobrien#include <stdlib.h> 4668349Sobrien#ifdef HAVE_UNISTD_H 4768349Sobrien#include <unistd.h> 4868349Sobrien#endif 4968349Sobrien 5068349Sobrien#define MAXLINELEN 300 /* longest sane line length */ 5168349Sobrien#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \ 5268349Sobrien || (x) == 0x85 || (x) == '\f') 5368349Sobrien 54186690Sobrienprivate unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t); 55191736Sobrienprivate size_t trim_nuls(const unsigned char *, size_t); 5668349Sobrien 57191736Sobrien/* 58191736Sobrien * Undo the NUL-termination kindly provided by process() 59191736Sobrien * but leave at least one byte to look at 60191736Sobrien */ 61191736Sobrienprivate size_t 62191736Sobrientrim_nuls(const unsigned char *buf, size_t nbytes) 63191736Sobrien{ 64191736Sobrien while (nbytes > 1 && buf[nbytes - 1] == '\0') 65191736Sobrien nbytes--; 66133359Sobrien 67191736Sobrien return nbytes; 68191736Sobrien} 69191736Sobrien 70133359Sobrienprotected int 71234250Sobrienfile_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, 72234250Sobrien int text) 7368349Sobrien{ 74191736Sobrien unichar *ubuf = NULL; 75191736Sobrien size_t ulen; 76191736Sobrien int rv = 1; 77191736Sobrien 78191736Sobrien const char *code = NULL; 79191736Sobrien const char *code_mime = NULL; 80191736Sobrien const char *type = NULL; 81191736Sobrien 82191736Sobrien if (ms->flags & MAGIC_APPLE) 83191736Sobrien return 0; 84191736Sobrien 85191736Sobrien nbytes = trim_nuls(buf, nbytes); 86191736Sobrien 87191736Sobrien /* If file doesn't look like any sort of text, give up. */ 88191736Sobrien if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime, 89234250Sobrien &type) == 0) 90191736Sobrien rv = 0; 91234250Sobrien else 92234250Sobrien rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, 93234250Sobrien type, text); 94191736Sobrien 95234250Sobrien free(ubuf); 96191736Sobrien 97191736Sobrien return rv; 98191736Sobrien} 99191736Sobrien 100191736Sobrienprotected int 101191736Sobrienfile_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, 102191736Sobrien size_t nbytes, unichar *ubuf, size_t ulen, const char *code, 103234250Sobrien const char *type, int text) 104191736Sobrien{ 105191736Sobrien unsigned char *utf8_buf = NULL, *utf8_end; 106191736Sobrien size_t mlen, i; 107159764Sobrien int rv = -1; 108175296Sobrien int mime = ms->flags & MAGIC_MIME; 10968349Sobrien 110133359Sobrien const char *subtype = NULL; 111133359Sobrien const char *subtype_mime = NULL; 11268349Sobrien 11368349Sobrien int has_escapes = 0; 11468349Sobrien int has_backspace = 0; 115159764Sobrien int seen_cr = 0; 11668349Sobrien 11768349Sobrien int n_crlf = 0; 11868349Sobrien int n_lf = 0; 11968349Sobrien int n_cr = 0; 12068349Sobrien int n_nel = 0; 121234250Sobrien int executable = 0; 12268349Sobrien 123169962Sobrien size_t last_line_end = (size_t)-1; 12468349Sobrien int has_long_lines = 0; 12568349Sobrien 126191736Sobrien if (ms->flags & MAGIC_APPLE) 127191736Sobrien return 0; 12868349Sobrien 129191736Sobrien nbytes = trim_nuls(buf, nbytes); 130133359Sobrien 131191736Sobrien /* If we have fewer than 2 bytes, give up. */ 132159764Sobrien if (nbytes <= 1) { 133159764Sobrien rv = 0; 134159764Sobrien goto done; 135159764Sobrien } 136159764Sobrien 137226048Sobrien if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { 138226048Sobrien /* Convert ubuf to UTF-8 and try text soft magic */ 139226048Sobrien /* malloc size is a conservative overestimate; could be 140226048Sobrien improved, or at least realloced after conversion. */ 141226048Sobrien mlen = ulen * 6; 142226048Sobrien if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) { 143226048Sobrien file_oomem(ms, mlen); 144226048Sobrien goto done; 145226048Sobrien } 146226048Sobrien if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) 147226048Sobrien == NULL) 148226048Sobrien goto done; 149226048Sobrien if ((rv = file_softmagic(ms, utf8_buf, 150234250Sobrien (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0) 151226048Sobrien rv = -1; 15268349Sobrien } 15368349Sobrien 154186690Sobrien /* Now try to discover other details about the file. */ 15568349Sobrien for (i = 0; i < ulen; i++) { 156159764Sobrien if (ubuf[i] == '\n') { 157159764Sobrien if (seen_cr) 158159764Sobrien n_crlf++; 159159764Sobrien else 160159764Sobrien n_lf++; 161159764Sobrien last_line_end = i; 162159764Sobrien } else if (seen_cr) 163159764Sobrien n_cr++; 164159764Sobrien 165159764Sobrien seen_cr = (ubuf[i] == '\r'); 166159764Sobrien if (seen_cr) 167159764Sobrien last_line_end = i; 168159764Sobrien 169159764Sobrien if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */ 170159764Sobrien n_nel++; 171159764Sobrien last_line_end = i; 172159764Sobrien } 173159764Sobrien 174159764Sobrien /* If this line is _longer_ than MAXLINELEN, remember it. */ 17568349Sobrien if (i > last_line_end + MAXLINELEN) 17668349Sobrien has_long_lines = 1; 17768349Sobrien 17868349Sobrien if (ubuf[i] == '\033') 17968349Sobrien has_escapes = 1; 18068349Sobrien if (ubuf[i] == '\b') 18168349Sobrien has_backspace = 1; 18268349Sobrien } 18368349Sobrien 184159764Sobrien /* Beware, if the data has been truncated, the final CR could have 185159764Sobrien been followed by a LF. If we have HOWMANY bytes, it indicates 186159764Sobrien that the data might have been truncated, probably even before 187159764Sobrien this function was called. */ 188159764Sobrien if (seen_cr && nbytes < HOWMANY) 189159764Sobrien n_cr++; 190159764Sobrien 191191736Sobrien if (strcmp(type, "binary") == 0) { 192191736Sobrien rv = 0; 193191736Sobrien goto done; 194191736Sobrien } 195175296Sobrien if (mime) { 196226048Sobrien if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) { 197175296Sobrien if (subtype_mime) { 198191736Sobrien if (file_printf(ms, "%s", subtype_mime) == -1) 199175296Sobrien goto done; 200175296Sobrien } else { 201175296Sobrien if (file_printf(ms, "text/plain") == -1) 202175296Sobrien goto done; 203175296Sobrien } 204133359Sobrien } 20568349Sobrien } else { 206226048Sobrien if (file_printedlen(ms)) { 207226048Sobrien switch (file_replace(ms, " text$", ", ")) { 208226048Sobrien case 0: 209226048Sobrien switch (file_replace(ms, " text executable$", 210226048Sobrien ", ")) { 211226048Sobrien case 0: 212226048Sobrien if (file_printf(ms, ", ") == -1) 213226048Sobrien goto done; 214226048Sobrien case -1: 215226048Sobrien goto done; 216226048Sobrien default: 217226048Sobrien executable = 1; 218226048Sobrien break; 219226048Sobrien } 220226048Sobrien break; 221226048Sobrien case -1: 222226048Sobrien goto done; 223226048Sobrien default: 224226048Sobrien break; 225226048Sobrien } 226226048Sobrien } 227226048Sobrien 228191736Sobrien if (file_printf(ms, "%s", code) == -1) 229159764Sobrien goto done; 23068349Sobrien 23168349Sobrien if (subtype) { 232191736Sobrien if (file_printf(ms, " %s", subtype) == -1) 233159764Sobrien goto done; 23468349Sobrien } 23568349Sobrien 236191736Sobrien if (file_printf(ms, " %s", type) == -1) 237159764Sobrien goto done; 23868349Sobrien 239226048Sobrien if (executable) 240226048Sobrien if (file_printf(ms, " executable") == -1) 241226048Sobrien goto done; 242226048Sobrien 24368349Sobrien if (has_long_lines) 244133359Sobrien if (file_printf(ms, ", with very long lines") == -1) 245159764Sobrien goto done; 24668349Sobrien 24768349Sobrien /* 24868349Sobrien * Only report line terminators if we find one other than LF, 24968349Sobrien * or if we find none at all. 25068349Sobrien */ 25168349Sobrien if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) || 25268349Sobrien (n_crlf != 0 || n_cr != 0 || n_nel != 0)) { 253133359Sobrien if (file_printf(ms, ", with") == -1) 254159764Sobrien goto done; 25568349Sobrien 256191736Sobrien if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) { 257133359Sobrien if (file_printf(ms, " no") == -1) 258159764Sobrien goto done; 259133359Sobrien } else { 26068349Sobrien if (n_crlf) { 261133359Sobrien if (file_printf(ms, " CRLF") == -1) 262159764Sobrien goto done; 26368349Sobrien if (n_cr || n_lf || n_nel) 264133359Sobrien if (file_printf(ms, ",") == -1) 265159764Sobrien goto done; 26668349Sobrien } 26768349Sobrien if (n_cr) { 268133359Sobrien if (file_printf(ms, " CR") == -1) 269159764Sobrien goto done; 27068349Sobrien if (n_lf || n_nel) 271133359Sobrien if (file_printf(ms, ",") == -1) 272159764Sobrien goto done; 27368349Sobrien } 27468349Sobrien if (n_lf) { 275133359Sobrien if (file_printf(ms, " LF") == -1) 276159764Sobrien goto done; 27768349Sobrien if (n_nel) 278133359Sobrien if (file_printf(ms, ",") == -1) 279159764Sobrien goto done; 28068349Sobrien } 28168349Sobrien if (n_nel) 282133359Sobrien if (file_printf(ms, " NEL") == -1) 283159764Sobrien goto done; 28468349Sobrien } 28568349Sobrien 286133359Sobrien if (file_printf(ms, " line terminators") == -1) 287159764Sobrien goto done; 28868349Sobrien } 28968349Sobrien 29068349Sobrien if (has_escapes) 291133359Sobrien if (file_printf(ms, ", with escape sequences") == -1) 292159764Sobrien goto done; 29368349Sobrien if (has_backspace) 294133359Sobrien if (file_printf(ms, ", with overstriking") == -1) 295159764Sobrien goto done; 29668349Sobrien } 297159764Sobrien rv = 1; 298159764Sobriendone: 299234250Sobrien free(utf8_buf); 30068349Sobrien 301159764Sobrien return rv; 30268349Sobrien} 30368349Sobrien 30468349Sobrien/* 305186690Sobrien * Encode Unicode string as UTF-8, returning pointer to character 306186690Sobrien * after end of string, or NULL if an invalid character is found. 307186690Sobrien */ 308186690Sobrienprivate unsigned char * 309186690Sobrienencode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen) 31068349Sobrien{ 311169962Sobrien size_t i; 312186690Sobrien unsigned char *end = buf + len; 313186690Sobrien 314186690Sobrien for (i = 0; i < ulen; i++) { 315186690Sobrien if (ubuf[i] <= 0x7f) { 316186690Sobrien if (end - buf < 1) 317186690Sobrien return NULL; 318186690Sobrien *buf++ = (unsigned char)ubuf[i]; 319186690Sobrien } else if (ubuf[i] <= 0x7ff) { 320186690Sobrien if (end - buf < 2) 321186690Sobrien return NULL; 322186690Sobrien *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0); 323186690Sobrien *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 324186690Sobrien } else if (ubuf[i] <= 0xffff) { 325186690Sobrien if (end - buf < 3) 326186690Sobrien return NULL; 327186690Sobrien *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0); 328186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 329186690Sobrien *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 330186690Sobrien } else if (ubuf[i] <= 0x1fffff) { 331186690Sobrien if (end - buf < 4) 332186690Sobrien return NULL; 333186690Sobrien *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0); 334186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 335186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 336186690Sobrien *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 337186690Sobrien } else if (ubuf[i] <= 0x3ffffff) { 338186690Sobrien if (end - buf < 5) 339186690Sobrien return NULL; 340186690Sobrien *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8); 341186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); 342186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 343186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 344186690Sobrien *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 345186690Sobrien } else if (ubuf[i] <= 0x7fffffff) { 346186690Sobrien if (end - buf < 6) 347186690Sobrien return NULL; 348186690Sobrien *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc); 349186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80); 350186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); 351186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); 352186690Sobrien *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); 353186690Sobrien *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); 354186690Sobrien } else /* Invalid character */ 355186690Sobrien return NULL; 356186690Sobrien } 357186690Sobrien 358186690Sobrien return buf; 359186690Sobrien} 360