[xmlsec] fixed XML dump output format



commit f2464d997a45ed0b25e2a8917d0a65c9625aa81a
Author: Aleksey Sanin <aleksey aleksey com>
Date:   Mon Aug 24 15:43:45 2009 -0700

    fixed XML dump output format

 ChangeLog                |    3 ++
 docs/index.html          |    1 +
 docs/news.html           |    1 +
 include/xmlsec/xmltree.h |    6 ++++
 src/keys.c               |   37 ++++++++++++++----------
 src/keysdata.c           |    4 ++-
 src/mscrypto/x509.c      |   25 ++++++++++++----
 src/nss/x509.c           |   16 +++++++---
 src/openssl/x509.c       |   23 +++++++++------
 src/transforms.c         |   23 ++++++++++-----
 src/xkms.c               |   69 ++++++++++++++++++++++++++++------------------
 src/xmldsig.c            |   26 +++++++++--------
 src/xmlenc.c             |   41 +++++++++++++++------------
 src/xmltree.c            |   46 ++++++++++++++++++++++++++++++
 14 files changed, 219 insertions(+), 102 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 497ec97..4012d7b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+2009-08-24 Aleksey Sanin  <aleksey aleksey com>
+	* Fix XML dump format
+
 2009-08-06 Aleksey Sanin  <aleksey aleksey com>
 	* fix build for openssl 1.0 (based on patch from Roumen Petrov)
 	* cleanup test invocation scripts 
diff --git a/docs/index.html b/docs/index.html
index 343bbb6..1450eb6 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -81,6 +81,7 @@
 </li>
 	<li>Increase default minimum hmac size to 80 bits</li>
 	<li>Added support for --with-libxml-src and --with-libxslt-src ./configure options</li>
+	<li>Fixed XML dump output</li>
 	</ul>
 </li>
 
diff --git a/docs/news.html b/docs/news.html
index 9959477..3b9179c 100644
--- a/docs/news.html
+++ b/docs/news.html
@@ -56,6 +56,7 @@
 </li>
 	<li>Increase default minimum hmac size to 80 bits</li>
 	<li>Added support for --with-libxml-src and --with-libxslt-src ./configure options</li>
+	<li>Fixed XML dump output</li>
 	</ul>
 </li>
 
diff --git a/include/xmlsec/xmltree.h b/include/xmlsec/xmltree.h
index 528a788..ffba25f 100644
--- a/include/xmlsec/xmltree.h
+++ b/include/xmlsec/xmltree.h
@@ -15,6 +15,8 @@
 extern "C" {
 #endif /* __cplusplus */ 
 
+#include <stdio.h>
+
 #include <libxml/tree.h>
 #include <libxml/xpath.h>
 #include <xmlsec/xmlsec.h>
@@ -95,6 +97,10 @@ XMLSEC_EXPORT xmlChar*		xmlSecGetQName		(xmlNodePtr node,
 							 const xmlChar* href,
 							 const xmlChar* local);
 
+
+XMLSEC_EXPORT int               xmlSecPrintXmlString    (FILE * fd,
+                                                         const xmlChar * str);
+
 /**
  * xmlSecIsHex:
  * @c: 			the character.
diff --git a/src/keys.c b/src/keys.c
index ce21519..2e01af1 100644
--- a/src/keys.c
+++ b/src/keys.c
@@ -279,10 +279,15 @@ xmlSecKeyUseWithDebugXmlDump(xmlSecKeyUseWithPtr keyUseWith, FILE* output) {
     xmlSecAssert(output != NULL);
 
     fprintf(output, "<KeyUseWith>\n");
-    fprintf(output, "<Application>%s</Application>", 
-        (keyUseWith->application) ? keyUseWith->application : BAD_CAST "");
-    fprintf(output, "<Identifier>%s</Identifier>", 
-        (keyUseWith->identifier) ? keyUseWith->identifier : BAD_CAST "");
+
+    fprintf(output, "<Application>");
+    xmlSecPrintXmlString(output, keyUseWith->application);
+    fprintf(output, "</Application>");
+
+    fprintf(output, "<Identifier>");
+    xmlSecPrintXmlString(output, keyUseWith->identifier);
+    fprintf(output, "</Identifier>");
+    
     fprintf(output, "</KeyUseWith>\n");
 }
 
@@ -504,10 +509,11 @@ xmlSecKeyReqDebugXmlDump(xmlSecKeyReqPtr keyReq, FILE* output) {
     xmlSecAssert(output != NULL);
 
     fprintf(output, "<KeyReq>\n");
-    fprintf(output, "<KeyId>%s</KeyId>\n", 
-	    (xmlSecKeyDataKlassGetName(keyReq->keyId)) ? 
-		xmlSecKeyDataKlassGetName(keyReq->keyId) : 
-		BAD_CAST "NULL");
+
+    fprintf(output, "<KeyId>");
+    xmlSecPrintXmlString(output, xmlSecKeyDataKlassGetName(keyReq->keyId));
+    fprintf(output, "</KeyId>\n");
+
     fprintf(output, "<KeyType>0x%08x</KeyType>\n", keyReq->keyType);
     fprintf(output, "<KeyUsage>0x%08x</KeyUsage>\n", keyReq->keyUsage);
     fprintf(output, "<KeyBitsSize>%d</KeyBitsSize>\n", keyReq->keyBitsSize);
@@ -1004,10 +1010,10 @@ xmlSecKeyDebugXmlDump(xmlSecKeyPtr key, FILE *output) {
     xmlSecAssert(output != NULL);
     
     fprintf(output, "<KeyInfo>\n");
-    if(key->value->id->dataNodeName != NULL) {
-        fprintf(output, "<KeyMethod>%s</KeyMethod>\n", 
-		key->value->id->dataNodeName); 
-    }
+
+    fprintf(output, "<KeyMethod>");
+    xmlSecPrintXmlString(output, key->value->id->dataNodeName); 
+    fprintf(output, "</KeyMethod>\n");
 
     fprintf(output, "<KeyType>");
     if((xmlSecKeyGetType(key) & xmlSecKeyDataTypeSymmetric) != 0) {
@@ -1021,9 +1027,10 @@ xmlSecKeyDebugXmlDump(xmlSecKeyPtr key, FILE *output) {
     } 
     fprintf(output, "</KeyType>\n");
 
-    if(key->name != NULL) {
-	fprintf(output, "<KeyName>%s</KeyName>\n", key->name);
-    }
+    fprintf(output, "<KeyName>");
+    xmlSecPrintXmlString(output, key->name);
+    fprintf(output, "</KeyName>\n");
+
     if(key->notValidBefore < key->notValidAfter) {
         fprintf(output, "<KeyValidity notValidBefore=\"%ld\" notValidAfter=\"%ld\"/>\n",
 		(unsigned long)key->notValidBefore, 
diff --git a/src/keysdata.c b/src/keysdata.c
index e44e1d9..3459fd0 100644
--- a/src/keysdata.c
+++ b/src/keysdata.c
@@ -1281,7 +1281,9 @@ xmlSecKeyDataIdListDebugXmlDump(xmlSecPtrListPtr list, FILE* output) {
 	xmlSecAssert(dataId != NULL);
 	xmlSecAssert(dataId->name != NULL);
 	    
-	fprintf(output, "<DataId name=\"%s\" />", dataId->name);
+	fprintf(output, "<DataId name=\"");
+        xmlSecPrintXmlString(output, dataId->name);
+        fprintf(output, "\"/>");
     }
     fprintf(output, "</KeyDataIdsList>\n");
 }
diff --git a/src/mscrypto/x509.c b/src/mscrypto/x509.c
index c7dc4ba..b849836 100644
--- a/src/mscrypto/x509.c
+++ b/src/mscrypto/x509.c
@@ -2111,20 +2111,31 @@ xmlSecMSCryptoX509CertDebugXmlDump(PCCERT_CONTEXT cert, FILE* output) {
     xmlSecAssert(output != NULL);
 
     /* todo: add error checks */
+    
+    /* subject */
     dwSize = CertGetNameString(cert, CERT_NAME_RDN_TYPE, 0, NULL, NULL, 0);
     subject = (LPSTR)xmlMalloc(dwSize);
     dwSize = CertGetNameString(cert, CERT_NAME_RDN_TYPE, 0, NULL, subject, dwSize);
+
+    fprintf(output, "<SubjectName>");
+    xmlSecPrintXmlString(output, BAD_CAST subject);
+    fprintf(output, "</SubjectName>\n");
+    xmlFree(subject);
+    
+    
+    /* issuer */
     dwSize = CertGetNameString(cert, CERT_NAME_RDN_TYPE, CERT_NAME_ISSUER_FLAG, NULL, NULL, 0);
     issuer = (LPSTR)xmlMalloc(dwSize);
     dwSize = CertGetNameString(cert, CERT_NAME_RDN_TYPE, CERT_NAME_ISSUER_FLAG, NULL, issuer, dwSize);
 
-    fprintf(output, "=== X509 Certificate\n");
-    fprintf(output, "==== Subject Name: %s\n", subject);
-    fprintf(output, "==== Issuer Name: %s\n", issuer);
-    if (subject) xmlFree(subject);
-    if (issuer) xmlFree(issuer);
+    fprintf(output, "<IssuerName>");
+    xmlSecPrintXmlString(output, BAD_CAST issuer);
+    fprintf(output, "</IssuerName>\n");
+    xmlFree(issuer);
+    
+    /* serial */
+    fprintf(output, "<SerialNumber>");
     sn = &(cert->pCertInfo->SerialNumber);
-
     for (i = 0; i < sn->cbData; i++) {
 	if (i != sn->cbData - 1) {
 	    fprintf(output, "%02x:", sn->pbData[i]);
@@ -2132,7 +2143,7 @@ xmlSecMSCryptoX509CertDebugXmlDump(PCCERT_CONTEXT cert, FILE* output) {
 	    fprintf(output, "%02x", sn->pbData[i]);
 	}
     }
-    fprintf(output, "\n");
+    fprintf(output, "</SerialNumber>\n");
 }
 
 
diff --git a/src/nss/x509.c b/src/nss/x509.c
index c5aa7c1..b5ed56d 100644
--- a/src/nss/x509.c
+++ b/src/nss/x509.c
@@ -2060,10 +2060,16 @@ xmlSecNssX509CertDebugXmlDump(CERTCertificate* cert, FILE* output) {
 
     xmlSecAssert(cert != NULL);
     xmlSecAssert(output != NULL);
-    
-    fprintf(output, "=== X509 Certificate\n");
-    fprintf(output, "==== Subject Name: %s\n", cert->subjectName);
-    fprintf(output, "==== Issuer Name: %s\n", cert->issuerName);
+
+    fprintf(output, "<SubjectName>");
+    xmlSecPrintXmlString(output, BAD_CAST cert->subjectName);
+    fprintf(output, "</SubjectName>\n");
+
+    fprintf(output, "<IssuerName>");
+    xmlSecPrintXmlString(output, BAD_CAST cert->issuerName);
+    fprintf(output, "</IssuerName>\n");
+
+    fprintf(output, "<SerialNumber>");
     sn = &cert->serialNumber;
     for (i = 0; i < sn->len; i++) {
 	if (i != sn->len - 1) {
@@ -2072,7 +2078,7 @@ xmlSecNssX509CertDebugXmlDump(CERTCertificate* cert, FILE* output) {
 	    fprintf(output, "%02x", sn->data[i]);
 	}
     }
-    fprintf(output, "\n");
+    fprintf(output, "</SerialNumber>\n");
 }
 
 
diff --git a/src/openssl/x509.c b/src/openssl/x509.c
index 4bbf774..77948e4 100644
--- a/src/openssl/x509.c
+++ b/src/openssl/x509.c
@@ -2269,20 +2269,25 @@ xmlSecOpenSSLX509CertDebugXmlDump(X509* cert, FILE* output) {
     xmlSecAssert(cert != NULL);
     xmlSecAssert(output != NULL);
     
-    fprintf(output, "=== X509 Certificate\n");
-    fprintf(output, "==== Subject Name: %s\n", 
-	 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf))); 
-    fprintf(output, "==== Issuer Name: %s\n", 
-	 X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf))); 
-    fprintf(output, "==== Issuer Serial: ");
+    fprintf(output, "<SubjectName>");
+    xmlSecPrintXmlString(output, 
+	BAD_CAST X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf))
+    );
+    fprintf(output, "</SubjectName>\n");
+    
+    
+    fprintf(output, "<IssuerName>");
+    xmlSecPrintXmlString(output, 
+	BAD_CAST X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf))); 
+    fprintf(output, "</IssuerName>\n");
+
+    fprintf(output, "<SerialNumber>");
     bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(cert),NULL);
     if(bn != NULL) {
 	BN_print_fp(output, bn);
 	BN_free(bn);
-	fprintf(output, "\n");
-    } else {
-	fprintf(output, "unknown\n");
     }
+    fprintf(output, "</SerialNumber>\n");
 }
 
 
diff --git a/src/transforms.c b/src/transforms.c
index f13e234..2c31073 100644
--- a/src/transforms.c
+++ b/src/transforms.c
@@ -1369,10 +1369,13 @@ xmlSecTransformCtxDebugXmlDump(xmlSecTransformCtxPtr ctx, FILE* output) {
     }
 
 
-    fprintf(output, "<Uri>%s</Uri>\n", 
-		(ctx->uri != NULL) ? ctx->uri : BAD_CAST "NULL");
-    fprintf(output, "<UriXPointer>%s</UriXPointer>\n", 
-		(ctx->xptrExpr != NULL) ? ctx->xptrExpr : BAD_CAST "NULL");    
+    fprintf(output, "<Uri>");
+    xmlSecPrintXmlString(output, ctx->uri);
+    fprintf(output, "</Uri>\n");
+    
+    fprintf(output, "<UriXPointer>");
+    xmlSecPrintXmlString(output, ctx->xptrExpr);
+    fprintf(output, "</UriXPointer>\n");
 
     for(transform = ctx->first; transform != NULL; transform = transform->next) {
 	xmlSecTransformDebugXmlDump(transform, output);
@@ -1953,9 +1956,11 @@ xmlSecTransformDebugXmlDump(xmlSecTransformPtr transform, FILE* output) {
     xmlSecAssert(xmlSecTransformIsValid(transform));
     xmlSecAssert(output != NULL);
 
-    fprintf(output, "<Transform name=\"%s\" href=\"%s\" />\n",
-		xmlSecErrorsSafeString(transform->id->name),
-		xmlSecErrorsSafeString(transform->id->href));
+    fprintf(output, "<Transform name=\"");
+    xmlSecPrintXmlString(output,transform->id->name);
+    fprintf(output, "\" href=\"");
+    xmlSecPrintXmlString(output, transform->id->href);
+    fprintf(output, "\" />\n");
 }
 
 /************************************************************************
@@ -2631,7 +2636,9 @@ xmlSecTransformIdListDebugXmlDump(xmlSecPtrListPtr list, FILE* output) {
 	xmlSecAssert(transformId != NULL);
 	xmlSecAssert(transformId->name != NULL);
 	    
-	fprintf(output, "<TransformId name=\"%s\" />", transformId->name);
+	fprintf(output, "<TransformId name=\"");
+        xmlSecPrintXmlString(output, transformId->name);
+        fprintf(output, "\" />");
     }
     fprintf(output, "</TransformIdsList>\n");
 }
diff --git a/src/xkms.c b/src/xkms.c
index a522d13..cd7a435 100644
--- a/src/xkms.c
+++ b/src/xkms.c
@@ -1419,33 +1419,43 @@ xmlSecXkmsServerCtxDebugXmlDump(xmlSecXkmsServerCtxPtr ctx, FILE* output) {
     xmlSecAssert(ctx != NULL);
     xmlSecAssert(output != NULL);
 
-    fprintf(output, "<XkmsServerRequestContext name=\"%s\">\n",
-	    (ctx->requestId != xmlSecXkmsServerRequestIdUnknown && 
-	     xmlSecXkmsServerRequestKlassGetName(ctx->requestId)) ? 
+    fprintf(output, "<XkmsServerRequestContext name=\"");
+    xmlSecPrintXmlString(output, 
+	    (ctx->requestId != xmlSecXkmsServerRequestIdUnknown) ?
 		xmlSecXkmsServerRequestKlassGetName(ctx->requestId) :
-		BAD_CAST "NULL");
+		BAD_CAST "NULL"
+    );
+    fprintf(output, "\">\n");
 
     xmlSecQName2IntegerDebugXmlDump(gXmlSecXkmsResultMajorInfo, 
 		ctx->resultMajor, BAD_CAST "MajorError", output);    
     xmlSecQName2IntegerDebugXmlDump(gXmlSecXkmsMinorErrorInfo, 
 		ctx->resultMinor, BAD_CAST "MinorError", output);    
 
-    fprintf(output, "<Id>%s</Id>\n", 
-		(ctx->id) ? ctx->id : BAD_CAST "");
-    fprintf(output, "<Service>%s</Service>\n", 
-		(ctx->service) ? ctx->service : BAD_CAST "");
-    fprintf(output, "<Nonce>%s</Nonce>\n", 
-		(ctx->nonce) ? ctx->nonce : BAD_CAST "");
-    fprintf(output, "<OriginalRequestId>%s</OriginalRequestId>\n", 
-		(ctx->originalRequestId) ? ctx->originalRequestId : BAD_CAST "");
-    fprintf(output, "<PendingNotificationMechanism>%s</PendingNotificationMechanism>\n", 
-		(ctx->pendingNotificationMechanism) ? 
-		    ctx->pendingNotificationMechanism : 
-		    BAD_CAST "");
-    fprintf(output, "<PendingNotificationIdentifier>%s</PendingNotificationIdentifier>\n", 
-		(ctx->pendingNotificationIdentifier) ? 
-		    ctx->pendingNotificationIdentifier : 
-		    BAD_CAST "");
+    fprintf(output, "<Id>");
+    xmlSecPrintXmlString(output, ctx->id);
+    fprintf(output, "</Id>\n");
+
+    fprintf(output, "<Service>");
+    xmlSecPrintXmlString(output, ctx->service);
+    fprintf(output, "</Service>\n");
+
+    fprintf(output, "<Nonce>");
+    xmlSecPrintXmlString(output, ctx->nonce);
+    fprintf(output, "</Nonce>\n");
+
+    fprintf(output, "<OriginalRequestId>");
+    xmlSecPrintXmlString(output, ctx->originalRequestId);
+    fprintf(output, "</OriginalRequestId>\n");
+    
+    fprintf(output, "<PendingNotificationMechanism>");
+    xmlSecPrintXmlString(output, ctx->pendingNotificationMechanism);
+    fprintf(output, "</PendingNotificationMechanism>\n");
+
+    fprintf(output, "<PendingNotificationIdentifier>");
+    xmlSecPrintXmlString(output, ctx->pendingNotificationIdentifier);
+    fprintf(output, "</PendingNotificationIdentifier>\n");
+
     if(ctx->responseLimit != XMLSEC_XKMS_NO_RESPONSE_LIMIT) {
         fprintf(output, "<ResponseLimit>%d</ResponseLimit>\n", ctx->responseLimit);
     }
@@ -1453,9 +1463,10 @@ xmlSecXkmsServerCtxDebugXmlDump(xmlSecXkmsServerCtxPtr ctx, FILE* output) {
 		ctx->responseMechanismMask, BAD_CAST "ResponseMechanism", output);    
 
 
-    if(ctx->expectedService != NULL) {    
-        fprintf(output, "<ExpectedService>%s</ExpectedService>\n", ctx->expectedService);
-    }
+    fprintf(output, "<ExpectedService>");
+    xmlSecPrintXmlString(output, ctx->expectedService);
+    fprintf(output, "</ExpectedService>\n");
+    
     fprintf(output, "<Flags>%08x</Flags>\n", ctx->flags);
     fprintf(output, "<Flags2>%08x</Flags2>\n", ctx->flags2);
 
@@ -2920,9 +2931,11 @@ xmlSecXkmsRespondWithDebugXmlDump(xmlSecXkmsRespondWithId id, FILE* output) {
     xmlSecAssert(id != xmlSecXkmsRespondWithIdUnknown);
     xmlSecAssert(output != NULL);
 
-    fprintf(output, "<RespondWith href=\"%s\">%s</RespondWith>\n", 
-        xmlSecErrorsSafeString(id->valueNs),
-        xmlSecErrorsSafeString(id->valueName));
+    fprintf(output, "<RespondWith href=\"");
+    xmlSecPrintXmlString(output, id->valueNs);
+    fprintf(output, "\">");
+    xmlSecPrintXmlString(output, id->valueName);
+    fprintf(output, "</RespondWith>\n");
 }
 
 int 
@@ -3781,7 +3794,9 @@ xmlSecXkmsServerRequestDebugXmlDump(xmlSecXkmsServerRequestId id, FILE* output)
     xmlSecAssert(id != xmlSecXkmsServerRequestIdUnknown);
     xmlSecAssert(output != NULL);
 
-    fprintf(output, "<ServerRequest>%s</ServerRequest>\n", xmlSecErrorsSafeString(id->name));
+    fprintf(output, "<ServerRequest>");
+    xmlSecPrintXmlString(output, id->name);
+    fprintf(output, "</ServerRequest>\n");
 }
 
 /************************************************************************
diff --git a/src/xmldsig.c b/src/xmldsig.c
index 8ebaaec..0233855 100644
--- a/src/xmldsig.c
+++ b/src/xmldsig.c
@@ -1167,9 +1167,9 @@ xmlSecDSigCtxDebugXmlDump(xmlSecDSigCtxPtr dsigCtx, FILE* output) {
     fprintf(output, "<Flags>%08x</Flags>\n", dsigCtx->flags);
     fprintf(output, "<Flags2>%08x</Flags2>\n", dsigCtx->flags2);
 
-    if(dsigCtx->id != NULL) {
-	fprintf(output, "<Id>%s</Id>\n", dsigCtx->id);
-    }
+    fprintf(output, "<Id>");
+    xmlSecPrintXmlString(output, dsigCtx->id);
+    fprintf(output, "</Id>\n");
 
     fprintf(output, "<KeyInfoReadCtx>\n");
     xmlSecKeyInfoCtxDebugXmlDump(&(dsigCtx->keyInfoReadCtx), output);
@@ -1716,15 +1716,17 @@ xmlSecDSigReferenceCtxDebugXmlDump(xmlSecDSigReferenceCtxPtr dsigRefCtx, FILE* o
 	    break;
     }
 
-    if(dsigRefCtx->id != NULL) {
-	fprintf(output, "<Id>%s</Id>\n", dsigRefCtx->id);
-    }
-    if(dsigRefCtx->uri != NULL) {
-	fprintf(output, "<URI>%s</URI>\n", dsigRefCtx->uri);
-    }
-    if(dsigRefCtx->type != NULL) {
-	fprintf(output, "<Type>%s</Type>\n", dsigRefCtx->type);
-    }
+    fprintf(output, "<Id>");
+    xmlSecPrintXmlString(output, dsigRefCtx->id);
+    fprintf(output, "</Id>\n");
+
+    fprintf(output, "<URI>");
+    xmlSecPrintXmlString(output, dsigRefCtx->uri);
+    fprintf(output, "</URI>\n");
+
+    fprintf(output, "<Type>");
+    xmlSecPrintXmlString(output, dsigRefCtx->type);
+    fprintf(output, "</Type>\n");
 
     fprintf(output, "<ReferenceTransformCtx>\n");
     xmlSecTransformCtxDebugXmlDump(&(dsigRefCtx->transformCtx), output);
diff --git a/src/xmlenc.c b/src/xmlenc.c
index 9112045..d8f23ae 100644
--- a/src/xmlenc.c
+++ b/src/xmlenc.c
@@ -1258,24 +1258,29 @@ xmlSecEncCtxDebugXmlDump(xmlSecEncCtxPtr encCtx, FILE* output) {
     fprintf(output, "<Flags>%08x</Flags>\n", encCtx->flags);
     fprintf(output, "<Flags2>%08x</Flags2>\n", encCtx->flags2);
 
-    if(encCtx->id != NULL) {
-	fprintf(output, "<Id>%s</Id>\n", encCtx->id);
-    }
-    if(encCtx->type != NULL) {
-	fprintf(output, "<Type>%s</Type>\n", encCtx->type);
-    }
-    if(encCtx->mimeType != NULL) {
-	fprintf(output, "<MimeType>%s</MimeType>\n", encCtx->mimeType);
-    }
-    if(encCtx->encoding != NULL) {
-	fprintf(output, "<Encoding>%s</Encoding>\n", encCtx->encoding);
-    }
-    if(encCtx->recipient != NULL) {
-	fprintf(output, "<Recipient>%s</Recipient>\n", encCtx->recipient);
-    }
-    if(encCtx->carriedKeyName != NULL) {
-	fprintf(output, "<CarriedKeyName>%s</CarriedKeyName>\n", encCtx->carriedKeyName);
-    }
+    fprintf(output, "<Id>");
+    xmlSecPrintXmlString(output, encCtx->id);
+    fprintf(output, "</Id>");
+
+    fprintf(output, "<Type>");
+    xmlSecPrintXmlString(output, encCtx->type);
+    fprintf(output, "</Type>");
+    
+    fprintf(output, "<MimeType>");
+    xmlSecPrintXmlString(output, encCtx->mimeType);
+    fprintf(output, "</MimeType>");
+
+    fprintf(output, "<Encoding>");
+    xmlSecPrintXmlString(output, encCtx->encoding);
+    fprintf(output, "</Encoding>");
+
+    fprintf(output, "<Recipient>");
+    xmlSecPrintXmlString(output, encCtx->recipient);
+    fprintf(output, "</Recipient>");
+
+    fprintf(output, "<CarriedKeyName>");
+    xmlSecPrintXmlString(output, encCtx->carriedKeyName);
+    fprintf(output, "</CarriedKeyName>");
 
     fprintf(output, "<KeyInfoReadCtx>\n");
     xmlSecKeyInfoCtxDebugXmlDump(&(encCtx->keyInfoReadCtx), output);
diff --git a/src/xmltree.c b/src/xmltree.c
index 1d3cfa9..0181be8 100644
--- a/src/xmltree.c
+++ b/src/xmltree.c
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
  
 #include <libxml/tree.h>
 #include <libxml/valid.h>
@@ -960,6 +961,51 @@ xmlSecIsEmptyString(const xmlChar* str) {
     return(1);
 }
 
+/**
+ * xmlSecPrintXmlString: 
+ * @fd:                the file descriptor to write the XML string to
+ * @str:               the string
+ *
+ * Encodes the @str (e.g. replaces '&' with '&amp;') and writes it to @fd.
+ *
+ * Returns he number of bytes transmitted or a negative value if an error occurs.
+ */
+int 
+xmlSecPrintXmlString(FILE * fd, const xmlChar * str) {
+    int res;
+    
+    if(str != NULL) {    
+        xmlChar * encoded_str = NULL;
+        encoded_str = xmlEncodeSpecialChars(NULL, str);
+        if(encoded_str == NULL) {
+	    xmlSecError(XMLSEC_ERRORS_HERE,	
+		        NULL,
+		        "xmlEncodeSpecialChars",
+		        XMLSEC_ERRORS_R_XML_FAILED,
+		        "string=%s",
+		        xmlSecErrorsSafeString(str));
+	    return(-1);
+        }
+    
+        res = fprintf(fd, "%s", (const char*)encoded_str);
+	xmlFree(encoded_str);
+    } else {
+        res = fprintf(fd, "NULL");
+    }
+    
+    if(res < 0) {
+	xmlSecError(XMLSEC_ERRORS_HERE,	
+		    NULL,
+		    "fprintf",
+		    XMLSEC_ERRORS_R_IO_FAILED,
+		    "res=%d,errno=%d",
+		    res, errno);
+	return(-1);
+    }
+    return(res);
+}
+
+
 /** 
  * xmlSecGetQName:
  * @node:		the context node.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]