[BACK]Return to preconv.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/preconv.c, Revision 1.9

1.9     ! schwarze    1: /*     $Id: preconv.c,v 1.8 2014/08/16 19:00:01 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
        !            28: preconv_encode(struct buf *ib, struct buf *ob, int *filenc)
1.1       kristaps   29: {
1.2       kristaps   30:        int              state, be;
                     31:        unsigned int     accum;
                     32:        size_t           i;
                     33:        unsigned char    cu;
                     34:        const long       one = 1L;
                     35:
1.9     ! schwarze   36:        if ( ! (*filenc & MPARSE_UTF8))
        !            37:                goto latin;
        !            38:
1.2       kristaps   39:        state = 0;
                     40:        accum = 0U;
                     41:        be = 0;
                     42:
                     43:        /* Quick test for big-endian value. */
                     44:
1.4       kristaps   45:        if ( ! (*((const char *)(&one))))
1.2       kristaps   46:                be = 1;
                     47:
1.9     ! schwarze   48:        for (i = ib->offs; i < ib->sz; i++) {
        !            49:                cu = ib->buf[i];
1.2       kristaps   50:                if (state) {
                     51:                        if ( ! (cu & 128) || (cu & 64)) {
                     52:                                /* Bad sequence header. */
1.9     ! schwarze   53:                                break;
1.2       kristaps   54:                        }
                     55:
                     56:                        /* Accept only legitimate bit patterns. */
                     57:
                     58:                        if (cu > 191 || cu < 128) {
                     59:                                /* Bad in-sequence bits. */
1.9     ! schwarze   60:                                break;
1.2       kristaps   61:                        }
                     62:
                     63:                        accum |= (cu & 63) << --state * 6;
                     64:
1.9     ! schwarze   65:                        if (state)
        !            66:                                continue;
        !            67:
1.2       kristaps   68:                        /*
                     69:                         * Accum is held in little-endian order as
                     70:                         * stipulated by the UTF-8 sequence coding.  We
                     71:                         * need to convert to a native big-endian if our
                     72:                         * architecture requires it.
                     73:                         */
                     74:
1.9     ! schwarze   75:                        if (be)
1.2       kristaps   76:                                accum = (accum >> 24) |
                     77:                                        ((accum << 8) & 0x00FF0000) |
                     78:                                        ((accum >> 8) & 0x0000FF00) |
                     79:                                        (accum << 24);
                     80:
1.9     ! schwarze   81:                        if (accum < 0x80)
        !            82:                                ob->buf[ob->offs++] = accum;
        !            83:                        else
        !            84:                                ob->offs += snprintf(ob->buf + ob->offs,
        !            85:                                    11, "\\[u%.4X]", accum);
        !            86:                        ib->offs = i + 1;
        !            87:                        *filenc &= ~MPARSE_LATIN1;
        !            88:                        return(1);
        !            89:                } else {
1.2       kristaps   90:                        /*
                     91:                         * Entering a UTF-8 state:  if we encounter a
                     92:                         * UTF-8 bitmask, calculate the expected UTF-8
                     93:                         * state from it.
                     94:                         */
                     95:                        for (state = 0; state < 7; state++)
                     96:                                if ( ! (cu & (1 << (7 - state))))
                     97:                                        break;
                     98:
                     99:                        /* Accept only legitimate bit patterns. */
                    100:
1.9     ! schwarze  101:                        switch (state--) {
1.2       kristaps  102:                        case (4):
                    103:                                if (cu <= 244 && cu >= 240) {
                    104:                                        accum = (cu & 7) << 18;
1.9     ! schwarze  105:                                        continue;
1.2       kristaps  106:                                }
                    107:                                /* Bad 4-sequence start bits. */
1.9     ! schwarze  108:                                break;
1.2       kristaps  109:                        case (3):
                    110:                                if (cu <= 239 && cu >= 224) {
                    111:                                        accum = (cu & 15) << 12;
1.9     ! schwarze  112:                                        continue;
1.2       kristaps  113:                                }
                    114:                                /* Bad 3-sequence start bits. */
1.9     ! schwarze  115:                                break;
1.2       kristaps  116:                        case (2):
                    117:                                if (cu <= 223 && cu >= 194) {
                    118:                                        accum = (cu & 31) << 6;
1.9     ! schwarze  119:                                        continue;
1.2       kristaps  120:                                }
                    121:                                /* Bad 2-sequence start bits. */
1.9     ! schwarze  122:                                break;
1.2       kristaps  123:                        default:
                    124:                                /* Bad sequence bit mask. */
1.9     ! schwarze  125:                                break;
1.2       kristaps  126:                        }
1.9     ! schwarze  127:                        break;
        !           128:                }
1.1       kristaps  129:        }
                    130:
1.9     ! schwarze  131:        /* FALLTHROUGH: Invalid or incomplete UTF-8 sequence. */
1.1       kristaps  132:
1.9     ! schwarze  133: latin:
        !           134:        if ( ! (*filenc & MPARSE_LATIN1))
1.1       kristaps  135:                return(0);
                    136:
1.9     ! schwarze  137:        ob->offs += snprintf(ob->buf + ob->offs, 11,
        !           138:            "\\[u%.4X]", (unsigned char)ib->buf[ib->offs++]);
1.1       kristaps  139:
1.9     ! schwarze  140:        *filenc &= ~MPARSE_UTF8;
        !           141:        return(1);
1.1       kristaps  142: }
                    143:
1.9     ! schwarze  144: int
        !           145: preconv_cue(const struct buf *b)
1.3       kristaps  146: {
                    147:        const char      *ln, *eoln, *eoph;
1.9     ! schwarze  148:        size_t           sz, phsz;
1.3       kristaps  149:
1.9     ! schwarze  150:        ln = b->buf + b->offs;
        !           151:        sz = b->sz - b->offs;
1.3       kristaps  152:
                    153:        /* Look for the end-of-line. */
                    154:
                    155:        if (NULL == (eoln = memchr(ln, '\n', sz)))
1.9     ! schwarze  156:                eoln = ln + sz;
1.3       kristaps  157:
                    158:        /* Check if we have the correct header/trailer. */
                    159:
                    160:        if ((sz = (size_t)(eoln - ln)) < 10 ||
                    161:                        memcmp(ln, ".\\\" -*-", 7) ||
                    162:                        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