static void testCharRanges(void) { char data[5]; xmlParserCtxtPtr ctxt; xmlParserInputBufferPtr buf; xmlParserInputPtr input; memset(data, 0, 5); /* * Set up a parsing context using the above data buffer as * the current input source. */ ctxt = xmlNewParserCtxt(); if (ctxt == NULL) { fprintf(stderr, "Failed to allocate parser context\n"); return; } buf = xmlParserInputBufferCreateStatic(data, sizeof(data), XML_CHAR_ENCODING_NONE); if (buf == NULL) { fprintf(stderr, "Failed to allocate input buffer\n"); goto error; } input = xmlNewInputStream(ctxt); if (input == NULL) { xmlFreeParserInputBuffer(buf); goto error; } input->filename = NULL; input->buf = buf; input->cur = input->base = xmlBufContent(input->buf->buffer); input->end = input->base + 4; inputPush(ctxt, input); printf("testing char range: 1"); fflush(stdout); testCharRangeByte1(ctxt, data); printf(" 2"); fflush(stdout); testCharRangeByte2(ctxt, data); printf(" 3"); fflush(stdout); testCharRangeByte3(ctxt, data); printf(" 4"); fflush(stdout); testCharRangeByte4(ctxt, data); printf(" done\n"); fflush(stdout); error: xmlFreeParserCtxt(ctxt); }
int xsltSaveResultToString(xmlChar **doc_txt_ptr, int * doc_txt_len, xmlDocPtr result, xsltStylesheetPtr style) { xmlOutputBufferPtr buf; *doc_txt_ptr = NULL; *doc_txt_len = 0; if (result->children == NULL) return(0); buf = xmlAllocOutputBuffer(NULL); if (buf == NULL) return(-1); xsltSaveResultTo(buf, result, style); if (buf->conv != NULL) { *doc_txt_len = xmlBufUse (buf->conv); *doc_txt_ptr = xmlStrndup (xmlBufContent (buf->conv), *doc_txt_len); } else { *doc_txt_len = xmlBufUse (buf->buffer); *doc_txt_ptr = xmlStrndup (xmlBufContent (buf->buffer), *doc_txt_len); } (void)xmlOutputBufferClose(buf); return 0; }
String::C xdoc2buf(Request& r, VXdoc& vdoc, XDocOutputOptions& oo, const String* file_spec, bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) { Charset* render=0; Charset* header=0; if(use_source_charset_to_render_and_client_charset_to_write_to_header) { render=&r.charsets.source(); header=&r.charsets.client(); } else { header=render=&charsets.get(oo.encoding->change_case(r.charsets.source(), String::CC_UPPER)); } const char* render_encoding=render->NAME_CSTR(); const char* header_encoding=header->NAME_CSTR(); xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding); // UTF-8 renderer contains empty input/output converters, // which is wrong for xmlOutputBufferCreateIO // while zero renderer goes perfectly if(render->isUTF8()) renderer=0; xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet()); if(!stylesheet.get()) throw Exception(0, 0, "xsltNewStylesheet failed"); #define OOSTRING2STYLE(name) \ stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0 #define OOBOOL2STYLE(name) \ if(oo.name>=0) stylesheet->name=oo.name OOSTRING2STYLE(method); OOSTRING2STYLE(encoding); OOSTRING2STYLE(mediaType); // OOSTRING2STYLE(doctypeSystem); // OOSTRING2STYLE(doctypePublic); OOBOOL2STYLE(indent); OOSTRING2STYLE(version); OOBOOL2STYLE(standalone); OOBOOL2STYLE(omitXmlDeclaration); xmlDoc& xmldoc=vdoc.get_xmldoc(); xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding); if(header_encoding) stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding); if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0 || xmlHaveGenericErrors()) throw XmlException(0, r); // write out result char *gnome_str; size_t gnome_length; #ifdef LIBXML2_NEW_BUFFER if(outputBuffer->conv) { gnome_length=xmlBufUse(outputBuffer->conv); gnome_str=(char *)xmlBufContent(outputBuffer->conv); } else { gnome_length=xmlOutputBufferGetSize(&(*outputBuffer)); gnome_str=(char *)xmlOutputBufferGetContent(&(*outputBuffer)); } #else if(outputBuffer->conv) { gnome_length=outputBuffer->conv->use; gnome_str=(char *)outputBuffer->conv->content; } else { gnome_length=outputBuffer->buffer->use; gnome_str=(char *)outputBuffer->buffer->content; } #endif if(file_spec){ file_write(r.charsets, *file_spec, gnome_str, gnome_length, true/*as_text*/); return String::C(); // actually, we don't need this output at all } else return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length); }
/** * htmlDocDumpMemoryFormat: * @cur: the document * @mem: OUT: the memory pointer * @size: OUT: the memory length * @format: should formatting spaces been added * * Dump an HTML document in memory and return the xmlChar * and it's size. * It's up to the caller to free the memory. */ void htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) { xmlOutputBufferPtr buf; xmlCharEncodingHandlerPtr handler = NULL; const char *encoding; xmlInitParser(); if ((mem == NULL) || (size == NULL)) return; if (cur == NULL) { *mem = NULL; *size = 0; return; } encoding = (const char *) htmlGetMetaEncoding(cur); if (encoding != NULL) { xmlCharEncoding enc; enc = xmlParseCharEncoding(encoding); if (enc != cur->charset) { if (cur->charset != XML_CHAR_ENCODING_UTF8) { /* * Not supported yet */ *mem = NULL; *size = 0; return; } handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } else { handler = xmlFindCharEncodingHandler(encoding); } } /* * Fallback to HTML or ASCII when the encoding is unspecified */ if (handler == NULL) handler = xmlFindCharEncodingHandler("HTML"); if (handler == NULL) handler = xmlFindCharEncodingHandler("ascii"); buf = xmlAllocOutputBufferInternal(handler); if (buf == NULL) { *mem = NULL; *size = 0; return; } htmlDocContentDumpFormatOutput(buf, cur, NULL, format); xmlOutputBufferFlush(buf); if (buf->conv != NULL) { *size = xmlBufUse(buf->conv); *mem = xmlStrndup(xmlBufContent(buf->conv), *size); } else { *size = xmlBufUse(buf->buffer); *mem = xmlStrndup(xmlBufContent(buf->buffer), *size); } (void)xmlOutputBufferClose(buf); }
/* Taken from libxml2, xmlSAXParseMemoryWithData */ xmlDocPtr mxslt_doc_xml_parse(mxslt_doc_t * document, xmlParserInputBufferPtr buf, char * localfile) { xmlParserCtxtPtr ctx; xmlParserInputPtr input; xmlDocPtr retval; mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE0, "xml_parse/xmlCreateMemoryParserCtxt -- replacing entities: %08x\n", xmlSubstituteEntitiesDefaultValue); if(buf == NULL) return NULL; /* SNIPPET: This is a good mix&shake of * xmlCreateMemoryParserCtxt, xmlCreateFileParserCtxt */ ctx=xmlNewParserCtxt(); if(ctx == NULL) { xmlFreeParserInputBuffer(buf); return NULL; } #if LIBXML_VERSION >= 20600 xmlCtxtUseOptions(ctx, MXSLT_XSLT_OPTIONS); #endif /* Default are no longer changed... since 2.6.0 they * are completely ignored, leading to broken modxslt :|| */ if(ctx->sax) { ctx->sax->resolveEntity=mxslt_sax_resolve_entity; ctx->sax->processingInstruction=mxslt_sax_processing_instruction; } /* Remember which document we are parsing * in this context */ /* ctx->_private=document; */ input=xmlNewInputStream(ctx); if(input == NULL) { xmlFreeParserInputBuffer(buf); xmlFreeParserCtxt(ctx); return NULL; } input->filename=(char *)xmlCanonicPath((xmlChar *)localfile); if(input->filename == NULL) { xmlFreeParserCtxt(ctx); xmlFreeParserInputBuffer(buf); xmlFreeInputStream(input); return NULL; } input->buf=buf; #if LIBXML_VERSION < 20900 input->base=input->buf->buffer->content; input->cur=input->buf->buffer->content; input->end=&input->buf->buffer->content[input->buf->buffer->use]; #else /* With libxml2 2.9.0, the buffer struct can only be accessed through * methods. */ input->base=xmlBufContent(input->buf->buffer); input->cur=xmlBufContent(input->buf->buffer); input->end=xmlBufEnd(input->buf->buffer); #endif inputPush(ctx, input); if(ctx->directory == NULL) ctx->directory=xmlParserGetDirectory(localfile); /* END SNIPPET */ if(mxslt_doc_debug_level(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE2)) mxslt_doc_dump_ctx(ctx, document); /* Parse document */ xmlParseDocument(ctx); if(ctx->wellFormed) retval=ctx->myDoc; else { retval=NULL; xmlFreeDoc(ctx->myDoc); ctx->myDoc=NULL; } xmlFreeParserCtxt(ctx); /* xmlFreeParserInputBuffer(buf); */ return retval; }