7
7
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
8
8
* Portions Copyright (c) 1994, Regents of the University of California
9
9
*
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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -112,12 +112,12 @@ xml_out(PG_FUNCTION_ARGS)
112
112
xmltype * s = PG_GETARG_XML_P (0 );
113
113
char * result ;
114
114
int32 len ;
115
-
115
+
116
116
len = VARSIZE (s ) - VARHDRSZ ;
117
117
result = palloc (len + 1 );
118
118
memcpy (result , VARDATA (s ), len );
119
119
result [len ] = '\0' ;
120
-
120
+
121
121
PG_RETURN_CSTRING (result );
122
122
}
123
123
@@ -344,7 +344,7 @@ xmlvalidate(PG_FUNCTION_ARGS)
344
344
ctxt = xmlNewParserCtxt ();
345
345
if (ctxt == NULL )
346
346
xml_ereport (ERROR , "could not allocate parser context" , ctxt );
347
- doc = xmlCtxtReadMemory (ctxt , (char * ) VARDATA (data ),
347
+ doc = xmlCtxtReadMemory (ctxt , (char * ) VARDATA (data ),
348
348
VARSIZE (data ) - VARHDRSZ , PG_XML_DEFAULT_URI , NULL , 0 );
349
349
if (doc == NULL )
350
350
xml_ereport (ERROR , "could not parse XML data" , ctxt );
@@ -371,15 +371,15 @@ xmlvalidate(PG_FUNCTION_ARGS)
371
371
372
372
if (xmlValidateDtd (xmlNewValidCtxt (), doc , dtd ) == 1 )
373
373
result = TRUE;
374
-
374
+
375
375
#if 0
376
376
xmlFreeURI (uri );
377
377
xmlFreeDtd (dtd );
378
378
xmlFreeDoc (doc );
379
379
xmlFreeParserCtxt (ctxt );
380
380
xmlCleanupParser ();
381
381
#endif
382
-
382
+
383
383
if (!result )
384
384
xml_ereport (NOTICE , "validation against DTD failed" , ctxt );
385
385
@@ -405,15 +405,15 @@ xml_init(void)
405
405
* if we can work.
406
406
*/
407
407
if (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
+
413
413
xmlMemSetup (xml_pfree , xml_palloc , xml_repalloc , xml_pstrdup );
414
414
xmlInitParser ();
415
415
LIBXML_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*/
417
417
xmlSetGenericErrorFunc (NULL , xml_errorHandler );
418
418
xml_errmsg = NULL ;
419
419
xml_errbuf = palloc (XML_ERRBUF_SIZE );
@@ -446,20 +446,21 @@ xml_parse(text *data, int opts, bool is_document)
446
446
#endif
447
447
448
448
xml_init ();
449
-
449
+
450
450
len = VARSIZE (data ) - VARHDRSZ ; /* will be useful later */
451
451
string = xml_text2xmlChar (data );
452
-
452
+
453
453
ctxt = xmlNewParserCtxt ();
454
454
if (ctxt == NULL )
455
455
xml_ereport (ERROR , "could not allocate parser context" , ctxt );
456
-
457
- /* first, we try to parse the string as it is XML doc, then, as XML chunk */
456
+
457
+ /* first, we try to parse the string as XML doc, then, as XML chunk */
458
458
ereport (DEBUG3 , (errmsg ("string to parse: %s" , string )));
459
459
if (len > 4 && CMP5 (string , '<' , '?' , 'x' , 'm' , 'l' ))
460
460
{
461
461
/* 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 );
463
464
if (doc == NULL )
464
465
{
465
466
xml_ereport (ERROR , "could not parse XML data" , ctxt );
@@ -509,38 +510,38 @@ xml_parse(text *data, int opts, bool is_document)
509
510
validationFailed = TRUE;
510
511
}
511
512
}
512
-
513
+
513
514
if (validationFailed )
514
515
xml_ereport (WARNING , "validation against DTD failed" , ctxt );
515
-
516
- /* TODO encoding issues
516
+
517
+ /* TODO encoding issues
517
518
* (thoughts:
518
519
* CASE:
519
520
* - XML data has explicit encoding attribute in its prolog
520
521
* - if not, assume that enc. of XML data is the same as client's one
521
- *
522
+ *
522
523
* The common rule is to accept the XML data only if its encoding
523
524
* is the same as encoding of the storage (server's). The other possible
524
525
* option is to accept all the docs, but DO TRANSFORMATION and, if needed,
525
526
* 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),
528
529
* it's much simplier (less errors...)
529
530
* ) */
530
531
/* ... */
531
-
532
+
532
533
xmlFreeParserCtxt (ctxt );
533
534
xmlCleanupParser ();
534
-
535
- ereport (DEBUG3 , (errmsg ("XML data successfully parsed, encoding: %s" ,
535
+
536
+ ereport (DEBUG3 , (errmsg ("XML data successfully parsed, encoding: %s" ,
536
537
(char * ) doc -> encoding )));
537
-
538
+
538
539
return doc ;
539
540
}
540
541
541
542
542
- /*
543
- * xmlChar<->text convertions
543
+ /*
544
+ * xmlChar<->text convertions
544
545
*/
545
546
static xmlChar *
546
547
xml_text2xmlChar (text * in )
@@ -551,13 +552,13 @@ xml_text2xmlChar(text *in)
551
552
res = palloc (len + 1 );
552
553
memcpy (res , VARDATA (in ), len );
553
554
res [len ] = '\0' ;
554
-
555
+
555
556
return (res );
556
557
}
557
558
558
559
559
- /*
560
- * Wrappers for memory management functions
560
+ /*
561
+ * Wrappers for memory management functions
561
562
*/
562
563
static void *
563
564
xml_palloc (size_t size )
@@ -588,7 +589,7 @@ xml_pstrdup(const char *string)
588
589
589
590
590
591
/*
591
- * Wrapper for "ereport" function.
592
+ * Wrapper for "ereport" function.
592
593
* Adds detail - libxml's native error message, if any.
593
594
*/
594
595
static void
@@ -597,16 +598,16 @@ xml_ereport(int level, char *msg, void *ctxt)
597
598
char * xmlErrDetail ;
598
599
int xmlErrLen , i ;
599
600
xmlErrorPtr libxmlErr = NULL ;
600
-
601
+
601
602
if (xml_errmsg != NULL )
602
603
{
603
604
ereport (DEBUG1 , (errmsg ("%s" , xml_errmsg )));
604
605
pfree (xml_errmsg );
605
606
}
606
-
607
+
607
608
if (ctxt != NULL )
608
609
libxmlErr = xmlCtxtGetLastError (ctxt );
609
-
610
+
610
611
if (libxmlErr == NULL )
611
612
{
612
613
if (level == ERROR )
@@ -645,7 +646,7 @@ static void
645
646
xml_errorHandler (void * ctxt , const char * msg ,...)
646
647
{
647
648
va_list args ;
648
-
649
+
649
650
va_start (args , msg );
650
651
vsnprintf (xml_errbuf , XML_ERRBUF_SIZE , msg , args );
651
652
va_end (args );
@@ -841,13 +842,13 @@ xml_ereport_by_code(int level, char *msg, int code)
841
842
det = "Unregistered error (libxml error code: %d)" ;
842
843
ereport (DEBUG1 , (errmsg ("Check out \"libxml/xmlerror.h\" and bring errcode \"%d\" processing to \"xml.c\"." , code )));
843
844
}
844
-
845
+
845
846
if (xml_errmsg != NULL )
846
847
{
847
848
ereport (DEBUG1 , (errmsg ("%s" , xml_errmsg )));
848
849
pfree (xml_errmsg );
849
850
}
850
-
851
+
851
852
ereport (level , (errmsg (msg ), errdetail (det , code )));
852
853
}
853
854
@@ -857,11 +858,16 @@ xml_ereport_by_code(int level, char *msg, int code)
857
858
* codepoint.
858
859
*/
859
860
static pg_wchar
860
- sqlchar_to_unicode (unsigned char * s )
861
+ sqlchar_to_unicode (char * s )
861
862
{
862
863
int save_enc ;
863
864
pg_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 ((unsigned char * ) s ,
868
+ pg_mblen (s ),
869
+ GetDatabaseEncoding (),
870
+ PG_UTF8 );
865
871
866
872
save_enc = GetDatabaseEncoding ();
867
873
SetDatabaseEncoding (PG_UTF8 );
@@ -898,11 +904,11 @@ is_valid_xml_namechar(pg_wchar c)
898
904
* Map SQL identifier to XML name; see SQL/XML:2003 section 9.1.
899
905
*/
900
906
char *
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 )
902
908
{
903
909
#ifdef USE_LIBXML
904
910
StringInfoData buf ;
905
- unsigned char * p ;
911
+ char * p ;
906
912
907
913
initStringInfo (& buf );
908
914
0 commit comments