📄 parserinternals.c.svn-base
字号:
/* * We used to keep the internal content in the * document encoding however this turns being unmaintainable * So xmlGetCharEncodingHandler() will return non-null * values for this now. */ if ((ctxt->inputNr == 1) && (ctxt->encoding == 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; } } if (handler == NULL) return(-1); ctxt->charset = XML_CHAR_ENCODING_UTF8; return(xmlSwitchToEncoding(ctxt, handler));}/** * xmlSwitchInputEncoding: * @ctxt: the parser context * @input: the input stream * @handler: the encoding handler * * change the input functions when discovering the character encoding * of a given entity. * * Returns 0 in case of success, -1 otherwise */intxmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input, xmlCharEncodingHandlerPtr handler){ int nbchars; if (handler == NULL) return (-1); if (input == NULL) return (-1); if (input->buf != NULL) { if (input->buf->encoder != NULL) { /* * Check in case the auto encoding detetection triggered * in already. */ if (input->buf->encoder == handler) return (0); /* * "UTF-16" can be used for both LE and BE if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name, BAD_CAST "UTF-16", 6)) && (!xmlStrncmp(BAD_CAST handler->name, BAD_CAST "UTF-16", 6))) { return(0); } */ /* * Note: this is a bit dangerous, but that's what it * takes to use nearly compatible signature for different * encodings. */ xmlCharEncCloseFunc(input->buf->encoder); input->buf->encoder = handler; return (0); } input->buf->encoder = handler; /* * Is there already some content down the pipe to convert ? */ if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) { int processed; unsigned int use; /* * Specific handling of the Byte Order Mark for * UTF-16 */ if ((handler->name != NULL) && (!strcmp(handler->name, "UTF-16LE")) && (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; } /* * Errata on XML-1.0 June 20 2001 * Specific handling of the Byte Order Mark for * UTF-8 */ if ((handler->name != NULL) && (!strcmp(handler->name, "UTF-8")) && (input->cur[0] == 0xEF) && (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) { input->cur += 3; } /* * Shrink the current input buffer. * Move it as the raw buffer and create a new input buffer */ 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) { /* * convert as much as possible of the buffer */ nbchars = xmlCharEncInFunc(input->buf->encoder, input->buf->buffer, input->buf->raw); } else { /* * convert just enough to get * '<?xml version="1.0" encoding="xxx"?>' * parsed with the autodetected encoding * into the parser reading buffer. */ nbchars = xmlCharEncFirstLine(input->buf->encoder, input->buf->buffer, input->buf->raw); } 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) || (input->buf == NULL)) { /* * When parsing a static memory array one must know the * size to be able to convert the buffer. */ xmlErrInternal(ctxt, "switching encoding : no input\n", NULL); return (-1); } else { int processed; /* * Shrink the current input buffer. * Move it as the raw buffer and create a new input buffer */ processed = input->cur - input->base; input->buf->raw = xmlBufferCreate(); xmlBufferAdd(input->buf->raw, input->cur, input->length - processed); input->buf->buffer = xmlBufferCreate(); /* * convert as much as possible of the raw input * to the parser reading buffer. */ nbchars = xmlCharEncInFunc(input->buf->encoder, input->buf->buffer, input->buf->raw); if (nbchars < 0) { xmlErrInternal(ctxt, "switching encoding: encoder error\n", NULL); return (-1); } /* * Conversion succeeded, get rid of the old buffer */ if ((input->free != NULL) && (input->base != NULL)) input->free((xmlChar *) input->base); input->base = input->cur = input->buf->buffer->content; input->end = &input->base[input->buf->buffer->use]; } } return (0);}/** * xmlSwitchToEncoding: * @ctxt: the parser context * @handler: the encoding handler * * change the input functions when discovering the character encoding * of a given entity. * * Returns 0 in case of success, -1 otherwise */intxmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler) { if (handler != NULL) { if (ctxt->input != NULL) { xmlSwitchInputEncoding(ctxt, ctxt->input, handler); } else { xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n", NULL); return(-1); } /* * The parsing is now done in UTF8 natively */ ctxt->charset = XML_CHAR_ENCODING_UTF8; } else return(-1); return(0);}/************************************************************************ * * * Commodity functions to handle entities processing * * * ************************************************************************//** * xmlFreeInputStream: * @input: an xmlParserInputPtr * * Free up an input stream. */voidxmlFreeInputStream(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);}/** * xmlNewInputStream: * @ctxt: an XML parser context * * Create a new input stream structure * Returns the new input stream or NULL */xmlParserInputPtrxmlNewInputStream(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; /* * we don't care about thread reentrancy unicity for a single * parser context (and hence thread) is sufficient. */ input->id = id++; return(input);}/** * xmlNewIOInputStream: * @ctxt: an XML parser context * @input: an I/O Input * @enc: the charset encoding if known * * Create a new input stream structure encapsulating the @input into * a stream suitable for the parser. * * Returns the new input stream or NULL */xmlParserInputPtrxmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input, xmlCharEncoding enc) { xmlParserInputPtr inputStream; 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);}/** * xmlNewEntityInputStream: * @ctxt: an XML parser context * @entity: an Entity pointer * * Create a new input stream based on an xmlEntityPtr * * Returns the new input stream or NULL */xmlParserInputPtrxmlNewEntityInputStream(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); } input->filename = (char *) entity->URI; input->base = entity->content; input->cur = entity->content; input->length = entity->length; input->end = &entity->content[input->length]; return(input);}/** * xmlNewStringInputStream: * @ctxt: an XML parser context * @buffer: an memory buffer * * Create a new input stream based on a memory buffer. * Returns the new input stream */xmlParserInputPtrxmlNewStringInputStream(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);}/** * xmlNewInputFromFile: * @ctxt: an XML parser context * @filename: the filename to use as entity * * Create a new input stream based on a file or an URL. * * Returns the new input stream or NULL in case of error */xmlParserInputPtrxmlNewInputFromFile(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) { __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", (const char *) filename); return(NULL); } inputStream = xmlNewInputStream(ctxt); if (inputStream == NULL) { if (directory != NULL) xmlFree((char *) directory); if (URI != NULL) xmlFree((char *) URI); 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); 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);}/************************************************************************ * * * Commodity functions to handle parser contexts * * * ************************************************************************//** * xmlInitParserCtxt: * @ctxt: an XML parser context * * Initialize a parser context * * Returns 0 in case of success and -1 in case of error */intxmlInitParserCtxt(xmlParserCtxtPtr ctxt){ if(ctxt==NULL) { xmlErrInternal(NULL, "Got NULL parser context\n", NULL); return(-1); } xmlDefaultSAXHandlerInit(); ctxt->dict = xmlDictCreate(); if (ctxt->dict == NULL) { xmlErrMemory(NULL, "cannot initialize parser context\n"); return(-1); } ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); if (ctxt->sax == NULL) { xmlErrMemory(NULL, "cannot initialize parser context\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -