Annotation of XML/HTMLparser.c, revision 1.3

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
                     10: #define HAVE_FCNTL_H
                     11: #include <io.h>
                     12: #else
                     13: #include <config.h>
                     14: #endif
                     15: #include <stdio.h>
                     16: #include <ctype.h>
                     17: #include <string.h> /* for memset() only */
                     18: #include <stdlib.h>
                     19: #include <sys/stat.h>
                     20: #ifdef HAVE_FCNTL_H
                     21: #include <fcntl.h>
                     22: #endif
                     23: #ifdef HAVE_UNISTD_H
                     24: #include <unistd.h>
                     25: #endif
                     26: #ifdef HAVE_ZLIB_H
                     27: #include <zlib.h>
                     28: #endif
                     29: 
                     30: #include "tree.h"
                     31: #include "HTMLparser.h"
                     32: #include "entities.h"
                     33: #include "encoding.h"
                     34: #include "valid.h"
                     35: #include "parserInternals.h"
                     36: 
                     37: /* #define DEBUG */
                     38: 
                     39: /************************************************************************
                     40:  *                                                                     *
                     41:  *             Parser stacks related functions and macros              *
                     42:  *                                                                     *
                     43:  ************************************************************************/
                     44: 
                     45: /*
                     46:  * Generic function for accessing stacks in the Parser Context
                     47:  */
                     48: 
                     49: #define PUSH_AND_POP(type, name)                                       \
                     50: int html##name##Push(htmlParserCtxtPtr ctxt, type value) {             \
                     51:     if (ctxt->name##Nr >= ctxt->name##Max) {                           \
                     52:        ctxt->name##Max *= 2;                                           \
                     53:         ctxt->name##Tab = (void *) realloc(ctxt->name##Tab,            \
                     54:                     ctxt->name##Max * sizeof(ctxt->name##Tab[0]));     \
                     55:         if (ctxt->name##Tab == NULL) {                                 \
                     56:            fprintf(stderr, "realloc failed !\n");                      \
                     57:            exit(1);                                                    \
                     58:        }                                                               \
                     59:     }                                                                  \
                     60:     ctxt->name##Tab[ctxt->name##Nr] = value;                           \
                     61:     ctxt->name = value;                                                        \
                     62:     return(ctxt->name##Nr++);                                          \
                     63: }                                                                      \
                     64: type html##name##Pop(htmlParserCtxtPtr ctxt) {                         \
                     65:     type ret;                                                          \
                     66:     if (ctxt->name##Nr <= 0) return(0);                                        \
                     67:     ctxt->name##Nr--;                                                  \
                     68:     if (ctxt->name##Nr > 0)                                            \
                     69:        ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1];               \
                     70:     else                                                               \
                     71:         ctxt->name = NULL;                                             \
                     72:     ret = ctxt->name##Tab[ctxt->name##Nr];                             \
                     73:     ctxt->name##Tab[ctxt->name##Nr] = 0;                               \
                     74:     return(ret);                                                       \
                     75: }                                                                      \
                     76: 
                     77: PUSH_AND_POP(xmlNodePtr, node)
                     78: 
                     79: /*
                     80:  * Macros for accessing the content. Those should be used only by the parser,
                     81:  * and not exported.
                     82:  *
                     83:  * Dirty macros, i.e. one need to make assumption on the context to use them
                     84:  *
                     85:  *   CUR_PTR return the current pointer to the CHAR to be parsed.
                     86:  *   CUR     returns the current CHAR value, i.e. a 8 bit value if compiled
                     87:  *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
                     88:  *           in UNICODE mode. This should be used internally by the parser
                     89:  *           only to compare to ASCII values otherwise it would break when
                     90:  *           running with UTF-8 encoding.
                     91:  *   NXT(n)  returns the n'th next CHAR. Same as CUR is should be used only
                     92:  *           to compare on ASCII based substring.
                     93:  *   UPP(n)  returns the n'th next CHAR converted to uppercase. Same as CUR
                     94:  *           it should be used only to compare on ASCII based substring.
                     95:  *   SKIP(n) Skip n CHAR, and must also be used only to skip ASCII defined
                     96:  *           strings within the parser.
                     97:  *
                     98:  * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
                     99:  *
                    100:  *   CURRENT Returns the current char value, with the full decoding of
                    101:  *           UTF-8 if we are using this mode. It returns an int.
                    102:  *   NEXT    Skip to the next character, this does the proper decoding
                    103:  *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
                    104:  *           It returns the pointer to the current CHAR.
                    105:  *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
                    106:  */
                    107: 
                    108: #define CUR (*ctxt->input->cur)
                    109: #define UPPER (toupper(*ctxt->input->cur))
                    110: #define SKIP(val) ctxt->input->cur += (val)
                    111: #define NXT(val) ctxt->input->cur[(val)]
                    112: #define UPP(val) (toupper(ctxt->input->cur[(val)]))
                    113: #define CUR_PTR ctxt->input->cur
                    114: 
                    115: #define SKIP_BLANKS                                                    \
                    116:     while (IS_BLANK(*(ctxt->input->cur))) NEXT
                    117: 
                    118: #ifndef USE_UTF_8
                    119: #define CURRENT (*ctxt->input->cur)
                    120: #define NEXT ((*ctxt->input->cur) ?                                    \
                    121:                 (((*(ctxt->input->cur) == '\n') ?                      \
                    122:                    (ctxt->input->line++, ctxt->input->col = 1) :       \
                    123:                    (ctxt->input->col++)), ctxt->input->cur++) :        \
                    124:                (ctxt->input->cur))
                    125: #else
                    126: #endif
                    127: 
                    128: 
                    129: /************************************************************************
                    130:  *                                                                     *
                    131:  *             The list of HTML elements and their properties          *
                    132:  *                                                                     *
                    133:  ************************************************************************/
                    134: 
                    135: /*
                    136:  *  Start Tag: 1 means the start tag can be ommited
                    137:  *  End Tag:   1 means the end tag can be ommited
                    138:  *             2 means it's forbidden (empty elements)
                    139:  *  Depr:      this element is deprecated
                    140:  *  DTD:       1 means that this element is valid only in the Loose DTD
                    141:  *             2 means that this element is valid only in the Frameset DTD
                    142:  *
                    143:  * Name,Start Tag,End Tag,  Empty,  Depr.,    DTD, Description
                    144:  */
                    145: htmlElemDesc  html40ElementTable[] = {
                    146: { "A",         0,      0,      0,      0,      0, "anchor " },
                    147: { "ABBR",      0,      0,      0,      0,      0, "abbreviated form" },
                    148: { "ACRONYM",   0,      0,      0,      0,      0, "" },
                    149: { "ADDRESS",   0,      0,      0,      0,      0, "information on author " },
                    150: { "APPLET",    0,      0,      0,      1,      1, "Java applet " },
                    151: { "AREA",      0,      2,      1,      0,      0, "client-side image map area " },
                    152: { "B",         0,      0,      0,      0,      0, "bold text style" },
                    153: { "BASE",      0,      2,      1,      0,      0, "document base URI " },
                    154: { "BASEFONT",  0,      2,      1,      1,      1, "base font size " },
                    155: { "BDO",       0,      0,      0,      0,      0, "I18N BiDi over-ride " },
                    156: { "BIG",       0,      0,      0,      0,      0, "large text style" },
                    157: { "BLOCKQUOTE",        0,      0,      0,      0,      0, "long quotation " },
                    158: { "BODY",      1,      1,      0,      0,      0, "document body " },
                    159: { "BR",                0,      2,      1,      0,      0, "forced line break " },
                    160: { "BUTTON",    0,      0,      0,      0,      0, "push button " },
                    161: { "CAPTION",   0,      0,      0,      0,      0, "table caption " },
                    162: { "CENTER",    0,      0,      0,      1,      1, "shorthand for DIV align=center " },
                    163: { "CITE",      0,      0,      0,      0,      0, "citation" },
                    164: { "CODE",      0,      0,      0,      0,      0, "computer code fragment" },
                    165: { "COL",       0,      2,      1,      0,      0, "table column " },
                    166: { "COLGROUP",  0,      1,      0,      0,      0, "table column group " },
                    167: { "DD",                0,      1,      0,      0,      0, "definition description " },
                    168: { "DEL",       0,      0,      0,      0,      0, "deleted text " },
                    169: { "DFN",       0,      0,      0,      0,      0, "instance definition" },
                    170: { "DIR",       0,      0,      0,      1,      1, "directory list" },
                    171: { "DIV",       0,      0,      0,      0,      0, "generic language/style container"},
                    172: { "DL",                0,      0,      0,      0,      0, "definition list " },
                    173: { "DT",                0,      1,      0,      0,      0, "definition term " },
                    174: { "EM",                0,      0,      0,      0,      0, "emphasis" },
                    175: { "FIELDSET",  0,      0,      0,      0,      0, "form control group " },
                    176: { "FONT",      0,      0,      0,      1,      1, "local change to font " },
                    177: { "FORM",      0,      0,      0,      0,      0, "interactive form " },
                    178: { "FRAME",     0,      2,      1,      0,      2, "subwindow " },
                    179: { "FRAMESET",  0,      0,      0,      0,      2, "window subdivision" },
                    180: { "H1",                0,      0,      0,      0,      0, "heading " },
                    181: { "H2",                0,      0,      0,      0,      0, "heading " },
                    182: { "H3",                0,      0,      0,      0,      0, "heading " },
                    183: { "H4",                0,      0,      0,      0,      0, "heading " },
                    184: { "H5",                0,      0,      0,      0,      0, "heading " },
                    185: { "H6",                0,      0,      0,      0,      0, "heading " },
                    186: { "HEAD",      1,      1,      0,      0,      0, "document head " },
                    187: { "HR",                0,      2,      1,      0,      0, "horizontal rule " },
                    188: { "HTML",      1,      1,      0,      0,      0, "document root element " },
                    189: { "I",         0,      0,      0,      0,      0, "italic text style" },
                    190: { "IFRAME",    0,      0,      0,      0,      1, "inline subwindow " },
                    191: { "IMG",       0,      2,      1,      0,      0, "Embedded image " },
                    192: { "INPUT",     0,      2,      1,      0,      0, "form control " },
                    193: { "INS",       0,      0,      0,      0,      0, "inserted text" },
                    194: { "ISINDEX",   0,      2,      1,      1,      1, "single line prompt " },
                    195: { "KBD",       0,      0,      0,      0,      0, "text to be entered by the user" },
                    196: { "LABEL",     0,      0,      0,      0,      0, "form field label text " },
                    197: { "LEGEND",    0,      0,      0,      0,      0, "fieldset legend " },
                    198: { "LI",                0,      1,      0,      0,      0, "list item " },
                    199: { "LINK",      0,      2,      1,      0,      0, "a media-independent link " },
                    200: { "MAP",       0,      0,      0,      0,      0, "client-side image map " },
                    201: { "MENU",      0,      0,      0,      1,      1, "menu list " },
                    202: { "META",      0,      2,      1,      0,      0, "generic metainformation " },
                    203: { "NOFRAMES",  0,      0,      0,      0,      2, "alternate content container for non frame-based rendering " },
                    204: { "NOSCRIPT",  0,      0,      0,      0,      0, "alternate content container for non script-based rendering " },
                    205: { "OBJECT",    0,      0,      0,      0,      0, "generic embedded object " },
                    206: { "OL",                0,      0,      0,      0,      0, "ordered list " },
                    207: { "OPTGROUP",  0,      0,      0,      0,      0, "option group " },
                    208: { "OPTION",    0,      1,      0,      0,      0, "selectable choice " },
                    209: { "P",         0,      1,      0,      0,      0, "paragraph " },
                    210: { "PARAM",     0,      2,      1,      0,      0, "named property value " },
                    211: { "PRE",       0,      0,      0,      0,      0, "preformatted text " },
                    212: { "Q",         0,      0,      0,      0,      0, "short inline quotation " },
                    213: { "S",         0,      0,      0,      1,      1, "strike-through text style" },
                    214: { "SAMP",      0,      0,      0,      0,      0, "sample program output, scripts, etc." },
                    215: { "SCRIPT",    0,      0,      0,      0,      0, "script statements " },
                    216: { "SELECT",    0,      0,      0,      0,      0, "option selector " },
                    217: { "SMALL",     0,      0,      0,      0,      0, "small text style" },
                    218: { "SPAN",      0,      0,      0,      0,      0, "generic language/style container " },
                    219: { "STRIKE",    0,      0,      0,      1,      1, "strike-through text" },
                    220: { "STRONG",    0,      0,      0,      0,      0, "strong emphasis" },
                    221: { "STYLE",     0,      0,      0,      0,      0, "style info " },
                    222: { "SUB",       0,      0,      0,      0,      0, "subscript" },
                    223: { "SUP",       0,      0,      0,      0,      0, "superscript " },
                    224: { "TABLE",     0,      0,      0,      0,      0, "&#160;" },
                    225: { "TBODY",     1,      1,      0,      0,      0, "table body " },
                    226: { "TD",                0,      1,      0,      0,      0, "table data cell" },
                    227: { "TEXTAREA",  0,      0,      0,      0,      0, "multi-line text field " },
                    228: { "TFOOT",     0,      1,      0,      0,      0, "table footer " },
                    229: { "TH",                0,      1,      0,      0,      0, "table header cell" },
                    230: { "THEAD",     0,      1,      0,      0,      0, "table header " },
                    231: { "TITLE",     0,      0,      0,      0,      0, "document title " },
                    232: { "TR",                0,      1,      0,      0,      0, "table row " },
                    233: { "TT",                0,      0,      0,      0,      0, "teletype or monospaced text style" },
                    234: { "U",         0,      0,      0,      1,      1, "underlined text style" },
                    235: { "UL",                0,      0,      0,      0,      0, "unordered list " },
                    236: { "VAR",       0,      0,      0,      0,      0, "instance of a variable or program argument" },
                    237: };
                    238: 
                    239: /*
                    240:  * start tags that imply the end of a current element
                    241:  * any tag of each line implies the end of the current element if the type of
                    242:  * that element is in the same line
                    243:  */
                    244: CHAR *htmlEquEnd[] = {
                    245: "DT", "DD", "LI", "OPTION", NULL,
                    246: "H1", "H2", "H3", "H4", "H5", "H6", NULL,
                    247: "OL", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP", NULL,
                    248: NULL
                    249: };
                    250: /*
                    251:  * acording the HTML DTD, HR should be added to the 2nd line above, as it
                    252:  * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
                    253:  * because many documents contain rules in headings...
                    254:  */
                    255: 
                    256: /*
                    257:  * start tags that imply the end of current element
                    258:  */
                    259: CHAR *htmlStartClose[] = {
                    260: "FORM",                "FORM", "P", "HR", "H1", "H2", "H3", "H4", "H5", "H6",
                    261:                "DL", "UL", "OL", "MENU", "DIR", "ADDRESS", "PRE",
                    262:                "LISTING", "XMP", "HEAD", NULL,
                    263: "HEAD",                "P", NULL,
                    264: "TITLE",       "P", NULL,
                    265: "BODY",                "HEAD", "STYLE", "LINK", "TITLE", "P", NULL,
                    266: "LI",          "P", "H1", "H2", "H3", "H4", "H5", "H6", "DL", "ADDRESS",
                    267:                "PRE", "LISTING", "XMP", "HEAD", NULL,
                    268: "HR",          "P", "HEAD", NULL,
                    269: "H1",          "P", "HEAD", NULL,
                    270: "H2",          "P", "HEAD", NULL,
                    271: "H3",          "P", "HEAD", NULL,
                    272: "H4",          "P", "HEAD", NULL,
                    273: "H5",          "P", "HEAD", NULL,
                    274: "H6",          "P", "HEAD", NULL,
                    275: "DIR",         "P", "HEAD", NULL,
                    276: "ADDRESS",     "P", "HEAD", "UL", NULL,
                    277: "PRE",         "P", "HEAD", "UL", NULL,
                    278: "LISTING",     "P", "HEAD", NULL,
                    279: "XMP",         "P", "HEAD", NULL,
                    280: "BLOCKQUOTE",  "P", "HEAD", NULL,
                    281: "DL",          "P", "DT", "MENU", "DIR", "ADDRESS", "PRE", "LISTING",
                    282:                "XMP", "HEAD", NULL,
                    283: "DT",          "P", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP", "HEAD", NULL,
                    284: "DD",          "P", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP", "HEAD", NULL,
                    285: "UL",          "P", "HEAD", "OL", "MENU", "DIR", "ADDRESS", "PRE",
                    286:                "LISTING", "XMP", NULL,
                    287: "OL",          "P", "HEAD", "UL", NULL,
                    288: "MENU",                "P", "HEAD", "UL", NULL,
                    289: "P",           "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6", NULL,
                    290: "DIV",         "P", "HEAD", NULL,
                    291: "NOSCRIPT",    "P", "HEAD", NULL,
                    292: "CENTER",      "FONT", "B", "I", "P", "HEAD", NULL,
                    293: "A",           "A", NULL,
                    294: "CAPTION",     "P", NULL,
                    295: "COLGROUP",    "CAPTION", "COLGROUP", "COL", "P", NULL,
                    296: "COL",         "CAPTION", "COL", "P", NULL,
                    297: "TABLE",       "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6", "PRE",
                    298:                "LISTING", "XMP", "A", NULL,
                    299: "TH",          "TH", "TD", NULL,
                    300: "TD",          "TH", "TD", NULL,
                    301: "TR",          "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", NULL,
                    302: "THEAD",       "CAPTION", "COL", "COLGROUP", NULL,
                    303: "TFOOT",       "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", "THEAD",
                    304:                "TBODY", NULL,
                    305: "TBODY",       "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", "THEAD",
                    306:                "TFOOT", "TBODY", NULL,
                    307: "OPTGROUP",    "OPTION", NULL,
                    308: "FIELDSET",    "LEGEND", "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6",
                    309:                "PRE", "LISTING", "XMP", "A", NULL,
                    310: NULL
                    311: };
                    312: 
                    313: static CHAR** htmlStartCloseIndex[100];
                    314: static int htmlStartCloseIndexinitialized = 0;
                    315: 
                    316: /************************************************************************
                    317:  *                                                                     *
                    318:  *             functions to handle HTML specific data                  *
                    319:  *                                                                     *
                    320:  ************************************************************************/
                    321: 
                    322: /**
                    323:  * htmlInitAutoClose:
                    324:  *
                    325:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
                    326:  *
                    327:  */
                    328: void
                    329: htmlInitAutoClose(void) {
                    330:     int index, i = 0;
                    331: 
                    332:     if (htmlStartCloseIndexinitialized) return;
                    333: 
                    334:     for (index = 0;index < 100;index ++) htmlStartCloseIndex[index] = NULL;
                    335:     index = 0;
                    336:     while ((htmlStartClose[i] != NULL) && (index < 100 - 1)) {
                    337:         htmlStartCloseIndex[index++] = &htmlStartClose[i];
                    338:        while (htmlStartClose[i] != NULL) i++;
                    339:        i++;
                    340:     }
                    341: }
                    342: 
                    343: /**
                    344:  * htmlTagLookup:
                    345:  * @tag:  The tag name
                    346:  *
                    347:  * Lookup the HTML tag in the ElementTable
                    348:  *
                    349:  * Returns the related htmlElemDescPtr or NULL if not found.
                    350:  */
                    351: htmlElemDescPtr
                    352: htmlTagLookup(const CHAR *tag) {
                    353:     int i = 0;
                    354: 
                    355:     for (i = 0; i < (sizeof(html40ElementTable) /
                    356:                      sizeof(html40ElementTable[0]));i++) {
                    357:         if (!xmlStrcmp(tag, html40ElementTable[i].name))
                    358:            return(&html40ElementTable[i]);
                    359:     }
                    360:     return(NULL);
                    361: }
                    362: 
                    363: /**
                    364:  * htmlCheckAutoClose:
                    365:  * @new:  The new tag name
                    366:  * @old:  The old tag name
                    367:  *
                    368:  * Checks wether the new tag is one of the registered valid tags for closing old.
                    369:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
                    370:  *
                    371:  * Returns 0 if no, 1 if yes.
                    372:  */
                    373: int
                    374: htmlCheckAutoClose(const CHAR *new, const CHAR *old) {
                    375:     int i, index;
                    376:     CHAR **close;
                    377: 
                    378:     if (htmlStartCloseIndexinitialized == 0) htmlInitAutoClose();
                    379: 
                    380:     /* inefficient, but not a big deal */
                    381:     for (index = 0; index < 100;index++) {
                    382:         close = htmlStartCloseIndex[index];
                    383:        if (close == NULL) return(0);
                    384:        if (!xmlStrcmp(*close, new)) break;
                    385:     }
                    386: 
                    387:     i = close - htmlStartClose;
                    388:     i++;
                    389:     while (htmlStartClose[i] != NULL) {
                    390:         if (!xmlStrcmp(htmlStartClose[i], old)) {
                    391:            return(1);
                    392:        }
                    393:        i++;
                    394:     }
                    395:     return(0);
                    396: }
                    397: 
                    398: /**
                    399:  * htmlAutoClose:
                    400:  * @ctxt:  an HTML parser context
                    401:  * @new:  The new tag name
                    402:  *
                    403:  * The HTmL DtD allows a tag to implicitely close other tags.
                    404:  * The list is kept in htmlStartClose array. This function is
                    405:  * called when a new tag has been detected and generates the
                    406:  * appropriates closes if possible/needed.
                    407:  */
                    408: void
                    409: htmlAutoClose(htmlParserCtxtPtr ctxt, const CHAR *new) {
                    410: 
                    411:     while ((ctxt->node != NULL) && 
                    412:            (htmlCheckAutoClose(new, ctxt->node->name))) {
                    413: #ifdef DEBUG
                    414:        printf("htmlAutoClose: %s closes %s\n", new, ctxt->node->name);
                    415: #endif
                    416:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                    417:            ctxt->sax->endElement(ctxt->userData, ctxt->node->name);
                    418:     }
                    419: }
                    420: 
                    421: /**
                    422:  * htmlAutoCloseOnClose:
                    423:  * @ctxt:  an HTML parser context
                    424:  * @new:  The new tag name
                    425:  *
                    426:  * The HTmL DtD allows an ending tag to implicitely close other tags.
                    427:  */
                    428: void
                    429: htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const CHAR *new) {
                    430:     htmlElemDescPtr info;
                    431: 
                    432:     while ((ctxt->node != NULL) && 
                    433:            (xmlStrcmp(new, ctxt->node->name))) {
                    434:        info = htmlTagLookup(ctxt->node->name);
                    435:        if ((info == NULL) || (info->endTag == 1)) {
                    436: #ifdef DEBUG
                    437:            printf("htmlAutoCloseOnClose: %s closes %s\n", new, ctxt->node->name);
                    438: #endif
                    439:            if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                    440:                ctxt->sax->endElement(ctxt->userData, ctxt->node->name);
                    441:         } else
                    442:            break;
                    443:     }
                    444: }
                    445: 
                    446: /************************************************************************
                    447:  *                                                                     *
                    448:  *             The list of HTML predefined entities                    *
                    449:  *                                                                     *
                    450:  ************************************************************************/
                    451: 
                    452: 
                    453: htmlEntityDesc  html40EntitiesTable[] = {
                    454: /*
                    455:  * the 4 absolute ones,
                    456:  */
                    457: { 34,  "quot", "quotation mark = APL quote, U+0022 ISOnum" },
                    458: { 38,  "amp",  "ampersand, U+0026 ISOnum" },
                    459: { 60,  "lt",   "less-than sign, U+003C ISOnum" },
                    460: { 62,  "gt",   "greater-than sign, U+003E ISOnum" },
                    461: 
                    462: /*
                    463:  * A bunch still in the 128-255 range
                    464:  * Replacing them depend really on the charset used.
                    465:  */
                    466: { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
                    467: { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
                    468: { 162, "cent", "cent sign, U+00A2 ISOnum" },
                    469: { 163, "pound","pound sign, U+00A3 ISOnum" },
                    470: { 164, "curren","currency sign, U+00A4 ISOnum" },
                    471: { 165, "yen",  "yen sign = yuan sign, U+00A5 ISOnum" },
                    472: { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
                    473: { 167, "sect", "section sign, U+00A7 ISOnum" },
                    474: { 168, "uml",  "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
                    475: { 169, "copy", "copyright sign, U+00A9 ISOnum" },
                    476: { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
                    477: { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
                    478: { 172, "not",  "not sign, U+00AC ISOnum" },
                    479: { 173, "shy",  "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
                    480: { 174, "reg",  "registered sign = registered trade mark sign, U+00AE ISOnum" },
                    481: { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
                    482: { 176, "deg",  "degree sign, U+00B0 ISOnum" },
                    483: { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
                    484: { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
                    485: { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
                    486: { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
                    487: { 181, "micro","micro sign, U+00B5 ISOnum" },
                    488: { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
                    489: { 183, "middot","middle dot = Georgian comma
                    490:                                   = Greek middle dot, U+00B7 ISOnum" },
                    491: { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
                    492: { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
                    493: { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
                    494: { 187, "raquo","right-pointing double angle quotation mark
                    495:                                   = right pointing guillemet, U+00BB ISOnum" },
                    496: { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
                    497: { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
                    498: { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
                    499: { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
                    500: { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
                    501: { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
                    502: { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
                    503: { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
                    504: { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
                    505: { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
                    506: { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
                    507: { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
                    508: { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
                    509: { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
                    510: { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
                    511: { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
                    512: { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
                    513: { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
                    514: { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
                    515: { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
                    516: { 208, "ETH",  "latin capital letter ETH, U+00D0 ISOlat1" },
                    517: { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
                    518: { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
                    519: { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
                    520: { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
                    521: { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
                    522: { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
                    523: { 215, "times","multiplication sign, U+00D7 ISOnum" },
                    524: { 216, "Oslash","latin capital letter O with stroke = latin capital letter O slash, U+00D8 ISOlat1" },
                    525: { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
                    526: { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
                    527: { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
                    528: { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
                    529: { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
                    530: { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
                    531: { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
                    532: { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
                    533: { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
                    534: { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
                    535: { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
                    536: { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
                    537: { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
                    538: { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
                    539: { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
                    540: { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
                    541: { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
                    542: { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
                    543: { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
                    544: { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
                    545: { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
                    546: { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
                    547: { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
                    548: { 240, "eth",  "latin small letter eth, U+00F0 ISOlat1" },
                    549: { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
                    550: { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
                    551: { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
                    552: { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
                    553: { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
                    554: { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
                    555: { 247, "divide","division sign, U+00F7 ISOnum" },
                    556: { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
                    557: { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
                    558: { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
                    559: { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
                    560: { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
                    561: { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
                    562: { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
                    563: { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
                    564: 
                    565: /*
                    566:  * Anything below should really be kept as entities references
                    567:  */
                    568: { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
                    569: 
                    570: { 913, "Alpha","greek capital letter alpha, U+0391" },
                    571: { 914, "Beta", "greek capital letter beta, U+0392" },
                    572: { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
                    573: { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
                    574: { 917, "Epsilon","greek capital letter epsilon, U+0395" },
                    575: { 918, "Zeta", "greek capital letter zeta, U+0396" },
                    576: { 919, "Eta",  "greek capital letter eta, U+0397" },
                    577: { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
                    578: { 921, "Iota", "greek capital letter iota, U+0399" },
                    579: { 922, "Kappa","greek capital letter kappa, U+039A" },
                    580: { 923, "Lambda""greek capital letter lambda, U+039B ISOgrk3" },
                    581: { 924, "Mu",   "greek capital letter mu, U+039C" },
                    582: { 925, "Nu",   "greek capital letter nu, U+039D" },
                    583: { 926, "Xi",   "greek capital letter xi, U+039E ISOgrk3" },
                    584: { 927, "Omicron","greek capital letter omicron, U+039F" },
                    585: { 928, "Pi",   "greek capital letter pi, U+03A0 ISOgrk3" },
                    586: { 929, "Rho",  "greek capital letter rho, U+03A1" },
                    587: { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
                    588: { 932, "Tau",  "greek capital letter tau, U+03A4" },
                    589: { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
                    590: { 934, "Phi",  "greek capital letter phi, U+03A6 ISOgrk3" },
                    591: { 935, "Chi",  "greek capital letter chi, U+03A7" },
                    592: { 936, "Psi",  "greek capital letter psi, U+03A8 ISOgrk3" },
                    593: { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
                    594: 
                    595: { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
                    596: { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
                    597: { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
                    598: { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
                    599: { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
                    600: { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
                    601: { 951, "eta",  "greek small letter eta, U+03B7 ISOgrk3" },
                    602: { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
                    603: { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
                    604: { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
                    605: { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
                    606: { 956, "mu",   "greek small letter mu, U+03BC ISOgrk3" },
                    607: { 957, "nu",   "greek small letter nu, U+03BD ISOgrk3" },
                    608: { 958, "xi",   "greek small letter xi, U+03BE ISOgrk3" },
                    609: { 959, "omicron","greek small letter omicron, U+03BF NEW" },
                    610: { 960, "pi",   "greek small letter pi, U+03C0 ISOgrk3" },
                    611: { 961, "rho",  "greek small letter rho, U+03C1 ISOgrk3" },
                    612: { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
                    613: { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
                    614: { 964, "tau",  "greek small letter tau, U+03C4 ISOgrk3" },
                    615: { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
                    616: { 966, "phi",  "greek small letter phi, U+03C6 ISOgrk3" },
                    617: { 967, "chi",  "greek small letter chi, U+03C7 ISOgrk3" },
                    618: { 968, "psi",  "greek small letter psi, U+03C8 ISOgrk3" },
                    619: { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
                    620: { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
                    621: { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
                    622: { 982, "piv",  "greek pi symbol, U+03D6 ISOgrk3" },
                    623: 
                    624: { 8226,        "bull", "bullet = black small circle, U+2022 ISOpub" },
                    625: { 8230,        "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
                    626: { 8242,        "prime","prime = minutes = feet, U+2032 ISOtech" },
                    627: { 8243,        "Prime","double prime = seconds = inches, U+2033 ISOtech" },
                    628: { 8254,        "oline","overline = spacing overscore, U+203E NEW" },
                    629: { 8260,        "frasl","fraction slash, U+2044 NEW" },
                    630: 
                    631: { 8472,        "weierp","script capital P = power set
                    632:                                      = Weierstrass p, U+2118 ISOamso" },
                    633: { 8465,        "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
                    634: { 8476,        "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
                    635: { 8482,        "trade","trade mark sign, U+2122 ISOnum" },
                    636: { 8501,        "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
                    637: { 8592,        "larr", "leftwards arrow, U+2190 ISOnum" },
                    638: { 8593,        "uarr", "upwards arrow, U+2191 ISOnum" },
                    639: { 8594,        "rarr", "rightwards arrow, U+2192 ISOnum" },
                    640: { 8595,        "darr", "downwards arrow, U+2193 ISOnum" },
                    641: { 8596,        "harr", "left right arrow, U+2194 ISOamsa" },
                    642: { 8629,        "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
                    643: { 8656,        "lArr", "leftwards double arrow, U+21D0 ISOtech" },
                    644: { 8657,        "uArr", "upwards double arrow, U+21D1 ISOamsa" },
                    645: { 8658,        "rArr", "rightwards double arrow, U+21D2 ISOtech" },
                    646: { 8659,        "dArr", "downwards double arrow, U+21D3 ISOamsa" },
                    647: { 8660,        "hArr", "left right double arrow, U+21D4 ISOamsa" },
                    648: 
                    649: 
                    650: { 8704,        "forall","for all, U+2200 ISOtech" },
                    651: { 8706,        "part", "partial differential, U+2202 ISOtech" },
                    652: { 8707,        "exist","there exists, U+2203 ISOtech" },
                    653: { 8709,        "empty","empty set = null set = diameter, U+2205 ISOamso" },
                    654: { 8711,        "nabla","nabla = backward difference, U+2207 ISOtech" },
                    655: { 8712,        "isin", "element of, U+2208 ISOtech" },
                    656: { 8713,        "notin","not an element of, U+2209 ISOtech" },
                    657: { 8715,        "ni",   "contains as member, U+220B ISOtech" },
                    658: { 8719,        "prod", "n-ary product = product sign, U+220F ISOamsb" },
                    659: { 8721,        "sum",  "n-ary sumation, U+2211 ISOamsb" },
                    660: { 8722,        "minus","minus sign, U+2212 ISOtech" },
                    661: { 8727,        "lowast","asterisk operator, U+2217 ISOtech" },
                    662: { 8730,        "radic","square root = radical sign, U+221A ISOtech" },
                    663: { 8733,        "prop", "proportional to, U+221D ISOtech" },
                    664: { 8734,        "infin","infinity, U+221E ISOtech" },
                    665: { 8736,        "ang",  "angle, U+2220 ISOamso" },
                    666: { 8743,        "and",  "logical and = wedge, U+2227 ISOtech" },
                    667: { 8744,        "or",   "logical or = vee, U+2228 ISOtech" },
                    668: { 8745,        "cap",  "intersection = cap, U+2229 ISOtech" },
                    669: { 8746,        "cup",  "union = cup, U+222A ISOtech" },
                    670: { 8747,        "int",  "integral, U+222B ISOtech" },
                    671: { 8756,        "there4","therefore, U+2234 ISOtech" },
                    672: { 8764,        "sim",  "tilde operator = varies with = similar to, U+223C ISOtech" },
                    673: { 8773,        "cong", "approximately equal to, U+2245 ISOtech" },
                    674: { 8776,        "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
                    675: { 8800,        "ne",   "not equal to, U+2260 ISOtech" },
                    676: { 8801,        "equiv","identical to, U+2261 ISOtech" },
                    677: { 8804,        "le",   "less-than or equal to, U+2264 ISOtech" },
                    678: { 8805,        "ge",   "greater-than or equal to, U+2265 ISOtech" },
                    679: { 8834,        "sub",  "subset of, U+2282 ISOtech" },
                    680: { 8835,        "sup",  "superset of, U+2283 ISOtech" },
                    681: { 8836,        "nsub", "not a subset of, U+2284 ISOamsn" },
                    682: { 8838,        "sube", "subset of or equal to, U+2286 ISOtech" },
                    683: { 8839,        "supe", "superset of or equal to, U+2287 ISOtech" },
                    684: { 8853,        "oplus","circled plus = direct sum, U+2295 ISOamsb" },
                    685: { 8855,        "otimes","circled times = vector product, U+2297 ISOamsb" },
                    686: { 8869,        "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
                    687: { 8901,        "sdot", "dot operator, U+22C5 ISOamsb" },
                    688: { 8968,        "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
                    689: { 8969,        "rceil","right ceiling, U+2309 ISOamsc" },
                    690: { 8970,        "lfloor","left floor = apl downstile, U+230A ISOamsc" },
                    691: { 8971,        "rfloor","right floor, U+230B ISOamsc" },
                    692: { 9001,        "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
                    693: { 9002,        "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
                    694: { 9674,        "loz",  "lozenge, U+25CA ISOpub" },
                    695: 
                    696: { 9824,        "spades","black spade suit, U+2660 ISOpub" },
                    697: { 9827,        "clubs","black club suit = shamrock, U+2663 ISOpub" },
                    698: { 9829,        "hearts","black heart suit = valentine, U+2665 ISOpub" },
                    699: { 9830,        "diams","black diamond suit, U+2666 ISOpub" },
                    700: 
                    701: { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
                    702: { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
                    703: { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
                    704: { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
                    705: { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
                    706: { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
                    707: { 732, "tilde","small tilde, U+02DC ISOdia" },
                    708: 
                    709: { 8194,        "ensp", "en space, U+2002 ISOpub" },
                    710: { 8195,        "emsp", "em space, U+2003 ISOpub" },
                    711: { 8201,        "thinsp","thin space, U+2009 ISOpub" },
                    712: { 8204,        "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
                    713: { 8205,        "zwj",  "zero width joiner, U+200D NEW RFC 2070" },
                    714: { 8206,        "lrm",  "left-to-right mark, U+200E NEW RFC 2070" },
                    715: { 8207,        "rlm",  "right-to-left mark, U+200F NEW RFC 2070" },
                    716: { 8211,        "ndash","en dash, U+2013 ISOpub" },
                    717: { 8212,        "mdash","em dash, U+2014 ISOpub" },
                    718: { 8216,        "lsquo","left single quotation mark, U+2018 ISOnum" },
                    719: { 8217,        "rsquo","right single quotation mark, U+2019 ISOnum" },
                    720: { 8218,        "sbquo","single low-9 quotation mark, U+201A NEW" },
                    721: { 8220,        "ldquo","left double quotation mark, U+201C ISOnum" },
                    722: { 8221,        "rdquo","right double quotation mark, U+201D ISOnum" },
                    723: { 8222,        "bdquo","double low-9 quotation mark, U+201E NEW" },
                    724: { 8224,        "dagger","dagger, U+2020 ISOpub" },
                    725: { 8225,        "Dagger","double dagger, U+2021 ISOpub" },
                    726: { 8240,        "permil","per mille sign, U+2030 ISOtech" },
                    727: { 8249,        "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
                    728: { 8250,        "rsaquo","single right-pointing angle quotation mark,
                    729:                                     U+203A ISO proposed" },
                    730: { 8364,        "euro", "euro sign, U+20AC NEW" }
                    731: };
                    732: 
                    733: /************************************************************************
                    734:  *                                                                     *
                    735:  *             Commodity functions to handle entities                  *
                    736:  *                                                                     *
                    737:  ************************************************************************/
                    738: 
                    739: /*
                    740:  * Macro used to grow the current buffer.
                    741:  */
                    742: #define growBuffer(buffer) {                                           \
                    743:     buffer##_size *= 2;                                                        \
                    744:     buffer = (CHAR *) realloc(buffer, buffer##_size * sizeof(CHAR));   \
                    745:     if (buffer == NULL) {                                              \
                    746:        perror("realloc failed");                                       \
                    747:        exit(1);                                                        \
                    748:     }                                                                  \
                    749: }
                    750: 
                    751: /**
                    752:  * htmlEntityLookup:
                    753:  * @name: the entity name
                    754:  *
                    755:  * Lookup the given entity in EntitiesTable
                    756:  *
                    757:  * TODO: the linear scan is really ugly, an hash table is really needed.
                    758:  *
                    759:  * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
                    760:  */
                    761: htmlEntityDescPtr
                    762: htmlEntityLookup(const CHAR *name) {
                    763:     int i;
                    764: 
                    765:     for (i = 0;i < (sizeof(html40EntitiesTable)/
                    766:                     sizeof(html40EntitiesTable[0]));i++) {
                    767:         if (!xmlStrcmp(name, html40EntitiesTable[i].name)) {
                    768: #ifdef DEBUG
                    769:             printf("Found entity %s\n", name);
                    770: #endif
                    771:             return(&html40EntitiesTable[i]);
                    772:        }
                    773:     }
                    774:     return(NULL);
                    775: }
                    776: 
                    777: 
                    778: /**
                    779:  * htmlDecodeEntities:
                    780:  * @ctxt:  the parser context
                    781:  * @len:  the len to decode (in bytes !), -1 for no size limit
                    782:  * @end:  an end marker CHAR, 0 if none
                    783:  * @end2:  an end marker CHAR, 0 if none
                    784:  * @end3:  an end marker CHAR, 0 if none
                    785:  *
                    786:  * Subtitute the HTML entities by their value
                    787:  *
                    788:  * TODO: once the internal representation will be UTF-8, all entities
                    789:  *       will be substituable, in the meantime we only apply the substitution
                    790:  *       to the one with values in the 0-255 UNICODE range
                    791:  *
                    792:  * Returns A newly allocated string with the substitution done. The caller
                    793:  *      must deallocate it !
                    794:  */
                    795: CHAR *
                    796: htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
                    797:                   CHAR end, CHAR  end2, CHAR end3) {
                    798:     CHAR *buffer = NULL;
                    799:     int buffer_size = 0;
                    800:     CHAR *out = NULL;
                    801:     CHAR *name = NULL;
                    802: 
                    803:     CHAR *cur = NULL;
                    804:     htmlEntityDescPtr ent;
                    805:     const CHAR *start = CUR_PTR;
                    806:     unsigned int max = (unsigned int) len;
                    807: 
                    808:     /*
                    809:      * allocate a translation buffer.
                    810:      */
                    811:     buffer_size = 1000;
                    812:     buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
                    813:     if (buffer == NULL) {
                    814:        perror("htmlDecodeEntities: malloc failed");
                    815:        return(NULL);
                    816:     }
                    817:     out = buffer;
                    818: 
                    819:     /*
                    820:      * Ok loop until we reach one of the ending char or a size limit.
                    821:      */
                    822:     while ((CUR_PTR - start < max) && (CUR != end) &&
                    823:            (CUR != end2) && (CUR != end3)) {
                    824: 
                    825:         if (CUR == '&') {
                    826:            if (NXT(1) == '#') {
                    827:                int val = htmlParseCharRef(ctxt);
                    828:                /* TODO: invalid for UTF-8 variable encoding !!! */
                    829:                *out++ = val;
                    830:            } else {
                    831:                ent = htmlParseEntityRef(ctxt, &name);
                    832:                if (name != NULL) {
                    833:                    if ((ent == NULL) || (ent->value <= 0) ||
                    834:                        (ent->value >= 255)) {
                    835:                        *out++ = '&';
                    836:                        cur = name;
                    837:                        while (*cur != 0) {
                    838:                            if (out - buffer > buffer_size - 100) {
                    839:                                int index = out - buffer;
                    840: 
                    841:                                growBuffer(buffer);
                    842:                                out = &buffer[index];
                    843:                            }
                    844:                            *out++ = *cur++;
                    845:                        }
                    846:                        *out++ = ';';
                    847:                    } else {
                    848:                        /* TODO: invalid for UTF-8 variable encoding !!! */
                    849:                        *out++ = (CHAR)ent->value;
                    850:                        if (out - buffer > buffer_size - 100) {
                    851:                            int index = out - buffer;
                    852: 
                    853:                            growBuffer(buffer);
                    854:                            out = &buffer[index];
                    855:                        }
                    856:                    }
                    857:                    free(name);
                    858:                }
                    859:            }
                    860:        } else {
                    861:            /*  TODO: invalid for UTF-8 , use COPY(out); */
                    862:            *out++ = CUR;
                    863:            if (out - buffer > buffer_size - 100) {
                    864:              int index = out - buffer;
                    865:              
                    866:              growBuffer(buffer);
                    867:              out = &buffer[index];
                    868:            }
                    869:            NEXT;
                    870:        }
                    871:     }
                    872:     *out++ = 0;
                    873:     return(buffer);
                    874: }
                    875: 
                    876: 
                    877: /************************************************************************
                    878:  *                                                                     *
                    879:  *             Commodity functions to handle encodings                 *
                    880:  *                                                                     *
                    881:  ************************************************************************/
                    882: 
                    883: /**
                    884:  * htmlSwitchEncoding:
                    885:  * @ctxt:  the parser context
                    886:  * @len:  the len of @cur
                    887:  *
                    888:  * change the input functions when discovering the character encoding
                    889:  * of a given entity.
                    890:  *
                    891:  */
                    892: void
                    893: htmlSwitchEncoding(htmlParserCtxtPtr ctxt, xmlCharEncoding enc)
                    894: {
                    895:     switch (enc) {
                    896:         case XML_CHAR_ENCODING_ERROR:
                    897:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    898:                ctxt->sax->error(ctxt->userData, "encoding unknown\n");
                    899:            ctxt->wellFormed = 0;
                    900:             break;
                    901:         case XML_CHAR_ENCODING_NONE:
                    902:            /* let's assume it's UTF-8 without the XML decl */
                    903:             return;
                    904:         case XML_CHAR_ENCODING_UTF8:
                    905:            /* default encoding, no conversion should be needed */
                    906:             return;
                    907:         case XML_CHAR_ENCODING_UTF16LE:
                    908:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    909:                 ctxt->sax->error(ctxt->userData,
                    910:                  "char encoding UTF16 little endian not supported\n");
                    911:             break;
                    912:         case XML_CHAR_ENCODING_UTF16BE:
                    913:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    914:                 ctxt->sax->error(ctxt->userData,
                    915:                  "char encoding UTF16 big endian not supported\n");
                    916:             break;
                    917:         case XML_CHAR_ENCODING_UCS4LE:
                    918:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    919:                 ctxt->sax->error(ctxt->userData,
                    920:                  "char encoding USC4 little endian not supported\n");
                    921:             break;
                    922:         case XML_CHAR_ENCODING_UCS4BE:
                    923:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    924:                 ctxt->sax->error(ctxt->userData,
                    925:                  "char encoding USC4 big endian not supported\n");
                    926:             break;
                    927:         case XML_CHAR_ENCODING_EBCDIC:
                    928:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    929:                 ctxt->sax->error(ctxt->userData,
                    930:                  "char encoding EBCDIC not supported\n");
                    931:             break;
                    932:         case XML_CHAR_ENCODING_UCS4_2143:
                    933:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    934:                 ctxt->sax->error(ctxt->userData,
                    935:                  "char encoding UCS4 2143 not supported\n");
                    936:             break;
                    937:         case XML_CHAR_ENCODING_UCS4_3412:
                    938:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    939:                 ctxt->sax->error(ctxt->userData,
                    940:                  "char encoding UCS4 3412 not supported\n");
                    941:             break;
                    942:         case XML_CHAR_ENCODING_UCS2:
                    943:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    944:                 ctxt->sax->error(ctxt->userData,
                    945:                  "char encoding UCS2 not supported\n");
                    946:             break;
                    947:         case XML_CHAR_ENCODING_8859_1:
                    948:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    949:                 ctxt->sax->error(ctxt->userData,
                    950:                  "char encoding ISO_8859_1 ISO Latin 1 not supported\n");
                    951:             break;
                    952:         case XML_CHAR_ENCODING_8859_2:
                    953:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    954:                 ctxt->sax->error(ctxt->userData,
                    955:                  "char encoding ISO_8859_2 ISO Latin 2 not supported\n");
                    956:             break;
                    957:         case XML_CHAR_ENCODING_8859_3:
                    958:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    959:                 ctxt->sax->error(ctxt->userData,
                    960:                  "char encoding ISO_8859_3 not supported\n");
                    961:             break;
                    962:         case XML_CHAR_ENCODING_8859_4:
                    963:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    964:                 ctxt->sax->error(ctxt->userData,
                    965:                  "char encoding ISO_8859_4 not supported\n");
                    966:             break;
                    967:         case XML_CHAR_ENCODING_8859_5:
                    968:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    969:                 ctxt->sax->error(ctxt->userData,
                    970:                  "char encoding ISO_8859_5 not supported\n");
                    971:             break;
                    972:         case XML_CHAR_ENCODING_8859_6:
                    973:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    974:                 ctxt->sax->error(ctxt->userData,
                    975:                  "char encoding ISO_8859_6 not supported\n");
                    976:             break;
                    977:         case XML_CHAR_ENCODING_8859_7:
                    978:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    979:                 ctxt->sax->error(ctxt->userData,
                    980:                  "char encoding ISO_8859_7 not supported\n");
                    981:             break;
                    982:         case XML_CHAR_ENCODING_8859_8:
                    983:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    984:                 ctxt->sax->error(ctxt->userData,
                    985:                  "char encoding ISO_8859_8 not supported\n");
                    986:             break;
                    987:         case XML_CHAR_ENCODING_8859_9:
                    988:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    989:                 ctxt->sax->error(ctxt->userData,
                    990:                  "char encoding ISO_8859_9 not supported\n");
                    991:             break;
                    992:         case XML_CHAR_ENCODING_2022_JP:
                    993:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    994:                 ctxt->sax->error(ctxt->userData,
                    995:                   "char encoding ISO-2022-JPnot supported\n");
                    996:             break;
                    997:         case XML_CHAR_ENCODING_SHIFT_JIS:
                    998:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    999:                 ctxt->sax->error(ctxt->userData,
                   1000:                   "char encoding Shift_JISnot supported\n");
                   1001:             break;
                   1002:         case XML_CHAR_ENCODING_EUC_JP:
                   1003:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1004:                 ctxt->sax->error(ctxt->userData,
                   1005:                   "char encoding EUC-JPnot supported\n");
                   1006:             break;
                   1007:     }
                   1008: }
                   1009: 
                   1010: 
                   1011: /************************************************************************
                   1012:  *                                                                     *
                   1013:  *             Commodity functions, cleanup needed ?                   *
                   1014:  *                                                                     *
                   1015:  ************************************************************************/
                   1016: 
                   1017: /**
                   1018:  * areBlanks:
                   1019:  * @ctxt:  an HTML parser context
                   1020:  * @str:  a CHAR *
                   1021:  * @len:  the size of @str
                   1022:  *
                   1023:  * Is this a sequence of blank chars that one can ignore ?
                   1024:  *
                   1025:  * TODO: to be corrected accodingly to DTD information if available
                   1026:  *
                   1027:  * Returns 1 if ignorable 0 otherwise.
                   1028:  */
                   1029: 
                   1030: static int areBlanks(htmlParserCtxtPtr ctxt, const CHAR *str, int len) {
                   1031:     int i;
                   1032:     xmlNodePtr lastChild;
                   1033: 
                   1034:     for (i = 0;i < len;i++)
                   1035:         if (!(IS_BLANK(str[i]))) return(0);
                   1036: 
                   1037:     if (CUR != '<') return(0);
                   1038:     if (ctxt->node == NULL) return(0);
                   1039:     lastChild = xmlGetLastChild(ctxt->node);
                   1040:     if (lastChild == NULL) {
                   1041:         if (ctxt->node->content != NULL) return(0);
                   1042:     } else if (xmlNodeIsText(lastChild))
                   1043:         return(0);
                   1044:     return(1);
                   1045: }
                   1046: 
                   1047: /**
                   1048:  * htmlHandleEntity:
                   1049:  * @ctxt:  an HTML parser context
                   1050:  * @entity:  an XML entity pointer.
                   1051:  *
                   1052:  * Default handling of an HTML entity, call the parser with the
                   1053:  * substitution string
                   1054:  */
                   1055: 
                   1056: void
                   1057: htmlHandleEntity(htmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
                   1058:     int len;
                   1059: 
                   1060:     if (entity->content == NULL) {
                   1061:         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1062:            ctxt->sax->error(ctxt->userData, "htmlHandleEntity %s: content == NULL\n",
                   1063:                       entity->name);
                   1064:        ctxt->wellFormed = 0;
                   1065:         return;
                   1066:     }
                   1067:     len = xmlStrlen(entity->content);
                   1068: 
                   1069:     /*
                   1070:      * Just handle the content as a set of chars.
                   1071:      */
                   1072:     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   1073:        ctxt->sax->characters(ctxt->userData, entity->content, len);
                   1074: 
                   1075: }
                   1076: 
                   1077: /**
                   1078:  * htmlNewDoc:
                   1079:  * @URI:  URI for the dtd, or NULL
                   1080:  * @ExternalID:  the external ID of the DTD, or NULL
                   1081:  *
                   1082:  * Returns a new document
                   1083:  */
                   1084: htmlDocPtr
                   1085: htmlNewDoc(const CHAR *URI, const CHAR *ExternalID) {
                   1086:     xmlDocPtr cur;
                   1087: 
                   1088:     /*
                   1089:      * Allocate a new document and fill the fields.
                   1090:      */
                   1091:     cur = (xmlDocPtr) malloc(sizeof(xmlDoc));
                   1092:     if (cur == NULL) {
                   1093:         fprintf(stderr, "xmlNewDoc : malloc failed\n");
                   1094:        return(NULL);
                   1095:     }
                   1096: 
                   1097:     cur->type = XML_DOCUMENT_NODE;
                   1098:     cur->version = NULL;
                   1099:     cur->intSubset = NULL;
                   1100:     xmlCreateIntSubset(cur, "HTML", ExternalID, URI);
                   1101:     cur->name = NULL;
                   1102:     cur->root = NULL; 
                   1103:     cur->extSubset = NULL;
                   1104:     cur->oldNs = NULL;
                   1105:     cur->encoding = NULL;
                   1106:     cur->standalone = 1;
                   1107:     cur->compression = 0;
                   1108: #ifndef XML_WITHOUT_CORBA
                   1109:     cur->_private = NULL;
                   1110:     cur->vepv = NULL;
                   1111: #endif
                   1112:     return(cur);
                   1113: }
                   1114: 
                   1115: 
                   1116: /************************************************************************
                   1117:  *                                                                     *
                   1118:  *                     The parser itself                               *
                   1119:  *     Relates to http://www.w3.org/TR/html40                          *
                   1120:  *                                                                     *
                   1121:  ************************************************************************/
                   1122: 
                   1123: /************************************************************************
                   1124:  *                                                                     *
                   1125:  *                     The parser itself                               *
                   1126:  *                                                                     *
                   1127:  ************************************************************************/
                   1128: 
                   1129: /**
                   1130:  * htmlParseHTMLName:
                   1131:  * @ctxt:  an HTML parser context
                   1132:  *
                   1133:  * parse an HTML tag or attribute name, note that we convert it to uppercase
                   1134:  * since HTML names are not case-sensitive.
                   1135:  *
                   1136:  * Returns the Tag Name parsed or NULL
                   1137:  */
                   1138: 
                   1139: CHAR *
                   1140: htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
                   1141:     CHAR *ret = NULL;
                   1142:     int i = 0;
                   1143:     CHAR loc[100];
                   1144: 
                   1145:     if (!IS_LETTER(CUR) && (CUR != '_') &&
                   1146:         (CUR != ':')) return(NULL);
                   1147: 
                   1148:     while ((i < 100) && ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)))) {
                   1149:        if ((CUR >= 0x61) && (CUR <= 0x7a)) loc[i] = CUR - 0x20;
                   1150:         else loc[i] = CUR;
                   1151:        i++;
                   1152:        
                   1153:        NEXT;
                   1154:     }
                   1155:     
                   1156:     ret = xmlStrndup(loc, i);
                   1157: 
                   1158:     return(ret);
                   1159: }
                   1160: 
                   1161: /**
                   1162:  * htmlParseName:
                   1163:  * @ctxt:  an HTML parser context
                   1164:  *
                   1165:  * parse an HTML name, this routine is case sensistive.
                   1166:  *
                   1167:  * Returns the Name parsed or NULL
                   1168:  */
                   1169: 
                   1170: CHAR *
                   1171: htmlParseName(htmlParserCtxtPtr ctxt) {
                   1172:     const CHAR *q;
                   1173:     CHAR *ret = NULL;
                   1174: 
                   1175:     if (!IS_LETTER(CUR) && (CUR != '_') &&
                   1176:         (CUR != ':')) return(NULL);
                   1177:     q = NEXT;
                   1178: 
                   1179:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1180:            (CUR == '.') || (CUR == '-') ||
                   1181:           (CUR == '_') || (CUR == ':') || 
                   1182:           (IS_COMBINING(CUR)) ||
                   1183:           (IS_EXTENDER(CUR)))
                   1184:        NEXT;
                   1185:     
                   1186:     ret = xmlStrndup(q, CUR_PTR - q);
                   1187: 
                   1188:     return(ret);
                   1189: }
                   1190: 
                   1191: /**
                   1192:  * htmlParseHTMLAttribute:
                   1193:  * @ctxt:  an HTML parser context
                   1194:  * 
                   1195:  * parse an HTML Nmtoken.
                   1196:  *
                   1197:  * Returns the Nmtoken parsed or NULL
                   1198:  */
                   1199: 
                   1200: CHAR *
                   1201: htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt) {
                   1202:     const CHAR *q;
                   1203:     CHAR *ret = NULL;
                   1204: 
                   1205:     q = NEXT;
                   1206: 
                   1207:     while ((!IS_BLANK(CUR)) && (CUR != '<') &&
                   1208:            (CUR != '&') && (CUR != '>') &&
                   1209:            (CUR != '\'') && (CUR != '"'))
                   1210:        NEXT;
                   1211:     
                   1212:     ret = xmlStrndup(q, CUR_PTR - q);
                   1213: 
                   1214:     return(ret);
                   1215: }
                   1216: 
                   1217: /**
                   1218:  * htmlParseNmtoken:
                   1219:  * @ctxt:  an HTML parser context
                   1220:  * 
                   1221:  * parse an HTML Nmtoken.
                   1222:  *
                   1223:  * Returns the Nmtoken parsed or NULL
                   1224:  */
                   1225: 
                   1226: CHAR *
                   1227: htmlParseNmtoken(htmlParserCtxtPtr ctxt) {
                   1228:     const CHAR *q;
                   1229:     CHAR *ret = NULL;
                   1230: 
                   1231:     q = NEXT;
                   1232: 
                   1233:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1234:            (CUR == '.') || (CUR == '-') ||
                   1235:           (CUR == '_') || (CUR == ':') || 
                   1236:           (IS_COMBINING(CUR)) ||
                   1237:           (IS_EXTENDER(CUR)))
                   1238:        NEXT;
                   1239:     
                   1240:     ret = xmlStrndup(q, CUR_PTR - q);
                   1241: 
                   1242:     return(ret);
                   1243: }
                   1244: 
                   1245: /**
                   1246:  * htmlParseEntityRef:
                   1247:  * @ctxt:  an HTML parser context
                   1248:  * @str:  location to store the entity name
                   1249:  *
                   1250:  * parse an HTML ENTITY references
                   1251:  *
                   1252:  * [68] EntityRef ::= '&' Name ';'
                   1253:  *
                   1254:  * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
                   1255:  *         if non-NULL *str will have to be freed by the caller.
                   1256:  */
                   1257: htmlEntityDescPtr
                   1258: htmlParseEntityRef(htmlParserCtxtPtr ctxt, CHAR **str) {
                   1259:     CHAR *name;
                   1260:     htmlEntityDescPtr ent = NULL;
                   1261:     *str = NULL;
                   1262: 
                   1263:     if (CUR == '&') {
                   1264:         NEXT;
                   1265:         name = htmlParseName(ctxt);
                   1266:        if (name == NULL) {
                   1267:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1268:                ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
                   1269:            ctxt->wellFormed = 0;
                   1270:        } else {
                   1271:            if (CUR == ';') {
                   1272:                NEXT;
                   1273:                *str = name;
                   1274: 
                   1275:                /*
                   1276:                 * Lookup the entity in the table.
                   1277:                 */
                   1278:                ent = htmlEntityLookup(name);
                   1279:            } else {
                   1280:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1281:                    ctxt->sax->error(ctxt->userData,
                   1282:                                     "htmlParseEntityRef: expecting ';'\n");
                   1283:                ctxt->wellFormed = 0;
                   1284:                if (ctxt->sax->characters != NULL) {
                   1285:                    ctxt->sax->characters(ctxt->userData, "&", 1);
                   1286:                    ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
                   1287:                }
                   1288:                free(name);
                   1289:            }
                   1290:        }
                   1291:     }
                   1292:     return(ent);
                   1293: }
                   1294: 
                   1295: /**
                   1296:  * htmlParseAttValue:
                   1297:  * @ctxt:  an HTML parser context
                   1298:  *
                   1299:  * parse a value for an attribute
                   1300:  * Note: the parser won't do substitution of entities here, this
                   1301:  * will be handled later in xmlStringGetNodeList, unless it was
                   1302:  * asked for ctxt->replaceEntities != 0 
                   1303:  *
                   1304:  * Returns the AttValue parsed or NULL.
                   1305:  */
                   1306: 
                   1307: CHAR *
                   1308: htmlParseAttValue(htmlParserCtxtPtr ctxt) {
                   1309:     CHAR *ret = NULL;
                   1310: 
                   1311:     if (CUR == '"') {
                   1312:         NEXT;
                   1313:        ret = htmlDecodeEntities(ctxt, -1, '"', '<', 0);
                   1314:        if (CUR == '<') {
                   1315:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1316:                ctxt->sax->error(ctxt->userData,
                   1317:                   "Unescaped '<' not allowed in attributes values\n");
                   1318:            ctxt->wellFormed = 0;
                   1319:        }
                   1320:         if (CUR != '"') {
                   1321:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1322:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   1323:            ctxt->wellFormed = 0;
                   1324:        } else
                   1325:            NEXT;
                   1326:     } else if (CUR == '\'') {
                   1327:         NEXT;
                   1328:        ret = htmlDecodeEntities(ctxt, -1, '\'', '<', 0);
                   1329:        if (CUR == '<') {
                   1330:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1331:                ctxt->sax->error(ctxt->userData,
                   1332:                   "Unescaped '<' not allowed in attributes values\n");
                   1333:            ctxt->wellFormed = 0;
                   1334:        }
                   1335:         if (CUR != '\'') {
                   1336:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1337:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   1338:            ctxt->wellFormed = 0;
                   1339:        } else
                   1340:            NEXT;
                   1341:     } else {
                   1342:         /*
                   1343:         * That's an HTMLism, the attribute value may not be quoted
                   1344:         */
                   1345:        ret = htmlParseHTMLAttribute(ctxt);
                   1346:        if (ret == NULL) {
                   1347:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1348:                ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
                   1349:            ctxt->wellFormed = 0;
                   1350:        }
                   1351:     }
                   1352:     
                   1353:     return(ret);
                   1354: }
                   1355: 
                   1356: /**
                   1357:  * htmlParseSystemLiteral:
                   1358:  * @ctxt:  an HTML parser context
                   1359:  * 
                   1360:  * parse an HTML Literal
                   1361:  *
                   1362:  * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
                   1363:  *
                   1364:  * Returns the SystemLiteral parsed or NULL
                   1365:  */
                   1366: 
                   1367: CHAR *
                   1368: htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
                   1369:     const CHAR *q;
                   1370:     CHAR *ret = NULL;
                   1371: 
                   1372:     if (CUR == '"') {
                   1373:         NEXT;
                   1374:        q = CUR_PTR;
                   1375:        while ((IS_CHAR(CUR)) && (CUR != '"'))
                   1376:            NEXT;
                   1377:        if (!IS_CHAR(CUR)) {
                   1378:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1379:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   1380:            ctxt->wellFormed = 0;
                   1381:        } else {
                   1382:            ret = xmlStrndup(q, CUR_PTR - q);
                   1383:            NEXT;
                   1384:         }
                   1385:     } else if (CUR == '\'') {
                   1386:         NEXT;
                   1387:        q = CUR_PTR;
                   1388:        while ((IS_CHAR(CUR)) && (CUR != '\''))
                   1389:            NEXT;
                   1390:        if (!IS_CHAR(CUR)) {
                   1391:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1392:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   1393:            ctxt->wellFormed = 0;
                   1394:        } else {
                   1395:            ret = xmlStrndup(q, CUR_PTR - q);
                   1396:            NEXT;
                   1397:         }
                   1398:     } else {
                   1399:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1400:            ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
                   1401:        ctxt->wellFormed = 0;
                   1402:     }
                   1403:     
                   1404:     return(ret);
                   1405: }
                   1406: 
                   1407: /**
                   1408:  * htmlParsePubidLiteral:
                   1409:  * @ctxt:  an HTML parser context
                   1410:  *
                   1411:  * parse an HTML public literal
                   1412:  *
                   1413:  * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
                   1414:  *
                   1415:  * Returns the PubidLiteral parsed or NULL.
                   1416:  */
                   1417: 
                   1418: CHAR *
                   1419: htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
                   1420:     const CHAR *q;
                   1421:     CHAR *ret = NULL;
                   1422:     /*
                   1423:      * Name ::= (Letter | '_') (NameChar)*
                   1424:      */
                   1425:     if (CUR == '"') {
                   1426:         NEXT;
                   1427:        q = CUR_PTR;
                   1428:        while (IS_PUBIDCHAR(CUR)) NEXT;
                   1429:        if (CUR != '"') {
                   1430:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1431:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   1432:            ctxt->wellFormed = 0;
                   1433:        } else {
                   1434:            ret = xmlStrndup(q, CUR_PTR - q);
                   1435:            NEXT;
                   1436:        }
                   1437:     } else if (CUR == '\'') {
                   1438:         NEXT;
                   1439:        q = CUR_PTR;
                   1440:        while ((IS_LETTER(CUR)) && (CUR != '\''))
                   1441:            NEXT;
                   1442:        if (!IS_LETTER(CUR)) {
                   1443:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1444:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   1445:            ctxt->wellFormed = 0;
                   1446:        } else {
                   1447:            ret = xmlStrndup(q, CUR_PTR - q);
                   1448:            NEXT;
                   1449:        }
                   1450:     } else {
                   1451:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1452:            ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
                   1453:        ctxt->wellFormed = 0;
                   1454:     }
                   1455:     
                   1456:     return(ret);
                   1457: }
                   1458: 
                   1459: /**
                   1460:  * htmlParseCharData:
                   1461:  * @ctxt:  an HTML parser context
                   1462:  * @cdata:  int indicating whether we are within a CDATA section
                   1463:  *
                   1464:  * parse a CharData section.
                   1465:  * if we are within a CDATA section ']]>' marks an end of section.
                   1466:  *
                   1467:  * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
                   1468:  */
                   1469: 
                   1470: void
                   1471: htmlParseCharData(htmlParserCtxtPtr ctxt, int cdata) {
                   1472:     const CHAR *q;
                   1473: 
                   1474:     q = CUR_PTR;
                   1475:     while ((IS_CHAR(CUR)) && (CUR != '<') &&
                   1476:            (CUR != '&')) {
                   1477:        if ((CUR == ']') && (NXT(1) == ']') &&
                   1478:            (NXT(2) == '>')) {
                   1479:            if (cdata) break;
                   1480:            else {
                   1481:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1482:                    ctxt->sax->error(ctxt->userData,
                   1483:                       "Sequence ']]>' not allowed in content\n");
                   1484:                ctxt->wellFormed = 0;
                   1485:            }
                   1486:        }
                   1487:         NEXT;
                   1488:     }
                   1489:     if (q == CUR_PTR) return;
                   1490: 
                   1491:     /*
                   1492:      * Ok the segment [q CUR_PTR] is to be consumed as chars.
                   1493:      */
                   1494:     if (ctxt->sax != NULL) {
                   1495:        if (areBlanks(ctxt, q, CUR_PTR - q)) {
                   1496:            if (ctxt->sax->ignorableWhitespace != NULL)
                   1497:                ctxt->sax->ignorableWhitespace(ctxt->userData, q, CUR_PTR - q);
                   1498:        } else {
                   1499:            if (ctxt->sax->characters != NULL)
                   1500:                ctxt->sax->characters(ctxt->userData, q, CUR_PTR - q);
                   1501:         }
                   1502:     }
                   1503: }
                   1504: 
                   1505: /**
                   1506:  * htmlParseExternalID:
                   1507:  * @ctxt:  an HTML parser context
                   1508:  * @publicID:  a CHAR** receiving PubidLiteral
                   1509:  * @strict: indicate whether we should restrict parsing to only
                   1510:  *          production [75], see NOTE below
                   1511:  *
                   1512:  * Parse an External ID or a Public ID
                   1513:  *
                   1514:  * NOTE: Productions [75] and [83] interract badly since [75] can generate
                   1515:  *       'PUBLIC' S PubidLiteral S SystemLiteral
                   1516:  *
                   1517:  * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
                   1518:  *                   | 'PUBLIC' S PubidLiteral S SystemLiteral
                   1519:  *
                   1520:  * [83] PublicID ::= 'PUBLIC' S PubidLiteral
                   1521:  *
                   1522:  * Returns the function returns SystemLiteral and in the second
                   1523:  *                case publicID receives PubidLiteral, is strict is off
                   1524:  *                it is possible to return NULL and have publicID set.
                   1525:  */
                   1526: 
                   1527: CHAR *
                   1528: htmlParseExternalID(htmlParserCtxtPtr ctxt, CHAR **publicID, int strict) {
                   1529:     CHAR *URI = NULL;
                   1530: 
                   1531:     if ((UPPER == 'S') && (UPP(1) == 'Y') &&
                   1532:          (UPP(2) == 'S') && (UPP(3) == 'T') &&
                   1533:         (UPP(4) == 'E') && (UPP(5) == 'M')) {
                   1534:         SKIP(6);
                   1535:        if (!IS_BLANK(CUR)) {
                   1536:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1537:                ctxt->sax->error(ctxt->userData,
                   1538:                    "Space required after 'SYSTEM'\n");
                   1539:            ctxt->wellFormed = 0;
                   1540:        }
                   1541:         SKIP_BLANKS;
                   1542:        URI = htmlParseSystemLiteral(ctxt);
                   1543:        if (URI == NULL) {
                   1544:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1545:                ctxt->sax->error(ctxt->userData,
                   1546:                  "htmlParseExternalID: SYSTEM, no URI\n");
                   1547:            ctxt->wellFormed = 0;
                   1548:         }
                   1549:     } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
                   1550:               (UPP(2) == 'B') && (UPP(3) == 'L') &&
                   1551:               (UPP(4) == 'I') && (UPP(5) == 'C')) {
                   1552:         SKIP(6);
                   1553:        if (!IS_BLANK(CUR)) {
                   1554:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1555:                ctxt->sax->error(ctxt->userData,
                   1556:                    "Space required after 'PUBLIC'\n");
                   1557:            ctxt->wellFormed = 0;
                   1558:        }
                   1559:         SKIP_BLANKS;
                   1560:        *publicID = htmlParsePubidLiteral(ctxt);
                   1561:        if (*publicID == NULL) {
                   1562:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1563:                ctxt->sax->error(ctxt->userData, 
                   1564:                  "htmlParseExternalID: PUBLIC, no Public Identifier\n");
                   1565:            ctxt->wellFormed = 0;
                   1566:        }
                   1567:        if (strict) {
                   1568:            /*
                   1569:             * We don't handle [83] so "S SystemLiteral" is required.
                   1570:             */
                   1571:            if (!IS_BLANK(CUR)) {
                   1572:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1573:                    ctxt->sax->error(ctxt->userData,
                   1574:                        "Space required after the Public Identifier\n");
                   1575:                ctxt->wellFormed = 0;
                   1576:            }
                   1577:        } else {
                   1578:            /*
                   1579:             * We handle [83] so we return immediately, if 
                   1580:             * "S SystemLiteral" is not detected. From a purely parsing
                   1581:             * point of view that's a nice mess.
                   1582:             */
                   1583:            const CHAR *ptr = CUR_PTR;
                   1584:            if (!IS_BLANK(*ptr)) return(NULL);
                   1585:            
                   1586:            while (IS_BLANK(*ptr)) ptr++;
                   1587:            if ((*ptr != '\'') || (*ptr != '"')) return(NULL);
                   1588:        }
                   1589:         SKIP_BLANKS;
                   1590:        URI = htmlParseSystemLiteral(ctxt);
                   1591:        if (URI == NULL) {
                   1592:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1593:                ctxt->sax->error(ctxt->userData, 
                   1594:                   "htmlParseExternalID: PUBLIC, no URI\n");
                   1595:            ctxt->wellFormed = 0;
                   1596:         }
                   1597:     }
                   1598:     return(URI);
                   1599: }
                   1600: 
                   1601: /**
                   1602:  * htmlParseComment:
                   1603:  * @ctxt:  an HTML parser context
                   1604:  * @create: should we create a node, or just skip the content
                   1605:  *
                   1606:  * Parse an XML (SGML) comment <!-- .... -->
                   1607:  *
                   1608:  * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
                   1609:  */
                   1610: void
                   1611: htmlParseComment(htmlParserCtxtPtr ctxt, int create) {
                   1612:     const CHAR *q, *start;
                   1613:     const CHAR *r;
                   1614:     CHAR *val;
                   1615: 
                   1616:     /*
                   1617:      * Check that there is a comment right here.
                   1618:      */
                   1619:     if ((CUR != '<') || (NXT(1) != '!') ||
                   1620:         (NXT(2) != '-') || (NXT(3) != '-')) return;
                   1621: 
                   1622:     SKIP(4);
                   1623:     start = q = CUR_PTR;
                   1624:     NEXT;
                   1625:     r = CUR_PTR;
                   1626:     NEXT;
                   1627:     while (IS_CHAR(CUR) &&
                   1628:            ((CUR == ':') || (CUR != '>') ||
                   1629:            (*r != '-') || (*q != '-'))) {
                   1630:        if ((*r == '-') && (*q == '-')) {
                   1631:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1632:                ctxt->sax->error(ctxt->userData,
                   1633:               "Comment must not contain '--' (double-hyphen)`\n");
                   1634:            ctxt->wellFormed = 0;
                   1635:        }
                   1636:         NEXT;r++;q++;
                   1637:     }
                   1638:     if (!IS_CHAR(CUR)) {
                   1639:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1640:            ctxt->sax->error(ctxt->userData, "Comment not terminated \n<!--%.50s\n", start);
                   1641:        ctxt->wellFormed = 0;
                   1642:     } else {
                   1643:         NEXT;
                   1644:        if (create) {
                   1645:            val = xmlStrndup(start, q - start);
                   1646:            if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL))
                   1647:                ctxt->sax->comment(ctxt->userData, val);
                   1648:            free(val);
                   1649:        }
                   1650:     }
                   1651: }
                   1652: 
                   1653: /**
                   1654:  * htmlParseCharRef:
                   1655:  * @ctxt:  an HTML parser context
                   1656:  *
                   1657:  * parse Reference declarations
                   1658:  *
                   1659:  * [66] CharRef ::= '&#' [0-9]+ ';' |
                   1660:  *                  '&#x' [0-9a-fA-F]+ ';'
                   1661:  *
                   1662:  * Returns the value parsed (as an int)
                   1663:  */
                   1664: int
                   1665: htmlParseCharRef(htmlParserCtxtPtr ctxt) {
                   1666:     int val = 0;
                   1667: 
                   1668:     if ((CUR == '&') && (NXT(1) == '#') &&
                   1669:         (NXT(2) == 'x')) {
                   1670:        SKIP(3);
                   1671:        while (CUR != ';') {
                   1672:            if ((CUR >= '0') && (CUR <= '9')) 
                   1673:                val = val * 16 + (CUR - '0');
                   1674:            else if ((CUR >= 'a') && (CUR <= 'f'))
                   1675:                val = val * 16 + (CUR - 'a') + 10;
                   1676:            else if ((CUR >= 'A') && (CUR <= 'F'))
                   1677:                val = val * 16 + (CUR - 'A') + 10;
                   1678:            else {
                   1679:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1680:                    ctxt->sax->error(ctxt->userData, 
                   1681:                         "htmlParseCharRef: invalid hexadecimal value\n");
                   1682:                ctxt->wellFormed = 0;
                   1683:                val = 0;
                   1684:                break;
                   1685:            }
                   1686:            NEXT;
                   1687:        }
                   1688:        if (CUR == ';')
                   1689:            NEXT;
                   1690:     } else if  ((CUR == '&') && (NXT(1) == '#')) {
                   1691:        SKIP(2);
                   1692:        while (CUR != ';') {
                   1693:            if ((CUR >= '0') && (CUR <= '9')) 
                   1694:                val = val * 10 + (CUR - '0');
                   1695:            else {
                   1696:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1697:                    ctxt->sax->error(ctxt->userData, 
                   1698:                         "htmlParseCharRef: invalid decimal value\n");
                   1699:                ctxt->wellFormed = 0;
                   1700:                val = 0;
                   1701:                break;
                   1702:            }
                   1703:            NEXT;
                   1704:        }
                   1705:        if (CUR == ';')
                   1706:            NEXT;
                   1707:     } else {
                   1708:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1709:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
                   1710:        ctxt->wellFormed = 0;
                   1711:     }
                   1712:     /*
                   1713:      * Check the value IS_CHAR ...
                   1714:      */
                   1715:     if (IS_CHAR(val)) {
                   1716:         return(val);
                   1717:     } else {
                   1718:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1719:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid CHAR value %d\n",
                   1720:                             val);
                   1721:        ctxt->wellFormed = 0;
                   1722:     }
                   1723:     return(0);
                   1724: }
                   1725: 
                   1726: 
                   1727: /**
                   1728:  * htmlParseDocTypeDecl :
                   1729:  * @ctxt:  an HTML parser context
                   1730:  *
                   1731:  * parse a DOCTYPE declaration
                   1732:  *
                   1733:  * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? 
                   1734:  *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
                   1735:  */
                   1736: 
                   1737: void
                   1738: htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
                   1739:     CHAR *name;
                   1740:     CHAR *ExternalID = NULL;
                   1741:     CHAR *URI = NULL;
                   1742: 
                   1743:     /*
                   1744:      * We know that '<!DOCTYPE' has been detected.
                   1745:      */
                   1746:     SKIP(9);
                   1747: 
                   1748:     SKIP_BLANKS;
                   1749: 
                   1750:     /*
                   1751:      * Parse the DOCTYPE name.
                   1752:      */
                   1753:     name = htmlParseName(ctxt);
                   1754:     if (name == NULL) {
                   1755:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1756:            ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
                   1757:        ctxt->wellFormed = 0;
                   1758:     }
                   1759:     /*
                   1760:      * Check that upper(name) == "HTML" !!!!!!!!!!!!!
                   1761:      */
                   1762: 
                   1763:     SKIP_BLANKS;
                   1764: 
                   1765:     /*
                   1766:      * Check for SystemID and ExternalID
                   1767:      */
                   1768:     URI = htmlParseExternalID(ctxt, &ExternalID, 1);
                   1769:     SKIP_BLANKS;
                   1770: 
                   1771:     /*
                   1772:      * We should be at the end of the DOCTYPE declaration.
                   1773:      */
                   1774:     if (CUR != '>') {
                   1775:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1776:            ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
                   1777:        ctxt->wellFormed = 0;
                   1778:         /* We shouldn't try to resynchronize ... */
                   1779:     } else {
                   1780:     }
                   1781:     NEXT;
                   1782: 
                   1783:     /*
                   1784:      * Create the document accordingly to the DOCTYPE
                   1785:      */
                   1786:     ctxt->myDoc = htmlNewDoc(URI, ExternalID);
                   1787: 
                   1788:     /*
                   1789:      * Cleanup, since we don't use all those identifiers
                   1790:      * TODO : the DOCTYPE if available should be stored !
                   1791:      */
                   1792:     if (URI != NULL) free(URI);
                   1793:     if (ExternalID != NULL) free(ExternalID);
                   1794:     if (name != NULL) free(name);
                   1795: }
                   1796: 
                   1797: /**
                   1798:  * htmlParseAttribute:
                   1799:  * @ctxt:  an HTML parser context
                   1800:  * @value:  a CHAR ** used to store the value of the attribute
                   1801:  *
                   1802:  * parse an attribute
                   1803:  *
                   1804:  * [41] Attribute ::= Name Eq AttValue
                   1805:  *
                   1806:  * [25] Eq ::= S? '=' S?
                   1807:  *
                   1808:  * With namespace:
                   1809:  *
                   1810:  * [NS 11] Attribute ::= QName Eq AttValue
                   1811:  *
                   1812:  * Also the case QName == xmlns:??? is handled independently as a namespace
                   1813:  * definition.
                   1814:  *
                   1815:  * Returns the attribute name, and the value in *value.
                   1816:  */
                   1817: 
                   1818: CHAR *
                   1819: htmlParseAttribute(htmlParserCtxtPtr ctxt, CHAR **value) {
                   1820:     CHAR *name, *val;
                   1821: 
                   1822:     *value = NULL;
                   1823:     name = htmlParseName(ctxt);
                   1824:     if (name == NULL) {
                   1825:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1826:            ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
                   1827:        ctxt->wellFormed = 0;
                   1828:         return(NULL);
                   1829:     }
                   1830: 
                   1831:     /*
                   1832:      * read the value
                   1833:      */
                   1834:     SKIP_BLANKS;
                   1835:     if (CUR == '=') {
                   1836:         NEXT;
                   1837:        SKIP_BLANKS;
                   1838:        val = htmlParseAttValue(ctxt);
                   1839:     } else {
                   1840:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1841:            ctxt->sax->error(ctxt->userData,
                   1842:               "Specification mandate value for attribute %s\n", name);
                   1843:        ctxt->wellFormed = 0;
                   1844:        return(NULL);
                   1845:     }
                   1846: 
                   1847:     *value = val;
                   1848:     return(name);
                   1849: }
                   1850: 
                   1851: /**
                   1852:  * htmlParseStartTag:
                   1853:  * @ctxt:  an HTML parser context
                   1854:  * 
                   1855:  * parse a start of tag either for rule element or
                   1856:  * EmptyElement. In both case we don't parse the tag closing chars.
                   1857:  *
                   1858:  * [40] STag ::= '<' Name (S Attribute)* S? '>'
                   1859:  *
                   1860:  * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
                   1861:  *
                   1862:  * With namespace:
                   1863:  *
                   1864:  * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
                   1865:  *
                   1866:  * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
                   1867:  *
                   1868:  * Returns the element name parsed
                   1869:  */
                   1870: 
                   1871: CHAR *
                   1872: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
                   1873:     CHAR *name;
                   1874:     CHAR *attname;
                   1875:     CHAR *attvalue;
                   1876:     const CHAR **atts = NULL;
                   1877:     int nbatts = 0;
                   1878:     int maxatts = 0;
                   1879:     int i;
                   1880: 
                   1881:     if (CUR != '<') return(NULL);
                   1882:     NEXT;
                   1883: 
                   1884:     name = htmlParseHTMLName(ctxt);
                   1885:     if (name == NULL) {
                   1886:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1887:            ctxt->sax->error(ctxt->userData, 
                   1888:             "htmlParseStartTag: invalid element name\n");
                   1889:        ctxt->wellFormed = 0;
                   1890:         return(NULL);
                   1891:     }
                   1892: 
                   1893:     /*
                   1894:      * Check for auto-closure of HTML elements.
                   1895:      */
                   1896:     htmlAutoClose(ctxt, name);
                   1897: 
                   1898:     /*
                   1899:      * Now parse the attributes, it ends up with the ending
                   1900:      *
                   1901:      * (S Attribute)* S?
                   1902:      */
                   1903:     SKIP_BLANKS;
                   1904:     while ((IS_CHAR(CUR)) &&
                   1905:            (CUR != '>') && 
                   1906:           ((CUR != '/') || (NXT(1) != '>'))) {
                   1907:        const CHAR *q = CUR_PTR;
                   1908: 
                   1909:        attname = htmlParseAttribute(ctxt, &attvalue);
                   1910:         if ((attname != NULL) && (attvalue != NULL)) {
                   1911:            /*
                   1912:             * Well formedness requires at most one declaration of an attribute
                   1913:             */
                   1914:            for (i = 0; i < nbatts;i += 2) {
                   1915:                if (!xmlStrcmp(atts[i], attname)) {
                   1916:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1917:                        ctxt->sax->error(ctxt->userData, "Attribute %s redefined\n",
                   1918:                                         name);
                   1919:                    ctxt->wellFormed = 0;
                   1920:                    free(attname);
                   1921:                    free(attvalue);
                   1922:                    break;
                   1923:                }
                   1924:            }
                   1925: 
                   1926:            /*
                   1927:             * Add the pair to atts
                   1928:             */
                   1929:            if (atts == NULL) {
                   1930:                maxatts = 10;
                   1931:                atts = (const CHAR **) malloc(maxatts * sizeof(CHAR *));
                   1932:                if (atts == NULL) {
                   1933:                    fprintf(stderr, "malloc of %ld byte failed\n",
                   1934:                            maxatts * (long)sizeof(CHAR *));
                   1935:                    return(NULL);
                   1936:                }
                   1937:            } else if (nbatts + 2 < maxatts) {
                   1938:                maxatts *= 2;
                   1939:                atts = (const CHAR **) realloc(atts, maxatts * sizeof(CHAR *));
                   1940:                if (atts == NULL) {
                   1941:                    fprintf(stderr, "realloc of %ld byte failed\n",
                   1942:                            maxatts * (long)sizeof(CHAR *));
                   1943:                    return(NULL);
                   1944:                }
                   1945:            }
                   1946:            atts[nbatts++] = attname;
                   1947:            atts[nbatts++] = attvalue;
                   1948:            atts[nbatts] = NULL;
                   1949:            atts[nbatts + 1] = NULL;
                   1950:        }
                   1951: 
                   1952:        SKIP_BLANKS;
                   1953:         if (q == CUR_PTR) {
                   1954:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1955:                ctxt->sax->error(ctxt->userData, 
                   1956:                 "htmlParseStartTag: problem parsing attributes\n");
                   1957:            ctxt->wellFormed = 0;
                   1958:            break;
                   1959:        }
                   1960:     }
                   1961: 
                   1962:     /*
                   1963:      * SAX: Start of Element !
                   1964:      */
                   1965:     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                   1966:         ctxt->sax->startElement(ctxt->userData, name, atts);
                   1967: 
                   1968:     if (atts != NULL) {
                   1969:         for (i = 0;i < nbatts;i++) free((CHAR *) atts[i]);
                   1970:        free(atts);
                   1971:     }
                   1972:     return(name);
                   1973: }
                   1974: 
                   1975: /**
                   1976:  * htmlParseEndTag:
                   1977:  * @ctxt:  an HTML parser context
                   1978:  * @tagname:  the tag name as parsed in the opening tag.
                   1979:  *
                   1980:  * parse an end of tag
                   1981:  *
                   1982:  * [42] ETag ::= '</' Name S? '>'
                   1983:  *
                   1984:  * With namespace
                   1985:  *
                   1986:  * [NS 9] ETag ::= '</' QName S? '>'
                   1987:  */
                   1988: 
                   1989: void
                   1990: htmlParseEndTag(htmlParserCtxtPtr ctxt, const CHAR *tagname) {
                   1991:     CHAR *name;
                   1992:     int i;
                   1993: 
                   1994:     if ((CUR != '<') || (NXT(1) != '/')) {
                   1995:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1996:            ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
                   1997:        ctxt->wellFormed = 0;
                   1998:        return;
                   1999:     }
                   2000:     SKIP(2);
                   2001: 
                   2002:     name = htmlParseHTMLName(ctxt);
                   2003: 
                   2004:     /*
                   2005:      * We should definitely be at the ending "S? '>'" part
                   2006:      */
                   2007:     SKIP_BLANKS;
                   2008:     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
                   2009:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2010:            ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
                   2011:        ctxt->wellFormed = 0;
                   2012:     } else
                   2013:        NEXT;
                   2014: 
                   2015:     /*
                   2016:      * Check that we are not closing an already closed tag,
                   2017:      * <p><b>...</p></b> is a really common error !
                   2018:      */
                   2019:     for (i = ctxt->nodeNr - 1;i >= 0;i--) {
                   2020:         if ((ctxt->nodeTab[i] != NULL) &&
                   2021:            (!xmlStrcmp(tagname, ctxt->nodeTab[i]->name)))
                   2022:            break;
                   2023:     }
                   2024:     if (i < 0) {
                   2025:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2026:            ctxt->sax->error(ctxt->userData, 
                   2027:                             "htmlParseEndTag: unexpected close for tag %s\n",
                   2028:                             tagname);
                   2029:        ctxt->wellFormed = 0;
                   2030:        return;
                   2031:     }
                   2032: 
                   2033:     /*
                   2034:      * Check for auto-closure of HTML elements.
                   2035:      */
                   2036:     htmlAutoCloseOnClose(ctxt, name);
                   2037: 
                   2038:     /*
                   2039:      * Well formedness constraints, opening and closing must match.
                   2040:      * With the exception that the autoclose may have popped stuff out
                   2041:      * of the stack.
                   2042:      */
                   2043:     if (xmlStrcmp(name, tagname)) {
                   2044:         if ((ctxt->node != NULL) && 
                   2045:            (xmlStrcmp(ctxt->node->name, name))) {
                   2046:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2047:                ctxt->sax->error(ctxt->userData,
                   2048:                 "Opening and ending tag mismatch: %s and %s\n",
                   2049:                                 name, ctxt->node->name);
                   2050:            ctxt->wellFormed = 0;
                   2051:         }
                   2052:     }
                   2053: 
                   2054:     /*
                   2055:      * SAX: End of Tag
                   2056:      */
                   2057:     if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2058:         ctxt->sax->endElement(ctxt->userData, name);
                   2059: 
                   2060:     if (name != NULL)
                   2061:        free(name);
                   2062: 
                   2063:     return;
                   2064: }
                   2065: 
                   2066: 
                   2067: /**
                   2068:  * htmlParseReference:
                   2069:  * @ctxt:  an HTML parser context
                   2070:  * 
                   2071:  * parse and handle entity references in content,
                   2072:  * this will end-up in a call to character() since this is either a
                   2073:  * CharRef, or a predefined entity.
                   2074:  */
                   2075: void
                   2076: htmlParseReference(htmlParserCtxtPtr ctxt) {
                   2077:     htmlEntityDescPtr ent;
                   2078:     CHAR out[2];
                   2079:     CHAR *name;
                   2080:     int val;
                   2081:     if (CUR != '&') return;
                   2082: 
                   2083:     if (NXT(1) == '#') {
                   2084:        val = htmlParseCharRef(ctxt);
                   2085:        /* TODO: invalid for UTF-8 variable encoding !!! */
                   2086:        out[0] = val;
                   2087:        out[1] = 0;
                   2088:        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   2089:            ctxt->sax->characters(ctxt->userData, out, 1);
                   2090:     } else {
                   2091:        ent = htmlParseEntityRef(ctxt, &name);
                   2092:        if (name == NULL) return; /* Shall we output & anyway ? */
                   2093:        if ((ent == NULL) || (ent->value <= 0) || (ent->value >= 255)) {
                   2094:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
                   2095:                ctxt->sax->characters(ctxt->userData, "&", 1);
                   2096:                ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
                   2097:                ctxt->sax->characters(ctxt->userData, ";", 1);
                   2098:            }
                   2099:        } else {
                   2100:            /* TODO: invalid for UTF-8 variable encoding !!! */
                   2101:            out[0] = ent->value;
                   2102:            out[1] = 0;
                   2103:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   2104:                ctxt->sax->characters(ctxt->userData, out, 1);
                   2105:        }
                   2106:        free(name);
                   2107:     }
                   2108: }
                   2109: 
                   2110: /**
                   2111:  * htmlParseContent:
                   2112:  * @ctxt:  an HTML parser context
                   2113:  * @name:  the node name
                   2114:  *
                   2115:  * Parse a content: comment, sub-element, reference or text.
                   2116:  *
                   2117:  */
                   2118: 
                   2119: void
                   2120: htmlParseContent(htmlParserCtxtPtr ctxt, const CHAR *name) {
                   2121:     htmlNodePtr currentNode;
                   2122: 
                   2123:     currentNode = ctxt->node;
                   2124:     while ((CUR != '<') || (NXT(1) != '/')) {
                   2125:        const CHAR *test = CUR_PTR;
                   2126: 
                   2127:        /*
                   2128:         * Has this node been popped out during parsing of
                   2129:         * the next element
                   2130:         */
                   2131:         if (currentNode != ctxt->node) return;
                   2132: 
                   2133:        /*
                   2134:         * First case :  a comment
                   2135:         */
                   2136:        if ((CUR == '<') && (NXT(1) == '!') &&
                   2137:                 (NXT(2) == '-') && (NXT(3) == '-')) {
                   2138:            htmlParseComment(ctxt, 1);
                   2139:        }
                   2140: 
                   2141:        /*
                   2142:         * Second case :  a sub-element.
                   2143:         */
                   2144:        else if (CUR == '<') {
                   2145:            htmlParseElement(ctxt);
                   2146:        }
                   2147: 
                   2148:        /*
                   2149:         * Third case : a reference. If if has not been resolved,
                   2150:         *    parsing returns it's Name, create the node 
                   2151:         */
                   2152:        else if (CUR == '&') {
                   2153:            htmlParseReference(ctxt);
                   2154:        }
                   2155: 
                   2156:        /*
                   2157:         * Last case, text. Note that References are handled directly.
                   2158:         */
                   2159:        else {
                   2160:            htmlParseCharData(ctxt, 0);
                   2161:        }
                   2162: 
                   2163:        if (test == CUR_PTR) {
                   2164:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2165:                ctxt->sax->error(ctxt->userData,
                   2166:                     "detected an error in element content\n");
                   2167:            ctxt->wellFormed = 0;
                   2168:             break;
                   2169:        }
                   2170:     }
                   2171: 
                   2172:     /*
                   2173:      * parse the end of tag: '</' should be here.
                   2174:      */
                   2175:     htmlParseEndTag(ctxt, name);
                   2176: }
                   2177: 
                   2178: /**
                   2179:  * htmlParseElement:
                   2180:  * @ctxt:  an HTML parser context
                   2181:  *
                   2182:  * parse an HTML element, this is highly recursive
                   2183:  *
                   2184:  * [39] element ::= EmptyElemTag | STag content ETag
                   2185:  *
                   2186:  * [41] Attribute ::= Name Eq AttValue
                   2187:  */
                   2188: 
                   2189: void
                   2190: htmlParseElement(htmlParserCtxtPtr ctxt) {
                   2191:     const CHAR *openTag = CUR_PTR;
                   2192:     CHAR *name;
                   2193:     htmlParserNodeInfo node_info;
                   2194:     htmlNodePtr currentNode;
                   2195:     htmlElemDescPtr info;
                   2196: 
                   2197:     /* Capture start position */
                   2198:     node_info.begin_pos = CUR_PTR - ctxt->input->base;
                   2199:     node_info.begin_line = ctxt->input->line;
                   2200: 
                   2201:     name = htmlParseStartTag(ctxt);
                   2202:     if (name == NULL) {
                   2203:         return;
                   2204:     }
                   2205: 
                   2206:     /*
                   2207:      * Lookup the info for that element.
                   2208:      */
                   2209:     info = htmlTagLookup(name);
                   2210:     if (info == NULL) {
                   2211:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2212:            ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   2213:                             name);
                   2214:        ctxt->wellFormed = 0;
                   2215:     } else if (info->depr) {
                   2216: /***************************
                   2217:        if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   2218:            ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
                   2219:                               name);
                   2220:  ***************************/
                   2221:     }
                   2222: 
                   2223:     /*
                   2224:      * Check for an Empty Element labelled the XML/SGML way
                   2225:      */
                   2226:     if ((CUR == '/') && (NXT(1) == '>')) {
                   2227:         SKIP(2);
                   2228:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2229:            ctxt->sax->endElement(ctxt->userData, name);
                   2230:        free(name);
                   2231:        return;
                   2232:     }
                   2233: 
                   2234:     if (CUR == '>') NEXT;
                   2235:     else {
                   2236:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2237:            ctxt->sax->error(ctxt->userData, "Couldn't find end of Start Tag\n%.30s\n",
                   2238:                             openTag);
                   2239:        ctxt->wellFormed = 0;
                   2240: 
                   2241:        /*
                   2242:         * end of parsing of this node.
                   2243:         */
                   2244:        nodePop(ctxt);
                   2245:        free(name);
                   2246:        return;
                   2247:     }
                   2248: 
                   2249:     /*
                   2250:      * Check for an Empty Element from DTD definition
                   2251:      */
                   2252:     if ((info != NULL) && (info->empty)) {
                   2253:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2254:            ctxt->sax->endElement(ctxt->userData, name);
                   2255:        free(name);
                   2256:        return;
                   2257:     }
                   2258: 
                   2259:     /*
                   2260:      * Parse the content of the element:
                   2261:      */
                   2262:     currentNode = ctxt->node;
                   2263:     htmlParseContent(ctxt, name);
                   2264: 
                   2265:     /*
                   2266:      * check whether the element get popped due to auto closure
                   2267:      * on start tag
                   2268:      */
                   2269:     if (currentNode != ctxt->node) {
                   2270:        free(name);
                   2271:         return;
                   2272:     }
                   2273: 
                   2274:     if (!IS_CHAR(CUR)) {
                   2275:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2276:            ctxt->sax->error(ctxt->userData,
                   2277:                 "Premature end of data in tag %.30s\n", openTag);
                   2278:        ctxt->wellFormed = 0;
                   2279: 
                   2280:        /*
                   2281:         * end of parsing of this node.
                   2282:         */
                   2283:        nodePop(ctxt);
                   2284:        free(name);
                   2285:        return;
                   2286:     }
                   2287: 
                   2288:     free(name);
                   2289: }
                   2290: 
                   2291: /**
                   2292:  * htmlParseDocument :
                   2293:  * @ctxt:  an HTML parser context
                   2294:  * 
                   2295:  * parse an HTML document (and build a tree if using the standard SAX
                   2296:  * interface).
                   2297:  *
                   2298:  * Returns 0, -1 in case of error. the parser context is augmented
                   2299:  *                as a result of the parsing.
                   2300:  */
                   2301: 
                   2302: int
                   2303: htmlParseDocument(htmlParserCtxtPtr ctxt) {
                   2304:     htmlDefaultSAXHandlerInit();
                   2305:     ctxt->html = 1;
                   2306: 
                   2307:     /*
                   2308:      * SAX: beginning of the document processing TODO: update for HTML.
                   2309:      */
                   2310:     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   2311:         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
                   2312: 
                   2313:     /*
                   2314:      * We should check for encoding here and plug-in some
                   2315:      * conversion code TODO !!!!
                   2316:      */
                   2317: 
                   2318:     /*
                   2319:      * Wipe out everything which is before the first '<'
                   2320:      */
                   2321:     if (IS_BLANK(CUR)) {
                   2322:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2323:            ctxt->sax->error(ctxt->userData,
                   2324:            "Extra spaces at the beginning of the document are not allowed\n");
                   2325:        ctxt->wellFormed = 0;
                   2326:        SKIP_BLANKS;
                   2327:     }
                   2328: 
                   2329:     if (CUR == 0) {
                   2330:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2331:            ctxt->sax->error(ctxt->userData, "Document is empty\n");
                   2332:        ctxt->wellFormed = 0;
                   2333:     }
                   2334: 
                   2335: 
                   2336:     /*
                   2337:      * Then possibly doc type declaration(s) and more Misc
                   2338:      * (doctypedecl Misc*)?
                   2339:      */
                   2340:     if ((CUR == '<') && (NXT(1) == '!') &&
                   2341:        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   2342:        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   2343:        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   2344:        (UPP(8) == 'E')) {
                   2345:        htmlParseDocTypeDecl(ctxt);
                   2346:     }
                   2347:     SKIP_BLANKS;
                   2348: 
                   2349:     /*
                   2350:      * Create the document if not done already.
                   2351:      */
                   2352:     if (ctxt->myDoc == NULL) {
                   2353:         ctxt->myDoc = htmlNewDoc(NULL, NULL);
                   2354:     }
                   2355: 
                   2356:     /*
                   2357:      * Time to start parsing the tree itself
                   2358:      */
                   2359:     htmlParseElement(ctxt);
                   2360: 
                   2361:     /*
                   2362:      * SAX: end of the document processing.
                   2363:      */
                   2364:     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   2365:         ctxt->sax->endDocument(ctxt->userData);
                   2366:     if (! ctxt->wellFormed) return(-1);
                   2367:     return(0);
                   2368: }
                   2369: 
                   2370: 
                   2371: /********************************************************************************
                   2372:  *                                                                             *
                   2373:  *                             Parser contexts handling                        *
                   2374:  *                                                                             *
                   2375:  ********************************************************************************/
                   2376: 
                   2377: /**
                   2378:  * xmlInitParserCtxt:
                   2379:  * @ctxt:  an HTML parser context
                   2380:  *
                   2381:  * Initialize a parser context
                   2382:  */
                   2383: 
                   2384: void
                   2385: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
                   2386: {
                   2387:     htmlSAXHandler *sax;
                   2388: 
                   2389:     sax = (htmlSAXHandler *) malloc(sizeof(htmlSAXHandler));
                   2390:     if (sax == NULL) {
                   2391:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   2392:     }
                   2393: 
                   2394:     /* Allocate the Input stack */
                   2395:     ctxt->inputTab = (htmlParserInputPtr *) malloc(5 * sizeof(htmlParserInputPtr));
                   2396:     ctxt->inputNr = 0;
                   2397:     ctxt->inputMax = 5;
                   2398:     ctxt->input = NULL;
                   2399:     ctxt->version = NULL;
                   2400:     ctxt->encoding = NULL;
                   2401:     ctxt->standalone = -1;
                   2402: 
                   2403:     /* Allocate the Node stack */
                   2404:     ctxt->nodeTab = (htmlNodePtr *) malloc(10 * sizeof(htmlNodePtr));
                   2405:     ctxt->nodeNr = 0;
                   2406:     ctxt->nodeMax = 10;
                   2407:     ctxt->node = NULL;
                   2408: 
                   2409:     if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
                   2410:     else {
                   2411:         ctxt->sax = sax;
                   2412:        memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
                   2413:     }
                   2414:     ctxt->userData = ctxt;
                   2415:     ctxt->myDoc = NULL;
                   2416:     ctxt->wellFormed = 1;
                   2417:     ctxt->replaceEntities = 0;
                   2418:     ctxt->html = 1;
                   2419:     ctxt->record_info = 0;
                   2420:     xmlInitNodeInfoSeq(&ctxt->node_seq);
                   2421: }
                   2422: 
                   2423: /**
                   2424:  * htmlFreeParserCtxt:
                   2425:  * @ctxt:  an HTML parser context
                   2426:  *
                   2427:  * Free all the memory used by a parser context. However the parsed
                   2428:  * document in ctxt->myDoc is not freed.
                   2429:  */
                   2430: 
                   2431: void
                   2432: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
                   2433: {
                   2434:     htmlParserInputPtr input;
                   2435: 
                   2436:     if (ctxt == NULL) return;
                   2437: 
                   2438:     while ((input = inputPop(ctxt)) != NULL) {
                   2439:         xmlFreeInputStream(input);
                   2440:     }
                   2441: 
                   2442:     if (ctxt->nodeTab != NULL) free(ctxt->nodeTab);
                   2443:     if (ctxt->inputTab != NULL) free(ctxt->inputTab);
                   2444:     if (ctxt->version != NULL) free((char *) ctxt->version);
                   2445:     if ((ctxt->sax != NULL) && (ctxt->sax != &htmlDefaultSAXHandler))
                   2446:         free(ctxt->sax);
                   2447:     free(ctxt);
                   2448: }
                   2449: 
                   2450: /**
                   2451:  * htmlCreateDocParserCtxt :
                   2452:  * @cur:  a pointer to an array of CHAR
                   2453:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2454:  *
                   2455:  * Create a parser context for an HTML document.
                   2456:  *
                   2457:  * Returns the new parser context or NULL
                   2458:  */
                   2459: htmlParserCtxtPtr
                   2460: htmlCreateDocParserCtxt(CHAR *cur, const char *encoding) {
                   2461:     htmlParserCtxtPtr ctxt;
                   2462:     htmlParserInputPtr input;
                   2463:     /* htmlCharEncoding enc; */
                   2464: 
                   2465:     ctxt = (htmlParserCtxtPtr) malloc(sizeof(htmlParserCtxt));
                   2466:     if (ctxt == NULL) {
                   2467:         perror("malloc");
                   2468:        return(NULL);
                   2469:     }
                   2470:     htmlInitParserCtxt(ctxt);
                   2471:     input = (htmlParserInputPtr) malloc(sizeof(htmlParserInput));
                   2472:     if (input == NULL) {
                   2473:         perror("malloc");
                   2474:        free(ctxt);
                   2475:        return(NULL);
                   2476:     }
                   2477: 
                   2478:     /*
                   2479:      * plug some encoding conversion routines here. !!!
                   2480:     if (encoding != NULL) {
                   2481:        enc = htmlDetectCharEncoding(cur);
                   2482:        htmlSwitchEncoding(ctxt, enc);
                   2483:     }
                   2484:      */
                   2485: 
                   2486:     input->filename = NULL;
                   2487:     input->line = 1;
                   2488:     input->col = 1;
                   2489:     input->base = cur;
                   2490:     input->cur = cur;
                   2491:     input->free = NULL;
                   2492: 
                   2493:     inputPush(ctxt, input);
                   2494:     return(ctxt);
                   2495: }
                   2496: 
                   2497: /********************************************************************************
                   2498:  *                                                                             *
                   2499:  *                             User entry points                               *
                   2500:  *                                                                             *
                   2501:  ********************************************************************************/
                   2502: 
                   2503: /**
                   2504:  * htmlSAXParseDoc :
                   2505:  * @cur:  a pointer to an array of CHAR
                   2506:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2507:  * @sax:  the SAX handler block
                   2508:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   2509:  *
                   2510:  * parse an HTML in-memory document and build a tree.
                   2511:  * It use the given SAX function block to handle the parsing callback.
                   2512:  * If sax is NULL, fallback to the default DOM tree building routines.
                   2513:  * 
                   2514:  * Returns the resulting document tree
                   2515:  */
                   2516: 
                   2517: htmlDocPtr
                   2518: htmlSAXParseDoc(CHAR *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
                   2519:     htmlDocPtr ret;
                   2520:     htmlParserCtxtPtr ctxt;
                   2521: 
                   2522:     if (cur == NULL) return(NULL);
                   2523: 
                   2524: 
                   2525:     ctxt = htmlCreateDocParserCtxt(cur, encoding);
                   2526:     if (ctxt == NULL) return(NULL);
                   2527:     if (sax != NULL) { 
                   2528:         ctxt->sax = sax;
                   2529:         ctxt->userData = userData;
                   2530:     }
                   2531: 
                   2532:     htmlParseDocument(ctxt);
                   2533:     ret = ctxt->myDoc;
                   2534:     if (sax != NULL) {
                   2535:        ctxt->sax = NULL;
                   2536:        ctxt->userData = NULL;
                   2537:     }
                   2538:     htmlFreeParserCtxt(ctxt);
                   2539:     
                   2540:     return(ret);
                   2541: }
                   2542: 
                   2543: /**
                   2544:  * htmlParseDoc :
                   2545:  * @cur:  a pointer to an array of CHAR
                   2546:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2547:  *
                   2548:  * parse an HTML in-memory document and build a tree.
                   2549:  * 
                   2550:  * Returns the resulting document tree
                   2551:  */
                   2552: 
                   2553: htmlDocPtr
                   2554: htmlParseDoc(CHAR *cur, const char *encoding) {
                   2555:     return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
                   2556: }
                   2557: 
                   2558: 
                   2559: /**
                   2560:  * htmlCreateFileParserCtxt :
                   2561:  * @filename:  the filename
                   2562:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2563:  *
                   2564:  * Create a parser context for a file content. 
                   2565:  * Automatic support for ZLIB/Compress compressed document is provided
                   2566:  * by default if found at compile-time.
                   2567:  *
                   2568:  * Returns the new parser context or NULL
                   2569:  */
                   2570: htmlParserCtxtPtr
                   2571: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
                   2572: {
                   2573:     htmlParserCtxtPtr ctxt;
                   2574: #ifdef HAVE_ZLIB_H
                   2575:     gzFile input;
                   2576: #else
                   2577:     int input;
                   2578: #endif
                   2579:     int res;
                   2580:     int len;
                   2581:     int cnt;
                   2582:     struct stat buf;
                   2583:     char *buffer, *nbuf;
                   2584:     htmlParserInputPtr inputStream;
                   2585:     /* htmlCharEncoding enc; */
                   2586: 
                   2587: #define MINLEN 40000
                   2588: 
                   2589:     if (strcmp(filename,"-") == 0) {
                   2590: #ifdef HAVE_ZLIB_H
                   2591:         input = gzdopen (fileno(stdin), "r");
                   2592:         if (input == NULL) {
                   2593:             fprintf (stderr, "Cannot read from stdin\n");
                   2594:             perror ("gzdopen failed");
                   2595:             return(NULL);
                   2596:        }
                   2597: #else
                   2598: #ifdef WIN32
                   2599:         input = -1;
                   2600: #else
                   2601:         input = fileno(stdin);
                   2602: #endif
                   2603:        if (input < 0) {
                   2604:            fprintf (stderr, "Cannot read from stdin\n");
                   2605:            perror ("open failed");
                   2606:            return(NULL);
                   2607:        }
                   2608: #endif
                   2609:        len = MINLEN;
                   2610:     } else {
                   2611: #ifdef HAVE_ZLIB_H
                   2612:        input = gzopen (filename, "r");
                   2613:        if (input == NULL) {
                   2614:            fprintf (stderr, "Cannot read file %s :\n", filename);
                   2615:            perror ("gzopen failed");
                   2616:            return(NULL);
                   2617:        }
                   2618: #else
                   2619: #ifdef WIN32
                   2620:        input = _open (filename, O_RDONLY | _O_BINARY);
                   2621: #else
                   2622:        input = open (filename, O_RDONLY);
                   2623: #endif
                   2624:        if (input < 0) {
                   2625:            fprintf (stderr, "Cannot read file %s :\n", filename);
                   2626:            perror ("open failed");
                   2627:            return(NULL);
                   2628:        }
                   2629: #endif
                   2630:        res = stat(filename, &buf);
                   2631:        if (res < 0)
                   2632:                return(NULL);
1.2       daniel   2633:        len = buf.st_size;
1.1       daniel   2634:        if (len < MINLEN)
                   2635:                len = MINLEN;
                   2636:     }
1.2       daniel   2637:     buffer = (char *)malloc((len+1)*sizeof(char));
1.1       daniel   2638:     if (buffer == NULL) {
                   2639:            fprintf (stderr, "Cannot malloc\n");
                   2640:            perror ("malloc failed");
                   2641:            return(NULL);
                   2642:     }
                   2643: 
                   2644:     cnt = 0;
                   2645:     while(1) {
                   2646:        if (cnt == len) {
                   2647:            len *= 2;
1.2       daniel   2648:            nbuf =  (char *)realloc(buffer,(len+1)*sizeof(char));
1.1       daniel   2649:            if (nbuf == NULL) {
                   2650:                fprintf(stderr,"Cannot realloc\n");
                   2651:                free(buffer);
                   2652:                perror ("realloc failed");
                   2653:                return(NULL);
                   2654:            }
                   2655:            buffer = nbuf;
                   2656:        }
                   2657: #ifdef HAVE_ZLIB_H
                   2658:        res = gzread(input, &buffer[cnt], len-cnt);
                   2659: #else
                   2660:        res = read(input, &buffer[cnt], len-cnt);
                   2661: #endif
                   2662:        if (res < 0) {
                   2663:            fprintf (stderr, "Cannot read file %s :\n", filename);
                   2664: #ifdef HAVE_ZLIB_H
                   2665:            perror ("gzread failed");
                   2666: #else
                   2667:            perror ("read failed");
                   2668: #endif
                   2669:            return(NULL);
                   2670:        }
                   2671:        if (res == 0)
                   2672:            break;
                   2673:        cnt +=res;
                   2674:     }
                   2675:     buffer[cnt] = '\0';
                   2676: 
                   2677: #ifdef HAVE_ZLIB_H
                   2678:     gzclose(input);
                   2679: #else
                   2680:     close(input);
                   2681: #endif
                   2682: 
                   2683:     ctxt = (htmlParserCtxtPtr) malloc(sizeof(htmlParserCtxt));
                   2684:     if (ctxt == NULL) {
                   2685:         perror("malloc");
                   2686:        return(NULL);
                   2687:     }
                   2688:     htmlInitParserCtxt(ctxt);
                   2689:     inputStream = (htmlParserInputPtr) malloc(sizeof(htmlParserInput));
                   2690:     if (inputStream == NULL) {
                   2691:         perror("malloc");
                   2692:        free(ctxt);
                   2693:        return(NULL);
                   2694:     }
                   2695: 
                   2696:     inputStream->filename = strdup(filename);
                   2697:     inputStream->line = 1;
                   2698:     inputStream->col = 1;
                   2699: 
                   2700:     /*
                   2701:      * plug some encoding conversion routines here. !!!
                   2702:     if (encoding != NULL) {
                   2703:        enc = htmlDetectCharEncoding(buffer);
                   2704:        htmlSwitchEncoding(ctxt, enc);
                   2705:     }
                   2706:      */
                   2707: 
                   2708:     inputStream->base = buffer;
                   2709:     inputStream->cur = buffer;
                   2710:     inputStream->free = (xmlParserInputDeallocate) free;
                   2711: 
                   2712:     inputPush(ctxt, inputStream);
                   2713:     return(ctxt);
                   2714: }
                   2715: 
                   2716: /**
                   2717:  * htmlSAXParseFile :
                   2718:  * @filename:  the filename
                   2719:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2720:  * @sax:  the SAX handler block
                   2721:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   2722:  *
                   2723:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   2724:  * compressed document is provided by default if found at compile-time.
                   2725:  * It use the given SAX function block to handle the parsing callback.
                   2726:  * If sax is NULL, fallback to the default DOM tree building routines.
                   2727:  *
                   2728:  * Returns the resulting document tree
                   2729:  */
                   2730: 
                   2731: htmlDocPtr
                   2732: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, 
                   2733:                  void *userData) {
                   2734:     htmlDocPtr ret;
                   2735:     htmlParserCtxtPtr ctxt;
                   2736: 
                   2737:     ctxt = htmlCreateFileParserCtxt(filename, encoding);
                   2738:     if (ctxt == NULL) return(NULL);
                   2739:     if (sax != NULL) {
                   2740:         ctxt->sax = sax;
                   2741:         ctxt->userData = userData;
                   2742:     }
                   2743: 
                   2744:     htmlParseDocument(ctxt);
                   2745: 
                   2746:     ret = ctxt->myDoc;
                   2747:     if (sax != NULL) {
                   2748:         ctxt->sax = NULL;
                   2749:         ctxt->userData = NULL;
                   2750:     }
                   2751:     htmlFreeParserCtxt(ctxt);
                   2752:     
                   2753:     return(ret);
                   2754: }
                   2755: 
                   2756: /**
                   2757:  * htmlParseFile :
                   2758:  * @filename:  the filename
                   2759:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2760:  *
                   2761:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   2762:  * compressed document is provided by default if found at compile-time.
                   2763:  *
                   2764:  * Returns the resulting document tree
                   2765:  */
                   2766: 
                   2767: htmlDocPtr
                   2768: htmlParseFile(const char *filename, const char *encoding) {
                   2769:     return(htmlSAXParseFile(filename, encoding, NULL, NULL));
                   2770: }

Webmaster