2015-03-15 11:35:39 +00:00
|
|
|
/* $Id: preconv.c,v 1.14 2015/03/06 09:24:59 kristaps Exp $ */
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
|
2014-12-02 07:34:06 +00:00
|
|
|
* Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
|
2012-10-18 09:55:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
#include <sys/types.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
#include <assert.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-12-02 07:34:06 +00:00
|
|
|
#include "mandoc.h"
|
|
|
|
#include "libmandoc.h"
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
int
|
|
|
|
preconv_encode(struct buf *ib, size_t *ii, struct buf *ob, size_t *oi,
|
|
|
|
int *filenc)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
2015-03-02 16:45:41 +00:00
|
|
|
unsigned char *cu;
|
|
|
|
int nby;
|
2012-10-18 09:55:16 +00:00
|
|
|
unsigned int accum;
|
2015-03-02 16:45:41 +00:00
|
|
|
|
2015-03-15 11:35:39 +00:00
|
|
|
cu = (unsigned char *)ib->buf + *ii;
|
2015-03-02 16:45:41 +00:00
|
|
|
assert(*cu & 0x80);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
if ( ! (*filenc & MPARSE_UTF8))
|
|
|
|
goto latin;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
nby = 1;
|
|
|
|
while (nby < 5 && *cu & (1 << (7 - nby)))
|
|
|
|
nby++;
|
|
|
|
|
|
|
|
switch (nby) {
|
|
|
|
case 2:
|
|
|
|
accum = *cu & 0x1f;
|
|
|
|
if (accum < 0x02) /* Obfuscated ASCII. */
|
|
|
|
goto latin;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
accum = *cu & 0x0f;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
accum = *cu & 0x07;
|
|
|
|
if (accum > 0x04) /* Beyond Unicode. */
|
|
|
|
goto latin;
|
|
|
|
break;
|
|
|
|
default: /* Bad sequence header. */
|
|
|
|
goto latin;
|
|
|
|
}
|
|
|
|
|
|
|
|
cu++;
|
|
|
|
switch (nby) {
|
|
|
|
case 3:
|
|
|
|
if ((accum == 0x00 && ! (*cu & 0x20)) || /* Use 2-byte. */
|
|
|
|
(accum == 0x0d && *cu & 0x20)) /* Surrogates. */
|
|
|
|
goto latin;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if ((accum == 0x00 && ! (*cu & 0x30)) || /* Use 3-byte. */
|
|
|
|
(accum == 0x04 && *cu & 0x30)) /* Beyond Unicode. */
|
|
|
|
goto latin;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
while (--nby) {
|
|
|
|
if ((*cu & 0xc0) != 0x80) /* Invalid continuation. */
|
|
|
|
goto latin;
|
|
|
|
accum <<= 6;
|
|
|
|
accum += *cu & 0x3f;
|
|
|
|
cu++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(accum > 0x7f);
|
|
|
|
assert(accum < 0x110000);
|
|
|
|
assert(accum < 0xd800 || accum > 0xdfff);
|
|
|
|
|
|
|
|
*oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum);
|
|
|
|
*ii = (char *)cu - ib->buf;
|
|
|
|
*filenc &= ~MPARSE_LATIN1;
|
|
|
|
return(1);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
latin:
|
|
|
|
if ( ! (*filenc & MPARSE_LATIN1))
|
2012-10-18 09:55:16 +00:00
|
|
|
return(0);
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
*oi += snprintf(ob->buf + *oi, 11,
|
|
|
|
"\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
*filenc &= ~MPARSE_UTF8;
|
|
|
|
return(1);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
int
|
|
|
|
preconv_cue(const struct buf *b, size_t offset)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
const char *ln, *eoln, *eoph;
|
2014-12-02 07:34:06 +00:00
|
|
|
size_t sz, phsz;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
ln = b->buf + offset;
|
|
|
|
sz = b->sz - offset;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/* Look for the end-of-line. */
|
|
|
|
|
|
|
|
if (NULL == (eoln = memchr(ln, '\n', sz)))
|
2014-12-02 07:34:06 +00:00
|
|
|
eoln = ln + sz;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/* Check if we have the correct header/trailer. */
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
if ((sz = (size_t)(eoln - ln)) < 10 ||
|
|
|
|
memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3))
|
|
|
|
return(MPARSE_UTF8 | MPARSE_LATIN1);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/* Move after the header and adjust for the trailer. */
|
|
|
|
|
|
|
|
ln += 7;
|
|
|
|
sz -= 10;
|
|
|
|
|
|
|
|
while (sz > 0) {
|
|
|
|
while (sz > 0 && ' ' == *ln) {
|
|
|
|
ln++;
|
|
|
|
sz--;
|
|
|
|
}
|
|
|
|
if (0 == sz)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Find the end-of-phrase marker (or eoln). */
|
|
|
|
|
|
|
|
if (NULL == (eoph = memchr(ln, ';', sz)))
|
|
|
|
eoph = eoln - 3;
|
|
|
|
else
|
|
|
|
eoph++;
|
|
|
|
|
|
|
|
/* Only account for the "coding" phrase. */
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
if ((phsz = eoph - ln) < 7 ||
|
|
|
|
strncasecmp(ln, "coding:", 7)) {
|
2012-10-18 09:55:16 +00:00
|
|
|
sz -= phsz;
|
|
|
|
ln += phsz;
|
|
|
|
continue;
|
2014-12-02 07:34:06 +00:00
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
sz -= 7;
|
|
|
|
ln += 7;
|
|
|
|
|
|
|
|
while (sz > 0 && ' ' == *ln) {
|
|
|
|
ln++;
|
|
|
|
sz--;
|
|
|
|
}
|
|
|
|
if (0 == sz)
|
2014-12-02 07:34:06 +00:00
|
|
|
return(0);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/* Check us against known encodings. */
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
if (phsz > 4 && !strncasecmp(ln, "utf-8", 5))
|
|
|
|
return(MPARSE_UTF8);
|
|
|
|
if (phsz > 10 && !strncasecmp(ln, "iso-latin-1", 11))
|
|
|
|
return(MPARSE_LATIN1);
|
|
|
|
return(0);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2014-12-02 07:34:06 +00:00
|
|
|
return(MPARSE_UTF8 | MPARSE_LATIN1);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|