Annotation of XML/HTMLparser.c, revision 1.73

1.1       daniel      1: /*
                      2:  * HTMLparser.c : an HTML 4.0 non-verifying parser
                      3:  *
                      4:  * See Copyright for the status of this software.
                      5:  *
                      6:  * Daniel.Veillard@w3.org
                      7:  */
                      8: 
                      9: #ifdef WIN32
1.29      daniel     10: #include "win32config.h"
1.1       daniel     11: #else
1.13      daniel     12: #include "config.h"
1.1       daniel     13: #endif
1.13      daniel     14: 
1.39      daniel     15: #include "xmlversion.h"
                     16: #ifdef LIBXML_HTML_ENABLED
                     17: 
1.1       daniel     18: #include <stdio.h>
1.50      veillard   19: #include <string.h>
1.13      daniel     20: #ifdef HAVE_CTYPE_H
1.1       daniel     21: #include <ctype.h>
1.13      daniel     22: #endif
                     23: #ifdef HAVE_STDLIB_H
1.1       daniel     24: #include <stdlib.h>
1.13      daniel     25: #endif
                     26: #ifdef HAVE_SYS_STAT_H
1.1       daniel     27: #include <sys/stat.h>
1.13      daniel     28: #endif
1.1       daniel     29: #ifdef HAVE_FCNTL_H
                     30: #include <fcntl.h>
                     31: #endif
                     32: #ifdef HAVE_UNISTD_H
                     33: #include <unistd.h>
                     34: #endif
                     35: #ifdef HAVE_ZLIB_H
                     36: #include <zlib.h>
                     37: #endif
                     38: 
1.39      daniel     39: #include <libxml/xmlmemory.h>
                     40: #include <libxml/tree.h>
                     41: #include <libxml/HTMLparser.h>
                     42: #include <libxml/entities.h>
                     43: #include <libxml/encoding.h>
1.50      veillard   44: #include <libxml/parser.h>
1.39      daniel     45: #include <libxml/valid.h>
                     46: #include <libxml/parserInternals.h>
                     47: #include <libxml/xmlIO.h>
1.31      daniel     48: #include "xml-error.h"
1.5       daniel     49: 
                     50: #define HTML_MAX_NAMELEN 1000
1.53      veillard   51: #define HTML_PARSER_BIG_BUFFER_SIZE 1000
1.31      daniel     52: #define HTML_PARSER_BUFFER_SIZE 100
1.1       daniel     53: 
                     54: /* #define DEBUG */
1.31      daniel     55: /* #define DEBUG_PUSH */
1.1       daniel     56: 
                     57: /************************************************************************
                     58:  *                                                                     *
                     59:  *             Parser stacks related functions and macros              *
                     60:  *                                                                     *
                     61:  ************************************************************************/
                     62: 
                     63: /*
                     64:  * Generic function for accessing stacks in the Parser Context
                     65:  */
                     66: 
1.30      daniel     67: #define PUSH_AND_POP(scope, type, name)                                        \
                     68: scope int html##name##Push(htmlParserCtxtPtr ctxt, type value) {       \
1.1       daniel     69:     if (ctxt->name##Nr >= ctxt->name##Max) {                           \
                     70:        ctxt->name##Max *= 2;                                           \
1.50      veillard   71:         ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,         \
1.1       daniel     72:                     ctxt->name##Max * sizeof(ctxt->name##Tab[0]));     \
                     73:         if (ctxt->name##Tab == NULL) {                                 \
                     74:            fprintf(stderr, "realloc failed !\n");                      \
1.33      daniel     75:            return(0);                                                  \
1.1       daniel     76:        }                                                               \
                     77:     }                                                                  \
                     78:     ctxt->name##Tab[ctxt->name##Nr] = value;                           \
                     79:     ctxt->name = value;                                                        \
                     80:     return(ctxt->name##Nr++);                                          \
                     81: }                                                                      \
1.30      daniel     82: scope type html##name##Pop(htmlParserCtxtPtr ctxt) {                   \
1.1       daniel     83:     type ret;                                                          \
1.18      daniel     84:     if (ctxt->name##Nr < 0) return(0);                                 \
1.1       daniel     85:     ctxt->name##Nr--;                                                  \
1.18      daniel     86:     if (ctxt->name##Nr < 0) return(0);                                 \
1.1       daniel     87:     if (ctxt->name##Nr > 0)                                            \
                     88:        ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1];               \
                     89:     else                                                               \
                     90:         ctxt->name = NULL;                                             \
                     91:     ret = ctxt->name##Tab[ctxt->name##Nr];                             \
                     92:     ctxt->name##Tab[ctxt->name##Nr] = 0;                               \
                     93:     return(ret);                                                       \
                     94: }                                                                      \
                     95: 
1.30      daniel     96: PUSH_AND_POP(extern, xmlNodePtr, node)
                     97: PUSH_AND_POP(extern, xmlChar*, name)
1.1       daniel     98: 
                     99: /*
                    100:  * Macros for accessing the content. Those should be used only by the parser,
                    101:  * and not exported.
                    102:  *
                    103:  * Dirty macros, i.e. one need to make assumption on the context to use them
                    104:  *
1.14      daniel    105:  *   CUR_PTR return the current pointer to the xmlChar to be parsed.
                    106:  *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
1.1       daniel    107:  *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
                    108:  *           in UNICODE mode. This should be used internally by the parser
                    109:  *           only to compare to ASCII values otherwise it would break when
                    110:  *           running with UTF-8 encoding.
1.14      daniel    111:  *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
1.1       daniel    112:  *           to compare on ASCII based substring.
1.14      daniel    113:  *   UPP(n)  returns the n'th next xmlChar converted to uppercase. Same as CUR
1.1       daniel    114:  *           it should be used only to compare on ASCII based substring.
1.14      daniel    115:  *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1.1       daniel    116:  *           strings within the parser.
                    117:  *
                    118:  * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
                    119:  *
                    120:  *   CURRENT Returns the current char value, with the full decoding of
                    121:  *           UTF-8 if we are using this mode. It returns an int.
                    122:  *   NEXT    Skip to the next character, this does the proper decoding
                    123:  *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
                    124:  *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
                    125:  */
                    126: 
                    127: #define UPPER (toupper(*ctxt->input->cur))
1.36      daniel    128: 
1.26      daniel    129: #define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val)
1.36      daniel    130: 
1.1       daniel    131: #define NXT(val) ctxt->input->cur[(val)]
1.36      daniel    132: 
1.1       daniel    133: #define UPP(val) (toupper(ctxt->input->cur[(val)]))
1.36      daniel    134: 
1.1       daniel    135: #define CUR_PTR ctxt->input->cur
1.36      daniel    136: 
1.5       daniel    137: #define SHRINK  xmlParserInputShrink(ctxt->input)
1.36      daniel    138: 
1.5       daniel    139: #define GROW  xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
1.1       daniel    140: 
1.36      daniel    141: #define CURRENT ((int) (*ctxt->input->cur))
1.1       daniel    142: 
1.53      veillard  143: #define SKIP_BLANKS htmlSkipBlankChars(ctxt);
                    144: 
                    145: #if 0
                    146: #define CUR ((int) (*ctxt->input->cur))
1.36      daniel    147: #define NEXT htmlNextChar(ctxt);
1.53      veillard  148: #else
                    149: /* Inported from XML */
                    150: 
                    151: /* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
                    152: #define CUR ((int) (*ctxt->input->cur))
                    153: #define NEXT xmlNextChar(ctxt);ctxt->nbChars++;
                    154: 
                    155: #define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
                    156: #define NXT(val) ctxt->input->cur[(val)]
                    157: #define CUR_PTR ctxt->input->cur
                    158: 
                    159: 
                    160: #define NEXTL(l)                                                       \
                    161:     if (*(ctxt->input->cur) == '\n') {                                 \
                    162:        ctxt->input->line++; ctxt->input->col = 1;                      \
                    163:     } else ctxt->input->col++;                                         \
                    164:     ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++;
                    165:     
                    166: /************
                    167:     \
                    168:     if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);    \
                    169:     if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
                    170:  ************/
                    171: 
                    172: #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l);
                    173: #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l);
                    174: 
                    175: #define COPY_BUF(l,b,i,v)                                              \
                    176:     if (l == 1) b[i++] = (xmlChar) v;                                  \
                    177:     else i += xmlCopyChar(l,&b[i],v);
                    178: #endif
                    179: 
                    180: /**
                    181:  * htmlCurrentChar:
                    182:  * @ctxt:  the HTML parser context
                    183:  * @len:  pointer to the length of the char read
                    184:  *
                    185:  * The current char value, if using UTF-8 this may actaully span multiple
                    186:  * bytes in the input buffer. Implement the end of line normalization:
                    187:  * 2.11 End-of-Line Handling
                    188:  * If the encoding is unspecified, in the case we find an ISO-Latin-1
                    189:  * char, then the encoding converter is plugged in automatically.
                    190:  *
                    191:  * Returns the current char value and its lenght
                    192:  */
                    193: 
                    194: int
                    195: htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
                    196:     if (ctxt->instate == XML_PARSER_EOF)
                    197:        return(0);
1.35      daniel    198: 
1.53      veillard  199:     if (ctxt->token != 0) {
                    200:        *len = 0;
                    201:        return(ctxt->token);
                    202:     }  
                    203:     if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
                    204:        /*
                    205:         * We are supposed to handle UTF8, check it's valid
                    206:         * From rfc2044: encoding of the Unicode values on UTF-8:
                    207:         *
                    208:         * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
                    209:         * 0000 0000-0000 007F   0xxxxxxx
                    210:         * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
                    211:         * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx 
                    212:         *
                    213:         * Check for the 0x110000 limit too
                    214:         */
                    215:        const unsigned char *cur = ctxt->input->cur;
                    216:        unsigned char c;
                    217:        unsigned int val;
                    218: 
                    219:        c = *cur;
                    220:        if (c & 0x80) {
                    221:            if (cur[1] == 0)
                    222:                xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    223:            if ((cur[1] & 0xc0) != 0x80)
                    224:                goto encoding_error;
                    225:            if ((c & 0xe0) == 0xe0) {
                    226: 
                    227:                if (cur[2] == 0)
                    228:                    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    229:                if ((cur[2] & 0xc0) != 0x80)
                    230:                    goto encoding_error;
                    231:                if ((c & 0xf0) == 0xf0) {
                    232:                    if (cur[3] == 0)
                    233:                        xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    234:                    if (((c & 0xf8) != 0xf0) ||
                    235:                        ((cur[3] & 0xc0) != 0x80))
                    236:                        goto encoding_error;
                    237:                    /* 4-byte code */
                    238:                    *len = 4;
                    239:                    val = (cur[0] & 0x7) << 18;
                    240:                    val |= (cur[1] & 0x3f) << 12;
                    241:                    val |= (cur[2] & 0x3f) << 6;
                    242:                    val |= cur[3] & 0x3f;
                    243:                } else {
                    244:                  /* 3-byte code */
                    245:                    *len = 3;
                    246:                    val = (cur[0] & 0xf) << 12;
                    247:                    val |= (cur[1] & 0x3f) << 6;
                    248:                    val |= cur[2] & 0x3f;
                    249:                }
                    250:            } else {
                    251:              /* 2-byte code */
                    252:                *len = 2;
                    253:                val = (cur[0] & 0x1f) << 6;
                    254:                val |= cur[1] & 0x3f;
                    255:            }
                    256:            if (!IS_CHAR(val)) {
1.67      veillard  257:                ctxt->errNo = XML_ERR_INVALID_ENCODING;
1.53      veillard  258:                if ((ctxt->sax != NULL) &&
                    259:                    (ctxt->sax->error != NULL))
                    260:                    ctxt->sax->error(ctxt->userData, 
                    261:                                     "Char 0x%X out of allowed range\n", val);
                    262:                ctxt->wellFormed = 0;
                    263:                ctxt->disableSAX = 1;
                    264:            }    
                    265:            return(val);
                    266:        } else {
                    267:            /* 1-byte code */
                    268:            *len = 1;
                    269:            return((int) *ctxt->input->cur);
                    270:        }
                    271:     }
                    272:     /*
                    273:      * Assume it's a fixed lenght encoding (1) with
                    274:      * a compatibke encoding for the ASCII set, since
                    275:      * XML constructs only use < 128 chars
                    276:      */
                    277:     *len = 1;
                    278:     if ((int) *ctxt->input->cur < 0x80)
                    279:        return((int) *ctxt->input->cur);
                    280: 
                    281:     /*
                    282:      * Humm this is bad, do an automatic flow conversion
                    283:      */
                    284:     xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
                    285:     ctxt->charset = XML_CHAR_ENCODING_UTF8;
                    286:     return(xmlCurrentChar(ctxt, len));
                    287: 
                    288: encoding_error:
                    289:     /*
                    290:      * If we detect an UTF8 error that probably mean that the
                    291:      * input encoding didn't get properly advertized in the
                    292:      * declaration header. Report the error and switch the encoding
                    293:      * to ISO-Latin-1 (if you don't like this policy, just declare the
                    294:      * encoding !)
                    295:      */
1.67      veillard  296:     ctxt->errNo = XML_ERR_INVALID_ENCODING;
1.53      veillard  297:     if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
                    298:        ctxt->sax->error(ctxt->userData, 
                    299:                         "Input is not proper UTF-8, indicate encoding !\n");
                    300:        ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
                    301:                        ctxt->input->cur[0], ctxt->input->cur[1],
                    302:                        ctxt->input->cur[2], ctxt->input->cur[3]);
                    303:     }
                    304: 
                    305:     ctxt->charset = XML_CHAR_ENCODING_8859_1; 
                    306:     *len = 1;
                    307:     return((int) *ctxt->input->cur);
                    308: }
1.35      daniel    309: 
                    310: /**
                    311:  * htmlNextChar:
                    312:  * @ctxt:  the HTML parser context
                    313:  *
                    314:  * Skip to the next char input char.
                    315:  */
                    316: 
                    317: void
                    318: htmlNextChar(htmlParserCtxtPtr ctxt) {
1.44      daniel    319:     if (ctxt->instate == XML_PARSER_EOF)
                    320:        return;
1.35      daniel    321:     if ((*ctxt->input->cur == 0) &&
                    322:         (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
                    323:            xmlPopInput(ctxt);
                    324:     } else {
                    325:         if (*(ctxt->input->cur) == '\n') {
                    326:            ctxt->input->line++; ctxt->input->col = 1;
                    327:        } else ctxt->input->col++;
                    328:        ctxt->input->cur++;
                    329:        ctxt->nbChars++;
                    330:         if (*ctxt->input->cur == 0)
                    331:            xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    332:     }
                    333: }
1.5       daniel    334: 
1.36      daniel    335: /**
                    336:  * htmlSkipBlankChars:
                    337:  * @ctxt:  the HTML parser context
                    338:  *
                    339:  * skip all blanks character found at that point in the input streams.
                    340:  *
                    341:  * Returns the number of space chars skipped
                    342:  */
                    343: 
                    344: int
                    345: htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
                    346:     int res = 0;
                    347: 
                    348:     while (IS_BLANK(*(ctxt->input->cur))) {
                    349:        if ((*ctxt->input->cur == 0) &&
                    350:            (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
                    351:                xmlPopInput(ctxt);
                    352:        } else {
                    353:            if (*(ctxt->input->cur) == '\n') {
                    354:                ctxt->input->line++; ctxt->input->col = 1;
                    355:            } else ctxt->input->col++;
                    356:            ctxt->input->cur++;
                    357:            ctxt->nbChars++;
                    358:            if (*ctxt->input->cur == 0)
                    359:                xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    360:        }
                    361:        res++;
                    362:     }
                    363:     return(res);
                    364: }
1.1       daniel    365: 
                    366: 
1.5       daniel    367: 
1.1       daniel    368: /************************************************************************
                    369:  *                                                                     *
                    370:  *             The list of HTML elements and their properties          *
                    371:  *                                                                     *
                    372:  ************************************************************************/
                    373: 
                    374: /*
                    375:  *  Start Tag: 1 means the start tag can be ommited
                    376:  *  End Tag:   1 means the end tag can be ommited
                    377:  *             2 means it's forbidden (empty elements)
                    378:  *  Depr:      this element is deprecated
                    379:  *  DTD:       1 means that this element is valid only in the Loose DTD
                    380:  *             2 means that this element is valid only in the Frameset DTD
                    381:  *
                    382:  * Name,Start Tag,End Tag,  Empty,  Depr.,    DTD, Description
                    383:  */
                    384: htmlElemDesc  html40ElementTable[] = {
1.26      daniel    385: { "a",         0,      0,      0,      0,      0, "anchor " },
                    386: { "abbr",      0,      0,      0,      0,      0, "abbreviated form" },
                    387: { "acronym",   0,      0,      0,      0,      0, "" },
                    388: { "address",   0,      0,      0,      0,      0, "information on author " },
                    389: { "applet",    0,      0,      0,      1,      1, "java applet " },
                    390: { "area",      0,      2,      1,      0,      0, "client-side image map area " },
                    391: { "b",         0,      0,      0,      0,      0, "bold text style" },
                    392: { "base",      0,      2,      1,      0,      0, "document base uri " },
                    393: { "basefont",  0,      2,      1,      1,      1, "base font size " },
                    394: { "bdo",       0,      0,      0,      0,      0, "i18n bidi over-ride " },
                    395: { "big",       0,      0,      0,      0,      0, "large text style" },
                    396: { "blockquote",        0,      0,      0,      0,      0, "long quotation " },
                    397: { "body",      1,      1,      0,      0,      0, "document body " },
                    398: { "br",                0,      2,      1,      0,      0, "forced line break " },
                    399: { "button",    0,      0,      0,      0,      0, "push button " },
                    400: { "caption",   0,      0,      0,      0,      0, "table caption " },
                    401: { "center",    0,      0,      0,      1,      1, "shorthand for div align=center " },
                    402: { "cite",      0,      0,      0,      0,      0, "citation" },
                    403: { "code",      0,      0,      0,      0,      0, "computer code fragment" },
                    404: { "col",       0,      2,      1,      0,      0, "table column " },
                    405: { "colgroup",  0,      1,      0,      0,      0, "table column group " },
                    406: { "dd",                0,      1,      0,      0,      0, "definition description " },
                    407: { "del",       0,      0,      0,      0,      0, "deleted text " },
                    408: { "dfn",       0,      0,      0,      0,      0, "instance definition" },
                    409: { "dir",       0,      0,      0,      1,      1, "directory list" },
                    410: { "div",       0,      0,      0,      0,      0, "generic language/style container"},
                    411: { "dl",                0,      0,      0,      0,      0, "definition list " },
                    412: { "dt",                0,      1,      0,      0,      0, "definition term " },
                    413: { "em",                0,      0,      0,      0,      0, "emphasis" },
                    414: { "fieldset",  0,      0,      0,      0,      0, "form control group " },
                    415: { "font",      0,      0,      0,      1,      1, "local change to font " },
                    416: { "form",      0,      0,      0,      0,      0, "interactive form " },
                    417: { "frame",     0,      2,      1,      0,      2, "subwindow " },
                    418: { "frameset",  0,      0,      0,      0,      2, "window subdivision" },
                    419: { "h1",                0,      0,      0,      0,      0, "heading " },
                    420: { "h2",                0,      0,      0,      0,      0, "heading " },
                    421: { "h3",                0,      0,      0,      0,      0, "heading " },
                    422: { "h4",                0,      0,      0,      0,      0, "heading " },
                    423: { "h5",                0,      0,      0,      0,      0, "heading " },
                    424: { "h6",                0,      0,      0,      0,      0, "heading " },
                    425: { "head",      1,      1,      0,      0,      0, "document head " },
                    426: { "hr",                0,      2,      1,      0,      0, "horizontal rule " },
                    427: { "html",      1,      1,      0,      0,      0, "document root element " },
                    428: { "i",         0,      0,      0,      0,      0, "italic text style" },
                    429: { "iframe",    0,      0,      0,      0,      1, "inline subwindow " },
                    430: { "img",       0,      2,      1,      0,      0, "embedded image " },
                    431: { "input",     0,      2,      1,      0,      0, "form control " },
                    432: { "ins",       0,      0,      0,      0,      0, "inserted text" },
                    433: { "isindex",   0,      2,      1,      1,      1, "single line prompt " },
                    434: { "kbd",       0,      0,      0,      0,      0, "text to be entered by the user" },
                    435: { "label",     0,      0,      0,      0,      0, "form field label text " },
                    436: { "legend",    0,      0,      0,      0,      0, "fieldset legend " },
                    437: { "li",                0,      1,      0,      0,      0, "list item " },
                    438: { "link",      0,      2,      1,      0,      0, "a media-independent link " },
                    439: { "map",       0,      0,      0,      0,      0, "client-side image map " },
                    440: { "menu",      0,      0,      0,      1,      1, "menu list " },
                    441: { "meta",      0,      2,      1,      0,      0, "generic metainformation " },
                    442: { "noframes",  0,      0,      0,      0,      2, "alternate content container for non frame-based rendering " },
                    443: { "noscript",  0,      0,      0,      0,      0, "alternate content container for non script-based rendering " },
                    444: { "object",    0,      0,      0,      0,      0, "generic embedded object " },
                    445: { "ol",                0,      0,      0,      0,      0, "ordered list " },
                    446: { "optgroup",  0,      0,      0,      0,      0, "option group " },
                    447: { "option",    0,      1,      0,      0,      0, "selectable choice " },
                    448: { "p",         0,      1,      0,      0,      0, "paragraph " },
                    449: { "param",     0,      2,      1,      0,      0, "named property value " },
                    450: { "pre",       0,      0,      0,      0,      0, "preformatted text " },
                    451: { "q",         0,      0,      0,      0,      0, "short inline quotation " },
                    452: { "s",         0,      0,      0,      1,      1, "strike-through text style" },
                    453: { "samp",      0,      0,      0,      0,      0, "sample program output, scripts, etc." },
                    454: { "script",    0,      0,      0,      0,      0, "script statements " },
                    455: { "select",    0,      0,      0,      0,      0, "option selector " },
                    456: { "small",     0,      0,      0,      0,      0, "small text style" },
                    457: { "span",      0,      0,      0,      0,      0, "generic language/style container " },
                    458: { "strike",    0,      0,      0,      1,      1, "strike-through text" },
                    459: { "strong",    0,      0,      0,      0,      0, "strong emphasis" },
                    460: { "style",     0,      0,      0,      0,      0, "style info " },
                    461: { "sub",       0,      0,      0,      0,      0, "subscript" },
                    462: { "sup",       0,      0,      0,      0,      0, "superscript " },
                    463: { "table",     0,      0,      0,      0,      0, "&#160;" },
                    464: { "tbody",     1,      1,      0,      0,      0, "table body " },
                    465: { "td",                0,      1,      0,      0,      0, "table data cell" },
                    466: { "textarea",  0,      0,      0,      0,      0, "multi-line text field " },
                    467: { "tfoot",     0,      1,      0,      0,      0, "table footer " },
                    468: { "th",                0,      1,      0,      0,      0, "table header cell" },
                    469: { "thead",     0,      1,      0,      0,      0, "table header " },
                    470: { "title",     0,      0,      0,      0,      0, "document title " },
                    471: { "tr",                0,      1,      0,      0,      0, "table row " },
                    472: { "tt",                0,      0,      0,      0,      0, "teletype or monospaced text style" },
                    473: { "u",         0,      0,      0,      1,      1, "underlined text style" },
                    474: { "ul",                0,      0,      0,      0,      0, "unordered list " },
                    475: { "var",       0,      0,      0,      0,      0, "instance of a variable or program argument" },
1.1       daniel    476: };
                    477: 
                    478: /*
                    479:  * start tags that imply the end of a current element
                    480:  * any tag of each line implies the end of the current element if the type of
                    481:  * that element is in the same line
                    482:  */
1.8       daniel    483: char *htmlEquEnd[] = {
1.26      daniel    484: "dt", "dd", "li", "option", NULL,
                    485: "h1", "h2", "h3", "h4", "h5", "h6", NULL,
                    486: "ol", "menu", "dir", "address", "pre", "listing", "xmp", NULL,
1.1       daniel    487: NULL
                    488: };
                    489: /*
                    490:  * acording the HTML DTD, HR should be added to the 2nd line above, as it
                    491:  * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
                    492:  * because many documents contain rules in headings...
                    493:  */
                    494: 
                    495: /*
                    496:  * start tags that imply the end of current element
                    497:  */
1.8       daniel    498: char *htmlStartClose[] = {
1.26      daniel    499: "form",                "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
                    500:                "dl", "ul", "ol", "menu", "dir", "address", "pre",
                    501:                "listing", "xmp", "head", NULL,
                    502: "head",                "p", NULL,
                    503: "title",       "p", NULL,
                    504: "body",                "head", "style", "link", "title", "p", NULL,
                    505: "li",          "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
                    506:                "pre", "listing", "xmp", "head", "li", NULL,
                    507: "hr",          "p", "head", NULL,
                    508: "h1",          "p", "head", NULL,
                    509: "h2",          "p", "head", NULL,
                    510: "h3",          "p", "head", NULL,
                    511: "h4",          "p", "head", NULL,
                    512: "h5",          "p", "head", NULL,
                    513: "h6",          "p", "head", NULL,
                    514: "dir",         "p", "head", NULL,
                    515: "address",     "p", "head", "ul", NULL,
                    516: "pre",         "p", "head", "ul", NULL,
                    517: "listing",     "p", "head", NULL,
                    518: "xmp",         "p", "head", NULL,
                    519: "blockquote",  "p", "head", NULL,
                    520: "dl",          "p", "dt", "menu", "dir", "address", "pre", "listing",
                    521:                "xmp", "head", NULL,
                    522: "dt",          "p", "menu", "dir", "address", "pre", "listing", "xmp",
                    523:                 "head", "dd", NULL,
                    524: "dd",          "p", "menu", "dir", "address", "pre", "listing", "xmp",
                    525:                 "head", "dt", NULL,
                    526: "ul",          "p", "head", "ol", "menu", "dir", "address", "pre",
                    527:                "listing", "xmp", NULL,
                    528: "ol",          "p", "head", "ul", NULL,
                    529: "menu",                "p", "head", "ul", NULL,
                    530: "p",           "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
                    531: "div",         "p", "head", NULL,
                    532: "noscript",    "p", "head", NULL,
                    533: "center",      "font", "b", "i", "p", "head", NULL,
                    534: "a",           "a", NULL,
                    535: "caption",     "p", NULL,
                    536: "colgroup",    "caption", "colgroup", "col", "p", NULL,
                    537: "col",         "caption", "col", "p", NULL,
                    538: "table",       "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
                    539:                "listing", "xmp", "a", NULL,
                    540: "th",          "th", "td", NULL,
                    541: "td",          "th", "td", "p", NULL,
                    542: "tr",          "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
                    543: "thead",       "caption", "col", "colgroup", NULL,
                    544: "tfoot",       "th", "td", "tr", "caption", "col", "colgroup", "thead",
                    545:                "tbody", "p", NULL,
                    546: "tbody",       "th", "td", "tr", "caption", "col", "colgroup", "thead",
                    547:                "tfoot", "tbody", "p", NULL,
                    548: "optgroup",    "option", NULL,
                    549: "fieldset",    "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
                    550:                "pre", "listing", "xmp", "a", NULL,
1.1       daniel    551: NULL
                    552: };
                    553: 
1.59      veillard  554: /*
                    555:  * The list of HTML elements which are supposed not to have
                    556:  * CDATA content and where a p element will be implied
                    557:  *
                    558:  * TODO: extend that list by reading the HTML SGML DtD on
                    559:  *       implied paragraph
                    560:  */
                    561: static char *htmlNoContentElements[] = {
                    562:     "html",
                    563:     "head",
                    564:     "body",
                    565:     NULL
                    566: };
                    567: 
1.43      daniel    568: 
1.8       daniel    569: static char** htmlStartCloseIndex[100];
1.1       daniel    570: static int htmlStartCloseIndexinitialized = 0;
                    571: 
                    572: /************************************************************************
                    573:  *                                                                     *
                    574:  *             functions to handle HTML specific data                  *
                    575:  *                                                                     *
                    576:  ************************************************************************/
                    577: 
                    578: /**
                    579:  * htmlInitAutoClose:
                    580:  *
                    581:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1.72      veillard  582:  * This is not reentrant. Call xmlInitParser() once before processing in
                    583:  * case of use in multithreaded programs.
1.1       daniel    584:  */
                    585: void
                    586: htmlInitAutoClose(void) {
                    587:     int index, i = 0;
                    588: 
                    589:     if (htmlStartCloseIndexinitialized) return;
                    590: 
                    591:     for (index = 0;index < 100;index ++) htmlStartCloseIndex[index] = NULL;
                    592:     index = 0;
                    593:     while ((htmlStartClose[i] != NULL) && (index < 100 - 1)) {
                    594:         htmlStartCloseIndex[index++] = &htmlStartClose[i];
                    595:        while (htmlStartClose[i] != NULL) i++;
                    596:        i++;
                    597:     }
1.72      veillard  598:     htmlStartCloseIndexinitialized = 1;
1.1       daniel    599: }
                    600: 
                    601: /**
                    602:  * htmlTagLookup:
1.69      veillard  603:  * @tag:  The tag name in lowercase
1.1       daniel    604:  *
                    605:  * Lookup the HTML tag in the ElementTable
                    606:  *
                    607:  * Returns the related htmlElemDescPtr or NULL if not found.
                    608:  */
                    609: htmlElemDescPtr
1.14      daniel    610: htmlTagLookup(const xmlChar *tag) {
1.61      veillard  611:     int i;
1.1       daniel    612: 
                    613:     for (i = 0; i < (sizeof(html40ElementTable) /
                    614:                      sizeof(html40ElementTable[0]));i++) {
1.73    ! veillard  615:         if (xmlStrEqual(tag, BAD_CAST html40ElementTable[i].name))
1.1       daniel    616:            return(&html40ElementTable[i]);
                    617:     }
                    618:     return(NULL);
                    619: }
                    620: 
                    621: /**
                    622:  * htmlCheckAutoClose:
1.50      veillard  623:  * @newtag:  The new tag name
                    624:  * @oldtag:  The old tag name
1.1       daniel    625:  *
                    626:  * Checks wether the new tag is one of the registered valid tags for closing old.
                    627:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
                    628:  *
                    629:  * Returns 0 if no, 1 if yes.
                    630:  */
                    631: int
1.50      veillard  632: htmlCheckAutoClose(const xmlChar *newtag, const xmlChar *oldtag) {
1.1       daniel    633:     int i, index;
1.64      veillard  634:     char **close = NULL;
1.1       daniel    635: 
                    636:     if (htmlStartCloseIndexinitialized == 0) htmlInitAutoClose();
                    637: 
                    638:     /* inefficient, but not a big deal */
                    639:     for (index = 0; index < 100;index++) {
                    640:         close = htmlStartCloseIndex[index];
                    641:        if (close == NULL) return(0);
1.73    ! veillard  642:        if (xmlStrEqual(BAD_CAST *close, newtag)) break;
1.1       daniel    643:     }
                    644: 
                    645:     i = close - htmlStartClose;
                    646:     i++;
                    647:     while (htmlStartClose[i] != NULL) {
1.73    ! veillard  648:         if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
1.1       daniel    649:            return(1);
                    650:        }
                    651:        i++;
                    652:     }
                    653:     return(0);
                    654: }
                    655: 
                    656: /**
1.50      veillard  657:  * htmlAutoCloseOnClose:
                    658:  * @ctxt:  an HTML parser context
                    659:  * @newtag:  The new tag name
                    660:  *
                    661:  * The HTmL DtD allows an ending tag to implicitely close other tags.
                    662:  */
                    663: void
                    664: htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
                    665:     htmlElemDescPtr info;
                    666:     xmlChar *oldname;
                    667:     int i;
                    668: 
                    669: #ifdef DEBUG
                    670:     fprintf(stderr,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
                    671:     for (i = 0;i < ctxt->nameNr;i++) 
                    672:         fprintf(stderr,"%d : %s\n", i, ctxt->nameTab[i]);
                    673: #endif
                    674: 
                    675:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
1.73    ! veillard  676:         if (xmlStrEqual(newtag, ctxt->nameTab[i])) break;
1.50      veillard  677:     }
                    678:     if (i < 0) return;
                    679: 
1.73    ! veillard  680:     while (!xmlStrEqual(newtag, ctxt->name)) {
1.50      veillard  681:        info = htmlTagLookup(ctxt->name);
                    682:        if ((info == NULL) || (info->endTag == 1)) {
                    683: #ifdef DEBUG
                    684:            fprintf(stderr,"htmlAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
                    685: #endif
                    686:         } else {
                    687:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    688:                ctxt->sax->error(ctxt->userData,
                    689:                 "Opening and ending tag mismatch: %s and %s\n",
                    690:                                 newtag, ctxt->name);
                    691:            ctxt->wellFormed = 0;
                    692:        }
                    693:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                    694:            ctxt->sax->endElement(ctxt->userData, ctxt->name);
                    695:        oldname = htmlnamePop(ctxt);
                    696:        if (oldname != NULL) {
                    697: #ifdef DEBUG
                    698:            fprintf(stderr,"htmlAutoCloseOnClose: popped %s\n", oldname);
                    699: #endif
                    700:            xmlFree(oldname);
                    701:        }       
                    702:     }
                    703: }
                    704: 
                    705: /**
1.1       daniel    706:  * htmlAutoClose:
                    707:  * @ctxt:  an HTML parser context
1.50      veillard  708:  * @newtag:  The new tag name or NULL
1.1       daniel    709:  *
                    710:  * The HTmL DtD allows a tag to implicitely close other tags.
                    711:  * The list is kept in htmlStartClose array. This function is
                    712:  * called when a new tag has been detected and generates the
                    713:  * appropriates closes if possible/needed.
1.50      veillard  714:  * If newtag is NULL this mean we are at the end of the resource
1.47      daniel    715:  * and we should check 
1.1       daniel    716:  */
                    717: void
1.50      veillard  718: htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1.15      daniel    719:     xmlChar *oldname;
1.50      veillard  720:     while ((newtag != NULL) && (ctxt->name != NULL) && 
                    721:            (htmlCheckAutoClose(newtag, ctxt->name))) {
1.1       daniel    722: #ifdef DEBUG
1.50      veillard  723:        fprintf(stderr,"htmlAutoClose: %s closes %s\n", newtag, ctxt->name);
1.1       daniel    724: #endif
                    725:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1.15      daniel    726:            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1.24      daniel    727:        oldname = htmlnamePop(ctxt);
1.18      daniel    728:        if (oldname != NULL) {
                    729: #ifdef DEBUG
                    730:            fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
                    731: #endif
1.17      daniel    732:            xmlFree(oldname);
1.18      daniel    733:         }
1.1       daniel    734:     }
1.50      veillard  735:     if (newtag == NULL) {
1.49      daniel    736:        htmlAutoCloseOnClose(ctxt, BAD_CAST"head");
                    737:        htmlAutoCloseOnClose(ctxt, BAD_CAST"body");
                    738:        htmlAutoCloseOnClose(ctxt, BAD_CAST"html");
                    739:     }
1.50      veillard  740:     while ((newtag == NULL) && (ctxt->name != NULL) &&
1.73    ! veillard  741:           ((xmlStrEqual(ctxt->name, BAD_CAST"head")) ||
        !           742:            (xmlStrEqual(ctxt->name, BAD_CAST"body")) ||
        !           743:            (xmlStrEqual(ctxt->name, BAD_CAST"html")))) {
1.47      daniel    744: #ifdef DEBUG
                    745:        fprintf(stderr,"htmlAutoClose: EOF closes %s\n", ctxt->name);
                    746: #endif
                    747:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                    748:            ctxt->sax->endElement(ctxt->userData, ctxt->name);
                    749:        oldname = htmlnamePop(ctxt);
                    750:        if (oldname != NULL) {
                    751: #ifdef DEBUG
                    752:            fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
                    753: #endif
                    754:            xmlFree(oldname);
                    755:         }
                    756:    }
                    757: 
1.1       daniel    758: }
                    759: 
                    760: /**
1.28      daniel    761:  * htmlAutoCloseTag:
                    762:  * @doc:  the HTML document
                    763:  * @name:  The tag name
                    764:  * @elem:  the HTML element
                    765:  *
                    766:  * The HTmL DtD allows a tag to implicitely close other tags.
                    767:  * The list is kept in htmlStartClose array. This function checks
                    768:  * if the element or one of it's children would autoclose the
                    769:  * given tag.
                    770:  *
                    771:  * Returns 1 if autoclose, 0 otherwise
                    772:  */
                    773: int
                    774: htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
                    775:     htmlNodePtr child;
                    776: 
                    777:     if (elem == NULL) return(1);
1.73    ! veillard  778:     if (xmlStrEqual(name, elem->name)) return(0);
1.28      daniel    779:     if (htmlCheckAutoClose(elem->name, name)) return(1);
1.37      daniel    780:     child = elem->children;
1.28      daniel    781:     while (child != NULL) {
                    782:         if (htmlAutoCloseTag(doc, name, child)) return(1);
                    783:        child = child->next;
                    784:     }
                    785:     return(0);
                    786: }
                    787: 
                    788: /**
                    789:  * htmlIsAutoClosed:
                    790:  * @doc:  the HTML document
                    791:  * @elem:  the HTML element
                    792:  *
                    793:  * The HTmL DtD allows a tag to implicitely close other tags.
                    794:  * The list is kept in htmlStartClose array. This function checks
                    795:  * if a tag is autoclosed by one of it's child
                    796:  *
                    797:  * Returns 1 if autoclosed, 0 otherwise
                    798:  */
                    799: int
                    800: htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
                    801:     htmlNodePtr child;
                    802: 
                    803:     if (elem == NULL) return(1);
1.37      daniel    804:     child = elem->children;
1.28      daniel    805:     while (child != NULL) {
                    806:        if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
                    807:        child = child->next;
                    808:     }
                    809:     return(0);
                    810: }
                    811: 
                    812: /**
1.43      daniel    813:  * htmlCheckImplied:
                    814:  * @ctxt:  an HTML parser context
1.50      veillard  815:  * @newtag:  The new tag name
1.43      daniel    816:  *
                    817:  * The HTmL DtD allows a tag to exists only implicitely
                    818:  * called when a new tag has been detected and generates the
                    819:  * appropriates implicit tags if missing
                    820:  */
                    821: void
1.50      veillard  822: htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1.73    ! veillard  823:     if (xmlStrEqual(newtag, BAD_CAST"html"))
1.43      daniel    824:        return;
                    825:     if (ctxt->nameNr <= 0) {
                    826: #ifdef DEBUG
                    827:        fprintf(stderr,"Implied element html: pushed html\n");
                    828: #endif    
                    829:        htmlnamePush(ctxt, xmlStrdup(BAD_CAST"html"));
                    830:        if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    831:            ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
                    832:     }
1.73    ! veillard  833:     if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1.43      daniel    834:         return;
                    835:     if (ctxt->nameNr <= 1) {
1.73    ! veillard  836:        if ((xmlStrEqual(newtag, BAD_CAST"script")) ||
        !           837:            (xmlStrEqual(newtag, BAD_CAST"style")) ||
        !           838:            (xmlStrEqual(newtag, BAD_CAST"meta")) ||
        !           839:            (xmlStrEqual(newtag, BAD_CAST"link")) ||
        !           840:            (xmlStrEqual(newtag, BAD_CAST"title")) ||
        !           841:            (xmlStrEqual(newtag, BAD_CAST"base"))) {
1.43      daniel    842:            /* 
                    843:             * dropped OBJECT ... i you put it first BODY will be
                    844:             * assumed !
                    845:             */
                    846: #ifdef DEBUG
                    847:            fprintf(stderr,"Implied element head: pushed head\n");
                    848: #endif    
                    849:            htmlnamePush(ctxt, xmlStrdup(BAD_CAST"head"));
                    850:            if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    851:                ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
                    852:        } else {
                    853: #ifdef DEBUG
                    854:            fprintf(stderr,"Implied element body: pushed body\n");
                    855: #endif    
                    856:            htmlnamePush(ctxt, xmlStrdup(BAD_CAST"body"));
                    857:            if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    858:                ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
                    859:        }
                    860:     }
                    861: }
                    862: 
1.59      veillard  863: /**
                    864:  * htmlCheckParagraph
                    865:  * @ctxt:  an HTML parser context
                    866:  *
                    867:  * Check whether a p element need to be implied before inserting
                    868:  * characters in the current element.
                    869:  *
                    870:  * Returns 1 if a paragraph has been inserted, 0 if not and -1
                    871:  *         in case of error.
                    872:  */
                    873: 
                    874: int
                    875: htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
                    876:     const xmlChar *tag;
                    877:     int i;
                    878: 
                    879:     if (ctxt == NULL)
                    880:        return(-1);
                    881:     tag = ctxt->name;
                    882:     if (tag == NULL) {
                    883:        htmlAutoClose(ctxt, BAD_CAST"p");
                    884:        htmlCheckImplied(ctxt, BAD_CAST"p");
                    885:        htmlnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
                    886:        if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    887:            ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
                    888:        return(1);
                    889:     }
                    890:     for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1.73    ! veillard  891:        if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
1.59      veillard  892: #ifdef DEBUG
                    893:            fprintf(stderr,"Implied element paragraph\n");
                    894: #endif    
                    895:            htmlAutoClose(ctxt, BAD_CAST"p");
                    896:            htmlCheckImplied(ctxt, BAD_CAST"p");
                    897:            htmlnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
                    898:            if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    899:                ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
                    900:            return(1);
                    901:        }
                    902:     }
                    903:     return(0);
                    904: }
                    905: 
1.1       daniel    906: /************************************************************************
                    907:  *                                                                     *
                    908:  *             The list of HTML predefined entities                    *
                    909:  *                                                                     *
                    910:  ************************************************************************/
                    911: 
                    912: 
                    913: htmlEntityDesc  html40EntitiesTable[] = {
                    914: /*
1.61      veillard  915:  * the 4 absolute ones, plus apostrophe.
1.1       daniel    916:  */
                    917: { 34,  "quot", "quotation mark = APL quote, U+0022 ISOnum" },
                    918: { 38,  "amp",  "ampersand, U+0026 ISOnum" },
1.61      veillard  919: { 39,  "apos", "single quote" },
1.1       daniel    920: { 60,  "lt",   "less-than sign, U+003C ISOnum" },
                    921: { 62,  "gt",   "greater-than sign, U+003E ISOnum" },
                    922: 
                    923: /*
                    924:  * A bunch still in the 128-255 range
                    925:  * Replacing them depend really on the charset used.
                    926:  */
                    927: { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
                    928: { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
                    929: { 162, "cent", "cent sign, U+00A2 ISOnum" },
                    930: { 163, "pound","pound sign, U+00A3 ISOnum" },
                    931: { 164, "curren","currency sign, U+00A4 ISOnum" },
                    932: { 165, "yen",  "yen sign = yuan sign, U+00A5 ISOnum" },
                    933: { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
                    934: { 167, "sect", "section sign, U+00A7 ISOnum" },
                    935: { 168, "uml",  "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
                    936: { 169, "copy", "copyright sign, U+00A9 ISOnum" },
                    937: { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
                    938: { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
                    939: { 172, "not",  "not sign, U+00AC ISOnum" },
                    940: { 173, "shy",  "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
                    941: { 174, "reg",  "registered sign = registered trade mark sign, U+00AE ISOnum" },
                    942: { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
                    943: { 176, "deg",  "degree sign, U+00B0 ISOnum" },
                    944: { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
                    945: { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
                    946: { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
                    947: { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
                    948: { 181, "micro","micro sign, U+00B5 ISOnum" },
                    949: { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1.7       daniel    950: { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1.1       daniel    951: { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
                    952: { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
                    953: { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1.7       daniel    954: { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1.1       daniel    955: { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
                    956: { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
                    957: { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
                    958: { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
                    959: { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
                    960: { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
                    961: { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
                    962: { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
                    963: { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
                    964: { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
                    965: { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
                    966: { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
                    967: { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
                    968: { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
                    969: { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
                    970: { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
                    971: { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
                    972: { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
                    973: { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
                    974: { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
                    975: { 208, "ETH",  "latin capital letter ETH, U+00D0 ISOlat1" },
                    976: { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
                    977: { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
                    978: { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
                    979: { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
                    980: { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
                    981: { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
                    982: { 215, "times","multiplication sign, U+00D7 ISOnum" },
1.7       daniel    983: { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1.1       daniel    984: { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
                    985: { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
                    986: { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
                    987: { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
                    988: { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
                    989: { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
                    990: { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
                    991: { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
                    992: { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
                    993: { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
                    994: { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
                    995: { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
                    996: { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
                    997: { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
                    998: { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
                    999: { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
                   1000: { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
                   1001: { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
                   1002: { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
                   1003: { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
                   1004: { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
                   1005: { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
                   1006: { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
                   1007: { 240, "eth",  "latin small letter eth, U+00F0 ISOlat1" },
                   1008: { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
                   1009: { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
                   1010: { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
                   1011: { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
                   1012: { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
                   1013: { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
                   1014: { 247, "divide","division sign, U+00F7 ISOnum" },
                   1015: { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
                   1016: { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
                   1017: { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
                   1018: { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
                   1019: { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
                   1020: { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
                   1021: { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
                   1022: { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
                   1023: 
1.61      veillard 1024: { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
                   1025: { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
                   1026: { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
                   1027: { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
                   1028: { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
                   1029: 
1.1       daniel   1030: /*
                   1031:  * Anything below should really be kept as entities references
                   1032:  */
                   1033: { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
                   1034: 
1.61      veillard 1035: { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
                   1036: { 732, "tilde","small tilde, U+02DC ISOdia" },
                   1037: 
1.1       daniel   1038: { 913, "Alpha","greek capital letter alpha, U+0391" },
                   1039: { 914, "Beta", "greek capital letter beta, U+0392" },
                   1040: { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
                   1041: { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
                   1042: { 917, "Epsilon","greek capital letter epsilon, U+0395" },
                   1043: { 918, "Zeta", "greek capital letter zeta, U+0396" },
                   1044: { 919, "Eta",  "greek capital letter eta, U+0397" },
                   1045: { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
                   1046: { 921, "Iota", "greek capital letter iota, U+0399" },
                   1047: { 922, "Kappa","greek capital letter kappa, U+039A" },
                   1048: { 923, "Lambda""greek capital letter lambda, U+039B ISOgrk3" },
                   1049: { 924, "Mu",   "greek capital letter mu, U+039C" },
                   1050: { 925, "Nu",   "greek capital letter nu, U+039D" },
                   1051: { 926, "Xi",   "greek capital letter xi, U+039E ISOgrk3" },
                   1052: { 927, "Omicron","greek capital letter omicron, U+039F" },
                   1053: { 928, "Pi",   "greek capital letter pi, U+03A0 ISOgrk3" },
                   1054: { 929, "Rho",  "greek capital letter rho, U+03A1" },
                   1055: { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
                   1056: { 932, "Tau",  "greek capital letter tau, U+03A4" },
                   1057: { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
                   1058: { 934, "Phi",  "greek capital letter phi, U+03A6 ISOgrk3" },
                   1059: { 935, "Chi",  "greek capital letter chi, U+03A7" },
                   1060: { 936, "Psi",  "greek capital letter psi, U+03A8 ISOgrk3" },
                   1061: { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
                   1062: 
                   1063: { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
                   1064: { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
                   1065: { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
                   1066: { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
                   1067: { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
                   1068: { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
                   1069: { 951, "eta",  "greek small letter eta, U+03B7 ISOgrk3" },
                   1070: { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
                   1071: { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
                   1072: { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
                   1073: { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
                   1074: { 956, "mu",   "greek small letter mu, U+03BC ISOgrk3" },
                   1075: { 957, "nu",   "greek small letter nu, U+03BD ISOgrk3" },
                   1076: { 958, "xi",   "greek small letter xi, U+03BE ISOgrk3" },
                   1077: { 959, "omicron","greek small letter omicron, U+03BF NEW" },
                   1078: { 960, "pi",   "greek small letter pi, U+03C0 ISOgrk3" },
                   1079: { 961, "rho",  "greek small letter rho, U+03C1 ISOgrk3" },
                   1080: { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
                   1081: { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
                   1082: { 964, "tau",  "greek small letter tau, U+03C4 ISOgrk3" },
                   1083: { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
                   1084: { 966, "phi",  "greek small letter phi, U+03C6 ISOgrk3" },
                   1085: { 967, "chi",  "greek small letter chi, U+03C7 ISOgrk3" },
                   1086: { 968, "psi",  "greek small letter psi, U+03C8 ISOgrk3" },
                   1087: { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
                   1088: { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
                   1089: { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
                   1090: { 982, "piv",  "greek pi symbol, U+03D6 ISOgrk3" },
                   1091: 
1.61      veillard 1092: { 8194,        "ensp", "en space, U+2002 ISOpub" },
                   1093: { 8195,        "emsp", "em space, U+2003 ISOpub" },
                   1094: { 8201,        "thinsp","thin space, U+2009 ISOpub" },
                   1095: { 8204,        "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
                   1096: { 8205,        "zwj",  "zero width joiner, U+200D NEW RFC 2070" },
                   1097: { 8206,        "lrm",  "left-to-right mark, U+200E NEW RFC 2070" },
                   1098: { 8207,        "rlm",  "right-to-left mark, U+200F NEW RFC 2070" },
                   1099: { 8211,        "ndash","en dash, U+2013 ISOpub" },
                   1100: { 8212,        "mdash","em dash, U+2014 ISOpub" },
                   1101: { 8216,        "lsquo","left single quotation mark, U+2018 ISOnum" },
                   1102: { 8217,        "rsquo","right single quotation mark, U+2019 ISOnum" },
                   1103: { 8218,        "sbquo","single low-9 quotation mark, U+201A NEW" },
                   1104: { 8220,        "ldquo","left double quotation mark, U+201C ISOnum" },
                   1105: { 8221,        "rdquo","right double quotation mark, U+201D ISOnum" },
                   1106: { 8222,        "bdquo","double low-9 quotation mark, U+201E NEW" },
                   1107: { 8224,        "dagger","dagger, U+2020 ISOpub" },
                   1108: { 8225,        "Dagger","double dagger, U+2021 ISOpub" },
                   1109: 
1.1       daniel   1110: { 8226,        "bull", "bullet = black small circle, U+2022 ISOpub" },
                   1111: { 8230,        "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1.61      veillard 1112: 
                   1113: { 8240,        "permil","per mille sign, U+2030 ISOtech" },
                   1114: 
1.1       daniel   1115: { 8242,        "prime","prime = minutes = feet, U+2032 ISOtech" },
                   1116: { 8243,        "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1.61      veillard 1117: 
                   1118: { 8249,        "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
                   1119: { 8250,        "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
                   1120: 
1.1       daniel   1121: { 8254,        "oline","overline = spacing overscore, U+203E NEW" },
                   1122: { 8260,        "frasl","fraction slash, U+2044 NEW" },
                   1123: 
1.61      veillard 1124: { 8364,        "euro", "euro sign, U+20AC NEW" },
                   1125: 
                   1126: { 8465,        "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1.7       daniel   1127: { 8472,        "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1.1       daniel   1128: { 8476,        "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
                   1129: { 8482,        "trade","trade mark sign, U+2122 ISOnum" },
                   1130: { 8501,        "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
                   1131: { 8592,        "larr", "leftwards arrow, U+2190 ISOnum" },
                   1132: { 8593,        "uarr", "upwards arrow, U+2191 ISOnum" },
                   1133: { 8594,        "rarr", "rightwards arrow, U+2192 ISOnum" },
                   1134: { 8595,        "darr", "downwards arrow, U+2193 ISOnum" },
                   1135: { 8596,        "harr", "left right arrow, U+2194 ISOamsa" },
                   1136: { 8629,        "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
                   1137: { 8656,        "lArr", "leftwards double arrow, U+21D0 ISOtech" },
                   1138: { 8657,        "uArr", "upwards double arrow, U+21D1 ISOamsa" },
                   1139: { 8658,        "rArr", "rightwards double arrow, U+21D2 ISOtech" },
                   1140: { 8659,        "dArr", "downwards double arrow, U+21D3 ISOamsa" },
                   1141: { 8660,        "hArr", "left right double arrow, U+21D4 ISOamsa" },
                   1142: 
                   1143: { 8704,        "forall","for all, U+2200 ISOtech" },
                   1144: { 8706,        "part", "partial differential, U+2202 ISOtech" },
                   1145: { 8707,        "exist","there exists, U+2203 ISOtech" },
                   1146: { 8709,        "empty","empty set = null set = diameter, U+2205 ISOamso" },
                   1147: { 8711,        "nabla","nabla = backward difference, U+2207 ISOtech" },
                   1148: { 8712,        "isin", "element of, U+2208 ISOtech" },
                   1149: { 8713,        "notin","not an element of, U+2209 ISOtech" },
                   1150: { 8715,        "ni",   "contains as member, U+220B ISOtech" },
                   1151: { 8719,        "prod", "n-ary product = product sign, U+220F ISOamsb" },
                   1152: { 8721,        "sum",  "n-ary sumation, U+2211 ISOamsb" },
                   1153: { 8722,        "minus","minus sign, U+2212 ISOtech" },
                   1154: { 8727,        "lowast","asterisk operator, U+2217 ISOtech" },
                   1155: { 8730,        "radic","square root = radical sign, U+221A ISOtech" },
                   1156: { 8733,        "prop", "proportional to, U+221D ISOtech" },
                   1157: { 8734,        "infin","infinity, U+221E ISOtech" },
                   1158: { 8736,        "ang",  "angle, U+2220 ISOamso" },
                   1159: { 8743,        "and",  "logical and = wedge, U+2227 ISOtech" },
                   1160: { 8744,        "or",   "logical or = vee, U+2228 ISOtech" },
                   1161: { 8745,        "cap",  "intersection = cap, U+2229 ISOtech" },
                   1162: { 8746,        "cup",  "union = cup, U+222A ISOtech" },
                   1163: { 8747,        "int",  "integral, U+222B ISOtech" },
                   1164: { 8756,        "there4","therefore, U+2234 ISOtech" },
                   1165: { 8764,        "sim",  "tilde operator = varies with = similar to, U+223C ISOtech" },
                   1166: { 8773,        "cong", "approximately equal to, U+2245 ISOtech" },
                   1167: { 8776,        "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
                   1168: { 8800,        "ne",   "not equal to, U+2260 ISOtech" },
                   1169: { 8801,        "equiv","identical to, U+2261 ISOtech" },
                   1170: { 8804,        "le",   "less-than or equal to, U+2264 ISOtech" },
                   1171: { 8805,        "ge",   "greater-than or equal to, U+2265 ISOtech" },
                   1172: { 8834,        "sub",  "subset of, U+2282 ISOtech" },
                   1173: { 8835,        "sup",  "superset of, U+2283 ISOtech" },
                   1174: { 8836,        "nsub", "not a subset of, U+2284 ISOamsn" },
                   1175: { 8838,        "sube", "subset of or equal to, U+2286 ISOtech" },
                   1176: { 8839,        "supe", "superset of or equal to, U+2287 ISOtech" },
                   1177: { 8853,        "oplus","circled plus = direct sum, U+2295 ISOamsb" },
                   1178: { 8855,        "otimes","circled times = vector product, U+2297 ISOamsb" },
                   1179: { 8869,        "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
                   1180: { 8901,        "sdot", "dot operator, U+22C5 ISOamsb" },
                   1181: { 8968,        "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
                   1182: { 8969,        "rceil","right ceiling, U+2309 ISOamsc" },
                   1183: { 8970,        "lfloor","left floor = apl downstile, U+230A ISOamsc" },
                   1184: { 8971,        "rfloor","right floor, U+230B ISOamsc" },
                   1185: { 9001,        "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
                   1186: { 9002,        "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
                   1187: { 9674,        "loz",  "lozenge, U+25CA ISOpub" },
                   1188: 
                   1189: { 9824,        "spades","black spade suit, U+2660 ISOpub" },
                   1190: { 9827,        "clubs","black club suit = shamrock, U+2663 ISOpub" },
                   1191: { 9829,        "hearts","black heart suit = valentine, U+2665 ISOpub" },
                   1192: { 9830,        "diams","black diamond suit, U+2666 ISOpub" },
                   1193: 
                   1194: };
                   1195: 
                   1196: /************************************************************************
                   1197:  *                                                                     *
                   1198:  *             Commodity functions to handle entities                  *
                   1199:  *                                                                     *
                   1200:  ************************************************************************/
                   1201: 
                   1202: /*
                   1203:  * Macro used to grow the current buffer.
                   1204:  */
                   1205: #define growBuffer(buffer) {                                           \
                   1206:     buffer##_size *= 2;                                                        \
1.14      daniel   1207:     buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar));  \
1.1       daniel   1208:     if (buffer == NULL) {                                              \
                   1209:        perror("realloc failed");                                       \
1.33      daniel   1210:        return(NULL);                                                   \
1.1       daniel   1211:     }                                                                  \
                   1212: }
                   1213: 
                   1214: /**
                   1215:  * htmlEntityLookup:
                   1216:  * @name: the entity name
                   1217:  *
                   1218:  * Lookup the given entity in EntitiesTable
                   1219:  *
                   1220:  * TODO: the linear scan is really ugly, an hash table is really needed.
                   1221:  *
                   1222:  * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
                   1223:  */
                   1224: htmlEntityDescPtr
1.14      daniel   1225: htmlEntityLookup(const xmlChar *name) {
1.1       daniel   1226:     int i;
                   1227: 
                   1228:     for (i = 0;i < (sizeof(html40EntitiesTable)/
                   1229:                     sizeof(html40EntitiesTable[0]));i++) {
1.73    ! veillard 1230:         if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
1.1       daniel   1231: #ifdef DEBUG
1.18      daniel   1232:             fprintf(stderr,"Found entity %s\n", name);
1.1       daniel   1233: #endif
                   1234:             return(&html40EntitiesTable[i]);
                   1235:        }
                   1236:     }
                   1237:     return(NULL);
                   1238: }
                   1239: 
1.53      veillard 1240: /**
1.61      veillard 1241:  * htmlEntityValueLookup:
                   1242:  * @value: the entity's unicode value
                   1243:  *
                   1244:  * Lookup the given entity in EntitiesTable
                   1245:  *
                   1246:  * TODO: the linear scan is really ugly, an hash table is really needed.
                   1247:  *
                   1248:  * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
                   1249:  */
                   1250: htmlEntityDescPtr
                   1251: htmlEntityValueLookup(int value) {
                   1252:     int i;
                   1253: #ifdef DEBUG
                   1254:     int lv = 0;
                   1255: #endif
                   1256: 
                   1257:     for (i = 0;i < (sizeof(html40EntitiesTable)/
                   1258:                     sizeof(html40EntitiesTable[0]));i++) {
1.71      veillard 1259:         if ((unsigned int) html40EntitiesTable[i].value >= value) {
                   1260:            if ((unsigned int) html40EntitiesTable[i].value > value)
1.61      veillard 1261:                break;
                   1262: #ifdef DEBUG
                   1263:            fprintf(stderr,"Found entity %s\n", html40EntitiesTable[i].name);
                   1264: #endif
                   1265:             return(&html40EntitiesTable[i]);
                   1266:        }
                   1267: #ifdef DEBUG
                   1268:        if (lv > html40EntitiesTable[i].value) {
                   1269:            fprintf(stderr, "html40EntitiesTable[] is not sorted (%d > %d)!\n",
                   1270:                    lv, html40EntitiesTable[i].value);
                   1271:        }
                   1272:        lv = html40EntitiesTable[i].value;
                   1273: #endif
                   1274:     }
                   1275:     return(NULL);
                   1276: }
                   1277: 
                   1278: /**
1.53      veillard 1279:  * UTF8ToHtml:
                   1280:  * @out:  a pointer to an array of bytes to store the result
                   1281:  * @outlen:  the length of @out
                   1282:  * @in:  a pointer to an array of UTF-8 chars
                   1283:  * @inlen:  the length of @in
                   1284:  *
                   1285:  * Take a block of UTF-8 chars in and try to convert it to an ASCII
                   1286:  * plus HTML entities block of chars out.
                   1287:  *
                   1288:  * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
                   1289:  * The value of @inlen after return is the number of octets consumed
                   1290:  *     as the return value is positive, else unpredictiable.
                   1291:  * The value of @outlen after return is the number of octets consumed.
                   1292:  */
                   1293: int
                   1294: UTF8ToHtml(unsigned char* out, int *outlen,
                   1295:               const unsigned char* in, int *inlen) {
                   1296:     const unsigned char* processed = in;
                   1297:     const unsigned char* outend;
                   1298:     const unsigned char* outstart = out;
                   1299:     const unsigned char* instart = in;
                   1300:     const unsigned char* inend;
                   1301:     unsigned int c, d;
                   1302:     int trailing;
                   1303: 
                   1304:     if (in == NULL) {
                   1305:         /*
                   1306:         * initialization nothing to do
                   1307:         */
                   1308:        *outlen = 0;
                   1309:        *inlen = 0;
                   1310:        return(0);
                   1311:     }
                   1312:     inend = in + (*inlen);
                   1313:     outend = out + (*outlen);
                   1314:     while (in < inend) {
                   1315:        d = *in++;
                   1316:        if      (d < 0x80)  { c= d; trailing= 0; }
                   1317:        else if (d < 0xC0) {
                   1318:            /* trailing byte in leading position */
                   1319:            *outlen = out - outstart;
                   1320:            *inlen = processed - instart;
                   1321:            return(-2);
                   1322:         } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
                   1323:         else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
                   1324:         else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
                   1325:        else {
                   1326:            /* no chance for this in Ascii */
                   1327:            *outlen = out - outstart;
                   1328:            *inlen = processed - instart;
                   1329:            return(-2);
                   1330:        }
                   1331: 
                   1332:        if (inend - in < trailing) {
                   1333:            break;
                   1334:        } 
                   1335: 
                   1336:        for ( ; trailing; trailing--) {
                   1337:            if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
                   1338:                break;
                   1339:            c <<= 6;
                   1340:            c |= d & 0x3F;
                   1341:        }
                   1342: 
                   1343:        /* assertion: c is a single UTF-4 value */
                   1344:        if (c < 0x80) {
1.62      veillard 1345:            if (out + 1 >= outend)
1.53      veillard 1346:                break;
                   1347:            *out++ = c;
                   1348:        } else {
1.61      veillard 1349:            int len;
                   1350:            htmlEntityDescPtr ent;
                   1351: 
1.53      veillard 1352:            /*
                   1353:             * Try to lookup a predefined HTML entity for it
                   1354:             */
                   1355: 
1.61      veillard 1356:            ent = htmlEntityValueLookup(c);
                   1357:            if (ent == NULL) {
                   1358:                /* no chance for this in Ascii */
                   1359:                *outlen = out - outstart;
                   1360:                *inlen = processed - instart;
                   1361:                return(-2);
1.53      veillard 1362:            }
1.61      veillard 1363:            len = strlen(ent->name);
1.62      veillard 1364:            if (out + 2 + len >= outend)
1.53      veillard 1365:                break;
                   1366:            *out++ = '&';
1.61      veillard 1367:            memcpy(out, ent->name, len);
                   1368:            out += len;
1.53      veillard 1369:            *out++ = ';';
                   1370:        }
                   1371:        processed = in;
                   1372:     }
                   1373:     *outlen = out - outstart;
                   1374:     *inlen = processed - instart;
                   1375:     return(0);
                   1376: }
                   1377: 
1.62      veillard 1378: /**
                   1379:  * htmlEncodeEntities:
                   1380:  * @out:  a pointer to an array of bytes to store the result
                   1381:  * @outlen:  the length of @out
                   1382:  * @in:  a pointer to an array of UTF-8 chars
                   1383:  * @inlen:  the length of @in
                   1384:  * @quoteChar: the quote character to escape (' or ") or zero.
                   1385:  *
                   1386:  * Take a block of UTF-8 chars in and try to convert it to an ASCII
                   1387:  * plus HTML entities block of chars out.
                   1388:  *
                   1389:  * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
                   1390:  * The value of @inlen after return is the number of octets consumed
                   1391:  *     as the return value is positive, else unpredictiable.
                   1392:  * The value of @outlen after return is the number of octets consumed.
                   1393:  */
                   1394: int
                   1395: htmlEncodeEntities(unsigned char* out, int *outlen,
                   1396:                   const unsigned char* in, int *inlen, int quoteChar) {
                   1397:     const unsigned char* processed = in;
                   1398:     const unsigned char* outend = out + (*outlen);
                   1399:     const unsigned char* outstart = out;
                   1400:     const unsigned char* instart = in;
                   1401:     const unsigned char* inend = in + (*inlen);
                   1402:     unsigned int c, d;
                   1403:     int trailing;
                   1404: 
                   1405:     while (in < inend) {
                   1406:        d = *in++;
                   1407:        if      (d < 0x80)  { c= d; trailing= 0; }
                   1408:        else if (d < 0xC0) {
                   1409:            /* trailing byte in leading position */
                   1410:            *outlen = out - outstart;
                   1411:            *inlen = processed - instart;
                   1412:            return(-2);
                   1413:         } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
                   1414:         else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
                   1415:         else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
                   1416:        else {
                   1417:            /* no chance for this in Ascii */
                   1418:            *outlen = out - outstart;
                   1419:            *inlen = processed - instart;
                   1420:            return(-2);
                   1421:        }
                   1422: 
                   1423:        if (inend - in < trailing)
                   1424:            break;
                   1425: 
                   1426:        while (trailing--) {
                   1427:            if (((d= *in++) & 0xC0) != 0x80) {
                   1428:                *outlen = out - outstart;
                   1429:                *inlen = processed - instart;
                   1430:                return(-2);
                   1431:            }
                   1432:            c <<= 6;
                   1433:            c |= d & 0x3F;
                   1434:        }
                   1435: 
                   1436:        /* assertion: c is a single UTF-4 value */
                   1437:        if (c < 0x80 && c != quoteChar && c != '&' && c != '<' && c != '>') {
                   1438:            if (out >= outend)
                   1439:                break;
                   1440:            *out++ = c;
                   1441:        } else {
                   1442:            htmlEntityDescPtr ent;
                   1443:            const char *cp;
                   1444:            char nbuf[16];
                   1445:            int len;
                   1446: 
                   1447:            /*
                   1448:             * Try to lookup a predefined HTML entity for it
                   1449:             */
                   1450:            ent = htmlEntityValueLookup(c);
                   1451:            if (ent == NULL) {
                   1452:                sprintf(nbuf, "#%u", c);
                   1453:                cp = nbuf;
                   1454:            }
                   1455:            else
                   1456:                cp = ent->name;
                   1457:            len = strlen(cp);
                   1458:            if (out + 2 + len > outend)
                   1459:                break;
                   1460:            *out++ = '&';
                   1461:            memcpy(out, cp, len);
                   1462:            out += len;
                   1463:            *out++ = ';';
                   1464:        }
                   1465:        processed = in;
                   1466:     }
                   1467:     *outlen = out - outstart;
                   1468:     *inlen = processed - instart;
                   1469:     return(0);
                   1470: }
1.1       daniel   1471: 
                   1472: /**
                   1473:  * htmlDecodeEntities:
                   1474:  * @ctxt:  the parser context
                   1475:  * @len:  the len to decode (in bytes !), -1 for no size limit
1.14      daniel   1476:  * @end:  an end marker xmlChar, 0 if none
                   1477:  * @end2:  an end marker xmlChar, 0 if none
                   1478:  * @end3:  an end marker xmlChar, 0 if none
1.1       daniel   1479:  *
                   1480:  * Subtitute the HTML entities by their value
                   1481:  *
1.19      daniel   1482:  * DEPRECATED !!!!
1.1       daniel   1483:  *
                   1484:  * Returns A newly allocated string with the substitution done. The caller
                   1485:  *      must deallocate it !
                   1486:  */
1.14      daniel   1487: xmlChar *
1.1       daniel   1488: htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
1.14      daniel   1489:                   xmlChar end, xmlChar  end2, xmlChar end3) {
1.53      veillard 1490:     xmlChar *name = NULL;
1.14      daniel   1491:     xmlChar *buffer = NULL;
1.53      veillard 1492:     unsigned int buffer_size = 0;
                   1493:     unsigned int nbchars = 0;
1.1       daniel   1494:     htmlEntityDescPtr ent;
                   1495:     unsigned int max = (unsigned int) len;
1.53      veillard 1496:     int c,l;
                   1497: 
                   1498:     if (ctxt->depth > 40) {
1.67      veillard 1499:        ctxt->errNo = XML_ERR_ENTITY_LOOP;
1.53      veillard 1500:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1501:            ctxt->sax->error(ctxt->userData,
                   1502:                "Detected entity reference loop\n");
                   1503:        ctxt->wellFormed = 0;
                   1504:        ctxt->disableSAX = 1;
                   1505:        return(NULL);
                   1506:     }
1.1       daniel   1507: 
                   1508:     /*
                   1509:      * allocate a translation buffer.
                   1510:      */
1.31      daniel   1511:     buffer_size = HTML_PARSER_BIG_BUFFER_SIZE;
1.14      daniel   1512:     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
1.1       daniel   1513:     if (buffer == NULL) {
1.53      veillard 1514:        perror("xmlDecodeEntities: malloc failed");
1.1       daniel   1515:        return(NULL);
                   1516:     }
                   1517: 
                   1518:     /*
                   1519:      * Ok loop until we reach one of the ending char or a size limit.
                   1520:      */
1.53      veillard 1521:     c = CUR_CHAR(l);
                   1522:     while ((nbchars < max) && (c != end) &&
                   1523:            (c != end2) && (c != end3)) {
                   1524: 
                   1525:        if (c == 0) break;
                   1526:         if (((c == '&') && (ctxt->token != '&')) && (NXT(1) == '#')) {
                   1527:            int val = htmlParseCharRef(ctxt);
                   1528:            COPY_BUF(0,buffer,nbchars,val);
                   1529:            NEXTL(l);
                   1530:        } else if ((c == '&') && (ctxt->token != '&')) {
                   1531:            ent = htmlParseEntityRef(ctxt, &name);
                   1532:            if (name != NULL) {
                   1533:                if (ent != NULL) {
                   1534:                    int val = ent->value;
                   1535:                    COPY_BUF(0,buffer,nbchars,val);
                   1536:                    NEXTL(l);
                   1537:                } else {
                   1538:                    const xmlChar *cur = name;
1.1       daniel   1539: 
1.53      veillard 1540:                    buffer[nbchars++] = '&';
                   1541:                    if (nbchars > buffer_size - HTML_PARSER_BUFFER_SIZE) {
                   1542:                        growBuffer(buffer);
                   1543:                    }
                   1544:                    while (*cur != 0) {
                   1545:                        buffer[nbchars++] = *cur++;
1.1       daniel   1546:                    }
1.53      veillard 1547:                    buffer[nbchars++] = ';';
1.1       daniel   1548:                }
                   1549:            }
                   1550:        } else {
1.53      veillard 1551:            COPY_BUF(l,buffer,nbchars,c);
                   1552:            NEXTL(l);
                   1553:            if (nbchars > buffer_size - HTML_PARSER_BUFFER_SIZE) {
                   1554:              growBuffer(buffer);
1.1       daniel   1555:            }
                   1556:        }
1.53      veillard 1557:        c = CUR_CHAR(l);
1.1       daniel   1558:     }
1.53      veillard 1559:     buffer[nbchars++] = 0;
1.1       daniel   1560:     return(buffer);
                   1561: }
                   1562: 
1.31      daniel   1563: /************************************************************************
                   1564:  *                                                                     *
                   1565:  *             Commodity functions to handle streams                   *
                   1566:  *                                                                     *
                   1567:  ************************************************************************/
                   1568: 
                   1569: /**
                   1570:  * htmlFreeInputStream:
                   1571:  * @input:  an htmlParserInputPtr
                   1572:  *
                   1573:  * Free up an input stream.
                   1574:  */
                   1575: void
                   1576: htmlFreeInputStream(htmlParserInputPtr input) {
                   1577:     if (input == NULL) return;
                   1578: 
                   1579:     if (input->filename != NULL) xmlFree((char *) input->filename);
                   1580:     if (input->directory != NULL) xmlFree((char *) input->directory);
                   1581:     if ((input->free != NULL) && (input->base != NULL))
                   1582:         input->free((xmlChar *) input->base);
                   1583:     if (input->buf != NULL) 
                   1584:         xmlFreeParserInputBuffer(input->buf);
                   1585:     memset(input, -1, sizeof(htmlParserInput));
                   1586:     xmlFree(input);
                   1587: }
                   1588: 
                   1589: /**
                   1590:  * htmlNewInputStream:
                   1591:  * @ctxt:  an HTML parser context
                   1592:  *
                   1593:  * Create a new input stream structure
                   1594:  * Returns the new input stream or NULL
                   1595:  */
                   1596: htmlParserInputPtr
                   1597: htmlNewInputStream(htmlParserCtxtPtr ctxt) {
                   1598:     htmlParserInputPtr input;
                   1599: 
                   1600:     input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
                   1601:     if (input == NULL) {
                   1602:         ctxt->errNo = XML_ERR_NO_MEMORY;
                   1603:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1604:            ctxt->sax->error(ctxt->userData, 
                   1605:                             "malloc: couldn't allocate a new input stream\n");
                   1606:        return(NULL);
                   1607:     }
1.51      veillard 1608:     memset(input, 0, sizeof(htmlParserInput));
1.31      daniel   1609:     input->filename = NULL;
                   1610:     input->directory = NULL;
                   1611:     input->base = NULL;
                   1612:     input->cur = NULL;
                   1613:     input->buf = NULL;
                   1614:     input->line = 1;
                   1615:     input->col = 1;
                   1616:     input->buf = NULL;
                   1617:     input->free = NULL;
1.51      veillard 1618:     input->version = NULL;
1.31      daniel   1619:     input->consumed = 0;
                   1620:     input->length = 0;
                   1621:     return(input);
                   1622: }
                   1623: 
1.1       daniel   1624: 
                   1625: /************************************************************************
                   1626:  *                                                                     *
                   1627:  *             Commodity functions, cleanup needed ?                   *
                   1628:  *                                                                     *
                   1629:  ************************************************************************/
                   1630: 
                   1631: /**
                   1632:  * areBlanks:
                   1633:  * @ctxt:  an HTML parser context
1.14      daniel   1634:  * @str:  a xmlChar *
1.1       daniel   1635:  * @len:  the size of @str
                   1636:  *
                   1637:  * Is this a sequence of blank chars that one can ignore ?
                   1638:  *
                   1639:  * Returns 1 if ignorable 0 otherwise.
                   1640:  */
                   1641: 
1.14      daniel   1642: static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1.1       daniel   1643:     int i;
                   1644:     xmlNodePtr lastChild;
                   1645: 
                   1646:     for (i = 0;i < len;i++)
                   1647:         if (!(IS_BLANK(str[i]))) return(0);
                   1648: 
1.48      daniel   1649:     if (CUR == 0) return(1);
1.1       daniel   1650:     if (CUR != '<') return(0);
1.62      veillard 1651:     if (ctxt->name == NULL)
                   1652:        return(1);
1.73    ! veillard 1653:     if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
1.63      veillard 1654:        return(1);
1.73    ! veillard 1655:     if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
1.62      veillard 1656:        return(1);
1.73    ! veillard 1657:     if (xmlStrEqual(ctxt->name, BAD_CAST"body"))
1.62      veillard 1658:        return(1);
1.1       daniel   1659:     if (ctxt->node == NULL) return(0);
                   1660:     lastChild = xmlGetLastChild(ctxt->node);
                   1661:     if (lastChild == NULL) {
                   1662:         if (ctxt->node->content != NULL) return(0);
                   1663:     } else if (xmlNodeIsText(lastChild))
                   1664:         return(0);
                   1665:     return(1);
                   1666: }
                   1667: 
                   1668: /**
                   1669:  * htmlHandleEntity:
                   1670:  * @ctxt:  an HTML parser context
                   1671:  * @entity:  an XML entity pointer.
                   1672:  *
                   1673:  * Default handling of an HTML entity, call the parser with the
                   1674:  * substitution string
                   1675:  */
                   1676: 
                   1677: void
                   1678: htmlHandleEntity(htmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
                   1679:     int len;
                   1680: 
                   1681:     if (entity->content == NULL) {
                   1682:         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1683:            ctxt->sax->error(ctxt->userData, "htmlHandleEntity %s: content == NULL\n",
                   1684:                       entity->name);
                   1685:        ctxt->wellFormed = 0;
                   1686:         return;
                   1687:     }
                   1688:     len = xmlStrlen(entity->content);
                   1689: 
                   1690:     /*
                   1691:      * Just handle the content as a set of chars.
                   1692:      */
1.59      veillard 1693:     htmlCheckParagraph(ctxt);
1.1       daniel   1694:     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   1695:        ctxt->sax->characters(ctxt->userData, entity->content, len);
                   1696: 
                   1697: }
                   1698: 
                   1699: /**
1.59      veillard 1700:  * htmlNewDocNoDtD:
1.1       daniel   1701:  * @URI:  URI for the dtd, or NULL
                   1702:  * @ExternalID:  the external ID of the DTD, or NULL
                   1703:  *
1.59      veillard 1704:  * Returns a new document, do not intialize the DTD if not provided
1.1       daniel   1705:  */
                   1706: htmlDocPtr
1.59      veillard 1707: htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
1.1       daniel   1708:     xmlDocPtr cur;
                   1709: 
                   1710:     /*
                   1711:      * Allocate a new document and fill the fields.
                   1712:      */
1.11      daniel   1713:     cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1.1       daniel   1714:     if (cur == NULL) {
                   1715:         fprintf(stderr, "xmlNewDoc : malloc failed\n");
                   1716:        return(NULL);
                   1717:     }
1.10      daniel   1718:     memset(cur, 0, sizeof(xmlDoc));
1.1       daniel   1719: 
1.20      daniel   1720:     cur->type = XML_HTML_DOCUMENT_NODE;
1.1       daniel   1721:     cur->version = NULL;
                   1722:     cur->intSubset = NULL;
1.59      veillard 1723:     if ((ExternalID != NULL) ||
                   1724:        (URI != NULL))
1.28      daniel   1725:        xmlCreateIntSubset(cur, BAD_CAST "HTML", ExternalID, URI);
1.41      daniel   1726:     cur->doc = cur;
1.1       daniel   1727:     cur->name = NULL;
1.37      daniel   1728:     cur->children = NULL; 
1.1       daniel   1729:     cur->extSubset = NULL;
                   1730:     cur->oldNs = NULL;
                   1731:     cur->encoding = NULL;
                   1732:     cur->standalone = 1;
                   1733:     cur->compression = 0;
1.12      daniel   1734:     cur->ids = NULL;
                   1735:     cur->refs = NULL;
1.1       daniel   1736: #ifndef XML_WITHOUT_CORBA
                   1737:     cur->_private = NULL;
                   1738: #endif
                   1739:     return(cur);
                   1740: }
                   1741: 
1.59      veillard 1742: /**
                   1743:  * htmlNewDoc:
                   1744:  * @URI:  URI for the dtd, or NULL
                   1745:  * @ExternalID:  the external ID of the DTD, or NULL
                   1746:  *
                   1747:  * Returns a new document
                   1748:  */
                   1749: htmlDocPtr
                   1750: htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
                   1751:     if ((URI == NULL) && (ExternalID == NULL))
                   1752:        return(htmlNewDocNoDtD(
                   1753:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   1754:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"));
                   1755: 
                   1756:     return(htmlNewDocNoDtD(URI, ExternalID));
                   1757: }
                   1758: 
1.1       daniel   1759: 
                   1760: /************************************************************************
                   1761:  *                                                                     *
                   1762:  *                     The parser itself                               *
                   1763:  *     Relates to http://www.w3.org/TR/html40                          *
                   1764:  *                                                                     *
                   1765:  ************************************************************************/
                   1766: 
                   1767: /************************************************************************
                   1768:  *                                                                     *
                   1769:  *                     The parser itself                               *
                   1770:  *                                                                     *
                   1771:  ************************************************************************/
                   1772: 
                   1773: /**
                   1774:  * htmlParseHTMLName:
                   1775:  * @ctxt:  an HTML parser context
                   1776:  *
1.26      daniel   1777:  * parse an HTML tag or attribute name, note that we convert it to lowercase
1.1       daniel   1778:  * since HTML names are not case-sensitive.
                   1779:  *
                   1780:  * Returns the Tag Name parsed or NULL
                   1781:  */
                   1782: 
1.14      daniel   1783: xmlChar *
1.1       daniel   1784: htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1785:     xmlChar *ret = NULL;
1.1       daniel   1786:     int i = 0;
1.31      daniel   1787:     xmlChar loc[HTML_PARSER_BUFFER_SIZE];
1.1       daniel   1788: 
                   1789:     if (!IS_LETTER(CUR) && (CUR != '_') &&
                   1790:         (CUR != ':')) return(NULL);
                   1791: 
1.31      daniel   1792:     while ((i < HTML_PARSER_BUFFER_SIZE) &&
1.45      daniel   1793:            ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1794:           (CUR == ':') || (CUR == '_'))) {
1.26      daniel   1795:        if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
1.1       daniel   1796:         else loc[i] = CUR;
                   1797:        i++;
                   1798:        
                   1799:        NEXT;
                   1800:     }
                   1801:     
                   1802:     ret = xmlStrndup(loc, i);
                   1803: 
                   1804:     return(ret);
                   1805: }
                   1806: 
                   1807: /**
                   1808:  * htmlParseName:
                   1809:  * @ctxt:  an HTML parser context
                   1810:  *
                   1811:  * parse an HTML name, this routine is case sensistive.
                   1812:  *
                   1813:  * Returns the Name parsed or NULL
                   1814:  */
                   1815: 
1.14      daniel   1816: xmlChar *
1.1       daniel   1817: htmlParseName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1818:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1819:     int len = 0;
1.1       daniel   1820: 
1.5       daniel   1821:     GROW;
                   1822:     if (!IS_LETTER(CUR) && (CUR != '_')) {
                   1823:        return(NULL);
                   1824:     }
1.1       daniel   1825: 
                   1826:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1827:            (CUR == '.') || (CUR == '-') ||
                   1828:           (CUR == '_') || (CUR == ':') || 
                   1829:           (IS_COMBINING(CUR)) ||
1.5       daniel   1830:           (IS_EXTENDER(CUR))) {
                   1831:        buf[len++] = CUR;
1.1       daniel   1832:        NEXT;
1.5       daniel   1833:        if (len >= HTML_MAX_NAMELEN) {
                   1834:            fprintf(stderr, 
                   1835:               "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
                   1836:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1837:                   (CUR == '.') || (CUR == '-') ||
                   1838:                   (CUR == '_') || (CUR == ':') || 
                   1839:                   (IS_COMBINING(CUR)) ||
                   1840:                   (IS_EXTENDER(CUR)))
                   1841:                 NEXT;
                   1842:            break;
                   1843:        }
                   1844:     }
                   1845:     return(xmlStrndup(buf, len));
1.1       daniel   1846: }
                   1847: 
                   1848: /**
                   1849:  * htmlParseHTMLAttribute:
                   1850:  * @ctxt:  an HTML parser context
1.19      daniel   1851:  * @stop:  a char stop value
1.1       daniel   1852:  * 
1.19      daniel   1853:  * parse an HTML attribute value till the stop (quote), if
                   1854:  * stop is 0 then it stops at the first space
1.1       daniel   1855:  *
1.19      daniel   1856:  * Returns the attribute parsed or NULL
1.1       daniel   1857:  */
                   1858: 
1.14      daniel   1859: xmlChar *
1.19      daniel   1860: htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
1.32      daniel   1861: #if 0
1.14      daniel   1862:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1863:     int len = 0;
1.1       daniel   1864: 
1.5       daniel   1865:     GROW;
1.19      daniel   1866:     while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
                   1867:        if ((stop == 0) && (IS_BLANK(CUR))) break;
1.5       daniel   1868:        buf[len++] = CUR;
1.1       daniel   1869:        NEXT;
1.5       daniel   1870:        if (len >= HTML_MAX_NAMELEN) {
                   1871:            fprintf(stderr, 
                   1872:               "htmlParseHTMLAttribute: reached HTML_MAX_NAMELEN limit\n");
                   1873:            while ((!IS_BLANK(CUR)) && (CUR != '<') &&
1.19      daniel   1874:                   (CUR != '>') &&
1.5       daniel   1875:                   (CUR != '\'') && (CUR != '"'))
                   1876:                 NEXT;
                   1877:            break;
                   1878:        }
                   1879:     }
                   1880:     return(xmlStrndup(buf, len));
1.32      daniel   1881: #else    
                   1882:     xmlChar *buffer = NULL;
                   1883:     int buffer_size = 0;
                   1884:     xmlChar *out = NULL;
                   1885:     xmlChar *name = NULL;
                   1886: 
                   1887:     xmlChar *cur = NULL;
                   1888:     htmlEntityDescPtr ent;
                   1889: 
                   1890:     /*
                   1891:      * allocate a translation buffer.
                   1892:      */
                   1893:     buffer_size = HTML_PARSER_BIG_BUFFER_SIZE;
                   1894:     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
                   1895:     if (buffer == NULL) {
                   1896:        perror("htmlParseHTMLAttribute: malloc failed");
                   1897:        return(NULL);
                   1898:     }
                   1899:     out = buffer;
                   1900: 
                   1901:     /*
                   1902:      * Ok loop until we reach one of the ending chars
                   1903:      */
                   1904:     while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
                   1905:        if ((stop == 0) && (IS_BLANK(CUR))) break;
                   1906:         if (CUR == '&') {
                   1907:            if (NXT(1) == '#') {
1.52      veillard 1908:                unsigned int c;
                   1909:                int bits;
                   1910: 
                   1911:                c = htmlParseCharRef(ctxt);
                   1912:                if      (c <    0x80)
                   1913:                        { *out++  = c;                bits= -6; }
                   1914:                else if (c <   0x800)
                   1915:                        { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   1916:                else if (c < 0x10000)
                   1917:                        { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   1918:                else                 
                   1919:                        { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   1920:         
                   1921:                for ( ; bits >= 0; bits-= 6) {
                   1922:                    *out++  = ((c >> bits) & 0x3F) | 0x80;
                   1923:                }
1.32      daniel   1924:            } else {
                   1925:                ent = htmlParseEntityRef(ctxt, &name);
                   1926:                if (name == NULL) {
                   1927:                    *out++ = '&';
                   1928:                    if (out - buffer > buffer_size - 100) {
                   1929:                        int index = out - buffer;
                   1930: 
                   1931:                        growBuffer(buffer);
                   1932:                        out = &buffer[index];
                   1933:                    }
1.52      veillard 1934:                } else if (ent == NULL) {
1.32      daniel   1935:                    *out++ = '&';
                   1936:                    cur = name;
                   1937:                    while (*cur != 0) {
                   1938:                        if (out - buffer > buffer_size - 100) {
                   1939:                            int index = out - buffer;
                   1940: 
                   1941:                            growBuffer(buffer);
                   1942:                            out = &buffer[index];
                   1943:                        }
                   1944:                        *out++ = *cur++;
                   1945:                    }
                   1946:                    xmlFree(name);
                   1947:                } else {
1.52      veillard 1948:                    unsigned int c;
                   1949:                    int bits;
                   1950: 
1.32      daniel   1951:                    if (out - buffer > buffer_size - 100) {
                   1952:                        int index = out - buffer;
                   1953: 
                   1954:                        growBuffer(buffer);
                   1955:                        out = &buffer[index];
                   1956:                    }
1.52      veillard 1957:                    c = (xmlChar)ent->value;
                   1958:                    if      (c <    0x80)
                   1959:                        { *out++  = c;                bits= -6; }
                   1960:                    else if (c <   0x800)
                   1961:                        { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   1962:                    else if (c < 0x10000)
                   1963:                        { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   1964:                    else                 
                   1965:                        { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   1966:             
                   1967:                    for ( ; bits >= 0; bits-= 6) {
                   1968:                        *out++  = ((c >> bits) & 0x3F) | 0x80;
                   1969:                    }
1.32      daniel   1970:                    xmlFree(name);
                   1971:                }
                   1972:            }
                   1973:        } else {
1.52      veillard 1974:            unsigned int c;
1.68      veillard 1975:            int bits, l;
1.52      veillard 1976: 
1.32      daniel   1977:            if (out - buffer > buffer_size - 100) {
1.52      veillard 1978:                int index = out - buffer;
                   1979: 
                   1980:                growBuffer(buffer);
                   1981:                out = &buffer[index];
                   1982:            }
1.68      veillard 1983:            c = CUR_CHAR(l);
1.52      veillard 1984:            if      (c <    0x80)
                   1985:                    { *out++  = c;                bits= -6; }
                   1986:            else if (c <   0x800)
                   1987:                    { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   1988:            else if (c < 0x10000)
                   1989:                    { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   1990:            else                 
                   1991:                    { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   1992:      
                   1993:            for ( ; bits >= 0; bits-= 6) {
                   1994:                *out++  = ((c >> bits) & 0x3F) | 0x80;
1.32      daniel   1995:            }
                   1996:            NEXT;
                   1997:        }
                   1998:     }
                   1999:     *out++ = 0;
                   2000:     return(buffer);
                   2001: #endif
1.1       daniel   2002: }
                   2003: 
                   2004: /**
                   2005:  * htmlParseNmtoken:
                   2006:  * @ctxt:  an HTML parser context
                   2007:  * 
                   2008:  * parse an HTML Nmtoken.
                   2009:  *
                   2010:  * Returns the Nmtoken parsed or NULL
                   2011:  */
                   2012: 
1.14      daniel   2013: xmlChar *
1.1       daniel   2014: htmlParseNmtoken(htmlParserCtxtPtr ctxt) {
1.14      daniel   2015:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   2016:     int len = 0;
1.1       daniel   2017: 
1.5       daniel   2018:     GROW;
1.1       daniel   2019:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   2020:            (CUR == '.') || (CUR == '-') ||
                   2021:           (CUR == '_') || (CUR == ':') || 
                   2022:           (IS_COMBINING(CUR)) ||
1.5       daniel   2023:           (IS_EXTENDER(CUR))) {
                   2024:        buf[len++] = CUR;
1.1       daniel   2025:        NEXT;
1.5       daniel   2026:        if (len >= HTML_MAX_NAMELEN) {
                   2027:            fprintf(stderr, 
                   2028:               "htmlParseNmtoken: reached HTML_MAX_NAMELEN limit\n");
                   2029:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   2030:                   (CUR == '.') || (CUR == '-') ||
                   2031:                   (CUR == '_') || (CUR == ':') || 
                   2032:                   (IS_COMBINING(CUR)) ||
                   2033:                   (IS_EXTENDER(CUR)))
                   2034:                 NEXT;
                   2035:            break;
                   2036:        }
                   2037:     }
                   2038:     return(xmlStrndup(buf, len));
1.1       daniel   2039: }
                   2040: 
                   2041: /**
                   2042:  * htmlParseEntityRef:
                   2043:  * @ctxt:  an HTML parser context
                   2044:  * @str:  location to store the entity name
                   2045:  *
                   2046:  * parse an HTML ENTITY references
                   2047:  *
                   2048:  * [68] EntityRef ::= '&' Name ';'
                   2049:  *
                   2050:  * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
                   2051:  *         if non-NULL *str will have to be freed by the caller.
                   2052:  */
                   2053: htmlEntityDescPtr
1.14      daniel   2054: htmlParseEntityRef(htmlParserCtxtPtr ctxt, xmlChar **str) {
                   2055:     xmlChar *name;
1.1       daniel   2056:     htmlEntityDescPtr ent = NULL;
                   2057:     *str = NULL;
                   2058: 
                   2059:     if (CUR == '&') {
                   2060:         NEXT;
                   2061:         name = htmlParseName(ctxt);
                   2062:        if (name == NULL) {
                   2063:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2064:                ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
                   2065:            ctxt->wellFormed = 0;
                   2066:        } else {
1.5       daniel   2067:            GROW;
1.1       daniel   2068:            if (CUR == ';') {
                   2069:                *str = name;
                   2070: 
                   2071:                /*
                   2072:                 * Lookup the entity in the table.
                   2073:                 */
                   2074:                ent = htmlEntityLookup(name);
1.32      daniel   2075:                if (ent != NULL) /* OK that's ugly !!! */
                   2076:                    NEXT;
1.1       daniel   2077:            } else {
                   2078:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2079:                    ctxt->sax->error(ctxt->userData,
                   2080:                                     "htmlParseEntityRef: expecting ';'\n");
1.32      daniel   2081:                *str = name;
1.1       daniel   2082:            }
                   2083:        }
                   2084:     }
                   2085:     return(ent);
                   2086: }
                   2087: 
                   2088: /**
                   2089:  * htmlParseAttValue:
                   2090:  * @ctxt:  an HTML parser context
                   2091:  *
                   2092:  * parse a value for an attribute
                   2093:  * Note: the parser won't do substitution of entities here, this
                   2094:  * will be handled later in xmlStringGetNodeList, unless it was
                   2095:  * asked for ctxt->replaceEntities != 0 
                   2096:  *
                   2097:  * Returns the AttValue parsed or NULL.
                   2098:  */
                   2099: 
1.14      daniel   2100: xmlChar *
1.1       daniel   2101: htmlParseAttValue(htmlParserCtxtPtr ctxt) {
1.14      daniel   2102:     xmlChar *ret = NULL;
1.1       daniel   2103: 
                   2104:     if (CUR == '"') {
                   2105:         NEXT;
1.19      daniel   2106:        ret = htmlParseHTMLAttribute(ctxt, '"');
1.1       daniel   2107:         if (CUR != '"') {
                   2108:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2109:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   2110:            ctxt->wellFormed = 0;
                   2111:        } else
                   2112:            NEXT;
                   2113:     } else if (CUR == '\'') {
                   2114:         NEXT;
1.19      daniel   2115:        ret = htmlParseHTMLAttribute(ctxt, '\'');
1.1       daniel   2116:         if (CUR != '\'') {
                   2117:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2118:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   2119:            ctxt->wellFormed = 0;
                   2120:        } else
                   2121:            NEXT;
                   2122:     } else {
                   2123:         /*
                   2124:         * That's an HTMLism, the attribute value may not be quoted
                   2125:         */
1.19      daniel   2126:        ret = htmlParseHTMLAttribute(ctxt, 0);
1.1       daniel   2127:        if (ret == NULL) {
                   2128:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2129:                ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
                   2130:            ctxt->wellFormed = 0;
                   2131:        }
                   2132:     }
                   2133:     return(ret);
                   2134: }
                   2135: 
                   2136: /**
                   2137:  * htmlParseSystemLiteral:
                   2138:  * @ctxt:  an HTML parser context
                   2139:  * 
                   2140:  * parse an HTML Literal
                   2141:  *
                   2142:  * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
                   2143:  *
                   2144:  * Returns the SystemLiteral parsed or NULL
                   2145:  */
                   2146: 
1.14      daniel   2147: xmlChar *
1.1       daniel   2148: htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   2149:     const xmlChar *q;
                   2150:     xmlChar *ret = NULL;
1.1       daniel   2151: 
                   2152:     if (CUR == '"') {
                   2153:         NEXT;
                   2154:        q = CUR_PTR;
                   2155:        while ((IS_CHAR(CUR)) && (CUR != '"'))
                   2156:            NEXT;
                   2157:        if (!IS_CHAR(CUR)) {
                   2158:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2159:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   2160:            ctxt->wellFormed = 0;
                   2161:        } else {
                   2162:            ret = xmlStrndup(q, CUR_PTR - q);
                   2163:            NEXT;
                   2164:         }
                   2165:     } else if (CUR == '\'') {
                   2166:         NEXT;
                   2167:        q = CUR_PTR;
                   2168:        while ((IS_CHAR(CUR)) && (CUR != '\''))
                   2169:            NEXT;
                   2170:        if (!IS_CHAR(CUR)) {
                   2171:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2172:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   2173:            ctxt->wellFormed = 0;
                   2174:        } else {
                   2175:            ret = xmlStrndup(q, CUR_PTR - q);
                   2176:            NEXT;
                   2177:         }
                   2178:     } else {
                   2179:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.38      daniel   2180:            ctxt->sax->error(ctxt->userData,
                   2181:                             "SystemLiteral \" or ' expected\n");
1.1       daniel   2182:        ctxt->wellFormed = 0;
                   2183:     }
                   2184:     
                   2185:     return(ret);
                   2186: }
                   2187: 
                   2188: /**
                   2189:  * htmlParsePubidLiteral:
                   2190:  * @ctxt:  an HTML parser context
                   2191:  *
                   2192:  * parse an HTML public literal
                   2193:  *
                   2194:  * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
                   2195:  *
                   2196:  * Returns the PubidLiteral parsed or NULL.
                   2197:  */
                   2198: 
1.14      daniel   2199: xmlChar *
1.1       daniel   2200: htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   2201:     const xmlChar *q;
                   2202:     xmlChar *ret = NULL;
1.1       daniel   2203:     /*
                   2204:      * Name ::= (Letter | '_') (NameChar)*
                   2205:      */
                   2206:     if (CUR == '"') {
                   2207:         NEXT;
                   2208:        q = CUR_PTR;
                   2209:        while (IS_PUBIDCHAR(CUR)) NEXT;
                   2210:        if (CUR != '"') {
                   2211:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2212:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   2213:            ctxt->wellFormed = 0;
                   2214:        } else {
                   2215:            ret = xmlStrndup(q, CUR_PTR - q);
                   2216:            NEXT;
                   2217:        }
                   2218:     } else if (CUR == '\'') {
                   2219:         NEXT;
                   2220:        q = CUR_PTR;
                   2221:        while ((IS_LETTER(CUR)) && (CUR != '\''))
                   2222:            NEXT;
                   2223:        if (!IS_LETTER(CUR)) {
                   2224:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2225:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   2226:            ctxt->wellFormed = 0;
                   2227:        } else {
                   2228:            ret = xmlStrndup(q, CUR_PTR - q);
                   2229:            NEXT;
                   2230:        }
                   2231:     } else {
                   2232:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2233:            ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
                   2234:        ctxt->wellFormed = 0;
                   2235:     }
                   2236:     
                   2237:     return(ret);
                   2238: }
                   2239: 
                   2240: /**
                   2241:  * htmlParseCharData:
                   2242:  * @ctxt:  an HTML parser context
                   2243:  * @cdata:  int indicating whether we are within a CDATA section
                   2244:  *
                   2245:  * parse a CharData section.
                   2246:  * if we are within a CDATA section ']]>' marks an end of section.
                   2247:  *
                   2248:  * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
                   2249:  */
                   2250: 
                   2251: void
                   2252: htmlParseCharData(htmlParserCtxtPtr ctxt, int cdata) {
1.53      veillard 2253:     xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
                   2254:     int nbchar = 0;
                   2255:     int cur, l;
                   2256: 
                   2257:     SHRINK;
                   2258:     cur = CUR_CHAR(l);
                   2259:     while (((cur != '<') || (ctxt->token == '<')) &&
                   2260:            ((cur != '&') || (ctxt->token == '&')) && 
                   2261:           (IS_CHAR(cur))) {
                   2262:        COPY_BUF(l,buf,nbchar,cur);
                   2263:        if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
                   2264:            /*
                   2265:             * Ok the segment is to be consumed as chars.
                   2266:             */
                   2267:            if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
                   2268:                if (areBlanks(ctxt, buf, nbchar)) {
                   2269:                    if (ctxt->sax->ignorableWhitespace != NULL)
                   2270:                        ctxt->sax->ignorableWhitespace(ctxt->userData,
                   2271:                                                       buf, nbchar);
                   2272:                } else {
1.59      veillard 2273:                    htmlCheckParagraph(ctxt);
1.53      veillard 2274:                    if (ctxt->sax->characters != NULL)
                   2275:                        ctxt->sax->characters(ctxt->userData, buf, nbchar);
                   2276:                }
1.1       daniel   2277:            }
1.53      veillard 2278:            nbchar = 0;
1.1       daniel   2279:        }
1.53      veillard 2280:        NEXTL(l);
                   2281:        cur = CUR_CHAR(l);
                   2282:     }
                   2283:     if (nbchar != 0) {
                   2284:        /*
                   2285:         * Ok the segment is to be consumed as chars.
                   2286:         */
                   2287:        if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
                   2288:            if (areBlanks(ctxt, buf, nbchar)) {
                   2289:                if (ctxt->sax->ignorableWhitespace != NULL)
                   2290:                    ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
                   2291:            } else {
1.59      veillard 2292:                htmlCheckParagraph(ctxt);
1.53      veillard 2293:                if (ctxt->sax->characters != NULL)
                   2294:                    ctxt->sax->characters(ctxt->userData, buf, nbchar);
1.25      daniel   2295:            }
                   2296:        }
1.1       daniel   2297:     }
                   2298: }
                   2299: 
                   2300: /**
                   2301:  * htmlParseExternalID:
                   2302:  * @ctxt:  an HTML parser context
1.14      daniel   2303:  * @publicID:  a xmlChar** receiving PubidLiteral
1.1       daniel   2304:  * @strict: indicate whether we should restrict parsing to only
                   2305:  *          production [75], see NOTE below
                   2306:  *
                   2307:  * Parse an External ID or a Public ID
                   2308:  *
                   2309:  * NOTE: Productions [75] and [83] interract badly since [75] can generate
                   2310:  *       'PUBLIC' S PubidLiteral S SystemLiteral
                   2311:  *
                   2312:  * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
                   2313:  *                   | 'PUBLIC' S PubidLiteral S SystemLiteral
                   2314:  *
                   2315:  * [83] PublicID ::= 'PUBLIC' S PubidLiteral
                   2316:  *
                   2317:  * Returns the function returns SystemLiteral and in the second
                   2318:  *                case publicID receives PubidLiteral, is strict is off
                   2319:  *                it is possible to return NULL and have publicID set.
                   2320:  */
                   2321: 
1.14      daniel   2322: xmlChar *
                   2323: htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
                   2324:     xmlChar *URI = NULL;
1.1       daniel   2325: 
                   2326:     if ((UPPER == 'S') && (UPP(1) == 'Y') &&
                   2327:          (UPP(2) == 'S') && (UPP(3) == 'T') &&
                   2328:         (UPP(4) == 'E') && (UPP(5) == 'M')) {
                   2329:         SKIP(6);
                   2330:        if (!IS_BLANK(CUR)) {
                   2331:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2332:                ctxt->sax->error(ctxt->userData,
                   2333:                    "Space required after 'SYSTEM'\n");
                   2334:            ctxt->wellFormed = 0;
                   2335:        }
                   2336:         SKIP_BLANKS;
                   2337:        URI = htmlParseSystemLiteral(ctxt);
                   2338:        if (URI == NULL) {
                   2339:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2340:                ctxt->sax->error(ctxt->userData,
                   2341:                  "htmlParseExternalID: SYSTEM, no URI\n");
                   2342:            ctxt->wellFormed = 0;
                   2343:         }
                   2344:     } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
                   2345:               (UPP(2) == 'B') && (UPP(3) == 'L') &&
                   2346:               (UPP(4) == 'I') && (UPP(5) == 'C')) {
                   2347:         SKIP(6);
                   2348:        if (!IS_BLANK(CUR)) {
                   2349:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2350:                ctxt->sax->error(ctxt->userData,
                   2351:                    "Space required after 'PUBLIC'\n");
                   2352:            ctxt->wellFormed = 0;
                   2353:        }
                   2354:         SKIP_BLANKS;
                   2355:        *publicID = htmlParsePubidLiteral(ctxt);
                   2356:        if (*publicID == NULL) {
                   2357:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2358:                ctxt->sax->error(ctxt->userData, 
                   2359:                  "htmlParseExternalID: PUBLIC, no Public Identifier\n");
                   2360:            ctxt->wellFormed = 0;
                   2361:        }
1.5       daniel   2362:         SKIP_BLANKS;
                   2363:         if ((CUR == '"') || (CUR == '\'')) {
                   2364:            URI = htmlParseSystemLiteral(ctxt);
1.1       daniel   2365:        }
                   2366:     }
                   2367:     return(URI);
                   2368: }
                   2369: 
                   2370: /**
                   2371:  * htmlParseComment:
                   2372:  * @ctxt:  an HTML parser context
                   2373:  *
                   2374:  * Parse an XML (SGML) comment <!-- .... -->
                   2375:  *
                   2376:  * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
                   2377:  */
                   2378: void
1.31      daniel   2379: htmlParseComment(htmlParserCtxtPtr ctxt) {
1.25      daniel   2380:     xmlChar *buf = NULL;
1.56      veillard 2381:     int len;
1.31      daniel   2382:     int size = HTML_PARSER_BUFFER_SIZE;
1.56      veillard 2383:     int q, ql;
                   2384:     int r, rl;
                   2385:     int cur, l;
                   2386:     xmlParserInputState state;
1.1       daniel   2387: 
                   2388:     /*
                   2389:      * Check that there is a comment right here.
                   2390:      */
1.56      veillard 2391:     if ((RAW != '<') || (NXT(1) != '!') ||
1.1       daniel   2392:         (NXT(2) != '-') || (NXT(3) != '-')) return;
                   2393: 
1.56      veillard 2394:     state = ctxt->instate;
                   2395:     ctxt->instate = XML_PARSER_COMMENT;
                   2396:     SHRINK;
                   2397:     SKIP(4);
1.25      daniel   2398:     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
                   2399:     if (buf == NULL) {
                   2400:        fprintf(stderr, "malloc of %d byte failed\n", size);
1.56      veillard 2401:        ctxt->instate = state;
1.25      daniel   2402:        return;
                   2403:     }
1.56      veillard 2404:     q = CUR_CHAR(ql);
                   2405:     NEXTL(ql);
                   2406:     r = CUR_CHAR(rl);
                   2407:     NEXTL(rl);
                   2408:     cur = CUR_CHAR(l);
                   2409:     len = 0;
                   2410:     while (IS_CHAR(cur) &&
                   2411:            ((cur != '>') ||
                   2412:            (r != '-') || (q != '-'))) {
                   2413:        if (len + 5 >= size) {
1.25      daniel   2414:            size *= 2;
1.50      veillard 2415:            buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
1.25      daniel   2416:            if (buf == NULL) {
                   2417:                fprintf(stderr, "realloc of %d byte failed\n", size);
1.56      veillard 2418:                ctxt->instate = state;
1.25      daniel   2419:                return;
                   2420:            }
                   2421:        }
1.56      veillard 2422:        COPY_BUF(ql,buf,len,q);
1.25      daniel   2423:        q = r;
1.56      veillard 2424:        ql = rl;
                   2425:        r = cur;
                   2426:        rl = l;
                   2427:        NEXTL(l);
                   2428:        cur = CUR_CHAR(l);
                   2429:        if (cur == 0) {
                   2430:            SHRINK;
                   2431:            GROW;
                   2432:            cur = CUR_CHAR(l);
                   2433:        }
1.1       daniel   2434:     }
1.56      veillard 2435:     buf[len] = 0;
                   2436:     if (!IS_CHAR(cur)) {
1.67      veillard 2437:        ctxt->errNo = XML_ERR_COMMENT_NOT_FINISHED;
1.1       daniel   2438:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.56      veillard 2439:            ctxt->sax->error(ctxt->userData,
                   2440:                             "Comment not terminated \n<!--%.50s\n", buf);
1.1       daniel   2441:        ctxt->wellFormed = 0;
1.56      veillard 2442:        xmlFree(buf);
1.1       daniel   2443:     } else {
                   2444:         NEXT;
1.56      veillard 2445:        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
                   2446:            (!ctxt->disableSAX))
1.31      daniel   2447:            ctxt->sax->comment(ctxt->userData, buf);
1.56      veillard 2448:        xmlFree(buf);
1.1       daniel   2449:     }
1.56      veillard 2450:     ctxt->instate = state;
1.1       daniel   2451: }
                   2452: 
                   2453: /**
                   2454:  * htmlParseCharRef:
                   2455:  * @ctxt:  an HTML parser context
                   2456:  *
                   2457:  * parse Reference declarations
                   2458:  *
                   2459:  * [66] CharRef ::= '&#' [0-9]+ ';' |
                   2460:  *                  '&#x' [0-9a-fA-F]+ ';'
                   2461:  *
                   2462:  * Returns the value parsed (as an int)
                   2463:  */
                   2464: int
                   2465: htmlParseCharRef(htmlParserCtxtPtr ctxt) {
                   2466:     int val = 0;
                   2467: 
                   2468:     if ((CUR == '&') && (NXT(1) == '#') &&
                   2469:         (NXT(2) == 'x')) {
                   2470:        SKIP(3);
                   2471:        while (CUR != ';') {
                   2472:            if ((CUR >= '0') && (CUR <= '9')) 
                   2473:                val = val * 16 + (CUR - '0');
                   2474:            else if ((CUR >= 'a') && (CUR <= 'f'))
                   2475:                val = val * 16 + (CUR - 'a') + 10;
                   2476:            else if ((CUR >= 'A') && (CUR <= 'F'))
                   2477:                val = val * 16 + (CUR - 'A') + 10;
                   2478:            else {
                   2479:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2480:                    ctxt->sax->error(ctxt->userData, 
                   2481:                         "htmlParseCharRef: invalid hexadecimal value\n");
                   2482:                ctxt->wellFormed = 0;
                   2483:                val = 0;
                   2484:                break;
                   2485:            }
                   2486:            NEXT;
                   2487:        }
                   2488:        if (CUR == ';')
                   2489:            NEXT;
                   2490:     } else if  ((CUR == '&') && (NXT(1) == '#')) {
                   2491:        SKIP(2);
                   2492:        while (CUR != ';') {
                   2493:            if ((CUR >= '0') && (CUR <= '9')) 
                   2494:                val = val * 10 + (CUR - '0');
                   2495:            else {
                   2496:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2497:                    ctxt->sax->error(ctxt->userData, 
                   2498:                         "htmlParseCharRef: invalid decimal value\n");
                   2499:                ctxt->wellFormed = 0;
                   2500:                val = 0;
                   2501:                break;
                   2502:            }
                   2503:            NEXT;
                   2504:        }
                   2505:        if (CUR == ';')
                   2506:            NEXT;
                   2507:     } else {
                   2508:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2509:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
                   2510:        ctxt->wellFormed = 0;
                   2511:     }
                   2512:     /*
                   2513:      * Check the value IS_CHAR ...
                   2514:      */
                   2515:     if (IS_CHAR(val)) {
                   2516:         return(val);
                   2517:     } else {
                   2518:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.14      daniel   2519:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid xmlChar value %d\n",
1.1       daniel   2520:                             val);
                   2521:        ctxt->wellFormed = 0;
                   2522:     }
                   2523:     return(0);
                   2524: }
                   2525: 
                   2526: 
                   2527: /**
                   2528:  * htmlParseDocTypeDecl :
                   2529:  * @ctxt:  an HTML parser context
                   2530:  *
                   2531:  * parse a DOCTYPE declaration
                   2532:  *
                   2533:  * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? 
                   2534:  *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
                   2535:  */
                   2536: 
                   2537: void
                   2538: htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
1.14      daniel   2539:     xmlChar *name;
                   2540:     xmlChar *ExternalID = NULL;
                   2541:     xmlChar *URI = NULL;
1.1       daniel   2542: 
                   2543:     /*
                   2544:      * We know that '<!DOCTYPE' has been detected.
                   2545:      */
                   2546:     SKIP(9);
                   2547: 
                   2548:     SKIP_BLANKS;
                   2549: 
                   2550:     /*
                   2551:      * Parse the DOCTYPE name.
                   2552:      */
                   2553:     name = htmlParseName(ctxt);
                   2554:     if (name == NULL) {
                   2555:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2556:            ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
                   2557:        ctxt->wellFormed = 0;
                   2558:     }
                   2559:     /*
                   2560:      * Check that upper(name) == "HTML" !!!!!!!!!!!!!
                   2561:      */
                   2562: 
                   2563:     SKIP_BLANKS;
                   2564: 
                   2565:     /*
                   2566:      * Check for SystemID and ExternalID
                   2567:      */
1.5       daniel   2568:     URI = htmlParseExternalID(ctxt, &ExternalID, 0);
1.1       daniel   2569:     SKIP_BLANKS;
                   2570: 
                   2571:     /*
                   2572:      * We should be at the end of the DOCTYPE declaration.
                   2573:      */
                   2574:     if (CUR != '>') {
                   2575:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2576:            ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
                   2577:        ctxt->wellFormed = 0;
                   2578:         /* We shouldn't try to resynchronize ... */
                   2579:     }
                   2580:     NEXT;
                   2581: 
                   2582:     /*
1.46      daniel   2583:      * Create or update the document accordingly to the DOCTYPE
1.1       daniel   2584:      */
1.46      daniel   2585:     if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
                   2586:        (!ctxt->disableSAX))
                   2587:        ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
1.1       daniel   2588: 
                   2589:     /*
                   2590:      * Cleanup, since we don't use all those identifiers
                   2591:      */
1.11      daniel   2592:     if (URI != NULL) xmlFree(URI);
                   2593:     if (ExternalID != NULL) xmlFree(ExternalID);
                   2594:     if (name != NULL) xmlFree(name);
1.1       daniel   2595: }
                   2596: 
                   2597: /**
                   2598:  * htmlParseAttribute:
                   2599:  * @ctxt:  an HTML parser context
1.14      daniel   2600:  * @value:  a xmlChar ** used to store the value of the attribute
1.1       daniel   2601:  *
                   2602:  * parse an attribute
                   2603:  *
                   2604:  * [41] Attribute ::= Name Eq AttValue
                   2605:  *
                   2606:  * [25] Eq ::= S? '=' S?
                   2607:  *
                   2608:  * With namespace:
                   2609:  *
                   2610:  * [NS 11] Attribute ::= QName Eq AttValue
                   2611:  *
                   2612:  * Also the case QName == xmlns:??? is handled independently as a namespace
                   2613:  * definition.
                   2614:  *
                   2615:  * Returns the attribute name, and the value in *value.
                   2616:  */
                   2617: 
1.14      daniel   2618: xmlChar *
                   2619: htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
1.31      daniel   2620:     xmlChar *name, *val = NULL;
1.1       daniel   2621: 
                   2622:     *value = NULL;
                   2623:     name = htmlParseName(ctxt);
                   2624:     if (name == NULL) {
                   2625:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2626:            ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
                   2627:        ctxt->wellFormed = 0;
                   2628:         return(NULL);
                   2629:     }
                   2630: 
                   2631:     /*
                   2632:      * read the value
                   2633:      */
                   2634:     SKIP_BLANKS;
                   2635:     if (CUR == '=') {
                   2636:         NEXT;
                   2637:        SKIP_BLANKS;
                   2638:        val = htmlParseAttValue(ctxt);
1.42      daniel   2639:        /******
1.1       daniel   2640:     } else {
1.42      daniel   2641:         * TODO : some attribute must have values, some may not
1.1       daniel   2642:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.31      daniel   2643:            ctxt->sax->warning(ctxt->userData,
1.42      daniel   2644:               "No value for attribute %s\n", name); */
1.1       daniel   2645:     }
                   2646: 
                   2647:     *value = val;
                   2648:     return(name);
                   2649: }
                   2650: 
                   2651: /**
1.47      daniel   2652:  * htmlCheckEncoding:
                   2653:  * @ctxt:  an HTML parser context
                   2654:  * @attvalue: the attribute value
                   2655:  *
                   2656:  * Checks an http-equiv attribute from a Meta tag to detect
                   2657:  * the encoding
                   2658:  * If a new encoding is detected the parser is switched to decode
                   2659:  * it and pass UTF8
                   2660:  */
                   2661: void
                   2662: htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
                   2663:     const xmlChar *encoding;
                   2664: 
                   2665:     if ((ctxt == NULL) || (attvalue == NULL))
                   2666:        return;
                   2667: 
1.69      veillard 2668:     encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
1.47      daniel   2669:     if (encoding != NULL) {
                   2670:        encoding += 8;
                   2671:     } else {
1.69      veillard 2672:        encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
1.47      daniel   2673:        if (encoding != NULL)
                   2674:            encoding += 9;
                   2675:     }
                   2676:     if (encoding != NULL) {
                   2677:        xmlCharEncoding enc;
                   2678:        xmlCharEncodingHandlerPtr handler;
                   2679: 
                   2680:        while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
                   2681: 
                   2682:        if (ctxt->input->encoding != NULL)
                   2683:            xmlFree((xmlChar *) ctxt->input->encoding);
                   2684:        ctxt->input->encoding = xmlStrdup(encoding);
                   2685: 
                   2686:        enc = xmlParseCharEncoding((const char *) encoding);
                   2687:        /*
                   2688:         * registered set of known encodings
                   2689:         */
                   2690:        if (enc != XML_CHAR_ENCODING_ERROR) {
                   2691:            xmlSwitchEncoding(ctxt, enc);
1.53      veillard 2692:            ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2693:        } else {
                   2694:            /*
                   2695:             * fallback for unknown encodings
                   2696:             */
                   2697:            handler = xmlFindCharEncodingHandler((const char *) encoding);
                   2698:            if (handler != NULL) {
                   2699:                xmlSwitchToEncoding(ctxt, handler);
1.54      veillard 2700:                ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2701:            } else {
                   2702:                ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
                   2703:            }
                   2704:        }
1.54      veillard 2705: 
                   2706:        if ((ctxt->input->buf != NULL) &&
                   2707:            (ctxt->input->buf->encoder != NULL) &&
                   2708:            (ctxt->input->buf->raw != NULL) &&
                   2709:            (ctxt->input->buf->buffer != NULL)) {
                   2710:            int nbchars;
1.56      veillard 2711:            int processed;
1.54      veillard 2712: 
                   2713:            /*
                   2714:             * convert as much as possible to the parser reading buffer.
                   2715:             */
1.56      veillard 2716:            processed = ctxt->input->cur - ctxt->input->base;
                   2717:            xmlBufferShrink(ctxt->input->buf->buffer, processed);
1.54      veillard 2718:            nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
                   2719:                                       ctxt->input->buf->buffer,
                   2720:                                       ctxt->input->buf->raw);
                   2721:            if (nbchars < 0) {
1.67      veillard 2722:                ctxt->errNo = XML_ERR_INVALID_ENCODING;
1.54      veillard 2723:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2724:                    ctxt->sax->error(ctxt->userData, 
                   2725:                     "htmlCheckEncoding: encoder error\n");
                   2726:            }
1.56      veillard 2727:            ctxt->input->base =
                   2728:            ctxt->input->cur = ctxt->input->buf->buffer->content;
1.54      veillard 2729:        }
1.47      daniel   2730:     }
                   2731: }
                   2732: 
                   2733: /**
                   2734:  * htmlCheckMeta:
                   2735:  * @ctxt:  an HTML parser context
                   2736:  * @atts:  the attributes values
                   2737:  *
                   2738:  * Checks an attributes from a Meta tag
                   2739:  */
                   2740: void
                   2741: htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
                   2742:     int i;
                   2743:     const xmlChar *att, *value;
                   2744:     int http = 0;
                   2745:     const xmlChar *content = NULL;
                   2746: 
                   2747:     if ((ctxt == NULL) || (atts == NULL))
                   2748:        return;
                   2749: 
                   2750:     i = 0;
                   2751:     att = atts[i++];
                   2752:     while (att != NULL) {
                   2753:        value = atts[i++];
1.69      veillard 2754:        if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
                   2755:         && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
1.47      daniel   2756:            http = 1;
1.69      veillard 2757:        else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
1.47      daniel   2758:            content = value;
                   2759:        att = atts[i++];
                   2760:     }
                   2761:     if ((http) && (content != NULL))
                   2762:        htmlCheckEncoding(ctxt, content);
                   2763: 
                   2764: }
                   2765: 
                   2766: /**
1.1       daniel   2767:  * htmlParseStartTag:
                   2768:  * @ctxt:  an HTML parser context
                   2769:  * 
                   2770:  * parse a start of tag either for rule element or
                   2771:  * EmptyElement. In both case we don't parse the tag closing chars.
                   2772:  *
                   2773:  * [40] STag ::= '<' Name (S Attribute)* S? '>'
                   2774:  *
                   2775:  * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
                   2776:  *
                   2777:  * With namespace:
                   2778:  *
                   2779:  * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
                   2780:  *
                   2781:  * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
                   2782:  *
                   2783:  */
                   2784: 
1.18      daniel   2785: void
1.1       daniel   2786: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2787:     xmlChar *name;
                   2788:     xmlChar *attname;
                   2789:     xmlChar *attvalue;
                   2790:     const xmlChar **atts = NULL;
1.1       daniel   2791:     int nbatts = 0;
                   2792:     int maxatts = 0;
1.47      daniel   2793:     int meta = 0;
1.1       daniel   2794:     int i;
                   2795: 
1.18      daniel   2796:     if (CUR != '<') return;
1.1       daniel   2797:     NEXT;
                   2798: 
1.19      daniel   2799:     GROW;
1.1       daniel   2800:     name = htmlParseHTMLName(ctxt);
                   2801:     if (name == NULL) {
                   2802:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2803:            ctxt->sax->error(ctxt->userData, 
                   2804:             "htmlParseStartTag: invalid element name\n");
                   2805:        ctxt->wellFormed = 0;
1.18      daniel   2806:         return;
1.1       daniel   2807:     }
1.73    ! veillard 2808:     if (xmlStrEqual(name, BAD_CAST"meta"))
1.47      daniel   2809:        meta = 1;
1.1       daniel   2810: 
                   2811:     /*
                   2812:      * Check for auto-closure of HTML elements.
                   2813:      */
                   2814:     htmlAutoClose(ctxt, name);
1.43      daniel   2815: 
                   2816:     /*
                   2817:      * Check for implied HTML elements.
                   2818:      */
                   2819:     htmlCheckImplied(ctxt, name);
1.1       daniel   2820: 
                   2821:     /*
                   2822:      * Now parse the attributes, it ends up with the ending
                   2823:      *
                   2824:      * (S Attribute)* S?
                   2825:      */
                   2826:     SKIP_BLANKS;
                   2827:     while ((IS_CHAR(CUR)) &&
                   2828:            (CUR != '>') && 
                   2829:           ((CUR != '/') || (NXT(1) != '>'))) {
1.26      daniel   2830:        long cons = ctxt->nbChars;
1.1       daniel   2831: 
1.19      daniel   2832:        GROW;
1.1       daniel   2833:        attname = htmlParseAttribute(ctxt, &attvalue);
1.31      daniel   2834:         if (attname != NULL) {
1.47      daniel   2835: 
1.1       daniel   2836:            /*
                   2837:             * Well formedness requires at most one declaration of an attribute
                   2838:             */
                   2839:            for (i = 0; i < nbatts;i += 2) {
1.73    ! veillard 2840:                if (xmlStrEqual(atts[i], attname)) {
1.1       daniel   2841:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.19      daniel   2842:                        ctxt->sax->error(ctxt->userData,
                   2843:                                         "Attribute %s redefined\n",
                   2844:                                         attname);
1.1       daniel   2845:                    ctxt->wellFormed = 0;
1.11      daniel   2846:                    xmlFree(attname);
1.31      daniel   2847:                    if (attvalue != NULL)
                   2848:                        xmlFree(attvalue);
1.19      daniel   2849:                    goto failed;
1.1       daniel   2850:                }
                   2851:            }
                   2852: 
                   2853:            /*
                   2854:             * Add the pair to atts
                   2855:             */
                   2856:            if (atts == NULL) {
                   2857:                maxatts = 10;
1.14      daniel   2858:                atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
1.1       daniel   2859:                if (atts == NULL) {
                   2860:                    fprintf(stderr, "malloc of %ld byte failed\n",
1.14      daniel   2861:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2862:                    if (name != NULL) xmlFree(name);
                   2863:                    return;
1.1       daniel   2864:                }
1.23      daniel   2865:            } else if (nbatts + 4 > maxatts) {
1.1       daniel   2866:                maxatts *= 2;
1.71      veillard 2867:                atts = (const xmlChar **) xmlRealloc((void *) atts,
                   2868:                                                     maxatts * sizeof(xmlChar *));
1.1       daniel   2869:                if (atts == NULL) {
                   2870:                    fprintf(stderr, "realloc of %ld byte failed\n",
1.14      daniel   2871:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2872:                    if (name != NULL) xmlFree(name);
                   2873:                    return;
1.1       daniel   2874:                }
                   2875:            }
                   2876:            atts[nbatts++] = attname;
                   2877:            atts[nbatts++] = attvalue;
                   2878:            atts[nbatts] = NULL;
                   2879:            atts[nbatts + 1] = NULL;
                   2880:        }
                   2881: 
1.19      daniel   2882: failed:
1.1       daniel   2883:        SKIP_BLANKS;
1.26      daniel   2884:         if (cons == ctxt->nbChars) {
1.1       daniel   2885:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2886:                ctxt->sax->error(ctxt->userData, 
                   2887:                 "htmlParseStartTag: problem parsing attributes\n");
                   2888:            ctxt->wellFormed = 0;
                   2889:            break;
                   2890:        }
                   2891:     }
                   2892: 
                   2893:     /*
1.47      daniel   2894:      * Handle specific association to the META tag
                   2895:      */
                   2896:     if (meta)
                   2897:        htmlCheckMeta(ctxt, atts);
                   2898: 
                   2899:     /*
1.1       daniel   2900:      * SAX: Start of Element !
                   2901:      */
1.15      daniel   2902:     htmlnamePush(ctxt, xmlStrdup(name));
1.18      daniel   2903: #ifdef DEBUG
                   2904:     fprintf(stderr,"Start of element %s: pushed %s\n", name, ctxt->name);
                   2905: #endif    
1.1       daniel   2906:     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                   2907:         ctxt->sax->startElement(ctxt->userData, name, atts);
                   2908: 
                   2909:     if (atts != NULL) {
1.31      daniel   2910:         for (i = 0;i < nbatts;i++) {
                   2911:            if (atts[i] != NULL)
                   2912:                xmlFree((xmlChar *) atts[i]);
                   2913:        }
1.45      daniel   2914:        xmlFree((void *) atts);
1.1       daniel   2915:     }
1.18      daniel   2916:     if (name != NULL) xmlFree(name);
1.1       daniel   2917: }
                   2918: 
                   2919: /**
                   2920:  * htmlParseEndTag:
                   2921:  * @ctxt:  an HTML parser context
                   2922:  *
                   2923:  * parse an end of tag
                   2924:  *
                   2925:  * [42] ETag ::= '</' Name S? '>'
                   2926:  *
                   2927:  * With namespace
                   2928:  *
                   2929:  * [NS 9] ETag ::= '</' QName S? '>'
                   2930:  */
                   2931: 
                   2932: void
1.18      daniel   2933: htmlParseEndTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2934:     xmlChar *name;
1.15      daniel   2935:     xmlChar *oldname;
1.1       daniel   2936:     int i;
                   2937: 
                   2938:     if ((CUR != '<') || (NXT(1) != '/')) {
                   2939:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2940:            ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
                   2941:        ctxt->wellFormed = 0;
                   2942:        return;
                   2943:     }
                   2944:     SKIP(2);
                   2945: 
                   2946:     name = htmlParseHTMLName(ctxt);
1.24      daniel   2947:     if (name == NULL) return;
1.1       daniel   2948: 
                   2949:     /*
                   2950:      * We should definitely be at the ending "S? '>'" part
                   2951:      */
                   2952:     SKIP_BLANKS;
                   2953:     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
                   2954:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2955:            ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
                   2956:        ctxt->wellFormed = 0;
                   2957:     } else
                   2958:        NEXT;
                   2959: 
                   2960:     /*
1.18      daniel   2961:      * If the name read is not one of the element in the parsing stack
                   2962:      * then return, it's just an error.
1.1       daniel   2963:      */
1.18      daniel   2964:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
1.73    ! veillard 2965:         if (xmlStrEqual(name, ctxt->nameTab[i])) break;
1.1       daniel   2966:     }
                   2967:     if (i < 0) {
                   2968:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.18      daniel   2969:            ctxt->sax->error(ctxt->userData,
                   2970:             "Unexpected end tag : %s\n", name);
1.11      daniel   2971:        xmlFree(name);
1.1       daniel   2972:        ctxt->wellFormed = 0;
                   2973:        return;
                   2974:     }
                   2975: 
1.18      daniel   2976: 
1.1       daniel   2977:     /*
                   2978:      * Check for auto-closure of HTML elements.
                   2979:      */
1.18      daniel   2980: 
1.1       daniel   2981:     htmlAutoCloseOnClose(ctxt, name);
                   2982: 
                   2983:     /*
                   2984:      * Well formedness constraints, opening and closing must match.
                   2985:      * With the exception that the autoclose may have popped stuff out
                   2986:      * of the stack.
                   2987:      */
1.73    ! veillard 2988:     if (!xmlStrEqual(name, ctxt->name)) {
1.18      daniel   2989: #ifdef DEBUG
                   2990:        fprintf(stderr,"End of tag %s: expecting %s\n", name, ctxt->name);
                   2991: #endif
1.15      daniel   2992:         if ((ctxt->name != NULL) && 
1.73    ! veillard 2993:            (!xmlStrEqual(ctxt->name, name))) {
1.1       daniel   2994:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2995:                ctxt->sax->error(ctxt->userData,
                   2996:                 "Opening and ending tag mismatch: %s and %s\n",
1.15      daniel   2997:                                 name, ctxt->name);
1.1       daniel   2998:            ctxt->wellFormed = 0;
                   2999:         }
                   3000:     }
                   3001: 
                   3002:     /*
                   3003:      * SAX: End of Tag
                   3004:      */
1.15      daniel   3005:     oldname = ctxt->name;
1.73    ! veillard 3006:     if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
1.18      daniel   3007:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3008:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3009:        oldname = htmlnamePop(ctxt);
1.18      daniel   3010:        if (oldname != NULL) {
                   3011: #ifdef DEBUG
                   3012:            fprintf(stderr,"End of tag %s: popping out %s\n", name, oldname);
                   3013: #endif
                   3014:            xmlFree(oldname);
                   3015: #ifdef DEBUG
                   3016:        } else {
                   3017:            fprintf(stderr,"End of tag %s: stack empty !!!\n", name);
                   3018: #endif
                   3019:        }
                   3020:     }
1.1       daniel   3021: 
                   3022:     if (name != NULL)
1.11      daniel   3023:        xmlFree(name);
1.1       daniel   3024: 
                   3025:     return;
                   3026: }
                   3027: 
                   3028: 
                   3029: /**
                   3030:  * htmlParseReference:
                   3031:  * @ctxt:  an HTML parser context
                   3032:  * 
                   3033:  * parse and handle entity references in content,
                   3034:  * this will end-up in a call to character() since this is either a
                   3035:  * CharRef, or a predefined entity.
                   3036:  */
                   3037: void
                   3038: htmlParseReference(htmlParserCtxtPtr ctxt) {
                   3039:     htmlEntityDescPtr ent;
1.52      veillard 3040:     xmlChar out[6];
1.14      daniel   3041:     xmlChar *name;
1.1       daniel   3042:     if (CUR != '&') return;
                   3043: 
                   3044:     if (NXT(1) == '#') {
1.52      veillard 3045:        unsigned int c;
                   3046:        int bits, i = 0;
                   3047: 
                   3048:        c = htmlParseCharRef(ctxt);
                   3049:         if      (c <    0x80) { out[i++]= c;                bits= -6; }
                   3050:         else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3051:         else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3052:         else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3053:  
                   3054:         for ( ; bits >= 0; bits-= 6) {
                   3055:             out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3056:         }
                   3057:        out[i] = 0;
                   3058: 
1.59      veillard 3059:        htmlCheckParagraph(ctxt);
1.1       daniel   3060:        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3061:            ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3062:     } else {
                   3063:        ent = htmlParseEntityRef(ctxt, &name);
1.32      daniel   3064:        if (name == NULL) {
1.59      veillard 3065:            htmlCheckParagraph(ctxt);
1.58      veillard 3066:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   3067:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.32      daniel   3068:            return;
                   3069:        }
1.52      veillard 3070:        if ((ent == NULL) || (ent->value <= 0)) {
1.59      veillard 3071:            htmlCheckParagraph(ctxt);
1.1       daniel   3072:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
1.8       daniel   3073:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1       daniel   3074:                ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1.32      daniel   3075:                /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
1.1       daniel   3076:            }
                   3077:        } else {
1.52      veillard 3078:            unsigned int c;
                   3079:            int bits, i = 0;
                   3080: 
                   3081:            c = ent->value;
                   3082:            if      (c <    0x80)
                   3083:                    { out[i++]= c;                bits= -6; }
                   3084:            else if (c <   0x800)
                   3085:                    { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3086:            else if (c < 0x10000)
                   3087:                    { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3088:            else                 
                   3089:                    { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3090:      
                   3091:            for ( ; bits >= 0; bits-= 6) {
                   3092:                out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3093:            }
                   3094:            out[i] = 0;
                   3095: 
1.59      veillard 3096:            htmlCheckParagraph(ctxt);
1.1       daniel   3097:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3098:                ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3099:        }
1.11      daniel   3100:        xmlFree(name);
1.1       daniel   3101:     }
                   3102: }
                   3103: 
                   3104: /**
                   3105:  * htmlParseContent:
                   3106:  * @ctxt:  an HTML parser context
                   3107:  * @name:  the node name
                   3108:  *
                   3109:  * Parse a content: comment, sub-element, reference or text.
                   3110:  *
                   3111:  */
                   3112: 
                   3113: void
1.18      daniel   3114: htmlParseContent(htmlParserCtxtPtr ctxt) {
1.15      daniel   3115:     xmlChar *currentNode;
1.18      daniel   3116:     int depth;
1.1       daniel   3117: 
1.26      daniel   3118:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3119:     depth = ctxt->nameNr;
                   3120:     while (1) {
1.26      daniel   3121:        long cons = ctxt->nbChars;
1.1       daniel   3122: 
1.18      daniel   3123:         GROW;
                   3124:        /*
                   3125:         * Our tag or one of it's parent or children is ending.
                   3126:         */
                   3127:         if ((CUR == '<') && (NXT(1) == '/')) {
                   3128:            htmlParseEndTag(ctxt);
1.26      daniel   3129:            if (currentNode != NULL) xmlFree(currentNode);
1.18      daniel   3130:            return;
                   3131:         }
                   3132: 
                   3133:        /*
                   3134:         * Has this node been popped out during parsing of
                   3135:         * the next element
                   3136:         */
1.73    ! veillard 3137:         if ((!xmlStrEqual(currentNode, ctxt->name)) &&
1.26      daniel   3138:            (depth >= ctxt->nameNr)) {
                   3139:            if (currentNode != NULL) xmlFree(currentNode);
                   3140:            return;
                   3141:        }
1.18      daniel   3142: 
1.1       daniel   3143:        /*
1.59      veillard 3144:         * Sometimes DOCTYPE arrives in the middle of the document
                   3145:         */
                   3146:        if ((CUR == '<') && (NXT(1) == '!') &&
                   3147:            (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3148:            (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3149:            (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3150:            (UPP(8) == 'E')) {
                   3151:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3152:                ctxt->sax->error(ctxt->userData,
                   3153:                     "Misplaced DOCTYPE declaration\n");
                   3154:            ctxt->wellFormed = 0;
                   3155:            htmlParseDocTypeDecl(ctxt);
                   3156:        }
                   3157: 
                   3158:        /*
1.1       daniel   3159:         * First case :  a comment
                   3160:         */
                   3161:        if ((CUR == '<') && (NXT(1) == '!') &&
                   3162:                 (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   3163:            htmlParseComment(ctxt);
1.1       daniel   3164:        }
                   3165: 
                   3166:        /*
                   3167:         * Second case :  a sub-element.
                   3168:         */
                   3169:        else if (CUR == '<') {
                   3170:            htmlParseElement(ctxt);
                   3171:        }
                   3172: 
                   3173:        /*
                   3174:         * Third case : a reference. If if has not been resolved,
                   3175:         *    parsing returns it's Name, create the node 
                   3176:         */
                   3177:        else if (CUR == '&') {
                   3178:            htmlParseReference(ctxt);
                   3179:        }
                   3180: 
                   3181:        /*
1.47      daniel   3182:         * Fourth : end of the resource
                   3183:         */
                   3184:        else if (CUR == 0) {
                   3185:            htmlAutoClose(ctxt, NULL);
                   3186:        }
                   3187: 
                   3188:        /*
1.1       daniel   3189:         * Last case, text. Note that References are handled directly.
                   3190:         */
                   3191:        else {
                   3192:            htmlParseCharData(ctxt, 0);
                   3193:        }
                   3194: 
1.26      daniel   3195:        if (cons == ctxt->nbChars) {
1.22      daniel   3196:            if (ctxt->node != NULL) {
                   3197:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3198:                    ctxt->sax->error(ctxt->userData,
                   3199:                         "detected an error in element content\n");
                   3200:                ctxt->wellFormed = 0;
                   3201:            }
1.1       daniel   3202:             break;
                   3203:        }
1.17      daniel   3204: 
1.5       daniel   3205:         GROW;
1.1       daniel   3206:     }
1.26      daniel   3207:     if (currentNode != NULL) xmlFree(currentNode);
1.1       daniel   3208: }
                   3209: 
                   3210: /**
                   3211:  * htmlParseElement:
                   3212:  * @ctxt:  an HTML parser context
                   3213:  *
                   3214:  * parse an HTML element, this is highly recursive
                   3215:  *
                   3216:  * [39] element ::= EmptyElemTag | STag content ETag
                   3217:  *
                   3218:  * [41] Attribute ::= Name Eq AttValue
                   3219:  */
                   3220: 
                   3221: void
                   3222: htmlParseElement(htmlParserCtxtPtr ctxt) {
1.14      daniel   3223:     xmlChar *name;
1.16      daniel   3224:     xmlChar *currentNode = NULL;
1.1       daniel   3225:     htmlElemDescPtr info;
1.10      daniel   3226:     htmlParserNodeInfo node_info;
1.31      daniel   3227:     xmlChar *oldname;
1.18      daniel   3228:     int depth = ctxt->nameNr;
1.1       daniel   3229: 
                   3230:     /* Capture start position */
1.10      daniel   3231:     if (ctxt->record_info) {
                   3232:         node_info.begin_pos = ctxt->input->consumed +
                   3233:                           (CUR_PTR - ctxt->input->base);
                   3234:        node_info.begin_line = ctxt->input->line;
                   3235:     }
1.1       daniel   3236: 
1.26      daniel   3237:     oldname = xmlStrdup(ctxt->name);
1.18      daniel   3238:     htmlParseStartTag(ctxt);
                   3239:     name = ctxt->name;
1.19      daniel   3240: #ifdef DEBUG
                   3241:     if (oldname == NULL)
                   3242:        fprintf(stderr, "Start of element %s\n", name);
                   3243:     else if (name == NULL)     
                   3244:        fprintf(stderr, "Start of element failed, was %s\n", oldname);
                   3245:     else       
                   3246:        fprintf(stderr, "Start of element %s, was %s\n", name, oldname);
                   3247: #endif
1.73    ! veillard 3248:     if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
1.18      daniel   3249:         (name == NULL)) {
1.19      daniel   3250:        if (CUR == '>')
                   3251:            NEXT;
1.26      daniel   3252:        if (oldname != NULL)
                   3253:            xmlFree(oldname);
1.1       daniel   3254:         return;
                   3255:     }
1.26      daniel   3256:     if (oldname != NULL)
                   3257:        xmlFree(oldname);
1.1       daniel   3258: 
                   3259:     /*
                   3260:      * Lookup the info for that element.
                   3261:      */
                   3262:     info = htmlTagLookup(name);
                   3263:     if (info == NULL) {
                   3264:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3265:            ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   3266:                             name);
                   3267:        ctxt->wellFormed = 0;
                   3268:     } else if (info->depr) {
                   3269: /***************************
                   3270:        if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   3271:            ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
                   3272:                               name);
                   3273:  ***************************/
                   3274:     }
                   3275: 
                   3276:     /*
                   3277:      * Check for an Empty Element labelled the XML/SGML way
                   3278:      */
                   3279:     if ((CUR == '/') && (NXT(1) == '>')) {
                   3280:         SKIP(2);
                   3281:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3282:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3283:        oldname = htmlnamePop(ctxt);
1.18      daniel   3284: #ifdef DEBUG
                   3285:         fprintf(stderr,"End of tag the XML way: popping out %s\n", oldname);
                   3286: #endif
1.17      daniel   3287:        if (oldname != NULL)
                   3288:            xmlFree(oldname);
1.1       daniel   3289:        return;
                   3290:     }
                   3291: 
1.5       daniel   3292:     if (CUR == '>') {
                   3293:         NEXT;
                   3294:     } else {
1.1       daniel   3295:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.56      veillard 3296:            ctxt->sax->error(ctxt->userData,
                   3297:                             "Couldn't find end of Start Tag %s\n",
                   3298:                             name);
1.1       daniel   3299:        ctxt->wellFormed = 0;
                   3300: 
                   3301:        /*
                   3302:         * end of parsing of this node.
                   3303:         */
1.73    ! veillard 3304:        if (xmlStrEqual(name, ctxt->name)) { 
1.18      daniel   3305:            nodePop(ctxt);
1.24      daniel   3306:            oldname = htmlnamePop(ctxt);
1.18      daniel   3307: #ifdef DEBUG
                   3308:            fprintf(stderr,"End of start tag problem: popping out %s\n", oldname);
                   3309: #endif
                   3310:            if (oldname != NULL)
                   3311:                xmlFree(oldname);
                   3312:        }    
1.10      daniel   3313: 
                   3314:        /*
                   3315:         * Capture end position and add node
                   3316:         */
                   3317:        if ( currentNode != NULL && ctxt->record_info ) {
                   3318:           node_info.end_pos = ctxt->input->consumed +
                   3319:                              (CUR_PTR - ctxt->input->base);
                   3320:           node_info.end_line = ctxt->input->line;
1.15      daniel   3321:           node_info.node = ctxt->node;
1.10      daniel   3322:           xmlParserAddNodeInfo(ctxt, &node_info);
                   3323:        }
1.1       daniel   3324:        return;
                   3325:     }
                   3326: 
                   3327:     /*
                   3328:      * Check for an Empty Element from DTD definition
                   3329:      */
                   3330:     if ((info != NULL) && (info->empty)) {
                   3331:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3332:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3333:        oldname = htmlnamePop(ctxt);
1.18      daniel   3334: #ifdef DEBUG
                   3335:        fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   3336: #endif
1.17      daniel   3337:        if (oldname != NULL)
                   3338:            xmlFree(oldname);
1.1       daniel   3339:        return;
                   3340:     }
                   3341: 
                   3342:     /*
                   3343:      * Parse the content of the element:
                   3344:      */
1.26      daniel   3345:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3346:     depth = ctxt->nameNr;
                   3347:     while (IS_CHAR(CUR)) {
                   3348:        htmlParseContent(ctxt);
                   3349:        if (ctxt->nameNr < depth) break; 
                   3350:     }  
1.1       daniel   3351: 
                   3352:     if (!IS_CHAR(CUR)) {
1.49      daniel   3353:        /************
1.1       daniel   3354:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3355:            ctxt->sax->error(ctxt->userData,
1.18      daniel   3356:                 "Premature end of data in tag %s\n", currentNode);
1.1       daniel   3357:        ctxt->wellFormed = 0;
1.49      daniel   3358:         *************/
1.1       daniel   3359: 
                   3360:        /*
                   3361:         * end of parsing of this node.
                   3362:         */
                   3363:        nodePop(ctxt);
1.24      daniel   3364:        oldname = htmlnamePop(ctxt);
1.18      daniel   3365: #ifdef DEBUG
                   3366:        fprintf(stderr,"Premature end of tag %s : popping out %s\n", name, oldname);
                   3367: #endif
1.17      daniel   3368:        if (oldname != NULL)
                   3369:            xmlFree(oldname);
1.26      daniel   3370:        if (currentNode != NULL)
                   3371:            xmlFree(currentNode);
1.1       daniel   3372:        return;
                   3373:     }
1.10      daniel   3374: 
                   3375:     /*
                   3376:      * Capture end position and add node
                   3377:      */
                   3378:     if ( currentNode != NULL && ctxt->record_info ) {
                   3379:        node_info.end_pos = ctxt->input->consumed +
                   3380:                           (CUR_PTR - ctxt->input->base);
                   3381:        node_info.end_line = ctxt->input->line;
1.15      daniel   3382:        node_info.node = ctxt->node;
1.10      daniel   3383:        xmlParserAddNodeInfo(ctxt, &node_info);
                   3384:     }
1.26      daniel   3385:     if (currentNode != NULL)
                   3386:        xmlFree(currentNode);
1.1       daniel   3387: }
                   3388: 
                   3389: /**
                   3390:  * htmlParseDocument :
                   3391:  * @ctxt:  an HTML parser context
                   3392:  * 
                   3393:  * parse an HTML document (and build a tree if using the standard SAX
                   3394:  * interface).
                   3395:  *
                   3396:  * Returns 0, -1 in case of error. the parser context is augmented
                   3397:  *                as a result of the parsing.
                   3398:  */
                   3399: 
                   3400: int
                   3401: htmlParseDocument(htmlParserCtxtPtr ctxt) {
1.59      veillard 3402:     xmlDtdPtr dtd;
                   3403: 
1.1       daniel   3404:     htmlDefaultSAXHandlerInit();
                   3405:     ctxt->html = 1;
                   3406: 
1.5       daniel   3407:     GROW;
1.1       daniel   3408:     /*
1.9       daniel   3409:      * SAX: beginning of the document processing.
1.1       daniel   3410:      */
                   3411:     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3412:         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
                   3413: 
                   3414:     /*
                   3415:      * Wipe out everything which is before the first '<'
                   3416:      */
1.22      daniel   3417:     SKIP_BLANKS;
1.1       daniel   3418:     if (CUR == 0) {
                   3419:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3420:            ctxt->sax->error(ctxt->userData, "Document is empty\n");
                   3421:        ctxt->wellFormed = 0;
                   3422:     }
                   3423: 
1.40      daniel   3424:     if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
                   3425:        ctxt->sax->startDocument(ctxt->userData);
                   3426: 
                   3427: 
1.22      daniel   3428:     /*
                   3429:      * Parse possible comments before any content
                   3430:      */
                   3431:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3432:            (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   3433:         htmlParseComment(ctxt);           
1.22      daniel   3434:        SKIP_BLANKS;
                   3435:     }     
                   3436: 
1.1       daniel   3437: 
                   3438:     /*
                   3439:      * Then possibly doc type declaration(s) and more Misc
                   3440:      * (doctypedecl Misc*)?
                   3441:      */
                   3442:     if ((CUR == '<') && (NXT(1) == '!') &&
                   3443:        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3444:        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3445:        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3446:        (UPP(8) == 'E')) {
                   3447:        htmlParseDocTypeDecl(ctxt);
                   3448:     }
                   3449:     SKIP_BLANKS;
                   3450: 
                   3451:     /*
1.55      veillard 3452:      * Parse possible comments before any content
                   3453:      */
                   3454:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3455:            (NXT(2) == '-') && (NXT(3) == '-')) {
                   3456:         htmlParseComment(ctxt);           
                   3457:        SKIP_BLANKS;
                   3458:     }     
                   3459: 
                   3460:     /*
1.1       daniel   3461:      * Time to start parsing the tree itself
                   3462:      */
1.22      daniel   3463:     htmlParseContent(ctxt);
1.1       daniel   3464: 
                   3465:     /*
1.47      daniel   3466:      * autoclose
                   3467:      */
                   3468:     if (CUR == 0)
                   3469:        htmlAutoClose(ctxt, NULL);
                   3470: 
                   3471: 
                   3472:     /*
1.1       daniel   3473:      * SAX: end of the document processing.
                   3474:      */
                   3475:     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3476:         ctxt->sax->endDocument(ctxt->userData);
1.59      veillard 3477: 
                   3478:     if (ctxt->myDoc != NULL) {
                   3479:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   3480:        if (dtd == NULL)
                   3481:            ctxt->myDoc->intSubset = 
                   3482:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   3483:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   3484:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
                   3485:     }
1.1       daniel   3486:     if (! ctxt->wellFormed) return(-1);
                   3487:     return(0);
                   3488: }
                   3489: 
                   3490: 
1.30      daniel   3491: /************************************************************************
                   3492:  *                                                                     *
                   3493:  *                     Parser contexts handling                        *
                   3494:  *                                                                     *
                   3495:  ************************************************************************/
1.1       daniel   3496: 
                   3497: /**
                   3498:  * xmlInitParserCtxt:
                   3499:  * @ctxt:  an HTML parser context
                   3500:  *
                   3501:  * Initialize a parser context
                   3502:  */
                   3503: 
                   3504: void
                   3505: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
                   3506: {
                   3507:     htmlSAXHandler *sax;
                   3508: 
1.21      daniel   3509:     if (ctxt == NULL) return;
                   3510:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   3511: 
1.11      daniel   3512:     sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
1.1       daniel   3513:     if (sax == NULL) {
                   3514:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3515:     }
1.68      veillard 3516:     else
                   3517:         memset(sax, 0, sizeof(htmlSAXHandler));
1.1       daniel   3518: 
                   3519:     /* Allocate the Input stack */
1.19      daniel   3520:     ctxt->inputTab = (htmlParserInputPtr *) 
                   3521:                       xmlMalloc(5 * sizeof(htmlParserInputPtr));
                   3522:     if (ctxt->inputTab == NULL) {
                   3523:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
1.65      veillard 3524:        ctxt->inputNr = 0;
                   3525:        ctxt->inputMax = 0;
                   3526:        ctxt->input = NULL;
                   3527:        return;
1.19      daniel   3528:     }
1.1       daniel   3529:     ctxt->inputNr = 0;
                   3530:     ctxt->inputMax = 5;
                   3531:     ctxt->input = NULL;
                   3532:     ctxt->version = NULL;
                   3533:     ctxt->encoding = NULL;
                   3534:     ctxt->standalone = -1;
1.30      daniel   3535:     ctxt->instate = XML_PARSER_START;
1.1       daniel   3536: 
                   3537:     /* Allocate the Node stack */
1.11      daniel   3538:     ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
1.65      veillard 3539:     if (ctxt->nodeTab == NULL) {
                   3540:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3541:        ctxt->nodeNr = 0;
                   3542:        ctxt->nodeMax = 0;
                   3543:        ctxt->node = NULL;
                   3544:        ctxt->inputNr = 0;
                   3545:        ctxt->inputMax = 0;
                   3546:        ctxt->input = NULL;
                   3547:        return;
                   3548:     }
1.1       daniel   3549:     ctxt->nodeNr = 0;
                   3550:     ctxt->nodeMax = 10;
                   3551:     ctxt->node = NULL;
                   3552: 
1.15      daniel   3553:     /* Allocate the Name stack */
                   3554:     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1.65      veillard 3555:     if (ctxt->nameTab == NULL) {
                   3556:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3557:        ctxt->nameNr = 0;
                   3558:        ctxt->nameMax = 10;
                   3559:        ctxt->name = NULL;
                   3560:        ctxt->nodeNr = 0;
                   3561:        ctxt->nodeMax = 0;
                   3562:        ctxt->node = NULL;
                   3563:        ctxt->inputNr = 0;
                   3564:        ctxt->inputMax = 0;
                   3565:        ctxt->input = NULL;
                   3566:        return;
                   3567:     }
1.15      daniel   3568:     ctxt->nameNr = 0;
                   3569:     ctxt->nameMax = 10;
                   3570:     ctxt->name = NULL;
                   3571: 
1.1       daniel   3572:     if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
                   3573:     else {
                   3574:         ctxt->sax = sax;
                   3575:        memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
                   3576:     }
                   3577:     ctxt->userData = ctxt;
                   3578:     ctxt->myDoc = NULL;
                   3579:     ctxt->wellFormed = 1;
                   3580:     ctxt->replaceEntities = 0;
                   3581:     ctxt->html = 1;
                   3582:     ctxt->record_info = 0;
1.21      daniel   3583:     ctxt->validate = 0;
1.26      daniel   3584:     ctxt->nbChars = 0;
1.30      daniel   3585:     ctxt->checkIndex = 0;
1.1       daniel   3586:     xmlInitNodeInfoSeq(&ctxt->node_seq);
                   3587: }
                   3588: 
                   3589: /**
                   3590:  * htmlFreeParserCtxt:
                   3591:  * @ctxt:  an HTML parser context
                   3592:  *
                   3593:  * Free all the memory used by a parser context. However the parsed
                   3594:  * document in ctxt->myDoc is not freed.
                   3595:  */
                   3596: 
                   3597: void
                   3598: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
                   3599: {
1.47      daniel   3600:     xmlFreeParserCtxt(ctxt);
1.1       daniel   3601: }
                   3602: 
                   3603: /**
                   3604:  * htmlCreateDocParserCtxt :
1.14      daniel   3605:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   3606:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3607:  *
                   3608:  * Create a parser context for an HTML document.
                   3609:  *
                   3610:  * Returns the new parser context or NULL
                   3611:  */
                   3612: htmlParserCtxtPtr
1.14      daniel   3613: htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
1.1       daniel   3614:     htmlParserCtxtPtr ctxt;
                   3615:     htmlParserInputPtr input;
                   3616:     /* htmlCharEncoding enc; */
                   3617: 
1.11      daniel   3618:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   3619:     if (ctxt == NULL) {
                   3620:         perror("malloc");
                   3621:        return(NULL);
                   3622:     }
                   3623:     htmlInitParserCtxt(ctxt);
1.11      daniel   3624:     input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   3625:     if (input == NULL) {
                   3626:         perror("malloc");
1.11      daniel   3627:        xmlFree(ctxt);
1.1       daniel   3628:        return(NULL);
                   3629:     }
1.19      daniel   3630:     memset(input, 0, sizeof(htmlParserInput));
1.1       daniel   3631: 
                   3632:     input->line = 1;
                   3633:     input->col = 1;
                   3634:     input->base = cur;
                   3635:     input->cur = cur;
                   3636: 
                   3637:     inputPush(ctxt, input);
                   3638:     return(ctxt);
                   3639: }
                   3640: 
1.31      daniel   3641: /************************************************************************
                   3642:  *                                                                     *
                   3643:  *             Progressive parsing interfaces                          *
                   3644:  *                                                                     *
                   3645:  ************************************************************************/
                   3646: 
                   3647: /**
                   3648:  * htmlParseLookupSequence:
                   3649:  * @ctxt:  an HTML parser context
                   3650:  * @first:  the first char to lookup
                   3651:  * @next:  the next char to lookup or zero
                   3652:  * @third:  the next char to lookup or zero
                   3653:  *
                   3654:  * Try to find if a sequence (first, next, third) or  just (first next) or
                   3655:  * (first) is available in the input stream.
                   3656:  * This function has a side effect of (possibly) incrementing ctxt->checkIndex
                   3657:  * to avoid rescanning sequences of bytes, it DOES change the state of the
                   3658:  * parser, do not use liberally.
                   3659:  * This is basically similar to xmlParseLookupSequence()
                   3660:  *
                   3661:  * Returns the index to the current parsing point if the full sequence
                   3662:  *      is available, -1 otherwise.
                   3663:  */
                   3664: int
                   3665: htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
                   3666:                        xmlChar next, xmlChar third) {
                   3667:     int base, len;
                   3668:     htmlParserInputPtr in;
                   3669:     const xmlChar *buf;
                   3670: 
                   3671:     in = ctxt->input;
                   3672:     if (in == NULL) return(-1);
                   3673:     base = in->cur - in->base;
                   3674:     if (base < 0) return(-1);
                   3675:     if (ctxt->checkIndex > base)
                   3676:         base = ctxt->checkIndex;
                   3677:     if (in->buf == NULL) {
                   3678:        buf = in->base;
                   3679:        len = in->length;
                   3680:     } else {
                   3681:        buf = in->buf->buffer->content;
                   3682:        len = in->buf->buffer->use;
                   3683:     }
                   3684:     /* take into account the sequence length */
                   3685:     if (third) len -= 2;
                   3686:     else if (next) len --;
                   3687:     for (;base < len;base++) {
                   3688:         if (buf[base] == first) {
                   3689:            if (third != 0) {
                   3690:                if ((buf[base + 1] != next) ||
                   3691:                    (buf[base + 2] != third)) continue;
                   3692:            } else if (next != 0) {
                   3693:                if (buf[base + 1] != next) continue;
                   3694:            }
                   3695:            ctxt->checkIndex = 0;
                   3696: #ifdef DEBUG_PUSH
                   3697:            if (next == 0)
                   3698:                fprintf(stderr, "HPP: lookup '%c' found at %d\n",
                   3699:                        first, base);
                   3700:            else if (third == 0)
                   3701:                fprintf(stderr, "HPP: lookup '%c%c' found at %d\n",
                   3702:                        first, next, base);
                   3703:            else 
                   3704:                fprintf(stderr, "HPP: lookup '%c%c%c' found at %d\n",
                   3705:                        first, next, third, base);
                   3706: #endif
                   3707:            return(base - (in->cur - in->base));
                   3708:        }
                   3709:     }
                   3710:     ctxt->checkIndex = base;
                   3711: #ifdef DEBUG_PUSH
                   3712:     if (next == 0)
                   3713:        fprintf(stderr, "HPP: lookup '%c' failed\n", first);
                   3714:     else if (third == 0)
                   3715:        fprintf(stderr, "HPP: lookup '%c%c' failed\n", first, next);
                   3716:     else       
                   3717:        fprintf(stderr, "HPP: lookup '%c%c%c' failed\n", first, next, third);
                   3718: #endif
                   3719:     return(-1);
                   3720: }
                   3721: 
                   3722: /**
1.32      daniel   3723:  * htmlParseTryOrFinish:
1.31      daniel   3724:  * @ctxt:  an HTML parser context
1.32      daniel   3725:  * @terminate:  last chunk indicator
1.31      daniel   3726:  *
                   3727:  * Try to progress on parsing
                   3728:  *
                   3729:  * Returns zero if no parsing was possible
                   3730:  */
                   3731: int
1.32      daniel   3732: htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
1.31      daniel   3733:     int ret = 0;
                   3734:     htmlParserInputPtr in;
1.47      daniel   3735:     int avail = 0;
1.31      daniel   3736:     xmlChar cur, next;
                   3737: 
                   3738: #ifdef DEBUG_PUSH
                   3739:     switch (ctxt->instate) {
                   3740:        case XML_PARSER_EOF:
                   3741:            fprintf(stderr, "HPP: try EOF\n"); break;
                   3742:        case XML_PARSER_START:
                   3743:            fprintf(stderr, "HPP: try START\n"); break;
                   3744:        case XML_PARSER_MISC:
                   3745:            fprintf(stderr, "HPP: try MISC\n");break;
                   3746:        case XML_PARSER_COMMENT:
                   3747:            fprintf(stderr, "HPP: try COMMENT\n");break;
                   3748:        case XML_PARSER_PROLOG:
                   3749:            fprintf(stderr, "HPP: try PROLOG\n");break;
                   3750:        case XML_PARSER_START_TAG:
                   3751:            fprintf(stderr, "HPP: try START_TAG\n");break;
                   3752:        case XML_PARSER_CONTENT:
                   3753:            fprintf(stderr, "HPP: try CONTENT\n");break;
                   3754:        case XML_PARSER_CDATA_SECTION:
                   3755:            fprintf(stderr, "HPP: try CDATA_SECTION\n");break;
                   3756:        case XML_PARSER_END_TAG:
                   3757:            fprintf(stderr, "HPP: try END_TAG\n");break;
                   3758:        case XML_PARSER_ENTITY_DECL:
                   3759:            fprintf(stderr, "HPP: try ENTITY_DECL\n");break;
                   3760:        case XML_PARSER_ENTITY_VALUE:
                   3761:            fprintf(stderr, "HPP: try ENTITY_VALUE\n");break;
                   3762:        case XML_PARSER_ATTRIBUTE_VALUE:
                   3763:            fprintf(stderr, "HPP: try ATTRIBUTE_VALUE\n");break;
                   3764:        case XML_PARSER_DTD:
                   3765:            fprintf(stderr, "HPP: try DTD\n");break;
                   3766:        case XML_PARSER_EPILOG:
                   3767:            fprintf(stderr, "HPP: try EPILOG\n");break;
                   3768:        case XML_PARSER_PI:
                   3769:            fprintf(stderr, "HPP: try PI\n");break;
                   3770:     }
                   3771: #endif
                   3772: 
                   3773:     while (1) {
                   3774: 
                   3775:        in = ctxt->input;
                   3776:        if (in == NULL) break;
                   3777:        if (in->buf == NULL)
                   3778:            avail = in->length - (in->cur - in->base);
                   3779:        else
                   3780:            avail = in->buf->buffer->use - (in->cur - in->base);
1.47      daniel   3781:        if ((avail == 0) && (terminate)) {
                   3782:            htmlAutoClose(ctxt, NULL);
1.54      veillard 3783:            if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   3784:                /*
                   3785:                 * SAX: end of the document processing.
                   3786:                 */
1.47      daniel   3787:                ctxt->instate = XML_PARSER_EOF;
1.54      veillard 3788:                if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3789:                    ctxt->sax->endDocument(ctxt->userData);
                   3790:            }
1.47      daniel   3791:        }
1.31      daniel   3792:         if (avail < 1)
                   3793:            goto done;
                   3794:         switch (ctxt->instate) {
                   3795:             case XML_PARSER_EOF:
                   3796:                /*
                   3797:                 * Document parsing is done !
                   3798:                 */
                   3799:                goto done;
                   3800:             case XML_PARSER_START:
                   3801:                /*
                   3802:                 * Very first chars read from the document flow.
                   3803:                 */
                   3804:                cur = in->cur[0];
                   3805:                if (IS_BLANK(cur)) {
                   3806:                    SKIP_BLANKS;
                   3807:                    if (in->buf == NULL)
                   3808:                        avail = in->length - (in->cur - in->base);
                   3809:                    else
                   3810:                        avail = in->buf->buffer->use - (in->cur - in->base);
                   3811:                }
                   3812:                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3813:                    ctxt->sax->setDocumentLocator(ctxt->userData,
                   3814:                                                  &xmlDefaultSAXLocator);
1.46      daniel   3815:                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
                   3816:                    (!ctxt->disableSAX))
                   3817:                    ctxt->sax->startDocument(ctxt->userData);
                   3818: 
1.31      daniel   3819:                cur = in->cur[0];
                   3820:                next = in->cur[1];
                   3821:                if ((cur == '<') && (next == '!') &&
                   3822:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3823:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3824:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3825:                    (UPP(8) == 'E')) {
1.32      daniel   3826:                    if ((!terminate) &&
                   3827:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3828:                        goto done;
                   3829: #ifdef DEBUG_PUSH
                   3830:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3831: #endif
                   3832:                    htmlParseDocTypeDecl(ctxt);
                   3833:                    ctxt->instate = XML_PARSER_PROLOG;
                   3834: #ifdef DEBUG_PUSH
                   3835:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3836: #endif
                   3837:                 } else {
                   3838:                    ctxt->instate = XML_PARSER_MISC;
                   3839:                }
                   3840: #ifdef DEBUG_PUSH
                   3841:                fprintf(stderr, "HPP: entering MISC\n");
                   3842: #endif
                   3843:                break;
                   3844:             case XML_PARSER_MISC:
                   3845:                SKIP_BLANKS;
                   3846:                if (in->buf == NULL)
                   3847:                    avail = in->length - (in->cur - in->base);
                   3848:                else
                   3849:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3850:                if (avail < 2)
                   3851:                    goto done;
                   3852:                cur = in->cur[0];
                   3853:                next = in->cur[1];
                   3854:                if ((cur == '<') && (next == '!') &&
                   3855:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3856:                    if ((!terminate) &&
                   3857:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3858:                        goto done;
                   3859: #ifdef DEBUG_PUSH
                   3860:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3861: #endif
                   3862:                    htmlParseComment(ctxt);
                   3863:                    ctxt->instate = XML_PARSER_MISC;
                   3864:                } else if ((cur == '<') && (next == '!') &&
                   3865:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3866:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3867:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3868:                    (UPP(8) == 'E')) {
1.32      daniel   3869:                    if ((!terminate) &&
                   3870:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3871:                        goto done;
                   3872: #ifdef DEBUG_PUSH
                   3873:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3874: #endif
                   3875:                    htmlParseDocTypeDecl(ctxt);
                   3876:                    ctxt->instate = XML_PARSER_PROLOG;
                   3877: #ifdef DEBUG_PUSH
                   3878:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3879: #endif
                   3880:                } else if ((cur == '<') && (next == '!') &&
                   3881:                           (avail < 9)) {
                   3882:                    goto done;
                   3883:                } else {
                   3884:                    ctxt->instate = XML_PARSER_START_TAG;
                   3885: #ifdef DEBUG_PUSH
                   3886:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3887: #endif
                   3888:                }
                   3889:                break;
                   3890:             case XML_PARSER_PROLOG:
                   3891:                SKIP_BLANKS;
                   3892:                if (in->buf == NULL)
                   3893:                    avail = in->length - (in->cur - in->base);
                   3894:                else
                   3895:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3896:                if (avail < 2) 
                   3897:                    goto done;
                   3898:                cur = in->cur[0];
                   3899:                next = in->cur[1];
                   3900:                if ((cur == '<') && (next == '!') &&
                   3901:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3902:                    if ((!terminate) &&
                   3903:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3904:                        goto done;
                   3905: #ifdef DEBUG_PUSH
                   3906:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3907: #endif
                   3908:                    htmlParseComment(ctxt);
                   3909:                    ctxt->instate = XML_PARSER_PROLOG;
                   3910:                } else if ((cur == '<') && (next == '!') &&
                   3911:                           (avail < 4)) {
                   3912:                    goto done;
                   3913:                } else {
                   3914:                    ctxt->instate = XML_PARSER_START_TAG;
                   3915: #ifdef DEBUG_PUSH
                   3916:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3917: #endif
                   3918:                }
                   3919:                break;
                   3920:             case XML_PARSER_EPILOG:
                   3921:                if (in->buf == NULL)
                   3922:                    avail = in->length - (in->cur - in->base);
                   3923:                else
                   3924:                    avail = in->buf->buffer->use - (in->cur - in->base);
1.55      veillard 3925:                if (avail < 1)
                   3926:                    goto done;
                   3927:                cur = in->cur[0];
                   3928:                if (IS_BLANK(cur)) {
                   3929:                    htmlParseCharData(ctxt, 0);
                   3930:                    goto done;
                   3931:                }
1.31      daniel   3932:                if (avail < 2)
                   3933:                    goto done;
                   3934:                next = in->cur[1];
                   3935:                if ((cur == '<') && (next == '!') &&
                   3936:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3937:                    if ((!terminate) &&
                   3938:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3939:                        goto done;
                   3940: #ifdef DEBUG_PUSH
                   3941:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3942: #endif
                   3943:                    htmlParseComment(ctxt);
                   3944:                    ctxt->instate = XML_PARSER_EPILOG;
                   3945:                } else if ((cur == '<') && (next == '!') &&
                   3946:                           (avail < 4)) {
                   3947:                    goto done;
                   3948:                } else {
1.67      veillard 3949:                    ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   3950:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3951:                        ctxt->sax->error(ctxt->userData,
                   3952:                            "Extra content at the end of the document\n");
                   3953:                    ctxt->wellFormed = 0;
                   3954:                    ctxt->instate = XML_PARSER_EOF;
                   3955: #ifdef DEBUG_PUSH
                   3956:                    fprintf(stderr, "HPP: entering EOF\n");
                   3957: #endif
                   3958:                    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3959:                        ctxt->sax->endDocument(ctxt->userData);
                   3960:                    goto done;
                   3961:                }
                   3962:                break;
                   3963:             case XML_PARSER_START_TAG: {
                   3964:                xmlChar *name, *oldname;
                   3965:                int depth = ctxt->nameNr;
                   3966:                htmlElemDescPtr info;
                   3967: 
                   3968:                if (avail < 2)
                   3969:                    goto done;
                   3970:                cur = in->cur[0];
                   3971:                if (cur != '<') {
                   3972:                    ctxt->instate = XML_PARSER_CONTENT;
                   3973: #ifdef DEBUG_PUSH
                   3974:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   3975: #endif
                   3976:                    break;
                   3977:                }
1.32      daniel   3978:                if ((!terminate) &&
                   3979:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3980:                    goto done;
                   3981: 
                   3982:                oldname = xmlStrdup(ctxt->name);
                   3983:                htmlParseStartTag(ctxt);
                   3984:                name = ctxt->name;
                   3985: #ifdef DEBUG
                   3986:                if (oldname == NULL)
                   3987:                    fprintf(stderr, "Start of element %s\n", name);
                   3988:                else if (name == NULL)  
                   3989:                    fprintf(stderr, "Start of element failed, was %s\n",
                   3990:                            oldname);
                   3991:                else    
                   3992:                    fprintf(stderr, "Start of element %s, was %s\n",
                   3993:                            name, oldname);
                   3994: #endif
                   3995:                if (((depth == ctxt->nameNr) &&
1.73    ! veillard 3996:                     (xmlStrEqual(oldname, ctxt->name))) ||
1.31      daniel   3997:                    (name == NULL)) {
                   3998:                    if (CUR == '>')
                   3999:                        NEXT;
                   4000:                    if (oldname != NULL)
                   4001:                        xmlFree(oldname);
                   4002:                    break;
                   4003:                }
                   4004:                if (oldname != NULL)
                   4005:                    xmlFree(oldname);
                   4006: 
                   4007:                /*
                   4008:                 * Lookup the info for that element.
                   4009:                 */
                   4010:                info = htmlTagLookup(name);
                   4011:                if (info == NULL) {
                   4012:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4013:                        ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   4014:                                         name);
                   4015:                    ctxt->wellFormed = 0;
                   4016:                } else if (info->depr) {
                   4017:                    /***************************
                   4018:                    if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   4019:                        ctxt->sax->warning(ctxt->userData,
                   4020:                                           "Tag %s is deprecated\n",
                   4021:                                           name);
                   4022:                     ***************************/
                   4023:                }
                   4024: 
                   4025:                /*
                   4026:                 * Check for an Empty Element labelled the XML/SGML way
                   4027:                 */
                   4028:                if ((CUR == '/') && (NXT(1) == '>')) {
                   4029:                    SKIP(2);
                   4030:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4031:                        ctxt->sax->endElement(ctxt->userData, name);
                   4032:                    oldname = htmlnamePop(ctxt);
                   4033: #ifdef DEBUG
                   4034:                    fprintf(stderr,"End of tag the XML way: popping out %s\n",
                   4035:                            oldname);
                   4036: #endif
                   4037:                    if (oldname != NULL)
                   4038:                        xmlFree(oldname);
                   4039:                    ctxt->instate = XML_PARSER_CONTENT;
                   4040: #ifdef DEBUG_PUSH
                   4041:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   4042: #endif
                   4043:                    break;
                   4044:                }
                   4045: 
                   4046:                if (CUR == '>') {
                   4047:                    NEXT;
                   4048:                } else {
                   4049:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4050:                        ctxt->sax->error(ctxt->userData, 
                   4051:                                         "Couldn't find end of Start Tag %s\n",
                   4052:                                         name);
                   4053:                    ctxt->wellFormed = 0;
                   4054: 
                   4055:                    /*
                   4056:                     * end of parsing of this node.
                   4057:                     */
1.73    ! veillard 4058:                    if (xmlStrEqual(name, ctxt->name)) { 
1.31      daniel   4059:                        nodePop(ctxt);
                   4060:                        oldname = htmlnamePop(ctxt);
                   4061: #ifdef DEBUG
                   4062:                        fprintf(stderr,
                   4063:                         "End of start tag problem: popping out %s\n", oldname);
                   4064: #endif
                   4065:                        if (oldname != NULL)
                   4066:                            xmlFree(oldname);
                   4067:                    }    
                   4068: 
                   4069:                    ctxt->instate = XML_PARSER_CONTENT;
                   4070: #ifdef DEBUG_PUSH
                   4071:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   4072: #endif
                   4073:                    break;
                   4074:                }
                   4075: 
                   4076:                /*
                   4077:                 * Check for an Empty Element from DTD definition
                   4078:                 */
                   4079:                if ((info != NULL) && (info->empty)) {
                   4080:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4081:                        ctxt->sax->endElement(ctxt->userData, name);
                   4082:                    oldname = htmlnamePop(ctxt);
                   4083: #ifdef DEBUG
                   4084:                    fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   4085: #endif
                   4086:                    if (oldname != NULL)
                   4087:                        xmlFree(oldname);
                   4088:                }
                   4089:                ctxt->instate = XML_PARSER_CONTENT;
                   4090: #ifdef DEBUG_PUSH
                   4091:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4092: #endif
                   4093:                 break;
                   4094:            }
1.56      veillard 4095:             case XML_PARSER_CONTENT: {
                   4096:                long cons;
1.31      daniel   4097:                 /*
                   4098:                 * Handle preparsed entities and charRef
                   4099:                 */
                   4100:                if (ctxt->token != 0) {
1.47      daniel   4101:                    xmlChar chr[2] = { 0 , 0 } ;
1.31      daniel   4102: 
1.47      daniel   4103:                    chr[0] = (xmlChar) ctxt->token;
1.59      veillard 4104:                    htmlCheckParagraph(ctxt);
1.31      daniel   4105:                    if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.47      daniel   4106:                        ctxt->sax->characters(ctxt->userData, chr, 1);
1.31      daniel   4107:                    ctxt->token = 0;
                   4108:                    ctxt->checkIndex = 0;
                   4109:                }
1.47      daniel   4110:                if ((avail == 1) && (terminate)) {
                   4111:                    cur = in->cur[0];
                   4112:                    if ((cur != '<') && (cur != '&')) {
1.48      daniel   4113:                        if (ctxt->sax != NULL) {
                   4114:                            if (IS_BLANK(cur)) {
                   4115:                                if (ctxt->sax->ignorableWhitespace != NULL)
                   4116:                                    ctxt->sax->ignorableWhitespace(
                   4117:                                            ctxt->userData, &cur, 1);
                   4118:                            } else {
1.59      veillard 4119:                                htmlCheckParagraph(ctxt);
1.48      daniel   4120:                                if (ctxt->sax->characters != NULL)
                   4121:                                    ctxt->sax->characters(
                   4122:                                            ctxt->userData, &cur, 1);
                   4123:                            }
                   4124:                        }
1.47      daniel   4125:                        ctxt->token = 0;
                   4126:                        ctxt->checkIndex = 0;
                   4127:                        NEXT;
                   4128:                    }
                   4129:                    break;
                   4130:                }
1.31      daniel   4131:                if (avail < 2)
                   4132:                    goto done;
                   4133:                cur = in->cur[0];
                   4134:                next = in->cur[1];
1.56      veillard 4135:                cons = ctxt->nbChars;
1.59      veillard 4136:                /*
                   4137:                 * Sometimes DOCTYPE arrives in the middle of the document
                   4138:                 */
                   4139:                if ((cur == '<') && (next == '!') &&
                   4140:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   4141:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   4142:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   4143:                    (UPP(8) == 'E')) {
                   4144:                    if ((!terminate) &&
                   4145:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
                   4146:                        goto done;
                   4147:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4148:                        ctxt->sax->error(ctxt->userData,
                   4149:                             "Misplaced DOCTYPE declaration\n");
                   4150:                    ctxt->wellFormed = 0;
                   4151:                    htmlParseDocTypeDecl(ctxt);
                   4152:                } else if ((cur == '<') && (next == '!') &&
1.31      daniel   4153:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   4154:                    if ((!terminate) &&
                   4155:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   4156:                        goto done;
                   4157: #ifdef DEBUG_PUSH
                   4158:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   4159: #endif
                   4160:                    htmlParseComment(ctxt);
                   4161:                    ctxt->instate = XML_PARSER_CONTENT;
                   4162:                } else if ((cur == '<') && (next == '!') && (avail < 4)) {
                   4163:                    goto done;
                   4164:                } else if ((cur == '<') && (next == '/')) {
                   4165:                    ctxt->instate = XML_PARSER_END_TAG;
                   4166:                    ctxt->checkIndex = 0;
                   4167: #ifdef DEBUG_PUSH
                   4168:                    fprintf(stderr, "HPP: entering END_TAG\n");
                   4169: #endif
                   4170:                    break;
                   4171:                } else if (cur == '<') {
                   4172:                    ctxt->instate = XML_PARSER_START_TAG;
                   4173:                    ctxt->checkIndex = 0;
                   4174: #ifdef DEBUG_PUSH
                   4175:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   4176: #endif
                   4177:                    break;
                   4178:                } else if (cur == '&') {
1.32      daniel   4179:                    if ((!terminate) &&
                   4180:                        (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
1.31      daniel   4181:                        goto done;
                   4182: #ifdef DEBUG_PUSH
                   4183:                    fprintf(stderr, "HPP: Parsing Reference\n");
                   4184: #endif
                   4185:                    /* TODO: check generation of subtrees if noent !!! */
                   4186:                    htmlParseReference(ctxt);
                   4187:                } else {
                   4188:                    /* TODO Avoid the extra copy, handle directly !!!!!! */
                   4189:                    /*
                   4190:                     * Goal of the following test is :
                   4191:                     *  - minimize calls to the SAX 'character' callback
                   4192:                     *    when they are mergeable
                   4193:                     */
                   4194:                    if ((ctxt->inputNr == 1) &&
                   4195:                        (avail < HTML_PARSER_BIG_BUFFER_SIZE)) {
1.32      daniel   4196:                        if ((!terminate) &&
                   4197:                            (htmlParseLookupSequence(ctxt, '<', 0, 0) < 0))
1.31      daniel   4198:                            goto done;
                   4199:                     }
                   4200:                    ctxt->checkIndex = 0;
                   4201: #ifdef DEBUG_PUSH
                   4202:                    fprintf(stderr, "HPP: Parsing char data\n");
                   4203: #endif
                   4204:                    htmlParseCharData(ctxt, 0);
                   4205:                }
1.56      veillard 4206:                if (cons == ctxt->nbChars) {
                   4207:                    if (ctxt->node != NULL) {
                   4208:                        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4209:                            ctxt->sax->error(ctxt->userData,
                   4210:                                 "detected an error in element content\n");
                   4211:                        ctxt->wellFormed = 0;
                   4212:                    }
1.70      veillard 4213:                    NEXT;
1.56      veillard 4214:                    break;
                   4215:                }
                   4216: 
1.31      daniel   4217:                break;
1.56      veillard 4218:            }
1.31      daniel   4219:             case XML_PARSER_END_TAG:
                   4220:                if (avail < 2)
                   4221:                    goto done;
1.32      daniel   4222:                if ((!terminate) &&
                   4223:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   4224:                    goto done;
                   4225:                htmlParseEndTag(ctxt);
                   4226:                if (ctxt->nameNr == 0) {
                   4227:                    ctxt->instate = XML_PARSER_EPILOG;
                   4228:                } else {
                   4229:                    ctxt->instate = XML_PARSER_CONTENT;
                   4230:                }
                   4231:                ctxt->checkIndex = 0;
                   4232: #ifdef DEBUG_PUSH
                   4233:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4234: #endif
                   4235:                break;
                   4236:             case XML_PARSER_CDATA_SECTION:
                   4237:                fprintf(stderr, "HPP: internal error, state == CDATA\n");
                   4238:                ctxt->instate = XML_PARSER_CONTENT;
                   4239:                ctxt->checkIndex = 0;
                   4240: #ifdef DEBUG_PUSH
                   4241:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4242: #endif
                   4243:                break;
                   4244:             case XML_PARSER_DTD:
                   4245:                fprintf(stderr, "HPP: internal error, state == DTD\n");
                   4246:                ctxt->instate = XML_PARSER_CONTENT;
                   4247:                ctxt->checkIndex = 0;
                   4248: #ifdef DEBUG_PUSH
                   4249:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4250: #endif
                   4251:                break;
                   4252:             case XML_PARSER_COMMENT:
                   4253:                fprintf(stderr, "HPP: internal error, state == COMMENT\n");
                   4254:                ctxt->instate = XML_PARSER_CONTENT;
                   4255:                ctxt->checkIndex = 0;
                   4256: #ifdef DEBUG_PUSH
                   4257:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4258: #endif
                   4259:                break;
                   4260:             case XML_PARSER_PI:
                   4261:                fprintf(stderr, "HPP: internal error, state == PI\n");
                   4262:                ctxt->instate = XML_PARSER_CONTENT;
                   4263:                ctxt->checkIndex = 0;
                   4264: #ifdef DEBUG_PUSH
                   4265:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4266: #endif
                   4267:                break;
                   4268:             case XML_PARSER_ENTITY_DECL:
                   4269:                fprintf(stderr, "HPP: internal error, state == ENTITY_DECL\n");
                   4270:                ctxt->instate = XML_PARSER_CONTENT;
                   4271:                ctxt->checkIndex = 0;
                   4272: #ifdef DEBUG_PUSH
                   4273:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4274: #endif
                   4275:                break;
                   4276:             case XML_PARSER_ENTITY_VALUE:
                   4277:                fprintf(stderr, "HPP: internal error, state == ENTITY_VALUE\n");
                   4278:                ctxt->instate = XML_PARSER_CONTENT;
                   4279:                ctxt->checkIndex = 0;
                   4280: #ifdef DEBUG_PUSH
                   4281:                fprintf(stderr, "HPP: entering DTD\n");
                   4282: #endif
                   4283:                break;
                   4284:             case XML_PARSER_ATTRIBUTE_VALUE:
                   4285:                fprintf(stderr, "HPP: internal error, state == ATTRIBUTE_VALUE\n");
                   4286:                ctxt->instate = XML_PARSER_START_TAG;
                   4287:                ctxt->checkIndex = 0;
                   4288: #ifdef DEBUG_PUSH
                   4289:                fprintf(stderr, "HPP: entering START_TAG\n");
1.53      veillard 4290: #endif
                   4291:                break;
                   4292:            case XML_PARSER_SYSTEM_LITERAL:
                   4293:                fprintf(stderr, "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
                   4294:                ctxt->instate = XML_PARSER_CONTENT;
                   4295:                ctxt->checkIndex = 0;
                   4296: #ifdef DEBUG_PUSH
                   4297:                fprintf(stderr, "HPP: entering CONTENT\n");
1.31      daniel   4298: #endif
                   4299:                break;
                   4300:        }
                   4301:     }
                   4302: done:    
1.47      daniel   4303:     if ((avail == 0) && (terminate)) {
                   4304:        htmlAutoClose(ctxt, NULL);
1.54      veillard 4305:        if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   4306:            /*
                   4307:             * SAX: end of the document processing.
                   4308:             */
1.47      daniel   4309:            ctxt->instate = XML_PARSER_EOF;
1.54      veillard 4310:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4311:                ctxt->sax->endDocument(ctxt->userData);
                   4312:        }
1.59      veillard 4313:     }
                   4314:     if ((ctxt->myDoc != NULL) &&
                   4315:        ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
                   4316:         (ctxt->instate == XML_PARSER_EPILOG))) {
                   4317:        xmlDtdPtr dtd;
                   4318:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   4319:        if (dtd == NULL)
                   4320:            ctxt->myDoc->intSubset = 
                   4321:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   4322:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   4323:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
1.47      daniel   4324:     }
1.31      daniel   4325: #ifdef DEBUG_PUSH
                   4326:     fprintf(stderr, "HPP: done %d\n", ret);
                   4327: #endif
                   4328:     return(ret);
                   4329: }
                   4330: 
                   4331: /**
1.32      daniel   4332:  * htmlParseTry:
                   4333:  * @ctxt:  an HTML parser context
                   4334:  *
                   4335:  * Try to progress on parsing
                   4336:  *
                   4337:  * Returns zero if no parsing was possible
                   4338:  */
                   4339: int
                   4340: htmlParseTry(htmlParserCtxtPtr ctxt) {
                   4341:     return(htmlParseTryOrFinish(ctxt, 0));
                   4342: }
                   4343: 
                   4344: /**
1.31      daniel   4345:  * htmlParseChunk:
                   4346:  * @ctxt:  an XML parser context
                   4347:  * @chunk:  an char array
                   4348:  * @size:  the size in byte of the chunk
                   4349:  * @terminate:  last chunk indicator
                   4350:  *
                   4351:  * Parse a Chunk of memory
                   4352:  *
                   4353:  * Returns zero if no error, the xmlParserErrors otherwise.
                   4354:  */
                   4355: int
                   4356: htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
                   4357:               int terminate) {
                   4358:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4359:         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
                   4360:        int base = ctxt->input->base - ctxt->input->buf->buffer->content;
                   4361:        int cur = ctxt->input->cur - ctxt->input->base;
                   4362:        
                   4363:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4364:        ctxt->input->base = ctxt->input->buf->buffer->content + base;
                   4365:        ctxt->input->cur = ctxt->input->base + cur;
                   4366: #ifdef DEBUG_PUSH
                   4367:        fprintf(stderr, "HPP: pushed %d\n", size);
                   4368: #endif
                   4369: 
1.34      daniel   4370:        if ((terminate) || (ctxt->input->buf->buffer->use > 80))
                   4371:            htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4372:     } else if (ctxt->instate != XML_PARSER_EOF) {
                   4373:        xmlParserInputBufferPush(ctxt->input->buf, 0, "");
1.32      daniel   4374:         htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4375:     }
1.31      daniel   4376:     if (terminate) {
                   4377:        if ((ctxt->instate != XML_PARSER_EOF) &&
                   4378:            (ctxt->instate != XML_PARSER_EPILOG) &&
                   4379:            (ctxt->instate != XML_PARSER_MISC)) {
1.67      veillard 4380:            ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   4381:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4382:                ctxt->sax->error(ctxt->userData,
                   4383:                    "Extra content at the end of the document\n");
                   4384:            ctxt->wellFormed = 0;
                   4385:        } 
                   4386:        if (ctxt->instate != XML_PARSER_EOF) {
                   4387:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4388:                ctxt->sax->endDocument(ctxt->userData);
                   4389:        }
                   4390:        ctxt->instate = XML_PARSER_EOF;
                   4391:     }
                   4392:     return((xmlParserErrors) ctxt->errNo);           
                   4393: }
                   4394: 
                   4395: /************************************************************************
                   4396:  *                                                                     *
                   4397:  *                     User entry points                               *
                   4398:  *                                                                     *
                   4399:  ************************************************************************/
                   4400: 
                   4401: /**
                   4402:  * htmlCreatePushParserCtxt :
                   4403:  * @sax:  a SAX handler
                   4404:  * @user_data:  The user data returned on SAX callbacks
                   4405:  * @chunk:  a pointer to an array of chars
                   4406:  * @size:  number of chars in the array
                   4407:  * @filename:  an optional file name or URI
                   4408:  * @enc:  an optional encoding
                   4409:  *
                   4410:  * Create a parser context for using the HTML parser in push mode
                   4411:  * To allow content encoding detection, @size should be >= 4
                   4412:  * The value of @filename is used for fetching external entities
                   4413:  * and error/warning reports.
                   4414:  *
                   4415:  * Returns the new parser context or NULL
                   4416:  */
                   4417: htmlParserCtxtPtr
                   4418: htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, 
                   4419:                          const char *chunk, int size, const char *filename,
                   4420:                         xmlCharEncoding enc) {
                   4421:     htmlParserCtxtPtr ctxt;
                   4422:     htmlParserInputPtr inputStream;
                   4423:     xmlParserInputBufferPtr buf;
                   4424: 
                   4425:     buf = xmlAllocParserInputBuffer(enc);
                   4426:     if (buf == NULL) return(NULL);
                   4427: 
                   4428:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
                   4429:     if (ctxt == NULL) {
                   4430:        xmlFree(buf);
                   4431:        return(NULL);
                   4432:     }
                   4433:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   4434:     htmlInitParserCtxt(ctxt);
                   4435:     if (sax != NULL) {
                   4436:        if (ctxt->sax != &htmlDefaultSAXHandler)
                   4437:            xmlFree(ctxt->sax);
                   4438:        ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
                   4439:        if (ctxt->sax == NULL) {
                   4440:            xmlFree(buf);
                   4441:            xmlFree(ctxt);
                   4442:            return(NULL);
                   4443:        }
                   4444:        memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
                   4445:        if (user_data != NULL)
                   4446:            ctxt->userData = user_data;
                   4447:     }  
                   4448:     if (filename == NULL) {
                   4449:        ctxt->directory = NULL;
                   4450:     } else {
                   4451:         ctxt->directory = xmlParserGetDirectory(filename);
                   4452:     }
                   4453: 
                   4454:     inputStream = htmlNewInputStream(ctxt);
                   4455:     if (inputStream == NULL) {
                   4456:        xmlFreeParserCtxt(ctxt);
                   4457:        return(NULL);
                   4458:     }
                   4459: 
                   4460:     if (filename == NULL)
                   4461:        inputStream->filename = NULL;
                   4462:     else
                   4463:        inputStream->filename = xmlMemStrdup(filename);
                   4464:     inputStream->buf = buf;
                   4465:     inputStream->base = inputStream->buf->buffer->content;
                   4466:     inputStream->cur = inputStream->buf->buffer->content;
                   4467: 
                   4468:     inputPush(ctxt, inputStream);
                   4469: 
                   4470:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4471:         (ctxt->input->buf != NULL))  {       
                   4472:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4473: #ifdef DEBUG_PUSH
                   4474:        fprintf(stderr, "HPP: pushed %d\n", size);
                   4475: #endif
                   4476:     }
                   4477: 
                   4478:     return(ctxt);
                   4479: }
1.1       daniel   4480: 
                   4481: /**
                   4482:  * htmlSAXParseDoc :
1.14      daniel   4483:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4484:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4485:  * @sax:  the SAX handler block
                   4486:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4487:  *
                   4488:  * parse an HTML in-memory document and build a tree.
                   4489:  * It use the given SAX function block to handle the parsing callback.
                   4490:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4491:  * 
                   4492:  * Returns the resulting document tree
                   4493:  */
                   4494: 
                   4495: htmlDocPtr
1.14      daniel   4496: htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
1.1       daniel   4497:     htmlDocPtr ret;
                   4498:     htmlParserCtxtPtr ctxt;
                   4499: 
                   4500:     if (cur == NULL) return(NULL);
                   4501: 
                   4502: 
                   4503:     ctxt = htmlCreateDocParserCtxt(cur, encoding);
                   4504:     if (ctxt == NULL) return(NULL);
                   4505:     if (sax != NULL) { 
                   4506:         ctxt->sax = sax;
                   4507:         ctxt->userData = userData;
                   4508:     }
                   4509: 
                   4510:     htmlParseDocument(ctxt);
                   4511:     ret = ctxt->myDoc;
                   4512:     if (sax != NULL) {
                   4513:        ctxt->sax = NULL;
                   4514:        ctxt->userData = NULL;
                   4515:     }
                   4516:     htmlFreeParserCtxt(ctxt);
                   4517:     
                   4518:     return(ret);
                   4519: }
                   4520: 
                   4521: /**
                   4522:  * htmlParseDoc :
1.14      daniel   4523:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4524:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4525:  *
                   4526:  * parse an HTML in-memory document and build a tree.
                   4527:  * 
                   4528:  * Returns the resulting document tree
                   4529:  */
                   4530: 
                   4531: htmlDocPtr
1.14      daniel   4532: htmlParseDoc(xmlChar *cur, const char *encoding) {
1.1       daniel   4533:     return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
                   4534: }
                   4535: 
                   4536: 
                   4537: /**
                   4538:  * htmlCreateFileParserCtxt :
                   4539:  * @filename:  the filename
                   4540:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4541:  *
                   4542:  * Create a parser context for a file content. 
                   4543:  * Automatic support for ZLIB/Compress compressed document is provided
                   4544:  * by default if found at compile-time.
                   4545:  *
                   4546:  * Returns the new parser context or NULL
                   4547:  */
                   4548: htmlParserCtxtPtr
                   4549: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
                   4550: {
                   4551:     htmlParserCtxtPtr ctxt;
                   4552:     htmlParserInputPtr inputStream;
1.5       daniel   4553:     xmlParserInputBufferPtr buf;
1.1       daniel   4554:     /* htmlCharEncoding enc; */
                   4555: 
1.5       daniel   4556:     buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
                   4557:     if (buf == NULL) return(NULL);
1.1       daniel   4558: 
1.11      daniel   4559:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   4560:     if (ctxt == NULL) {
                   4561:         perror("malloc");
                   4562:        return(NULL);
                   4563:     }
1.19      daniel   4564:     memset(ctxt, 0, sizeof(htmlParserCtxt));
1.1       daniel   4565:     htmlInitParserCtxt(ctxt);
1.11      daniel   4566:     inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   4567:     if (inputStream == NULL) {
                   4568:         perror("malloc");
1.11      daniel   4569:        xmlFree(ctxt);
1.1       daniel   4570:        return(NULL);
                   4571:     }
1.19      daniel   4572:     memset(inputStream, 0, sizeof(htmlParserInput));
1.1       daniel   4573: 
1.11      daniel   4574:     inputStream->filename = xmlMemStrdup(filename);
1.1       daniel   4575:     inputStream->line = 1;
                   4576:     inputStream->col = 1;
1.5       daniel   4577:     inputStream->buf = buf;
1.21      daniel   4578:     inputStream->directory = NULL;
1.1       daniel   4579: 
1.5       daniel   4580:     inputStream->base = inputStream->buf->buffer->content;
                   4581:     inputStream->cur = inputStream->buf->buffer->content;
                   4582:     inputStream->free = NULL;
1.1       daniel   4583: 
                   4584:     inputPush(ctxt, inputStream);
                   4585:     return(ctxt);
                   4586: }
                   4587: 
                   4588: /**
                   4589:  * htmlSAXParseFile :
                   4590:  * @filename:  the filename
                   4591:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4592:  * @sax:  the SAX handler block
                   4593:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4594:  *
                   4595:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4596:  * compressed document is provided by default if found at compile-time.
                   4597:  * It use the given SAX function block to handle the parsing callback.
                   4598:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4599:  *
                   4600:  * Returns the resulting document tree
                   4601:  */
                   4602: 
                   4603: htmlDocPtr
                   4604: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, 
                   4605:                  void *userData) {
                   4606:     htmlDocPtr ret;
                   4607:     htmlParserCtxtPtr ctxt;
1.57      veillard 4608:     htmlSAXHandlerPtr oldsax = NULL;
1.1       daniel   4609: 
                   4610:     ctxt = htmlCreateFileParserCtxt(filename, encoding);
                   4611:     if (ctxt == NULL) return(NULL);
                   4612:     if (sax != NULL) {
1.55      veillard 4613:        oldsax = ctxt->sax;
1.1       daniel   4614:         ctxt->sax = sax;
                   4615:         ctxt->userData = userData;
                   4616:     }
                   4617: 
                   4618:     htmlParseDocument(ctxt);
                   4619: 
                   4620:     ret = ctxt->myDoc;
                   4621:     if (sax != NULL) {
1.55      veillard 4622:         ctxt->sax = oldsax;
1.1       daniel   4623:         ctxt->userData = NULL;
                   4624:     }
                   4625:     htmlFreeParserCtxt(ctxt);
                   4626:     
                   4627:     return(ret);
                   4628: }
                   4629: 
                   4630: /**
                   4631:  * htmlParseFile :
                   4632:  * @filename:  the filename
                   4633:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4634:  *
                   4635:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4636:  * compressed document is provided by default if found at compile-time.
                   4637:  *
                   4638:  * Returns the resulting document tree
                   4639:  */
                   4640: 
                   4641: htmlDocPtr
                   4642: htmlParseFile(const char *filename, const char *encoding) {
                   4643:     return(htmlSAXParseFile(filename, encoding, NULL, NULL));
                   4644: }
1.39      daniel   4645: 
                   4646: #endif /* LIBXML_HTML_ENABLED */

Webmaster