Annotation of XML/HTMLparser.c, revision 1.68

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:
                    601:  * @tag:  The tag name
                    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: 
                   2666:     encoding = xmlStrstr(attvalue, BAD_CAST"charset=");
                   2667:     if (encoding == NULL) 
                   2668:        encoding = xmlStrstr(attvalue, BAD_CAST"Charset=");
                   2669:     if (encoding == NULL) 
                   2670:        encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET=");
                   2671:     if (encoding != NULL) {
                   2672:        encoding += 8;
                   2673:     } else {
                   2674:        encoding = xmlStrstr(attvalue, BAD_CAST"charset =");
                   2675:        if (encoding == NULL) 
                   2676:            encoding = xmlStrstr(attvalue, BAD_CAST"Charset =");
                   2677:        if (encoding == NULL) 
                   2678:            encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET =");
                   2679:        if (encoding != NULL)
                   2680:            encoding += 9;
                   2681:     }
                   2682:     if (encoding != NULL) {
                   2683:        xmlCharEncoding enc;
                   2684:        xmlCharEncodingHandlerPtr handler;
                   2685: 
                   2686:        while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
                   2687: 
                   2688:        if (ctxt->input->encoding != NULL)
                   2689:            xmlFree((xmlChar *) ctxt->input->encoding);
                   2690:        ctxt->input->encoding = xmlStrdup(encoding);
                   2691: 
                   2692:        enc = xmlParseCharEncoding((const char *) encoding);
                   2693:        /*
                   2694:         * registered set of known encodings
                   2695:         */
                   2696:        if (enc != XML_CHAR_ENCODING_ERROR) {
                   2697:            xmlSwitchEncoding(ctxt, enc);
1.53      veillard 2698:            ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2699:        } else {
                   2700:            /*
                   2701:             * fallback for unknown encodings
                   2702:             */
                   2703:            handler = xmlFindCharEncodingHandler((const char *) encoding);
                   2704:            if (handler != NULL) {
                   2705:                xmlSwitchToEncoding(ctxt, handler);
1.54      veillard 2706:                ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2707:            } else {
                   2708:                ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
                   2709:            }
                   2710:        }
1.54      veillard 2711: 
                   2712:        if ((ctxt->input->buf != NULL) &&
                   2713:            (ctxt->input->buf->encoder != NULL) &&
                   2714:            (ctxt->input->buf->raw != NULL) &&
                   2715:            (ctxt->input->buf->buffer != NULL)) {
                   2716:            int nbchars;
1.56      veillard 2717:            int processed;
1.54      veillard 2718: 
                   2719:            /*
                   2720:             * convert as much as possible to the parser reading buffer.
                   2721:             */
1.56      veillard 2722:            processed = ctxt->input->cur - ctxt->input->base;
                   2723:            xmlBufferShrink(ctxt->input->buf->buffer, processed);
1.54      veillard 2724:            nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
                   2725:                                       ctxt->input->buf->buffer,
                   2726:                                       ctxt->input->buf->raw);
                   2727:            if (nbchars < 0) {
1.67      veillard 2728:                ctxt->errNo = XML_ERR_INVALID_ENCODING;
1.54      veillard 2729:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2730:                    ctxt->sax->error(ctxt->userData, 
                   2731:                     "htmlCheckEncoding: encoder error\n");
                   2732:            }
1.56      veillard 2733:            ctxt->input->base =
                   2734:            ctxt->input->cur = ctxt->input->buf->buffer->content;
1.54      veillard 2735:        }
1.47      daniel   2736:     }
                   2737: }
                   2738: 
                   2739: /**
                   2740:  * htmlCheckMeta:
                   2741:  * @ctxt:  an HTML parser context
                   2742:  * @atts:  the attributes values
                   2743:  *
                   2744:  * Checks an attributes from a Meta tag
                   2745:  */
                   2746: void
                   2747: htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
                   2748:     int i;
                   2749:     const xmlChar *att, *value;
                   2750:     int http = 0;
                   2751:     const xmlChar *content = NULL;
                   2752: 
                   2753:     if ((ctxt == NULL) || (atts == NULL))
                   2754:        return;
                   2755: 
                   2756:     i = 0;
                   2757:     att = atts[i++];
                   2758:     while (att != NULL) {
                   2759:        value = atts[i++];
                   2760:        if ((value != NULL) &&
                   2761:            ((!xmlStrcmp(att, BAD_CAST"http-equiv")) ||
                   2762:             (!xmlStrcmp(att, BAD_CAST"Http-Equiv")) ||
                   2763:             (!xmlStrcmp(att, BAD_CAST"HTTP-EQUIV"))) &&
                   2764:            ((!xmlStrcmp(value, BAD_CAST"Content-Type")) ||
                   2765:             (!xmlStrcmp(value, BAD_CAST"content-type")) ||
                   2766:             (!xmlStrcmp(value, BAD_CAST"CONTENT-TYPE"))))
                   2767:            http = 1;
                   2768:        else if ((value != NULL) &&
                   2769:                 ((!xmlStrcmp(att, BAD_CAST"content")) ||
                   2770:                  (!xmlStrcmp(att, BAD_CAST"Content")) ||
                   2771:                  (!xmlStrcmp(att, BAD_CAST"CONTENT"))))
                   2772:            content = value;
                   2773:        att = atts[i++];
                   2774:     }
                   2775:     if ((http) && (content != NULL))
                   2776:        htmlCheckEncoding(ctxt, content);
                   2777: 
                   2778: }
                   2779: 
                   2780: /**
1.1       daniel   2781:  * htmlParseStartTag:
                   2782:  * @ctxt:  an HTML parser context
                   2783:  * 
                   2784:  * parse a start of tag either for rule element or
                   2785:  * EmptyElement. In both case we don't parse the tag closing chars.
                   2786:  *
                   2787:  * [40] STag ::= '<' Name (S Attribute)* S? '>'
                   2788:  *
                   2789:  * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
                   2790:  *
                   2791:  * With namespace:
                   2792:  *
                   2793:  * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
                   2794:  *
                   2795:  * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
                   2796:  *
                   2797:  */
                   2798: 
1.18      daniel   2799: void
1.1       daniel   2800: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2801:     xmlChar *name;
                   2802:     xmlChar *attname;
                   2803:     xmlChar *attvalue;
                   2804:     const xmlChar **atts = NULL;
1.1       daniel   2805:     int nbatts = 0;
                   2806:     int maxatts = 0;
1.47      daniel   2807:     int meta = 0;
1.1       daniel   2808:     int i;
                   2809: 
1.18      daniel   2810:     if (CUR != '<') return;
1.1       daniel   2811:     NEXT;
                   2812: 
1.19      daniel   2813:     GROW;
1.1       daniel   2814:     name = htmlParseHTMLName(ctxt);
                   2815:     if (name == NULL) {
                   2816:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2817:            ctxt->sax->error(ctxt->userData, 
                   2818:             "htmlParseStartTag: invalid element name\n");
                   2819:        ctxt->wellFormed = 0;
1.18      daniel   2820:         return;
1.1       daniel   2821:     }
1.47      daniel   2822:     if (!xmlStrcmp(name, BAD_CAST"meta"))
                   2823:        meta = 1;
1.1       daniel   2824: 
                   2825:     /*
                   2826:      * Check for auto-closure of HTML elements.
                   2827:      */
                   2828:     htmlAutoClose(ctxt, name);
1.43      daniel   2829: 
                   2830:     /*
                   2831:      * Check for implied HTML elements.
                   2832:      */
                   2833:     htmlCheckImplied(ctxt, name);
1.1       daniel   2834: 
                   2835:     /*
                   2836:      * Now parse the attributes, it ends up with the ending
                   2837:      *
                   2838:      * (S Attribute)* S?
                   2839:      */
                   2840:     SKIP_BLANKS;
                   2841:     while ((IS_CHAR(CUR)) &&
                   2842:            (CUR != '>') && 
                   2843:           ((CUR != '/') || (NXT(1) != '>'))) {
1.26      daniel   2844:        long cons = ctxt->nbChars;
1.1       daniel   2845: 
1.19      daniel   2846:        GROW;
1.1       daniel   2847:        attname = htmlParseAttribute(ctxt, &attvalue);
1.31      daniel   2848:         if (attname != NULL) {
1.47      daniel   2849: 
1.1       daniel   2850:            /*
                   2851:             * Well formedness requires at most one declaration of an attribute
                   2852:             */
                   2853:            for (i = 0; i < nbatts;i += 2) {
                   2854:                if (!xmlStrcmp(atts[i], attname)) {
                   2855:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.19      daniel   2856:                        ctxt->sax->error(ctxt->userData,
                   2857:                                         "Attribute %s redefined\n",
                   2858:                                         attname);
1.1       daniel   2859:                    ctxt->wellFormed = 0;
1.11      daniel   2860:                    xmlFree(attname);
1.31      daniel   2861:                    if (attvalue != NULL)
                   2862:                        xmlFree(attvalue);
1.19      daniel   2863:                    goto failed;
1.1       daniel   2864:                }
                   2865:            }
                   2866: 
                   2867:            /*
                   2868:             * Add the pair to atts
                   2869:             */
                   2870:            if (atts == NULL) {
                   2871:                maxatts = 10;
1.14      daniel   2872:                atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
1.1       daniel   2873:                if (atts == NULL) {
                   2874:                    fprintf(stderr, "malloc of %ld byte failed\n",
1.14      daniel   2875:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2876:                    if (name != NULL) xmlFree(name);
                   2877:                    return;
1.1       daniel   2878:                }
1.23      daniel   2879:            } else if (nbatts + 4 > maxatts) {
1.1       daniel   2880:                maxatts *= 2;
1.14      daniel   2881:                atts = (const xmlChar **) xmlRealloc(atts, maxatts * sizeof(xmlChar *));
1.1       daniel   2882:                if (atts == NULL) {
                   2883:                    fprintf(stderr, "realloc of %ld byte failed\n",
1.14      daniel   2884:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2885:                    if (name != NULL) xmlFree(name);
                   2886:                    return;
1.1       daniel   2887:                }
                   2888:            }
                   2889:            atts[nbatts++] = attname;
                   2890:            atts[nbatts++] = attvalue;
                   2891:            atts[nbatts] = NULL;
                   2892:            atts[nbatts + 1] = NULL;
                   2893:        }
                   2894: 
1.19      daniel   2895: failed:
1.1       daniel   2896:        SKIP_BLANKS;
1.26      daniel   2897:         if (cons == ctxt->nbChars) {
1.1       daniel   2898:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2899:                ctxt->sax->error(ctxt->userData, 
                   2900:                 "htmlParseStartTag: problem parsing attributes\n");
                   2901:            ctxt->wellFormed = 0;
                   2902:            break;
                   2903:        }
                   2904:     }
                   2905: 
                   2906:     /*
1.47      daniel   2907:      * Handle specific association to the META tag
                   2908:      */
                   2909:     if (meta)
                   2910:        htmlCheckMeta(ctxt, atts);
                   2911: 
                   2912:     /*
1.1       daniel   2913:      * SAX: Start of Element !
                   2914:      */
1.15      daniel   2915:     htmlnamePush(ctxt, xmlStrdup(name));
1.18      daniel   2916: #ifdef DEBUG
                   2917:     fprintf(stderr,"Start of element %s: pushed %s\n", name, ctxt->name);
                   2918: #endif    
1.1       daniel   2919:     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                   2920:         ctxt->sax->startElement(ctxt->userData, name, atts);
                   2921: 
                   2922:     if (atts != NULL) {
1.31      daniel   2923:         for (i = 0;i < nbatts;i++) {
                   2924:            if (atts[i] != NULL)
                   2925:                xmlFree((xmlChar *) atts[i]);
                   2926:        }
1.45      daniel   2927:        xmlFree((void *) atts);
1.1       daniel   2928:     }
1.18      daniel   2929:     if (name != NULL) xmlFree(name);
1.1       daniel   2930: }
                   2931: 
                   2932: /**
                   2933:  * htmlParseEndTag:
                   2934:  * @ctxt:  an HTML parser context
                   2935:  *
                   2936:  * parse an end of tag
                   2937:  *
                   2938:  * [42] ETag ::= '</' Name S? '>'
                   2939:  *
                   2940:  * With namespace
                   2941:  *
                   2942:  * [NS 9] ETag ::= '</' QName S? '>'
                   2943:  */
                   2944: 
                   2945: void
1.18      daniel   2946: htmlParseEndTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2947:     xmlChar *name;
1.15      daniel   2948:     xmlChar *oldname;
1.1       daniel   2949:     int i;
                   2950: 
                   2951:     if ((CUR != '<') || (NXT(1) != '/')) {
                   2952:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2953:            ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
                   2954:        ctxt->wellFormed = 0;
                   2955:        return;
                   2956:     }
                   2957:     SKIP(2);
                   2958: 
                   2959:     name = htmlParseHTMLName(ctxt);
1.24      daniel   2960:     if (name == NULL) return;
1.1       daniel   2961: 
                   2962:     /*
                   2963:      * We should definitely be at the ending "S? '>'" part
                   2964:      */
                   2965:     SKIP_BLANKS;
                   2966:     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
                   2967:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2968:            ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
                   2969:        ctxt->wellFormed = 0;
                   2970:     } else
                   2971:        NEXT;
                   2972: 
                   2973:     /*
1.18      daniel   2974:      * If the name read is not one of the element in the parsing stack
                   2975:      * then return, it's just an error.
1.1       daniel   2976:      */
1.18      daniel   2977:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
                   2978:         if (!xmlStrcmp(name, ctxt->nameTab[i])) break;
1.1       daniel   2979:     }
                   2980:     if (i < 0) {
                   2981:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.18      daniel   2982:            ctxt->sax->error(ctxt->userData,
                   2983:             "Unexpected end tag : %s\n", name);
1.11      daniel   2984:        xmlFree(name);
1.1       daniel   2985:        ctxt->wellFormed = 0;
                   2986:        return;
                   2987:     }
                   2988: 
1.18      daniel   2989: 
1.1       daniel   2990:     /*
                   2991:      * Check for auto-closure of HTML elements.
                   2992:      */
1.18      daniel   2993: 
1.1       daniel   2994:     htmlAutoCloseOnClose(ctxt, name);
                   2995: 
                   2996:     /*
                   2997:      * Well formedness constraints, opening and closing must match.
                   2998:      * With the exception that the autoclose may have popped stuff out
                   2999:      * of the stack.
                   3000:      */
1.18      daniel   3001:     if (xmlStrcmp(name, ctxt->name)) {
                   3002: #ifdef DEBUG
                   3003:        fprintf(stderr,"End of tag %s: expecting %s\n", name, ctxt->name);
                   3004: #endif
1.15      daniel   3005:         if ((ctxt->name != NULL) && 
                   3006:            (xmlStrcmp(ctxt->name, name))) {
1.1       daniel   3007:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3008:                ctxt->sax->error(ctxt->userData,
                   3009:                 "Opening and ending tag mismatch: %s and %s\n",
1.15      daniel   3010:                                 name, ctxt->name);
1.1       daniel   3011:            ctxt->wellFormed = 0;
                   3012:         }
                   3013:     }
                   3014: 
                   3015:     /*
                   3016:      * SAX: End of Tag
                   3017:      */
1.15      daniel   3018:     oldname = ctxt->name;
1.24      daniel   3019:     if ((oldname != NULL) && (!xmlStrcmp(oldname, name))) {
1.18      daniel   3020:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3021:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3022:        oldname = htmlnamePop(ctxt);
1.18      daniel   3023:        if (oldname != NULL) {
                   3024: #ifdef DEBUG
                   3025:            fprintf(stderr,"End of tag %s: popping out %s\n", name, oldname);
                   3026: #endif
                   3027:            xmlFree(oldname);
                   3028: #ifdef DEBUG
                   3029:        } else {
                   3030:            fprintf(stderr,"End of tag %s: stack empty !!!\n", name);
                   3031: #endif
                   3032:        }
                   3033:     }
1.1       daniel   3034: 
                   3035:     if (name != NULL)
1.11      daniel   3036:        xmlFree(name);
1.1       daniel   3037: 
                   3038:     return;
                   3039: }
                   3040: 
                   3041: 
                   3042: /**
                   3043:  * htmlParseReference:
                   3044:  * @ctxt:  an HTML parser context
                   3045:  * 
                   3046:  * parse and handle entity references in content,
                   3047:  * this will end-up in a call to character() since this is either a
                   3048:  * CharRef, or a predefined entity.
                   3049:  */
                   3050: void
                   3051: htmlParseReference(htmlParserCtxtPtr ctxt) {
                   3052:     htmlEntityDescPtr ent;
1.52      veillard 3053:     xmlChar out[6];
1.14      daniel   3054:     xmlChar *name;
1.1       daniel   3055:     if (CUR != '&') return;
                   3056: 
                   3057:     if (NXT(1) == '#') {
1.52      veillard 3058:        unsigned int c;
                   3059:        int bits, i = 0;
                   3060: 
                   3061:        c = htmlParseCharRef(ctxt);
                   3062:         if      (c <    0x80) { out[i++]= c;                bits= -6; }
                   3063:         else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3064:         else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3065:         else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3066:  
                   3067:         for ( ; bits >= 0; bits-= 6) {
                   3068:             out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3069:         }
                   3070:        out[i] = 0;
                   3071: 
1.59      veillard 3072:        htmlCheckParagraph(ctxt);
1.1       daniel   3073:        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3074:            ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3075:     } else {
                   3076:        ent = htmlParseEntityRef(ctxt, &name);
1.32      daniel   3077:        if (name == NULL) {
1.59      veillard 3078:            htmlCheckParagraph(ctxt);
1.58      veillard 3079:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   3080:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.32      daniel   3081:            return;
                   3082:        }
1.52      veillard 3083:        if ((ent == NULL) || (ent->value <= 0)) {
1.59      veillard 3084:            htmlCheckParagraph(ctxt);
1.1       daniel   3085:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
1.8       daniel   3086:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1       daniel   3087:                ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1.32      daniel   3088:                /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
1.1       daniel   3089:            }
                   3090:        } else {
1.52      veillard 3091:            unsigned int c;
                   3092:            int bits, i = 0;
                   3093: 
                   3094:            c = ent->value;
                   3095:            if      (c <    0x80)
                   3096:                    { out[i++]= c;                bits= -6; }
                   3097:            else if (c <   0x800)
                   3098:                    { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3099:            else if (c < 0x10000)
                   3100:                    { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3101:            else                 
                   3102:                    { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3103:      
                   3104:            for ( ; bits >= 0; bits-= 6) {
                   3105:                out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3106:            }
                   3107:            out[i] = 0;
                   3108: 
1.59      veillard 3109:            htmlCheckParagraph(ctxt);
1.1       daniel   3110:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3111:                ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3112:        }
1.11      daniel   3113:        xmlFree(name);
1.1       daniel   3114:     }
                   3115: }
                   3116: 
                   3117: /**
                   3118:  * htmlParseContent:
                   3119:  * @ctxt:  an HTML parser context
                   3120:  * @name:  the node name
                   3121:  *
                   3122:  * Parse a content: comment, sub-element, reference or text.
                   3123:  *
                   3124:  */
                   3125: 
                   3126: void
1.18      daniel   3127: htmlParseContent(htmlParserCtxtPtr ctxt) {
1.15      daniel   3128:     xmlChar *currentNode;
1.18      daniel   3129:     int depth;
1.1       daniel   3130: 
1.26      daniel   3131:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3132:     depth = ctxt->nameNr;
                   3133:     while (1) {
1.26      daniel   3134:        long cons = ctxt->nbChars;
1.1       daniel   3135: 
1.18      daniel   3136:         GROW;
                   3137:        /*
                   3138:         * Our tag or one of it's parent or children is ending.
                   3139:         */
                   3140:         if ((CUR == '<') && (NXT(1) == '/')) {
                   3141:            htmlParseEndTag(ctxt);
1.26      daniel   3142:            if (currentNode != NULL) xmlFree(currentNode);
1.18      daniel   3143:            return;
                   3144:         }
                   3145: 
                   3146:        /*
                   3147:         * Has this node been popped out during parsing of
                   3148:         * the next element
                   3149:         */
1.26      daniel   3150:         if ((xmlStrcmp(currentNode, ctxt->name)) &&
                   3151:            (depth >= ctxt->nameNr)) {
                   3152:            if (currentNode != NULL) xmlFree(currentNode);
                   3153:            return;
                   3154:        }
1.18      daniel   3155: 
1.1       daniel   3156:        /*
1.59      veillard 3157:         * Sometimes DOCTYPE arrives in the middle of the document
                   3158:         */
                   3159:        if ((CUR == '<') && (NXT(1) == '!') &&
                   3160:            (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3161:            (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3162:            (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3163:            (UPP(8) == 'E')) {
                   3164:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3165:                ctxt->sax->error(ctxt->userData,
                   3166:                     "Misplaced DOCTYPE declaration\n");
                   3167:            ctxt->wellFormed = 0;
                   3168:            htmlParseDocTypeDecl(ctxt);
                   3169:        }
                   3170: 
                   3171:        /*
1.1       daniel   3172:         * First case :  a comment
                   3173:         */
                   3174:        if ((CUR == '<') && (NXT(1) == '!') &&
                   3175:                 (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   3176:            htmlParseComment(ctxt);
1.1       daniel   3177:        }
                   3178: 
                   3179:        /*
                   3180:         * Second case :  a sub-element.
                   3181:         */
                   3182:        else if (CUR == '<') {
                   3183:            htmlParseElement(ctxt);
                   3184:        }
                   3185: 
                   3186:        /*
                   3187:         * Third case : a reference. If if has not been resolved,
                   3188:         *    parsing returns it's Name, create the node 
                   3189:         */
                   3190:        else if (CUR == '&') {
                   3191:            htmlParseReference(ctxt);
                   3192:        }
                   3193: 
                   3194:        /*
1.47      daniel   3195:         * Fourth : end of the resource
                   3196:         */
                   3197:        else if (CUR == 0) {
                   3198:            htmlAutoClose(ctxt, NULL);
                   3199:        }
                   3200: 
                   3201:        /*
1.1       daniel   3202:         * Last case, text. Note that References are handled directly.
                   3203:         */
                   3204:        else {
                   3205:            htmlParseCharData(ctxt, 0);
                   3206:        }
                   3207: 
1.26      daniel   3208:        if (cons == ctxt->nbChars) {
1.22      daniel   3209:            if (ctxt->node != NULL) {
                   3210:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3211:                    ctxt->sax->error(ctxt->userData,
                   3212:                         "detected an error in element content\n");
                   3213:                ctxt->wellFormed = 0;
                   3214:            }
1.1       daniel   3215:             break;
                   3216:        }
1.17      daniel   3217: 
1.5       daniel   3218:         GROW;
1.1       daniel   3219:     }
1.26      daniel   3220:     if (currentNode != NULL) xmlFree(currentNode);
1.1       daniel   3221: }
                   3222: 
                   3223: /**
                   3224:  * htmlParseElement:
                   3225:  * @ctxt:  an HTML parser context
                   3226:  *
                   3227:  * parse an HTML element, this is highly recursive
                   3228:  *
                   3229:  * [39] element ::= EmptyElemTag | STag content ETag
                   3230:  *
                   3231:  * [41] Attribute ::= Name Eq AttValue
                   3232:  */
                   3233: 
                   3234: void
                   3235: htmlParseElement(htmlParserCtxtPtr ctxt) {
1.14      daniel   3236:     xmlChar *name;
1.16      daniel   3237:     xmlChar *currentNode = NULL;
1.1       daniel   3238:     htmlElemDescPtr info;
1.10      daniel   3239:     htmlParserNodeInfo node_info;
1.31      daniel   3240:     xmlChar *oldname;
1.18      daniel   3241:     int depth = ctxt->nameNr;
1.1       daniel   3242: 
                   3243:     /* Capture start position */
1.10      daniel   3244:     if (ctxt->record_info) {
                   3245:         node_info.begin_pos = ctxt->input->consumed +
                   3246:                           (CUR_PTR - ctxt->input->base);
                   3247:        node_info.begin_line = ctxt->input->line;
                   3248:     }
1.1       daniel   3249: 
1.26      daniel   3250:     oldname = xmlStrdup(ctxt->name);
1.18      daniel   3251:     htmlParseStartTag(ctxt);
                   3252:     name = ctxt->name;
1.19      daniel   3253: #ifdef DEBUG
                   3254:     if (oldname == NULL)
                   3255:        fprintf(stderr, "Start of element %s\n", name);
                   3256:     else if (name == NULL)     
                   3257:        fprintf(stderr, "Start of element failed, was %s\n", oldname);
                   3258:     else       
                   3259:        fprintf(stderr, "Start of element %s, was %s\n", name, oldname);
                   3260: #endif
1.26      daniel   3261:     if (((depth == ctxt->nameNr) && (!xmlStrcmp(oldname, ctxt->name))) ||
1.18      daniel   3262:         (name == NULL)) {
1.19      daniel   3263:        if (CUR == '>')
                   3264:            NEXT;
1.26      daniel   3265:        if (oldname != NULL)
                   3266:            xmlFree(oldname);
1.1       daniel   3267:         return;
                   3268:     }
1.26      daniel   3269:     if (oldname != NULL)
                   3270:        xmlFree(oldname);
1.1       daniel   3271: 
                   3272:     /*
                   3273:      * Lookup the info for that element.
                   3274:      */
                   3275:     info = htmlTagLookup(name);
                   3276:     if (info == NULL) {
                   3277:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3278:            ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   3279:                             name);
                   3280:        ctxt->wellFormed = 0;
                   3281:     } else if (info->depr) {
                   3282: /***************************
                   3283:        if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   3284:            ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
                   3285:                               name);
                   3286:  ***************************/
                   3287:     }
                   3288: 
                   3289:     /*
                   3290:      * Check for an Empty Element labelled the XML/SGML way
                   3291:      */
                   3292:     if ((CUR == '/') && (NXT(1) == '>')) {
                   3293:         SKIP(2);
                   3294:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3295:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3296:        oldname = htmlnamePop(ctxt);
1.18      daniel   3297: #ifdef DEBUG
                   3298:         fprintf(stderr,"End of tag the XML way: popping out %s\n", oldname);
                   3299: #endif
1.17      daniel   3300:        if (oldname != NULL)
                   3301:            xmlFree(oldname);
1.1       daniel   3302:        return;
                   3303:     }
                   3304: 
1.5       daniel   3305:     if (CUR == '>') {
                   3306:         NEXT;
                   3307:     } else {
1.1       daniel   3308:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.56      veillard 3309:            ctxt->sax->error(ctxt->userData,
                   3310:                             "Couldn't find end of Start Tag %s\n",
                   3311:                             name);
1.1       daniel   3312:        ctxt->wellFormed = 0;
                   3313: 
                   3314:        /*
                   3315:         * end of parsing of this node.
                   3316:         */
1.18      daniel   3317:        if (!xmlStrcmp(name, ctxt->name)) { 
                   3318:            nodePop(ctxt);
1.24      daniel   3319:            oldname = htmlnamePop(ctxt);
1.18      daniel   3320: #ifdef DEBUG
                   3321:            fprintf(stderr,"End of start tag problem: popping out %s\n", oldname);
                   3322: #endif
                   3323:            if (oldname != NULL)
                   3324:                xmlFree(oldname);
                   3325:        }    
1.10      daniel   3326: 
                   3327:        /*
                   3328:         * Capture end position and add node
                   3329:         */
                   3330:        if ( currentNode != NULL && ctxt->record_info ) {
                   3331:           node_info.end_pos = ctxt->input->consumed +
                   3332:                              (CUR_PTR - ctxt->input->base);
                   3333:           node_info.end_line = ctxt->input->line;
1.15      daniel   3334:           node_info.node = ctxt->node;
1.10      daniel   3335:           xmlParserAddNodeInfo(ctxt, &node_info);
                   3336:        }
1.1       daniel   3337:        return;
                   3338:     }
                   3339: 
                   3340:     /*
                   3341:      * Check for an Empty Element from DTD definition
                   3342:      */
                   3343:     if ((info != NULL) && (info->empty)) {
                   3344:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3345:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3346:        oldname = htmlnamePop(ctxt);
1.18      daniel   3347: #ifdef DEBUG
                   3348:        fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   3349: #endif
1.17      daniel   3350:        if (oldname != NULL)
                   3351:            xmlFree(oldname);
1.1       daniel   3352:        return;
                   3353:     }
                   3354: 
                   3355:     /*
                   3356:      * Parse the content of the element:
                   3357:      */
1.26      daniel   3358:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3359:     depth = ctxt->nameNr;
                   3360:     while (IS_CHAR(CUR)) {
                   3361:        htmlParseContent(ctxt);
                   3362:        if (ctxt->nameNr < depth) break; 
                   3363:     }  
1.1       daniel   3364: 
                   3365:     if (!IS_CHAR(CUR)) {
1.49      daniel   3366:        /************
1.1       daniel   3367:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3368:            ctxt->sax->error(ctxt->userData,
1.18      daniel   3369:                 "Premature end of data in tag %s\n", currentNode);
1.1       daniel   3370:        ctxt->wellFormed = 0;
1.49      daniel   3371:         *************/
1.1       daniel   3372: 
                   3373:        /*
                   3374:         * end of parsing of this node.
                   3375:         */
                   3376:        nodePop(ctxt);
1.24      daniel   3377:        oldname = htmlnamePop(ctxt);
1.18      daniel   3378: #ifdef DEBUG
                   3379:        fprintf(stderr,"Premature end of tag %s : popping out %s\n", name, oldname);
                   3380: #endif
1.17      daniel   3381:        if (oldname != NULL)
                   3382:            xmlFree(oldname);
1.26      daniel   3383:        if (currentNode != NULL)
                   3384:            xmlFree(currentNode);
1.1       daniel   3385:        return;
                   3386:     }
1.10      daniel   3387: 
                   3388:     /*
                   3389:      * Capture end position and add node
                   3390:      */
                   3391:     if ( currentNode != NULL && ctxt->record_info ) {
                   3392:        node_info.end_pos = ctxt->input->consumed +
                   3393:                           (CUR_PTR - ctxt->input->base);
                   3394:        node_info.end_line = ctxt->input->line;
1.15      daniel   3395:        node_info.node = ctxt->node;
1.10      daniel   3396:        xmlParserAddNodeInfo(ctxt, &node_info);
                   3397:     }
1.26      daniel   3398:     if (currentNode != NULL)
                   3399:        xmlFree(currentNode);
1.1       daniel   3400: }
                   3401: 
                   3402: /**
                   3403:  * htmlParseDocument :
                   3404:  * @ctxt:  an HTML parser context
                   3405:  * 
                   3406:  * parse an HTML document (and build a tree if using the standard SAX
                   3407:  * interface).
                   3408:  *
                   3409:  * Returns 0, -1 in case of error. the parser context is augmented
                   3410:  *                as a result of the parsing.
                   3411:  */
                   3412: 
                   3413: int
                   3414: htmlParseDocument(htmlParserCtxtPtr ctxt) {
1.59      veillard 3415:     xmlDtdPtr dtd;
                   3416: 
1.1       daniel   3417:     htmlDefaultSAXHandlerInit();
                   3418:     ctxt->html = 1;
                   3419: 
1.5       daniel   3420:     GROW;
1.1       daniel   3421:     /*
1.9       daniel   3422:      * SAX: beginning of the document processing.
1.1       daniel   3423:      */
                   3424:     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3425:         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
                   3426: 
                   3427:     /*
                   3428:      * Wipe out everything which is before the first '<'
                   3429:      */
1.22      daniel   3430:     SKIP_BLANKS;
1.1       daniel   3431:     if (CUR == 0) {
                   3432:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3433:            ctxt->sax->error(ctxt->userData, "Document is empty\n");
                   3434:        ctxt->wellFormed = 0;
                   3435:     }
                   3436: 
1.40      daniel   3437:     if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
                   3438:        ctxt->sax->startDocument(ctxt->userData);
                   3439: 
                   3440: 
1.22      daniel   3441:     /*
                   3442:      * Parse possible comments before any content
                   3443:      */
                   3444:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3445:            (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   3446:         htmlParseComment(ctxt);           
1.22      daniel   3447:        SKIP_BLANKS;
                   3448:     }     
                   3449: 
1.1       daniel   3450: 
                   3451:     /*
                   3452:      * Then possibly doc type declaration(s) and more Misc
                   3453:      * (doctypedecl Misc*)?
                   3454:      */
                   3455:     if ((CUR == '<') && (NXT(1) == '!') &&
                   3456:        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3457:        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3458:        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3459:        (UPP(8) == 'E')) {
                   3460:        htmlParseDocTypeDecl(ctxt);
                   3461:     }
                   3462:     SKIP_BLANKS;
                   3463: 
                   3464:     /*
1.55      veillard 3465:      * Parse possible comments before any content
                   3466:      */
                   3467:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3468:            (NXT(2) == '-') && (NXT(3) == '-')) {
                   3469:         htmlParseComment(ctxt);           
                   3470:        SKIP_BLANKS;
                   3471:     }     
                   3472: 
                   3473:     /*
1.1       daniel   3474:      * Time to start parsing the tree itself
                   3475:      */
1.22      daniel   3476:     htmlParseContent(ctxt);
1.1       daniel   3477: 
                   3478:     /*
1.47      daniel   3479:      * autoclose
                   3480:      */
                   3481:     if (CUR == 0)
                   3482:        htmlAutoClose(ctxt, NULL);
                   3483: 
                   3484: 
                   3485:     /*
1.1       daniel   3486:      * SAX: end of the document processing.
                   3487:      */
                   3488:     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3489:         ctxt->sax->endDocument(ctxt->userData);
1.59      veillard 3490: 
                   3491:     if (ctxt->myDoc != NULL) {
                   3492:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   3493:        if (dtd == NULL)
                   3494:            ctxt->myDoc->intSubset = 
                   3495:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   3496:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   3497:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
                   3498:     }
1.1       daniel   3499:     if (! ctxt->wellFormed) return(-1);
                   3500:     return(0);
                   3501: }
                   3502: 
                   3503: 
1.30      daniel   3504: /************************************************************************
                   3505:  *                                                                     *
                   3506:  *                     Parser contexts handling                        *
                   3507:  *                                                                     *
                   3508:  ************************************************************************/
1.1       daniel   3509: 
                   3510: /**
                   3511:  * xmlInitParserCtxt:
                   3512:  * @ctxt:  an HTML parser context
                   3513:  *
                   3514:  * Initialize a parser context
                   3515:  */
                   3516: 
                   3517: void
                   3518: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
                   3519: {
                   3520:     htmlSAXHandler *sax;
                   3521: 
1.21      daniel   3522:     if (ctxt == NULL) return;
                   3523:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   3524: 
1.11      daniel   3525:     sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
1.1       daniel   3526:     if (sax == NULL) {
                   3527:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3528:     }
1.68    ! veillard 3529:     else
        !          3530:         memset(sax, 0, sizeof(htmlSAXHandler));
1.1       daniel   3531: 
                   3532:     /* Allocate the Input stack */
1.19      daniel   3533:     ctxt->inputTab = (htmlParserInputPtr *) 
                   3534:                       xmlMalloc(5 * sizeof(htmlParserInputPtr));
                   3535:     if (ctxt->inputTab == NULL) {
                   3536:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
1.65      veillard 3537:        ctxt->inputNr = 0;
                   3538:        ctxt->inputMax = 0;
                   3539:        ctxt->input = NULL;
                   3540:        return;
1.19      daniel   3541:     }
1.1       daniel   3542:     ctxt->inputNr = 0;
                   3543:     ctxt->inputMax = 5;
                   3544:     ctxt->input = NULL;
                   3545:     ctxt->version = NULL;
                   3546:     ctxt->encoding = NULL;
                   3547:     ctxt->standalone = -1;
1.30      daniel   3548:     ctxt->instate = XML_PARSER_START;
1.1       daniel   3549: 
                   3550:     /* Allocate the Node stack */
1.11      daniel   3551:     ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
1.65      veillard 3552:     if (ctxt->nodeTab == NULL) {
                   3553:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3554:        ctxt->nodeNr = 0;
                   3555:        ctxt->nodeMax = 0;
                   3556:        ctxt->node = NULL;
                   3557:        ctxt->inputNr = 0;
                   3558:        ctxt->inputMax = 0;
                   3559:        ctxt->input = NULL;
                   3560:        return;
                   3561:     }
1.1       daniel   3562:     ctxt->nodeNr = 0;
                   3563:     ctxt->nodeMax = 10;
                   3564:     ctxt->node = NULL;
                   3565: 
1.15      daniel   3566:     /* Allocate the Name stack */
                   3567:     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1.65      veillard 3568:     if (ctxt->nameTab == NULL) {
                   3569:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   3570:        ctxt->nameNr = 0;
                   3571:        ctxt->nameMax = 10;
                   3572:        ctxt->name = NULL;
                   3573:        ctxt->nodeNr = 0;
                   3574:        ctxt->nodeMax = 0;
                   3575:        ctxt->node = NULL;
                   3576:        ctxt->inputNr = 0;
                   3577:        ctxt->inputMax = 0;
                   3578:        ctxt->input = NULL;
                   3579:        return;
                   3580:     }
1.15      daniel   3581:     ctxt->nameNr = 0;
                   3582:     ctxt->nameMax = 10;
                   3583:     ctxt->name = NULL;
                   3584: 
1.1       daniel   3585:     if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
                   3586:     else {
                   3587:         ctxt->sax = sax;
                   3588:        memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
                   3589:     }
                   3590:     ctxt->userData = ctxt;
                   3591:     ctxt->myDoc = NULL;
                   3592:     ctxt->wellFormed = 1;
                   3593:     ctxt->replaceEntities = 0;
                   3594:     ctxt->html = 1;
                   3595:     ctxt->record_info = 0;
1.21      daniel   3596:     ctxt->validate = 0;
1.26      daniel   3597:     ctxt->nbChars = 0;
1.30      daniel   3598:     ctxt->checkIndex = 0;
1.1       daniel   3599:     xmlInitNodeInfoSeq(&ctxt->node_seq);
                   3600: }
                   3601: 
                   3602: /**
                   3603:  * htmlFreeParserCtxt:
                   3604:  * @ctxt:  an HTML parser context
                   3605:  *
                   3606:  * Free all the memory used by a parser context. However the parsed
                   3607:  * document in ctxt->myDoc is not freed.
                   3608:  */
                   3609: 
                   3610: void
                   3611: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
                   3612: {
1.47      daniel   3613:     xmlFreeParserCtxt(ctxt);
1.1       daniel   3614: }
                   3615: 
                   3616: /**
                   3617:  * htmlCreateDocParserCtxt :
1.14      daniel   3618:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   3619:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3620:  *
                   3621:  * Create a parser context for an HTML document.
                   3622:  *
                   3623:  * Returns the new parser context or NULL
                   3624:  */
                   3625: htmlParserCtxtPtr
1.14      daniel   3626: htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
1.1       daniel   3627:     htmlParserCtxtPtr ctxt;
                   3628:     htmlParserInputPtr input;
                   3629:     /* htmlCharEncoding enc; */
                   3630: 
1.11      daniel   3631:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   3632:     if (ctxt == NULL) {
                   3633:         perror("malloc");
                   3634:        return(NULL);
                   3635:     }
                   3636:     htmlInitParserCtxt(ctxt);
1.11      daniel   3637:     input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   3638:     if (input == NULL) {
                   3639:         perror("malloc");
1.11      daniel   3640:        xmlFree(ctxt);
1.1       daniel   3641:        return(NULL);
                   3642:     }
1.19      daniel   3643:     memset(input, 0, sizeof(htmlParserInput));
1.1       daniel   3644: 
                   3645:     input->line = 1;
                   3646:     input->col = 1;
                   3647:     input->base = cur;
                   3648:     input->cur = cur;
                   3649: 
                   3650:     inputPush(ctxt, input);
                   3651:     return(ctxt);
                   3652: }
                   3653: 
1.31      daniel   3654: /************************************************************************
                   3655:  *                                                                     *
                   3656:  *             Progressive parsing interfaces                          *
                   3657:  *                                                                     *
                   3658:  ************************************************************************/
                   3659: 
                   3660: /**
                   3661:  * htmlParseLookupSequence:
                   3662:  * @ctxt:  an HTML parser context
                   3663:  * @first:  the first char to lookup
                   3664:  * @next:  the next char to lookup or zero
                   3665:  * @third:  the next char to lookup or zero
                   3666:  *
                   3667:  * Try to find if a sequence (first, next, third) or  just (first next) or
                   3668:  * (first) is available in the input stream.
                   3669:  * This function has a side effect of (possibly) incrementing ctxt->checkIndex
                   3670:  * to avoid rescanning sequences of bytes, it DOES change the state of the
                   3671:  * parser, do not use liberally.
                   3672:  * This is basically similar to xmlParseLookupSequence()
                   3673:  *
                   3674:  * Returns the index to the current parsing point if the full sequence
                   3675:  *      is available, -1 otherwise.
                   3676:  */
                   3677: int
                   3678: htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
                   3679:                        xmlChar next, xmlChar third) {
                   3680:     int base, len;
                   3681:     htmlParserInputPtr in;
                   3682:     const xmlChar *buf;
                   3683: 
                   3684:     in = ctxt->input;
                   3685:     if (in == NULL) return(-1);
                   3686:     base = in->cur - in->base;
                   3687:     if (base < 0) return(-1);
                   3688:     if (ctxt->checkIndex > base)
                   3689:         base = ctxt->checkIndex;
                   3690:     if (in->buf == NULL) {
                   3691:        buf = in->base;
                   3692:        len = in->length;
                   3693:     } else {
                   3694:        buf = in->buf->buffer->content;
                   3695:        len = in->buf->buffer->use;
                   3696:     }
                   3697:     /* take into account the sequence length */
                   3698:     if (third) len -= 2;
                   3699:     else if (next) len --;
                   3700:     for (;base < len;base++) {
                   3701:         if (buf[base] == first) {
                   3702:            if (third != 0) {
                   3703:                if ((buf[base + 1] != next) ||
                   3704:                    (buf[base + 2] != third)) continue;
                   3705:            } else if (next != 0) {
                   3706:                if (buf[base + 1] != next) continue;
                   3707:            }
                   3708:            ctxt->checkIndex = 0;
                   3709: #ifdef DEBUG_PUSH
                   3710:            if (next == 0)
                   3711:                fprintf(stderr, "HPP: lookup '%c' found at %d\n",
                   3712:                        first, base);
                   3713:            else if (third == 0)
                   3714:                fprintf(stderr, "HPP: lookup '%c%c' found at %d\n",
                   3715:                        first, next, base);
                   3716:            else 
                   3717:                fprintf(stderr, "HPP: lookup '%c%c%c' found at %d\n",
                   3718:                        first, next, third, base);
                   3719: #endif
                   3720:            return(base - (in->cur - in->base));
                   3721:        }
                   3722:     }
                   3723:     ctxt->checkIndex = base;
                   3724: #ifdef DEBUG_PUSH
                   3725:     if (next == 0)
                   3726:        fprintf(stderr, "HPP: lookup '%c' failed\n", first);
                   3727:     else if (third == 0)
                   3728:        fprintf(stderr, "HPP: lookup '%c%c' failed\n", first, next);
                   3729:     else       
                   3730:        fprintf(stderr, "HPP: lookup '%c%c%c' failed\n", first, next, third);
                   3731: #endif
                   3732:     return(-1);
                   3733: }
                   3734: 
                   3735: /**
1.32      daniel   3736:  * htmlParseTryOrFinish:
1.31      daniel   3737:  * @ctxt:  an HTML parser context
1.32      daniel   3738:  * @terminate:  last chunk indicator
1.31      daniel   3739:  *
                   3740:  * Try to progress on parsing
                   3741:  *
                   3742:  * Returns zero if no parsing was possible
                   3743:  */
                   3744: int
1.32      daniel   3745: htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
1.31      daniel   3746:     int ret = 0;
                   3747:     htmlParserInputPtr in;
1.47      daniel   3748:     int avail = 0;
1.31      daniel   3749:     xmlChar cur, next;
                   3750: 
                   3751: #ifdef DEBUG_PUSH
                   3752:     switch (ctxt->instate) {
                   3753:        case XML_PARSER_EOF:
                   3754:            fprintf(stderr, "HPP: try EOF\n"); break;
                   3755:        case XML_PARSER_START:
                   3756:            fprintf(stderr, "HPP: try START\n"); break;
                   3757:        case XML_PARSER_MISC:
                   3758:            fprintf(stderr, "HPP: try MISC\n");break;
                   3759:        case XML_PARSER_COMMENT:
                   3760:            fprintf(stderr, "HPP: try COMMENT\n");break;
                   3761:        case XML_PARSER_PROLOG:
                   3762:            fprintf(stderr, "HPP: try PROLOG\n");break;
                   3763:        case XML_PARSER_START_TAG:
                   3764:            fprintf(stderr, "HPP: try START_TAG\n");break;
                   3765:        case XML_PARSER_CONTENT:
                   3766:            fprintf(stderr, "HPP: try CONTENT\n");break;
                   3767:        case XML_PARSER_CDATA_SECTION:
                   3768:            fprintf(stderr, "HPP: try CDATA_SECTION\n");break;
                   3769:        case XML_PARSER_END_TAG:
                   3770:            fprintf(stderr, "HPP: try END_TAG\n");break;
                   3771:        case XML_PARSER_ENTITY_DECL:
                   3772:            fprintf(stderr, "HPP: try ENTITY_DECL\n");break;
                   3773:        case XML_PARSER_ENTITY_VALUE:
                   3774:            fprintf(stderr, "HPP: try ENTITY_VALUE\n");break;
                   3775:        case XML_PARSER_ATTRIBUTE_VALUE:
                   3776:            fprintf(stderr, "HPP: try ATTRIBUTE_VALUE\n");break;
                   3777:        case XML_PARSER_DTD:
                   3778:            fprintf(stderr, "HPP: try DTD\n");break;
                   3779:        case XML_PARSER_EPILOG:
                   3780:            fprintf(stderr, "HPP: try EPILOG\n");break;
                   3781:        case XML_PARSER_PI:
                   3782:            fprintf(stderr, "HPP: try PI\n");break;
                   3783:     }
                   3784: #endif
                   3785: 
                   3786:     while (1) {
                   3787: 
                   3788:        in = ctxt->input;
                   3789:        if (in == NULL) break;
                   3790:        if (in->buf == NULL)
                   3791:            avail = in->length - (in->cur - in->base);
                   3792:        else
                   3793:            avail = in->buf->buffer->use - (in->cur - in->base);
1.47      daniel   3794:        if ((avail == 0) && (terminate)) {
                   3795:            htmlAutoClose(ctxt, NULL);
1.54      veillard 3796:            if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   3797:                /*
                   3798:                 * SAX: end of the document processing.
                   3799:                 */
1.47      daniel   3800:                ctxt->instate = XML_PARSER_EOF;
1.54      veillard 3801:                if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3802:                    ctxt->sax->endDocument(ctxt->userData);
                   3803:            }
1.47      daniel   3804:        }
1.31      daniel   3805:         if (avail < 1)
                   3806:            goto done;
                   3807:         switch (ctxt->instate) {
                   3808:             case XML_PARSER_EOF:
                   3809:                /*
                   3810:                 * Document parsing is done !
                   3811:                 */
                   3812:                goto done;
                   3813:             case XML_PARSER_START:
                   3814:                /*
                   3815:                 * Very first chars read from the document flow.
                   3816:                 */
                   3817:                cur = in->cur[0];
                   3818:                if (IS_BLANK(cur)) {
                   3819:                    SKIP_BLANKS;
                   3820:                    if (in->buf == NULL)
                   3821:                        avail = in->length - (in->cur - in->base);
                   3822:                    else
                   3823:                        avail = in->buf->buffer->use - (in->cur - in->base);
                   3824:                }
                   3825:                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3826:                    ctxt->sax->setDocumentLocator(ctxt->userData,
                   3827:                                                  &xmlDefaultSAXLocator);
1.46      daniel   3828:                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
                   3829:                    (!ctxt->disableSAX))
                   3830:                    ctxt->sax->startDocument(ctxt->userData);
                   3831: 
1.31      daniel   3832:                cur = in->cur[0];
                   3833:                next = in->cur[1];
                   3834:                if ((cur == '<') && (next == '!') &&
                   3835:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3836:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3837:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3838:                    (UPP(8) == 'E')) {
1.32      daniel   3839:                    if ((!terminate) &&
                   3840:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3841:                        goto done;
                   3842: #ifdef DEBUG_PUSH
                   3843:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3844: #endif
                   3845:                    htmlParseDocTypeDecl(ctxt);
                   3846:                    ctxt->instate = XML_PARSER_PROLOG;
                   3847: #ifdef DEBUG_PUSH
                   3848:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3849: #endif
                   3850:                 } else {
                   3851:                    ctxt->instate = XML_PARSER_MISC;
                   3852:                }
                   3853: #ifdef DEBUG_PUSH
                   3854:                fprintf(stderr, "HPP: entering MISC\n");
                   3855: #endif
                   3856:                break;
                   3857:             case XML_PARSER_MISC:
                   3858:                SKIP_BLANKS;
                   3859:                if (in->buf == NULL)
                   3860:                    avail = in->length - (in->cur - in->base);
                   3861:                else
                   3862:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3863:                if (avail < 2)
                   3864:                    goto done;
                   3865:                cur = in->cur[0];
                   3866:                next = in->cur[1];
                   3867:                if ((cur == '<') && (next == '!') &&
                   3868:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3869:                    if ((!terminate) &&
                   3870:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3871:                        goto done;
                   3872: #ifdef DEBUG_PUSH
                   3873:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3874: #endif
                   3875:                    htmlParseComment(ctxt);
                   3876:                    ctxt->instate = XML_PARSER_MISC;
                   3877:                } else if ((cur == '<') && (next == '!') &&
                   3878:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3879:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3880:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3881:                    (UPP(8) == 'E')) {
1.32      daniel   3882:                    if ((!terminate) &&
                   3883:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3884:                        goto done;
                   3885: #ifdef DEBUG_PUSH
                   3886:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3887: #endif
                   3888:                    htmlParseDocTypeDecl(ctxt);
                   3889:                    ctxt->instate = XML_PARSER_PROLOG;
                   3890: #ifdef DEBUG_PUSH
                   3891:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3892: #endif
                   3893:                } else if ((cur == '<') && (next == '!') &&
                   3894:                           (avail < 9)) {
                   3895:                    goto done;
                   3896:                } else {
                   3897:                    ctxt->instate = XML_PARSER_START_TAG;
                   3898: #ifdef DEBUG_PUSH
                   3899:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3900: #endif
                   3901:                }
                   3902:                break;
                   3903:             case XML_PARSER_PROLOG:
                   3904:                SKIP_BLANKS;
                   3905:                if (in->buf == NULL)
                   3906:                    avail = in->length - (in->cur - in->base);
                   3907:                else
                   3908:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3909:                if (avail < 2) 
                   3910:                    goto done;
                   3911:                cur = in->cur[0];
                   3912:                next = in->cur[1];
                   3913:                if ((cur == '<') && (next == '!') &&
                   3914:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3915:                    if ((!terminate) &&
                   3916:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3917:                        goto done;
                   3918: #ifdef DEBUG_PUSH
                   3919:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3920: #endif
                   3921:                    htmlParseComment(ctxt);
                   3922:                    ctxt->instate = XML_PARSER_PROLOG;
                   3923:                } else if ((cur == '<') && (next == '!') &&
                   3924:                           (avail < 4)) {
                   3925:                    goto done;
                   3926:                } else {
                   3927:                    ctxt->instate = XML_PARSER_START_TAG;
                   3928: #ifdef DEBUG_PUSH
                   3929:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3930: #endif
                   3931:                }
                   3932:                break;
                   3933:             case XML_PARSER_EPILOG:
                   3934:                if (in->buf == NULL)
                   3935:                    avail = in->length - (in->cur - in->base);
                   3936:                else
                   3937:                    avail = in->buf->buffer->use - (in->cur - in->base);
1.55      veillard 3938:                if (avail < 1)
                   3939:                    goto done;
                   3940:                cur = in->cur[0];
                   3941:                if (IS_BLANK(cur)) {
                   3942:                    htmlParseCharData(ctxt, 0);
                   3943:                    goto done;
                   3944:                }
1.31      daniel   3945:                if (avail < 2)
                   3946:                    goto done;
                   3947:                next = in->cur[1];
                   3948:                if ((cur == '<') && (next == '!') &&
                   3949:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3950:                    if ((!terminate) &&
                   3951:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3952:                        goto done;
                   3953: #ifdef DEBUG_PUSH
                   3954:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3955: #endif
                   3956:                    htmlParseComment(ctxt);
                   3957:                    ctxt->instate = XML_PARSER_EPILOG;
                   3958:                } else if ((cur == '<') && (next == '!') &&
                   3959:                           (avail < 4)) {
                   3960:                    goto done;
                   3961:                } else {
1.67      veillard 3962:                    ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   3963:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3964:                        ctxt->sax->error(ctxt->userData,
                   3965:                            "Extra content at the end of the document\n");
                   3966:                    ctxt->wellFormed = 0;
                   3967:                    ctxt->instate = XML_PARSER_EOF;
                   3968: #ifdef DEBUG_PUSH
                   3969:                    fprintf(stderr, "HPP: entering EOF\n");
                   3970: #endif
                   3971:                    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3972:                        ctxt->sax->endDocument(ctxt->userData);
                   3973:                    goto done;
                   3974:                }
                   3975:                break;
                   3976:             case XML_PARSER_START_TAG: {
                   3977:                xmlChar *name, *oldname;
                   3978:                int depth = ctxt->nameNr;
                   3979:                htmlElemDescPtr info;
                   3980: 
                   3981:                if (avail < 2)
                   3982:                    goto done;
                   3983:                cur = in->cur[0];
                   3984:                if (cur != '<') {
                   3985:                    ctxt->instate = XML_PARSER_CONTENT;
                   3986: #ifdef DEBUG_PUSH
                   3987:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   3988: #endif
                   3989:                    break;
                   3990:                }
1.32      daniel   3991:                if ((!terminate) &&
                   3992:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3993:                    goto done;
                   3994: 
                   3995:                oldname = xmlStrdup(ctxt->name);
                   3996:                htmlParseStartTag(ctxt);
                   3997:                name = ctxt->name;
                   3998: #ifdef DEBUG
                   3999:                if (oldname == NULL)
                   4000:                    fprintf(stderr, "Start of element %s\n", name);
                   4001:                else if (name == NULL)  
                   4002:                    fprintf(stderr, "Start of element failed, was %s\n",
                   4003:                            oldname);
                   4004:                else    
                   4005:                    fprintf(stderr, "Start of element %s, was %s\n",
                   4006:                            name, oldname);
                   4007: #endif
                   4008:                if (((depth == ctxt->nameNr) &&
                   4009:                     (!xmlStrcmp(oldname, ctxt->name))) ||
                   4010:                    (name == NULL)) {
                   4011:                    if (CUR == '>')
                   4012:                        NEXT;
                   4013:                    if (oldname != NULL)
                   4014:                        xmlFree(oldname);
                   4015:                    break;
                   4016:                }
                   4017:                if (oldname != NULL)
                   4018:                    xmlFree(oldname);
                   4019: 
                   4020:                /*
                   4021:                 * Lookup the info for that element.
                   4022:                 */
                   4023:                info = htmlTagLookup(name);
                   4024:                if (info == NULL) {
                   4025:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4026:                        ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   4027:                                         name);
                   4028:                    ctxt->wellFormed = 0;
                   4029:                } else if (info->depr) {
                   4030:                    /***************************
                   4031:                    if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   4032:                        ctxt->sax->warning(ctxt->userData,
                   4033:                                           "Tag %s is deprecated\n",
                   4034:                                           name);
                   4035:                     ***************************/
                   4036:                }
                   4037: 
                   4038:                /*
                   4039:                 * Check for an Empty Element labelled the XML/SGML way
                   4040:                 */
                   4041:                if ((CUR == '/') && (NXT(1) == '>')) {
                   4042:                    SKIP(2);
                   4043:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4044:                        ctxt->sax->endElement(ctxt->userData, name);
                   4045:                    oldname = htmlnamePop(ctxt);
                   4046: #ifdef DEBUG
                   4047:                    fprintf(stderr,"End of tag the XML way: popping out %s\n",
                   4048:                            oldname);
                   4049: #endif
                   4050:                    if (oldname != NULL)
                   4051:                        xmlFree(oldname);
                   4052:                    ctxt->instate = XML_PARSER_CONTENT;
                   4053: #ifdef DEBUG_PUSH
                   4054:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   4055: #endif
                   4056:                    break;
                   4057:                }
                   4058: 
                   4059:                if (CUR == '>') {
                   4060:                    NEXT;
                   4061:                } else {
                   4062:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4063:                        ctxt->sax->error(ctxt->userData, 
                   4064:                                         "Couldn't find end of Start Tag %s\n",
                   4065:                                         name);
                   4066:                    ctxt->wellFormed = 0;
                   4067: 
                   4068:                    /*
                   4069:                     * end of parsing of this node.
                   4070:                     */
                   4071:                    if (!xmlStrcmp(name, ctxt->name)) { 
                   4072:                        nodePop(ctxt);
                   4073:                        oldname = htmlnamePop(ctxt);
                   4074: #ifdef DEBUG
                   4075:                        fprintf(stderr,
                   4076:                         "End of start tag problem: popping out %s\n", oldname);
                   4077: #endif
                   4078:                        if (oldname != NULL)
                   4079:                            xmlFree(oldname);
                   4080:                    }    
                   4081: 
                   4082:                    ctxt->instate = XML_PARSER_CONTENT;
                   4083: #ifdef DEBUG_PUSH
                   4084:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   4085: #endif
                   4086:                    break;
                   4087:                }
                   4088: 
                   4089:                /*
                   4090:                 * Check for an Empty Element from DTD definition
                   4091:                 */
                   4092:                if ((info != NULL) && (info->empty)) {
                   4093:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4094:                        ctxt->sax->endElement(ctxt->userData, name);
                   4095:                    oldname = htmlnamePop(ctxt);
                   4096: #ifdef DEBUG
                   4097:                    fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   4098: #endif
                   4099:                    if (oldname != NULL)
                   4100:                        xmlFree(oldname);
                   4101:                }
                   4102:                ctxt->instate = XML_PARSER_CONTENT;
                   4103: #ifdef DEBUG_PUSH
                   4104:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4105: #endif
                   4106:                 break;
                   4107:            }
1.56      veillard 4108:             case XML_PARSER_CONTENT: {
                   4109:                long cons;
1.31      daniel   4110:                 /*
                   4111:                 * Handle preparsed entities and charRef
                   4112:                 */
                   4113:                if (ctxt->token != 0) {
1.47      daniel   4114:                    xmlChar chr[2] = { 0 , 0 } ;
1.31      daniel   4115: 
1.47      daniel   4116:                    chr[0] = (xmlChar) ctxt->token;
1.59      veillard 4117:                    htmlCheckParagraph(ctxt);
1.31      daniel   4118:                    if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.47      daniel   4119:                        ctxt->sax->characters(ctxt->userData, chr, 1);
1.31      daniel   4120:                    ctxt->token = 0;
                   4121:                    ctxt->checkIndex = 0;
                   4122:                }
1.47      daniel   4123:                if ((avail == 1) && (terminate)) {
                   4124:                    cur = in->cur[0];
                   4125:                    if ((cur != '<') && (cur != '&')) {
1.48      daniel   4126:                        if (ctxt->sax != NULL) {
                   4127:                            if (IS_BLANK(cur)) {
                   4128:                                if (ctxt->sax->ignorableWhitespace != NULL)
                   4129:                                    ctxt->sax->ignorableWhitespace(
                   4130:                                            ctxt->userData, &cur, 1);
                   4131:                            } else {
1.59      veillard 4132:                                htmlCheckParagraph(ctxt);
1.48      daniel   4133:                                if (ctxt->sax->characters != NULL)
                   4134:                                    ctxt->sax->characters(
                   4135:                                            ctxt->userData, &cur, 1);
                   4136:                            }
                   4137:                        }
1.47      daniel   4138:                        ctxt->token = 0;
                   4139:                        ctxt->checkIndex = 0;
                   4140:                        NEXT;
                   4141:                    }
                   4142:                    break;
                   4143:                }
1.31      daniel   4144:                if (avail < 2)
                   4145:                    goto done;
                   4146:                cur = in->cur[0];
                   4147:                next = in->cur[1];
1.56      veillard 4148:                cons = ctxt->nbChars;
1.59      veillard 4149:                /*
                   4150:                 * Sometimes DOCTYPE arrives in the middle of the document
                   4151:                 */
                   4152:                if ((cur == '<') && (next == '!') &&
                   4153:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   4154:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   4155:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   4156:                    (UPP(8) == 'E')) {
                   4157:                    if ((!terminate) &&
                   4158:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
                   4159:                        goto done;
                   4160:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4161:                        ctxt->sax->error(ctxt->userData,
                   4162:                             "Misplaced DOCTYPE declaration\n");
                   4163:                    ctxt->wellFormed = 0;
                   4164:                    htmlParseDocTypeDecl(ctxt);
                   4165:                } else if ((cur == '<') && (next == '!') &&
1.31      daniel   4166:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   4167:                    if ((!terminate) &&
                   4168:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   4169:                        goto done;
                   4170: #ifdef DEBUG_PUSH
                   4171:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   4172: #endif
                   4173:                    htmlParseComment(ctxt);
                   4174:                    ctxt->instate = XML_PARSER_CONTENT;
                   4175:                } else if ((cur == '<') && (next == '!') && (avail < 4)) {
                   4176:                    goto done;
                   4177:                } else if ((cur == '<') && (next == '/')) {
                   4178:                    ctxt->instate = XML_PARSER_END_TAG;
                   4179:                    ctxt->checkIndex = 0;
                   4180: #ifdef DEBUG_PUSH
                   4181:                    fprintf(stderr, "HPP: entering END_TAG\n");
                   4182: #endif
                   4183:                    break;
                   4184:                } else if (cur == '<') {
                   4185:                    ctxt->instate = XML_PARSER_START_TAG;
                   4186:                    ctxt->checkIndex = 0;
                   4187: #ifdef DEBUG_PUSH
                   4188:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   4189: #endif
                   4190:                    break;
                   4191:                } else if (cur == '&') {
1.32      daniel   4192:                    if ((!terminate) &&
                   4193:                        (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
1.31      daniel   4194:                        goto done;
                   4195: #ifdef DEBUG_PUSH
                   4196:                    fprintf(stderr, "HPP: Parsing Reference\n");
                   4197: #endif
                   4198:                    /* TODO: check generation of subtrees if noent !!! */
                   4199:                    htmlParseReference(ctxt);
                   4200:                } else {
                   4201:                    /* TODO Avoid the extra copy, handle directly !!!!!! */
                   4202:                    /*
                   4203:                     * Goal of the following test is :
                   4204:                     *  - minimize calls to the SAX 'character' callback
                   4205:                     *    when they are mergeable
                   4206:                     */
                   4207:                    if ((ctxt->inputNr == 1) &&
                   4208:                        (avail < HTML_PARSER_BIG_BUFFER_SIZE)) {
1.32      daniel   4209:                        if ((!terminate) &&
                   4210:                            (htmlParseLookupSequence(ctxt, '<', 0, 0) < 0))
1.31      daniel   4211:                            goto done;
                   4212:                     }
                   4213:                    ctxt->checkIndex = 0;
                   4214: #ifdef DEBUG_PUSH
                   4215:                    fprintf(stderr, "HPP: Parsing char data\n");
                   4216: #endif
                   4217:                    htmlParseCharData(ctxt, 0);
                   4218:                }
1.56      veillard 4219:                if (cons == ctxt->nbChars) {
                   4220:                    if (ctxt->node != NULL) {
                   4221:                        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4222:                            ctxt->sax->error(ctxt->userData,
                   4223:                                 "detected an error in element content\n");
                   4224:                        ctxt->wellFormed = 0;
                   4225:                        NEXT;
                   4226:                    }
                   4227:                    break;
                   4228:                }
                   4229: 
1.31      daniel   4230:                break;
1.56      veillard 4231:            }
1.31      daniel   4232:             case XML_PARSER_END_TAG:
                   4233:                if (avail < 2)
                   4234:                    goto done;
1.32      daniel   4235:                if ((!terminate) &&
                   4236:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   4237:                    goto done;
                   4238:                htmlParseEndTag(ctxt);
                   4239:                if (ctxt->nameNr == 0) {
                   4240:                    ctxt->instate = XML_PARSER_EPILOG;
                   4241:                } else {
                   4242:                    ctxt->instate = XML_PARSER_CONTENT;
                   4243:                }
                   4244:                ctxt->checkIndex = 0;
                   4245: #ifdef DEBUG_PUSH
                   4246:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4247: #endif
                   4248:                break;
                   4249:             case XML_PARSER_CDATA_SECTION:
                   4250:                fprintf(stderr, "HPP: internal error, state == CDATA\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_DTD:
                   4258:                fprintf(stderr, "HPP: internal error, state == DTD\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_COMMENT:
                   4266:                fprintf(stderr, "HPP: internal error, state == COMMENT\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_PI:
                   4274:                fprintf(stderr, "HPP: internal error, state == PI\n");
                   4275:                ctxt->instate = XML_PARSER_CONTENT;
                   4276:                ctxt->checkIndex = 0;
                   4277: #ifdef DEBUG_PUSH
                   4278:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4279: #endif
                   4280:                break;
                   4281:             case XML_PARSER_ENTITY_DECL:
                   4282:                fprintf(stderr, "HPP: internal error, state == ENTITY_DECL\n");
                   4283:                ctxt->instate = XML_PARSER_CONTENT;
                   4284:                ctxt->checkIndex = 0;
                   4285: #ifdef DEBUG_PUSH
                   4286:                fprintf(stderr, "HPP: entering CONTENT\n");
                   4287: #endif
                   4288:                break;
                   4289:             case XML_PARSER_ENTITY_VALUE:
                   4290:                fprintf(stderr, "HPP: internal error, state == ENTITY_VALUE\n");
                   4291:                ctxt->instate = XML_PARSER_CONTENT;
                   4292:                ctxt->checkIndex = 0;
                   4293: #ifdef DEBUG_PUSH
                   4294:                fprintf(stderr, "HPP: entering DTD\n");
                   4295: #endif
                   4296:                break;
                   4297:             case XML_PARSER_ATTRIBUTE_VALUE:
                   4298:                fprintf(stderr, "HPP: internal error, state == ATTRIBUTE_VALUE\n");
                   4299:                ctxt->instate = XML_PARSER_START_TAG;
                   4300:                ctxt->checkIndex = 0;
                   4301: #ifdef DEBUG_PUSH
                   4302:                fprintf(stderr, "HPP: entering START_TAG\n");
1.53      veillard 4303: #endif
                   4304:                break;
                   4305:            case XML_PARSER_SYSTEM_LITERAL:
                   4306:                fprintf(stderr, "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
                   4307:                ctxt->instate = XML_PARSER_CONTENT;
                   4308:                ctxt->checkIndex = 0;
                   4309: #ifdef DEBUG_PUSH
                   4310:                fprintf(stderr, "HPP: entering CONTENT\n");
1.31      daniel   4311: #endif
                   4312:                break;
                   4313:        }
                   4314:     }
                   4315: done:    
1.47      daniel   4316:     if ((avail == 0) && (terminate)) {
                   4317:        htmlAutoClose(ctxt, NULL);
1.54      veillard 4318:        if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   4319:            /*
                   4320:             * SAX: end of the document processing.
                   4321:             */
1.47      daniel   4322:            ctxt->instate = XML_PARSER_EOF;
1.54      veillard 4323:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4324:                ctxt->sax->endDocument(ctxt->userData);
                   4325:        }
1.59      veillard 4326:     }
                   4327:     if ((ctxt->myDoc != NULL) &&
                   4328:        ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
                   4329:         (ctxt->instate == XML_PARSER_EPILOG))) {
                   4330:        xmlDtdPtr dtd;
                   4331:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   4332:        if (dtd == NULL)
                   4333:            ctxt->myDoc->intSubset = 
                   4334:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   4335:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   4336:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
1.47      daniel   4337:     }
1.31      daniel   4338: #ifdef DEBUG_PUSH
                   4339:     fprintf(stderr, "HPP: done %d\n", ret);
                   4340: #endif
                   4341:     return(ret);
                   4342: }
                   4343: 
                   4344: /**
1.32      daniel   4345:  * htmlParseTry:
                   4346:  * @ctxt:  an HTML parser context
                   4347:  *
                   4348:  * Try to progress on parsing
                   4349:  *
                   4350:  * Returns zero if no parsing was possible
                   4351:  */
                   4352: int
                   4353: htmlParseTry(htmlParserCtxtPtr ctxt) {
                   4354:     return(htmlParseTryOrFinish(ctxt, 0));
                   4355: }
                   4356: 
                   4357: /**
1.31      daniel   4358:  * htmlParseChunk:
                   4359:  * @ctxt:  an XML parser context
                   4360:  * @chunk:  an char array
                   4361:  * @size:  the size in byte of the chunk
                   4362:  * @terminate:  last chunk indicator
                   4363:  *
                   4364:  * Parse a Chunk of memory
                   4365:  *
                   4366:  * Returns zero if no error, the xmlParserErrors otherwise.
                   4367:  */
                   4368: int
                   4369: htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
                   4370:               int terminate) {
                   4371:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4372:         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
                   4373:        int base = ctxt->input->base - ctxt->input->buf->buffer->content;
                   4374:        int cur = ctxt->input->cur - ctxt->input->base;
                   4375:        
                   4376:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4377:        ctxt->input->base = ctxt->input->buf->buffer->content + base;
                   4378:        ctxt->input->cur = ctxt->input->base + cur;
                   4379: #ifdef DEBUG_PUSH
                   4380:        fprintf(stderr, "HPP: pushed %d\n", size);
                   4381: #endif
                   4382: 
1.34      daniel   4383:        if ((terminate) || (ctxt->input->buf->buffer->use > 80))
                   4384:            htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4385:     } else if (ctxt->instate != XML_PARSER_EOF) {
                   4386:        xmlParserInputBufferPush(ctxt->input->buf, 0, "");
1.32      daniel   4387:         htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4388:     }
1.31      daniel   4389:     if (terminate) {
                   4390:        if ((ctxt->instate != XML_PARSER_EOF) &&
                   4391:            (ctxt->instate != XML_PARSER_EPILOG) &&
                   4392:            (ctxt->instate != XML_PARSER_MISC)) {
1.67      veillard 4393:            ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   4394:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4395:                ctxt->sax->error(ctxt->userData,
                   4396:                    "Extra content at the end of the document\n");
                   4397:            ctxt->wellFormed = 0;
                   4398:        } 
                   4399:        if (ctxt->instate != XML_PARSER_EOF) {
                   4400:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4401:                ctxt->sax->endDocument(ctxt->userData);
                   4402:        }
                   4403:        ctxt->instate = XML_PARSER_EOF;
                   4404:     }
                   4405:     return((xmlParserErrors) ctxt->errNo);           
                   4406: }
                   4407: 
                   4408: /************************************************************************
                   4409:  *                                                                     *
                   4410:  *                     User entry points                               *
                   4411:  *                                                                     *
                   4412:  ************************************************************************/
                   4413: 
                   4414: /**
                   4415:  * htmlCreatePushParserCtxt :
                   4416:  * @sax:  a SAX handler
                   4417:  * @user_data:  The user data returned on SAX callbacks
                   4418:  * @chunk:  a pointer to an array of chars
                   4419:  * @size:  number of chars in the array
                   4420:  * @filename:  an optional file name or URI
                   4421:  * @enc:  an optional encoding
                   4422:  *
                   4423:  * Create a parser context for using the HTML parser in push mode
                   4424:  * To allow content encoding detection, @size should be >= 4
                   4425:  * The value of @filename is used for fetching external entities
                   4426:  * and error/warning reports.
                   4427:  *
                   4428:  * Returns the new parser context or NULL
                   4429:  */
                   4430: htmlParserCtxtPtr
                   4431: htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, 
                   4432:                          const char *chunk, int size, const char *filename,
                   4433:                         xmlCharEncoding enc) {
                   4434:     htmlParserCtxtPtr ctxt;
                   4435:     htmlParserInputPtr inputStream;
                   4436:     xmlParserInputBufferPtr buf;
                   4437: 
                   4438:     buf = xmlAllocParserInputBuffer(enc);
                   4439:     if (buf == NULL) return(NULL);
                   4440: 
                   4441:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
                   4442:     if (ctxt == NULL) {
                   4443:        xmlFree(buf);
                   4444:        return(NULL);
                   4445:     }
                   4446:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   4447:     htmlInitParserCtxt(ctxt);
                   4448:     if (sax != NULL) {
                   4449:        if (ctxt->sax != &htmlDefaultSAXHandler)
                   4450:            xmlFree(ctxt->sax);
                   4451:        ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
                   4452:        if (ctxt->sax == NULL) {
                   4453:            xmlFree(buf);
                   4454:            xmlFree(ctxt);
                   4455:            return(NULL);
                   4456:        }
                   4457:        memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
                   4458:        if (user_data != NULL)
                   4459:            ctxt->userData = user_data;
                   4460:     }  
                   4461:     if (filename == NULL) {
                   4462:        ctxt->directory = NULL;
                   4463:     } else {
                   4464:         ctxt->directory = xmlParserGetDirectory(filename);
                   4465:     }
                   4466: 
                   4467:     inputStream = htmlNewInputStream(ctxt);
                   4468:     if (inputStream == NULL) {
                   4469:        xmlFreeParserCtxt(ctxt);
                   4470:        return(NULL);
                   4471:     }
                   4472: 
                   4473:     if (filename == NULL)
                   4474:        inputStream->filename = NULL;
                   4475:     else
                   4476:        inputStream->filename = xmlMemStrdup(filename);
                   4477:     inputStream->buf = buf;
                   4478:     inputStream->base = inputStream->buf->buffer->content;
                   4479:     inputStream->cur = inputStream->buf->buffer->content;
                   4480: 
                   4481:     inputPush(ctxt, inputStream);
                   4482: 
                   4483:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4484:         (ctxt->input->buf != NULL))  {       
                   4485:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4486: #ifdef DEBUG_PUSH
                   4487:        fprintf(stderr, "HPP: pushed %d\n", size);
                   4488: #endif
                   4489:     }
                   4490: 
                   4491:     return(ctxt);
                   4492: }
1.1       daniel   4493: 
                   4494: /**
                   4495:  * htmlSAXParseDoc :
1.14      daniel   4496:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4497:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4498:  * @sax:  the SAX handler block
                   4499:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4500:  *
                   4501:  * parse an HTML in-memory document and build a tree.
                   4502:  * It use the given SAX function block to handle the parsing callback.
                   4503:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4504:  * 
                   4505:  * Returns the resulting document tree
                   4506:  */
                   4507: 
                   4508: htmlDocPtr
1.14      daniel   4509: htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
1.1       daniel   4510:     htmlDocPtr ret;
                   4511:     htmlParserCtxtPtr ctxt;
                   4512: 
                   4513:     if (cur == NULL) return(NULL);
                   4514: 
                   4515: 
                   4516:     ctxt = htmlCreateDocParserCtxt(cur, encoding);
                   4517:     if (ctxt == NULL) return(NULL);
                   4518:     if (sax != NULL) { 
                   4519:         ctxt->sax = sax;
                   4520:         ctxt->userData = userData;
                   4521:     }
                   4522: 
                   4523:     htmlParseDocument(ctxt);
                   4524:     ret = ctxt->myDoc;
                   4525:     if (sax != NULL) {
                   4526:        ctxt->sax = NULL;
                   4527:        ctxt->userData = NULL;
                   4528:     }
                   4529:     htmlFreeParserCtxt(ctxt);
                   4530:     
                   4531:     return(ret);
                   4532: }
                   4533: 
                   4534: /**
                   4535:  * htmlParseDoc :
1.14      daniel   4536:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4537:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4538:  *
                   4539:  * parse an HTML in-memory document and build a tree.
                   4540:  * 
                   4541:  * Returns the resulting document tree
                   4542:  */
                   4543: 
                   4544: htmlDocPtr
1.14      daniel   4545: htmlParseDoc(xmlChar *cur, const char *encoding) {
1.1       daniel   4546:     return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
                   4547: }
                   4548: 
                   4549: 
                   4550: /**
                   4551:  * htmlCreateFileParserCtxt :
                   4552:  * @filename:  the filename
                   4553:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4554:  *
                   4555:  * Create a parser context for a file content. 
                   4556:  * Automatic support for ZLIB/Compress compressed document is provided
                   4557:  * by default if found at compile-time.
                   4558:  *
                   4559:  * Returns the new parser context or NULL
                   4560:  */
                   4561: htmlParserCtxtPtr
                   4562: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
                   4563: {
                   4564:     htmlParserCtxtPtr ctxt;
                   4565:     htmlParserInputPtr inputStream;
1.5       daniel   4566:     xmlParserInputBufferPtr buf;
1.1       daniel   4567:     /* htmlCharEncoding enc; */
                   4568: 
1.5       daniel   4569:     buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
                   4570:     if (buf == NULL) return(NULL);
1.1       daniel   4571: 
1.11      daniel   4572:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   4573:     if (ctxt == NULL) {
                   4574:         perror("malloc");
                   4575:        return(NULL);
                   4576:     }
1.19      daniel   4577:     memset(ctxt, 0, sizeof(htmlParserCtxt));
1.1       daniel   4578:     htmlInitParserCtxt(ctxt);
1.11      daniel   4579:     inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   4580:     if (inputStream == NULL) {
                   4581:         perror("malloc");
1.11      daniel   4582:        xmlFree(ctxt);
1.1       daniel   4583:        return(NULL);
                   4584:     }
1.19      daniel   4585:     memset(inputStream, 0, sizeof(htmlParserInput));
1.1       daniel   4586: 
1.11      daniel   4587:     inputStream->filename = xmlMemStrdup(filename);
1.1       daniel   4588:     inputStream->line = 1;
                   4589:     inputStream->col = 1;
1.5       daniel   4590:     inputStream->buf = buf;
1.21      daniel   4591:     inputStream->directory = NULL;
1.1       daniel   4592: 
1.5       daniel   4593:     inputStream->base = inputStream->buf->buffer->content;
                   4594:     inputStream->cur = inputStream->buf->buffer->content;
                   4595:     inputStream->free = NULL;
1.1       daniel   4596: 
                   4597:     inputPush(ctxt, inputStream);
                   4598:     return(ctxt);
                   4599: }
                   4600: 
                   4601: /**
                   4602:  * htmlSAXParseFile :
                   4603:  * @filename:  the filename
                   4604:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4605:  * @sax:  the SAX handler block
                   4606:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4607:  *
                   4608:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4609:  * compressed document is provided by default if found at compile-time.
                   4610:  * It use the given SAX function block to handle the parsing callback.
                   4611:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4612:  *
                   4613:  * Returns the resulting document tree
                   4614:  */
                   4615: 
                   4616: htmlDocPtr
                   4617: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, 
                   4618:                  void *userData) {
                   4619:     htmlDocPtr ret;
                   4620:     htmlParserCtxtPtr ctxt;
1.57      veillard 4621:     htmlSAXHandlerPtr oldsax = NULL;
1.1       daniel   4622: 
                   4623:     ctxt = htmlCreateFileParserCtxt(filename, encoding);
                   4624:     if (ctxt == NULL) return(NULL);
                   4625:     if (sax != NULL) {
1.55      veillard 4626:        oldsax = ctxt->sax;
1.1       daniel   4627:         ctxt->sax = sax;
                   4628:         ctxt->userData = userData;
                   4629:     }
                   4630: 
                   4631:     htmlParseDocument(ctxt);
                   4632: 
                   4633:     ret = ctxt->myDoc;
                   4634:     if (sax != NULL) {
1.55      veillard 4635:         ctxt->sax = oldsax;
1.1       daniel   4636:         ctxt->userData = NULL;
                   4637:     }
                   4638:     htmlFreeParserCtxt(ctxt);
                   4639:     
                   4640:     return(ret);
                   4641: }
                   4642: 
                   4643: /**
                   4644:  * htmlParseFile :
                   4645:  * @filename:  the filename
                   4646:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4647:  *
                   4648:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4649:  * compressed document is provided by default if found at compile-time.
                   4650:  *
                   4651:  * Returns the resulting document tree
                   4652:  */
                   4653: 
                   4654: htmlDocPtr
                   4655: htmlParseFile(const char *filename, const char *encoding) {
                   4656:     return(htmlSAXParseFile(filename, encoding, NULL, NULL));
                   4657: }
1.39      daniel   4658: 
                   4659: #endif /* LIBXML_HTML_ENABLED */

Webmaster