1*cec8643bSMichal Nowak /* $Id: preconv.c,v 1.17 2018/12/13 11:55:47 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4260e9a87SYuri Pankov * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
595c635efSGarrett D'Amore *
695c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore *
1095c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1195c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1295c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1395c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1795c635efSGarrett D'Amore */
1895c635efSGarrett D'Amore #include "config.h"
1995c635efSGarrett D'Amore
20260e9a87SYuri Pankov #include <sys/types.h>
2195c635efSGarrett D'Amore
2295c635efSGarrett D'Amore #include <assert.h>
2395c635efSGarrett D'Amore #include <stdio.h>
2495c635efSGarrett D'Amore #include <string.h>
25*cec8643bSMichal Nowak
26260e9a87SYuri Pankov #include "mandoc.h"
27*cec8643bSMichal Nowak #include "roff.h"
28*cec8643bSMichal Nowak #include "mandoc_parse.h"
29260e9a87SYuri Pankov #include "libmandoc.h"
3095c635efSGarrett D'Amore
31260e9a87SYuri Pankov int
preconv_encode(const struct buf * ib,size_t * ii,struct buf * ob,size_t * oi,int * filenc)32a40ea1a7SYuri Pankov preconv_encode(const struct buf *ib, size_t *ii, struct buf *ob, size_t *oi,
33260e9a87SYuri Pankov int *filenc)
3495c635efSGarrett D'Amore {
35a40ea1a7SYuri Pankov const unsigned char *cu;
36a40ea1a7SYuri Pankov int nby;
37a40ea1a7SYuri Pankov unsigned int accum;
3895c635efSGarrett D'Amore
39a40ea1a7SYuri Pankov cu = (const unsigned char *)ib->buf + *ii;
40260e9a87SYuri Pankov assert(*cu & 0x80);
41260e9a87SYuri Pankov
42260e9a87SYuri Pankov if ( ! (*filenc & MPARSE_UTF8))
43260e9a87SYuri Pankov goto latin;
44260e9a87SYuri Pankov
45260e9a87SYuri Pankov nby = 1;
46260e9a87SYuri Pankov while (nby < 5 && *cu & (1 << (7 - nby)))
47260e9a87SYuri Pankov nby++;
48260e9a87SYuri Pankov
49260e9a87SYuri Pankov switch (nby) {
50260e9a87SYuri Pankov case 2:
51260e9a87SYuri Pankov accum = *cu & 0x1f;
52260e9a87SYuri Pankov if (accum < 0x02) /* Obfuscated ASCII. */
53260e9a87SYuri Pankov goto latin;
54260e9a87SYuri Pankov break;
55260e9a87SYuri Pankov case 3:
56260e9a87SYuri Pankov accum = *cu & 0x0f;
57260e9a87SYuri Pankov break;
58260e9a87SYuri Pankov case 4:
59260e9a87SYuri Pankov accum = *cu & 0x07;
60260e9a87SYuri Pankov if (accum > 0x04) /* Beyond Unicode. */
61260e9a87SYuri Pankov goto latin;
62260e9a87SYuri Pankov break;
63260e9a87SYuri Pankov default: /* Bad sequence header. */
64260e9a87SYuri Pankov goto latin;
6595c635efSGarrett D'Amore }
6695c635efSGarrett D'Amore
67260e9a87SYuri Pankov cu++;
68260e9a87SYuri Pankov switch (nby) {
69260e9a87SYuri Pankov case 3:
70260e9a87SYuri Pankov if ((accum == 0x00 && ! (*cu & 0x20)) || /* Use 2-byte. */
71260e9a87SYuri Pankov (accum == 0x0d && *cu & 0x20)) /* Surrogates. */
72260e9a87SYuri Pankov goto latin;
73260e9a87SYuri Pankov break;
74260e9a87SYuri Pankov case 4:
75260e9a87SYuri Pankov if ((accum == 0x00 && ! (*cu & 0x30)) || /* Use 3-byte. */
76260e9a87SYuri Pankov (accum == 0x04 && *cu & 0x30)) /* Beyond Unicode. */
77260e9a87SYuri Pankov goto latin;
78260e9a87SYuri Pankov break;
79260e9a87SYuri Pankov default:
80260e9a87SYuri Pankov break;
8195c635efSGarrett D'Amore }
8295c635efSGarrett D'Amore
83260e9a87SYuri Pankov while (--nby) {
84260e9a87SYuri Pankov if ((*cu & 0xc0) != 0x80) /* Invalid continuation. */
85260e9a87SYuri Pankov goto latin;
86260e9a87SYuri Pankov accum <<= 6;
87260e9a87SYuri Pankov accum += *cu & 0x3f;
88260e9a87SYuri Pankov cu++;
8995c635efSGarrett D'Amore }
9095c635efSGarrett D'Amore
91260e9a87SYuri Pankov assert(accum > 0x7f);
92260e9a87SYuri Pankov assert(accum < 0x110000);
93260e9a87SYuri Pankov assert(accum < 0xd800 || accum > 0xdfff);
9495c635efSGarrett D'Amore
95260e9a87SYuri Pankov *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum);
96a40ea1a7SYuri Pankov *ii = (const char *)cu - ib->buf;
97260e9a87SYuri Pankov *filenc &= ~MPARSE_LATIN1;
98371584c2SYuri Pankov return 1;
9995c635efSGarrett D'Amore
100260e9a87SYuri Pankov latin:
101260e9a87SYuri Pankov if ( ! (*filenc & MPARSE_LATIN1))
102371584c2SYuri Pankov return 0;
10395c635efSGarrett D'Amore
104260e9a87SYuri Pankov *oi += snprintf(ob->buf + *oi, 11,
105260e9a87SYuri Pankov "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]);
10695c635efSGarrett D'Amore
107260e9a87SYuri Pankov *filenc &= ~MPARSE_UTF8;
108371584c2SYuri Pankov return 1;
10995c635efSGarrett D'Amore }
11095c635efSGarrett D'Amore
111260e9a87SYuri Pankov int
preconv_cue(const struct buf * b,size_t offset)112260e9a87SYuri Pankov preconv_cue(const struct buf *b, size_t offset)
11395c635efSGarrett D'Amore {
11495c635efSGarrett D'Amore const char *ln, *eoln, *eoph;
115260e9a87SYuri Pankov size_t sz, phsz;
11695c635efSGarrett D'Amore
117260e9a87SYuri Pankov ln = b->buf + offset;
118260e9a87SYuri Pankov sz = b->sz - offset;
11995c635efSGarrett D'Amore
12095c635efSGarrett D'Amore /* Look for the end-of-line. */
12195c635efSGarrett D'Amore
12295c635efSGarrett D'Amore if (NULL == (eoln = memchr(ln, '\n', sz)))
123260e9a87SYuri Pankov eoln = ln + sz;
12495c635efSGarrett D'Amore
12595c635efSGarrett D'Amore /* Check if we have the correct header/trailer. */
12695c635efSGarrett D'Amore
127260e9a87SYuri Pankov if ((sz = (size_t)(eoln - ln)) < 10 ||
128260e9a87SYuri Pankov memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3))
129371584c2SYuri Pankov return MPARSE_UTF8 | MPARSE_LATIN1;
13095c635efSGarrett D'Amore
13195c635efSGarrett D'Amore /* Move after the header and adjust for the trailer. */
13295c635efSGarrett D'Amore
13395c635efSGarrett D'Amore ln += 7;
13495c635efSGarrett D'Amore sz -= 10;
13595c635efSGarrett D'Amore
13695c635efSGarrett D'Amore while (sz > 0) {
13795c635efSGarrett D'Amore while (sz > 0 && ' ' == *ln) {
13895c635efSGarrett D'Amore ln++;
13995c635efSGarrett D'Amore sz--;
14095c635efSGarrett D'Amore }
14195c635efSGarrett D'Amore if (0 == sz)
14295c635efSGarrett D'Amore break;
14395c635efSGarrett D'Amore
14495c635efSGarrett D'Amore /* Find the end-of-phrase marker (or eoln). */
14595c635efSGarrett D'Amore
14695c635efSGarrett D'Amore if (NULL == (eoph = memchr(ln, ';', sz)))
14795c635efSGarrett D'Amore eoph = eoln - 3;
14895c635efSGarrett D'Amore else
14995c635efSGarrett D'Amore eoph++;
15095c635efSGarrett D'Amore
15195c635efSGarrett D'Amore /* Only account for the "coding" phrase. */
15295c635efSGarrett D'Amore
153260e9a87SYuri Pankov if ((phsz = eoph - ln) < 7 ||
154260e9a87SYuri Pankov strncasecmp(ln, "coding:", 7)) {
15595c635efSGarrett D'Amore sz -= phsz;
15695c635efSGarrett D'Amore ln += phsz;
15795c635efSGarrett D'Amore continue;
158260e9a87SYuri Pankov }
15995c635efSGarrett D'Amore
16095c635efSGarrett D'Amore sz -= 7;
16195c635efSGarrett D'Amore ln += 7;
16295c635efSGarrett D'Amore
16395c635efSGarrett D'Amore while (sz > 0 && ' ' == *ln) {
16495c635efSGarrett D'Amore ln++;
16595c635efSGarrett D'Amore sz--;
16695c635efSGarrett D'Amore }
16795c635efSGarrett D'Amore if (0 == sz)
168371584c2SYuri Pankov return 0;
16995c635efSGarrett D'Amore
17095c635efSGarrett D'Amore /* Check us against known encodings. */
17195c635efSGarrett D'Amore
172260e9a87SYuri Pankov if (phsz > 4 && !strncasecmp(ln, "utf-8", 5))
173371584c2SYuri Pankov return MPARSE_UTF8;
174260e9a87SYuri Pankov if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11))
175371584c2SYuri Pankov return MPARSE_LATIN1;
176371584c2SYuri Pankov return 0;
17795c635efSGarrett D'Amore }
178371584c2SYuri Pankov return MPARSE_UTF8 | MPARSE_LATIN1;
17995c635efSGarrett D'Amore }
180