DOCBparser.c revision eae522a0d8aa9f830d2e447f29b504030dbb6bbd
1/*
2 * DOCBparser.c : an attempt to parse SGML Docbook documents
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#include "libxml.h"
10#ifdef LIBXML_DOCB_ENABLED
11
12#include <string.h>
13#ifdef HAVE_CTYPE_H
14#include <ctype.h>
15#endif
16#ifdef HAVE_STDLIB_H
17#include <stdlib.h>
18#endif
19#ifdef HAVE_SYS_STAT_H
20#include <sys/stat.h>
21#endif
22#ifdef HAVE_FCNTL_H
23#include <fcntl.h>
24#endif
25#ifdef HAVE_UNISTD_H
26#include <unistd.h>
27#endif
28#ifdef HAVE_ZLIB_H
29#include <zlib.h>
30#endif
31
32#include <libxml/xmlmemory.h>
33#include <libxml/tree.h>
34#include <libxml/SAX.h>
35#include <libxml/parser.h>
36#include <libxml/parserInternals.h>
37#include <libxml/xmlerror.h>
38#include <libxml/DOCBparser.h>
39#include <libxml/entities.h>
40#include <libxml/encoding.h>
41#include <libxml/valid.h>
42#include <libxml/xmlIO.h>
43#include <libxml/uri.h>
44
45/*
46 * Internal description of an SGML entity
47 */
48typedef struct _docbEntityDesc docbEntityDesc;
49typedef docbEntityDesc *docbEntityDescPtr;
50struct _docbEntityDesc {
51    int value;         /* the UNICODE value for the character */
52    const char *name;  /* The entity name */
53    const char *desc;   /* the description */
54};
55
56#if 0
57docbElemDescPtr        docbTagLookup   (const xmlChar *tag);
58docbEntityDescPtr      docbEntityLookup(const xmlChar *name);
59docbEntityDescPtr      docbEntityValueLookup(int value);
60
61int                    docbIsAutoClosed(docbDocPtr doc,
62                                        docbNodePtr elem);
63int                    docbAutoCloseTag(docbDocPtr doc,
64                                        const xmlChar *name,
65                                        docbNodePtr elem);
66
67#endif
68static int                    docbParseCharRef(docbParserCtxtPtr ctxt);
69static docbEntityDescPtr      docbParseEntityRef(docbParserCtxtPtr ctxt,
70                                        xmlChar **str);
71static void                   docbParseElement(docbParserCtxtPtr ctxt);
72
73/*
74 * Internal description of an SGML element
75 */
76typedef struct _docbElemDesc docbElemDesc;
77typedef docbElemDesc *docbElemDescPtr;
78struct _docbElemDesc {
79    const char *name;  /* The tag name */
80    int startTag;       /* Whether the start tag can be implied */
81    int endTag;         /* Whether the end tag can be implied */
82    int empty;          /* Is this an empty element ? */
83    int depr;           /* Is this a deprecated element ? */
84    int dtd;            /* 1: only in Loose DTD, 2: only Frameset one */
85    const char *desc;   /* the description */
86};
87
88
89#define DOCB_MAX_NAMELEN 1000
90#define DOCB_PARSER_BIG_BUFFER_SIZE 1000
91#define DOCB_PARSER_BUFFER_SIZE 100
92
93/* #define DEBUG */
94/* #define DEBUG_PUSH */
95
96/************************************************************************
97 *                                                                     *
98 *             Parser stacks related functions and macros              *
99 *                                                                     *
100 ************************************************************************/
101
102/*
103 * Generic function for accessing stacks in the Parser Context
104 */
105
106#define PUSH_AND_POP(scope, type, name)                                        \
107scope int docb##name##Push(docbParserCtxtPtr ctxt, type value) {       \
108    if (ctxt->name##Nr >= ctxt->name##Max) {                           \
109       ctxt->name##Max *= 2;                                           \
110        ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,         \
111                    ctxt->name##Max * sizeof(ctxt->name##Tab[0]));     \
112        if (ctxt->name##Tab == NULL) {                                 \
113           xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");                      \
114           return(0);                                                  \
115       }                                                               \
116    }                                                                  \
117    ctxt->name##Tab[ctxt->name##Nr] = value;                           \
118    ctxt->name = value;                                                        \
119    return(ctxt->name##Nr++);                                          \
120}                                                                      \
121scope type docb##name##Pop(docbParserCtxtPtr ctxt) {                   \
122    type ret;                                                          \
123    if (ctxt->name##Nr < 0) return(0);                                 \
124    ctxt->name##Nr--;                                                  \
125    if (ctxt->name##Nr < 0) return(0);                                 \
126    if (ctxt->name##Nr > 0)                                            \
127       ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1];               \
128    else                                                               \
129        ctxt->name = NULL;                                             \
130    ret = ctxt->name##Tab[ctxt->name##Nr];                             \
131    ctxt->name##Tab[ctxt->name##Nr] = 0;                               \
132    return(ret);                                                       \
133}                                                                      \
134
135/* PUSH_AND_POP(static, xmlNodePtr, node) */
136PUSH_AND_POP(static, xmlChar*, name)
137
138/*
139 * Macros for accessing the content. Those should be used only by the parser,
140 * and not exported.
141 *
142 * Dirty macros, i.e. one need to make assumption on the context to use them
143 *
144 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
145 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
146 *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
147 *           in UNICODE mode. This should be used internally by the parser
148 *           only to compare to ASCII values otherwise it would break when
149 *           running with UTF-8 encoding.
150 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
151 *           to compare on ASCII based substring.
152 *   UPP(n)  returns the n'th next xmlChar converted to uppercase. Same as CUR
153 *           it should be used only to compare on ASCII based substring.
154 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
155 *           strings within the parser.
156 *
157 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
158 *
159 *   CURRENT Returns the current char value, with the full decoding of
160 *           UTF-8 if we are using this mode. It returns an int.
161 *   NEXT    Skip to the next character, this does the proper decoding
162 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
163 *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
164 */
165
166#define UPPER (toupper(*ctxt->input->cur))
167
168#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val)
169
170#define NXT(val) ctxt->input->cur[(val)]
171
172#define UPP(val) (toupper(ctxt->input->cur[(val)]))
173
174#define CUR_PTR ctxt->input->cur
175
176#define SHRINK  xmlParserInputShrink(ctxt->input)
177
178#define GROW  xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
179
180#define CURRENT ((int) (*ctxt->input->cur))
181
182#define SKIP_BLANKS docbSkipBlankChars(ctxt)
183
184/* Imported from XML */
185
186/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
187#define CUR ((int) (*ctxt->input->cur))
188#define NEXT xmlNextChar(ctxt),ctxt->nbChars++
189
190#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
191#define NXT(val) ctxt->input->cur[(val)]
192#define CUR_PTR ctxt->input->cur
193
194
195#define NEXTL(l) do {                                                  \
196    if (*(ctxt->input->cur) == '\n') {                                 \
197       ctxt->input->line++; ctxt->input->col = 1;                      \
198    } else ctxt->input->col++;                                         \
199    ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++;           \
200  } while (0)
201
202/************
203    \
204    if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);    \
205    if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
206 ************/
207
208#define CUR_CHAR(l) docbCurrentChar(ctxt, &l)
209#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
210
211#define COPY_BUF(l,b,i,v)                                              \
212    if (l == 1) b[i++] = (xmlChar) v;                                  \
213    else i += xmlCopyChar(l,&b[i],v)
214
215/**
216 * docbCurrentChar:
217 * @ctxt:  the DocBook SGML parser context
218 * @len:  pointer to the length of the char read
219 *
220 * The current char value, if using UTF-8 this may actaully span multiple
221 * bytes in the input buffer. Implement the end of line normalization:
222 * 2.11 End-of-Line Handling
223 * If the encoding is unspecified, in the case we find an ISO-Latin-1
224 * char, then the encoding converter is plugged in automatically.
225 *
226 * Returns the current char value and its lenght
227 */
228
229static int
230docbCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
231    if (ctxt->instate == XML_PARSER_EOF)
232       return(0);
233
234    if (ctxt->token != 0) {
235       *len = 0;
236       return(ctxt->token);
237    }
238    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
239       /*
240        * We are supposed to handle UTF8, check it's valid
241        * From rfc2044: encoding of the Unicode values on UTF-8:
242        *
243        * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
244        * 0000 0000-0000 007F   0xxxxxxx
245        * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
246        * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
247        *
248        * Check for the 0x110000 limit too
249        */
250       const unsigned char *cur = ctxt->input->cur;
251       unsigned char c;
252       unsigned int val;
253
254       c = *cur;
255       if (c & 0x80) {
256           if (cur[1] == 0)
257               xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
258           if ((cur[1] & 0xc0) != 0x80)
259               goto encoding_error;
260           if ((c & 0xe0) == 0xe0) {
261
262               if (cur[2] == 0)
263                   xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
264               if ((cur[2] & 0xc0) != 0x80)
265                   goto encoding_error;
266               if ((c & 0xf0) == 0xf0) {
267                   if (cur[3] == 0)
268                       xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
269                   if (((c & 0xf8) != 0xf0) ||
270                       ((cur[3] & 0xc0) != 0x80))
271                       goto encoding_error;
272                   /* 4-byte code */
273                   *len = 4;
274                   val = (cur[0] & 0x7) << 18;
275                   val |= (cur[1] & 0x3f) << 12;
276                   val |= (cur[2] & 0x3f) << 6;
277                   val |= cur[3] & 0x3f;
278               } else {
279                 /* 3-byte code */
280                   *len = 3;
281                   val = (cur[0] & 0xf) << 12;
282                   val |= (cur[1] & 0x3f) << 6;
283                   val |= cur[2] & 0x3f;
284               }
285           } else {
286             /* 2-byte code */
287               *len = 2;
288               val = (cur[0] & 0x1f) << 6;
289               val |= cur[1] & 0x3f;
290           }
291           if (!IS_CHAR(val)) {
292               ctxt->errNo = XML_ERR_INVALID_ENCODING;
293               if ((ctxt->sax != NULL) &&
294                   (ctxt->sax->error != NULL))
295                   ctxt->sax->error(ctxt->userData,
296                                    "Char 0x%X out of allowed range\n", val);
297               ctxt->wellFormed = 0;
298               ctxt->disableSAX = 1;
299           }
300           return(val);
301       } else {
302           /* 1-byte code */
303           *len = 1;
304           return((int) *ctxt->input->cur);
305       }
306    }
307    /*
308     * Assume it's a fixed lenght encoding (1) with
309     * a compatibke encoding for the ASCII set, since
310     * XML constructs only use < 128 chars
311     */
312    *len = 1;
313    if ((int) *ctxt->input->cur < 0x80)
314       return((int) *ctxt->input->cur);
315
316    /*
317     * Humm this is bad, do an automatic flow conversion
318     */
319    xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
320    ctxt->charset = XML_CHAR_ENCODING_UTF8;
321    return(xmlCurrentChar(ctxt, len));
322
323encoding_error:
324    /*
325     * If we detect an UTF8 error that probably mean that the
326     * input encoding didn't get properly advertized in the
327     * declaration header. Report the error and switch the encoding
328     * to ISO-Latin-1 (if you don't like this policy, just declare the
329     * encoding !)
330     */
331    ctxt->errNo = XML_ERR_INVALID_ENCODING;
332    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
333       ctxt->sax->error(ctxt->userData,
334                        "Input is not proper UTF-8, indicate encoding !\n");
335       ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
336                       ctxt->input->cur[0], ctxt->input->cur[1],
337                       ctxt->input->cur[2], ctxt->input->cur[3]);
338    }
339
340    ctxt->charset = XML_CHAR_ENCODING_8859_1;
341    *len = 1;
342    return((int) *ctxt->input->cur);
343}
344
345#if 0
346/**
347 * sgmlNextChar:
348 * @ctxt:  the DocBook SGML parser context
349 *
350 * Skip to the next char input char.
351 */
352
353static void
354sgmlNextChar(docbParserCtxtPtr ctxt) {
355    if (ctxt->instate == XML_PARSER_EOF)
356       return;
357    if ((*ctxt->input->cur == 0) &&
358        (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
359           xmlPopInput(ctxt);
360    } else {
361        if (*(ctxt->input->cur) == '\n') {
362           ctxt->input->line++; ctxt->input->col = 1;
363       } else ctxt->input->col++;
364       ctxt->input->cur++;
365       ctxt->nbChars++;
366        if (*ctxt->input->cur == 0)
367           xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
368    }
369}
370#endif
371
372/**
373 * docbSkipBlankChars:
374 * @ctxt:  the DocBook SGML parser context
375 *
376 * skip all blanks character found at that point in the input streams.
377 *
378 * Returns the number of space chars skipped
379 */
380
381static int
382docbSkipBlankChars(xmlParserCtxtPtr ctxt) {
383    int res = 0;
384
385    while (IS_BLANK(*(ctxt->input->cur))) {
386       if ((*ctxt->input->cur == 0) &&
387           (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
388               xmlPopInput(ctxt);
389       } else {
390           if (*(ctxt->input->cur) == '\n') {
391               ctxt->input->line++; ctxt->input->col = 1;
392           } else ctxt->input->col++;
393           ctxt->input->cur++;
394           ctxt->nbChars++;
395           if (*ctxt->input->cur == 0)
396               xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
397       }
398       res++;
399    }
400    return(res);
401}
402
403
404
405/************************************************************************
406 *                                                                     *
407 *             The list of SGML elements and their properties          *
408 *                                                                     *
409 ************************************************************************/
410
411/*
412 *  Start Tag: 1 means the start tag can be ommited
413 *  End Tag:   1 means the end tag can be ommited
414 *             2 means it's forbidden (empty elements)
415 *  Depr:      this element is deprecated
416 *  DTD:       1 means that this element is valid only in the Loose DTD
417 *             2 means that this element is valid only in the Frameset DTD
418 *
419 * Name,Start Tag,End Tag,  Empty,  Depr.,    DTD, Description
420 */
421static docbElemDesc
422docbookElementTable[] = {
423{ "abbrev",    0,      0,      0,      3,      0, "" }, /* word */
424{ "abstract",  0,      0,      0,      9,      0, "" }, /* title */
425{ "accel",     0,      0,      0,      7,      0, "" }, /* smallcptr */
426{ "ackno",     0,      0,      0,      4,      0, "" }, /* docinfo */
427{ "acronym",   0,      0,      0,      3,      0, "" }, /* word */
428{ "action",    0,      0,      0,      7,      0, "" }, /* smallcptr */
429{ "address",   0,      0,      0,      1,      0, "" },
430{ "affiliation",0,     0,      0,      9,      0, "" }, /* shortaffil */
431{ "alt",       0,      0,      0,      1,      0, "" },
432{ "anchor",    0,      2,      1,      0,      0, "" },
433{ "answer",    0,      0,      0,      9,      0, "" }, /* label */
434{ "appendix",  0,      0,      0,      9,      0, "" }, /* appendixinfo */
435{ "appendixinfo",0,    0,      0,      9,      0, "" }, /* graphic */
436{ "application",0,     0,      0,      2,      0, "" }, /* para */
437{ "area",      0,      2,      1,      0,      0, "" },
438{ "areaset",   0,      0,      0,      9,      0, "" }, /* area */
439{ "areaspec",  0,      0,      0,      9,      0, "" }, /* area */
440{ "arg",       0,      0,      0,      1,      0, "" },
441{ "article",   0,      0,      0,      9,      0, "" }, /* div.title.content */
442{ "articleinfo",0,     0,      0,      9,      0, "" }, /* graphic */
443{ "artpagenums",0,     0,      0,      4,      0, "" }, /* docinfo */
444{ "attribution",0,     0,      0,      2,      0, "" }, /* para */
445{ "audiodata", 0,      2,      1,      0,      0, "" },
446{ "audioobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
447{ "authorblurb",0,     0,      0,      9,      0, "" }, /* title */
448{ "authorgroup",0,     0,      0,      9,      0, "" }, /* author */
449{ "authorinitials",0,  0,      0,      4,      0, "" }, /* docinfo */
450{ "author",    0,      0,      0,      9,      0, "" }, /* person.ident.mix */
451{ "beginpage", 0,      2,      1,      0,      0, "" },
452{ "bibliodiv", 0,      0,      0,      9,      0, "" }, /* sect.title.content */
453{ "biblioentry",0,     0,      0,      9,      0, "" }, /* articleinfo */
454{ "bibliography",0,    0,      0,      9,      0, "" }, /* bibliographyinfo */
455{ "bibliographyinfo",0,        0,      0,      9,      0, "" }, /* graphic */
456{ "bibliomisc",        0,      0,      0,      2,      0, "" }, /* para */
457{ "bibliomixed",0,     0,      0,      1,      0, "" }, /* %bibliocomponent.mix, bibliomset) */
458{ "bibliomset",        0,      0,      0,      1,      0, "" }, /* %bibliocomponent.mix; | bibliomset) */
459{ "biblioset", 0,      0,      0,      9,      0, "" }, /* bibliocomponent.mix */
460{ "blockquote",        0,      0,      0,      9,      0, "" }, /* title */
461{ "book",      0,      0,      0,      9,      0, "" }, /* div.title.content */
462{ "bookinfo",  0,      0,      0,      9,      0, "" }, /* graphic */
463{ "bridgehead",        0,      0,      0,      8,      0, "" }, /* title */
464{ "callout",   0,      0,      0,      9,      0, "" }, /* component.mix */
465{ "calloutlist",0,     0,      0,      9,      0, "" }, /* formalobject.title.content */
466{ "caption",   0,      0,      0,      9,      0, "" }, /* textobject.mix */
467{ "caution",   0,      0,      0,      9,      0, "" }, /* title */
468{ "chapter",   0,      0,      0,      9,      0, "" }, /* chapterinfo */
469{ "chapterinfo",0,     0,      0,      9,      0, "" }, /* graphic */
470{ "citation",  0,      0,      0,      2,      0, "" }, /* para */
471{ "citerefentry",0,    0,      0,      9,      0, "" }, /* refentrytitle */
472{ "citetitle", 0,      0,      0,      2,      0, "" }, /* para */
473{ "city",      0,      0,      0,      4,      0, "" }, /* docinfo */
474{ "classname", 0,      0,      0,      7,      0, "" }, /* smallcptr */
475{ "classsynopsisinfo",0,0,     0,      9,      0, "" }, /* cptr */
476{ "classsynopsis",0,   0,      0,      9,      0, "" }, /* ooclass */
477{ "cmdsynopsis",0,     0,      0,      9,      0, "" }, /* command */
478{ "co",                0,      2,      1,      0,      0, "" },
479{ "collab",    0,      0,      0,      9,      0, "" }, /* collabname */
480{ "collabname",        0,      0,      0,      4,      0, "" }, /* docinfo */
481{ "colophon",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
482{ "colspec",   0,      2,      1,      0,      0, "" },
483{ "colspec",   0,      2,      1,      0,      0, "" },
484{ "command",   0,      0,      0,      9,      0, "" }, /* cptr */
485{ "computeroutput",0,  0,      0,      9,      0, "" }, /* cptr */
486{ "confdates", 0,      0,      0,      4,      0, "" }, /* docinfo */
487{ "confgroup", 0,      0,      0,      9,      0, "" }, /* confdates */
488{ "confnum",   0,      0,      0,      4,      0, "" }, /* docinfo */
489{ "confsponsor",0,     0,      0,      4,      0, "" }, /* docinfo */
490{ "conftitle", 0,      0,      0,      4,      0, "" }, /* docinfo */
491{ "constant",  0,      0,      0,      7,      0, "" }, /* smallcptr */
492{ "constructorsynopsis",0,0,   0,      9,      0, "" }, /* modifier */
493{ "contractnum",0,     0,      0,      4,      0, "" }, /* docinfo */
494{ "contractsponsor",0, 0,      0,      4,      0, "" }, /* docinfo */
495{ "contrib",   0,      0,      0,      4,      0, "" }, /* docinfo */
496{ "copyright", 0,      0,      0,      9,      0, "" }, /* year */
497{ "corpauthor",        0,      0,      0,      4,      0, "" }, /* docinfo */
498{ "corpname",  0,      0,      0,      4,      0, "" }, /* docinfo */
499{ "country",   0,      0,      0,      4,      0, "" }, /* docinfo */
500{ "database",  0,      0,      0,      7,      0, "" }, /* smallcptr */
501{ "date",      0,      0,      0,      4,      0, "" }, /* docinfo */
502{ "dedication",        0,      0,      0,      9,      0, "" }, /* sect.title.content */
503{ "destructorsynopsis",0,0,    0,      9,      0, "" }, /* modifier */
504{ "edition",   0,      0,      0,      4,      0, "" }, /* docinfo */
505{ "editor",    0,      0,      0,      9,      0, "" }, /* person.ident.mix */
506{ "email",     0,      0,      0,      4,      0, "" }, /* docinfo */
507{ "emphasis",  0,      0,      0,      2,      0, "" }, /* para */
508{ "entry",     0,      0,      0,      9,      0, "" }, /* tbl.entry.mdl */
509{ "entrytbl",  0,      0,      0,      9,      0, "" }, /* tbl.entrytbl.mdl */
510{ "envar",     0,      0,      0,      7,      0, "" }, /* smallcptr */
511{ "epigraph",  0,      0,      0,      9,      0, "" }, /* attribution */
512{ "equation",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
513{ "errorcode", 0,      0,      0,      7,      0, "" }, /* smallcptr */
514{ "errorname", 0,      0,      0,      7,      0, "" }, /* smallcptr */
515{ "errortype", 0,      0,      0,      7,      0, "" }, /* smallcptr */
516{ "example",   0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
517{ "exceptionname",0,   0,      0,      7,      0, "" }, /* smallcptr */
518{ "fax",       0,      0,      0,      4,      0, "" }, /* docinfo */
519{ "fieldsynopsis",     0,      0,      0,      9,      0, "" }, /* modifier */
520{ "figure",    0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
521{ "filename",  0,      0,      0,      7,      0, "" }, /* smallcptr */
522{ "firstname", 0,      0,      0,      4,      0, "" }, /* docinfo */
523{ "firstterm", 0,      0,      0,      3,      0, "" }, /* word */
524{ "footnote",  0,      0,      0,      9,      0, "" }, /* footnote.mix */
525{ "footnoteref",0,     2,      1,      0,      0, "" },
526{ "foreignphrase",0,   0,      0,      2,      0, "" }, /* para */
527{ "formalpara",        0,      0,      0,      9,      0, "" }, /* title */
528{ "funcdef",   0,      0,      0,      1,      0, "" },
529{ "funcparams",        0,      0,      0,      9,      0, "" }, /* cptr */
530{ "funcprototype",0,   0,      0,      9,      0, "" }, /* funcdef */
531{ "funcsynopsis",0,    0,      0,      9,      0, "" }, /* funcsynopsisinfo */
532{ "funcsynopsisinfo",  0,      0,      0,      9,      0, "" }, /* cptr */
533{ "function",  0,      0,      0,      9,      0, "" }, /* cptr */
534{ "glossary",  0,      0,      0,      9,      0, "" }, /* glossaryinfo */
535{ "glossaryinfo",0,    0,      0,      9,      0, "" }, /* graphic */
536{ "glossdef",  0,      0,      0,      9,      0, "" }, /* glossdef.mix */
537{ "glossdiv",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
538{ "glossentry",        0,      0,      0,      9,      0, "" }, /* glossterm */
539{ "glosslist", 0,      0,      0,      9,      0, "" }, /* glossentry */
540{ "glossseealso",0,    0,      0,      2,      0, "" }, /* para */
541{ "glosssee",  0,      0,      0,      2,      0, "" }, /* para */
542{ "glossterm", 0,      0,      0,      2,      0, "" }, /* para */
543{ "graphic",   0,      2,      1,      0,      0, "" },
544{ "graphicco", 0,      0,      0,      9,      0, "" }, /* areaspec */
545{ "group",     0,      0,      0,      9,      0, "" }, /* arg */
546{ "guibutton", 0,      0,      0,      7,      0, "" }, /* smallcptr */
547{ "guiicon",   0,      0,      0,      7,      0, "" }, /* smallcptr */
548{ "guilabel",  0,      0,      0,      7,      0, "" }, /* smallcptr */
549{ "guimenuitem",0,     0,      0,      7,      0, "" }, /* smallcptr */
550{ "guimenu",   0,      0,      0,      7,      0, "" }, /* smallcptr */
551{ "guisubmenu",        0,      0,      0,      7,      0, "" }, /* smallcptr */
552{ "hardware",  0,      0,      0,      7,      0, "" }, /* smallcptr */
553{ "highlights",        0,      0,      0,      9,      0, "" }, /* highlights.mix */
554{ "holder",    0,      0,      0,      4,      0, "" }, /* docinfo */
555{ "honorific", 0,      0,      0,      4,      0, "" }, /* docinfo */
556{ "imagedata", 0,      2,      1,      0,      0, "" },
557{ "imageobjectco",0,   0,      0,      9,      0, "" }, /* areaspec */
558{ "imageobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
559{ "important", 0,      0,      0,      9,      0, "" }, /* title */
560{ "indexdiv",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
561{ "indexentry",        0,      0,      0,      9,      0, "" }, /* primaryie */
562{ "index",     0,      0,      0,      9,      0, "" }, /* indexinfo */
563{ "indexinfo", 0,      0,      0,      9,      0, "" }, /* graphic */
564{ "indexterm", 0,      0,      0,      9,      0, "" }, /* primary */
565{ "informalequation",0,        0,      0,      9,      0, "" }, /* equation.content */
566{ "informalexample",0, 0,      0,      9,      0, "" }, /* example.mix */
567{ "informalfigure",0,  0,      0,      9,      0, "" }, /* figure.mix */
568{ "informaltable",0,   0,      0,      9,      0, "" }, /* graphic */
569{ "initializer",0,     0,      0,      7,      0, "" }, /* smallcptr */
570{ "inlineequation",0,  0,      0,      9,      0, "" }, /* inlineequation.content */
571{ "inlinegraphic",0,   2,      1,      0,      0, "" },
572{ "inlinemediaobject",0,0,     0,      9,      0, "" }, /* objectinfo */
573{ "interfacename",0,   0,      0,      7,      0, "" }, /* smallcptr */
574{ "interface", 0,      0,      0,      7,      0, "" }, /* smallcptr */
575{ "invpartnumber",0,   0,      0,      4,      0, "" }, /* docinfo */
576{ "isbn",      0,      0,      0,      4,      0, "" }, /* docinfo */
577{ "issn",      0,      0,      0,      4,      0, "" }, /* docinfo */
578{ "issuenum",  0,      0,      0,      4,      0, "" }, /* docinfo */
579{ "itemizedlist",0,    0,      0,      9,      0, "" }, /* formalobject.title.content */
580{ "itermset",  0,      0,      0,      9,      0, "" }, /* indexterm */
581{ "jobtitle",  0,      0,      0,      4,      0, "" }, /* docinfo */
582{ "keycap",    0,      0,      0,      7,      0, "" }, /* smallcptr */
583{ "keycode",   0,      0,      0,      7,      0, "" }, /* smallcptr */
584{ "keycombo",  0,      0,      0,      9,      0, "" }, /* keycap */
585{ "keysym",    0,      0,      0,      7,      0, "" }, /* smallcptr */
586{ "keyword",   0,      0,      0,      1,      0, "" },
587{ "keywordset",        0,      0,      0,      9,      0, "" }, /* keyword */
588{ "label",     0,      0,      0,      3,      0, "" }, /* word */
589{ "legalnotice",0,     0,      0,      9,      0, "" }, /* title */
590{ "lineage",   0,      0,      0,      4,      0, "" }, /* docinfo */
591{ "lineannotation",0,  0,      0,      2,      0, "" }, /* para */
592{ "link",      0,      0,      0,      2,      0, "" }, /* para */
593{ "listitem",  0,      0,      0,      9,      0, "" }, /* component.mix */
594{ "literal",   0,      0,      0,      9,      0, "" }, /* cptr */
595{ "literallayout",0,   0,      0,      2,      0, "" }, /* para */
596{ "lot",       0,      0,      0,      9,      0, "" }, /* bookcomponent.title.content */
597{ "lotentry",  0,      0,      0,      2,      0, "" }, /* para */
598{ "manvolnum", 0,      0,      0,      3,      0, "" }, /* word */
599{ "markup",    0,      0,      0,      7,      0, "" }, /* smallcptr */
600{ "medialabel",        0,      0,      0,      7,      0, "" }, /* smallcptr */
601{ "mediaobjectco",0,   0,      0,      9,      0, "" }, /* objectinfo */
602{ "mediaobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
603{ "member",    0,      0,      0,      2,      0, "" }, /* para */
604{ "menuchoice",        0,      0,      0,      9,      0, "" }, /* shortcut */
605{ "methodname",        0,      0,      0,      7,      0, "" }, /* smallcptr */
606{ "methodparam",0,     0,      0,      9,      0, "" }, /* modifier */
607{ "methodsynopsis",0,  0,      0,      9,      0, "" }, /* modifier */
608{ "modespec",  0,      0,      0,      4,      0, "" }, /* docinfo */
609{ "modifier",  0,      0,      0,      7,      0, "" }, /* smallcptr */
610{ "mousebutton",0,     0,      0,      7,      0, "" }, /* smallcptr */
611{ "msgaud",    0,      0,      0,      2,      0, "" }, /* para */
612{ "msgentry",  0,      0,      0,      9,      0, "" }, /* msg */
613{ "msgexplan", 0,      0,      0,      9,      0, "" }, /* title */
614{ "msginfo",   0,      0,      0,      9,      0, "" }, /* msglevel */
615{ "msglevel",  0,      0,      0,      7,      0, "" }, /* smallcptr */
616{ "msgmain",   0,      0,      0,      9,      0, "" }, /* title */
617{ "msgorig",   0,      0,      0,      7,      0, "" }, /* smallcptr */
618{ "msgrel",    0,      0,      0,      9,      0, "" }, /* title */
619{ "msgset",    0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
620{ "msgsub",    0,      0,      0,      9,      0, "" }, /* title */
621{ "msgtext",   0,      0,      0,      9,      0, "" }, /* component.mix */
622{ "msg",       0,      0,      0,      9,      0, "" }, /* title */
623{ "note",      0,      0,      0,      9,      0, "" }, /* title */
624{ "objectinfo",        0,      0,      0,      9,      0, "" }, /* graphic */
625{ "olink",     0,      0,      0,      2,      0, "" }, /* para */
626{ "ooclass",   0,      0,      0,      9,      0, "" }, /* modifier */
627{ "ooexception",0,     0,      0,      9,      0, "" }, /* modifier */
628{ "oointerface",0,     0,      0,      9,      0, "" }, /* modifier */
629{ "optional",  0,      0,      0,      9,      0, "" }, /* cptr */
630{ "option",    0,      0,      0,      7,      0, "" }, /* smallcptr */
631{ "orderedlist",0,     0,      0,      9,      0, "" }, /* formalobject.title.content */
632{ "orgdiv",    0,      0,      0,      4,      0, "" }, /* docinfo */
633{ "orgname",   0,      0,      0,      4,      0, "" }, /* docinfo */
634{ "otheraddr", 0,      0,      0,      4,      0, "" }, /* docinfo */
635{ "othercredit",0,     0,      0,      9,      0, "" }, /* person.ident.mix */
636{ "othername", 0,      0,      0,      4,      0, "" }, /* docinfo */
637{ "pagenums",  0,      0,      0,      4,      0, "" }, /* docinfo */
638{ "paramdef",  0,      0,      0,      1,      0, "" },
639{ "parameter", 0,      0,      0,      7,      0, "" }, /* smallcptr */
640{ "para",      0,      0,      0,      2,      0, "" }, /* para */
641{ "partinfo",  0,      0,      0,      9,      0, "" }, /* graphic */
642{ "partintro", 0,      0,      0,      9,      0, "" }, /* div.title.content */
643{ "part",      0,      0,      0,      9,      0, "" }, /* partinfo */
644{ "phone",     0,      0,      0,      4,      0, "" }, /* docinfo */
645{ "phrase",    0,      0,      0,      2,      0, "" }, /* para */
646{ "pob",       0,      0,      0,      4,      0, "" }, /* docinfo */
647{ "postcode",  0,      0,      0,      4,      0, "" }, /* docinfo */
648{ "prefaceinfo",0,     0,      0,      9,      0, "" }, /* graphic */
649{ "preface",   0,      0,      0,      9,      0, "" }, /* prefaceinfo */
650{ "primaryie", 0,      0,      0,      4,      0, "" }, /* ndxterm */
651{ "primary  ", 0,      0,      0,      4,      0, "" }, /* ndxterm */
652{ "printhistory",0,    0,      0,      9,      0, "" }, /* para.class */
653{ "procedure", 0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
654{ "productname",0,     0,      0,      2,      0, "" }, /* para */
655{ "productnumber",0,   0,      0,      4,      0, "" }, /* docinfo */
656{ "programlistingco",0,        0,      0,      9,      0, "" }, /* areaspec */
657{ "programlisting",0,  0,      0,      2,      0, "" }, /* para */
658{ "prompt",    0,      0,      0,      7,      0, "" }, /* smallcptr */
659{ "property",  0,      0,      0,      7,      0, "" }, /* smallcptr */
660{ "pubdate",   0,      0,      0,      4,      0, "" }, /* docinfo */
661{ "publishername",0,   0,      0,      4,      0, "" }, /* docinfo */
662{ "publisher", 0,      0,      0,      9,      0, "" }, /* publishername */
663{ "pubsnumber",        0,      0,      0,      4,      0, "" }, /* docinfo */
664{ "qandadiv",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
665{ "qandaentry",        0,      0,      0,      9,      0, "" }, /* revhistory */
666{ "qandaset",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
667{ "question",  0,      0,      0,      9,      0, "" }, /* label */
668{ "quote",     0,      0,      0,      2,      0, "" }, /* para */
669{ "refclass",  0,      0,      0,      9,      0, "" }, /* refclass.char.mix */
670{ "refdescriptor",0,   0,      0,      9,      0, "" }, /* refname.char.mix */
671{ "refentryinfo",0,    0,      0,      9,      0, "" }, /* graphic */
672{ "refentry",  0,      0,      0,      9,      0, "" }, /* ndxterm.class */
673{ "refentrytitle",0,   0,      0,      2,      0, "" }, /* para */
674{ "referenceinfo",0,   0,      0,      9,      0, "" }, /* graphic */
675{ "reference", 0,      0,      0,      9,      0, "" }, /* referenceinfo */
676{ "refmeta",   0,      0,      0,      9,      0, "" }, /* ndxterm.class */
677{ "refmiscinfo",0,     0,      0,      4,      0, "" }, /* docinfo */
678{ "refnamediv",        0,      0,      0,      9,      0, "" }, /* refdescriptor */
679{ "refname",   0,      0,      0,      9,      0, "" }, /* refname.char.mix */
680{ "refpurpose",        0,      0,      0,      9,      0, "" }, /* refinline.char.mix */
681{ "refsect1info",0,    0,      0,      9,      0, "" }, /* graphic */
682{ "refsect1",  0,      0,      0,      9,      0, "" }, /* refsect */
683{ "refsect2info",0,    0,      0,      9,      0, "" }, /* graphic */
684{ "refsect2",  0,      0,      0,      9,      0, "" }, /* refsect */
685{ "refsect3info",0,    0,      0,      9,      0, "" }, /* graphic */
686{ "refsect3",  0,      0,      0,      9,      0, "" }, /* refsect */
687{ "refsynopsisdivinfo",0,0,    0,      9,      0, "" }, /* graphic */
688{ "refsynopsisdiv",0,  0,      0,      9,      0, "" }, /* refsynopsisdivinfo */
689{ "releaseinfo",0,     0,      0,      4,      0, "" }, /* docinfo */
690{ "remark",    0,      0,      0,      2,      0, "" }, /* para */
691{ "replaceable",0,     0,      0,      1,      0, "" },
692{ "returnvalue",0,     0,      0,      7,      0, "" }, /* smallcptr */
693{ "revdescription",0,  0,      0,      9,      0, "" }, /* revdescription.mix */
694{ "revhistory",        0,      0,      0,      9,      0, "" }, /* revision */
695{ "revision",  0,      0,      0,      9,      0, "" }, /* revnumber */
696{ "revnumber", 0,      0,      0,      4,      0, "" }, /* docinfo */
697{ "revremark", 0,      0,      0,      4,      0, "" }, /* docinfo */
698{ "row",       0,      0,      0,      9,      0, "" }, /* tbl.row.mdl */
699{ "row",       0,      0,      0,      9,      0, "" }, /* tbl.row.mdl */
700{ "sbr",       0,      2,      1,      0,      0, "" },
701{ "screenco",  0,      0,      0,      9,      0, "" }, /* areaspec */
702{ "screeninfo",        0,      0,      0,      2,      0, "" }, /* para */
703{ "screen",    0,      0,      0,      2,      0, "" }, /* para */
704{ "screenshot",        0,      0,      0,      9,      0, "" }, /* screeninfo */
705{ "secondaryie",0,     0,      0,      4,      0, "" }, /* ndxterm */
706{ "secondary", 0,      0,      0,      4,      0, "" }, /* ndxterm */
707{ "sect1info", 0,      0,      0,      9,      0, "" }, /* graphic */
708{ "sect1",     0,      0,      0,      9,      0, "" }, /* sect */
709{ "sect2info", 0,      0,      0,      9,      0, "" }, /* graphic */
710{ "sect2",     0,      0,      0,      9,      0, "" }, /* sect */
711{ "sect3info", 0,      0,      0,      9,      0, "" }, /* graphic */
712{ "sect3",     0,      0,      0,      9,      0, "" }, /* sect */
713{ "sect4info", 0,      0,      0,      9,      0, "" }, /* graphic */
714{ "sect4",     0,      0,      0,      9,      0, "" }, /* sect */
715{ "sect5info", 0,      0,      0,      9,      0, "" }, /* graphic */
716{ "sect5",     0,      0,      0,      9,      0, "" }, /* sect */
717{ "sectioninfo",0,     0,      0,      9,      0, "" }, /* graphic */
718{ "section",   0,      0,      0,      9,      0, "" }, /* sectioninfo */
719{ "seealsoie", 0,      0,      0,      4,      0, "" }, /* ndxterm */
720{ "seealso",   0,      0,      0,      4,      0, "" }, /* ndxterm */
721{ "seeie",     0,      0,      0,      4,      0, "" }, /* ndxterm */
722{ "see",       0,      0,      0,      4,      0, "" }, /* ndxterm */
723{ "seglistitem",0,     0,      0,      9,      0, "" }, /* seg */
724{ "segmentedlist",0,   0,      0,      9,      0, "" }, /* formalobject.title.content */
725{ "seg",       0,      0,      0,      2,      0, "" }, /* para */
726{ "segtitle",  0,      0,      0,      8,      0, "" }, /* title */
727{ "seriesvolnums",     0,      0,      0,      4,      0, "" }, /* docinfo */
728{ "set",       0,      0,      0,      9,      0, "" }, /* div.title.content */
729{ "setindexinfo",0,    0,      0,      9,      0, "" }, /* graphic */
730{ "setindex",  0,      0,      0,      9,      0, "" }, /* setindexinfo */
731{ "setinfo",   0,      0,      0,      9,      0, "" }, /* graphic */
732{ "sgmltag",   0,      0,      0,      7,      0, "" }, /* smallcptr */
733{ "shortaffil",        0,      0,      0,      4,      0, "" }, /* docinfo */
734{ "shortcut",  0,      0,      0,      9,      0, "" }, /* keycap */
735{ "sidebarinfo",0,     0,      0,      9,      0, "" }, /* graphic */
736{ "sidebar",   0,      0,      0,      9,      0, "" }, /* sidebarinfo */
737{ "simpara",   0,      0,      0,      2,      0, "" }, /* para */
738{ "simplelist",        0,      0,      0,      9,      0, "" }, /* member */
739{ "simplemsgentry",    0,      0,      0,      9,      0, "" }, /* msgtext */
740{ "simplesect",        0,      0,      0,      9,      0, "" }, /* sect.title.content */
741{ "spanspec",  0,      2,      1,      0,      0, "" },
742{ "state",     0,      0,      0,      4,      0, "" }, /* docinfo */
743{ "step",      0,      0,      0,      9,      0, "" }, /* title */
744{ "street",    0,      0,      0,      4,      0, "" }, /* docinfo */
745{ "structfield",0,     0,      0,      7,      0, "" }, /* smallcptr */
746{ "structname",        0,      0,      0,      7,      0, "" }, /* smallcptr */
747{ "subjectset",        0,      0,      0,      9,      0, "" }, /* subject */
748{ "subject",   0,      0,      0,      9,      0, "" }, /* subjectterm */
749{ "subjectterm",0,     0,      0,      1,      0, "" },
750{ "subscript", 0,      0,      0,      1,      0, "" },
751{ "substeps",  0,      0,      0,      9,      0, "" }, /* step */
752{ "subtitle",  0,      0,      0,      8,      0, "" }, /* title */
753{ "superscript",       0,      0,      0,      1,      0, "" },
754{ "surname",   0,      0,      0,      4,      0, "" }, /* docinfo */
755{ "symbol",    0,      0,      0,      7,      0, "" }, /* smallcptr */
756{ "synopfragment",     0,      0,      0,      9,      0, "" }, /* arg */
757{ "synopfragmentref",  0,      0,      0,      1,      0, "" },
758{ "synopsis",  0,      0,      0,      2,      0, "" }, /* para */
759{ "systemitem",        0,      0,      0,      7,      0, "" }, /* smallcptr */
760{ "table",     0,      0,      0,      9,      0, "" }, /* tbl.table.mdl */
761/* { "%tbl.table.name;",       0,      0,      0,      9,      0, "" },*/ /* tbl.table.mdl */
762{ "tbody",     0,      0,      0,      9,      0, "" }, /* row */
763{ "tbody",     0,      0,      0,      9,      0, "" }, /* row */
764{ "term",      0,      0,      0,      2,      0, "" }, /* para */
765{ "tertiaryie",        0,      0,      0,      4,      0, "" }, /* ndxterm */
766{ "tertiary ", 0,      0,      0,      4,      0, "" }, /* ndxterm */
767{ "textobject",        0,      0,      0,      9,      0, "" }, /* objectinfo */
768{ "tfoot",     0,      0,      0,      9,      0, "" }, /* tbl.hdft.mdl */
769{ "tgroup",    0,      0,      0,      9,      0, "" }, /* tbl.tgroup.mdl */
770{ "tgroup",    0,      0,      0,      9,      0, "" }, /* tbl.tgroup.mdl */
771{ "thead",     0,      0,      0,      9,      0, "" }, /* row */
772{ "thead",     0,      0,      0,      9,      0, "" }, /* tbl.hdft.mdl */
773{ "tip",       0,      0,      0,      9,      0, "" }, /* title */
774{ "titleabbrev",0,     0,      0,      8,      0, "" }, /* title */
775{ "title",     0,      0,      0,      8,      0, "" }, /* title */
776{ "tocback",   0,      0,      0,      2,      0, "" }, /* para */
777{ "toc",       0,      0,      0,      9,      0, "" }, /* bookcomponent.title.content */
778{ "tocchap",   0,      0,      0,      9,      0, "" }, /* tocentry */
779{ "tocentry",  0,      0,      0,      2,      0, "" }, /* para */
780{ "tocfront",  0,      0,      0,      2,      0, "" }, /* para */
781{ "toclevel1", 0,      0,      0,      9,      0, "" }, /* tocentry */
782{ "toclevel2", 0,      0,      0,      9,      0, "" }, /* tocentry */
783{ "toclevel3", 0,      0,      0,      9,      0, "" }, /* tocentry */
784{ "toclevel4", 0,      0,      0,      9,      0, "" }, /* tocentry */
785{ "toclevel5", 0,      0,      0,      9,      0, "" }, /* tocentry */
786{ "tocpart",   0,      0,      0,      9,      0, "" }, /* tocentry */
787{ "token",     0,      0,      0,      7,      0, "" }, /* smallcptr */
788{ "trademark", 0,      0,      0,      1,      0, "" },
789{ "type",      0,      0,      0,      7,      0, "" }, /* smallcptr */
790{ "ulink",     0,      0,      0,      2,      0, "" }, /* para */
791{ "userinput", 0,      0,      0,      9,      0, "" }, /* cptr */
792{ "varargs",   0,      2,      1,      0,      0, "" },
793{ "variablelist",0,    0,      0,      9,      0, "" }, /* formalobject.title.content */
794{ "varlistentry",0,    0,      0,      9,      0, "" }, /* term */
795{ "varname",   0,      0,      0,      7,      0, "" }, /* smallcptr */
796{ "videodata", 0,      2,      1,      0,      0, "" },
797{ "videoobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
798{ "void",      0,      2,      1,      0,      0, "" },
799{ "volumenum", 0,      0,      0,      4,      0, "" }, /* docinfo */
800{ "warning",   0,      0,      0,      9,      0, "" }, /* title */
801{ "wordasword",        0,      0,      0,      3,      0, "" }, /* word */
802{ "xref",      0,      2,      1,      0,      0, "" },
803{ "year",      0,      0,      0,      4,      0, "" }, /* docinfo */
804};
805
806#if 0
807/*
808 * start tags that imply the end of a current element
809 * any tag of each line implies the end of the current element if the type of
810 * that element is in the same line
811 */
812static const char *docbEquEnd[] = {
813"dt", "dd", "li", "option", NULL,
814"h1", "h2", "h3", "h4", "h5", "h6", NULL,
815"ol", "menu", "dir", "address", "pre", "listing", "xmp", NULL,
816NULL
817};
818#endif
819
820/*
821 * acording the SGML DTD, HR should be added to the 2nd line above, as it
822 * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
823 * because many documents contain rules in headings...
824 */
825
826/*
827 * start tags that imply the end of current element
828 */
829static const char *docbStartClose[] = {
830NULL
831};
832
833/*
834 * The list of SGML elements which are supposed not to have
835 * CDATA content and where a p element will be implied
836 *
837 * TODO: extend that list by reading the SGML SGML DtD on
838 *       implied paragraph
839 */
840static char *docbNoContentElements[] = {
841    NULL
842};
843
844
845static const char** docbStartCloseIndex[100];
846static int docbStartCloseIndexinitialized = 0;
847
848/************************************************************************
849 *                                                                     *
850 *             functions to handle SGML specific data                  *
851 *                                                                     *
852 ************************************************************************/
853
854/**
855 * docbInitAutoClose:
856 *
857 * Initialize the docbStartCloseIndex for fast lookup of closing tags names.
858 *
859 */
860static void
861docbInitAutoClose(void) {
862    int indx, i = 0;
863
864    if (docbStartCloseIndexinitialized) return;
865
866    for (indx = 0;indx < 100;indx ++) docbStartCloseIndex[indx] = NULL;
867    indx = 0;
868    while ((docbStartClose[i] != NULL) && (indx < 100 - 1)) {
869        docbStartCloseIndex[indx++] = &docbStartClose[i];
870       while (docbStartClose[i] != NULL) i++;
871       i++;
872    }
873}
874
875/**
876 * docbTagLookup:
877 * @tag:  The tag name
878 *
879 * Lookup the SGML tag in the ElementTable
880 *
881 * Returns the related docbElemDescPtr or NULL if not found.
882 */
883static docbElemDescPtr
884docbTagLookup(const xmlChar *tag) {
885    unsigned int i;
886
887    for (i = 0; i < (sizeof(docbookElementTable) /
888                     sizeof(docbookElementTable[0]));i++) {
889        if (xmlStrEqual(tag, BAD_CAST docbookElementTable[i].name))
890           return(&docbookElementTable[i]);
891    }
892    return(NULL);
893}
894
895/**
896 * docbCheckAutoClose:
897 * @newtag:  The new tag name
898 * @oldtag:  The old tag name
899 *
900 * Checks wether the new tag is one of the registered valid tags for closing old.
901 * Initialize the docbStartCloseIndex for fast lookup of closing tags names.
902 *
903 * Returns 0 if no, 1 if yes.
904 */
905static int
906docbCheckAutoClose(const xmlChar *newtag, const xmlChar *oldtag) {
907    int i, indx;
908    const char **closed = NULL;
909
910    if (docbStartCloseIndexinitialized == 0) docbInitAutoClose();
911
912    /* inefficient, but not a big deal */
913    for (indx = 0; indx < 100;indx++) {
914        closed = docbStartCloseIndex[indx];
915       if (closed == NULL) return(0);
916       if (xmlStrEqual(BAD_CAST *closed, newtag)) break;
917    }
918
919    i = closed - docbStartClose;
920    i++;
921    while (docbStartClose[i] != NULL) {
922        if (xmlStrEqual(BAD_CAST docbStartClose[i], oldtag)) {
923           return(1);
924       }
925       i++;
926    }
927    return(0);
928}
929
930/**
931 * docbAutoCloseOnClose:
932 * @ctxt:  an SGML parser context
933 * @newtag:  The new tag name
934 *
935 * The HTmL DtD allows an ending tag to implicitely close other tags.
936 */
937static void
938docbAutoCloseOnClose(docbParserCtxtPtr ctxt, const xmlChar *newtag) {
939    docbElemDescPtr info;
940    xmlChar *oldname;
941    int i;
942
943    if ((newtag[0] == '/') && (newtag[1] == 0))
944       return;
945
946#ifdef DEBUG
947    xmlGenericError(xmlGenericErrorContext,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
948    for (i = 0;i < ctxt->nameNr;i++)
949        xmlGenericError(xmlGenericErrorContext,"%d : %s\n", i, ctxt->nameTab[i]);
950#endif
951
952    for (i = (ctxt->nameNr - 1);i >= 0;i--) {
953        if (xmlStrEqual(newtag, ctxt->nameTab[i])) break;
954    }
955    if (i < 0) return;
956
957    while (!xmlStrEqual(newtag, ctxt->name)) {
958       info = docbTagLookup(ctxt->name);
959       if ((info == NULL) || (info->endTag == 1)) {
960#ifdef DEBUG
961           xmlGenericError(xmlGenericErrorContext,"docbAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
962#endif
963        } else {
964           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
965               ctxt->sax->error(ctxt->userData,
966                "Opening and ending tag mismatch: %s and %s\n",
967                                newtag, ctxt->name);
968           ctxt->wellFormed = 0;
969       }
970       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
971           ctxt->sax->endElement(ctxt->userData, ctxt->name);
972       oldname = docbnamePop(ctxt);
973       if (oldname != NULL) {
974#ifdef DEBUG
975           xmlGenericError(xmlGenericErrorContext,"docbAutoCloseOnClose: popped %s\n", oldname);
976#endif
977           xmlFree(oldname);
978       }
979    }
980}
981
982/**
983 * docbAutoClose:
984 * @ctxt:  an SGML parser context
985 * @newtag:  The new tag name or NULL
986 *
987 * The HTmL DtD allows a tag to implicitely close other tags.
988 * The list is kept in docbStartClose array. This function is
989 * called when a new tag has been detected and generates the
990 * appropriates closes if possible/needed.
991 * If newtag is NULL this mean we are at the end of the resource
992 * and we should check
993 */
994static void
995docbAutoClose(docbParserCtxtPtr ctxt, const xmlChar *newtag) {
996    xmlChar *oldname;
997    while ((newtag != NULL) && (ctxt->name != NULL) &&
998           (docbCheckAutoClose(newtag, ctxt->name))) {
999#ifdef DEBUG
1000       xmlGenericError(xmlGenericErrorContext,"docbAutoClose: %s closes %s\n", newtag, ctxt->name);
1001#endif
1002       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1003           ctxt->sax->endElement(ctxt->userData, ctxt->name);
1004       oldname = docbnamePop(ctxt);
1005       if (oldname != NULL) {
1006#ifdef DEBUG
1007           xmlGenericError(xmlGenericErrorContext,"docbAutoClose: popped %s\n", oldname);
1008#endif
1009           xmlFree(oldname);
1010        }
1011    }
1012}
1013
1014/**
1015 * docbAutoCloseTag:
1016 * @doc:  the SGML document
1017 * @name:  The tag name
1018 * @elem:  the SGML element
1019 *
1020 * The HTmL DtD allows a tag to implicitely close other tags.
1021 * The list is kept in docbStartClose array. This function checks
1022 * if the element or one of it's children would autoclose the
1023 * given tag.
1024 *
1025 * Returns 1 if autoclose, 0 otherwise
1026 */
1027static int
1028docbAutoCloseTag(docbDocPtr doc, const xmlChar *name, docbNodePtr elem) {
1029    docbNodePtr child;
1030
1031    if (elem == NULL) return(1);
1032    if (xmlStrEqual(name, elem->name)) return(0);
1033    if (docbCheckAutoClose(elem->name, name)) return(1);
1034    child = elem->children;
1035    while (child != NULL) {
1036        if (docbAutoCloseTag(doc, name, child)) return(1);
1037       child = child->next;
1038    }
1039    return(0);
1040}
1041
1042#if 0
1043/**
1044 * docbIsAutoClosed:
1045 * @doc:  the SGML document
1046 * @elem:  the SGML element
1047 *
1048 * The list is kept in docbStartClose array. This function checks
1049 * if a tag is autoclosed by one of it's child
1050 *
1051 * Returns 1 if autoclosed, 0 otherwise
1052 */
1053static int
1054docbIsAutoClosed(docbDocPtr doc, docbNodePtr elem) {
1055    docbNodePtr child;
1056
1057    if (elem == NULL) return(1);
1058    child = elem->children;
1059    while (child != NULL) {
1060       if (docbAutoCloseTag(doc, elem->name, child)) return(1);
1061       child = child->next;
1062    }
1063    return(0);
1064}
1065#endif
1066
1067/**
1068 * docbCheckParagraph
1069 * @ctxt:  an SGML parser context
1070 *
1071 * Check whether a p element need to be implied before inserting
1072 * characters in the current element.
1073 *
1074 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1075 *         in case of error.
1076 */
1077
1078static int
1079docbCheckParagraph(docbParserCtxtPtr ctxt) {
1080    const xmlChar *tag;
1081    int i;
1082
1083    if (ctxt == NULL)
1084       return(-1);
1085    tag = ctxt->name;
1086    if (tag == NULL) {
1087       docbAutoClose(ctxt, BAD_CAST"p");
1088       docbnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
1089       if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1090           ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1091       return(1);
1092    }
1093    for (i = 0; docbNoContentElements[i] != NULL; i++) {
1094       if (xmlStrEqual(tag, BAD_CAST docbNoContentElements[i])) {
1095#ifdef DEBUG
1096           xmlGenericError(xmlGenericErrorContext,"Implied element paragraph\n");
1097#endif
1098           docbAutoClose(ctxt, BAD_CAST"p");
1099           docbnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
1100           if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1101               ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1102           return(1);
1103       }
1104    }
1105    return(0);
1106}
1107
1108/************************************************************************
1109 *                                                                     *
1110 *             The list of SGML predefined entities                    *
1111 *                                                                     *
1112 ************************************************************************/
1113
1114
1115static docbEntityDesc
1116docbookEntitiesTable[] = {
1117/*
1118 * the 4 absolute ones, plus apostrophe.
1119 */
1120{ 0x0026, "amp", "AMPERSAND" },
1121{ 0x003C, "lt",        "LESS-THAN SIGN" },
1122
1123/*
1124 * Converted with VI macros from docbook ent files
1125 */
1126{ 0x0021, "excl", "EXCLAMATION MARK" },
1127{ 0x0022, "quot", "QUOTATION MARK" },
1128{ 0x0023, "num", "NUMBER SIGN" },
1129{ 0x0024, "dollar", "DOLLAR SIGN" },
1130{ 0x0025, "percnt", "PERCENT SIGN" },
1131{ 0x0027, "apos", "APOSTROPHE" },
1132{ 0x0028, "lpar", "LEFT PARENTHESIS" },
1133{ 0x0029, "rpar", "RIGHT PARENTHESIS" },
1134{ 0x002A, "ast", "ASTERISK OPERATOR" },
1135{ 0x002B, "plus", "PLUS SIGN" },
1136{ 0x002C, "comma", "COMMA" },
1137{ 0x002D, "hyphen", "HYPHEN-MINUS" },
1138{ 0x002E, "period", "FULL STOP" },
1139{ 0x002F, "sol", "SOLIDUS" },
1140{ 0x003A, "colon", "COLON" },
1141{ 0x003B, "semi", "SEMICOLON" },
1142{ 0x003D, "equals", "EQUALS SIGN" },
1143{ 0x003E, "gt", "GREATER-THAN SIGN" },
1144{ 0x003F, "quest", "QUESTION MARK" },
1145{ 0x0040, "commat", "COMMERCIAL AT" },
1146{ 0x005B, "lsqb", "LEFT SQUARE BRACKET" },
1147{ 0x005C, "bsol", "REVERSE SOLIDUS" },
1148{ 0x005D, "rsqb", "RIGHT SQUARE BRACKET" },
1149{ 0x005E, "circ", "RING OPERATOR" },
1150{ 0x005F, "lowbar", "LOW LINE" },
1151{ 0x0060, "grave", "GRAVE ACCENT" },
1152{ 0x007B, "lcub", "LEFT CURLY BRACKET" },
1153{ 0x007C, "verbar", "VERTICAL LINE" },
1154{ 0x007D, "rcub", "RIGHT CURLY BRACKET" },
1155{ 0x00A0, "nbsp", "NO-BREAK SPACE" },
1156{ 0x00A1, "iexcl", "INVERTED EXCLAMATION MARK" },
1157{ 0x00A2, "cent", "CENT SIGN" },
1158{ 0x00A3, "pound", "POUND SIGN" },
1159{ 0x00A4, "curren", "CURRENCY SIGN" },
1160{ 0x00A5, "yen", "YEN SIGN" },
1161{ 0x00A6, "brvbar", "BROKEN BAR" },
1162{ 0x00A7, "sect", "SECTION SIGN" },
1163{ 0x00A8, "die", "" },
1164{ 0x00A8, "Dot", "" },
1165{ 0x00A8, "uml", "" },
1166{ 0x00A9, "copy", "COPYRIGHT SIGN" },
1167{ 0x00AA, "ordf", "FEMININE ORDINAL INDICATOR" },
1168{ 0x00AB, "laquo", "LEFT-POINTING DOUBLE ANGLE QUOTATION MARK" },
1169{ 0x00AC, "not", "NOT SIGN" },
1170{ 0x00AD, "shy", "SOFT HYPHEN" },
1171{ 0x00AE, "reg", "REG TRADE MARK SIGN" },
1172{ 0x00AF, "macr", "MACRON" },
1173{ 0x00B0, "deg", "DEGREE SIGN" },
1174{ 0x00B1, "plusmn", "PLUS-MINUS SIGN" },
1175{ 0x00B2, "sup2", "SUPERSCRIPT TWO" },
1176{ 0x00B3, "sup3", "SUPERSCRIPT THREE" },
1177{ 0x00B4, "acute", "ACUTE ACCENT" },
1178{ 0x00B5, "micro", "MICRO SIGN" },
1179{ 0x00B6, "para", "PILCROW SIGN" },
1180{ 0x00B7, "middot", "MIDDLE DOT" },
1181{ 0x00B8, "cedil", "CEDILLA" },
1182{ 0x00B9, "sup1", "SUPERSCRIPT ONE" },
1183{ 0x00BA, "ordm", "MASCULINE ORDINAL INDICATOR" },
1184{ 0x00BB, "raquo", "RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK" },
1185{ 0x00BC, "frac14", "VULGAR FRACTION ONE QUARTER" },
1186{ 0x00BD, "frac12", "VULGAR FRACTION ONE HALF" },
1187{ 0x00BD, "half", "VULGAR FRACTION ONE HALF" },
1188{ 0x00BE, "frac34", "VULGAR FRACTION THREE QUARTERS" },
1189{ 0x00BF, "iquest", "INVERTED QUESTION MARK" },
1190{ 0x00C0, "Agrave", "LATIN CAPITAL LETTER A WITH GRAVE" },
1191{ 0x00C1, "Aacute", "LATIN CAPITAL LETTER A WITH ACUTE" },
1192{ 0x00C2, "Acirc", "LATIN CAPITAL LETTER A WITH CIRCUMFLEX" },
1193{ 0x00C3, "Atilde", "LATIN CAPITAL LETTER A WITH TILDE" },
1194{ 0x00C4, "Auml", "LATIN CAPITAL LETTER A WITH DIAERESIS" },
1195{ 0x00C5, "Aring", "LATIN CAPITAL LETTER A WITH RING ABOVE" },
1196{ 0x00C6, "AElig", "LATIN CAPITAL LETTER AE" },
1197{ 0x00C7, "Ccedil", "LATIN CAPITAL LETTER C WITH CEDILLA" },
1198{ 0x00C8, "Egrave", "LATIN CAPITAL LETTER E WITH GRAVE" },
1199{ 0x00C9, "Eacute", "LATIN CAPITAL LETTER E WITH ACUTE" },
1200{ 0x00CA, "Ecirc", "LATIN CAPITAL LETTER E WITH CIRCUMFLEX" },
1201{ 0x00CB, "Euml", "LATIN CAPITAL LETTER E WITH DIAERESIS" },
1202{ 0x00CC, "Igrave", "LATIN CAPITAL LETTER I WITH GRAVE" },
1203{ 0x00CD, "Iacute", "LATIN CAPITAL LETTER I WITH ACUTE" },
1204{ 0x00CE, "Icirc", "LATIN CAPITAL LETTER I WITH CIRCUMFLEX" },
1205{ 0x00CF, "Iuml", "LATIN CAPITAL LETTER I WITH DIAERESIS" },
1206{ 0x00D0, "ETH", "LATIN CAPITAL LETTER ETH" },
1207{ 0x00D1, "Ntilde", "LATIN CAPITAL LETTER N WITH TILDE" },
1208{ 0x00D2, "Ograve", "LATIN CAPITAL LETTER O WITH GRAVE" },
1209{ 0x00D3, "Oacute", "LATIN CAPITAL LETTER O WITH ACUTE" },
1210{ 0x00D4, "Ocirc", "LATIN CAPITAL LETTER O WITH CIRCUMFLEX" },
1211{ 0x00D5, "Otilde", "LATIN CAPITAL LETTER O WITH TILDE" },
1212{ 0x00D6, "Ouml", "LATIN CAPITAL LETTER O WITH DIAERESIS" },
1213{ 0x00D7, "times", "MULTIPLICATION SIGN" },
1214{ 0x00D8, "Oslash", "LATIN CAPITAL LETTER O WITH STROKE" },
1215{ 0x00D9, "Ugrave", "LATIN CAPITAL LETTER U WITH GRAVE" },
1216{ 0x00DA, "Uacute", "LATIN CAPITAL LETTER U WITH ACUTE" },
1217{ 0x00DB, "Ucirc", "LATIN CAPITAL LETTER U WITH CIRCUMFLEX" },
1218{ 0x00DC, "Uuml", "LATIN CAPITAL LETTER U WITH DIAERESIS" },
1219{ 0x00DD, "Yacute", "LATIN CAPITAL LETTER Y WITH ACUTE" },
1220{ 0x00DE, "THORN", "LATIN CAPITAL LETTER THORN" },
1221{ 0x00DF, "szlig", "LATIN SMALL LETTER SHARP S" },
1222{ 0x00E0, "agrave", "LATIN SMALL LETTER A WITH GRAVE" },
1223{ 0x00E1, "aacute", "LATIN SMALL LETTER A WITH ACUTE" },
1224{ 0x00E2, "acirc", "LATIN SMALL LETTER A WITH CIRCUMFLEX" },
1225{ 0x00E3, "atilde", "LATIN SMALL LETTER A WITH TILDE" },
1226{ 0x00E4, "auml", "LATIN SMALL LETTER A WITH DIAERESIS" },
1227{ 0x00E5, "aring", "LATIN SMALL LETTER A WITH RING ABOVE" },
1228{ 0x00E6, "aelig", "LATIN SMALL LETTER AE" },
1229{ 0x00E7, "ccedil", "LATIN SMALL LETTER C WITH CEDILLA" },
1230{ 0x00E8, "egrave", "LATIN SMALL LETTER E WITH GRAVE" },
1231{ 0x00E9, "eacute", "LATIN SMALL LETTER E WITH ACUTE" },
1232{ 0x00EA, "ecirc", "LATIN SMALL LETTER E WITH CIRCUMFLEX" },
1233{ 0x00EB, "euml", "LATIN SMALL LETTER E WITH DIAERESIS" },
1234{ 0x00EC, "igrave", "LATIN SMALL LETTER I WITH GRAVE" },
1235{ 0x00ED, "iacute", "LATIN SMALL LETTER I WITH ACUTE" },
1236{ 0x00EE, "icirc", "LATIN SMALL LETTER I WITH CIRCUMFLEX" },
1237{ 0x00EF, "iuml", "LATIN SMALL LETTER I WITH DIAERESIS" },
1238{ 0x00F0, "eth", "LATIN SMALL LETTER ETH" },
1239{ 0x00F1, "ntilde", "LATIN SMALL LETTER N WITH TILDE" },
1240{ 0x00F2, "ograve", "LATIN SMALL LETTER O WITH GRAVE" },
1241{ 0x00F3, "oacute", "LATIN SMALL LETTER O WITH ACUTE" },
1242{ 0x00F4, "ocirc", "LATIN SMALL LETTER O WITH CIRCUMFLEX" },
1243{ 0x00F5, "otilde", "LATIN SMALL LETTER O WITH TILDE" },
1244{ 0x00F6, "ouml", "LATIN SMALL LETTER O WITH DIAERESIS" },
1245{ 0x00F7, "divide", "DIVISION SIGN" },
1246{ 0x00F8, "oslash", "CIRCLED DIVISION SLASH" },
1247{ 0x00F9, "ugrave", "LATIN SMALL LETTER U WITH GRAVE" },
1248{ 0x00FA, "uacute", "LATIN SMALL LETTER U WITH ACUTE" },
1249{ 0x00FB, "ucirc", "LATIN SMALL LETTER U WITH CIRCUMFLEX" },
1250{ 0x00FC, "uuml", "LATIN SMALL LETTER U WITH DIAERESIS" },
1251{ 0x00FD, "yacute", "LATIN SMALL LETTER Y WITH ACUTE" },
1252{ 0x00FE, "thorn", "LATIN SMALL LETTER THORN" },
1253{ 0x00FF, "yuml", "LATIN SMALL LETTER Y WITH DIAERESIS" },
1254{ 0x0100, "Amacr", "LATIN CAPITAL LETTER A WITH MACRON" },
1255{ 0x0101, "amacr", "LATIN SMALL LETTER A WITH MACRON" },
1256{ 0x0102, "Abreve", "LATIN CAPITAL LETTER A WITH BREVE" },
1257{ 0x0103, "abreve", "LATIN SMALL LETTER A WITH BREVE" },
1258{ 0x0104, "Aogon", "LATIN CAPITAL LETTER A WITH OGONEK" },
1259{ 0x0105, "aogon", "LATIN SMALL LETTER A WITH OGONEK" },
1260{ 0x0106, "Cacute", "LATIN CAPITAL LETTER C WITH ACUTE" },
1261{ 0x0107, "cacute", "LATIN SMALL LETTER C WITH ACUTE" },
1262{ 0x0108, "Ccirc", "LATIN CAPITAL LETTER C WITH CIRCUMFLEX" },
1263{ 0x0109, "ccirc", "LATIN SMALL LETTER C WITH CIRCUMFLEX" },
1264{ 0x010A, "Cdot", "LATIN CAPITAL LETTER C WITH DOT ABOVE" },
1265{ 0x010B, "cdot", "DOT OPERATOR" },
1266{ 0x010C, "Ccaron", "LATIN CAPITAL LETTER C WITH CARON" },
1267{ 0x010D, "ccaron", "LATIN SMALL LETTER C WITH CARON" },
1268{ 0x010E, "Dcaron", "LATIN CAPITAL LETTER D WITH CARON" },
1269{ 0x010F, "dcaron", "LATIN SMALL LETTER D WITH CARON" },
1270{ 0x0110, "Dstrok", "LATIN CAPITAL LETTER D WITH STROKE" },
1271{ 0x0111, "dstrok", "LATIN SMALL LETTER D WITH STROKE" },
1272{ 0x0112, "Emacr", "LATIN CAPITAL LETTER E WITH MACRON" },
1273{ 0x0113, "emacr", "LATIN SMALL LETTER E WITH MACRON" },
1274{ 0x0116, "Edot", "LATIN CAPITAL LETTER E WITH DOT ABOVE" },
1275{ 0x0117, "edot", "LATIN SMALL LETTER E WITH DOT ABOVE" },
1276{ 0x0118, "Eogon", "LATIN CAPITAL LETTER E WITH OGONEK" },
1277{ 0x0119, "eogon", "LATIN SMALL LETTER E WITH OGONEK" },
1278{ 0x011A, "Ecaron", "LATIN CAPITAL LETTER E WITH CARON" },
1279{ 0x011B, "ecaron", "LATIN SMALL LETTER E WITH CARON" },
1280{ 0x011C, "Gcirc", "LATIN CAPITAL LETTER G WITH CIRCUMFLEX" },
1281{ 0x011D, "gcirc", "LATIN SMALL LETTER G WITH CIRCUMFLEX" },
1282{ 0x011E, "Gbreve", "LATIN CAPITAL LETTER G WITH BREVE" },
1283{ 0x011F, "gbreve", "LATIN SMALL LETTER G WITH BREVE" },
1284{ 0x0120, "Gdot", "LATIN CAPITAL LETTER G WITH DOT ABOVE" },
1285{ 0x0121, "gdot", "LATIN SMALL LETTER G WITH DOT ABOVE" },
1286{ 0x0122, "Gcedil", "LATIN CAPITAL LETTER G WITH CEDILLA" },
1287{ 0x0124, "Hcirc", "LATIN CAPITAL LETTER H WITH CIRCUMFLEX" },
1288{ 0x0125, "hcirc", "LATIN SMALL LETTER H WITH CIRCUMFLEX" },
1289{ 0x0126, "Hstrok", "LATIN CAPITAL LETTER H WITH STROKE" },
1290{ 0x0127, "hstrok", "LATIN SMALL LETTER H WITH STROKE" },
1291{ 0x0128, "Itilde", "LATIN CAPITAL LETTER I WITH TILDE" },
1292{ 0x0129, "itilde", "LATIN SMALL LETTER I WITH TILDE" },
1293{ 0x012A, "Imacr", "LATIN CAPITAL LETTER I WITH MACRON" },
1294{ 0x012B, "imacr", "LATIN SMALL LETTER I WITH MACRON" },
1295{ 0x012E, "Iogon", "LATIN CAPITAL LETTER I WITH OGONEK" },
1296{ 0x012F, "iogon", "LATIN SMALL LETTER I WITH OGONEK" },
1297{ 0x0130, "Idot", "LATIN CAPITAL LETTER I WITH DOT ABOVE" },
1298{ 0x0131, "inodot", "LATIN SMALL LETTER DOTLESS I" },
1299{ 0x0131, "inodot", "LATIN SMALL LETTER DOTLESS I" },
1300{ 0x0132, "IJlig", "LATIN CAPITAL LIGATURE IJ" },
1301{ 0x0133, "ijlig", "LATIN SMALL LIGATURE IJ" },
1302{ 0x0134, "Jcirc", "LATIN CAPITAL LETTER J WITH CIRCUMFLEX" },
1303{ 0x0135, "jcirc", "LATIN SMALL LETTER J WITH CIRCUMFLEX" },
1304{ 0x0136, "Kcedil", "LATIN CAPITAL LETTER K WITH CEDILLA" },
1305{ 0x0137, "kcedil", "LATIN SMALL LETTER K WITH CEDILLA" },
1306{ 0x0138, "kgreen", "LATIN SMALL LETTER KRA" },
1307{ 0x0139, "Lacute", "LATIN CAPITAL LETTER L WITH ACUTE" },
1308{ 0x013A, "lacute", "LATIN SMALL LETTER L WITH ACUTE" },
1309{ 0x013B, "Lcedil", "LATIN CAPITAL LETTER L WITH CEDILLA" },
1310{ 0x013C, "lcedil", "LATIN SMALL LETTER L WITH CEDILLA" },
1311{ 0x013D, "Lcaron", "LATIN CAPITAL LETTER L WITH CARON" },
1312{ 0x013E, "lcaron", "LATIN SMALL LETTER L WITH CARON" },
1313{ 0x013F, "Lmidot", "LATIN CAPITAL LETTER L WITH MIDDLE DOT" },
1314{ 0x0140, "lmidot", "LATIN SMALL LETTER L WITH MIDDLE DOT" },
1315{ 0x0141, "Lstrok", "LATIN CAPITAL LETTER L WITH STROKE" },
1316{ 0x0142, "lstrok", "LATIN SMALL LETTER L WITH STROKE" },
1317{ 0x0143, "Nacute", "LATIN CAPITAL LETTER N WITH ACUTE" },
1318{ 0x0144, "nacute", "LATIN SMALL LETTER N WITH ACUTE" },
1319{ 0x0145, "Ncedil", "LATIN CAPITAL LETTER N WITH CEDILLA" },
1320{ 0x0146, "ncedil", "LATIN SMALL LETTER N WITH CEDILLA" },
1321{ 0x0147, "Ncaron", "LATIN CAPITAL LETTER N WITH CARON" },
1322{ 0x0148, "ncaron", "LATIN SMALL LETTER N WITH CARON" },
1323{ 0x0149, "napos", "LATIN SMALL LETTER N PRECEDED BY APOSTROPHE" },
1324{ 0x014A, "ENG", "LATIN CAPITAL LETTER ENG" },
1325{ 0x014B, "eng", "LATIN SMALL LETTER ENG" },
1326{ 0x014C, "Omacr", "LATIN CAPITAL LETTER O WITH MACRON" },
1327{ 0x014D, "omacr", "LATIN SMALL LETTER O WITH MACRON" },
1328{ 0x0150, "Odblac", "LATIN CAPITAL LETTER O WITH DOUBLE ACUTE" },
1329{ 0x0151, "odblac", "LATIN SMALL LETTER O WITH DOUBLE ACUTE" },
1330{ 0x0152, "OElig", "LATIN CAPITAL LIGATURE OE" },
1331{ 0x0153, "oelig", "LATIN SMALL LIGATURE OE" },
1332{ 0x0154, "Racute", "LATIN CAPITAL LETTER R WITH ACUTE" },
1333{ 0x0155, "racute", "LATIN SMALL LETTER R WITH ACUTE" },
1334{ 0x0156, "Rcedil", "LATIN CAPITAL LETTER R WITH CEDILLA" },
1335{ 0x0157, "rcedil", "LATIN SMALL LETTER R WITH CEDILLA" },
1336{ 0x0158, "Rcaron", "LATIN CAPITAL LETTER R WITH CARON" },
1337{ 0x0159, "rcaron", "LATIN SMALL LETTER R WITH CARON" },
1338{ 0x015A, "Sacute", "LATIN CAPITAL LETTER S WITH ACUTE" },
1339{ 0x015B, "sacute", "LATIN SMALL LETTER S WITH ACUTE" },
1340{ 0x015C, "Scirc", "LATIN CAPITAL LETTER S WITH CIRCUMFLEX" },
1341{ 0x015D, "scirc", "LATIN SMALL LETTER S WITH CIRCUMFLEX" },
1342{ 0x015E, "Scedil", "LATIN CAPITAL LETTER S WITH CEDILLA" },
1343{ 0x015F, "scedil", "LATIN SMALL LETTER S WITH CEDILLA" },
1344{ 0x0160, "Scaron", "LATIN CAPITAL LETTER S WITH CARON" },
1345{ 0x0161, "scaron", "LATIN SMALL LETTER S WITH CARON" },
1346{ 0x0162, "Tcedil", "LATIN CAPITAL LETTER T WITH CEDILLA" },
1347{ 0x0163, "tcedil", "LATIN SMALL LETTER T WITH CEDILLA" },
1348{ 0x0164, "Tcaron", "LATIN CAPITAL LETTER T WITH CARON" },
1349{ 0x0165, "tcaron", "LATIN SMALL LETTER T WITH CARON" },
1350{ 0x0166, "Tstrok", "LATIN CAPITAL LETTER T WITH STROKE" },
1351{ 0x0167, "tstrok", "LATIN SMALL LETTER T WITH STROKE" },
1352{ 0x0168, "Utilde", "LATIN CAPITAL LETTER U WITH TILDE" },
1353{ 0x0169, "utilde", "LATIN SMALL LETTER U WITH TILDE" },
1354{ 0x016A, "Umacr", "LATIN CAPITAL LETTER U WITH MACRON" },
1355{ 0x016B, "umacr", "LATIN SMALL LETTER U WITH MACRON" },
1356{ 0x016C, "Ubreve", "LATIN CAPITAL LETTER U WITH BREVE" },
1357{ 0x016D, "ubreve", "LATIN SMALL LETTER U WITH BREVE" },
1358{ 0x016E, "Uring", "LATIN CAPITAL LETTER U WITH RING ABOVE" },
1359{ 0x016F, "uring", "LATIN SMALL LETTER U WITH RING ABOVE" },
1360{ 0x0170, "Udblac", "LATIN CAPITAL LETTER U WITH DOUBLE ACUTE" },
1361{ 0x0171, "udblac", "LATIN SMALL LETTER U WITH DOUBLE ACUTE" },
1362{ 0x0172, "Uogon", "LATIN CAPITAL LETTER U WITH OGONEK" },
1363{ 0x0173, "uogon", "LATIN SMALL LETTER U WITH OGONEK" },
1364{ 0x0174, "Wcirc", "LATIN CAPITAL LETTER W WITH CIRCUMFLEX" },
1365{ 0x0175, "wcirc", "LATIN SMALL LETTER W WITH CIRCUMFLEX" },
1366{ 0x0176, "Ycirc", "LATIN CAPITAL LETTER Y WITH CIRCUMFLEX" },
1367{ 0x0177, "ycirc", "LATIN SMALL LETTER Y WITH CIRCUMFLEX" },
1368{ 0x0178, "Yuml", "LATIN CAPITAL LETTER Y WITH DIAERESIS" },
1369{ 0x0179, "Zacute", "LATIN CAPITAL LETTER Z WITH ACUTE" },
1370{ 0x017A, "zacute", "LATIN SMALL LETTER Z WITH ACUTE" },
1371{ 0x017B, "Zdot", "LATIN CAPITAL LETTER Z WITH DOT ABOVE" },
1372{ 0x017C, "zdot", "LATIN SMALL LETTER Z WITH DOT ABOVE" },
1373{ 0x017D, "Zcaron", "LATIN CAPITAL LETTER Z WITH CARON" },
1374{ 0x017E, "zcaron", "LATIN SMALL LETTER Z WITH CARON" },
1375{ 0x0192, "fnof", "LATIN SMALL LETTER F WITH HOOK" },
1376{ 0x01F5, "gacute", "LATIN SMALL LETTER G WITH ACUTE" },
1377{ 0x02C7, "caron", "CARON" },
1378{ 0x02D8, "breve", "BREVE" },
1379{ 0x02D9, "dot", "DOT ABOVE" },
1380{ 0x02DA, "ring", "RING ABOVE" },
1381{ 0x02DB, "ogon", "OGONEK" },
1382{ 0x02DC, "tilde", "TILDE" },
1383{ 0x02DD, "dblac", "DOUBLE ACUTE ACCENT" },
1384{ 0x0386, "Aacgr", "GREEK CAPITAL LETTER ALPHA WITH TONOS" },
1385{ 0x0388, "Eacgr", "GREEK CAPITAL LETTER EPSILON WITH TONOS" },
1386{ 0x0389, "EEacgr", "GREEK CAPITAL LETTER ETA WITH TONOS" },
1387{ 0x038A, "Iacgr", "GREEK CAPITAL LETTER IOTA WITH TONOS" },
1388{ 0x038C, "Oacgr", "GREEK CAPITAL LETTER OMICRON WITH TONOS" },
1389{ 0x038E, "Uacgr", "GREEK CAPITAL LETTER UPSILON WITH TONOS" },
1390{ 0x038F, "OHacgr", "GREEK CAPITAL LETTER OMEGA WITH TONOS" },
1391{ 0x0390, "idiagr", "GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS" },
1392{ 0x0391, "Agr", "GREEK CAPITAL LETTER ALPHA" },
1393{ 0x0392, "Bgr", "GREEK CAPITAL LETTER BETA" },
1394{ 0x0393, "b.Gamma", "GREEK CAPITAL LETTER GAMMA" },
1395{ 0x0393, "Gamma", "GREEK CAPITAL LETTER GAMMA" },
1396{ 0x0393, "Ggr", "GREEK CAPITAL LETTER GAMMA" },
1397{ 0x0394, "b.Delta", "GREEK CAPITAL LETTER DELTA" },
1398{ 0x0394, "Delta", "GREEK CAPITAL LETTER DELTA" },
1399{ 0x0394, "Dgr", "GREEK CAPITAL LETTER DELTA" },
1400{ 0x0395, "Egr", "GREEK CAPITAL LETTER EPSILON" },
1401{ 0x0396, "Zgr", "GREEK CAPITAL LETTER ZETA" },
1402{ 0x0397, "EEgr", "GREEK CAPITAL LETTER ETA" },
1403{ 0x0398, "b.Theta", "GREEK CAPITAL LETTER THETA" },
1404{ 0x0398, "Theta", "GREEK CAPITAL LETTER THETA" },
1405{ 0x0398, "THgr", "GREEK CAPITAL LETTER THETA" },
1406{ 0x0399, "Igr", "GREEK CAPITAL LETTER IOTA" },
1407{ 0x039A, "Kgr", "GREEK CAPITAL LETTER KAPPA" },
1408{ 0x039B, "b.Lambda", "GREEK CAPITAL LETTER LAMDA" },
1409{ 0x039B, "Lambda", "GREEK CAPITAL LETTER LAMDA" },
1410{ 0x039B, "Lgr", "GREEK CAPITAL LETTER LAMDA" },
1411{ 0x039C, "Mgr", "GREEK CAPITAL LETTER MU" },
1412{ 0x039D, "Ngr", "GREEK CAPITAL LETTER NU" },
1413{ 0x039E, "b.Xi", "GREEK CAPITAL LETTER XI" },
1414{ 0x039E, "Xgr", "GREEK CAPITAL LETTER XI" },
1415{ 0x039E, "Xi", "GREEK CAPITAL LETTER XI" },
1416{ 0x039F, "Ogr", "GREEK CAPITAL LETTER OMICRON" },
1417{ 0x03A0, "b.Pi", "GREEK CAPITAL LETTER PI" },
1418{ 0x03A0, "Pgr", "GREEK CAPITAL LETTER PI" },
1419{ 0x03A0, "Pi", "GREEK CAPITAL LETTER PI" },
1420{ 0x03A1, "Rgr", "GREEK CAPITAL LETTER RHO" },
1421{ 0x03A3, "b.Sigma", "GREEK CAPITAL LETTER SIGMA" },
1422{ 0x03A3, "Sgr", "GREEK CAPITAL LETTER SIGMA" },
1423{ 0x03A3, "Sigma", "GREEK CAPITAL LETTER SIGMA" },
1424{ 0x03A4, "Tgr", "GREEK CAPITAL LETTER TAU" },
1425{ 0x03A5, "Ugr", "" },
1426{ 0x03A6, "b.Phi", "GREEK CAPITAL LETTER PHI" },
1427{ 0x03A6, "PHgr", "GREEK CAPITAL LETTER PHI" },
1428{ 0x03A6, "Phi", "GREEK CAPITAL LETTER PHI" },
1429{ 0x03A7, "KHgr", "GREEK CAPITAL LETTER CHI" },
1430{ 0x03A8, "b.Psi", "GREEK CAPITAL LETTER PSI" },
1431{ 0x03A8, "PSgr", "GREEK CAPITAL LETTER PSI" },
1432{ 0x03A8, "Psi", "GREEK CAPITAL LETTER PSI" },
1433{ 0x03A9, "b.Omega", "GREEK CAPITAL LETTER OMEGA" },
1434{ 0x03A9, "OHgr", "GREEK CAPITAL LETTER OMEGA" },
1435{ 0x03A9, "Omega", "GREEK CAPITAL LETTER OMEGA" },
1436{ 0x03AA, "Idigr", "GREEK CAPITAL LETTER IOTA WITH DIALYTIKA" },
1437{ 0x03AB, "Udigr", "GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA" },
1438{ 0x03AC, "aacgr", "GREEK SMALL LETTER ALPHA WITH TONOS" },
1439{ 0x03AD, "eacgr", "GREEK SMALL LETTER EPSILON WITH TONOS" },
1440{ 0x03AE, "eeacgr", "GREEK SMALL LETTER ETA WITH TONOS" },
1441{ 0x03AF, "iacgr", "GREEK SMALL LETTER IOTA WITH TONOS" },
1442{ 0x03B0, "udiagr", "GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS" },
1443{ 0x03B1, "agr", "" },
1444{ 0x03B1, "alpha", "" },
1445{ 0x03B1, "b.alpha", "" },
1446{ 0x03B2, "b.beta", "GREEK SMALL LETTER BETA" },
1447{ 0x03B2, "beta", "GREEK SMALL LETTER BETA" },
1448{ 0x03B2, "bgr", "GREEK SMALL LETTER BETA" },
1449{ 0x03B3, "b.gamma", "GREEK SMALL LETTER GAMMA" },
1450{ 0x03B3, "gamma", "GREEK SMALL LETTER GAMMA" },
1451{ 0x03B3, "ggr", "GREEK SMALL LETTER GAMMA" },
1452{ 0x03B4, "b.delta", "GREEK SMALL LETTER DELTA" },
1453{ 0x03B4, "delta", "GREEK SMALL LETTER DELTA" },
1454{ 0x03B4, "dgr", "GREEK SMALL LETTER DELTA" },
1455{ 0x03B5, "b.epsi", "" },
1456{ 0x03B5, "b.epsis", "" },
1457{ 0x03B5, "b.epsiv", "" },
1458{ 0x03B5, "egr", "" },
1459{ 0x03B5, "epsiv", "" },
1460{ 0x03B6, "b.zeta", "GREEK SMALL LETTER ZETA" },
1461{ 0x03B6, "zeta", "GREEK SMALL LETTER ZETA" },
1462{ 0x03B6, "zgr", "GREEK SMALL LETTER ZETA" },
1463{ 0x03B7, "b.eta", "GREEK SMALL LETTER ETA" },
1464{ 0x03B7, "eegr", "GREEK SMALL LETTER ETA" },
1465{ 0x03B7, "eta", "GREEK SMALL LETTER ETA" },
1466{ 0x03B8, "b.thetas", "" },
1467{ 0x03B8, "thetas", "" },
1468{ 0x03B8, "thgr", "" },
1469{ 0x03B9, "b.iota", "GREEK SMALL LETTER IOTA" },
1470{ 0x03B9, "igr", "GREEK SMALL LETTER IOTA" },
1471{ 0x03B9, "iota", "GREEK SMALL LETTER IOTA" },
1472{ 0x03BA, "b.kappa", "GREEK SMALL LETTER KAPPA" },
1473{ 0x03BA, "kappa", "GREEK SMALL LETTER KAPPA" },
1474{ 0x03BA, "kgr", "GREEK SMALL LETTER KAPPA" },
1475{ 0x03BB, "b.lambda", "GREEK SMALL LETTER LAMDA" },
1476{ 0x03BB, "lambda", "GREEK SMALL LETTER LAMDA" },
1477{ 0x03BB, "lgr", "GREEK SMALL LETTER LAMDA" },
1478{ 0x03BC, "b.mu", "GREEK SMALL LETTER MU" },
1479{ 0x03BC, "mgr", "GREEK SMALL LETTER MU" },
1480{ 0x03BC, "mu", "GREEK SMALL LETTER MU" },
1481{ 0x03BD, "b.nu", "GREEK SMALL LETTER NU" },
1482{ 0x03BD, "ngr", "GREEK SMALL LETTER NU" },
1483{ 0x03BD, "nu", "GREEK SMALL LETTER NU" },
1484{ 0x03BE, "b.xi", "GREEK SMALL LETTER XI" },
1485{ 0x03BE, "xgr", "GREEK SMALL LETTER XI" },
1486{ 0x03BE, "xi", "GREEK SMALL LETTER XI" },
1487{ 0x03BF, "ogr", "GREEK SMALL LETTER OMICRON" },
1488{ 0x03C0, "b.pi", "GREEK SMALL LETTER PI" },
1489{ 0x03C0, "pgr", "GREEK SMALL LETTER PI" },
1490{ 0x03C0, "pi", "GREEK SMALL LETTER PI" },
1491{ 0x03C1, "b.rho", "GREEK SMALL LETTER RHO" },
1492{ 0x03C1, "rgr", "GREEK SMALL LETTER RHO" },
1493{ 0x03C1, "rho", "GREEK SMALL LETTER RHO" },
1494{ 0x03C2, "b.sigmav", "" },
1495{ 0x03C2, "sfgr", "" },
1496{ 0x03C2, "sigmav", "" },
1497{ 0x03C3, "b.sigma", "GREEK SMALL LETTER SIGMA" },
1498{ 0x03C3, "sgr", "GREEK SMALL LETTER SIGMA" },
1499{ 0x03C3, "sigma", "GREEK SMALL LETTER SIGMA" },
1500{ 0x03C4, "b.tau", "GREEK SMALL LETTER TAU" },
1501{ 0x03C4, "tau", "GREEK SMALL LETTER TAU" },
1502{ 0x03C4, "tgr", "GREEK SMALL LETTER TAU" },
1503{ 0x03C5, "b.upsi", "GREEK SMALL LETTER UPSILON" },
1504{ 0x03C5, "ugr", "GREEK SMALL LETTER UPSILON" },
1505{ 0x03C5, "upsi", "GREEK SMALL LETTER UPSILON" },
1506{ 0x03C6, "b.phis", "GREEK SMALL LETTER PHI" },
1507{ 0x03C6, "phgr", "GREEK SMALL LETTER PHI" },
1508{ 0x03C6, "phis", "GREEK SMALL LETTER PHI" },
1509{ 0x03C7, "b.chi", "GREEK SMALL LETTER CHI" },
1510{ 0x03C7, "chi", "GREEK SMALL LETTER CHI" },
1511{ 0x03C7, "khgr", "GREEK SMALL LETTER CHI" },
1512{ 0x03C8, "b.psi", "GREEK SMALL LETTER PSI" },
1513{ 0x03C8, "psgr", "GREEK SMALL LETTER PSI" },
1514{ 0x03C8, "psi", "GREEK SMALL LETTER PSI" },
1515{ 0x03C9, "b.omega", "GREEK SMALL LETTER OMEGA" },
1516{ 0x03C9, "ohgr", "GREEK SMALL LETTER OMEGA" },
1517{ 0x03C9, "omega", "GREEK SMALL LETTER OMEGA" },
1518{ 0x03CA, "idigr", "GREEK SMALL LETTER IOTA WITH DIALYTIKA" },
1519{ 0x03CB, "udigr", "GREEK SMALL LETTER UPSILON WITH DIALYTIKA" },
1520{ 0x03CC, "oacgr", "GREEK SMALL LETTER OMICRON WITH TONOS" },
1521{ 0x03CD, "uacgr", "GREEK SMALL LETTER UPSILON WITH TONOS" },
1522{ 0x03CE, "ohacgr", "GREEK SMALL LETTER OMEGA WITH TONOS" },
1523{ 0x03D1, "b.thetav", "" },
1524{ 0x03D1, "thetav", "" },
1525{ 0x03D2, "b.Upsi", "" },
1526{ 0x03D2, "Upsi", "" },
1527{ 0x03D5, "b.phiv", "GREEK PHI SYMBOL" },
1528{ 0x03D5, "phiv", "GREEK PHI SYMBOL" },
1529{ 0x03D6, "b.piv", "GREEK PI SYMBOL" },
1530{ 0x03D6, "piv", "GREEK PI SYMBOL" },
1531{ 0x03DC, "b.gammad", "GREEK LETTER DIGAMMA" },
1532{ 0x03DC, "gammad", "GREEK LETTER DIGAMMA" },
1533{ 0x03F0, "b.kappav", "GREEK KAPPA SYMBOL" },
1534{ 0x03F0, "kappav", "GREEK KAPPA SYMBOL" },
1535{ 0x03F1, "b.rhov", "GREEK RHO SYMBOL" },
1536{ 0x03F1, "rhov", "GREEK RHO SYMBOL" },
1537{ 0x0401, "IOcy", "CYRILLIC CAPITAL LETTER IO" },
1538{ 0x0402, "DJcy", "CYRILLIC CAPITAL LETTER DJE" },
1539{ 0x0403, "GJcy", "CYRILLIC CAPITAL LETTER GJE" },
1540{ 0x0404, "Jukcy", "CYRILLIC CAPITAL LETTER UKRAINIAN IE" },
1541{ 0x0405, "DScy", "CYRILLIC CAPITAL LETTER DZE" },
1542{ 0x0406, "Iukcy", "CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I" },
1543{ 0x0407, "YIcy", "CYRILLIC CAPITAL LETTER YI" },
1544{ 0x0408, "Jsercy", "CYRILLIC CAPITAL LETTER JE" },
1545{ 0x0409, "LJcy", "CYRILLIC CAPITAL LETTER LJE" },
1546{ 0x040A, "NJcy", "CYRILLIC CAPITAL LETTER NJE" },
1547{ 0x040B, "TSHcy", "CYRILLIC CAPITAL LETTER TSHE" },
1548{ 0x040C, "KJcy", "CYRILLIC CAPITAL LETTER KJE" },
1549{ 0x040E, "Ubrcy", "CYRILLIC CAPITAL LETTER SHORT U" },
1550{ 0x040F, "DZcy", "CYRILLIC CAPITAL LETTER DZHE" },
1551{ 0x0410, "Acy", "CYRILLIC CAPITAL LETTER A" },
1552{ 0x0411, "Bcy", "CYRILLIC CAPITAL LETTER BE" },
1553{ 0x0412, "Vcy", "CYRILLIC CAPITAL LETTER VE" },
1554{ 0x0413, "Gcy", "CYRILLIC CAPITAL LETTER GHE" },
1555{ 0x0414, "Dcy", "CYRILLIC CAPITAL LETTER DE" },
1556{ 0x0415, "IEcy", "CYRILLIC CAPITAL LETTER IE" },
1557{ 0x0416, "ZHcy", "CYRILLIC CAPITAL LETTER ZHE" },
1558{ 0x0417, "Zcy", "CYRILLIC CAPITAL LETTER ZE" },
1559{ 0x0418, "Icy", "CYRILLIC CAPITAL LETTER I" },
1560{ 0x0419, "Jcy", "CYRILLIC CAPITAL LETTER SHORT I" },
1561{ 0x041A, "Kcy", "CYRILLIC CAPITAL LETTER KA" },
1562{ 0x041B, "Lcy", "CYRILLIC CAPITAL LETTER EL" },
1563{ 0x041C, "Mcy", "CYRILLIC CAPITAL LETTER EM" },
1564{ 0x041D, "Ncy", "CYRILLIC CAPITAL LETTER EN" },
1565{ 0x041E, "Ocy", "CYRILLIC CAPITAL LETTER O" },
1566{ 0x041F, "Pcy", "CYRILLIC CAPITAL LETTER PE" },
1567{ 0x0420, "Rcy", "CYRILLIC CAPITAL LETTER ER" },
1568{ 0x0421, "Scy", "CYRILLIC CAPITAL LETTER ES" },
1569{ 0x0422, "Tcy", "CYRILLIC CAPITAL LETTER TE" },
1570{ 0x0423, "Ucy", "CYRILLIC CAPITAL LETTER U" },
1571{ 0x0424, "Fcy", "CYRILLIC CAPITAL LETTER EF" },
1572{ 0x0425, "KHcy", "CYRILLIC CAPITAL LETTER HA" },
1573{ 0x0426, "TScy", "CYRILLIC CAPITAL LETTER TSE" },
1574{ 0x0427, "CHcy", "CYRILLIC CAPITAL LETTER CHE" },
1575{ 0x0428, "SHcy", "CYRILLIC CAPITAL LETTER SHA" },
1576{ 0x0429, "SHCHcy", "CYRILLIC CAPITAL LETTER SHCHA" },
1577{ 0x042A, "HARDcy", "CYRILLIC CAPITAL LETTER HARD SIGN" },
1578{ 0x042B, "Ycy", "CYRILLIC CAPITAL LETTER YERU" },
1579{ 0x042C, "SOFTcy", "CYRILLIC CAPITAL LETTER SOFT SIGN" },
1580{ 0x042D, "Ecy", "CYRILLIC CAPITAL LETTER E" },
1581{ 0x042E, "YUcy", "CYRILLIC CAPITAL LETTER YU" },
1582{ 0x042F, "YAcy", "CYRILLIC CAPITAL LETTER YA" },
1583{ 0x0430, "acy", "CYRILLIC SMALL LETTER A" },
1584{ 0x0431, "bcy", "CYRILLIC SMALL LETTER BE" },
1585{ 0x0432, "vcy", "CYRILLIC SMALL LETTER VE" },
1586{ 0x0433, "gcy", "CYRILLIC SMALL LETTER GHE" },
1587{ 0x0434, "dcy", "CYRILLIC SMALL LETTER DE" },
1588{ 0x0435, "iecy", "CYRILLIC SMALL LETTER IE" },
1589{ 0x0436, "zhcy", "CYRILLIC SMALL LETTER ZHE" },
1590{ 0x0437, "zcy", "CYRILLIC SMALL LETTER ZE" },
1591{ 0x0438, "icy", "CYRILLIC SMALL LETTER I" },
1592{ 0x0439, "jcy", "CYRILLIC SMALL LETTER SHORT I" },
1593{ 0x043A, "kcy", "CYRILLIC SMALL LETTER KA" },
1594{ 0x043B, "lcy", "CYRILLIC SMALL LETTER EL" },
1595{ 0x043C, "mcy", "CYRILLIC SMALL LETTER EM" },
1596{ 0x043D, "ncy", "CYRILLIC SMALL LETTER EN" },
1597{ 0x043E, "ocy", "CYRILLIC SMALL LETTER O" },
1598{ 0x043F, "pcy", "CYRILLIC SMALL LETTER PE" },
1599{ 0x0440, "rcy", "CYRILLIC SMALL LETTER ER" },
1600{ 0x0441, "scy", "CYRILLIC SMALL LETTER ES" },
1601{ 0x0442, "tcy", "CYRILLIC SMALL LETTER TE" },
1602{ 0x0443, "ucy", "CYRILLIC SMALL LETTER U" },
1603{ 0x0444, "fcy", "CYRILLIC SMALL LETTER EF" },
1604{ 0x0445, "khcy", "CYRILLIC SMALL LETTER HA" },
1605{ 0x0446, "tscy", "CYRILLIC SMALL LETTER TSE" },
1606{ 0x0447, "chcy", "CYRILLIC SMALL LETTER CHE" },
1607{ 0x0448, "shcy", "CYRILLIC SMALL LETTER SHA" },
1608{ 0x0449, "shchcy", "CYRILLIC SMALL LETTER SHCHA" },
1609{ 0x044A, "hardcy", "CYRILLIC SMALL LETTER HARD SIGN" },
1610{ 0x044B, "ycy", "CYRILLIC SMALL LETTER YERU" },
1611{ 0x044C, "softcy", "CYRILLIC SMALL LETTER SOFT SIGN" },
1612{ 0x044D, "ecy", "CYRILLIC SMALL LETTER E" },
1613{ 0x044E, "yucy", "CYRILLIC SMALL LETTER YU" },
1614{ 0x044F, "yacy", "CYRILLIC SMALL LETTER YA" },
1615{ 0x0451, "iocy", "CYRILLIC SMALL LETTER IO" },
1616{ 0x0452, "djcy", "CYRILLIC SMALL LETTER DJE" },
1617{ 0x0453, "gjcy", "CYRILLIC SMALL LETTER GJE" },
1618{ 0x0454, "jukcy", "CYRILLIC SMALL LETTER UKRAINIAN IE" },
1619{ 0x0455, "dscy", "CYRILLIC SMALL LETTER DZE" },
1620{ 0x0456, "iukcy", "CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I" },
1621{ 0x0457, "yicy", "CYRILLIC SMALL LETTER YI" },
1622{ 0x0458, "jsercy", "CYRILLIC SMALL LETTER JE" },
1623{ 0x0459, "ljcy", "CYRILLIC SMALL LETTER LJE" },
1624{ 0x045A, "njcy", "CYRILLIC SMALL LETTER NJE" },
1625{ 0x045B, "tshcy", "CYRILLIC SMALL LETTER TSHE" },
1626{ 0x045C, "kjcy", "CYRILLIC SMALL LETTER KJE" },
1627{ 0x045E, "ubrcy", "CYRILLIC SMALL LETTER SHORT U" },
1628{ 0x045F, "dzcy", "CYRILLIC SMALL LETTER DZHE" },
1629{ 0x2002, "ensp", "EN SPACE" },
1630{ 0x2003, "emsp", "EM SPACE" },
1631{ 0x2004, "emsp13", "THREE-PER-EM SPACE" },
1632{ 0x2005, "emsp14", "FOUR-PER-EM SPACE" },
1633{ 0x2007, "numsp", "FIGURE SPACE" },
1634{ 0x2008, "puncsp", "PUNCTUATION SPACE" },
1635{ 0x2009, "thinsp", "THIN SPACE" },
1636{ 0x200A, "hairsp", "HAIR SPACE" },
1637{ 0x2010, "dash", "HYPHEN" },
1638{ 0x2013, "ndash", "EN DASH" },
1639{ 0x2014, "mdash", "EM DASH" },
1640{ 0x2015, "horbar", "HORIZONTAL BAR" },
1641{ 0x2016, "Verbar", "DOUBLE VERTICAL LINE" },
1642{ 0x2018, "lsquo", "" },
1643{ 0x2018, "rsquor", "" },
1644{ 0x2019, "rsquo", "RIGHT SINGLE QUOTATION MARK" },
1645{ 0x201A, "lsquor", "SINGLE LOW-9 QUOTATION MARK" },
1646{ 0x201C, "ldquo", "" },
1647{ 0x201C, "rdquor", "" },
1648{ 0x201D, "rdquo", "RIGHT DOUBLE QUOTATION MARK" },
1649{ 0x201E, "ldquor", "DOUBLE LOW-9 QUOTATION MARK" },
1650{ 0x2020, "dagger", "DAGGER" },
1651{ 0x2021, "Dagger", "DOUBLE DAGGER" },
1652{ 0x2022, "bull", "BULLET" },
1653{ 0x2025, "nldr", "TWO DOT LEADER" },
1654{ 0x2026, "hellip", "HORIZONTAL ELLIPSIS" },
1655{ 0x2026, "mldr", "HORIZONTAL ELLIPSIS" },
1656{ 0x2030, "permil", "PER MILLE SIGN" },
1657{ 0x2032, "prime", "PRIME" },
1658{ 0x2032, "vprime", "PRIME" },
1659{ 0x2033, "Prime", "DOUBLE PRIME" },
1660{ 0x2034, "tprime", "TRIPLE PRIME" },
1661{ 0x2035, "bprime", "REVERSED PRIME" },
1662{ 0x2041, "caret", "CARET" },
1663{ 0x2043, "hybull", "HYPHEN BULLET" },
1664{ 0x20DB, "tdot", "COMBINING THREE DOTS ABOVE" },
1665{ 0x20DC, "DotDot", "COMBINING FOUR DOTS ABOVE" },
1666{ 0x2105, "incare", "CARE OF" },
1667{ 0x210B, "hamilt", "SCRIPT CAPITAL H" },
1668{ 0x210F, "planck", "PLANCK CONSTANT OVER TWO PI" },
1669{ 0x2111, "image", "BLACK-LETTER CAPITAL I" },
1670{ 0x2112, "lagran", "SCRIPT CAPITAL L" },
1671{ 0x2113, "ell", "SCRIPT SMALL L" },
1672{ 0x2116, "numero", "NUMERO SIGN" },
1673{ 0x2117, "copysr", "SOUND RECORDING COPYRIGHT" },
1674{ 0x2118, "weierp", "SCRIPT CAPITAL P" },
1675{ 0x211C, "real", "BLACK-LETTER CAPITAL R" },
1676{ 0x211E, "rx", "PRESCRIPTION TAKE" },
1677{ 0x2122, "trade", "TRADE MARK SIGN" },
1678{ 0x2126, "ohm", "OHM SIGN" },
1679{ 0x212B, "angst", "ANGSTROM SIGN" },
1680{ 0x212C, "bernou", "SCRIPT CAPITAL B" },
1681{ 0x2133, "phmmat", "SCRIPT CAPITAL M" },
1682{ 0x2134, "order", "SCRIPT SMALL O" },
1683{ 0x2135, "aleph", "ALEF SYMBOL" },
1684{ 0x2136, "beth", "BET SYMBOL" },
1685{ 0x2137, "gimel", "GIMEL SYMBOL" },
1686{ 0x2138, "daleth", "DALET SYMBOL" },
1687{ 0x2153, "frac13", "VULGAR FRACTION ONE THIRD" },
1688{ 0x2154, "frac23", "VULGAR FRACTION TWO THIRDS" },
1689{ 0x2155, "frac15", "VULGAR FRACTION ONE FIFTH" },
1690{ 0x2156, "frac25", "VULGAR FRACTION TWO FIFTHS" },
1691{ 0x2157, "frac35", "VULGAR FRACTION THREE FIFTHS" },
1692{ 0x2158, "frac45", "VULGAR FRACTION FOUR FIFTHS" },
1693{ 0x2159, "frac16", "VULGAR FRACTION ONE SIXTH" },
1694{ 0x215A, "frac56", "VULGAR FRACTION FIVE SIXTHS" },
1695{ 0x215B, "frac18", "" },
1696{ 0x215C, "frac38", "" },
1697{ 0x215D, "frac58", "" },
1698{ 0x215E, "frac78", "" },
1699{ 0x2190, "larr", "LEFTWARDS DOUBLE ARROW" },
1700{ 0x2191, "uarr", "UPWARDS ARROW" },
1701{ 0x2192, "rarr", "RIGHTWARDS DOUBLE ARROW" },
1702{ 0x2193, "darr", "DOWNWARDS ARROW" },
1703{ 0x2194, "harr", "LEFT RIGHT ARROW" },
1704{ 0x2194, "xhArr", "LEFT RIGHT ARROW" },
1705{ 0x2194, "xharr", "LEFT RIGHT ARROW" },
1706{ 0x2195, "varr", "UP DOWN ARROW" },
1707{ 0x2196, "nwarr", "NORTH WEST ARROW" },
1708{ 0x2197, "nearr", "NORTH EAST ARROW" },
1709{ 0x2198, "drarr", "SOUTH EAST ARROW" },
1710{ 0x2199, "dlarr", "SOUTH WEST ARROW" },
1711{ 0x219A, "nlarr", "LEFTWARDS ARROW WITH STROKE" },
1712{ 0x219B, "nrarr", "RIGHTWARDS ARROW WITH STROKE" },
1713{ 0x219D, "rarrw", "RIGHTWARDS SQUIGGLE ARROW" },
1714{ 0x219E, "Larr", "LEFTWARDS TWO HEADED ARROW" },
1715{ 0x21A0, "Rarr", "RIGHTWARDS TWO HEADED ARROW" },
1716{ 0x21A2, "larrtl", "LEFTWARDS ARROW WITH TAIL" },
1717{ 0x21A3, "rarrtl", "RIGHTWARDS ARROW WITH TAIL" },
1718{ 0x21A6, "map", "RIGHTWARDS ARROW FROM BAR" },
1719{ 0x21A9, "larrhk", "LEFTWARDS ARROW WITH HOOK" },
1720{ 0x21AA, "rarrhk", "RIGHTWARDS ARROW WITH HOOK" },
1721{ 0x21AB, "larrlp", "LEFTWARDS ARROW WITH LOOP" },
1722{ 0x21AC, "rarrlp", "RIGHTWARDS ARROW WITH LOOP" },
1723{ 0x21AD, "harrw", "LEFT RIGHT WAVE ARROW" },
1724{ 0x21AE, "nharr", "LEFT RIGHT ARROW WITH STROKE" },
1725{ 0x21B0, "lsh", "UPWARDS ARROW WITH TIP LEFTWARDS" },
1726{ 0x21B1, "rsh", "UPWARDS ARROW WITH TIP RIGHTWARDS" },
1727{ 0x21B6, "cularr", "ANTICLOCKWISE TOP SEMICIRCLE ARROW" },
1728{ 0x21B7, "curarr", "CLOCKWISE TOP SEMICIRCLE ARROW" },
1729{ 0x21BA, "olarr", "ANTICLOCKWISE OPEN CIRCLE ARROW" },
1730{ 0x21BB, "orarr", "CLOCKWISE OPEN CIRCLE ARROW" },
1731{ 0x21BC, "lharu", "LEFTWARDS HARPOON WITH BARB UPWARDS" },
1732{ 0x21BD, "lhard", "LEFTWARDS HARPOON WITH BARB DOWNWARDS" },
1733{ 0x21BE, "uharr", "UPWARDS HARPOON WITH BARB RIGHTWARDS" },
1734{ 0x21BF, "uharl", "UPWARDS HARPOON WITH BARB LEFTWARDS" },
1735{ 0x21C0, "rharu", "RIGHTWARDS HARPOON WITH BARB UPWARDS" },
1736{ 0x21C1, "rhard", "RIGHTWARDS HARPOON WITH BARB DOWNWARDS" },
1737{ 0x21C2, "dharr", "DOWNWARDS HARPOON WITH BARB RIGHTWARDS" },
1738{ 0x21C3, "dharl", "DOWNWARDS HARPOON WITH BARB LEFTWARDS" },
1739{ 0x21C4, "rlarr2", "RIGHTWARDS ARROW OVER LEFTWARDS ARROW" },
1740{ 0x21C6, "lrarr2", "LEFTWARDS ARROW OVER RIGHTWARDS ARROW" },
1741{ 0x21C7, "larr2", "LEFTWARDS PAIRED ARROWS" },
1742{ 0x21C8, "uarr2", "UPWARDS PAIRED ARROWS" },
1743{ 0x21C9, "rarr2", "RIGHTWARDS PAIRED ARROWS" },
1744{ 0x21CA, "darr2", "DOWNWARDS PAIRED ARROWS" },
1745{ 0x21CB, "lrhar2", "LEFTWARDS HARPOON OVER RIGHTWARDS HARPOON" },
1746{ 0x21CC, "rlhar2", "RIGHTWARDS HARPOON OVER LEFTWARDS HARPOON" },
1747{ 0x21CD, "nlArr", "LEFTWARDS DOUBLE ARROW WITH STROKE" },
1748{ 0x21CE, "nhArr", "LEFT RIGHT DOUBLE ARROW WITH STROKE" },
1749{ 0x21CF, "nrArr", "RIGHTWARDS DOUBLE ARROW WITH STROKE" },
1750{ 0x21D0, "lArr", "LEFTWARDS ARROW" },
1751{ 0x21D0, "xlArr", "LEFTWARDS DOUBLE ARROW" },
1752{ 0x21D1, "uArr", "UPWARDS DOUBLE ARROW" },
1753{ 0x21D2, "rArr", "RIGHTWARDS ARROW" },
1754{ 0x21D2, "xrArr", "RIGHTWARDS DOUBLE ARROW" },
1755{ 0x21D3, "dArr", "DOWNWARDS DOUBLE ARROW" },
1756{ 0x21D4, "hArr", "" },
1757{ 0x21D4, "iff", "LEFT RIGHT DOUBLE ARROW" },
1758{ 0x21D5, "vArr", "UP DOWN DOUBLE ARROW" },
1759{ 0x21DA, "lAarr", "LEFTWARDS TRIPLE ARROW" },
1760{ 0x21DB, "rAarr", "RIGHTWARDS TRIPLE ARROW" },
1761{ 0x2200, "forall", "" },
1762{ 0x2201, "comp", "COMPLEMENT" },
1763{ 0x2202, "part", "" },
1764{ 0x2203, "exist", "" },
1765{ 0x2204, "nexist", "THERE DOES NOT EXIST" },
1766{ 0x2205, "empty", "" },
1767{ 0x2207, "nabla", "NABLA" },
1768{ 0x2209, "notin", "" },
1769{ 0x220A, "epsi", "" },
1770{ 0x220A, "epsis", "" },
1771{ 0x220A, "isin", "" },
1772{ 0x220D, "bepsi", "SMALL CONTAINS AS MEMBER" },
1773{ 0x220D, "ni", "" },
1774{ 0x220F, "prod", "N-ARY PRODUCT" },
1775{ 0x2210, "amalg", "N-ARY COPRODUCT" },
1776{ 0x2210, "coprod", "N-ARY COPRODUCT" },
1777{ 0x2210, "samalg", "" },
1778{ 0x2211, "sum", "N-ARY SUMMATION" },
1779{ 0x2212, "minus", "MINUS SIGN" },
1780{ 0x2213, "mnplus", "" },
1781{ 0x2214, "plusdo", "DOT PLUS" },
1782{ 0x2216, "setmn", "SET MINUS" },
1783{ 0x2216, "ssetmn", "SET MINUS" },
1784{ 0x2217, "lowast", "ASTERISK OPERATOR" },
1785{ 0x2218, "compfn", "RING OPERATOR" },
1786{ 0x221A, "radic", "" },
1787{ 0x221D, "prop", "" },
1788{ 0x221D, "vprop", "" },
1789{ 0x221E, "infin", "" },
1790{ 0x221F, "ang90", "RIGHT ANGLE" },
1791{ 0x2220, "ang", "ANGLE" },
1792{ 0x2221, "angmsd", "MEASURED ANGLE" },
1793{ 0x2222, "angsph", "" },
1794{ 0x2223, "mid", "" },
1795{ 0x2224, "nmid", "DOES NOT DIVIDE" },
1796{ 0x2225, "par", "PARALLEL TO" },
1797{ 0x2225, "spar", "PARALLEL TO" },
1798{ 0x2226, "npar", "NOT PARALLEL TO" },
1799{ 0x2226, "nspar", "NOT PARALLEL TO" },
1800{ 0x2227, "and", "" },
1801{ 0x2228, "or", "" },
1802{ 0x2229, "cap", "" },
1803{ 0x222A, "cup", "" },
1804{ 0x222B, "int", "" },
1805{ 0x222E, "conint", "" },
1806{ 0x2234, "there4", "" },
1807{ 0x2235, "becaus", "BECAUSE" },
1808{ 0x223C, "sim", "" },
1809{ 0x223C, "thksim", "TILDE OPERATOR" },
1810{ 0x223D, "bsim", "" },
1811{ 0x2240, "wreath", "WREATH PRODUCT" },
1812{ 0x2241, "nsim", "" },
1813{ 0x2243, "sime", "" },
1814{ 0x2244, "nsime", "" },
1815{ 0x2245, "cong", "" },
1816{ 0x2247, "ncong", "NEITHER APPROXIMATELY NOR ACTUALLY EQUAL TO" },
1817{ 0x2248, "ap", "" },
1818{ 0x2248, "thkap", "ALMOST EQUAL TO" },
1819{ 0x2249, "nap", "NOT ALMOST EQUAL TO" },
1820{ 0x224A, "ape", "" },
1821{ 0x224C, "bcong", "ALL EQUAL TO" },
1822{ 0x224D, "asymp", "EQUIVALENT TO" },
1823{ 0x224E, "bump", "" },
1824{ 0x224F, "bumpe", "" },
1825{ 0x2250, "esdot", "" },
1826{ 0x2251, "eDot", "" },
1827{ 0x2252, "efDot", "" },
1828{ 0x2253, "erDot", "" },
1829{ 0x2254, "colone", "" },
1830{ 0x2255, "ecolon", "" },
1831{ 0x2256, "ecir", "" },
1832{ 0x2257, "cire", "" },
1833{ 0x2259, "wedgeq", "ESTIMATES" },
1834{ 0x225C, "trie", "" },
1835{ 0x2260, "ne", "" },
1836{ 0x2261, "equiv", "" },
1837{ 0x2262, "nequiv", "NOT IDENTICAL TO" },
1838{ 0x2264, "le", "" },
1839{ 0x2264, "les", "LESS-THAN OR EQUAL TO" },
1840{ 0x2265, "ge", "GREATER-THAN OR EQUAL TO" },
1841{ 0x2265, "ges", "GREATER-THAN OR EQUAL TO" },
1842{ 0x2266, "lE", "" },
1843{ 0x2267, "gE", "" },
1844{ 0x2268, "lnE", "" },
1845{ 0x2268, "lne", "" },
1846{ 0x2268, "lvnE", "LESS-THAN BUT NOT EQUAL TO" },
1847{ 0x2269, "gnE", "" },
1848{ 0x2269, "gne", "" },
1849{ 0x2269, "gvnE", "GREATER-THAN BUT NOT EQUAL TO" },
1850{ 0x226A, "Lt", "MUCH LESS-THAN" },
1851{ 0x226B, "Gt", "MUCH GREATER-THAN" },
1852{ 0x226C, "twixt", "BETWEEN" },
1853{ 0x226E, "nlt", "NOT LESS-THAN" },
1854{ 0x226F, "ngt", "NOT GREATER-THAN" },
1855{ 0x2270, "nlE", "" },
1856{ 0x2270, "nle", "NEITHER LESS-THAN NOR EQUAL TO" },
1857{ 0x2270, "nles", "" },
1858{ 0x2271, "ngE", "" },
1859{ 0x2271, "nge", "NEITHER GREATER-THAN NOR EQUAL TO" },
1860{ 0x2271, "nges", "" },
1861{ 0x2272, "lap", "LESS-THAN OR EQUIVALENT TO" },
1862{ 0x2272, "lsim", "LESS-THAN OR EQUIVALENT TO" },
1863{ 0x2273, "gap", "GREATER-THAN OR EQUIVALENT TO" },
1864{ 0x2273, "gsim", "GREATER-THAN OR EQUIVALENT TO" },
1865{ 0x2276, "lg", "LESS-THAN OR GREATER-THAN" },
1866{ 0x2277, "gl", "" },
1867{ 0x227A, "pr", "" },
1868{ 0x227B, "sc", "" },
1869{ 0x227C, "cupre", "" },
1870{ 0x227C, "pre", "" },
1871{ 0x227D, "sccue", "" },
1872{ 0x227D, "sce", "" },
1873{ 0x227E, "prap", "" },
1874{ 0x227E, "prsim", "" },
1875{ 0x227F, "scap", "" },
1876{ 0x227F, "scsim", "" },
1877{ 0x2280, "npr", "DOES NOT PRECEDE" },
1878{ 0x2281, "nsc", "DOES NOT SUCCEED" },
1879{ 0x2282, "sub", "" },
1880{ 0x2283, "sup", "" },
1881{ 0x2284, "nsub", "NOT A SUBSET OF" },
1882{ 0x2285, "nsup", "NOT A SUPERSET OF" },
1883{ 0x2286, "subE", "" },
1884{ 0x2286, "sube", "" },
1885{ 0x2287, "supE", "" },
1886{ 0x2287, "supe", "" },
1887{ 0x2288, "nsubE", "" },
1888{ 0x2288, "nsube", "" },
1889{ 0x2289, "nsupE", "" },
1890{ 0x2289, "nsupe", "" },
1891{ 0x228A, "subne", "" },
1892{ 0x228A, "subnE", "SUBSET OF WITH NOT EQUAL TO" },
1893{ 0x228A, "vsubne", "SUBSET OF WITH NOT EQUAL TO" },
1894{ 0x228B, "supnE", "" },
1895{ 0x228B, "supne", "" },
1896{ 0x228B, "vsupnE", "SUPERSET OF WITH NOT EQUAL TO" },
1897{ 0x228B, "vsupne", "SUPERSET OF WITH NOT EQUAL TO" },
1898{ 0x228E, "uplus", "MULTISET UNION" },
1899{ 0x228F, "sqsub", "" },
1900{ 0x2290, "sqsup", "" },
1901{ 0x2291, "sqsube", "" },
1902{ 0x2292, "sqsupe", "" },
1903{ 0x2293, "sqcap", "SQUARE CAP" },
1904{ 0x2294, "sqcup", "SQUARE CUP" },
1905{ 0x2295, "oplus", "CIRCLED PLUS" },
1906{ 0x2296, "ominus", "CIRCLED MINUS" },
1907{ 0x2297, "otimes", "CIRCLED TIMES" },
1908{ 0x2298, "osol", "CIRCLED DIVISION SLASH" },
1909{ 0x2299, "odot", "CIRCLED DOT OPERATOR" },
1910{ 0x229A, "ocir", "CIRCLED RING OPERATOR" },
1911{ 0x229B, "oast", "CIRCLED ASTERISK OPERATOR" },
1912{ 0x229D, "odash", "CIRCLED DASH" },
1913{ 0x229E, "plusb", "SQUARED PLUS" },
1914{ 0x229F, "minusb", "SQUARED MINUS" },
1915{ 0x22A0, "timesb", "SQUARED TIMES" },
1916{ 0x22A1, "sdotb", "SQUARED DOT OPERATOR" },
1917{ 0x22A2, "vdash", "" },
1918{ 0x22A3, "dashv", "" },
1919{ 0x22A4, "top", "DOWN TACK" },
1920{ 0x22A5, "bottom", "" },
1921{ 0x22A5, "perp", "" },
1922{ 0x22A7, "models", "MODELS" },
1923{ 0x22A8, "vDash", "" },
1924{ 0x22A9, "Vdash", "" },
1925{ 0x22AA, "Vvdash", "" },
1926{ 0x22AC, "nvdash", "DOES NOT PROVE" },
1927{ 0x22AD, "nvDash", "NOT TRUE" },
1928{ 0x22AE, "nVdash", "DOES NOT FORCE" },
1929{ 0x22AF, "nVDash", "NEGATED DOUBLE VERTICAL BAR DOUBLE RIGHT TURNSTILE" },
1930{ 0x22B2, "vltri", "" },
1931{ 0x22B3, "vrtri", "" },
1932{ 0x22B4, "ltrie", "" },
1933{ 0x22B5, "rtrie", "" },
1934{ 0x22B8, "mumap", "MULTIMAP" },
1935{ 0x22BA, "intcal", "INTERCALATE" },
1936{ 0x22BB, "veebar", "" },
1937{ 0x22BC, "barwed", "NAND" },
1938{ 0x22C4, "diam", "DIAMOND OPERATOR" },
1939{ 0x22C5, "sdot", "DOT OPERATOR" },
1940{ 0x22C6, "sstarf", "STAR OPERATOR" },
1941{ 0x22C6, "star", "STAR OPERATOR" },
1942{ 0x22C7, "divonx", "DIVISION TIMES" },
1943{ 0x22C8, "bowtie", "" },
1944{ 0x22C9, "ltimes", "LEFT NORMAL FACTOR SEMIDIRECT PRODUCT" },
1945{ 0x22CA, "rtimes", "RIGHT NORMAL FACTOR SEMIDIRECT PRODUCT" },
1946{ 0x22CB, "lthree", "LEFT SEMIDIRECT PRODUCT" },
1947{ 0x22CC, "rthree", "RIGHT SEMIDIRECT PRODUCT" },
1948{ 0x22CD, "bsime", "" },
1949{ 0x22CE, "cuvee", "CURLY LOGICAL OR" },
1950{ 0x22CF, "cuwed", "CURLY LOGICAL AND" },
1951{ 0x22D0, "Sub", "" },
1952{ 0x22D1, "Sup", "" },
1953{ 0x22D2, "Cap", "DOUBLE INTERSECTION" },
1954{ 0x22D3, "Cup", "DOUBLE UNION" },
1955{ 0x22D4, "fork", "" },
1956{ 0x22D6, "ldot", "" },
1957{ 0x22D7, "gsdot", "" },
1958{ 0x22D8, "Ll", "" },
1959{ 0x22D9, "Gg", "VERY MUCH GREATER-THAN" },
1960{ 0x22DA, "lEg", "" },
1961{ 0x22DA, "leg", "" },
1962{ 0x22DB, "gEl", "" },
1963{ 0x22DB, "gel", "" },
1964{ 0x22DC, "els", "" },
1965{ 0x22DD, "egs", "" },
1966{ 0x22DE, "cuepr", "" },
1967{ 0x22DF, "cuesc", "" },
1968{ 0x22E0, "npre", "DOES NOT PRECEDE OR EQUAL" },
1969{ 0x22E1, "nsce", "DOES NOT SUCCEED OR EQUAL" },
1970{ 0x22E6, "lnsim", "" },
1971{ 0x22E7, "gnsim", "GREATER-THAN BUT NOT EQUIVALENT TO" },
1972{ 0x22E8, "prnap", "" },
1973{ 0x22E8, "prnsim", "" },
1974{ 0x22E9, "scnap", "" },
1975{ 0x22E9, "scnsim", "" },
1976{ 0x22EA, "nltri", "NOT NORMAL SUBGROUP OF" },
1977{ 0x22EB, "nrtri", "DOES NOT CONTAIN AS NORMAL SUBGROUP" },
1978{ 0x22EC, "nltrie", "NOT NORMAL SUBGROUP OF OR EQUAL TO" },
1979{ 0x22ED, "nrtrie", "DOES NOT CONTAIN AS NORMAL SUBGROUP OR EQUAL" },
1980{ 0x22EE, "vellip", "" },
1981{ 0x2306, "Barwed", "PERSPECTIVE" },
1982{ 0x2308, "lceil", "LEFT CEILING" },
1983{ 0x2309, "rceil", "RIGHT CEILING" },
1984{ 0x230A, "lfloor", "LEFT FLOOR" },
1985{ 0x230B, "rfloor", "RIGHT FLOOR" },
1986{ 0x230C, "drcrop", "BOTTOM RIGHT CROP" },
1987{ 0x230D, "dlcrop", "BOTTOM LEFT CROP" },
1988{ 0x230E, "urcrop", "TOP RIGHT CROP" },
1989{ 0x230F, "ulcrop", "TOP LEFT CROP" },
1990{ 0x2315, "telrec", "TELEPHONE RECORDER" },
1991{ 0x2316, "target", "POSITION INDICATOR" },
1992{ 0x231C, "ulcorn", "TOP LEFT CORNER" },
1993{ 0x231D, "urcorn", "TOP RIGHT CORNER" },
1994{ 0x231E, "dlcorn", "BOTTOM LEFT CORNER" },
1995{ 0x231F, "drcorn", "BOTTOM RIGHT CORNER" },
1996{ 0x2322, "frown", "" },
1997{ 0x2322, "sfrown", "FROWN" },
1998{ 0x2323, "smile", "" },
1999{ 0x2323, "ssmile", "SMILE" },
2000{ 0x2423, "blank", "OPEN BOX" },
2001{ 0x24C8, "oS", "CIRCLED LATIN CAPITAL LETTER S" },
2002{ 0x2500, "boxh", "BOX DRAWINGS LIGHT HORIZONTAL" },
2003{ 0x2502, "boxv", "BOX DRAWINGS LIGHT VERTICAL" },
2004{ 0x250C, "boxdr", "BOX DRAWINGS LIGHT DOWN AND RIGHT" },
2005{ 0x2510, "boxdl", "BOX DRAWINGS LIGHT DOWN AND LEFT" },
2006{ 0x2514, "boxur", "BOX DRAWINGS LIGHT UP AND RIGHT" },
2007{ 0x2518, "boxul", "BOX DRAWINGS LIGHT UP AND LEFT" },
2008{ 0x251C, "boxvr", "BOX DRAWINGS LIGHT VERTICAL AND RIGHT" },
2009{ 0x2524, "boxvl", "BOX DRAWINGS LIGHT VERTICAL AND LEFT" },
2010{ 0x252C, "boxhd", "BOX DRAWINGS LIGHT DOWN AND HORIZONTAL" },
2011{ 0x2534, "boxhu", "BOX DRAWINGS LIGHT UP AND HORIZONTAL" },
2012{ 0x253C, "boxvh", "BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL" },
2013{ 0x2550, "boxH", "BOX DRAWINGS DOUBLE HORIZONTAL" },
2014{ 0x2551, "boxV", "BOX DRAWINGS DOUBLE VERTICAL" },
2015{ 0x2552, "boxDR", "BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE" },
2016{ 0x2553, "boxDr", "BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE" },
2017{ 0x2554, "boxdR", "BOX DRAWINGS DOUBLE DOWN AND RIGHT" },
2018{ 0x2555, "boxDL", "BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE" },
2019{ 0x2556, "boxdL", "BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE" },
2020{ 0x2557, "boxDl", "BOX DRAWINGS DOUBLE DOWN AND LEFT" },
2021{ 0x2558, "boxUR", "BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE" },
2022{ 0x2559, "boxuR", "BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE" },
2023{ 0x255A, "boxUr", "BOX DRAWINGS DOUBLE UP AND RIGHT" },
2024{ 0x255B, "boxUL", "BOX DRAWINGS UP SINGLE AND LEFT DOUBLE" },
2025{ 0x255C, "boxUl", "BOX DRAWINGS UP DOUBLE AND LEFT SINGLE" },
2026{ 0x255D, "boxuL", "BOX DRAWINGS DOUBLE UP AND LEFT" },
2027{ 0x255E, "boxvR", "BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE" },
2028{ 0x255F, "boxVR", "BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE" },
2029{ 0x2560, "boxVr", "BOX DRAWINGS DOUBLE VERTICAL AND RIGHT" },
2030{ 0x2561, "boxvL", "BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE" },
2031{ 0x2562, "boxVL", "BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE" },
2032{ 0x2563, "boxVl", "BOX DRAWINGS DOUBLE VERTICAL AND LEFT" },
2033{ 0x2564, "boxhD", "BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE" },
2034{ 0x2565, "boxHD", "BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE" },
2035{ 0x2566, "boxHd", "BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL" },
2036{ 0x2567, "boxhU", "BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE" },
2037{ 0x2568, "boxHU", "BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE" },
2038{ 0x2569, "boxHu", "BOX DRAWINGS DOUBLE UP AND HORIZONTAL" },
2039{ 0x256A, "boxvH", "BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE" },
2040{ 0x256B, "boxVH", "BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE" },
2041{ 0x256C, "boxVh", "BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL" },
2042{ 0x2580, "uhblk", "UPPER HALF BLOCK" },
2043{ 0x2584, "lhblk", "LOWER HALF BLOCK" },
2044{ 0x2588, "block", "FULL BLOCK" },
2045{ 0x2591, "blk14", "LIGHT SHADE" },
2046{ 0x2592, "blk12", "MEDIUM SHADE" },
2047{ 0x2593, "blk34", "DARK SHADE" },
2048{ 0x25A1, "square", "WHITE SQUARE" },
2049{ 0x25A1, "squ", "WHITE SQUARE" },
2050{ 0x25AA, "squf", "" },
2051{ 0x25AD, "rect", "WHITE RECTANGLE" },
2052{ 0x25AE, "marker", "BLACK VERTICAL RECTANGLE" },
2053{ 0x25B3, "xutri", "WHITE UP-POINTING TRIANGLE" },
2054{ 0x25B4, "utrif", "BLACK UP-POINTING TRIANGLE" },
2055{ 0x25B5, "utri", "WHITE UP-POINTING TRIANGLE" },
2056{ 0x25B8, "rtrif", "BLACK RIGHT-POINTING TRIANGLE" },
2057{ 0x25B9, "rtri", "WHITE RIGHT-POINTING TRIANGLE" },
2058{ 0x25BD, "xdtri", "WHITE DOWN-POINTING TRIANGLE" },
2059{ 0x25BE, "dtrif", "BLACK DOWN-POINTING TRIANGLE" },
2060{ 0x25BF, "dtri", "WHITE DOWN-POINTING TRIANGLE" },
2061{ 0x25C2, "ltrif", "BLACK LEFT-POINTING TRIANGLE" },
2062{ 0x25C3, "ltri", "WHITE LEFT-POINTING TRIANGLE" },
2063{ 0x25CA, "loz", "LOZENGE" },
2064{ 0x25CB, "cir", "WHITE CIRCLE" },
2065{ 0x25CB, "xcirc", "WHITE CIRCLE" },
2066{ 0x2605, "starf", "BLACK STAR" },
2067{ 0x260E, "phone", "TELEPHONE SIGN" },
2068{ 0x2640, "female", "" },
2069{ 0x2642, "male", "MALE SIGN" },
2070{ 0x2660, "spades", "BLACK SPADE SUIT" },
2071{ 0x2663, "clubs", "BLACK CLUB SUIT" },
2072{ 0x2665, "hearts", "BLACK HEART SUIT" },
2073{ 0x2666, "diams", "BLACK DIAMOND SUIT" },
2074{ 0x2669, "sung", "" },
2075{ 0x266D, "flat", "MUSIC FLAT SIGN" },
2076{ 0x266E, "natur", "MUSIC NATURAL SIGN" },
2077{ 0x266F, "sharp", "MUSIC SHARP SIGN" },
2078{ 0x2713, "check", "CHECK MARK" },
2079{ 0x2717, "cross", "BALLOT X" },
2080{ 0x2720, "malt", "MALTESE CROSS" },
2081{ 0x2726, "lozf", "" },
2082{ 0x2736, "sext", "SIX POINTED BLACK STAR" },
2083{ 0x3008, "lang", "" },
2084{ 0x3009, "rang", "" },
2085{ 0xE291, "rpargt", "" },
2086{ 0xE2A2, "lnap", "" },
2087{ 0xE2AA, "nsmid", "" },
2088{ 0xE2B3, "prnE", "" },
2089{ 0xE2B5, "scnE", "" },
2090{ 0xE2B8, "vsubnE", "" },
2091{ 0xE301, "smid", "" },
2092{ 0xE411, "gnap", "" },
2093{ 0xFB00, "fflig", "" },
2094{ 0xFB01, "filig", "" },
2095{ 0xFB02, "fllig", "" },
2096{ 0xFB03, "ffilig", "" },
2097{ 0xFB04, "ffllig", "" },
2098{ 0xFE68, "sbsol", "SMALL REVERSE SOLIDUS" },
2099};
2100
2101/************************************************************************
2102 *                                                                     *
2103 *             Commodity functions to handle entities                  *
2104 *                                                                     *
2105 ************************************************************************/
2106
2107/*
2108 * Macro used to grow the current buffer.
2109 */
2110#define growBuffer(buffer) {                                           \
2111    buffer##_size *= 2;                                                        \
2112    buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar));  \
2113    if (buffer == NULL) {                                              \
2114       perror("realloc failed");                                       \
2115       return(NULL);                                                   \
2116    }                                                                  \
2117}
2118
2119/**
2120 * docbEntityLookup:
2121 * @name: the entity name
2122 *
2123 * Lookup the given entity in EntitiesTable
2124 *
2125 * TODO: the linear scan is really ugly, an hash table is really needed.
2126 *
2127 * Returns the associated docbEntityDescPtr if found, NULL otherwise.
2128 */
2129static docbEntityDescPtr
2130docbEntityLookup(const xmlChar *name) {
2131    unsigned int i;
2132
2133    for (i = 0;i < (sizeof(docbookEntitiesTable)/
2134                    sizeof(docbookEntitiesTable[0]));i++) {
2135        if (xmlStrEqual(name, BAD_CAST docbookEntitiesTable[i].name)) {
2136#ifdef DEBUG
2137            xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", name);
2138#endif
2139            return(&docbookEntitiesTable[i]);
2140       }
2141    }
2142    return(NULL);
2143}
2144
2145/**
2146 * docbEntityValueLookup:
2147 * @value: the entity's unicode value
2148 *
2149 * Lookup the given entity in EntitiesTable
2150 *
2151 * TODO: the linear scan is really ugly, an hash table is really needed.
2152 *
2153 * Returns the associated docbEntityDescPtr if found, NULL otherwise.
2154 */
2155static docbEntityDescPtr
2156docbEntityValueLookup(int value) {
2157    unsigned int i;
2158#ifdef DEBUG
2159    int lv = 0;
2160#endif
2161
2162    for (i = 0;i < (sizeof(docbookEntitiesTable)/
2163                    sizeof(docbookEntitiesTable[0]));i++) {
2164        if (docbookEntitiesTable[i].value >= value) {
2165           if (docbookEntitiesTable[i].value > value)
2166               break;
2167#ifdef DEBUG
2168           xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", docbookEntitiesTable[i].name);
2169#endif
2170            return(&docbookEntitiesTable[i]);
2171       }
2172#ifdef DEBUG
2173       if (lv > docbookEntitiesTable[i].value) {
2174           xmlGenericError(xmlGenericErrorContext,
2175                   "docbookEntitiesTable[] is not sorted (%d > %d)!\n",
2176                   lv, docbookEntitiesTable[i].value);
2177       }
2178       lv = docbookEntitiesTable[i].value;
2179#endif
2180    }
2181    return(NULL);
2182}
2183
2184#if 0
2185/**
2186 * UTF8ToSgml:
2187 * @out:  a pointer to an array of bytes to store the result
2188 * @outlen:  the length of @out
2189 * @in:  a pointer to an array of UTF-8 chars
2190 * @inlen:  the length of @in
2191 *
2192 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2193 * plus SGML entities block of chars out.
2194 *
2195 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2196 * The value of @inlen after return is the number of octets consumed
2197 *     as the return value is positive, else unpredictiable.
2198 * The value of @outlen after return is the number of octets consumed.
2199 */
2200int
2201UTF8ToSgml(unsigned char* out, int *outlen,
2202           const unsigned char* in, int *inlen) {
2203    const unsigned char* processed = in;
2204    const unsigned char* outend;
2205    const unsigned char* outstart = out;
2206    const unsigned char* instart = in;
2207    const unsigned char* inend;
2208    unsigned int c, d;
2209    int trailing;
2210
2211    if (in == NULL) {
2212        /*
2213        * initialization nothing to do
2214        */
2215       *outlen = 0;
2216       *inlen = 0;
2217       return(0);
2218    }
2219    inend = in + (*inlen);
2220    outend = out + (*outlen);
2221    while (in < inend) {
2222       d = *in++;
2223       if      (d < 0x80)  { c= d; trailing= 0; }
2224       else if (d < 0xC0) {
2225           /* trailing byte in leading position */
2226           *outlen = out - outstart;
2227           *inlen = processed - instart;
2228           return(-2);
2229        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2230        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2231        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2232       else {
2233           /* no chance for this in Ascii */
2234           *outlen = out - outstart;
2235           *inlen = processed - instart;
2236           return(-2);
2237       }
2238
2239       if (inend - in < trailing) {
2240           break;
2241       }
2242
2243       for ( ; trailing; trailing--) {
2244           if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
2245               break;
2246           c <<= 6;
2247           c |= d & 0x3F;
2248       }
2249
2250       /* assertion: c is a single UTF-4 value */
2251       if (c < 0x80) {
2252           if (out + 1 >= outend)
2253               break;
2254           *out++ = c;
2255       } else {
2256           int len;
2257           docbEntityDescPtr ent;
2258
2259           /*
2260            * Try to lookup a predefined SGML entity for it
2261            */
2262
2263           ent = docbEntityValueLookup(c);
2264           if (ent == NULL) {
2265               /* no chance for this in Ascii */
2266               *outlen = out - outstart;
2267               *inlen = processed - instart;
2268               return(-2);
2269           }
2270           len = strlen(ent->name);
2271           if (out + 2 + len >= outend)
2272               break;
2273           *out++ = '&';
2274           memcpy(out, ent->name, len);
2275           out += len;
2276           *out++ = ';';
2277       }
2278       processed = in;
2279    }
2280    *outlen = out - outstart;
2281    *inlen = processed - instart;
2282    return(0);
2283}
2284#endif
2285
2286/**
2287 * docbEncodeEntities:
2288 * @out:  a pointer to an array of bytes to store the result
2289 * @outlen:  the length of @out
2290 * @in:  a pointer to an array of UTF-8 chars
2291 * @inlen:  the length of @in
2292 * @quoteChar: the quote character to escape (' or ") or zero.
2293 *
2294 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2295 * plus SGML entities block of chars out.
2296 *
2297 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2298 * The value of @inlen after return is the number of octets consumed
2299 *     as the return value is positive, else unpredictiable.
2300 * The value of @outlen after return is the number of octets consumed.
2301 */
2302int
2303docbEncodeEntities(unsigned char* out, int *outlen,
2304                  const unsigned char* in, int *inlen, int quoteChar) {
2305    const unsigned char* processed = in;
2306    const unsigned char* outend = out + (*outlen);
2307    const unsigned char* outstart = out;
2308    const unsigned char* instart = in;
2309    const unsigned char* inend = in + (*inlen);
2310    unsigned int c, d;
2311    int trailing;
2312
2313    while (in < inend) {
2314       d = *in++;
2315       if      (d < 0x80)  { c= d; trailing= 0; }
2316       else if (d < 0xC0) {
2317           /* trailing byte in leading position */
2318           *outlen = out - outstart;
2319           *inlen = processed - instart;
2320           return(-2);
2321        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2322        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2323        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2324       else {
2325           /* no chance for this in Ascii */
2326           *outlen = out - outstart;
2327           *inlen = processed - instart;
2328           return(-2);
2329       }
2330
2331       if (inend - in < trailing)
2332           break;
2333
2334       while (trailing--) {
2335           if (((d= *in++) & 0xC0) != 0x80) {
2336               *outlen = out - outstart;
2337               *inlen = processed - instart;
2338               return(-2);
2339           }
2340           c <<= 6;
2341           c |= d & 0x3F;
2342       }
2343
2344       /* assertion: c is a single UTF-4 value */
2345       if (c < 0x80 && c != (unsigned int) quoteChar && c != '&' && c != '<' && c != '>') {
2346           if (out >= outend)
2347               break;
2348           *out++ = c;
2349       } else {
2350           docbEntityDescPtr ent;
2351           const char *cp;
2352           char nbuf[16];
2353           int len;
2354
2355           /*
2356            * Try to lookup a predefined SGML entity for it
2357            */
2358           ent = docbEntityValueLookup(c);
2359           if (ent == NULL) {
2360               sprintf(nbuf, "#%u", c);
2361               cp = nbuf;
2362           }
2363           else
2364               cp = ent->name;
2365           len = strlen(cp);
2366           if (out + 2 + len > outend)
2367               break;
2368           *out++ = '&';
2369           memcpy(out, cp, len);
2370           out += len;
2371           *out++ = ';';
2372       }
2373       processed = in;
2374    }
2375    *outlen = out - outstart;
2376    *inlen = processed - instart;
2377    return(0);
2378}
2379
2380
2381/************************************************************************
2382 *                                                                     *
2383 *             Commodity functions to handle streams                   *
2384 *                                                                     *
2385 ************************************************************************/
2386
2387/**
2388 * docbNewInputStream:
2389 * @ctxt:  an SGML parser context
2390 *
2391 * Create a new input stream structure
2392 * Returns the new input stream or NULL
2393 */
2394static docbParserInputPtr
2395docbNewInputStream(docbParserCtxtPtr ctxt) {
2396    docbParserInputPtr input;
2397
2398    input = (xmlParserInputPtr) xmlMalloc(sizeof(docbParserInput));
2399    if (input == NULL) {
2400        ctxt->errNo = XML_ERR_NO_MEMORY;
2401       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2402           ctxt->sax->error(ctxt->userData,
2403                            "malloc: couldn't allocate a new input stream\n");
2404       return(NULL);
2405    }
2406    memset(input, 0, sizeof(docbParserInput));
2407    input->filename = NULL;
2408    input->directory = NULL;
2409    input->base = NULL;
2410    input->cur = NULL;
2411    input->buf = NULL;
2412    input->line = 1;
2413    input->col = 1;
2414    input->buf = NULL;
2415    input->free = NULL;
2416    input->version = NULL;
2417    input->consumed = 0;
2418    input->length = 0;
2419    return(input);
2420}
2421
2422
2423/************************************************************************
2424 *                                                                     *
2425 *             Commodity functions, cleanup needed ?                   *
2426 *                                                                     *
2427 ************************************************************************/
2428
2429/**
2430 * areBlanks:
2431 * @ctxt:  an SGML parser context
2432 * @str:  a xmlChar *
2433 * @len:  the size of @str
2434 *
2435 * Is this a sequence of blank chars that one can ignore ?
2436 *
2437 * Returns 1 if ignorable 0 otherwise.
2438 */
2439
2440static int areBlanks(docbParserCtxtPtr ctxt, const xmlChar *str, int len) {
2441    int i;
2442    xmlNodePtr lastChild;
2443
2444    for (i = 0;i < len;i++)
2445        if (!(IS_BLANK(str[i]))) return(0);
2446
2447    if (CUR == 0) return(1);
2448    if (CUR != '<') return(0);
2449    if (ctxt->name == NULL)
2450       return(1);
2451    if (ctxt->node == NULL) return(0);
2452    lastChild = xmlGetLastChild(ctxt->node);
2453    if (lastChild == NULL) {
2454        if (ctxt->node->content != NULL) return(0);
2455    } else if (xmlNodeIsText(lastChild))
2456        return(0);
2457    return(1);
2458}
2459
2460/************************************************************************
2461 *                                                                     *
2462 *                     The parser itself                               *
2463 *                                                                     *
2464 ************************************************************************/
2465
2466/**
2467 * docbParseSGMLName:
2468 * @ctxt:  an SGML parser context
2469 *
2470 * parse an SGML tag or attribute name, note that we convert it to lowercase
2471 * since SGML names are not case-sensitive.
2472 *
2473 * Returns the Tag Name parsed or NULL
2474 */
2475
2476static xmlChar *
2477docbParseSGMLName(docbParserCtxtPtr ctxt) {
2478    xmlChar *ret = NULL;
2479    int i = 0;
2480    xmlChar loc[DOCB_PARSER_BUFFER_SIZE];
2481
2482    if (!IS_LETTER(CUR) && (CUR != '_') &&
2483        (CUR != ':')) return(NULL);
2484
2485    while ((i < DOCB_PARSER_BUFFER_SIZE) &&
2486           ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2487          (CUR == ':') || (CUR == '_'))) {
2488       if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2489        else loc[i] = CUR;
2490       i++;
2491
2492       NEXT;
2493    }
2494
2495    ret = xmlStrndup(loc, i);
2496
2497    return(ret);
2498}
2499
2500/**
2501 * docbParseName:
2502 * @ctxt:  an SGML parser context
2503 *
2504 * parse an SGML name, this routine is case sensistive.
2505 *
2506 * Returns the Name parsed or NULL
2507 */
2508
2509static xmlChar *
2510docbParseName(docbParserCtxtPtr ctxt) {
2511    xmlChar buf[DOCB_MAX_NAMELEN];
2512    int len = 0;
2513
2514    GROW;
2515    if (!IS_LETTER(CUR) && (CUR != '_')) {
2516       return(NULL);
2517    }
2518
2519    while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2520           (CUR == '.') || (CUR == '-') ||
2521          (CUR == '_') || (CUR == ':') ||
2522          (IS_COMBINING(CUR)) ||
2523          (IS_EXTENDER(CUR))) {
2524       buf[len++] = CUR;
2525       NEXT;
2526       if (len >= DOCB_MAX_NAMELEN) {
2527           xmlGenericError(xmlGenericErrorContext,
2528              "docbParseName: reached DOCB_MAX_NAMELEN limit\n");
2529           while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2530                  (CUR == '.') || (CUR == '-') ||
2531                  (CUR == '_') || (CUR == ':') ||
2532                  (IS_COMBINING(CUR)) ||
2533                  (IS_EXTENDER(CUR)))
2534                NEXT;
2535           break;
2536       }
2537    }
2538    return(xmlStrndup(buf, len));
2539}
2540
2541/**
2542 * docbParseSGMLAttribute:
2543 * @ctxt:  an SGML parser context
2544 * @stop:  a char stop value
2545 *
2546 * parse an SGML attribute value till the stop (quote), if
2547 * stop is 0 then it stops at the first space
2548 *
2549 * Returns the attribute parsed or NULL
2550 */
2551
2552static xmlChar *
2553docbParseSGMLAttribute(docbParserCtxtPtr ctxt, const xmlChar stop) {
2554    xmlChar *buffer = NULL;
2555    int buffer_size = 0;
2556    xmlChar *out = NULL;
2557    xmlChar *name = NULL;
2558
2559    xmlChar *cur = NULL;
2560    docbEntityDescPtr ent;
2561
2562    /*
2563     * allocate a translation buffer.
2564     */
2565    buffer_size = DOCB_PARSER_BIG_BUFFER_SIZE;
2566    buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
2567    if (buffer == NULL) {
2568       perror("docbParseSGMLAttribute: malloc failed");
2569       return(NULL);
2570    }
2571    out = buffer;
2572
2573    /*
2574     * Ok loop until we reach one of the ending chars
2575     */
2576    while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
2577       if ((stop == 0) && (IS_BLANK(CUR))) break;
2578        if (CUR == '&') {
2579           if (NXT(1) == '#') {
2580               unsigned int c;
2581               int bits;
2582
2583               c = docbParseCharRef(ctxt);
2584               if      (c <    0x80)
2585                       { *out++  = c;                bits= -6; }
2586               else if (c <   0x800)
2587                       { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2588               else if (c < 0x10000)
2589                       { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2590               else
2591                       { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2592
2593               for ( ; bits >= 0; bits-= 6) {
2594                   *out++  = ((c >> bits) & 0x3F) | 0x80;
2595               }
2596           } else {
2597               ent = docbParseEntityRef(ctxt, &name);
2598               if (name == NULL) {
2599                   *out++ = '&';
2600                   if (out - buffer > buffer_size - 100) {
2601                       int indx = out - buffer;
2602
2603                       growBuffer(buffer);
2604                       out = &buffer[indx];
2605                   }
2606               } else if (ent == NULL) {
2607                   *out++ = '&';
2608                   cur = name;
2609                   while (*cur != 0) {
2610                       if (out - buffer > buffer_size - 100) {
2611                           int indx = out - buffer;
2612
2613                           growBuffer(buffer);
2614                           out = &buffer[indx];
2615                       }
2616                       *out++ = *cur++;
2617                   }
2618                   xmlFree(name);
2619               } else {
2620                   unsigned int c;
2621                   int bits;
2622
2623                   if (out - buffer > buffer_size - 100) {
2624                       int indx = out - buffer;
2625
2626                       growBuffer(buffer);
2627                       out = &buffer[indx];
2628                   }
2629                   c = (xmlChar)ent->value;
2630                   if      (c <    0x80)
2631                       { *out++  = c;                bits= -6; }
2632                   else if (c <   0x800)
2633                       { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2634                   else if (c < 0x10000)
2635                       { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2636                   else
2637                       { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2638
2639                   for ( ; bits >= 0; bits-= 6) {
2640                       *out++  = ((c >> bits) & 0x3F) | 0x80;
2641                   }
2642                   xmlFree(name);
2643               }
2644           }
2645       } else {
2646           unsigned int c;
2647           int bits;
2648
2649           if (out - buffer > buffer_size - 100) {
2650               int indx = out - buffer;
2651
2652               growBuffer(buffer);
2653               out = &buffer[indx];
2654           }
2655           c = CUR;
2656           if      (c <    0x80)
2657                   { *out++  = c;                bits= -6; }
2658           else if (c <   0x800)
2659                   { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2660           else if (c < 0x10000)
2661                   { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2662           else
2663                   { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2664
2665           for ( ; bits >= 0; bits-= 6) {
2666               *out++  = ((c >> bits) & 0x3F) | 0x80;
2667           }
2668           NEXT;
2669       }
2670    }
2671    *out++ = 0;
2672    return(buffer);
2673}
2674
2675
2676/**
2677 * docbParseEntityRef:
2678 * @ctxt:  an SGML parser context
2679 * @str:  location to store the entity name
2680 *
2681 * parse an SGML ENTITY references
2682 *
2683 * [68] EntityRef ::= '&' Name ';'
2684 *
2685 * Returns the associated docbEntityDescPtr if found, or NULL otherwise,
2686 *         if non-NULL *str will have to be freed by the caller.
2687 */
2688static docbEntityDescPtr
2689docbParseEntityRef(docbParserCtxtPtr ctxt, xmlChar **str) {
2690    xmlChar *name;
2691    docbEntityDescPtr ent = NULL;
2692    *str = NULL;
2693
2694    if (CUR == '&') {
2695        NEXT;
2696        name = docbParseName(ctxt);
2697       if (name == NULL) {
2698           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2699               ctxt->sax->error(ctxt->userData, "docbParseEntityRef: no name\n");
2700           ctxt->wellFormed = 0;
2701       } else {
2702           GROW;
2703           if (CUR == ';') {
2704               *str = name;
2705
2706               /*
2707                * Lookup the entity in the table.
2708                */
2709               ent = docbEntityLookup(name);
2710               if (ent != NULL) /* OK that's ugly !!! */
2711                   NEXT;
2712           } else {
2713               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2714                   ctxt->sax->error(ctxt->userData,
2715                                    "docbParseEntityRef: expecting ';'\n");
2716               *str = name;
2717           }
2718       }
2719    }
2720    return(ent);
2721}
2722
2723/**
2724 * docbParseAttValue:
2725 * @ctxt:  an SGML parser context
2726 *
2727 * parse a value for an attribute
2728 * Note: the parser won't do substitution of entities here, this
2729 * will be handled later in xmlStringGetNodeList, unless it was
2730 * asked for ctxt->replaceEntities != 0
2731 *
2732 * Returns the AttValue parsed or NULL.
2733 */
2734
2735static xmlChar *
2736docbParseAttValue(docbParserCtxtPtr ctxt) {
2737    xmlChar *ret = NULL;
2738
2739    if (CUR == '"') {
2740        NEXT;
2741       ret = docbParseSGMLAttribute(ctxt, '"');
2742        if (CUR != '"') {
2743           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2744               ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2745           ctxt->wellFormed = 0;
2746       } else
2747           NEXT;
2748    } else if (CUR == '\'') {
2749        NEXT;
2750       ret = docbParseSGMLAttribute(ctxt, '\'');
2751        if (CUR != '\'') {
2752           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2753               ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2754           ctxt->wellFormed = 0;
2755       } else
2756           NEXT;
2757    } else {
2758        /*
2759        * That's an SGMLism, the attribute value may not be quoted
2760        */
2761       ret = docbParseSGMLAttribute(ctxt, 0);
2762       if (ret == NULL) {
2763           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2764               ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
2765           ctxt->wellFormed = 0;
2766       }
2767    }
2768    return(ret);
2769}
2770
2771/**
2772 * docbParseSystemLiteral:
2773 * @ctxt:  an SGML parser context
2774 *
2775 * parse an SGML Literal
2776 *
2777 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2778 *
2779 * Returns the SystemLiteral parsed or NULL
2780 */
2781
2782static xmlChar *
2783docbParseSystemLiteral(docbParserCtxtPtr ctxt) {
2784    const xmlChar *q;
2785    xmlChar *ret = NULL;
2786
2787    if (CUR == '"') {
2788        NEXT;
2789       q = CUR_PTR;
2790       while ((IS_CHAR(CUR)) && (CUR != '"'))
2791           NEXT;
2792       if (!IS_CHAR(CUR)) {
2793           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2794               ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2795           ctxt->wellFormed = 0;
2796       } else {
2797           ret = xmlStrndup(q, CUR_PTR - q);
2798           NEXT;
2799        }
2800    } else if (CUR == '\'') {
2801        NEXT;
2802       q = CUR_PTR;
2803       while ((IS_CHAR(CUR)) && (CUR != '\''))
2804           NEXT;
2805       if (!IS_CHAR(CUR)) {
2806           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2807               ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2808           ctxt->wellFormed = 0;
2809       } else {
2810           ret = xmlStrndup(q, CUR_PTR - q);
2811           NEXT;
2812        }
2813    } else {
2814       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2815           ctxt->sax->error(ctxt->userData,
2816                            "SystemLiteral \" or ' expected\n");
2817       ctxt->wellFormed = 0;
2818    }
2819
2820    return(ret);
2821}
2822
2823/**
2824 * docbParsePubidLiteral:
2825 * @ctxt:  an SGML parser context
2826 *
2827 * parse an SGML public literal
2828 *
2829 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2830 *
2831 * Returns the PubidLiteral parsed or NULL.
2832 */
2833
2834static xmlChar *
2835docbParsePubidLiteral(docbParserCtxtPtr ctxt) {
2836    const xmlChar *q;
2837    xmlChar *ret = NULL;
2838    /*
2839     * Name ::= (Letter | '_') (NameChar)*
2840     */
2841    if (CUR == '"') {
2842        NEXT;
2843       q = CUR_PTR;
2844       while (IS_PUBIDCHAR(CUR)) NEXT;
2845       if (CUR != '"') {
2846           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2847               ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2848           ctxt->wellFormed = 0;
2849       } else {
2850           ret = xmlStrndup(q, CUR_PTR - q);
2851           NEXT;
2852       }
2853    } else if (CUR == '\'') {
2854        NEXT;
2855       q = CUR_PTR;
2856       while ((IS_LETTER(CUR)) && (CUR != '\''))
2857           NEXT;
2858       if (!IS_LETTER(CUR)) {
2859           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2860               ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2861           ctxt->wellFormed = 0;
2862       } else {
2863           ret = xmlStrndup(q, CUR_PTR - q);
2864           NEXT;
2865       }
2866    } else {
2867       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2868           ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
2869       ctxt->wellFormed = 0;
2870    }
2871
2872    return(ret);
2873}
2874
2875/**
2876 * docbParseCharData:
2877 * @ctxt:  an SGML parser context
2878 * @cdata:  int indicating whether we are within a CDATA section
2879 *
2880 * parse a CharData section.
2881 * if we are within a CDATA section ']]>' marks an end of section.
2882 *
2883 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2884 */
2885
2886static void
2887docbParseCharData(docbParserCtxtPtr ctxt) {
2888    xmlChar buf[DOCB_PARSER_BIG_BUFFER_SIZE + 5];
2889    int nbchar = 0;
2890    int cur, l;
2891
2892    SHRINK;
2893    cur = CUR_CHAR(l);
2894    while (((cur != '<') || (ctxt->token == '<')) &&
2895           ((cur != '&') || (ctxt->token == '&')) &&
2896          (IS_CHAR(cur))) {
2897       COPY_BUF(l,buf,nbchar,cur);
2898       if (nbchar >= DOCB_PARSER_BIG_BUFFER_SIZE) {
2899           /*
2900            * Ok the segment is to be consumed as chars.
2901            */
2902           if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2903               if (areBlanks(ctxt, buf, nbchar)) {
2904                   if (ctxt->sax->ignorableWhitespace != NULL)
2905                       ctxt->sax->ignorableWhitespace(ctxt->userData,
2906                                                      buf, nbchar);
2907               } else {
2908                   docbCheckParagraph(ctxt);
2909                   if (ctxt->sax->characters != NULL)
2910                       ctxt->sax->characters(ctxt->userData, buf, nbchar);
2911               }
2912           }
2913           nbchar = 0;
2914       }
2915       NEXTL(l);
2916       cur = CUR_CHAR(l);
2917    }
2918    if (nbchar != 0) {
2919       /*
2920        * Ok the segment is to be consumed as chars.
2921        */
2922       if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2923           if (areBlanks(ctxt, buf, nbchar)) {
2924               if (ctxt->sax->ignorableWhitespace != NULL)
2925                   ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2926           } else {
2927               docbCheckParagraph(ctxt);
2928               if (ctxt->sax->characters != NULL)
2929                   ctxt->sax->characters(ctxt->userData, buf, nbchar);
2930           }
2931       }
2932    }
2933}
2934
2935/**
2936 * docbParseExternalID:
2937 * @ctxt:  an SGML parser context
2938 * @publicID:  a xmlChar** receiving PubidLiteral
2939 *
2940 * Parse an External ID or a Public ID
2941 *
2942 * Returns the function returns SystemLiteral and in the second
2943 *                case publicID receives PubidLiteral,
2944 *                it is possible to return NULL and have publicID set.
2945 */
2946
2947static xmlChar *
2948docbParseExternalID(docbParserCtxtPtr ctxt, xmlChar **publicID) {
2949    xmlChar *URI = NULL;
2950
2951    if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2952         (UPP(2) == 'S') && (UPP(3) == 'T') &&
2953        (UPP(4) == 'E') && (UPP(5) == 'M')) {
2954        SKIP(6);
2955       if (!IS_BLANK(CUR)) {
2956           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2957               ctxt->sax->error(ctxt->userData,
2958                   "Space required after 'SYSTEM'\n");
2959           ctxt->wellFormed = 0;
2960       }
2961        SKIP_BLANKS;
2962       URI = docbParseSystemLiteral(ctxt);
2963       if (URI == NULL) {
2964           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2965               ctxt->sax->error(ctxt->userData,
2966                 "docbParseExternalID: SYSTEM, no URI\n");
2967           ctxt->wellFormed = 0;
2968        }
2969    } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2970              (UPP(2) == 'B') && (UPP(3) == 'L') &&
2971              (UPP(4) == 'I') && (UPP(5) == 'C')) {
2972        SKIP(6);
2973       if (!IS_BLANK(CUR)) {
2974           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2975               ctxt->sax->error(ctxt->userData,
2976                   "Space required after 'PUBLIC'\n");
2977           ctxt->wellFormed = 0;
2978       }
2979        SKIP_BLANKS;
2980       *publicID = docbParsePubidLiteral(ctxt);
2981       if (*publicID == NULL) {
2982           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2983               ctxt->sax->error(ctxt->userData,
2984                 "docbParseExternalID: PUBLIC, no Public Identifier\n");
2985           ctxt->wellFormed = 0;
2986       }
2987        SKIP_BLANKS;
2988        if ((CUR == '"') || (CUR == '\'')) {
2989           URI = docbParseSystemLiteral(ctxt);
2990       }
2991    }
2992    return(URI);
2993}
2994
2995/**
2996 * docbParseComment:
2997 * @ctxt:  an SGML parser context
2998 *
2999 * Parse an XML (SGML) comment <!-- .... -->
3000 *
3001 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3002 */
3003static void
3004docbParseComment(docbParserCtxtPtr ctxt) {
3005    xmlChar *buf = NULL;
3006    int len;
3007    int size = DOCB_PARSER_BUFFER_SIZE;
3008    int q, ql;
3009    int r, rl;
3010    int cur, l;
3011    xmlParserInputState state;
3012
3013    /*
3014     * Check that there is a comment right here.
3015     */
3016    if ((RAW != '<') || (NXT(1) != '!') ||
3017        (NXT(2) != '-') || (NXT(3) != '-')) return;
3018
3019    state = ctxt->instate;
3020    ctxt->instate = XML_PARSER_COMMENT;
3021    SHRINK;
3022    SKIP(4);
3023    buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
3024    if (buf == NULL) {
3025       xmlGenericError(xmlGenericErrorContext,
3026               "malloc of %d byte failed\n", size);
3027       ctxt->instate = state;
3028       return;
3029    }
3030    q = CUR_CHAR(ql);
3031    NEXTL(ql);
3032    r = CUR_CHAR(rl);
3033    NEXTL(rl);
3034    cur = CUR_CHAR(l);
3035    len = 0;
3036    while (IS_CHAR(cur) &&
3037           ((cur != '>') ||
3038           (r != '-') || (q != '-'))) {
3039       if (len + 5 >= size) {
3040           size *= 2;
3041           buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3042           if (buf == NULL) {
3043               xmlGenericError(xmlGenericErrorContext,
3044                       "realloc of %d byte failed\n", size);
3045               ctxt->instate = state;
3046               return;
3047           }
3048       }
3049       COPY_BUF(ql,buf,len,q);
3050       q = r;
3051       ql = rl;
3052       r = cur;
3053       rl = l;
3054       NEXTL(l);
3055       cur = CUR_CHAR(l);
3056       if (cur == 0) {
3057           SHRINK;
3058           GROW;
3059           cur = CUR_CHAR(l);
3060       }
3061    }
3062    buf[len] = 0;
3063    if (!IS_CHAR(cur)) {
3064       ctxt->errNo = XML_ERR_COMMENT_NOT_FINISHED;
3065       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3066           ctxt->sax->error(ctxt->userData,
3067                            "Comment not terminated \n<!--%.50s\n", buf);
3068       ctxt->wellFormed = 0;
3069       xmlFree(buf);
3070    } else {
3071        NEXT;
3072       if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3073           (!ctxt->disableSAX))
3074           ctxt->sax->comment(ctxt->userData, buf);
3075       xmlFree(buf);
3076    }
3077    ctxt->instate = state;
3078}
3079
3080/**
3081 * docbParseCharRef:
3082 * @ctxt:  an SGML parser context
3083 *
3084 * parse Reference declarations
3085 *
3086 * [66] CharRef ::= '&#' [0-9]+ ';' |
3087 *                  '&#x' [0-9a-fA-F]+ ';'
3088 *
3089 * Returns the value parsed (as an int)
3090 */
3091static int
3092docbParseCharRef(docbParserCtxtPtr ctxt) {
3093    int val = 0;
3094
3095    if ((CUR == '&') && (NXT(1) == '#') &&
3096        (NXT(2) == 'x')) {
3097       SKIP(3);
3098       while (CUR != ';') {
3099           if ((CUR >= '0') && (CUR <= '9'))
3100               val = val * 16 + (CUR - '0');
3101           else if ((CUR >= 'a') && (CUR <= 'f'))
3102               val = val * 16 + (CUR - 'a') + 10;
3103           else if ((CUR >= 'A') && (CUR <= 'F'))
3104               val = val * 16 + (CUR - 'A') + 10;
3105           else {
3106               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3107                   ctxt->sax->error(ctxt->userData,
3108                        "docbParseCharRef: invalid hexadecimal value\n");
3109               ctxt->wellFormed = 0;
3110               val = 0;
3111               break;
3112           }
3113           NEXT;
3114       }
3115       if (CUR == ';')
3116           NEXT;
3117    } else if  ((CUR == '&') && (NXT(1) == '#')) {
3118       SKIP(2);
3119       while (CUR != ';') {
3120           if ((CUR >= '0') && (CUR <= '9'))
3121               val = val * 10 + (CUR - '0');
3122           else {
3123               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3124                   ctxt->sax->error(ctxt->userData,
3125                        "docbParseCharRef: invalid decimal value\n");
3126               ctxt->wellFormed = 0;
3127               val = 0;
3128               break;
3129           }
3130           NEXT;
3131       }
3132       if (CUR == ';')
3133           NEXT;
3134    } else {
3135       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3136           ctxt->sax->error(ctxt->userData, "docbParseCharRef: invalid value\n");
3137       ctxt->wellFormed = 0;
3138    }
3139    /*
3140     * Check the value IS_CHAR ...
3141     */
3142    if (IS_CHAR(val)) {
3143        return(val);
3144    } else {
3145       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3146           ctxt->sax->error(ctxt->userData, "docbParseCharRef: invalid xmlChar value %d\n",
3147                            val);
3148       ctxt->wellFormed = 0;
3149    }
3150    return(0);
3151}
3152
3153
3154/**
3155 * docbParseDocTypeDecl :
3156 * @ctxt:  an SGML parser context
3157 *
3158 * parse a DOCTYPE declaration
3159 *
3160 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3161 *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3162 */
3163
3164static void
3165docbParseDocTypeDecl(docbParserCtxtPtr ctxt) {
3166    xmlChar *name;
3167    xmlChar *ExternalID = NULL;
3168    xmlChar *URI = NULL;
3169
3170    /*
3171     * We know that '<!DOCTYPE' has been detected.
3172     */
3173    SKIP(9);
3174
3175    SKIP_BLANKS;
3176
3177    /*
3178     * Parse the DOCTYPE name.
3179     */
3180    name = docbParseName(ctxt);
3181    if (name == NULL) {
3182       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3183           ctxt->sax->error(ctxt->userData, "docbParseDocTypeDecl : no DOCTYPE name !\n");
3184       ctxt->wellFormed = 0;
3185    }
3186    /*
3187     * Check that upper(name) == "SGML" !!!!!!!!!!!!!
3188     */
3189
3190    SKIP_BLANKS;
3191
3192    /*
3193     * Check for SystemID and ExternalID
3194     */
3195    URI = docbParseExternalID(ctxt, &ExternalID);
3196    SKIP_BLANKS;
3197
3198    /*
3199     * Create or update the document accordingly to the DOCTYPE
3200     */
3201    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3202       (!ctxt->disableSAX))
3203       ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3204
3205    /*
3206     * Is there any internal subset declarations ?
3207     * they are handled separately in docbParseInternalSubset()
3208     */
3209    if (RAW == '[')
3210       return;
3211
3212
3213    /*
3214     * We should be at the end of the DOCTYPE declaration.
3215     */
3216    if (CUR != '>') {
3217       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3218           ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
3219       ctxt->wellFormed = 0;
3220        /* We shouldn't try to resynchronize ... */
3221    }
3222    NEXT;
3223
3224    /*
3225     * Cleanup, since we don't use all those identifiers
3226     */
3227    if (URI != NULL) xmlFree(URI);
3228    if (ExternalID != NULL) xmlFree(ExternalID);
3229    if (name != NULL) xmlFree(name);
3230}
3231
3232/**
3233 * docbParseAttribute:
3234 * @ctxt:  an SGML parser context
3235 * @value:  a xmlChar ** used to store the value of the attribute
3236 *
3237 * parse an attribute
3238 *
3239 * [41] Attribute ::= Name Eq AttValue
3240 *
3241 * [25] Eq ::= S? '=' S?
3242 *
3243 * With namespace:
3244 *
3245 * [NS 11] Attribute ::= QName Eq AttValue
3246 *
3247 * Also the case QName == xmlns:??? is handled independently as a namespace
3248 * definition.
3249 *
3250 * Returns the attribute name, and the value in *value.
3251 */
3252
3253static xmlChar *
3254docbParseAttribute(docbParserCtxtPtr ctxt, xmlChar **value) {
3255    xmlChar *name, *val = NULL;
3256
3257    *value = NULL;
3258    name = docbParseName(ctxt);
3259    if (name == NULL) {
3260       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3261           ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
3262       ctxt->wellFormed = 0;
3263        return(NULL);
3264    }
3265
3266    /*
3267     * read the value
3268     */
3269    SKIP_BLANKS;
3270    if (CUR == '=') {
3271        NEXT;
3272       SKIP_BLANKS;
3273       val = docbParseAttValue(ctxt);
3274       /******
3275    } else {
3276        * TODO : some attribute must have values, some may not
3277       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3278           ctxt->sax->warning(ctxt->userData,
3279              "No value for attribute %s\n", name); */
3280    }
3281
3282    *value = val;
3283    return(name);
3284}
3285
3286/**
3287 * docbCheckEncoding:
3288 * @ctxt:  an SGML parser context
3289 * @attvalue: the attribute value
3290 *
3291 * Checks an http-equiv attribute from a Meta tag to detect
3292 * the encoding
3293 * If a new encoding is detected the parser is switched to decode
3294 * it and pass UTF8
3295 */
3296static void
3297docbCheckEncoding(docbParserCtxtPtr ctxt, const xmlChar *attvalue) {
3298    const xmlChar *encoding;
3299
3300    if ((ctxt == NULL) || (attvalue == NULL))
3301       return;
3302
3303    encoding = xmlStrstr(attvalue, BAD_CAST"charset=");
3304    if (encoding == NULL)
3305       encoding = xmlStrstr(attvalue, BAD_CAST"Charset=");
3306    if (encoding == NULL)
3307       encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET=");
3308    if (encoding != NULL) {
3309       encoding += 8;
3310    } else {
3311       encoding = xmlStrstr(attvalue, BAD_CAST"charset =");
3312       if (encoding == NULL)
3313           encoding = xmlStrstr(attvalue, BAD_CAST"Charset =");
3314       if (encoding == NULL)
3315           encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET =");
3316       if (encoding != NULL)
3317           encoding += 9;
3318    }
3319    /*
3320     * Restricted from 2.3.5 */
3321    if (encoding != NULL) {
3322       xmlCharEncoding enc;
3323
3324       if (ctxt->input->encoding != NULL)
3325           xmlFree((xmlChar *) ctxt->input->encoding);
3326       ctxt->input->encoding = encoding;
3327
3328       enc = xmlParseCharEncoding((const char *) encoding);
3329       if (enc == XML_CHAR_ENCODING_8859_1) {
3330           ctxt->charset = XML_CHAR_ENCODING_8859_1;
3331       } else if (enc != XML_CHAR_ENCODING_UTF8) {
3332           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3333               ctxt->sax->error(ctxt->userData,
3334                    "Unsupported encoding %s\n", encoding);
3335           /* xmlFree(encoding); */
3336           ctxt->wellFormed = 0;
3337           ctxt->disableSAX = 1;
3338           ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3339       }
3340    }
3341}
3342
3343/**
3344 * docbCheckMeta:
3345 * @ctxt:  an SGML parser context
3346 * @atts:  the attributes values
3347 *
3348 * Checks an attributes from a Meta tag
3349 */
3350static void
3351docbCheckMeta(docbParserCtxtPtr ctxt, const xmlChar **atts) {
3352    int i;
3353    const xmlChar *att, *value;
3354    int http = 0;
3355    const xmlChar *content = NULL;
3356
3357    if ((ctxt == NULL) || (atts == NULL))
3358       return;
3359
3360    i = 0;
3361    att = atts[i++];
3362    while (att != NULL) {
3363       value = atts[i++];
3364       if ((value != NULL) &&
3365           ((xmlStrEqual(att, BAD_CAST"http-equiv")) ||
3366            (xmlStrEqual(att, BAD_CAST"Http-Equiv")) ||
3367            (xmlStrEqual(att, BAD_CAST"HTTP-EQUIV"))) &&
3368           ((xmlStrEqual(value, BAD_CAST"Content-Type")) ||
3369            (xmlStrEqual(value, BAD_CAST"content-type")) ||
3370            (xmlStrEqual(value, BAD_CAST"CONTENT-TYPE"))))
3371           http = 1;
3372       else if ((value != NULL) &&
3373                ((xmlStrEqual(att, BAD_CAST"content")) ||
3374                 (xmlStrEqual(att, BAD_CAST"Content")) ||
3375                 (xmlStrEqual(att, BAD_CAST"CONTENT"))))
3376           content = value;
3377       att = atts[i++];
3378    }
3379    if ((http) && (content != NULL))
3380       docbCheckEncoding(ctxt, content);
3381
3382}
3383
3384/**
3385 * docbParseStartTag:
3386 * @ctxt:  an SGML parser context
3387 *
3388 * parse a start of tag either for rule element or
3389 * EmptyElement. In both case we don't parse the tag closing chars.
3390 *
3391 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3392 *
3393 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3394 *
3395 * With namespace:
3396 *
3397 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3398 *
3399 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3400 *
3401 */
3402
3403static void
3404docbParseStartTag(docbParserCtxtPtr ctxt) {
3405    xmlChar *name;
3406    xmlChar *attname;
3407    xmlChar *attvalue;
3408    const xmlChar **atts = NULL;
3409    int nbatts = 0;
3410    int maxatts = 0;
3411    int meta = 0;
3412    int i;
3413
3414    if (CUR != '<') return;
3415    NEXT;
3416
3417    GROW;
3418    name = docbParseSGMLName(ctxt);
3419    if (name == NULL) {
3420       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3421           ctxt->sax->error(ctxt->userData,
3422            "docbParseStartTag: invalid element name\n");
3423       ctxt->wellFormed = 0;
3424        return;
3425    }
3426    if (xmlStrEqual(name, BAD_CAST"meta"))
3427       meta = 1;
3428
3429    /*
3430     * Check for auto-closure of SGML elements.
3431     */
3432    docbAutoClose(ctxt, name);
3433
3434    /*
3435     * Now parse the attributes, it ends up with the ending
3436     *
3437     * (S Attribute)* S?
3438     */
3439    SKIP_BLANKS;
3440    while ((IS_CHAR(CUR)) &&
3441           (CUR != '>') &&
3442          ((CUR != '/') || (NXT(1) != '>'))) {
3443       long cons = ctxt->nbChars;
3444
3445       GROW;
3446       attname = docbParseAttribute(ctxt, &attvalue);
3447        if (attname != NULL) {
3448
3449           /*
3450            * Well formedness requires at most one declaration of an attribute
3451            */
3452           for (i = 0; i < nbatts;i += 2) {
3453               if (xmlStrEqual(atts[i], attname)) {
3454                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3455                       ctxt->sax->error(ctxt->userData,
3456                                        "Attribute %s redefined\n",
3457                                        attname);
3458                   ctxt->wellFormed = 0;
3459                   xmlFree(attname);
3460                   if (attvalue != NULL)
3461                       xmlFree(attvalue);
3462                   goto failed;
3463               }
3464           }
3465
3466           /*
3467            * Add the pair to atts
3468            */
3469           if (atts == NULL) {
3470               maxatts = 10;
3471               atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
3472               if (atts == NULL) {
3473                   xmlGenericError(xmlGenericErrorContext,
3474                           "malloc of %ld byte failed\n",
3475                           maxatts * (long)sizeof(xmlChar *));
3476                   if (name != NULL) xmlFree(name);
3477                   return;
3478               }
3479           } else if (nbatts + 4 > maxatts) {
3480               maxatts *= 2;
3481               atts = (const xmlChar **) xmlRealloc(atts, maxatts * sizeof(xmlChar *));
3482               if (atts == NULL) {
3483                   xmlGenericError(xmlGenericErrorContext,
3484                           "realloc of %ld byte failed\n",
3485                           maxatts * (long)sizeof(xmlChar *));
3486                   if (name != NULL) xmlFree(name);
3487                   return;
3488               }
3489           }
3490           atts[nbatts++] = attname;
3491           atts[nbatts++] = attvalue;
3492           atts[nbatts] = NULL;
3493           atts[nbatts + 1] = NULL;
3494       }
3495
3496failed:
3497       SKIP_BLANKS;
3498        if (cons == ctxt->nbChars) {
3499           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3500               ctxt->sax->error(ctxt->userData,
3501                "docbParseStartTag: problem parsing attributes\n");
3502           ctxt->wellFormed = 0;
3503           break;
3504       }
3505    }
3506
3507    /*
3508     * Handle specific association to the META tag
3509     */
3510    if (meta)
3511       docbCheckMeta(ctxt, atts);
3512
3513    /*
3514     * SAX: Start of Element !
3515     */
3516    docbnamePush(ctxt, xmlStrdup(name));
3517#ifdef DEBUG
3518    xmlGenericError(xmlGenericErrorContext,"Start of element %s: pushed %s\n", name, ctxt->name);
3519#endif
3520    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
3521        ctxt->sax->startElement(ctxt->userData, name, atts);
3522
3523    if (atts != NULL) {
3524        for (i = 0;i < nbatts;i++) {
3525           if (atts[i] != NULL)
3526               xmlFree((xmlChar *) atts[i]);
3527       }
3528       xmlFree((void *) atts);
3529    }
3530    if (name != NULL) xmlFree(name);
3531}
3532
3533/**
3534 * docbParseEndTag:
3535 * @ctxt:  an SGML parser context
3536 *
3537 * parse an end of tag
3538 *
3539 * [42] ETag ::= '</' Name S? '>'
3540 *
3541 * With namespace
3542 *
3543 * [NS 9] ETag ::= '</' QName S? '>'
3544 */
3545
3546static void
3547docbParseEndTag(docbParserCtxtPtr ctxt) {
3548    xmlChar *name;
3549    xmlChar *oldname;
3550    int i;
3551
3552    if ((CUR != '<') || (NXT(1) != '/')) {
3553       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3554           ctxt->sax->error(ctxt->userData, "docbParseEndTag: '</' not found\n");
3555       ctxt->wellFormed = 0;
3556       return;
3557    }
3558    SKIP(2);
3559
3560    name = docbParseSGMLName(ctxt);
3561    if (name == NULL) {
3562       if (CUR == '>') {
3563           NEXT;
3564           oldname = docbnamePop(ctxt);
3565           if (oldname != NULL) {
3566               if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3567                   ctxt->sax->endElement(ctxt->userData, name);
3568#ifdef DEBUG
3569               xmlGenericError(xmlGenericErrorContext,"End of tag </>: popping out %s\n", oldname);
3570#endif
3571               xmlFree(oldname);
3572#ifdef DEBUG
3573           } else {
3574               xmlGenericError(xmlGenericErrorContext,"End of tag </>: stack empty !!!\n");
3575#endif
3576           }
3577           return;
3578       } else
3579           return;
3580    }
3581
3582    /*
3583     * We should definitely be at the ending "S? '>'" part
3584     */
3585    SKIP_BLANKS;
3586    if ((!IS_CHAR(CUR)) || (CUR != '>')) {
3587       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3588           ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
3589       ctxt->wellFormed = 0;
3590    } else
3591       NEXT;
3592
3593    /*
3594     * If the name read is not one of the element in the parsing stack
3595     * then return, it's just an error.
3596     */
3597    for (i = (ctxt->nameNr - 1);i >= 0;i--) {
3598        if (xmlStrEqual(name, ctxt->nameTab[i])) break;
3599    }
3600    if (i < 0) {
3601       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3602           ctxt->sax->error(ctxt->userData,
3603            "Unexpected end tag : %s\n", name);
3604       xmlFree(name);
3605       ctxt->wellFormed = 0;
3606       return;
3607    }
3608
3609
3610    /*
3611     * Check for auto-closure of SGML elements.
3612     */
3613
3614    docbAutoCloseOnClose(ctxt, name);
3615
3616    /*
3617     * Well formedness constraints, opening and closing must match.
3618     * With the exception that the autoclose may have popped stuff out
3619     * of the stack.
3620     */
3621    if (((name[0] != '/') || (name[1] != 0)) &&
3622       (!xmlStrEqual(name, ctxt->name))) {
3623#ifdef DEBUG
3624       xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", name, ctxt->name);
3625#endif
3626        if ((ctxt->name != NULL) &&
3627           (!xmlStrEqual(ctxt->name, name))) {
3628           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3629               ctxt->sax->error(ctxt->userData,
3630                "Opening and ending tag mismatch: %s and %s\n",
3631                                name, ctxt->name);
3632           ctxt->wellFormed = 0;
3633        }
3634    }
3635
3636    /*
3637     * SAX: End of Tag
3638     */
3639    oldname = ctxt->name;
3640    if (((name[0] == '/') && (name[1] == 0)) ||
3641       ((oldname != NULL) && (xmlStrEqual(oldname, name)))) {
3642       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3643           ctxt->sax->endElement(ctxt->userData, name);
3644       oldname = docbnamePop(ctxt);
3645       if (oldname != NULL) {
3646#ifdef DEBUG
3647           xmlGenericError(xmlGenericErrorContext,"End of tag %s: popping out %s\n", name, oldname);
3648#endif
3649           xmlFree(oldname);
3650#ifdef DEBUG
3651       } else {
3652           xmlGenericError(xmlGenericErrorContext,"End of tag %s: stack empty !!!\n", name);
3653#endif
3654       }
3655    }
3656
3657    if (name != NULL)
3658       xmlFree(name);
3659
3660    return;
3661}
3662
3663
3664/**
3665 * docbParseReference:
3666 * @ctxt:  an SGML parser context
3667 *
3668 * parse and handle entity references in content,
3669 * this will end-up in a call to character() since this is either a
3670 * CharRef, or a predefined entity.
3671 */
3672static void
3673docbParseReference(docbParserCtxtPtr ctxt) {
3674    docbEntityDescPtr ent;
3675    xmlChar out[6];
3676    xmlChar *name;
3677    if (CUR != '&') return;
3678
3679    if (NXT(1) == '#') {
3680       unsigned int c;
3681       int bits, i = 0;
3682
3683       c = docbParseCharRef(ctxt);
3684        if      (c <    0x80) { out[i++]= c;                bits= -6; }
3685        else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
3686        else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
3687        else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
3688
3689        for ( ; bits >= 0; bits-= 6) {
3690            out[i++]= ((c >> bits) & 0x3F) | 0x80;
3691        }
3692       out[i] = 0;
3693
3694       docbCheckParagraph(ctxt);
3695       if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3696           ctxt->sax->characters(ctxt->userData, out, i);
3697    } else {
3698       ent = docbParseEntityRef(ctxt, &name);
3699       if (name == NULL) {
3700           docbCheckParagraph(ctxt);
3701           if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3702               ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3703           return;
3704       }
3705       if ((ent == NULL) || (ent->value <= 0)) {
3706           docbCheckParagraph(ctxt);
3707           if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3708               ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3709               ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3710               /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3711           }
3712       } else {
3713           unsigned int c;
3714           int bits, i = 0;
3715
3716           c = ent->value;
3717           if      (c <    0x80)
3718                   { out[i++]= c;                bits= -6; }
3719           else if (c <   0x800)
3720                   { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
3721           else if (c < 0x10000)
3722                   { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
3723           else
3724                   { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
3725
3726           for ( ; bits >= 0; bits-= 6) {
3727               out[i++]= ((c >> bits) & 0x3F) | 0x80;
3728           }
3729           out[i] = 0;
3730
3731           docbCheckParagraph(ctxt);
3732           if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3733               ctxt->sax->characters(ctxt->userData, out, i);
3734       }
3735       xmlFree(name);
3736    }
3737}
3738
3739/**
3740 * docbParseContent:
3741 * @ctxt:  an SGML parser context
3742 * @name:  the node name
3743 *
3744 * Parse a content: comment, sub-element, reference or text.
3745 *
3746 */
3747
3748static void
3749docbParseContent(docbParserCtxtPtr ctxt) {
3750    xmlChar *currentNode;
3751    int depth;
3752
3753    currentNode = xmlStrdup(ctxt->name);
3754    depth = ctxt->nameNr;
3755    while (1) {
3756       long cons = ctxt->nbChars;
3757
3758        GROW;
3759       /*
3760        * Our tag or one of it's parent or children is ending.
3761        */
3762        if ((CUR == '<') && (NXT(1) == '/')) {
3763           docbParseEndTag(ctxt);
3764           if (currentNode != NULL) xmlFree(currentNode);
3765           return;
3766        }
3767
3768       /*
3769        * Has this node been popped out during parsing of
3770        * the next element
3771        */
3772        if ((!xmlStrEqual(currentNode, ctxt->name)) &&
3773           (depth >= ctxt->nameNr)) {
3774           if (currentNode != NULL) xmlFree(currentNode);
3775           return;
3776       }
3777
3778       /*
3779        * Sometimes DOCTYPE arrives in the middle of the document
3780        */
3781       if ((CUR == '<') && (NXT(1) == '!') &&
3782           (UPP(2) == 'D') && (UPP(3) == 'O') &&
3783           (UPP(4) == 'C') && (UPP(5) == 'T') &&
3784           (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3785           (UPP(8) == 'E')) {
3786           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3787               ctxt->sax->error(ctxt->userData,
3788                    "Misplaced DOCTYPE declaration\n");
3789           ctxt->wellFormed = 0;
3790           docbParseDocTypeDecl(ctxt);
3791       }
3792
3793       /*
3794        * First case :  a comment
3795        */
3796       if ((CUR == '<') && (NXT(1) == '!') &&
3797                (NXT(2) == '-') && (NXT(3) == '-')) {
3798           docbParseComment(ctxt);
3799       }
3800
3801       /*
3802        * Second case :  a sub-element.
3803        */
3804       else if (CUR == '<') {
3805           docbParseElement(ctxt);
3806       }
3807
3808       /*
3809        * Third case : a reference. If if has not been resolved,
3810        *    parsing returns it's Name, create the node
3811        */
3812       else if (CUR == '&') {
3813           docbParseReference(ctxt);
3814       }
3815
3816       /*
3817        * Fourth : end of the resource
3818        */
3819       else if (CUR == 0) {
3820           docbAutoClose(ctxt, NULL);
3821       }
3822
3823       /*
3824        * Last case, text. Note that References are handled directly.
3825        */
3826       else {
3827           docbParseCharData(ctxt);
3828       }
3829
3830       if (cons == ctxt->nbChars) {
3831           if (ctxt->node != NULL) {
3832               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3833                   ctxt->sax->error(ctxt->userData,
3834                        "detected an error in element content\n");
3835               ctxt->wellFormed = 0;
3836           }
3837            break;
3838       }
3839
3840        GROW;
3841    }
3842    if (currentNode != NULL) xmlFree(currentNode);
3843}
3844
3845/**
3846 * docbParseElement:
3847 * @ctxt:  an SGML parser context
3848 *
3849 * parse an SGML element, this is highly recursive
3850 *
3851 * [39] element ::= EmptyElemTag | STag content ETag
3852 *
3853 * [41] Attribute ::= Name Eq AttValue
3854 */
3855
3856static void
3857docbParseElement(docbParserCtxtPtr ctxt) {
3858    xmlChar *name;
3859    xmlChar *currentNode = NULL;
3860    docbElemDescPtr info;
3861    docbParserNodeInfo node_info;
3862    xmlChar *oldname;
3863    int depth = ctxt->nameNr;
3864
3865    /* Capture start position */
3866    if (ctxt->record_info) {
3867        node_info.begin_pos = ctxt->input->consumed +
3868                          (CUR_PTR - ctxt->input->base);
3869       node_info.begin_line = ctxt->input->line;
3870    }
3871
3872    oldname = xmlStrdup(ctxt->name);
3873    docbParseStartTag(ctxt);
3874    name = ctxt->name;
3875#ifdef DEBUG
3876    if (oldname == NULL)
3877       xmlGenericError(xmlGenericErrorContext,
3878               "Start of element %s\n", name);
3879    else if (name == NULL)
3880       xmlGenericError(xmlGenericErrorContext,
3881               "Start of element failed, was %s\n", oldname);
3882    else
3883       xmlGenericError(xmlGenericErrorContext,
3884               "Start of element %s, was %s\n", name, oldname);
3885#endif
3886    if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
3887        (name == NULL)) {
3888       if (CUR == '>')
3889           NEXT;
3890       if (oldname != NULL)
3891           xmlFree(oldname);
3892        return;
3893    }
3894    if (oldname != NULL)
3895       xmlFree(oldname);
3896
3897    /*
3898     * Lookup the info for that element.
3899     */
3900    info = docbTagLookup(name);
3901    if (info == NULL) {
3902       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3903           ctxt->sax->error(ctxt->userData, "Tag %s unknown\n",
3904                            name);
3905       ctxt->wellFormed = 0;
3906    } else if (info->depr) {
3907/***************************
3908       if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
3909           ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
3910                              name);
3911 ***************************/
3912    }
3913
3914    /*
3915     * Check for an Empty Element labelled the XML/SGML way
3916     */
3917    if ((CUR == '/') && (NXT(1) == '>')) {
3918        SKIP(2);
3919       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3920           ctxt->sax->endElement(ctxt->userData, name);
3921       oldname = docbnamePop(ctxt);
3922#ifdef DEBUG
3923        xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n", oldname);
3924#endif
3925       if (oldname != NULL)
3926           xmlFree(oldname);
3927       return;
3928    }
3929
3930    if (CUR == '>') {
3931        NEXT;
3932    } else {
3933       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3934           ctxt->sax->error(ctxt->userData,
3935                            "Couldn't find end of Start Tag %s\n",
3936                            name);
3937       ctxt->wellFormed = 0;
3938
3939       /*
3940        * end of parsing of this node.
3941        */
3942       if (xmlStrEqual(name, ctxt->name)) {
3943           nodePop(ctxt);
3944           oldname = docbnamePop(ctxt);
3945#ifdef DEBUG
3946           xmlGenericError(xmlGenericErrorContext,"End of start tag problem: popping out %s\n", oldname);
3947#endif
3948           if (oldname != NULL)
3949               xmlFree(oldname);
3950       }
3951
3952       /*
3953        * Capture end position and add node
3954        */
3955       if ( currentNode != NULL && ctxt->record_info ) {
3956          node_info.end_pos = ctxt->input->consumed +
3957                             (CUR_PTR - ctxt->input->base);
3958          node_info.end_line = ctxt->input->line;
3959          node_info.node = ctxt->node;
3960          xmlParserAddNodeInfo(ctxt, &node_info);
3961       }
3962       return;
3963    }
3964
3965    /*
3966     * Check for an Empty Element from DTD definition
3967     */
3968    if ((info != NULL) && (info->empty)) {
3969       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3970           ctxt->sax->endElement(ctxt->userData, name);
3971       oldname = docbnamePop(ctxt);
3972#ifdef DEBUG
3973       xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
3974#endif
3975       if (oldname != NULL)
3976           xmlFree(oldname);
3977       return;
3978    }
3979
3980    /*
3981     * Parse the content of the element:
3982     */
3983    currentNode = xmlStrdup(ctxt->name);
3984    depth = ctxt->nameNr;
3985    while (IS_CHAR(CUR)) {
3986       docbParseContent(ctxt);
3987       if (ctxt->nameNr < depth) break;
3988    }
3989
3990    if (!IS_CHAR(CUR)) {
3991       /************
3992       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3993           ctxt->sax->error(ctxt->userData,
3994                "Premature end of data in tag %s\n", currentNode);
3995       ctxt->wellFormed = 0;
3996        *************/
3997
3998       /*
3999        * end of parsing of this node.
4000        */
4001       nodePop(ctxt);
4002       oldname = docbnamePop(ctxt);
4003#ifdef DEBUG
4004       xmlGenericError(xmlGenericErrorContext,"Premature end of tag %s : popping out %s\n", name, oldname);
4005#endif
4006       if (oldname != NULL)
4007           xmlFree(oldname);
4008       if (currentNode != NULL)
4009           xmlFree(currentNode);
4010       return;
4011    }
4012
4013    /*
4014     * Capture end position and add node
4015     */
4016    if ( currentNode != NULL && ctxt->record_info ) {
4017       node_info.end_pos = ctxt->input->consumed +
4018                          (CUR_PTR - ctxt->input->base);
4019       node_info.end_line = ctxt->input->line;
4020       node_info.node = ctxt->node;
4021       xmlParserAddNodeInfo(ctxt, &node_info);
4022    }
4023    if (currentNode != NULL)
4024       xmlFree(currentNode);
4025}
4026
4027/**
4028 * docbParseEntityDecl:
4029 * @ctxt:  an SGML parser context
4030 *
4031 * parse <!ENTITY declarations
4032 *
4033 */
4034
4035static void
4036docbParseEntityDecl(xmlParserCtxtPtr ctxt) {
4037    xmlChar *name = NULL;
4038    xmlChar *value = NULL;
4039    xmlChar *URI = NULL, *literal = NULL;
4040    xmlChar *ndata = NULL;
4041    int isParameter = 0;
4042    xmlChar *orig = NULL;
4043
4044    GROW;
4045    if ((RAW == '<') && (NXT(1) == '!') &&
4046        (NXT(2) == 'E') && (NXT(3) == 'N') &&
4047        (NXT(4) == 'T') && (NXT(5) == 'I') &&
4048        (NXT(6) == 'T') && (NXT(7) == 'Y')) {
4049       xmlParserInputPtr input = ctxt->input;
4050       ctxt->instate = XML_PARSER_ENTITY_DECL;
4051       SHRINK;
4052       SKIP(8);
4053       if (!IS_BLANK(CUR)) {
4054           ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4055           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4056               ctxt->sax->error(ctxt->userData,
4057                                "Space required after '<!ENTITY'\n");
4058           ctxt->wellFormed = 0;
4059           ctxt->disableSAX = 1;
4060       }
4061       SKIP_BLANKS;
4062
4063       if (RAW == '%') {
4064           NEXT;
4065           if (!IS_BLANK(CUR)) {
4066               ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4067               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4068                   ctxt->sax->error(ctxt->userData,
4069                                    "Space required after '%'\n");
4070               ctxt->wellFormed = 0;
4071               ctxt->disableSAX = 1;
4072           }
4073           SKIP_BLANKS;
4074           isParameter = 1;
4075       }
4076
4077        name = xmlParseName(ctxt);
4078       if (name == NULL) {
4079           ctxt->errNo = XML_ERR_NAME_REQUIRED;
4080           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4081               ctxt->sax->error(ctxt->userData, "sgmlarseEntityDecl: no name\n");
4082           ctxt->wellFormed = 0;
4083           ctxt->disableSAX = 1;
4084            return;
4085       }
4086       if (!IS_BLANK(CUR)) {
4087           ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4088           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4089               ctxt->sax->error(ctxt->userData,
4090                    "Space required after the entity name\n");
4091           ctxt->wellFormed = 0;
4092           ctxt->disableSAX = 1;
4093       }
4094        SKIP_BLANKS;
4095
4096       /*
4097        * handle the various case of definitions...
4098        */
4099       if (isParameter) {
4100           if ((RAW == '"') || (RAW == '\'')) {
4101               value = xmlParseEntityValue(ctxt, &orig);
4102               if (value) {
4103                   if ((ctxt->sax != NULL) &&
4104                       (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4105                       ctxt->sax->entityDecl(ctxt->userData, name,
4106                                   XML_INTERNAL_PARAMETER_ENTITY,
4107                                   NULL, NULL, value);
4108               }
4109           } else {
4110               URI = xmlParseExternalID(ctxt, &literal, 1);
4111               if ((URI == NULL) && (literal == NULL)) {
4112                   ctxt->errNo = XML_ERR_VALUE_REQUIRED;
4113                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4114                       ctxt->sax->error(ctxt->userData,
4115                           "Entity value required\n");
4116                   ctxt->wellFormed = 0;
4117                   ctxt->disableSAX = 1;
4118               }
4119               if (URI) {
4120                   xmlURIPtr uri;
4121
4122                   uri = xmlParseURI((const char *) URI);
4123                   if (uri == NULL) {
4124                       ctxt->errNo = XML_ERR_INVALID_URI;
4125                       if ((ctxt->sax != NULL) &&
4126                           (!ctxt->disableSAX) &&
4127                           (ctxt->sax->error != NULL))
4128                           ctxt->sax->error(ctxt->userData,
4129                                       "Invalid URI: %s\n", URI);
4130                       ctxt->wellFormed = 0;
4131                   } else {
4132                       if (uri->fragment != NULL) {
4133                           ctxt->errNo = XML_ERR_URI_FRAGMENT;
4134                           if ((ctxt->sax != NULL) &&
4135                               (!ctxt->disableSAX) &&
4136                               (ctxt->sax->error != NULL))
4137                               ctxt->sax->error(ctxt->userData,
4138                                           "Fragment not allowed: %s\n", URI);
4139                           ctxt->wellFormed = 0;
4140                       } else {
4141                           if ((ctxt->sax != NULL) &&
4142                               (!ctxt->disableSAX) &&
4143                               (ctxt->sax->entityDecl != NULL))
4144                               ctxt->sax->entityDecl(ctxt->userData, name,
4145                                           XML_EXTERNAL_PARAMETER_ENTITY,
4146                                           literal, URI, NULL);
4147                       }
4148                       xmlFreeURI(uri);
4149                   }
4150               }
4151           }
4152       } else {
4153           if ((RAW == '"') || (RAW == '\'')) {
4154               value = xmlParseEntityValue(ctxt, &orig);
4155               if ((ctxt->sax != NULL) &&
4156                   (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4157                   ctxt->sax->entityDecl(ctxt->userData, name,
4158                               XML_INTERNAL_GENERAL_ENTITY,
4159                               NULL, NULL, value);
4160           } else {
4161               URI = xmlParseExternalID(ctxt, &literal, 1);
4162               if ((URI == NULL) && (literal == NULL)) {
4163                   ctxt->errNo = XML_ERR_VALUE_REQUIRED;
4164                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4165                       ctxt->sax->error(ctxt->userData,
4166                           "Entity value required\n");
4167                   ctxt->wellFormed = 0;
4168                   ctxt->disableSAX = 1;
4169               }
4170               if (URI) {
4171                   xmlURIPtr uri;
4172
4173                   uri = xmlParseURI((const char *)URI);
4174                   if (uri == NULL) {
4175                       ctxt->errNo = XML_ERR_INVALID_URI;
4176                       if ((ctxt->sax != NULL) &&
4177                           (!ctxt->disableSAX) &&
4178                           (ctxt->sax->error != NULL))
4179                           ctxt->sax->error(ctxt->userData,
4180                                       "Invalid URI: %s\n", URI);
4181                       ctxt->wellFormed = 0;
4182                   } else {
4183                       if (uri->fragment != NULL) {
4184                           ctxt->errNo = XML_ERR_URI_FRAGMENT;
4185                           if ((ctxt->sax != NULL) &&
4186                               (!ctxt->disableSAX) &&
4187                               (ctxt->sax->error != NULL))
4188                               ctxt->sax->error(ctxt->userData,
4189                                           "Fragment not allowed: %s\n", URI);
4190                           ctxt->wellFormed = 0;
4191                       }
4192                       xmlFreeURI(uri);
4193                   }
4194               }
4195               if ((RAW != '>') && (!IS_BLANK(CUR))) {
4196                   ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4197                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4198                       ctxt->sax->error(ctxt->userData,
4199                           "Space required before content model\n");
4200                   ctxt->wellFormed = 0;
4201                   ctxt->disableSAX = 1;
4202               }
4203               SKIP_BLANKS;
4204
4205               /*
4206                * SGML specific: here we can get the content model
4207                */
4208               if (RAW != '>') {
4209                   xmlChar *contmod;
4210
4211                   contmod = xmlParseName(ctxt);
4212
4213                   if (contmod == NULL) {
4214                       ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4215                       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4216                           ctxt->sax->error(ctxt->userData,
4217                               "Could not parse entity content model\n");
4218                       ctxt->wellFormed = 0;
4219                       ctxt->disableSAX = 1;
4220                   } else {
4221                       if (xmlStrEqual(contmod, BAD_CAST"NDATA")) {
4222                           if (!IS_BLANK(CUR)) {
4223                               ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4224                               if ((ctxt->sax != NULL) &&
4225                                   (ctxt->sax->error != NULL))
4226                                   ctxt->sax->error(ctxt->userData,
4227                                       "Space required after 'NDATA'\n");
4228                               ctxt->wellFormed = 0;
4229                               ctxt->disableSAX = 1;
4230                           }
4231                           SKIP_BLANKS;
4232                           ndata = xmlParseName(ctxt);
4233                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4234                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4235                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4236                                       name, literal, URI, ndata);
4237                           }
4238                       } else if (xmlStrEqual(contmod, BAD_CAST"SUBDOC")) {
4239                           if ((ctxt->sax != NULL) &&
4240                               (ctxt->sax->warning != NULL))
4241                               ctxt->sax->warning(ctxt->userData,
4242                                   "SUBDOC entities are not supported\n");
4243                           SKIP_BLANKS;
4244                           ndata = xmlParseName(ctxt);
4245                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4246                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4247                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4248                                       name, literal, URI, ndata);
4249                           }
4250                       } else if (xmlStrEqual(contmod, BAD_CAST"CDATA")) {
4251                           if ((ctxt->sax != NULL) &&
4252                               (ctxt->sax->warning != NULL))
4253                               ctxt->sax->warning(ctxt->userData,
4254                                   "CDATA entities are not supported\n");
4255                           SKIP_BLANKS;
4256                           ndata = xmlParseName(ctxt);
4257                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4258                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4259                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4260                                       name, literal, URI, ndata);
4261                           }
4262                       }
4263                       xmlFree(contmod);
4264                   }
4265               } else {
4266                   if ((ctxt->sax != NULL) &&
4267                       (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4268                       ctxt->sax->entityDecl(ctxt->userData, name,
4269                                   XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4270                                   literal, URI, NULL);
4271               }
4272           }
4273       }
4274       SKIP_BLANKS;
4275       if (RAW != '>') {
4276           ctxt->errNo = XML_ERR_ENTITY_NOT_FINISHED;
4277           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4278               ctxt->sax->error(ctxt->userData,
4279                   "docbParseEntityDecl: entity %s not terminated\n", name);
4280           ctxt->wellFormed = 0;
4281           ctxt->disableSAX = 1;
4282       } else {
4283           if (input != ctxt->input) {
4284               ctxt->errNo = XML_ERR_ENTITY_BOUNDARY;
4285               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4286                   ctxt->sax->error(ctxt->userData,
4287"Entity declaration doesn't start and stop in the same entity\n");
4288               ctxt->wellFormed = 0;
4289               ctxt->disableSAX = 1;
4290           }
4291           NEXT;
4292       }
4293       if (orig != NULL) {
4294           /*
4295            * Ugly mechanism to save the raw entity value.
4296            */
4297           xmlEntityPtr cur = NULL;
4298
4299           if (isParameter) {
4300               if ((ctxt->sax != NULL) &&
4301                   (ctxt->sax->getParameterEntity != NULL))
4302                   cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4303           } else {
4304               if ((ctxt->sax != NULL) &&
4305                   (ctxt->sax->getEntity != NULL))
4306                   cur = ctxt->sax->getEntity(ctxt->userData, name);
4307           }
4308            if (cur != NULL) {
4309               if (cur->orig != NULL)
4310                   xmlFree(orig);
4311               else
4312                   cur->orig = orig;
4313           } else
4314               xmlFree(orig);
4315       }
4316       if (name != NULL) xmlFree(name);
4317       if (value != NULL) xmlFree(value);
4318       if (URI != NULL) xmlFree(URI);
4319       if (literal != NULL) xmlFree(literal);
4320       if (ndata != NULL) xmlFree(ndata);
4321    }
4322}
4323
4324/**
4325 * docbParseMarkupDecl:
4326 * @ctxt:  an SGML parser context
4327 *
4328 * parse Markup declarations
4329 *
4330 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
4331 *                     NotationDecl | PI | Comment
4332 */
4333static void
4334docbParseMarkupDecl(xmlParserCtxtPtr ctxt) {
4335    GROW;
4336    xmlParseElementDecl(ctxt);
4337    xmlParseAttributeListDecl(ctxt);
4338    docbParseEntityDecl(ctxt);
4339    xmlParseNotationDecl(ctxt);
4340    xmlParsePI(ctxt);
4341    xmlParseComment(ctxt);
4342    /*
4343     * This is only for internal subset. On external entities,
4344     * the replacement is done before parsing stage
4345     */
4346    if ((ctxt->external == 0) && (ctxt->inputNr == 1))
4347       xmlParsePEReference(ctxt);
4348    ctxt->instate = XML_PARSER_DTD;
4349}
4350
4351/**
4352 * docbParseInternalsubset:
4353 * @ctxt:  an SGML parser context
4354 *
4355 * parse the internal subset declaration
4356 *
4357 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
4358 */
4359
4360static void
4361docbParseInternalSubset(xmlParserCtxtPtr ctxt) {
4362    /*
4363     * Is there any DTD definition ?
4364     */
4365    if (RAW == '[') {
4366        ctxt->instate = XML_PARSER_DTD;
4367        NEXT;
4368       /*
4369        * Parse the succession of Markup declarations and
4370        * PEReferences.
4371        * Subsequence (markupdecl | PEReference | S)*
4372        */
4373       while (RAW != ']') {
4374           const xmlChar *check = CUR_PTR;
4375           int cons = ctxt->input->consumed;
4376
4377           SKIP_BLANKS;
4378           docbParseMarkupDecl(ctxt);
4379           xmlParsePEReference(ctxt);
4380
4381           /*
4382            * Pop-up of finished entities.
4383            */
4384           while ((RAW == 0) && (ctxt->inputNr > 1))
4385               xmlPopInput(ctxt);
4386
4387           if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
4388               ctxt->errNo = XML_ERR_INTERNAL_ERROR;
4389               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4390                   ctxt->sax->error(ctxt->userData,
4391            "docbParseInternalSubset: error detected in Markup declaration\n");
4392               ctxt->wellFormed = 0;
4393               ctxt->disableSAX = 1;
4394               break;
4395           }
4396       }
4397       if (RAW == ']') {
4398           NEXT;
4399           SKIP_BLANKS;
4400       }
4401    }
4402
4403    /*
4404     * We should be at the end of the DOCTYPE declaration.
4405     */
4406    if (RAW != '>') {
4407       ctxt->errNo = XML_ERR_DOCTYPE_NOT_FINISHED;
4408       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4409           ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
4410       ctxt->wellFormed = 0;
4411       ctxt->disableSAX = 1;
4412    }
4413    NEXT;
4414}
4415
4416/**
4417 * docbParseMisc:
4418 * @ctxt:  an XML parser context
4419 *
4420 * parse an XML Misc* optionnal field.
4421 *
4422 * [27] Misc ::= Comment | PI |  S
4423 */
4424
4425static void
4426docbParseMisc(xmlParserCtxtPtr ctxt) {
4427    while (((RAW == '<') && (NXT(1) == '?')) ||
4428           ((RAW == '<') && (NXT(1) == '!') &&
4429           (NXT(2) == '-') && (NXT(3) == '-')) ||
4430           IS_BLANK(CUR)) {
4431        if ((RAW == '<') && (NXT(1) == '?')) {
4432           xmlParsePI(ctxt); /* TODO: SGML PIs differs */
4433       } else if (IS_BLANK(CUR)) {
4434           NEXT;
4435       } else
4436           xmlParseComment(ctxt);
4437    }
4438}
4439
4440/**
4441 * docbParseDocument :
4442 * @ctxt:  an SGML parser context
4443 *
4444 * parse an SGML document (and build a tree if using the standard SAX
4445 * interface).
4446 *
4447 * Returns 0, -1 in case of error. the parser context is augmented
4448 *                as a result of the parsing.
4449 */
4450
4451int
4452docbParseDocument(docbParserCtxtPtr ctxt) {
4453    xmlChar start[4];
4454    xmlCharEncoding enc;
4455    xmlDtdPtr dtd;
4456
4457    docbDefaultSAXHandlerInit();
4458    ctxt->html = 2;
4459
4460    GROW;
4461    /*
4462     * SAX: beginning of the document processing.
4463     */
4464    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4465        ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4466
4467    /*
4468     * Get the 4 first bytes and decode the charset
4469     * if enc != XML_CHAR_ENCODING_NONE
4470     * plug some encoding conversion routines.
4471     */
4472    start[0] = RAW;
4473    start[1] = NXT(1);
4474    start[2] = NXT(2);
4475    start[3] = NXT(3);
4476    enc = xmlDetectCharEncoding(start, 4);
4477    if (enc != XML_CHAR_ENCODING_NONE) {
4478        xmlSwitchEncoding(ctxt, enc);
4479    }
4480
4481    /*
4482     * Wipe out everything which is before the first '<'
4483     */
4484    SKIP_BLANKS;
4485    if (CUR == 0) {
4486       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4487           ctxt->sax->error(ctxt->userData, "Document is empty\n");
4488       ctxt->wellFormed = 0;
4489    }
4490
4491    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4492       ctxt->sax->startDocument(ctxt->userData);
4493
4494
4495    /*
4496     * The Misc part of the Prolog
4497     */
4498    GROW;
4499    docbParseMisc(ctxt);
4500
4501    /*
4502     * Then possibly doc type declaration(s) and more Misc
4503     * (doctypedecl Misc*)?
4504     */
4505    GROW;
4506    if ((RAW == '<') && (NXT(1) == '!') &&
4507       (NXT(2) == 'D') && (NXT(3) == 'O') &&
4508       (NXT(4) == 'C') && (NXT(5) == 'T') &&
4509       (NXT(6) == 'Y') && (NXT(7) == 'P') &&
4510       (NXT(8) == 'E')) {
4511
4512       ctxt->inSubset = 1;
4513       docbParseDocTypeDecl(ctxt);
4514       if (RAW == '[') {
4515           ctxt->instate = XML_PARSER_DTD;
4516           docbParseInternalSubset(ctxt);
4517       }
4518
4519       /*
4520        * Create and update the external subset.
4521        */
4522       ctxt->inSubset = 2;
4523       if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
4524           (!ctxt->disableSAX))
4525           ctxt->sax->internalSubset(ctxt->userData, ctxt->intSubName,
4526                                     ctxt->extSubSystem, ctxt->extSubURI);
4527       ctxt->inSubset = 0;
4528
4529
4530       ctxt->instate = XML_PARSER_PROLOG;
4531       docbParseMisc(ctxt);
4532    }
4533
4534    /*
4535     * Time to start parsing the tree itself
4536     */
4537    docbParseContent(ctxt);
4538
4539    /*
4540     * autoclose
4541     */
4542    if (CUR == 0)
4543       docbAutoClose(ctxt, NULL);
4544
4545
4546    /*
4547     * SAX: end of the document processing.
4548     */
4549    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4550        ctxt->sax->endDocument(ctxt->userData);
4551
4552    if (ctxt->myDoc != NULL) {
4553       dtd = ctxt->myDoc->intSubset;
4554       if (dtd == NULL)
4555           ctxt->myDoc->intSubset =
4556               xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "SGML",
4557                   BAD_CAST "-//W3C//DTD SGML 4.0 Transitional//EN",
4558                   BAD_CAST "http://www.w3.org/TR/REC-docbook/loose.dtd");
4559    }
4560    if (! ctxt->wellFormed) return(-1);
4561    return(0);
4562}
4563
4564
4565/************************************************************************
4566 *                                                                     *
4567 *                     Parser contexts handling                        *
4568 *                                                                     *
4569 ************************************************************************/
4570
4571/**
4572 * xmlInitParserCtxt:
4573 * @ctxt:  an SGML parser context
4574 *
4575 * Initialize a parser context
4576 */
4577
4578static void
4579docbInitParserCtxt(docbParserCtxtPtr ctxt)
4580{
4581    docbSAXHandler *sax;
4582
4583    if (ctxt == NULL) return;
4584    memset(ctxt, 0, sizeof(docbParserCtxt));
4585
4586    sax = (docbSAXHandler *) xmlMalloc(sizeof(docbSAXHandler));
4587    if (sax == NULL) {
4588        xmlGenericError(xmlGenericErrorContext,
4589               "docbInitParserCtxt: out of memory\n");
4590    }
4591    memset(sax, 0, sizeof(docbSAXHandler));
4592
4593    /* Allocate the Input stack */
4594    ctxt->inputTab = (docbParserInputPtr *)
4595                      xmlMalloc(5 * sizeof(docbParserInputPtr));
4596    if (ctxt->inputTab == NULL) {
4597        xmlGenericError(xmlGenericErrorContext,
4598               "docbInitParserCtxt: out of memory\n");
4599    }
4600    ctxt->inputNr = 0;
4601    ctxt->inputMax = 5;
4602    ctxt->input = NULL;
4603    ctxt->version = NULL;
4604    ctxt->encoding = NULL;
4605    ctxt->standalone = -1;
4606    ctxt->instate = XML_PARSER_START;
4607
4608    /* Allocate the Node stack */
4609    ctxt->nodeTab = (docbNodePtr *) xmlMalloc(10 * sizeof(docbNodePtr));
4610    ctxt->nodeNr = 0;
4611    ctxt->nodeMax = 10;
4612    ctxt->node = NULL;
4613
4614    /* Allocate the Name stack */
4615    ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
4616    ctxt->nameNr = 0;
4617    ctxt->nameMax = 10;
4618    ctxt->name = NULL;
4619
4620    if (sax == NULL) ctxt->sax = &docbDefaultSAXHandler;
4621    else {
4622        ctxt->sax = sax;
4623       memcpy(sax, &docbDefaultSAXHandler, sizeof(docbSAXHandler));
4624    }
4625    ctxt->userData = ctxt;
4626    ctxt->myDoc = NULL;
4627    ctxt->wellFormed = 1;
4628    ctxt->replaceEntities = 0;
4629    ctxt->html = 2;
4630    ctxt->record_info = 0;
4631    ctxt->validate = 0;
4632    ctxt->nbChars = 0;
4633    ctxt->checkIndex = 0;
4634    xmlInitNodeInfoSeq(&ctxt->node_seq);
4635}
4636
4637/**
4638 * docbFreeParserCtxt:
4639 * @ctxt:  an SGML parser context
4640 *
4641 * Free all the memory used by a parser context. However the parsed
4642 * document in ctxt->myDoc is not freed.
4643 */
4644
4645void
4646docbFreeParserCtxt(docbParserCtxtPtr ctxt)
4647{
4648    xmlFreeParserCtxt(ctxt);
4649}
4650
4651/**
4652 * docbCreateDocParserCtxt :
4653 * @cur:  a pointer to an array of xmlChar
4654 * @encoding:  a free form C string describing the SGML document encoding, or NULL
4655 *
4656 * Create a parser context for an SGML document.
4657 *
4658 * Returns the new parser context or NULL
4659 */
4660static docbParserCtxtPtr
4661docbCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
4662    docbParserCtxtPtr ctxt;
4663    docbParserInputPtr input;
4664    /* sgmlCharEncoding enc; */
4665
4666    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
4667    if (ctxt == NULL) {
4668        perror("malloc");
4669       return(NULL);
4670    }
4671    docbInitParserCtxt(ctxt);
4672    input = (docbParserInputPtr) xmlMalloc(sizeof(docbParserInput));
4673    if (input == NULL) {
4674        perror("malloc");
4675       xmlFree(ctxt);
4676       return(NULL);
4677    }
4678    memset(input, 0, sizeof(docbParserInput));
4679
4680    input->line = 1;
4681    input->col = 1;
4682    input->base = cur;
4683    input->cur = cur;
4684
4685    inputPush(ctxt, input);
4686    return(ctxt);
4687}
4688
4689/************************************************************************
4690 *                                                                     *
4691 *             Progressive parsing interfaces                          *
4692 *                                                                     *
4693 ************************************************************************/
4694
4695/**
4696 * docbParseLookupSequence:
4697 * @ctxt:  an SGML parser context
4698 * @first:  the first char to lookup
4699 * @next:  the next char to lookup or zero
4700 * @third:  the next char to lookup or zero
4701 *
4702 * Try to find if a sequence (first, next, third) or  just (first next) or
4703 * (first) is available in the input stream.
4704 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4705 * to avoid rescanning sequences of bytes, it DOES change the state of the
4706 * parser, do not use liberally.
4707 * This is basically similar to xmlParseLookupSequence()
4708 *
4709 * Returns the index to the current parsing point if the full sequence
4710 *      is available, -1 otherwise.
4711 */
4712static int
4713docbParseLookupSequence(docbParserCtxtPtr ctxt, xmlChar first,
4714                       xmlChar next, xmlChar third) {
4715    int base, len;
4716    docbParserInputPtr in;
4717    const xmlChar *buf;
4718
4719    in = ctxt->input;
4720    if (in == NULL) return(-1);
4721    base = in->cur - in->base;
4722    if (base < 0) return(-1);
4723    if (ctxt->checkIndex > base)
4724        base = ctxt->checkIndex;
4725    if (in->buf == NULL) {
4726       buf = in->base;
4727       len = in->length;
4728    } else {
4729       buf = in->buf->buffer->content;
4730       len = in->buf->buffer->use;
4731    }
4732    /* take into account the sequence length */
4733    if (third) len -= 2;
4734    else if (next) len --;
4735    for (;base < len;base++) {
4736        if (buf[base] == first) {
4737           if (third != 0) {
4738               if ((buf[base + 1] != next) ||
4739                   (buf[base + 2] != third)) continue;
4740           } else if (next != 0) {
4741               if (buf[base + 1] != next) continue;
4742           }
4743           ctxt->checkIndex = 0;
4744#ifdef DEBUG_PUSH
4745           if (next == 0)
4746               xmlGenericError(xmlGenericErrorContext,
4747                       "HPP: lookup '%c' found at %d\n",
4748                       first, base);
4749           else if (third == 0)
4750               xmlGenericError(xmlGenericErrorContext,
4751                       "HPP: lookup '%c%c' found at %d\n",
4752                       first, next, base);
4753           else
4754               xmlGenericError(xmlGenericErrorContext,
4755                       "HPP: lookup '%c%c%c' found at %d\n",
4756                       first, next, third, base);
4757#endif
4758           return(base - (in->cur - in->base));
4759       }
4760    }
4761    ctxt->checkIndex = base;
4762#ifdef DEBUG_PUSH
4763    if (next == 0)
4764       xmlGenericError(xmlGenericErrorContext,
4765               "HPP: lookup '%c' failed\n", first);
4766    else if (third == 0)
4767       xmlGenericError(xmlGenericErrorContext,
4768               "HPP: lookup '%c%c' failed\n", first, next);
4769    else
4770       xmlGenericError(xmlGenericErrorContext,
4771               "HPP: lookup '%c%c%c' failed\n", first, next, third);
4772#endif
4773    return(-1);
4774}
4775
4776/**
4777 * docbParseTryOrFinish:
4778 * @ctxt:  an SGML parser context
4779 * @terminate:  last chunk indicator
4780 *
4781 * Try to progress on parsing
4782 *
4783 * Returns zero if no parsing was possible
4784 */
4785static int
4786docbParseTryOrFinish(docbParserCtxtPtr ctxt, int terminate) {
4787    int ret = 0;
4788    docbParserInputPtr in;
4789    int avail = 0;
4790    xmlChar cur, next;
4791
4792#ifdef DEBUG_PUSH
4793    switch (ctxt->instate) {
4794       case XML_PARSER_EOF:
4795           xmlGenericError(xmlGenericErrorContext,
4796                   "HPP: try EOF\n"); break;
4797       case XML_PARSER_START:
4798           xmlGenericError(xmlGenericErrorContext,
4799                   "HPP: try START\n"); break;
4800       case XML_PARSER_MISC:
4801           xmlGenericError(xmlGenericErrorContext,
4802                   "HPP: try MISC\n");break;
4803       case XML_PARSER_COMMENT:
4804           xmlGenericError(xmlGenericErrorContext,
4805                   "HPP: try COMMENT\n");break;
4806       case XML_PARSER_PROLOG:
4807           xmlGenericError(xmlGenericErrorContext,
4808                   "HPP: try PROLOG\n");break;
4809       case XML_PARSER_START_TAG:
4810           xmlGenericError(xmlGenericErrorContext,
4811                   "HPP: try START_TAG\n");break;
4812       case XML_PARSER_CONTENT:
4813           xmlGenericError(xmlGenericErrorContext,
4814                   "HPP: try CONTENT\n");break;
4815       case XML_PARSER_CDATA_SECTION:
4816           xmlGenericError(xmlGenericErrorContext,
4817                   "HPP: try CDATA_SECTION\n");break;
4818       case XML_PARSER_END_TAG:
4819           xmlGenericError(xmlGenericErrorContext,
4820                   "HPP: try END_TAG\n");break;
4821       case XML_PARSER_ENTITY_DECL:
4822           xmlGenericError(xmlGenericErrorContext,
4823                   "HPP: try ENTITY_DECL\n");break;
4824       case XML_PARSER_ENTITY_VALUE:
4825           xmlGenericError(xmlGenericErrorContext,
4826                   "HPP: try ENTITY_VALUE\n");break;
4827       case XML_PARSER_ATTRIBUTE_VALUE:
4828           xmlGenericError(xmlGenericErrorContext,
4829                   "HPP: try ATTRIBUTE_VALUE\n");break;
4830       case XML_PARSER_DTD:
4831           xmlGenericError(xmlGenericErrorContext,
4832                   "HPP: try DTD\n");break;
4833       case XML_PARSER_EPILOG:
4834           xmlGenericError(xmlGenericErrorContext,
4835                   "HPP: try EPILOG\n");break;
4836       case XML_PARSER_PI:
4837           xmlGenericError(xmlGenericErrorContext,
4838                   "HPP: try PI\n");break;
4839    }
4840#endif
4841
4842    while (1) {
4843
4844       in = ctxt->input;
4845       if (in == NULL) break;
4846       if (in->buf == NULL)
4847           avail = in->length - (in->cur - in->base);
4848       else
4849           avail = in->buf->buffer->use - (in->cur - in->base);
4850       if ((avail == 0) && (terminate)) {
4851           docbAutoClose(ctxt, NULL);
4852           if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4853               /*
4854                * SAX: end of the document processing.
4855                */
4856               ctxt->instate = XML_PARSER_EOF;
4857               if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4858                   ctxt->sax->endDocument(ctxt->userData);
4859           }
4860       }
4861        if (avail < 1)
4862           goto done;
4863        switch (ctxt->instate) {
4864            case XML_PARSER_EOF:
4865               /*
4866                * Document parsing is done !
4867                */
4868               goto done;
4869            case XML_PARSER_START:
4870               /*
4871                * Very first chars read from the document flow.
4872                */
4873               cur = in->cur[0];
4874               if (IS_BLANK(cur)) {
4875                   SKIP_BLANKS;
4876                   if (in->buf == NULL)
4877                       avail = in->length - (in->cur - in->base);
4878                   else
4879                       avail = in->buf->buffer->use - (in->cur - in->base);
4880               }
4881               if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4882                   ctxt->sax->setDocumentLocator(ctxt->userData,
4883                                                 &xmlDefaultSAXLocator);
4884               if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4885                   (!ctxt->disableSAX))
4886                   ctxt->sax->startDocument(ctxt->userData);
4887
4888               cur = in->cur[0];
4889               next = in->cur[1];
4890               if ((cur == '<') && (next == '!') &&
4891                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
4892                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
4893                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4894                   (UPP(8) == 'E')) {
4895                   if ((!terminate) &&
4896                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
4897                       goto done;
4898#ifdef DEBUG_PUSH
4899                   xmlGenericError(xmlGenericErrorContext,
4900                           "HPP: Parsing internal subset\n");
4901#endif
4902                   docbParseDocTypeDecl(ctxt);
4903                   ctxt->instate = XML_PARSER_PROLOG;
4904#ifdef DEBUG_PUSH
4905                   xmlGenericError(xmlGenericErrorContext,
4906                           "HPP: entering PROLOG\n");
4907#endif
4908                } else {
4909                   ctxt->instate = XML_PARSER_MISC;
4910               }
4911#ifdef DEBUG_PUSH
4912               xmlGenericError(xmlGenericErrorContext,
4913                       "HPP: entering MISC\n");
4914#endif
4915               break;
4916            case XML_PARSER_MISC:
4917               SKIP_BLANKS;
4918               if (in->buf == NULL)
4919                   avail = in->length - (in->cur - in->base);
4920               else
4921                   avail = in->buf->buffer->use - (in->cur - in->base);
4922               if (avail < 2)
4923                   goto done;
4924               cur = in->cur[0];
4925               next = in->cur[1];
4926               if ((cur == '<') && (next == '!') &&
4927                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
4928                   if ((!terminate) &&
4929                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
4930                       goto done;
4931#ifdef DEBUG_PUSH
4932                   xmlGenericError(xmlGenericErrorContext,
4933                           "HPP: Parsing Comment\n");
4934#endif
4935                   docbParseComment(ctxt);
4936                   ctxt->instate = XML_PARSER_MISC;
4937               } else if ((cur == '<') && (next == '!') &&
4938                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
4939                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
4940                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4941                   (UPP(8) == 'E')) {
4942                   if ((!terminate) &&
4943                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
4944                       goto done;
4945#ifdef DEBUG_PUSH
4946                   xmlGenericError(xmlGenericErrorContext,
4947                           "HPP: Parsing internal subset\n");
4948#endif
4949                   docbParseDocTypeDecl(ctxt);
4950                   ctxt->instate = XML_PARSER_PROLOG;
4951#ifdef DEBUG_PUSH
4952                   xmlGenericError(xmlGenericErrorContext,
4953                           "HPP: entering PROLOG\n");
4954#endif
4955               } else if ((cur == '<') && (next == '!') &&
4956                          (avail < 9)) {
4957                   goto done;
4958               } else {
4959                   ctxt->instate = XML_PARSER_START_TAG;
4960#ifdef DEBUG_PUSH
4961                   xmlGenericError(xmlGenericErrorContext,
4962                           "HPP: entering START_TAG\n");
4963#endif
4964               }
4965               break;
4966            case XML_PARSER_PROLOG:
4967               SKIP_BLANKS;
4968               if (in->buf == NULL)
4969                   avail = in->length - (in->cur - in->base);
4970               else
4971                   avail = in->buf->buffer->use - (in->cur - in->base);
4972               if (avail < 2)
4973                   goto done;
4974               cur = in->cur[0];
4975               next = in->cur[1];
4976               if ((cur == '<') && (next == '!') &&
4977                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
4978                   if ((!terminate) &&
4979                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
4980                       goto done;
4981#ifdef DEBUG_PUSH
4982                   xmlGenericError(xmlGenericErrorContext,
4983                           "HPP: Parsing Comment\n");
4984#endif
4985                   docbParseComment(ctxt);
4986                   ctxt->instate = XML_PARSER_PROLOG;
4987               } else if ((cur == '<') && (next == '!') &&
4988                          (avail < 4)) {
4989                   goto done;
4990               } else {
4991                   ctxt->instate = XML_PARSER_START_TAG;
4992#ifdef DEBUG_PUSH
4993                   xmlGenericError(xmlGenericErrorContext,
4994                           "HPP: entering START_TAG\n");
4995#endif
4996               }
4997               break;
4998            case XML_PARSER_EPILOG:
4999               if (in->buf == NULL)
5000                   avail = in->length - (in->cur - in->base);
5001               else
5002                   avail = in->buf->buffer->use - (in->cur - in->base);
5003               if (avail < 1)
5004                   goto done;
5005               cur = in->cur[0];
5006               if (IS_BLANK(cur)) {
5007                   docbParseCharData(ctxt);
5008                   goto done;
5009               }
5010               if (avail < 2)
5011                   goto done;
5012               next = in->cur[1];
5013               if ((cur == '<') && (next == '!') &&
5014                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5015                   if ((!terminate) &&
5016                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5017                       goto done;
5018#ifdef DEBUG_PUSH
5019                   xmlGenericError(xmlGenericErrorContext,
5020                           "HPP: Parsing Comment\n");
5021#endif
5022                   docbParseComment(ctxt);
5023                   ctxt->instate = XML_PARSER_EPILOG;
5024               } else if ((cur == '<') && (next == '!') &&
5025                          (avail < 4)) {
5026                   goto done;
5027               } else {
5028                   ctxt->errNo = XML_ERR_DOCUMENT_END;
5029                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5030                       ctxt->sax->error(ctxt->userData,
5031                           "Extra content at the end of the document\n");
5032                   ctxt->wellFormed = 0;
5033                   ctxt->instate = XML_PARSER_EOF;
5034#ifdef DEBUG_PUSH
5035                   xmlGenericError(xmlGenericErrorContext,
5036                           "HPP: entering EOF\n");
5037#endif
5038                   if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5039                       ctxt->sax->endDocument(ctxt->userData);
5040                   goto done;
5041               }
5042               break;
5043            case XML_PARSER_START_TAG: {
5044               xmlChar *name, *oldname;
5045               int depth = ctxt->nameNr;
5046               docbElemDescPtr info;
5047
5048               if (avail < 2)
5049                   goto done;
5050               cur = in->cur[0];
5051               if (cur != '<') {
5052                   ctxt->instate = XML_PARSER_CONTENT;
5053#ifdef DEBUG_PUSH
5054                   xmlGenericError(xmlGenericErrorContext,
5055                           "HPP: entering CONTENT\n");
5056#endif
5057                   break;
5058               }
5059               if ((!terminate) &&
5060                   (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5061                   goto done;
5062
5063               oldname = xmlStrdup(ctxt->name);
5064               docbParseStartTag(ctxt);
5065               name = ctxt->name;
5066#ifdef DEBUG
5067               if (oldname == NULL)
5068                   xmlGenericError(xmlGenericErrorContext,
5069                           "Start of element %s\n", name);
5070               else if (name == NULL)
5071                   xmlGenericError(xmlGenericErrorContext,
5072                           "Start of element failed, was %s\n",
5073                           oldname);
5074               else
5075                   xmlGenericError(xmlGenericErrorContext,
5076                           "Start of element %s, was %s\n",
5077                           name, oldname);
5078#endif
5079               if (((depth == ctxt->nameNr) &&
5080                    (xmlStrEqual(oldname, ctxt->name))) ||
5081                   (name == NULL)) {
5082                   if (CUR == '>')
5083                       NEXT;
5084                   if (oldname != NULL)
5085                       xmlFree(oldname);
5086                   break;
5087               }
5088               if (oldname != NULL)
5089                   xmlFree(oldname);
5090
5091               /*
5092                * Lookup the info for that element.
5093                */
5094               info = docbTagLookup(name);
5095               if (info == NULL) {
5096                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5097                       ctxt->sax->error(ctxt->userData, "Tag %s unknown\n",
5098                                        name);
5099                   ctxt->wellFormed = 0;
5100               } else if (info->depr) {
5101                   /***************************
5102                   if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
5103                       ctxt->sax->warning(ctxt->userData,
5104                                          "Tag %s is deprecated\n",
5105                                          name);
5106                    ***************************/
5107               }
5108
5109               /*
5110                * Check for an Empty Element labelled the XML/SGML way
5111                */
5112               if ((CUR == '/') && (NXT(1) == '>')) {
5113                   SKIP(2);
5114                   if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5115                       ctxt->sax->endElement(ctxt->userData, name);
5116                   oldname = docbnamePop(ctxt);
5117#ifdef DEBUG
5118                   xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n",
5119                           oldname);
5120#endif
5121                   if (oldname != NULL)
5122                       xmlFree(oldname);
5123                   ctxt->instate = XML_PARSER_CONTENT;
5124#ifdef DEBUG_PUSH
5125                   xmlGenericError(xmlGenericErrorContext,
5126                           "HPP: entering CONTENT\n");
5127#endif
5128                   break;
5129               }
5130
5131               if (CUR == '>') {
5132                   NEXT;
5133               } else {
5134                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5135                       ctxt->sax->error(ctxt->userData,
5136                                        "Couldn't find end of Start Tag %s\n",
5137                                        name);
5138                   ctxt->wellFormed = 0;
5139
5140                   /*
5141                    * end of parsing of this node.
5142                    */
5143                   if (xmlStrEqual(name, ctxt->name)) {
5144                       nodePop(ctxt);
5145                       oldname = docbnamePop(ctxt);
5146#ifdef DEBUG
5147                       xmlGenericError(xmlGenericErrorContext,
5148                        "End of start tag problem: popping out %s\n", oldname);
5149#endif
5150                       if (oldname != NULL)
5151                           xmlFree(oldname);
5152                   }
5153
5154                   ctxt->instate = XML_PARSER_CONTENT;
5155#ifdef DEBUG_PUSH
5156                   xmlGenericError(xmlGenericErrorContext,
5157                           "HPP: entering CONTENT\n");
5158#endif
5159                   break;
5160               }
5161
5162               /*
5163                * Check for an Empty Element from DTD definition
5164                */
5165               if ((info != NULL) && (info->empty)) {
5166                   if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5167                       ctxt->sax->endElement(ctxt->userData, name);
5168                   oldname = docbnamePop(ctxt);
5169#ifdef DEBUG
5170                   xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
5171#endif
5172                   if (oldname != NULL)
5173                       xmlFree(oldname);
5174               }
5175               ctxt->instate = XML_PARSER_CONTENT;
5176#ifdef DEBUG_PUSH
5177               xmlGenericError(xmlGenericErrorContext,
5178                       "HPP: entering CONTENT\n");
5179#endif
5180                break;
5181           }
5182            case XML_PARSER_CONTENT: {
5183               long cons;
5184                /*
5185                * Handle preparsed entities and charRef
5186                */
5187               if (ctxt->token != 0) {
5188                   xmlChar chr[2] = { 0 , 0 } ;
5189
5190                   chr[0] = (xmlChar) ctxt->token;
5191                   docbCheckParagraph(ctxt);
5192                   if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
5193                       ctxt->sax->characters(ctxt->userData, chr, 1);
5194                   ctxt->token = 0;
5195                   ctxt->checkIndex = 0;
5196               }
5197               if ((avail == 1) && (terminate)) {
5198                   cur = in->cur[0];
5199                   if ((cur != '<') && (cur != '&')) {
5200                       if (ctxt->sax != NULL) {
5201                           if (IS_BLANK(cur)) {
5202                               if (ctxt->sax->ignorableWhitespace != NULL)
5203                                   ctxt->sax->ignorableWhitespace(
5204                                           ctxt->userData, &cur, 1);
5205                           } else {
5206                               docbCheckParagraph(ctxt);
5207                               if (ctxt->sax->characters != NULL)
5208                                   ctxt->sax->characters(
5209                                           ctxt->userData, &cur, 1);
5210                           }
5211                       }
5212                       ctxt->token = 0;
5213                       ctxt->checkIndex = 0;
5214                       NEXT;
5215                   }
5216                   break;
5217               }
5218               if (avail < 2)
5219                   goto done;
5220               cur = in->cur[0];
5221               next = in->cur[1];
5222               cons = ctxt->nbChars;
5223               /*
5224                * Sometimes DOCTYPE arrives in the middle of the document
5225                */
5226               if ((cur == '<') && (next == '!') &&
5227                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
5228                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
5229                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5230                   (UPP(8) == 'E')) {
5231                   if ((!terminate) &&
5232                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5233                       goto done;
5234                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5235                       ctxt->sax->error(ctxt->userData,
5236                            "Misplaced DOCTYPE declaration\n");
5237                   ctxt->wellFormed = 0;
5238                   docbParseDocTypeDecl(ctxt);
5239               } else if ((cur == '<') && (next == '!') &&
5240                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5241                   if ((!terminate) &&
5242                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5243                       goto done;
5244#ifdef DEBUG_PUSH
5245                   xmlGenericError(xmlGenericErrorContext,
5246                           "HPP: Parsing Comment\n");
5247#endif
5248                   docbParseComment(ctxt);
5249                   ctxt->instate = XML_PARSER_CONTENT;
5250               } else if ((cur == '<') && (next == '!') && (avail < 4)) {
5251                   goto done;
5252               } else if ((cur == '<') && (next == '/')) {
5253                   ctxt->instate = XML_PARSER_END_TAG;
5254                   ctxt->checkIndex = 0;
5255#ifdef DEBUG_PUSH
5256                   xmlGenericError(xmlGenericErrorContext,
5257                           "HPP: entering END_TAG\n");
5258#endif
5259                   break;
5260               } else if (cur == '<') {
5261                   ctxt->instate = XML_PARSER_START_TAG;
5262                   ctxt->checkIndex = 0;
5263#ifdef DEBUG_PUSH
5264                   xmlGenericError(xmlGenericErrorContext,
5265                           "HPP: entering START_TAG\n");
5266#endif
5267                   break;
5268               } else if (cur == '&') {
5269                   if ((!terminate) &&
5270                       (docbParseLookupSequence(ctxt, ';', 0, 0) < 0))
5271                       goto done;
5272#ifdef DEBUG_PUSH
5273                   xmlGenericError(xmlGenericErrorContext,
5274                           "HPP: Parsing Reference\n");
5275#endif
5276                   /* TODO: check generation of subtrees if noent !!! */
5277                   docbParseReference(ctxt);
5278               } else {
5279                   /* TODO Avoid the extra copy, handle directly !!!!!! */
5280                   /*
5281                    * Goal of the following test is :
5282                    *  - minimize calls to the SAX 'character' callback
5283                    *    when they are mergeable
5284                    */
5285                   if ((ctxt->inputNr == 1) &&
5286                       (avail < DOCB_PARSER_BIG_BUFFER_SIZE)) {
5287                       if ((!terminate) &&
5288                           (docbParseLookupSequence(ctxt, '<', 0, 0) < 0))
5289                           goto done;
5290                    }
5291                   ctxt->checkIndex = 0;
5292#ifdef DEBUG_PUSH
5293                   xmlGenericError(xmlGenericErrorContext,
5294                           "HPP: Parsing char data\n");
5295#endif
5296                   docbParseCharData(ctxt);
5297               }
5298               if (cons == ctxt->nbChars) {
5299                   if (ctxt->node != NULL) {
5300                       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5301                           ctxt->sax->error(ctxt->userData,
5302                                "detected an error in element content\n");
5303                       ctxt->wellFormed = 0;
5304                       NEXT;
5305                   }
5306                   break;
5307               }
5308
5309               break;
5310           }
5311            case XML_PARSER_END_TAG:
5312               if (avail < 2)
5313                   goto done;
5314               if ((!terminate) &&
5315                   (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5316                   goto done;
5317               docbParseEndTag(ctxt);
5318               if (ctxt->nameNr == 0) {
5319                   ctxt->instate = XML_PARSER_EPILOG;
5320               } else {
5321                   ctxt->instate = XML_PARSER_CONTENT;
5322               }
5323               ctxt->checkIndex = 0;
5324#ifdef DEBUG_PUSH
5325               xmlGenericError(xmlGenericErrorContext,
5326                       "HPP: entering CONTENT\n");
5327#endif
5328               break;
5329            case XML_PARSER_CDATA_SECTION:
5330               xmlGenericError(xmlGenericErrorContext,
5331                       "HPP: internal error, state == CDATA\n");
5332               ctxt->instate = XML_PARSER_CONTENT;
5333               ctxt->checkIndex = 0;
5334#ifdef DEBUG_PUSH
5335               xmlGenericError(xmlGenericErrorContext,
5336                       "HPP: entering CONTENT\n");
5337#endif
5338               break;
5339            case XML_PARSER_DTD:
5340               xmlGenericError(xmlGenericErrorContext,
5341                       "HPP: internal error, state == DTD\n");
5342               ctxt->instate = XML_PARSER_CONTENT;
5343               ctxt->checkIndex = 0;
5344#ifdef DEBUG_PUSH
5345               xmlGenericError(xmlGenericErrorContext,
5346                       "HPP: entering CONTENT\n");
5347#endif
5348               break;
5349            case XML_PARSER_COMMENT:
5350               xmlGenericError(xmlGenericErrorContext,
5351                       "HPP: internal error, state == COMMENT\n");
5352               ctxt->instate = XML_PARSER_CONTENT;
5353               ctxt->checkIndex = 0;
5354#ifdef DEBUG_PUSH
5355               xmlGenericError(xmlGenericErrorContext,
5356                       "HPP: entering CONTENT\n");
5357#endif
5358               break;
5359            case XML_PARSER_PI:
5360               xmlGenericError(xmlGenericErrorContext,
5361                       "HPP: internal error, state == PI\n");
5362               ctxt->instate = XML_PARSER_CONTENT;
5363               ctxt->checkIndex = 0;
5364#ifdef DEBUG_PUSH
5365               xmlGenericError(xmlGenericErrorContext,
5366                       "HPP: entering CONTENT\n");
5367#endif
5368               break;
5369            case XML_PARSER_ENTITY_DECL:
5370               xmlGenericError(xmlGenericErrorContext,
5371                       "HPP: internal error, state == ENTITY_DECL\n");
5372               ctxt->instate = XML_PARSER_CONTENT;
5373               ctxt->checkIndex = 0;
5374#ifdef DEBUG_PUSH
5375               xmlGenericError(xmlGenericErrorContext,
5376                       "HPP: entering CONTENT\n");
5377#endif
5378               break;
5379            case XML_PARSER_ENTITY_VALUE:
5380               xmlGenericError(xmlGenericErrorContext,
5381                       "HPP: internal error, state == ENTITY_VALUE\n");
5382               ctxt->instate = XML_PARSER_CONTENT;
5383               ctxt->checkIndex = 0;
5384#ifdef DEBUG_PUSH
5385               xmlGenericError(xmlGenericErrorContext,
5386                       "HPP: entering DTD\n");
5387#endif
5388               break;
5389            case XML_PARSER_ATTRIBUTE_VALUE:
5390               xmlGenericError(xmlGenericErrorContext,
5391                       "HPP: internal error, state == ATTRIBUTE_VALUE\n");
5392               ctxt->instate = XML_PARSER_START_TAG;
5393               ctxt->checkIndex = 0;
5394#ifdef DEBUG_PUSH
5395               xmlGenericError(xmlGenericErrorContext,
5396                       "HPP: entering START_TAG\n");
5397#endif
5398               break;
5399           case XML_PARSER_SYSTEM_LITERAL:
5400               xmlGenericError(xmlGenericErrorContext,
5401                       "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
5402               ctxt->instate = XML_PARSER_CONTENT;
5403               ctxt->checkIndex = 0;
5404#ifdef DEBUG_PUSH
5405               xmlGenericError(xmlGenericErrorContext,
5406                       "HPP: entering CONTENT\n");
5407#endif
5408               break;
5409
5410           case XML_PARSER_IGNORE:
5411               xmlGenericError(xmlGenericErrorContext,
5412                       "HPP: internal error, state == XML_PARSER_IGNORE\n");
5413               ctxt->instate = XML_PARSER_CONTENT;
5414               ctxt->checkIndex = 0;
5415#ifdef DEBUG_PUSH
5416               xmlGenericError(xmlGenericErrorContext,
5417                       "HPP: entering CONTENT\n");
5418#endif
5419               break;
5420       }
5421    }
5422done:
5423    if ((avail == 0) && (terminate)) {
5424       docbAutoClose(ctxt, NULL);
5425       if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5426           /*
5427            * SAX: end of the document processing.
5428            */
5429           ctxt->instate = XML_PARSER_EOF;
5430           if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5431               ctxt->sax->endDocument(ctxt->userData);
5432       }
5433    }
5434    if ((ctxt->myDoc != NULL) &&
5435       ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5436        (ctxt->instate == XML_PARSER_EPILOG))) {
5437       xmlDtdPtr dtd;
5438       dtd = ctxt->myDoc->intSubset;
5439       if (dtd == NULL)
5440           ctxt->myDoc->intSubset =
5441               xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "SGML",
5442                   BAD_CAST "-//W3C//DTD SGML 4.0 Transitional//EN",
5443                   BAD_CAST "http://www.w3.org/TR/REC-docbook/loose.dtd");
5444    }
5445#ifdef DEBUG_PUSH
5446    xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5447#endif
5448    return(ret);
5449}
5450
5451/**
5452 * docbParseChunk:
5453 * @ctxt:  an XML parser context
5454 * @chunk:  an char array
5455 * @size:  the size in byte of the chunk
5456 * @terminate:  last chunk indicator
5457 *
5458 * Parse a Chunk of memory
5459 *
5460 * Returns zero if no error, the xmlParserErrors otherwise.
5461 */
5462int
5463docbParseChunk(docbParserCtxtPtr ctxt, const char *chunk, int size,
5464              int terminate) {
5465    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5466        (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
5467       int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5468       int cur = ctxt->input->cur - ctxt->input->base;
5469
5470       xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5471       ctxt->input->base = ctxt->input->buf->buffer->content + base;
5472       ctxt->input->cur = ctxt->input->base + cur;
5473#ifdef DEBUG_PUSH
5474       xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5475#endif
5476
5477       if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5478           docbParseTryOrFinish(ctxt, terminate);
5479    } else if (ctxt->instate != XML_PARSER_EOF) {
5480       xmlParserInputBufferPush(ctxt->input->buf, 0, "");
5481        docbParseTryOrFinish(ctxt, terminate);
5482    }
5483    if (terminate) {
5484       if ((ctxt->instate != XML_PARSER_EOF) &&
5485           (ctxt->instate != XML_PARSER_EPILOG) &&
5486           (ctxt->instate != XML_PARSER_MISC)) {
5487           ctxt->errNo = XML_ERR_DOCUMENT_END;
5488           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5489               ctxt->sax->error(ctxt->userData,
5490                   "Extra content at the end of the document\n");
5491           ctxt->wellFormed = 0;
5492       }
5493       if (ctxt->instate != XML_PARSER_EOF) {
5494           if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5495               ctxt->sax->endDocument(ctxt->userData);
5496       }
5497       ctxt->instate = XML_PARSER_EOF;
5498    }
5499    return((xmlParserErrors) ctxt->errNo);
5500}
5501
5502/************************************************************************
5503 *                                                                     *
5504 *                     User entry points                               *
5505 *                                                                     *
5506 ************************************************************************/
5507
5508/**
5509 * docbCreatePushParserCtxt :
5510 * @sax:  a SAX handler
5511 * @user_data:  The user data returned on SAX callbacks
5512 * @chunk:  a pointer to an array of chars
5513 * @size:  number of chars in the array
5514 * @filename:  an optional file name or URI
5515 * @enc:  an optional encoding
5516 *
5517 * Create a parser context for using the DocBook SGML parser in push mode
5518 * To allow content encoding detection, @size should be >= 4
5519 * The value of @filename is used for fetching external entities
5520 * and error/warning reports.
5521 *
5522 * Returns the new parser context or NULL
5523 */
5524docbParserCtxtPtr
5525docbCreatePushParserCtxt(docbSAXHandlerPtr sax, void *user_data,
5526                         const char *chunk, int size, const char *filename,
5527                        xmlCharEncoding enc) {
5528    docbParserCtxtPtr ctxt;
5529    docbParserInputPtr inputStream;
5530    xmlParserInputBufferPtr buf;
5531
5532    buf = xmlAllocParserInputBuffer(enc);
5533    if (buf == NULL) return(NULL);
5534
5535    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
5536    if (ctxt == NULL) {
5537       xmlFree(buf);
5538       return(NULL);
5539    }
5540    memset(ctxt, 0, sizeof(docbParserCtxt));
5541    docbInitParserCtxt(ctxt);
5542    if (sax != NULL) {
5543       if (ctxt->sax != &docbDefaultSAXHandler)
5544           xmlFree(ctxt->sax);
5545       ctxt->sax = (docbSAXHandlerPtr) xmlMalloc(sizeof(docbSAXHandler));
5546       if (ctxt->sax == NULL) {
5547           xmlFree(buf);
5548           xmlFree(ctxt);
5549           return(NULL);
5550       }
5551       memcpy(ctxt->sax, sax, sizeof(docbSAXHandler));
5552       if (user_data != NULL)
5553           ctxt->userData = user_data;
5554    }
5555    if (filename == NULL) {
5556       ctxt->directory = NULL;
5557    } else {
5558        ctxt->directory = xmlParserGetDirectory(filename);
5559    }
5560
5561    inputStream = docbNewInputStream(ctxt);
5562    if (inputStream == NULL) {
5563       xmlFreeParserCtxt(ctxt);
5564       return(NULL);
5565    }
5566
5567    if (filename == NULL)
5568       inputStream->filename = NULL;
5569    else
5570       inputStream->filename = xmlMemStrdup(filename);
5571    inputStream->buf = buf;
5572    inputStream->base = inputStream->buf->buffer->content;
5573    inputStream->cur = inputStream->buf->buffer->content;
5574
5575    inputPush(ctxt, inputStream);
5576
5577    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5578        (ctxt->input->buf != NULL))  {
5579       xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5580#ifdef DEBUG_PUSH
5581       xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5582#endif
5583    }
5584
5585    return(ctxt);
5586}
5587
5588/**
5589 * docbSAXParseDoc :
5590 * @cur:  a pointer to an array of xmlChar
5591 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5592 * @sax:  the SAX handler block
5593 * @userData: if using SAX, this pointer will be provided on callbacks.
5594 *
5595 * parse an SGML in-memory document and build a tree.
5596 * It use the given SAX function block to handle the parsing callback.
5597 * If sax is NULL, fallback to the default DOM tree building routines.
5598 *
5599 * Returns the resulting document tree
5600 */
5601
5602docbDocPtr
5603docbSAXParseDoc(xmlChar *cur, const char *encoding, docbSAXHandlerPtr sax, void *userData) {
5604    docbDocPtr ret;
5605    docbParserCtxtPtr ctxt;
5606
5607    if (cur == NULL) return(NULL);
5608
5609
5610    ctxt = docbCreateDocParserCtxt(cur, encoding);
5611    if (ctxt == NULL) return(NULL);
5612    if (sax != NULL) {
5613        ctxt->sax = sax;
5614        ctxt->userData = userData;
5615    }
5616
5617    docbParseDocument(ctxt);
5618    ret = ctxt->myDoc;
5619    if (sax != NULL) {
5620       ctxt->sax = NULL;
5621       ctxt->userData = NULL;
5622    }
5623    docbFreeParserCtxt(ctxt);
5624
5625    return(ret);
5626}
5627
5628/**
5629 * docbParseDoc :
5630 * @cur:  a pointer to an array of xmlChar
5631 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5632 *
5633 * parse an SGML in-memory document and build a tree.
5634 *
5635 * Returns the resulting document tree
5636 */
5637
5638docbDocPtr
5639docbParseDoc(xmlChar *cur, const char *encoding) {
5640    return(docbSAXParseDoc(cur, encoding, NULL, NULL));
5641}
5642
5643
5644/**
5645 * docbCreateFileParserCtxt :
5646 * @filename:  the filename
5647 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5648 *
5649 * Create a parser context for a file content.
5650 * Automatic support for ZLIB/Compress compressed document is provided
5651 * by default if found at compile-time.
5652 *
5653 * Returns the new parser context or NULL
5654 */
5655docbParserCtxtPtr
5656docbCreateFileParserCtxt(const char *filename, const char *encoding)
5657{
5658    docbParserCtxtPtr ctxt;
5659    docbParserInputPtr inputStream;
5660    xmlParserInputBufferPtr buf;
5661    /* sgmlCharEncoding enc; */
5662
5663    buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
5664    if (buf == NULL) return(NULL);
5665
5666    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
5667    if (ctxt == NULL) {
5668        perror("malloc");
5669       return(NULL);
5670    }
5671    memset(ctxt, 0, sizeof(docbParserCtxt));
5672    docbInitParserCtxt(ctxt);
5673    inputStream = (docbParserInputPtr) xmlMalloc(sizeof(docbParserInput));
5674    if (inputStream == NULL) {
5675        perror("malloc");
5676       xmlFree(ctxt);
5677       return(NULL);
5678    }
5679    memset(inputStream, 0, sizeof(docbParserInput));
5680
5681    inputStream->filename = xmlMemStrdup(filename);
5682    inputStream->line = 1;
5683    inputStream->col = 1;
5684    inputStream->buf = buf;
5685    inputStream->directory = NULL;
5686
5687    inputStream->base = inputStream->buf->buffer->content;
5688    inputStream->cur = inputStream->buf->buffer->content;
5689    inputStream->free = NULL;
5690
5691    inputPush(ctxt, inputStream);
5692    return(ctxt);
5693}
5694
5695/**
5696 * docbSAXParseFile :
5697 * @filename:  the filename
5698 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5699 * @sax:  the SAX handler block
5700 * @userData: if using SAX, this pointer will be provided on callbacks.
5701 *
5702 * parse an SGML file and build a tree. Automatic support for ZLIB/Compress
5703 * compressed document is provided by default if found at compile-time.
5704 * It use the given SAX function block to handle the parsing callback.
5705 * If sax is NULL, fallback to the default DOM tree building routines.
5706 *
5707 * Returns the resulting document tree
5708 */
5709
5710docbDocPtr
5711docbSAXParseFile(const char *filename, const char *encoding, docbSAXHandlerPtr sax,
5712                 void *userData) {
5713    docbDocPtr ret;
5714    docbParserCtxtPtr ctxt;
5715    docbSAXHandlerPtr oldsax = NULL;
5716
5717    ctxt = docbCreateFileParserCtxt(filename, encoding);
5718    if (ctxt == NULL) return(NULL);
5719    if (sax != NULL) {
5720       oldsax = ctxt->sax;
5721        ctxt->sax = sax;
5722        ctxt->userData = userData;
5723    }
5724
5725    docbParseDocument(ctxt);
5726
5727    ret = ctxt->myDoc;
5728    if (sax != NULL) {
5729        ctxt->sax = oldsax;
5730        ctxt->userData = NULL;
5731    }
5732    docbFreeParserCtxt(ctxt);
5733
5734    return(ret);
5735}
5736
5737/**
5738 * docbParseFile :
5739 * @filename:  the filename
5740 * @encoding:  a free form C string describing document encoding, or NULL
5741 *
5742 * parse a Docbook SGML file and build a tree. Automatic support for
5743 * ZLIB/Compress compressed document is provided by default if found
5744 * at compile-time.
5745 *
5746 * Returns the resulting document tree
5747 */
5748
5749docbDocPtr
5750docbParseFile(const char *filename, const char *encoding) {
5751    return(docbSAXParseFile(filename, encoding, NULL, NULL));
5752}
5753
5754#endif /* LIBXML_DOCB_ENABLED */
5755