Annotation of XML/HTMLparser.c, revision 1.69

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

Webmaster