diff options
Diffstat (limited to 'contrib/file/src/ascmagic.c')
-rw-r--r-- | contrib/file/src/ascmagic.c | 110 |
1 files changed, 56 insertions, 54 deletions
diff --git a/contrib/file/src/ascmagic.c b/contrib/file/src/ascmagic.c index 9d383be32163..2d61267923bc 100644 --- a/contrib/file/src/ascmagic.c +++ b/contrib/file/src/ascmagic.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.107 2020/06/08 19:58:36 christos Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.116 2023/05/21 16:08:50 christos Exp $") #endif /* lint */ #include "magic.h" @@ -50,14 +50,15 @@ FILE_RCSID("@(#)$File: ascmagic.c,v 1.107 2020/06/08 19:58:36 christos Exp $") #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \ || (x) == 0x85 || (x) == '\f') -private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t); -private size_t trim_nuls(const unsigned char *, size_t); +file_private unsigned char *encode_utf8(unsigned char *, size_t, file_unichar_t *, + size_t); +file_private size_t trim_nuls(const unsigned char *, size_t); /* * Undo the NUL-termination kindly provided by process() * but leave at least one byte to look at */ -private size_t +file_private size_t trim_nuls(const unsigned char *buf, size_t nbytes) { while (nbytes > 1 && buf[nbytes - 1] == '\0') @@ -66,10 +67,10 @@ trim_nuls(const unsigned char *buf, size_t nbytes) return nbytes; } -protected int +file_protected int file_ascmagic(struct magic_set *ms, const struct buffer *b, int text) { - unichar *ubuf = NULL; + file_unichar_t *ubuf = NULL; size_t ulen = 0; int rv = 1; struct buffer bb; @@ -100,10 +101,10 @@ file_ascmagic(struct magic_set *ms, const struct buffer *b, int text) return rv; } -protected int -file_ascmagic_with_encoding(struct magic_set *ms, - const struct buffer *b, unichar *ubuf, size_t ulen, const char *code, - const char *type, int text) +file_protected int +file_ascmagic_with_encoding(struct magic_set *ms, const struct buffer *b, + file_unichar_t *ubuf, size_t ulen, const char *code, const char *type, + int text) { struct buffer bb; const unsigned char *buf = CAST(const unsigned char *, b->fbuf); @@ -120,14 +121,14 @@ file_ascmagic_with_encoding(struct magic_set *ms, int has_backspace = 0; int seen_cr = 0; - int n_crlf = 0; - int n_lf = 0; - int n_cr = 0; - int n_nel = 0; + size_t n_crlf = 0; + size_t n_lf = 0; + size_t n_cr = 0; + size_t n_nel = 0; int executable = 0; size_t last_line_end = CAST(size_t, -1); - int has_long_lines = 0; + size_t has_long_lines = 0; nbytes = trim_nuls(buf, nbytes); @@ -147,8 +148,10 @@ file_ascmagic_with_encoding(struct magic_set *ms, goto done; } if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) - == NULL) + == NULL) { + rv = 0; goto done; + } buffer_init(&bb, b->fd, &b->st, utf8_buf, CAST(size_t, utf8_end - utf8_buf)); @@ -190,8 +193,11 @@ file_ascmagic_with_encoding(struct magic_set *ms, } /* If this line is _longer_ than MAXLINELEN, remember it. */ - if (i > last_line_end + MAXLINELEN) - has_long_lines = 1; + if (i > last_line_end + MAXLINELEN) { + size_t ll = i - last_line_end; + if (ll > has_long_lines) + has_long_lines = ll; + } if (ubuf[i] == '\033') has_escapes = 1; @@ -199,13 +205,6 @@ file_ascmagic_with_encoding(struct magic_set *ms, has_backspace = 1; } - /* Beware, if the data has been truncated, the final CR could have - been followed by a LF. If we have ms->bytes_max bytes, it indicates - that the data might have been truncated, probably even before - this function was called. */ - if (seen_cr && nbytes < ms->bytes_max) - n_cr++; - if (strcmp(type, "binary") == 0) { rv = 0; goto done; @@ -224,10 +223,9 @@ file_ascmagic_with_encoding(struct magic_set *ms, } if (need_separator && file_separator(ms) == -1) goto done; - } else { - if (file_printf(ms, "text/plain") == -1) - goto done; } + if (file_printf(ms, "text/plain") == -1) + goto done; } } else { if (len) { @@ -269,7 +267,8 @@ file_ascmagic_with_encoding(struct magic_set *ms, goto done; if (has_long_lines) - if (file_printf(ms, ", with very long lines") == -1) + if (file_printf(ms, ", with very long lines (%" + SIZE_T_FORMAT "u)", has_long_lines) == -1) goto done; /* @@ -281,7 +280,8 @@ file_ascmagic_with_encoding(struct magic_set *ms, if (file_printf(ms, ", with") == -1) goto done; - if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) { + if (n_crlf == 0 && n_cr == 0 && + n_nel == 0 && n_lf == 0) { if (file_printf(ms, " no") == -1) goto done; } else { @@ -333,8 +333,8 @@ done: * Encode Unicode string as UTF-8, returning pointer to character * after end of string, or NULL if an invalid character is found. */ -private unsigned char * -encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen) +file_private unsigned char * +encode_utf8(unsigned char *buf, size_t len, file_unichar_t *ubuf, size_t ulen) { size_t i; unsigned char *end = buf + len; @@ -344,43 +344,45 @@ encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen) if (end - buf < 1) return NULL; *buf++ = CAST(unsigned char, ubuf[i]); - } else if (ubuf[i] <= 0x7ff) { + continue; + } + if (ubuf[i] <= 0x7ff) { if (end - buf < 2) return NULL; *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0); - *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80); - } else if (ubuf[i] <= 0xffff) { + goto out1; + } + if (ubuf[i] <= 0xffff) { if (end - buf < 3) return NULL; *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80); - } else if (ubuf[i] <= 0x1fffff) { + goto out2; + } + if (ubuf[i] <= 0x1fffff) { if (end - buf < 4) return NULL; *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80); - } else if (ubuf[i] <= 0x3ffffff) { + goto out3; + } + if (ubuf[i] <= 0x3ffffff) { if (end - buf < 5) return NULL; *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80); - } else if (ubuf[i] <= 0x7fffffff) { + goto out4; + } + if (ubuf[i] <= 0x7fffffff) { if (end - buf < 6) return NULL; *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80); - *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80); - } else /* Invalid character */ - return NULL; + goto out5; + } + /* Invalid character */ + return NULL; + out5: *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80); + out4: *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80); + out3: *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80); + out2: *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80); + out1: *buf++ = CAST(unsigned char, ((ubuf[i] >> 0) & 0x3f) + 0x80); } return buf; |