77 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88 * Portions Copyright (c) 1994, Regents of the University of California
99 *
10- * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.1 2006/12/21 16:05:15 petere Exp $
10+ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.2 2006/12/23 04:56:50 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -112,12 +112,12 @@ xml_out(PG_FUNCTION_ARGS)
112112xmltype * s = PG_GETARG_XML_P (0 );
113113char * result ;
114114int32 len ;
115-
115+
116116len = VARSIZE (s )- VARHDRSZ ;
117117result = palloc (len + 1 );
118118memcpy (result ,VARDATA (s ),len );
119119result [len ]= '\0' ;
120-
120+
121121PG_RETURN_CSTRING (result );
122122}
123123
@@ -344,7 +344,7 @@ xmlvalidate(PG_FUNCTION_ARGS)
344344ctxt = xmlNewParserCtxt ();
345345if (ctxt == NULL )
346346xml_ereport (ERROR ,"could not allocate parser context" ,ctxt );
347- doc = xmlCtxtReadMemory (ctxt , (char * )VARDATA (data ),
347+ doc = xmlCtxtReadMemory (ctxt , (char * )VARDATA (data ),
348348VARSIZE (data )- VARHDRSZ ,PG_XML_DEFAULT_URI ,NULL ,0 );
349349if (doc == NULL )
350350xml_ereport (ERROR ,"could not parse XML data" ,ctxt );
@@ -371,15 +371,15 @@ xmlvalidate(PG_FUNCTION_ARGS)
371371
372372if (xmlValidateDtd (xmlNewValidCtxt (),doc ,dtd )== 1 )
373373result = TRUE;
374-
374+
375375#if 0
376376xmlFreeURI (uri );
377377xmlFreeDtd (dtd );
378378xmlFreeDoc (doc );
379379xmlFreeParserCtxt (ctxt );
380380xmlCleanupParser ();
381381#endif
382-
382+
383383if (!result )
384384xml_ereport (NOTICE ,"validation against DTD failed" ,ctxt );
385385
@@ -405,15 +405,15 @@ xml_init(void)
405405 * if we can work.
406406 */
407407if (sizeof (char )!= sizeof (xmlChar ))
408- ereport (ERROR ,
409- (errmsg ("cannot initialize XML library" ),
410- errdetail ("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u." ,
411- sizeof (char ),sizeof (xmlChar ))));
412-
408+ ereport (ERROR ,
409+ (errmsg ("could not initialize XML library" ),
410+ errdetail ("libxml2 has incompatible char type: sizeof(char)=%u, sizeof(xmlChar)=%u." ,
411+ ( int ) sizeof (char ), ( int ) sizeof (xmlChar ))));
412+
413413xmlMemSetup (xml_pfree ,xml_palloc ,xml_repalloc ,xml_pstrdup );
414414xmlInitParser ();
415415LIBXML_TEST_VERSION ;
416- /* do not flood PG's logfile with libxml error messages - reset error handler*/
416+ /* do not flood PG's logfile with libxml error messages - reset error handler*/
417417xmlSetGenericErrorFunc (NULL ,xml_errorHandler );
418418xml_errmsg = NULL ;
419419xml_errbuf = palloc (XML_ERRBUF_SIZE );
@@ -446,20 +446,21 @@ xml_parse(text *data, int opts, bool is_document)
446446#endif
447447
448448xml_init ();
449-
449+
450450len = VARSIZE (data )- VARHDRSZ ;/* will be useful later */
451451string = xml_text2xmlChar (data );
452-
452+
453453ctxt = xmlNewParserCtxt ();
454454if (ctxt == NULL )
455455xml_ereport (ERROR ,"could not allocate parser context" ,ctxt );
456-
457- /* first, we try to parse the string asit is XML doc, then, as XML chunk */
456+
457+ /* first, we try to parse the string as XML doc, then, as XML chunk */
458458ereport (DEBUG3 , (errmsg ("string to parse: %s" ,string )));
459459if (len > 4 && CMP5 (string ,'<' ,'?' ,'x' ,'m' ,'l' ))
460460{
461461/* consider it as DOCUMENT */
462- doc = xmlCtxtReadMemory (ctxt ,string ,len ,PG_XML_DEFAULT_URI ,NULL ,opts );
462+ doc = xmlCtxtReadMemory (ctxt , (char * )string ,len ,
463+ PG_XML_DEFAULT_URI ,NULL ,opts );
463464if (doc == NULL )
464465{
465466xml_ereport (ERROR ,"could not parse XML data" ,ctxt );
@@ -509,38 +510,38 @@ xml_parse(text *data, int opts, bool is_document)
509510validationFailed = TRUE;
510511}
511512}
512-
513+
513514if (validationFailed )
514515xml_ereport (WARNING ,"validation against DTD failed" ,ctxt );
515-
516- /* TODO encoding issues
516+
517+ /* TODO encoding issues
517518 * (thoughts:
518519 * CASE:
519520 * - XML data has explicit encoding attribute in its prolog
520521 * - if not, assume that enc. of XML data is the same as client's one
521- *
522+ *
522523 * The common rule is to accept the XML data only if its encoding
523524 * is the same as encoding of the storage (server's). The other possible
524525 * option is to accept all the docs, but DO TRANSFORMATION and, if needed,
525526 * change the prolog.
526- *
527- * I think I'd stick the first way (for the 1st version),
527+ *
528+ * I think I'd stick the first way (for the 1st version),
528529 * it's much simplier (less errors...)
529530 * ) */
530531/* ... */
531-
532+
532533xmlFreeParserCtxt (ctxt );
533534xmlCleanupParser ();
534-
535- ereport (DEBUG3 , (errmsg ("XML data successfully parsed, encoding: %s" ,
535+
536+ ereport (DEBUG3 , (errmsg ("XML data successfully parsed, encoding: %s" ,
536537(char * )doc -> encoding )));
537-
538+
538539return doc ;
539540}
540541
541542
542- /*
543- * xmlChar<->text convertions
543+ /*
544+ * xmlChar<->text convertions
544545 */
545546static xmlChar *
546547xml_text2xmlChar (text * in )
@@ -551,13 +552,13 @@ xml_text2xmlChar(text *in)
551552res = palloc (len + 1 );
552553memcpy (res ,VARDATA (in ),len );
553554res [len ]= '\0' ;
554-
555+
555556return (res );
556557}
557558
558559
559- /*
560- * Wrappers for memory management functions
560+ /*
561+ * Wrappers for memory management functions
561562 */
562563static void *
563564xml_palloc (size_t size )
@@ -588,7 +589,7 @@ xml_pstrdup(const char *string)
588589
589590
590591/*
591- * Wrapper for "ereport" function.
592+ * Wrapper for "ereport" function.
592593 * Adds detail - libxml's native error message, if any.
593594 */
594595static void
@@ -597,16 +598,16 @@ xml_ereport(int level, char *msg, void *ctxt)
597598char * xmlErrDetail ;
598599int xmlErrLen ,i ;
599600xmlErrorPtr libxmlErr = NULL ;
600-
601+
601602if (xml_errmsg != NULL )
602603{
603604ereport (DEBUG1 , (errmsg ("%s" ,xml_errmsg )));
604605pfree (xml_errmsg );
605606}
606-
607+
607608if (ctxt != NULL )
608609libxmlErr = xmlCtxtGetLastError (ctxt );
609-
610+
610611if (libxmlErr == NULL )
611612{
612613if (level == ERROR )
@@ -645,7 +646,7 @@ static void
645646xml_errorHandler (void * ctxt ,const char * msg ,...)
646647{
647648va_list args ;
648-
649+
649650va_start (args ,msg );
650651vsnprintf (xml_errbuf ,XML_ERRBUF_SIZE ,msg ,args );
651652va_end (args );
@@ -841,13 +842,13 @@ xml_ereport_by_code(int level, char *msg, int code)
841842det = "Unregistered error (libxml error code: %d)" ;
842843ereport (DEBUG1 , (errmsg ("Check out \"libxml/xmlerror.h\" and bring errcode \"%d\" processing to \"xml.c\"." ,code )));
843844 }
844-
845+
845846if (xml_errmsg != NULL )
846847{
847848ereport (DEBUG1 , (errmsg ("%s" ,xml_errmsg )));
848849pfree (xml_errmsg );
849850}
850-
851+
851852ereport (level , (errmsg (msg ),errdetail (det ,code )));
852853}
853854
@@ -857,11 +858,16 @@ xml_ereport_by_code(int level, char *msg, int code)
857858 * codepoint.
858859 */
859860static pg_wchar
860- sqlchar_to_unicode (unsigned char * s )
861+ sqlchar_to_unicode (char * s )
861862{
862863int save_enc ;
863864pg_wchar ret ;
864- char * utf8string = pg_do_encoding_conversion (s ,pg_mblen (s ),GetDatabaseEncoding (),PG_UTF8 );
865+ char * utf8string ;
866+
867+ utf8string = (char * )pg_do_encoding_conversion ((unsignedchar * )s ,
868+ pg_mblen (s ),
869+ GetDatabaseEncoding (),
870+ PG_UTF8 );
865871
866872save_enc = GetDatabaseEncoding ();
867873SetDatabaseEncoding (PG_UTF8 );
@@ -898,11 +904,11 @@ is_valid_xml_namechar(pg_wchar c)
898904 * Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
899905 */
900906char *
901- map_sql_identifier_to_xml_name (unsigned char * ident ,bool fully_escaped )
907+ map_sql_identifier_to_xml_name (char * ident ,bool fully_escaped )
902908{
903909#ifdef USE_LIBXML
904910StringInfoData buf ;
905- unsigned char * p ;
911+ char * p ;
906912
907913initStringInfo (& buf );
908914