preconv.c revision 1.4 1 1.4 christos /* Id: preconv.c,v 1.16 2017/02/18 13:43:52 schwarze Exp */
2 1.1 joerg /*
3 1.1 joerg * Copyright (c) 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 1.2 christos * Copyright (c) 2014 Ingo Schwarze <schwarze (at) openbsd.org>
5 1.1 joerg *
6 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
7 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
8 1.1 joerg * copyright notice and this permission notice appear in all copies.
9 1.1 joerg *
10 1.1 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 joerg */
18 1.1 joerg #include "config.h"
19 1.1 joerg
20 1.2 christos #include <sys/types.h>
21 1.1 joerg
22 1.1 joerg #include <assert.h>
23 1.1 joerg #include <stdio.h>
24 1.1 joerg #include <string.h>
25 1.2 christos #include "mandoc.h"
26 1.2 christos #include "libmandoc.h"
27 1.1 joerg
28 1.2 christos int
29 1.2 christos preconv_encode(const struct buf *ib, size_t *ii, struct buf *ob, size_t *oi,
30 1.2 christos int *filenc)
31 1.1 joerg {
32 1.2 christos const unsigned char *cu;
33 1.4 christos int nby;
34 1.4 christos unsigned int accum;
35 1.1 joerg
36 1.2 christos cu = (const unsigned char *)ib->buf + *ii;
37 1.2 christos assert(*cu & 0x80);
38 1.1 joerg
39 1.2 christos if ( ! (*filenc & MPARSE_UTF8))
40 1.2 christos goto latin;
41 1.1 joerg
42 1.2 christos nby = 1;
43 1.2 christos while (nby < 5 && *cu & (1 << (7 - nby)))
44 1.2 christos nby++;
45 1.2 christos
46 1.2 christos switch (nby) {
47 1.2 christos case 2:
48 1.2 christos accum = *cu & 0x1f;
49 1.2 christos if (accum < 0x02) /* Obfuscated ASCII. */
50 1.2 christos goto latin;
51 1.2 christos break;
52 1.2 christos case 3:
53 1.2 christos accum = *cu & 0x0f;
54 1.2 christos break;
55 1.2 christos case 4:
56 1.2 christos accum = *cu & 0x07;
57 1.2 christos if (accum > 0x04) /* Beyond Unicode. */
58 1.2 christos goto latin;
59 1.2 christos break;
60 1.2 christos default: /* Bad sequence header. */
61 1.2 christos goto latin;
62 1.2 christos }
63 1.2 christos
64 1.2 christos cu++;
65 1.2 christos switch (nby) {
66 1.2 christos case 3:
67 1.2 christos if ((accum == 0x00 && ! (*cu & 0x20)) || /* Use 2-byte. */
68 1.2 christos (accum == 0x0d && *cu & 0x20)) /* Surrogates. */
69 1.2 christos goto latin;
70 1.2 christos break;
71 1.2 christos case 4:
72 1.2 christos if ((accum == 0x00 && ! (*cu & 0x30)) || /* Use 3-byte. */
73 1.2 christos (accum == 0x04 && *cu & 0x30)) /* Beyond Unicode. */
74 1.2 christos goto latin;
75 1.2 christos break;
76 1.2 christos default:
77 1.2 christos break;
78 1.2 christos }
79 1.2 christos
80 1.2 christos while (--nby) {
81 1.2 christos if ((*cu & 0xc0) != 0x80) /* Invalid continuation. */
82 1.2 christos goto latin;
83 1.2 christos accum <<= 6;
84 1.2 christos accum += *cu & 0x3f;
85 1.2 christos cu++;
86 1.2 christos }
87 1.2 christos
88 1.2 christos assert(accum > 0x7f);
89 1.2 christos assert(accum < 0x110000);
90 1.2 christos assert(accum < 0xd800 || accum > 0xdfff);
91 1.2 christos
92 1.2 christos *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum);
93 1.2 christos *ii = (const char *)cu - ib->buf;
94 1.2 christos *filenc &= ~MPARSE_LATIN1;
95 1.3 christos return 1;
96 1.1 joerg
97 1.2 christos latin:
98 1.2 christos if ( ! (*filenc & MPARSE_LATIN1))
99 1.3 christos return 0;
100 1.1 joerg
101 1.2 christos *oi += snprintf(ob->buf + *oi, 11,
102 1.2 christos "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]);
103 1.1 joerg
104 1.2 christos *filenc &= ~MPARSE_UTF8;
105 1.3 christos return 1;
106 1.1 joerg }
107 1.1 joerg
108 1.2 christos int
109 1.2 christos preconv_cue(const struct buf *b, size_t offset)
110 1.1 joerg {
111 1.1 joerg const char *ln, *eoln, *eoph;
112 1.2 christos size_t sz, phsz;
113 1.1 joerg
114 1.2 christos ln = b->buf + offset;
115 1.2 christos sz = b->sz - offset;
116 1.1 joerg
117 1.1 joerg /* Look for the end-of-line. */
118 1.1 joerg
119 1.1 joerg if (NULL == (eoln = memchr(ln, '\n', sz)))
120 1.2 christos eoln = ln + sz;
121 1.1 joerg
122 1.1 joerg /* Check if we have the correct header/trailer. */
123 1.1 joerg
124 1.2 christos if ((sz = (size_t)(eoln - ln)) < 10 ||
125 1.2 christos memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3))
126 1.3 christos return MPARSE_UTF8 | MPARSE_LATIN1;
127 1.1 joerg
128 1.1 joerg /* Move after the header and adjust for the trailer. */
129 1.1 joerg
130 1.1 joerg ln += 7;
131 1.1 joerg sz -= 10;
132 1.1 joerg
133 1.1 joerg while (sz > 0) {
134 1.1 joerg while (sz > 0 && ' ' == *ln) {
135 1.1 joerg ln++;
136 1.1 joerg sz--;
137 1.1 joerg }
138 1.1 joerg if (0 == sz)
139 1.1 joerg break;
140 1.1 joerg
141 1.1 joerg /* Find the end-of-phrase marker (or eoln). */
142 1.1 joerg
143 1.1 joerg if (NULL == (eoph = memchr(ln, ';', sz)))
144 1.1 joerg eoph = eoln - 3;
145 1.1 joerg else
146 1.1 joerg eoph++;
147 1.1 joerg
148 1.1 joerg /* Only account for the "coding" phrase. */
149 1.1 joerg
150 1.2 christos if ((phsz = eoph - ln) < 7 ||
151 1.2 christos strncasecmp(ln, "coding:", 7)) {
152 1.1 joerg sz -= phsz;
153 1.1 joerg ln += phsz;
154 1.1 joerg continue;
155 1.2 christos }
156 1.1 joerg
157 1.1 joerg sz -= 7;
158 1.1 joerg ln += 7;
159 1.1 joerg
160 1.1 joerg while (sz > 0 && ' ' == *ln) {
161 1.1 joerg ln++;
162 1.1 joerg sz--;
163 1.1 joerg }
164 1.1 joerg if (0 == sz)
165 1.3 christos return 0;
166 1.1 joerg
167 1.1 joerg /* Check us against known encodings. */
168 1.1 joerg
169 1.2 christos if (phsz > 4 && !strncasecmp(ln, "utf-8", 5))
170 1.3 christos return MPARSE_UTF8;
171 1.2 christos if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11))
172 1.3 christos return MPARSE_LATIN1;
173 1.3 christos return 0;
174 1.1 joerg }
175 1.3 christos return MPARSE_UTF8 | MPARSE_LATIN1;
176 1.1 joerg }
177