1 /* $Id: preconv.c,v 1.17 2018/12/13 11:55:47 schwarze Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
28 #include "mandoc_parse.h"
29 #include "libmandoc.h"
32 preconv_encode(const struct buf *ib, size_t *ii, struct buf *ob, size_t *oi,
35 const unsigned char *cu;
39 cu = (const unsigned char *)ib->buf + *ii;
42 if ( ! (*filenc & MPARSE_UTF8))
46 while (nby < 5 && *cu & (1 << (7 - nby)))
52 if (accum < 0x02) /* Obfuscated ASCII. */
60 if (accum > 0x04) /* Beyond Unicode. */
63 default: /* Bad sequence header. */
70 if ((accum == 0x00 && ! (*cu & 0x20)) || /* Use 2-byte. */
71 (accum == 0x0d && *cu & 0x20)) /* Surrogates. */
75 if ((accum == 0x00 && ! (*cu & 0x30)) || /* Use 3-byte. */
76 (accum == 0x04 && *cu & 0x30)) /* Beyond Unicode. */
84 if ((*cu & 0xc0) != 0x80) /* Invalid continuation. */
92 assert(accum < 0x110000);
93 assert(accum < 0xd800 || accum > 0xdfff);
95 *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum);
96 *ii = (const char *)cu - ib->buf;
97 *filenc &= ~MPARSE_LATIN1;
101 if ( ! (*filenc & MPARSE_LATIN1))
104 *oi += snprintf(ob->buf + *oi, 11,
105 "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]);
107 *filenc &= ~MPARSE_UTF8;
112 preconv_cue(const struct buf *b, size_t offset)
114 const char *ln, *eoln, *eoph;
117 ln = b->buf + offset;
120 /* Look for the end-of-line. */
122 if (NULL == (eoln = memchr(ln, '\n', sz)))
125 /* Check if we have the correct header/trailer. */
127 if ((sz = (size_t)(eoln - ln)) < 10 ||
128 memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3))
129 return MPARSE_UTF8 | MPARSE_LATIN1;
131 /* Move after the header and adjust for the trailer. */
137 while (sz > 0 && ' ' == *ln) {
144 /* Find the end-of-phrase marker (or eoln). */
146 if (NULL == (eoph = memchr(ln, ';', sz)))
151 /* Only account for the "coding" phrase. */
153 if ((phsz = eoph - ln) < 7 ||
154 strncasecmp(ln, "coding:", 7)) {
163 while (sz > 0 && ' ' == *ln) {
170 /* Check us against known encodings. */
172 if (phsz > 4 && !strncasecmp(ln, "utf-8", 5))
174 if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11))
175 return MPARSE_LATIN1;
178 return MPARSE_UTF8 | MPARSE_LATIN1;