Annotation of src/usr.bin/mandoc/preconv.c, Revision 1.2
1.2 ! schwarze 1: /* $OpenBSD: preconv.c,v 1.1 2014/10/30 00:05:02 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdio.h>
22: #include <string.h>
23: #include "mandoc.h"
24: #include "libmandoc.h"
25:
26: int
1.2 ! schwarze 27: preconv_encode(struct buf *ib, size_t *ii, struct buf *ob, size_t *oi,
! 28: int *filenc)
1.1 schwarze 29: {
30: size_t i;
31: const long one = 1L;
32: int state, be;
33: unsigned int accum;
34: unsigned char cu;
35:
36: if ( ! (*filenc & MPARSE_UTF8))
37: goto latin;
38:
39: state = 0;
40: accum = 0U;
41: be = 0;
42:
43: /* Quick test for big-endian value. */
44:
45: if ( ! (*((const char *)(&one))))
46: be = 1;
47:
1.2 ! schwarze 48: for (i = *ii; i < ib->sz; i++) {
1.1 schwarze 49: cu = ib->buf[i];
50: if (state) {
51: if ( ! (cu & 128) || (cu & 64)) {
52: /* Bad sequence header. */
53: break;
54: }
55:
56: /* Accept only legitimate bit patterns. */
57:
58: if (cu > 191 || cu < 128) {
59: /* Bad in-sequence bits. */
60: break;
61: }
62:
63: accum |= (cu & 63) << --state * 6;
64:
65: if (state)
66: continue;
67:
68: /*
69: * Accum is held in little-endian order as
70: * stipulated by the UTF-8 sequence coding. We
71: * need to convert to a native big-endian if our
72: * architecture requires it.
73: */
74:
75: if (be)
76: accum = (accum >> 24) |
77: ((accum << 8) & 0x00FF0000) |
78: ((accum >> 8) & 0x0000FF00) |
79: (accum << 24);
80:
81: if (accum < 0x80)
1.2 ! schwarze 82: ob->buf[(*oi)++] = accum;
1.1 schwarze 83: else
1.2 ! schwarze 84: *oi += snprintf(ob->buf + *oi,
1.1 schwarze 85: 11, "\\[u%.4X]", accum);
1.2 ! schwarze 86: *ii = i + 1;
1.1 schwarze 87: *filenc &= ~MPARSE_LATIN1;
88: return(1);
89: } else {
90: /*
91: * Entering a UTF-8 state: if we encounter a
92: * UTF-8 bitmask, calculate the expected UTF-8
93: * state from it.
94: */
95: for (state = 0; state < 7; state++)
96: if ( ! (cu & (1 << (7 - state))))
97: break;
98:
99: /* Accept only legitimate bit patterns. */
100:
101: switch (state--) {
102: case (4):
103: if (cu <= 244 && cu >= 240) {
104: accum = (cu & 7) << 18;
105: continue;
106: }
107: /* Bad 4-sequence start bits. */
108: break;
109: case (3):
110: if (cu <= 239 && cu >= 224) {
111: accum = (cu & 15) << 12;
112: continue;
113: }
114: /* Bad 3-sequence start bits. */
115: break;
116: case (2):
117: if (cu <= 223 && cu >= 194) {
118: accum = (cu & 31) << 6;
119: continue;
120: }
121: /* Bad 2-sequence start bits. */
122: break;
123: default:
124: /* Bad sequence bit mask. */
125: break;
126: }
127: break;
128: }
129: }
130:
131: /* FALLTHROUGH: Invalid or incomplete UTF-8 sequence. */
132:
133: latin:
134: if ( ! (*filenc & MPARSE_LATIN1))
135: return(0);
136:
1.2 ! schwarze 137: *oi += snprintf(ob->buf + *oi, 11,
! 138: "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]);
1.1 schwarze 139:
140: *filenc &= ~MPARSE_UTF8;
141: return(1);
142: }
143:
144: int
1.2 ! schwarze 145: preconv_cue(const struct buf *b, size_t offset)
1.1 schwarze 146: {
147: const char *ln, *eoln, *eoph;
148: size_t sz, phsz;
149:
1.2 ! schwarze 150: ln = b->buf + offset;
! 151: sz = b->sz - offset;
1.1 schwarze 152:
153: /* Look for the end-of-line. */
154:
155: if (NULL == (eoln = memchr(ln, '\n', sz)))
156: eoln = ln + sz;
157:
158: /* Check if we have the correct header/trailer. */
159:
160: if ((sz = (size_t)(eoln - ln)) < 10 ||
161: memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3))
162: return(MPARSE_UTF8 | MPARSE_LATIN1);
163:
164: /* Move after the header and adjust for the trailer. */
165:
166: ln += 7;
167: sz -= 10;
168:
169: while (sz > 0) {
170: while (sz > 0 && ' ' == *ln) {
171: ln++;
172: sz--;
173: }
174: if (0 == sz)
175: break;
176:
177: /* Find the end-of-phrase marker (or eoln). */
178:
179: if (NULL == (eoph = memchr(ln, ';', sz)))
180: eoph = eoln - 3;
181: else
182: eoph++;
183:
184: /* Only account for the "coding" phrase. */
185:
186: if ((phsz = eoph - ln) < 7 ||
187: strncasecmp(ln, "coding:", 7)) {
188: sz -= phsz;
189: ln += phsz;
190: continue;
191: }
192:
193: sz -= 7;
194: ln += 7;
195:
196: while (sz > 0 && ' ' == *ln) {
197: ln++;
198: sz--;
199: }
200: if (0 == sz)
201: return(0);
202:
203: /* Check us against known encodings. */
204:
205: if (phsz > 4 && !strncasecmp(ln, "utf-8", 5))
206: return(MPARSE_UTF8);
207: if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11))
208: return(MPARSE_LATIN1);
209: return(0);
210: }
211: return(MPARSE_UTF8 | MPARSE_LATIN1);
212: }