Annotation of XML/HTMLparser.c, revision 1.58

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

Webmaster