=================================================================== RCS file: /cvs/mandoc/preconv.c,v retrieving revision 1.10 retrieving revision 1.12 diff -u -p -r1.10 -r1.12 --- mandoc/preconv.c 2014/10/26 18:22:51 1.10 +++ mandoc/preconv.c 2014/11/14 04:24:04 1.12 @@ -1,4 +1,4 @@ -/* $Id: preconv.c,v 1.10 2014/10/26 18:22:51 schwarze Exp $ */ +/* $Id: preconv.c,v 1.12 2014/11/14 04:24:04 schwarze Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -25,11 +25,11 @@ #include "libmandoc.h" int -preconv_encode(struct buf *ib, struct buf *ob, int *filenc) +preconv_encode(struct buf *ib, size_t *ii, struct buf *ob, size_t *oi, + int *filenc) { size_t i; - const long one = 1L; - int state, be; + int state; unsigned int accum; unsigned char cu; @@ -38,14 +38,8 @@ preconv_encode(struct buf *ib, struct buf *ob, int *fi state = 0; accum = 0U; - be = 0; - /* Quick test for big-endian value. */ - - if ( ! (*((const char *)(&one)))) - be = 1; - - for (i = ib->offs; i < ib->sz; i++) { + for (i = *ii; i < ib->sz; i++) { cu = ib->buf[i]; if (state) { if ( ! (cu & 128) || (cu & 64)) { @@ -65,25 +59,12 @@ preconv_encode(struct buf *ib, struct buf *ob, int *fi if (state) continue; - /* - * Accum is held in little-endian order as - * stipulated by the UTF-8 sequence coding. We - * need to convert to a native big-endian if our - * architecture requires it. - */ - - if (be) - accum = (accum >> 24) | - ((accum << 8) & 0x00FF0000) | - ((accum >> 8) & 0x0000FF00) | - (accum << 24); - if (accum < 0x80) - ob->buf[ob->offs++] = accum; + ob->buf[(*oi)++] = accum; else - ob->offs += snprintf(ob->buf + ob->offs, + *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum); - ib->offs = i + 1; + *ii = i + 1; *filenc &= ~MPARSE_LATIN1; return(1); } else { @@ -92,7 +73,7 @@ preconv_encode(struct buf *ib, struct buf *ob, int *fi * UTF-8 bitmask, calculate the expected UTF-8 * state from it. */ - for (state = 0; state < 7; state++) + for (state = 0; state < 7; state++) if ( ! (cu & (1 << (7 - state)))) break; @@ -134,21 +115,21 @@ latin: if ( ! (*filenc & MPARSE_LATIN1)) return(0); - ob->offs += snprintf(ob->buf + ob->offs, 11, - "\\[u%.4X]", (unsigned char)ib->buf[ib->offs++]); + *oi += snprintf(ob->buf + *oi, 11, + "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]); *filenc &= ~MPARSE_UTF8; return(1); } int -preconv_cue(const struct buf *b) +preconv_cue(const struct buf *b, size_t offset) { const char *ln, *eoln, *eoph; size_t sz, phsz; - ln = b->buf + b->offs; - sz = b->sz - b->offs; + ln = b->buf + offset; + sz = b->sz - offset; /* Look for the end-of-line. */ @@ -157,7 +138,7 @@ preconv_cue(const struct buf *b) /* Check if we have the correct header/trailer. */ - if ((sz = (size_t)(eoln - ln)) < 10 || + if ((sz = (size_t)(eoln - ln)) < 10 || memcmp(ln, ".\\\" -*-", 7) || memcmp(eoln - 3, "-*-", 3)) return(MPARSE_UTF8 | MPARSE_LATIN1); @@ -188,7 +169,7 @@ preconv_cue(const struct buf *b) sz -= phsz; ln += phsz; continue; - } + } sz -= 7; ln += 7;