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