12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162 |
- #define IN_LIBXML
- #include "libxml.h"
- #if defined(WIN32) && !defined (__CYGWIN__)
- #define XML_DIR_SEP '\\'
- #else
- #define XML_DIR_SEP '/'
- #endif
- #include <string.h>
- #ifdef HAVE_CTYPE_H
- #include <ctype.h>
- #endif
- #ifdef HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
- #ifdef HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #ifdef HAVE_FCNTL_H
- #include <fcntl.h>
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef HAVE_ZLIB_H
- #include <zlib.h>
- #endif
- #include <libxml/xmlmemory.h>
- #include <libxml/tree.h>
- #include <libxml/parser.h>
- #include <libxml/parserInternals.h>
- #include <libxml/valid.h>
- #include <libxml/entities.h>
- #include <libxml/xmlerror.h>
- #include <libxml/encoding.h>
- #include <libxml/valid.h>
- #include <libxml/xmlIO.h>
- #include <libxml/uri.h>
- #include <libxml/dict.h>
- #include <libxml/SAX.h>
- #ifdef LIBXML_CATALOG_ENABLED
- #include <libxml/catalog.h>
- #endif
- #include <libxml/globals.h>
- #include <libxml/chvalid.h>
- void
- xmlCheckVersion(int version) {
- int myversion = (int) LIBXML_VERSION;
- xmlInitParser();
- if ((myversion / 10000) != (version / 10000)) {
- xmlGenericError(xmlGenericErrorContext,
- "Fatal: program compiled against libxml %d using libxml %d\n",
- (version / 10000), (myversion / 10000));
- fprintf(stderr,
- "Fatal: program compiled against libxml %d using libxml %d\n",
- (version / 10000), (myversion / 10000));
- }
- if ((myversion / 100) < (version / 100)) {
- xmlGenericError(xmlGenericErrorContext,
- "Warning: program compiled against libxml %d using older %d\n",
- (version / 100), (myversion / 100));
- }
- }
- void
- xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
- {
- if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
- (ctxt->instate == XML_PARSER_EOF))
- return;
- if (ctxt != NULL) {
- ctxt->errNo = XML_ERR_NO_MEMORY;
- ctxt->instate = XML_PARSER_EOF;
- ctxt->disableSAX = 1;
- }
- if (extra)
- __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
- NULL, NULL, 0, 0,
- "Memory allocation failed : %s\n", extra);
- else
- __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
- XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
- NULL, NULL, 0, 0, "Memory allocation failed\n");
- }
- void
- __xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
- const char *msg, const xmlChar * str1, const xmlChar * str2)
- {
- if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
- (ctxt->instate == XML_PARSER_EOF))
- return;
- if (ctxt != NULL)
- ctxt->errNo = xmlerr;
- __xmlRaiseError(NULL, NULL, NULL,
- ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
- NULL, 0, (const char *) str1, (const char *) str2,
- NULL, 0, 0, msg, str1, str2);
- if (ctxt != NULL) {
- ctxt->wellFormed = 0;
- if (ctxt->recovery == 0)
- ctxt->disableSAX = 1;
- }
- }
- static void
- xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
- {
- if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
- (ctxt->instate == XML_PARSER_EOF))
- return;
- if (ctxt != NULL)
- ctxt->errNo = XML_ERR_INTERNAL_ERROR;
- __xmlRaiseError(NULL, NULL, NULL,
- ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
- XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
- 0, 0, msg, str);
- if (ctxt != NULL) {
- ctxt->wellFormed = 0;
- if (ctxt->recovery == 0)
- ctxt->disableSAX = 1;
- }
- }
- static void
- xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
- const char *msg, int val)
- {
- if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
- (ctxt->instate == XML_PARSER_EOF))
- return;
- if (ctxt != NULL)
- ctxt->errNo = error;
- __xmlRaiseError(NULL, NULL, NULL,
- ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
- NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
- if (ctxt != NULL) {
- ctxt->wellFormed = 0;
- if (ctxt->recovery == 0)
- ctxt->disableSAX = 1;
- }
- }
- int
- xmlIsLetter(int c) {
- return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
- }
- #define LINE_LEN 80
- #ifdef DEBUG_INPUT
- #define CHECK_BUFFER(in) check_buffer(in)
- static
- void check_buffer(xmlParserInputPtr in) {
- if (in->base != in->buf->buffer->content) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlParserInput: base mismatch problem\n");
- }
- if (in->cur < in->base) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlParserInput: cur < base problem\n");
- }
- if (in->cur > in->base + in->buf->buffer->use) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlParserInput: cur > base + use problem\n");
- }
- xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
- (int) in, (int) in->buf->buffer->content, in->cur - in->base,
- in->buf->buffer->use, in->buf->buffer->size);
- }
- #else
- #define CHECK_BUFFER(in)
- #endif
- int
- xmlParserInputRead(xmlParserInputPtr in, int len) {
- int ret;
- int used;
- int indx;
- if (in == NULL) return(-1);
- #ifdef DEBUG_INPUT
- xmlGenericError(xmlGenericErrorContext, "Read\n");
- #endif
- if (in->buf == NULL) return(-1);
- if (in->base == NULL) return(-1);
- if (in->cur == NULL) return(-1);
- if (in->buf->buffer == NULL) return(-1);
- if (in->buf->readcallback == NULL) return(-1);
- CHECK_BUFFER(in);
- used = in->cur - in->buf->buffer->content;
- ret = xmlBufferShrink(in->buf->buffer, used);
- if (ret > 0) {
- in->cur -= ret;
- in->consumed += ret;
- }
- ret = xmlParserInputBufferRead(in->buf, len);
- if (in->base != in->buf->buffer->content) {
-
- indx = in->cur - in->base;
- in->base = in->buf->buffer->content;
- in->cur = &in->buf->buffer->content[indx];
- }
- in->end = &in->buf->buffer->content[in->buf->buffer->use];
- CHECK_BUFFER(in);
- return(ret);
- }
- int
- xmlParserInputGrow(xmlParserInputPtr in, int len) {
- int ret;
- int indx;
- if (in == NULL) return(-1);
- #ifdef DEBUG_INPUT
- xmlGenericError(xmlGenericErrorContext, "Grow\n");
- #endif
- if (in->buf == NULL) return(-1);
- if (in->base == NULL) return(-1);
- if (in->cur == NULL) return(-1);
- if (in->buf->buffer == NULL) return(-1);
- CHECK_BUFFER(in);
- indx = in->cur - in->base;
- if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
- CHECK_BUFFER(in);
- return(0);
- }
- if (in->buf->readcallback != NULL)
- ret = xmlParserInputBufferGrow(in->buf, len);
- else
- return(0);
-
- if (in->base != in->buf->buffer->content) {
-
- indx = in->cur - in->base;
- in->base = in->buf->buffer->content;
- in->cur = &in->buf->buffer->content[indx];
- }
- in->end = &in->buf->buffer->content[in->buf->buffer->use];
- CHECK_BUFFER(in);
- return(ret);
- }
- void
- xmlParserInputShrink(xmlParserInputPtr in) {
- int used;
- int ret;
- int indx;
- #ifdef DEBUG_INPUT
- xmlGenericError(xmlGenericErrorContext, "Shrink\n");
- #endif
- if (in == NULL) return;
- if (in->buf == NULL) return;
- if (in->base == NULL) return;
- if (in->cur == NULL) return;
- if (in->buf->buffer == NULL) return;
- CHECK_BUFFER(in);
- used = in->cur - in->buf->buffer->content;
-
- if (used > INPUT_CHUNK) {
- ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
- if (ret > 0) {
- in->cur -= ret;
- in->consumed += ret;
- }
- in->end = &in->buf->buffer->content[in->buf->buffer->use];
- }
- CHECK_BUFFER(in);
- if (in->buf->buffer->use > INPUT_CHUNK) {
- return;
- }
- xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
- if (in->base != in->buf->buffer->content) {
-
- indx = in->cur - in->base;
- in->base = in->buf->buffer->content;
- in->cur = &in->buf->buffer->content[indx];
- }
- in->end = &in->buf->buffer->content[in->buf->buffer->use];
- CHECK_BUFFER(in);
- }
- void
- xmlNextChar(xmlParserCtxtPtr ctxt)
- {
- if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
- (ctxt->input == NULL))
- return;
- if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
- if ((*ctxt->input->cur == 0) &&
- (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
- (ctxt->instate != XML_PARSER_COMMENT)) {
-
- xmlPopInput(ctxt);
- } else {
- const unsigned char *cur;
- unsigned char c;
-
- if (*(ctxt->input->cur) == '\n') {
- ctxt->input->line++; ctxt->input->col = 1;
- } else
- ctxt->input->col++;
-
- cur = ctxt->input->cur;
- c = *cur;
- if (c & 0x80) {
- if (c == 0xC0)
- goto encoding_error;
- if (cur[1] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if ((cur[1] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xe0) == 0xe0) {
- unsigned int val;
- if (cur[2] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if ((cur[2] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xf0) == 0xf0) {
- if (cur[3] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if (((c & 0xf8) != 0xf0) ||
- ((cur[3] & 0xc0) != 0x80))
- goto encoding_error;
-
- ctxt->input->cur += 4;
- val = (cur[0] & 0x7) << 18;
- val |= (cur[1] & 0x3f) << 12;
- val |= (cur[2] & 0x3f) << 6;
- val |= cur[3] & 0x3f;
- } else {
-
- ctxt->input->cur += 3;
- val = (cur[0] & 0xf) << 12;
- val |= (cur[1] & 0x3f) << 6;
- val |= cur[2] & 0x3f;
- }
- if (((val > 0xd7ff) && (val < 0xe000)) ||
- ((val > 0xfffd) && (val < 0x10000)) ||
- (val >= 0x110000)) {
- xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
- "Char 0x%X out of allowed range\n",
- val);
- }
- } else
-
- ctxt->input->cur += 2;
- } else
-
- ctxt->input->cur++;
- ctxt->nbChars++;
- if (*ctxt->input->cur == 0)
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- }
- } else {
-
- if (*(ctxt->input->cur) == '\n') {
- ctxt->input->line++; ctxt->input->col = 1;
- } else
- ctxt->input->col++;
- ctxt->input->cur++;
- ctxt->nbChars++;
- if (*ctxt->input->cur == 0)
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- }
- if ((*ctxt->input->cur == '%') && (!ctxt->html))
- xmlParserHandlePEReference(ctxt);
- if ((*ctxt->input->cur == 0) &&
- (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
- xmlPopInput(ctxt);
- return;
- encoding_error:
-
- if ((ctxt == NULL) || (ctxt->input == NULL) ||
- (ctxt->input->end - ctxt->input->cur < 4)) {
- __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
- "Input is not proper UTF-8, indicate encoding !\n",
- NULL, NULL);
- } else {
- char buffer[150];
- snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
- ctxt->input->cur[0], ctxt->input->cur[1],
- ctxt->input->cur[2], ctxt->input->cur[3]);
- __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
- "Input is not proper UTF-8, indicate encoding !\n%s",
- BAD_CAST buffer, NULL);
- }
- ctxt->charset = XML_CHAR_ENCODING_8859_1;
- ctxt->input->cur++;
- return;
- }
- int
- xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
- if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
- if (ctxt->instate == XML_PARSER_EOF)
- return(0);
- if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
- *len = 1;
- return((int) *ctxt->input->cur);
- }
- if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
-
- const unsigned char *cur = ctxt->input->cur;
- unsigned char c;
- unsigned int val;
- c = *cur;
- if (c & 0x80) {
- if (((c & 0x40) == 0) || (c == 0xC0))
- goto encoding_error;
- if (cur[1] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if ((cur[1] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xe0) == 0xe0) {
- if (cur[2] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if ((cur[2] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xf0) == 0xf0) {
- if (cur[3] == 0) {
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- cur = ctxt->input->cur;
- }
- if (((c & 0xf8) != 0xf0) ||
- ((cur[3] & 0xc0) != 0x80))
- goto encoding_error;
-
- *len = 4;
- val = (cur[0] & 0x7) << 18;
- val |= (cur[1] & 0x3f) << 12;
- val |= (cur[2] & 0x3f) << 6;
- val |= cur[3] & 0x3f;
- if (val < 0x10000)
- goto encoding_error;
- } else {
-
- *len = 3;
- val = (cur[0] & 0xf) << 12;
- val |= (cur[1] & 0x3f) << 6;
- val |= cur[2] & 0x3f;
- if (val < 0x800)
- goto encoding_error;
- }
- } else {
-
- *len = 2;
- val = (cur[0] & 0x1f) << 6;
- val |= cur[1] & 0x3f;
- if (val < 0x80)
- goto encoding_error;
- }
- if (!IS_CHAR(val)) {
- xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
- "Char 0x%X out of allowed range\n", val);
- }
- return(val);
- } else {
-
- *len = 1;
- if (*ctxt->input->cur == 0)
- xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
- if ((*ctxt->input->cur == 0) &&
- (ctxt->input->end > ctxt->input->cur)) {
- xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
- "Char 0x0 out of allowed range\n", 0);
- }
- if (*ctxt->input->cur == 0xD) {
- if (ctxt->input->cur[1] == 0xA) {
- ctxt->nbChars++;
- ctxt->input->cur++;
- }
- return(0xA);
- }
- return((int) *ctxt->input->cur);
- }
- }
-
- *len = 1;
- if (*ctxt->input->cur == 0xD) {
- if (ctxt->input->cur[1] == 0xA) {
- ctxt->nbChars++;
- ctxt->input->cur++;
- }
- return(0xA);
- }
- return((int) *ctxt->input->cur);
- encoding_error:
-
- if (ctxt->input->end - ctxt->input->cur < 4) {
- *len = 0;
- return(0);
- }
-
- {
- char buffer[150];
- snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
- ctxt->input->cur[0], ctxt->input->cur[1],
- ctxt->input->cur[2], ctxt->input->cur[3]);
- __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
- "Input is not proper UTF-8, indicate encoding !\n%s",
- BAD_CAST buffer, NULL);
- }
- ctxt->charset = XML_CHAR_ENCODING_8859_1;
- *len = 1;
- return((int) *ctxt->input->cur);
- }
- int
- xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
- {
- if ((len == NULL) || (cur == NULL)) return(0);
- if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
-
- unsigned char c;
- unsigned int val;
- c = *cur;
- if (c & 0x80) {
- if ((cur[1] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xe0) == 0xe0) {
- if ((cur[2] & 0xc0) != 0x80)
- goto encoding_error;
- if ((c & 0xf0) == 0xf0) {
- if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
- goto encoding_error;
-
- *len = 4;
- val = (cur[0] & 0x7) << 18;
- val |= (cur[1] & 0x3f) << 12;
- val |= (cur[2] & 0x3f) << 6;
- val |= cur[3] & 0x3f;
- } else {
-
- *len = 3;
- val = (cur[0] & 0xf) << 12;
- val |= (cur[1] & 0x3f) << 6;
- val |= cur[2] & 0x3f;
- }
- } else {
-
- *len = 2;
- val = (cur[0] & 0x1f) << 6;
- val |= cur[1] & 0x3f;
- }
- if (!IS_CHAR(val)) {
- xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
- "Char 0x%X out of allowed range\n", val);
- }
- return (val);
- } else {
-
- *len = 1;
- return ((int) *cur);
- }
- }
-
- *len = 1;
- return ((int) *cur);
- encoding_error:
-
- if ((ctxt == NULL) || (ctxt->input == NULL) ||
- (ctxt->input->end - ctxt->input->cur < 4)) {
- *len = 0;
- return(0);
- }
-
- {
- char buffer[150];
- snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
- ctxt->input->cur[0], ctxt->input->cur[1],
- ctxt->input->cur[2], ctxt->input->cur[3]);
- __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
- "Input is not proper UTF-8, indicate encoding !\n%s",
- BAD_CAST buffer, NULL);
- }
- *len = 1;
- return ((int) *cur);
- }
- int
- xmlCopyCharMultiByte(xmlChar *out, int val) {
- if (out == NULL) return(0);
-
- if (val >= 0x80) {
- xmlChar *savedout = out;
- int bits;
- if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
- else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
- else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
- else {
- xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
- "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
- val);
- return(0);
- }
- for ( ; bits >= 0; bits-= 6)
- *out++= ((val >> bits) & 0x3F) | 0x80 ;
- return (out - savedout);
- }
- *out = (xmlChar) val;
- return 1;
- }
- int
- xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
- if (out == NULL) return(0);
-
- if (val >= 0x80) {
- return(xmlCopyCharMultiByte (out, val));
- }
- *out = (xmlChar) val;
- return 1;
- }
- int
- xmlCharEncFirstLineInt(xmlCharEncodingHandler *handler, xmlBufferPtr out,
- xmlBufferPtr in, int len);
- static int
- xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
- xmlCharEncodingHandlerPtr handler, int len);
- static int
- xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
- xmlCharEncodingHandlerPtr handler, int len);
- int
- xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
- {
- xmlCharEncodingHandlerPtr handler;
- int len = -1;
- if (ctxt == NULL) return(-1);
- switch (enc) {
- case XML_CHAR_ENCODING_ERROR:
- __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
- "encoding unknown\n", NULL, NULL);
- return(-1);
- case XML_CHAR_ENCODING_NONE:
-
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
- return(0);
- case XML_CHAR_ENCODING_UTF8:
-
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
-
- if ((ctxt->input != NULL) &&
- (ctxt->input->cur[0] == 0xEF) &&
- (ctxt->input->cur[1] == 0xBB) &&
- (ctxt->input->cur[2] == 0xBF)) {
- ctxt->input->cur += 3;
- }
- return(0);
- case XML_CHAR_ENCODING_UTF16LE:
- case XML_CHAR_ENCODING_UTF16BE:
-
- if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
- (ctxt->input->cur[0] == 0xEF) &&
- (ctxt->input->cur[1] == 0xBB) &&
- (ctxt->input->cur[2] == 0xBF)) {
- ctxt->input->cur += 3;
- }
- len = 90;
- break;
- case XML_CHAR_ENCODING_UCS2:
- len = 90;
- break;
- case XML_CHAR_ENCODING_UCS4BE:
- case XML_CHAR_ENCODING_UCS4LE:
- case XML_CHAR_ENCODING_UCS4_2143:
- case XML_CHAR_ENCODING_UCS4_3412:
- len = 180;
- break;
- case XML_CHAR_ENCODING_EBCDIC:
- case XML_CHAR_ENCODING_8859_1:
- case XML_CHAR_ENCODING_8859_2:
- case XML_CHAR_ENCODING_8859_3:
- case XML_CHAR_ENCODING_8859_4:
- case XML_CHAR_ENCODING_8859_5:
- case XML_CHAR_ENCODING_8859_6:
- case XML_CHAR_ENCODING_8859_7:
- case XML_CHAR_ENCODING_8859_8:
- case XML_CHAR_ENCODING_8859_9:
- case XML_CHAR_ENCODING_ASCII:
- case XML_CHAR_ENCODING_2022_JP:
- case XML_CHAR_ENCODING_SHIFT_JIS:
- case XML_CHAR_ENCODING_EUC_JP:
- len = 45;
- break;
- }
- handler = xmlGetCharEncodingHandler(enc);
- if (handler == NULL) {
-
- switch (enc) {
- case XML_CHAR_ENCODING_ASCII:
-
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
- return(0);
- case XML_CHAR_ENCODING_UTF16LE:
- break;
- case XML_CHAR_ENCODING_UTF16BE:
- break;
- case XML_CHAR_ENCODING_UCS4LE:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "USC4 little endian", NULL);
- break;
- case XML_CHAR_ENCODING_UCS4BE:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "USC4 big endian", NULL);
- break;
- case XML_CHAR_ENCODING_EBCDIC:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "EBCDIC", NULL);
- break;
- case XML_CHAR_ENCODING_UCS4_2143:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "UCS4 2143", NULL);
- break;
- case XML_CHAR_ENCODING_UCS4_3412:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "UCS4 3412", NULL);
- break;
- case XML_CHAR_ENCODING_UCS2:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "UCS2", NULL);
- break;
- case XML_CHAR_ENCODING_8859_1:
- case XML_CHAR_ENCODING_8859_2:
- case XML_CHAR_ENCODING_8859_3:
- case XML_CHAR_ENCODING_8859_4:
- case XML_CHAR_ENCODING_8859_5:
- case XML_CHAR_ENCODING_8859_6:
- case XML_CHAR_ENCODING_8859_7:
- case XML_CHAR_ENCODING_8859_8:
- case XML_CHAR_ENCODING_8859_9:
-
- if ((ctxt->inputNr == 1) &&
- (ctxt->encoding == NULL) &&
- (ctxt->input != NULL) &&
- (ctxt->input->encoding != NULL)) {
- ctxt->encoding = xmlStrdup(ctxt->input->encoding);
- }
- ctxt->charset = enc;
- return(0);
- case XML_CHAR_ENCODING_2022_JP:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "ISO-2022-JP", NULL);
- break;
- case XML_CHAR_ENCODING_SHIFT_JIS:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "Shift_JIS", NULL);
- break;
- case XML_CHAR_ENCODING_EUC_JP:
- __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
- "encoding not supported %s\n",
- BAD_CAST "EUC-JP", NULL);
- break;
- default:
- break;
- }
- }
- if (handler == NULL)
- return(-1);
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
- return(xmlSwitchToEncodingInt(ctxt, handler, len));
- }
- static int
- xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
- xmlCharEncodingHandlerPtr handler, int len)
- {
- int nbchars;
- if (handler == NULL)
- return (-1);
- if (input == NULL)
- return (-1);
- if (input->buf != NULL) {
- if (input->buf->encoder != NULL) {
-
- if (input->buf->encoder == handler)
- return (0);
-
-
- xmlCharEncCloseFunc(input->buf->encoder);
- input->buf->encoder = handler;
- return (0);
- }
- input->buf->encoder = handler;
-
- if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) {
- int processed;
- unsigned int use;
-
- if ((handler->name != NULL) &&
- (!strcmp(handler->name, "UTF-16LE") ||
- !strcmp(handler->name, "UTF-16")) &&
- (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
- input->cur += 2;
- }
- if ((handler->name != NULL) &&
- (!strcmp(handler->name, "UTF-16BE")) &&
- (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
- input->cur += 2;
- }
-
- if ((handler->name != NULL) &&
- (!strcmp(handler->name, "UTF-8")) &&
- (input->cur[0] == 0xEF) &&
- (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
- input->cur += 3;
- }
-
- processed = input->cur - input->base;
- xmlBufferShrink(input->buf->buffer, processed);
- input->buf->raw = input->buf->buffer;
- input->buf->buffer = xmlBufferCreate();
- input->buf->rawconsumed = processed;
- use = input->buf->raw->use;
- if (ctxt->html) {
-
- nbchars = xmlCharEncInFunc(input->buf->encoder,
- input->buf->buffer,
- input->buf->raw);
- } else {
-
- nbchars = xmlCharEncFirstLineInt(input->buf->encoder,
- input->buf->buffer,
- input->buf->raw,
- len);
- }
- if (nbchars < 0) {
- xmlErrInternal(ctxt,
- "switching encoding: encoder error\n",
- NULL);
- return (-1);
- }
- input->buf->rawconsumed += use - input->buf->raw->use;
- input->base = input->cur = input->buf->buffer->content;
- input->end = &input->base[input->buf->buffer->use];
- }
- return (0);
- } else if (input->length == 0) {
-
- xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
- return (-1);
- }
- return (0);
- }
- int
- xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
- xmlCharEncodingHandlerPtr handler) {
- return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
- }
- static int
- xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
- xmlCharEncodingHandlerPtr handler, int len) {
- int ret = 0;
- if (handler != NULL) {
- if (ctxt->input != NULL) {
- ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
- } else {
- xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
- NULL);
- return(-1);
- }
-
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
- } else
- return(-1);
- return(ret);
- }
- int
- xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
- {
- return (xmlSwitchToEncodingInt(ctxt, handler, -1));
- }
- void
- xmlFreeInputStream(xmlParserInputPtr input) {
- if (input == NULL) return;
- if (input->filename != NULL) xmlFree((char *) input->filename);
- if (input->directory != NULL) xmlFree((char *) input->directory);
- if (input->encoding != NULL) xmlFree((char *) input->encoding);
- if (input->version != NULL) xmlFree((char *) input->version);
- if ((input->free != NULL) && (input->base != NULL))
- input->free((xmlChar *) input->base);
- if (input->buf != NULL)
- xmlFreeParserInputBuffer(input->buf);
- xmlFree(input);
- }
- xmlParserInputPtr
- xmlNewInputStream(xmlParserCtxtPtr ctxt) {
- xmlParserInputPtr input;
- static int id = 0;
- input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
- if (input == NULL) {
- xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
- return(NULL);
- }
- memset(input, 0, sizeof(xmlParserInput));
- input->line = 1;
- input->col = 1;
- input->standalone = -1;
-
- input->id = id++;
- return(input);
- }
- xmlParserInputPtr
- xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
- xmlCharEncoding enc) {
- xmlParserInputPtr inputStream;
- if (input == NULL) return(NULL);
- if (xmlParserDebugEntities)
- xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
- inputStream = xmlNewInputStream(ctxt);
- if (inputStream == NULL) {
- return(NULL);
- }
- inputStream->filename = NULL;
- inputStream->buf = input;
- inputStream->base = inputStream->buf->buffer->content;
- inputStream->cur = inputStream->buf->buffer->content;
- inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
- if (enc != XML_CHAR_ENCODING_NONE) {
- xmlSwitchEncoding(ctxt, enc);
- }
- return(inputStream);
- }
- xmlParserInputPtr
- xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
- xmlParserInputPtr input;
- if (entity == NULL) {
- xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
- NULL);
- return(NULL);
- }
- if (xmlParserDebugEntities)
- xmlGenericError(xmlGenericErrorContext,
- "new input from entity: %s\n", entity->name);
- if (entity->content == NULL) {
- switch (entity->etype) {
- case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
- xmlErrInternal(ctxt, "Cannot parse entity %s\n",
- entity->name);
- break;
- case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
- case XML_EXTERNAL_PARAMETER_ENTITY:
- return(xmlLoadExternalEntity((char *) entity->URI,
- (char *) entity->ExternalID, ctxt));
- case XML_INTERNAL_GENERAL_ENTITY:
- xmlErrInternal(ctxt,
- "Internal entity %s without content !\n",
- entity->name);
- break;
- case XML_INTERNAL_PARAMETER_ENTITY:
- xmlErrInternal(ctxt,
- "Internal parameter entity %s without content !\n",
- entity->name);
- break;
- case XML_INTERNAL_PREDEFINED_ENTITY:
- xmlErrInternal(ctxt,
- "Predefined entity %s without content !\n",
- entity->name);
- break;
- }
- return(NULL);
- }
- input = xmlNewInputStream(ctxt);
- if (input == NULL) {
- return(NULL);
- }
- if (entity->URI != NULL)
- input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
- input->base = entity->content;
- input->cur = entity->content;
- input->length = entity->length;
- input->end = &entity->content[input->length];
- return(input);
- }
- xmlParserInputPtr
- xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
- xmlParserInputPtr input;
- if (buffer == NULL) {
- xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
- NULL);
- return(NULL);
- }
- if (xmlParserDebugEntities)
- xmlGenericError(xmlGenericErrorContext,
- "new fixed input: %.30s\n", buffer);
- input = xmlNewInputStream(ctxt);
- if (input == NULL) {
- xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
- return(NULL);
- }
- input->base = buffer;
- input->cur = buffer;
- input->length = xmlStrlen(buffer);
- input->end = &buffer[input->length];
- return(input);
- }
- xmlParserInputPtr
- xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
- xmlParserInputBufferPtr buf;
- xmlParserInputPtr inputStream;
- char *directory = NULL;
- xmlChar *URI = NULL;
- if (xmlParserDebugEntities)
- xmlGenericError(xmlGenericErrorContext,
- "new input from file: %s\n", filename);
- if (ctxt == NULL) return(NULL);
- buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
- if (buf == NULL) {
- if (filename == NULL)
- __xmlLoaderErr(ctxt,
- "failed to load external entity: NULL filename \n",
- NULL);
- else
- __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
- (const char *) filename);
- return(NULL);
- }
- inputStream = xmlNewInputStream(ctxt);
- if (inputStream == NULL)
- return(NULL);
- inputStream->buf = buf;
- inputStream = xmlCheckHTTPInput(ctxt, inputStream);
- if (inputStream == NULL)
- return(NULL);
-
- if (inputStream->filename == NULL)
- URI = xmlStrdup((xmlChar *) filename);
- else
- URI = xmlStrdup((xmlChar *) inputStream->filename);
- directory = xmlParserGetDirectory((const char *) URI);
- if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
- inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
- if (URI != NULL) xmlFree((char *) URI);
- inputStream->directory = directory;
- inputStream->base = inputStream->buf->buffer->content;
- inputStream->cur = inputStream->buf->buffer->content;
- inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
- if ((ctxt->directory == NULL) && (directory != NULL))
- ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
- return(inputStream);
- }
- int
- xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
- {
- xmlParserInputPtr input;
- if(ctxt==NULL) {
- xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
- return(-1);
- }
- xmlDefaultSAXHandlerInit();
- if (ctxt->dict == NULL)
- ctxt->dict = xmlDictCreate();
- if (ctxt->dict == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- return(-1);
- }
- if (ctxt->sax == NULL)
- ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
- if (ctxt->sax == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- return(-1);
- }
- else
- xmlSAXVersion(ctxt->sax, 2);
- ctxt->maxatts = 0;
- ctxt->atts = NULL;
-
- if (ctxt->inputTab == NULL) {
- ctxt->inputTab = (xmlParserInputPtr *)
- xmlMalloc(5 * sizeof(xmlParserInputPtr));
- ctxt->inputMax = 5;
- }
- if (ctxt->inputTab == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- ctxt->inputNr = 0;
- ctxt->inputMax = 0;
- ctxt->input = NULL;
- return(-1);
- }
- while ((input = inputPop(ctxt)) != NULL) {
- xmlFreeInputStream(input);
- }
- ctxt->inputNr = 0;
- ctxt->input = NULL;
- ctxt->version = NULL;
- ctxt->encoding = NULL;
- ctxt->standalone = -1;
- ctxt->hasExternalSubset = 0;
- ctxt->hasPErefs = 0;
- ctxt->html = 0;
- ctxt->external = 0;
- ctxt->instate = XML_PARSER_START;
- ctxt->token = 0;
- ctxt->directory = NULL;
-
- if (ctxt->nodeTab == NULL) {
- ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
- ctxt->nodeMax = 10;
- }
- if (ctxt->nodeTab == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- ctxt->nodeNr = 0;
- ctxt->nodeMax = 0;
- ctxt->node = NULL;
- ctxt->inputNr = 0;
- ctxt->inputMax = 0;
- ctxt->input = NULL;
- return(-1);
- }
- ctxt->nodeNr = 0;
- ctxt->node = NULL;
-
- if (ctxt->nameTab == NULL) {
- ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
- ctxt->nameMax = 10;
- }
- if (ctxt->nameTab == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- ctxt->nodeNr = 0;
- ctxt->nodeMax = 0;
- ctxt->node = NULL;
- ctxt->inputNr = 0;
- ctxt->inputMax = 0;
- ctxt->input = NULL;
- ctxt->nameNr = 0;
- ctxt->nameMax = 0;
- ctxt->name = NULL;
- return(-1);
- }
- ctxt->nameNr = 0;
- ctxt->name = NULL;
-
- if (ctxt->spaceTab == NULL) {
- ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
- ctxt->spaceMax = 10;
- }
- if (ctxt->spaceTab == NULL) {
- xmlErrMemory(NULL, "cannot initialize parser context\n");
- ctxt->nodeNr = 0;
- ctxt->nodeMax = 0;
- ctxt->node = NULL;
- ctxt->inputNr = 0;
- ctxt->inputMax = 0;
- ctxt->input = NULL;
- ctxt->nameNr = 0;
- ctxt->nameMax = 0;
- ctxt->name = NULL;
- ctxt->spaceNr = 0;
- ctxt->spaceMax = 0;
- ctxt->space = NULL;
- return(-1);
- }
- ctxt->spaceNr = 1;
- ctxt->spaceMax = 10;
- ctxt->spaceTab[0] = -1;
- ctxt->space = &ctxt->spaceTab[0];
- ctxt->userData = ctxt;
- ctxt->myDoc = NULL;
- ctxt->wellFormed = 1;
- ctxt->nsWellFormed = 1;
- ctxt->valid = 1;
- ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
- ctxt->validate = xmlDoValidityCheckingDefaultValue;
- ctxt->pedantic = xmlPedanticParserDefaultValue;
- ctxt->linenumbers = xmlLineNumbersDefaultValue;
- ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
- if (ctxt->keepBlanks == 0)
- ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
- ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
- ctxt->vctxt.userData = ctxt;
- ctxt->vctxt.error = xmlParserValidityError;
- ctxt->vctxt.warning = xmlParserValidityWarning;
- if (ctxt->validate) {
- if (xmlGetWarningsDefaultValue == 0)
- ctxt->vctxt.warning = NULL;
- else
- ctxt->vctxt.warning = xmlParserValidityWarning;
- ctxt->vctxt.nodeMax = 0;
- }
- ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
- ctxt->record_info = 0;
- ctxt->nbChars = 0;
- ctxt->checkIndex = 0;
- ctxt->inSubset = 0;
- ctxt->errNo = XML_ERR_OK;
- ctxt->depth = 0;
- ctxt->charset = XML_CHAR_ENCODING_UTF8;
- ctxt->catalogs = NULL;
- ctxt->nbentities = 0;
- xmlInitNodeInfoSeq(&ctxt->node_seq);
- return(0);
- }
- void
- xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
- {
- xmlParserInputPtr input;
- if (ctxt == NULL) return;
- while ((input = inputPop(ctxt)) != NULL) {
- xmlFreeInputStream(input);
- }
- if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
- if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
- if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
- if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
- if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
- if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
- if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
- if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
- if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
- #ifdef LIBXML_SAX1_ENABLED
- if ((ctxt->sax != NULL) &&
- (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
- #else
- if (ctxt->sax != NULL)
- #endif
- xmlFree(ctxt->sax);
- if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
- if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
- if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
- if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
- if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
- if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
- if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
- if (ctxt->attsDefault != NULL)
- xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
- if (ctxt->attsSpecial != NULL)
- xmlHashFree(ctxt->attsSpecial, NULL);
- if (ctxt->freeElems != NULL) {
- xmlNodePtr cur, next;
- cur = ctxt->freeElems;
- while (cur != NULL) {
- next = cur->next;
- xmlFree(cur);
- cur = next;
- }
- }
- if (ctxt->freeAttrs != NULL) {
- xmlAttrPtr cur, next;
- cur = ctxt->freeAttrs;
- while (cur != NULL) {
- next = cur->next;
- xmlFree(cur);
- cur = next;
- }
- }
-
- if (ctxt->lastError.message != NULL)
- xmlFree(ctxt->lastError.message);
- if (ctxt->lastError.file != NULL)
- xmlFree(ctxt->lastError.file);
- if (ctxt->lastError.str1 != NULL)
- xmlFree(ctxt->lastError.str1);
- if (ctxt->lastError.str2 != NULL)
- xmlFree(ctxt->lastError.str2);
- if (ctxt->lastError.str3 != NULL)
- xmlFree(ctxt->lastError.str3);
- #ifdef LIBXML_CATALOG_ENABLED
- if (ctxt->catalogs != NULL)
- xmlCatalogFreeLocal(ctxt->catalogs);
- #endif
- xmlFree(ctxt);
- }
- xmlParserCtxtPtr
- xmlNewParserCtxt(void)
- {
- xmlParserCtxtPtr ctxt;
- ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
- if (ctxt == NULL) {
- xmlErrMemory(NULL, "cannot allocate parser context\n");
- return(NULL);
- }
- memset(ctxt, 0, sizeof(xmlParserCtxt));
- if (xmlInitParserCtxt(ctxt) < 0) {
- xmlFreeParserCtxt(ctxt);
- return(NULL);
- }
- return(ctxt);
- }
- void
- xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
- {
- if (ctxt==NULL)
- return;
- xmlClearNodeInfoSeq(&ctxt->node_seq);
- xmlCtxtReset(ctxt);
- }
- const xmlParserNodeInfo *
- xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
- {
- unsigned long pos;
- if ((ctx == NULL) || (node == NULL))
- return (NULL);
-
- pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
- if (pos < ctx->node_seq.length
- && ctx->node_seq.buffer[pos].node == node)
- return &ctx->node_seq.buffer[pos];
- else
- return NULL;
- }
- void
- xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
- {
- if (seq == NULL)
- return;
- seq->length = 0;
- seq->maximum = 0;
- seq->buffer = NULL;
- }
- void
- xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
- {
- if (seq == NULL)
- return;
- if (seq->buffer != NULL)
- xmlFree(seq->buffer);
- xmlInitNodeInfoSeq(seq);
- }
- unsigned long
- xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
- const xmlNodePtr node)
- {
- unsigned long upper, lower, middle;
- int found = 0;
- if ((seq == NULL) || (node == NULL))
- return ((unsigned long) -1);
-
- lower = 1;
- upper = seq->length;
- middle = 0;
- while (lower <= upper && !found) {
- middle = lower + (upper - lower) / 2;
- if (node == seq->buffer[middle - 1].node)
- found = 1;
- else if (node < seq->buffer[middle - 1].node)
- upper = middle - 1;
- else
- lower = middle + 1;
- }
-
- if (middle == 0 || seq->buffer[middle - 1].node < node)
- return middle;
- else
- return middle - 1;
- }
- void
- xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
- const xmlParserNodeInfoPtr info)
- {
- unsigned long pos;
- if ((ctxt == NULL) || (info == NULL)) return;
-
- pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
- info->node);
- if ((pos < ctxt->node_seq.length) &&
- (ctxt->node_seq.buffer != NULL) &&
- (ctxt->node_seq.buffer[pos].node == info->node)) {
- ctxt->node_seq.buffer[pos] = *info;
- }
-
- else {
- if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
- xmlParserNodeInfo *tmp_buffer;
- unsigned int byte_size;
- if (ctxt->node_seq.maximum == 0)
- ctxt->node_seq.maximum = 2;
- byte_size = (sizeof(*ctxt->node_seq.buffer) *
- (2 * ctxt->node_seq.maximum));
- if (ctxt->node_seq.buffer == NULL)
- tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
- else
- tmp_buffer =
- (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
- byte_size);
- if (tmp_buffer == NULL) {
- xmlErrMemory(ctxt, "failed to allocate buffer\n");
- return;
- }
- ctxt->node_seq.buffer = tmp_buffer;
- ctxt->node_seq.maximum *= 2;
- }
-
- if (pos != ctxt->node_seq.length) {
- unsigned long i;
- for (i = ctxt->node_seq.length; i > pos; i--)
- ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
- }
-
- ctxt->node_seq.buffer[pos] = *info;
- ctxt->node_seq.length++;
- }
- }
- int
- xmlPedanticParserDefault(int val) {
- int old = xmlPedanticParserDefaultValue;
- xmlPedanticParserDefaultValue = val;
- return(old);
- }
- int
- xmlLineNumbersDefault(int val) {
- int old = xmlLineNumbersDefaultValue;
- xmlLineNumbersDefaultValue = val;
- return(old);
- }
- int
- xmlSubstituteEntitiesDefault(int val) {
- int old = xmlSubstituteEntitiesDefaultValue;
- xmlSubstituteEntitiesDefaultValue = val;
- return(old);
- }
- int
- xmlKeepBlanksDefault(int val) {
- int old = xmlKeepBlanksDefaultValue;
- xmlKeepBlanksDefaultValue = val;
- if (!val) xmlIndentTreeOutput = 1;
- return(old);
- }
- #define bottom_parserInternals
- #include "elfgcchack.h"
|