1*260e9a87SYuri Pankov /* $Id: preconv.c,v 1.14 2015/03/06 09:24:59 kristaps Exp $ */ 295c635efSGarrett D'Amore /* 395c635efSGarrett D'Amore * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4*260e9a87SYuri 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 20*260e9a87SYuri 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*260e9a87SYuri Pankov #include "mandoc.h" 26*260e9a87SYuri Pankov #include "libmandoc.h" 2795c635efSGarrett D'Amore 28*260e9a87SYuri Pankov int 29*260e9a87SYuri Pankov preconv_encode(struct buf *ib, size_t *ii, struct buf *ob, size_t *oi, 30*260e9a87SYuri Pankov int *filenc) 3195c635efSGarrett D'Amore { 32*260e9a87SYuri Pankov unsigned char *cu; 33*260e9a87SYuri Pankov int nby; 3495c635efSGarrett D'Amore unsigned int accum; 3595c635efSGarrett D'Amore 36*260e9a87SYuri Pankov cu = (unsigned char *)ib->buf + *ii; 37*260e9a87SYuri Pankov assert(*cu & 0x80); 3895c635efSGarrett D'Amore 39*260e9a87SYuri Pankov if ( ! (*filenc & MPARSE_UTF8)) 40*260e9a87SYuri Pankov goto latin; 4195c635efSGarrett D'Amore 42*260e9a87SYuri Pankov nby = 1; 43*260e9a87SYuri Pankov while (nby < 5 && *cu & (1 << (7 - nby))) 44*260e9a87SYuri Pankov nby++; 4595c635efSGarrett D'Amore 46*260e9a87SYuri Pankov switch (nby) { 47*260e9a87SYuri Pankov case 2: 48*260e9a87SYuri Pankov accum = *cu & 0x1f; 49*260e9a87SYuri Pankov if (accum < 0x02) /* Obfuscated ASCII. */ 50*260e9a87SYuri Pankov goto latin; 5195c635efSGarrett D'Amore break; 52*260e9a87SYuri Pankov case 3: 53*260e9a87SYuri Pankov accum = *cu & 0x0f; 54*260e9a87SYuri Pankov break; 55*260e9a87SYuri Pankov case 4: 56*260e9a87SYuri Pankov accum = *cu & 0x07; 57*260e9a87SYuri Pankov if (accum > 0x04) /* Beyond Unicode. */ 58*260e9a87SYuri Pankov goto latin; 59*260e9a87SYuri Pankov break; 60*260e9a87SYuri Pankov default: /* Bad sequence header. */ 61*260e9a87SYuri Pankov goto latin; 62*260e9a87SYuri Pankov } 6395c635efSGarrett D'Amore 64*260e9a87SYuri Pankov cu++; 65*260e9a87SYuri Pankov switch (nby) { 66*260e9a87SYuri Pankov case 3: 67*260e9a87SYuri Pankov if ((accum == 0x00 && ! (*cu & 0x20)) || /* Use 2-byte. */ 68*260e9a87SYuri Pankov (accum == 0x0d && *cu & 0x20)) /* Surrogates. */ 69*260e9a87SYuri Pankov goto latin; 7095c635efSGarrett D'Amore break; 71*260e9a87SYuri Pankov case 4: 72*260e9a87SYuri Pankov if ((accum == 0x00 && ! (*cu & 0x30)) || /* Use 3-byte. */ 73*260e9a87SYuri Pankov (accum == 0x04 && *cu & 0x30)) /* Beyond Unicode. */ 74*260e9a87SYuri Pankov goto latin; 7595c635efSGarrett D'Amore break; 7695c635efSGarrett D'Amore default: 7795c635efSGarrett D'Amore break; 7895c635efSGarrett D'Amore } 7995c635efSGarrett D'Amore 80*260e9a87SYuri Pankov while (--nby) { 81*260e9a87SYuri Pankov if ((*cu & 0xc0) != 0x80) /* Invalid continuation. */ 82*260e9a87SYuri Pankov goto latin; 83*260e9a87SYuri Pankov accum <<= 6; 84*260e9a87SYuri Pankov accum += *cu & 0x3f; 85*260e9a87SYuri Pankov cu++; 86*260e9a87SYuri Pankov } 8795c635efSGarrett D'Amore 88*260e9a87SYuri Pankov assert(accum > 0x7f); 89*260e9a87SYuri Pankov assert(accum < 0x110000); 90*260e9a87SYuri Pankov assert(accum < 0xd800 || accum > 0xdfff); 91*260e9a87SYuri Pankov 92*260e9a87SYuri Pankov *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum); 93*260e9a87SYuri Pankov *ii = (char *)cu - ib->buf; 94*260e9a87SYuri Pankov *filenc &= ~MPARSE_LATIN1; 95*260e9a87SYuri Pankov return(1); 96*260e9a87SYuri Pankov 97*260e9a87SYuri Pankov latin: 98*260e9a87SYuri Pankov if ( ! (*filenc & MPARSE_LATIN1)) 99*260e9a87SYuri Pankov return(0); 100*260e9a87SYuri Pankov 101*260e9a87SYuri Pankov *oi += snprintf(ob->buf + *oi, 11, 102*260e9a87SYuri Pankov "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]); 103*260e9a87SYuri Pankov 104*260e9a87SYuri Pankov *filenc &= ~MPARSE_UTF8; 10595c635efSGarrett D'Amore return(1); 10695c635efSGarrett D'Amore } 10795c635efSGarrett D'Amore 108*260e9a87SYuri Pankov int 109*260e9a87SYuri Pankov preconv_cue(const struct buf *b, size_t offset) 11095c635efSGarrett D'Amore { 11195c635efSGarrett D'Amore const char *ln, *eoln, *eoph; 112*260e9a87SYuri Pankov size_t sz, phsz; 11395c635efSGarrett D'Amore 114*260e9a87SYuri Pankov ln = b->buf + offset; 115*260e9a87SYuri Pankov sz = b->sz - offset; 11695c635efSGarrett D'Amore 11795c635efSGarrett D'Amore /* Look for the end-of-line. */ 11895c635efSGarrett D'Amore 11995c635efSGarrett D'Amore if (NULL == (eoln = memchr(ln, '\n', sz))) 120*260e9a87SYuri Pankov eoln = ln + sz; 12195c635efSGarrett D'Amore 12295c635efSGarrett D'Amore /* Check if we have the correct header/trailer. */ 12395c635efSGarrett D'Amore 12495c635efSGarrett D'Amore if ((sz = (size_t)(eoln - ln)) < 10 || 125*260e9a87SYuri Pankov memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3)) 126*260e9a87SYuri Pankov return(MPARSE_UTF8 | MPARSE_LATIN1); 12795c635efSGarrett D'Amore 12895c635efSGarrett D'Amore /* Move after the header and adjust for the trailer. */ 12995c635efSGarrett D'Amore 13095c635efSGarrett D'Amore ln += 7; 13195c635efSGarrett D'Amore sz -= 10; 13295c635efSGarrett D'Amore 13395c635efSGarrett D'Amore while (sz > 0) { 13495c635efSGarrett D'Amore while (sz > 0 && ' ' == *ln) { 13595c635efSGarrett D'Amore ln++; 13695c635efSGarrett D'Amore sz--; 13795c635efSGarrett D'Amore } 13895c635efSGarrett D'Amore if (0 == sz) 13995c635efSGarrett D'Amore break; 14095c635efSGarrett D'Amore 14195c635efSGarrett D'Amore /* Find the end-of-phrase marker (or eoln). */ 14295c635efSGarrett D'Amore 14395c635efSGarrett D'Amore if (NULL == (eoph = memchr(ln, ';', sz))) 14495c635efSGarrett D'Amore eoph = eoln - 3; 14595c635efSGarrett D'Amore else 14695c635efSGarrett D'Amore eoph++; 14795c635efSGarrett D'Amore 14895c635efSGarrett D'Amore /* Only account for the "coding" phrase. */ 14995c635efSGarrett D'Amore 150*260e9a87SYuri Pankov if ((phsz = eoph - ln) < 7 || 15195c635efSGarrett D'Amore strncasecmp(ln, "coding:", 7)) { 15295c635efSGarrett D'Amore sz -= phsz; 15395c635efSGarrett D'Amore ln += phsz; 15495c635efSGarrett D'Amore continue; 15595c635efSGarrett D'Amore } 15695c635efSGarrett D'Amore 15795c635efSGarrett D'Amore sz -= 7; 15895c635efSGarrett D'Amore ln += 7; 15995c635efSGarrett D'Amore 16095c635efSGarrett D'Amore while (sz > 0 && ' ' == *ln) { 16195c635efSGarrett D'Amore ln++; 16295c635efSGarrett D'Amore sz--; 16395c635efSGarrett D'Amore } 16495c635efSGarrett D'Amore if (0 == sz) 165*260e9a87SYuri Pankov return(0); 16695c635efSGarrett D'Amore 16795c635efSGarrett D'Amore /* Check us against known encodings. */ 16895c635efSGarrett D'Amore 169*260e9a87SYuri Pankov if (phsz > 4 && !strncasecmp(ln, "utf-8", 5)) 170*260e9a87SYuri Pankov return(MPARSE_UTF8); 171*260e9a87SYuri Pankov if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11)) 172*260e9a87SYuri Pankov return(MPARSE_LATIN1); 17395c635efSGarrett D'Amore return(0); 17495c635efSGarrett D'Amore } 175*260e9a87SYuri Pankov return(MPARSE_UTF8 | MPARSE_LATIN1); 17695c635efSGarrett D'Amore } 177