diff options
Diffstat (limited to 'src/xslt.c')
-rw-r--r-- | src/xslt.c | 181 |
1 files changed, 60 insertions, 121 deletions
@@ -1,13 +1,20 @@ -/** +/* * XML Security Library (http://www.aleksey.com/xmlsec). * - * XSLT Transform (http://www.w3.org/TR/xmldsig-core/#sec-XSLT) * * This is free software; see Copyright file in the source * distribution for preciese wording. * * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved. */ +/** + * SECTION:xslt + * @Short_description: XSLT transform implementation. + * @Stability: Private + * + * [XSLT Transform](http://www.w3.org/TR/xmldsig-core/#sec-XSLT) implementation. + */ + #include "globals.h" #ifndef XMLSEC_NO_XSLT @@ -212,6 +219,10 @@ xmlSecXsltFinalize(xmlSecTransformPtr transform) { xsltFreeStylesheet(ctx->xslt); } if(ctx->parserCtx != NULL) { + if(ctx->parserCtx->myDoc != NULL) { + xmlFreeDoc(ctx->parserCtx->myDoc); + ctx->parserCtx->myDoc = NULL; + } xmlFreeParserCtxt(ctx->parserCtx); } memset(ctx, 0, sizeof(xmlSecXsltCtx)); @@ -236,11 +247,7 @@ xmlSecXsltReadNode(xmlSecTransformPtr transform, xmlNodePtr node, xmlSecTransfor /* read content in the buffer */ buffer = xmlBufferCreate(); if(buffer == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlBufferCreate", - XMLSEC_ERRORS_R_XML_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXmlError("xmlBufferCreate", xmlSecTransformGetName(transform)); return(-1); } cur = node->children; @@ -253,11 +260,8 @@ xmlSecXsltReadNode(xmlSecTransformPtr transform, xmlNodePtr node, xmlSecTransfor doc = xmlSecParseMemory(xmlBufferContent(buffer), xmlBufferLength(buffer), 1); if(doc == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecParseMemory", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecParseMemory", + xmlSecTransformGetName(transform)); xmlBufferFree(buffer); return(-1); } @@ -265,11 +269,8 @@ xmlSecXsltReadNode(xmlSecTransformPtr transform, xmlNodePtr node, xmlSecTransfor /* pre-process stylesheet */ ctx->xslt = xsltParseStylesheetDoc(doc); if(ctx->xslt == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xsltParseStylesheetDoc", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltParseStylesheetDoc", NULL, xmlSecTransformGetName(transform)); + /* after parsing stylesheet doc is assigned * to it and will be freed by xsltFreeStylesheet() */ xmlFreeDoc(doc); @@ -301,11 +302,7 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, ctx->parserCtx = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL); if(ctx->parserCtx == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlCreatePushParserCtxt", - XMLSEC_ERRORS_R_XML_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXmlError("xmlCreatePushParserCtxt", xmlSecTransformGetName(transform)); return(-1); } @@ -317,11 +314,7 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, } else if(transform->status == xmlSecTransformStatusFinished) { return(0); } else if(transform->status != xmlSecTransformStatusWorking) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_STATUS, - "status=%d", transform->status); + xmlSecInvalidTransfromStatusError(transform); return(-1); } xmlSecAssert2(transform->status == xmlSecTransformStatusWorking, -1); @@ -331,11 +324,10 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, if((data != NULL) && (dataSize > 0)) { ret = xmlParseChunk(ctx->parserCtx, (const char*)data, dataSize, 0); if(ret != 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlParseChunk", - XMLSEC_ERRORS_R_XML_FAILED, - "size=%d", dataSize); + xmlSecXmlParserError2("xmlParseChunk", ctx->parserCtx, + xmlSecTransformGetName(transform), + "size=%d", dataSize); + return(-1); } } @@ -349,11 +341,8 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, /* finalize */ ret = xmlParseChunk(ctx->parserCtx, NULL, 0, 1); if((ret != 0) || (ctx->parserCtx->myDoc == NULL)) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlParseChunk", - XMLSEC_ERRORS_R_XML_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXmlParserError("xmlParseChunk", ctx->parserCtx, + xmlSecTransformGetName(transform)); return(-1); } @@ -363,11 +352,8 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, docOut = xmlSecXsApplyStylesheet(ctx, docIn); if(docOut == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecXsApplyStylesheet", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecXsApplyStylesheet", + xmlSecTransformGetName(transform)); xmlFreeDoc(docIn); return(-1); } @@ -376,22 +362,16 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, if(transform->next != NULL) { output = xmlSecTransformCreateOutputBuffer(transform->next, transformCtx); if(output == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecTransformCreateOutputBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecTransformCreateOutputBuffer", + xmlSecTransformGetName(transform)); xmlFreeDoc(docOut); return(-1); } } else { output = xmlSecBufferCreateOutputBuffer(&(transform->outBuf)); if(output == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferCreateOutputBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferCreateOutputBuffer", + xmlSecTransformGetName(transform)); xmlFreeDoc(docOut); return(-1); } @@ -399,22 +379,14 @@ xmlSecXsltPushBin(xmlSecTransformPtr transform, const xmlSecByte* data, ret = xsltSaveResultTo(output, docOut, ctx->xslt); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xsltSaveResultTo", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltParseStylesheetDoc", ctx->xslt, xmlSecTransformGetName(transform)); xmlOutputBufferClose(output); xmlFreeDoc(docOut); return(-1); } ret = xmlOutputBufferClose(output); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlOutputBufferClose", - XMLSEC_ERRORS_R_XML_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXmlError("xmlOutputBufferClose", xmlSecTransformGetName(transform)); xmlFreeDoc(docOut); return(-1); } @@ -459,21 +431,16 @@ xmlSecXsltExecute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPtr ret = xmlSecXslProcess(ctx, in, out); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecXslProcess", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecXslProcess", + xmlSecTransformGetName(transform)); return(-1); } ret = xmlSecBufferRemoveHead(in, inSize); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecBufferRemoveHead", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "size=%d", inSize); + xmlSecInternalError2("xmlSecBufferRemoveHead", + xmlSecTransformGetName(transform), + "size=%d", inSize); return(-1); } @@ -482,11 +449,7 @@ xmlSecXsltExecute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPtr /* the only way we can get here is if there is no input */ xmlSecAssert2(inSize == 0, -1); } else { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - NULL, - XMLSEC_ERRORS_R_INVALID_STATUS, - "status=%d", transform->status); + xmlSecInvalidTransfromStatusError(transform); return(-1); } return(0); @@ -507,61 +470,47 @@ xmlSecXslProcess(xmlSecXsltCtxPtr ctx, xmlSecBufferPtr in, xmlSecBufferPtr out) docIn = xmlSecParseMemory(xmlSecBufferGetData(in), xmlSecBufferGetSize(in), 1); if(docIn == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecParseMemory", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecParseMemory", NULL); goto done; } docOut = xmlSecXsApplyStylesheet(ctx, docIn); if(docOut == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecXsApplyStylesheet", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecXsApplyStylesheet", NULL); goto done; } output = xmlSecBufferCreateOutputBuffer(out); if(output == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecBufferCreateOutputBuffer", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecBufferCreateOutputBuffer", NULL); goto done; } ret = xsltSaveResultTo(output, docOut, ctx->xslt); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xsltSaveResultTo", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltSaveResultTo", ctx->xslt, NULL); goto done; } ret = xmlOutputBufferClose(output); output = NULL; if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlOutputBufferClose", - XMLSEC_ERRORS_R_XML_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXmlError("xmlOutputBufferClose", NULL); return(-1); } res = 0; done: - if(output != NULL) xmlOutputBufferClose(output); - if(docIn != NULL) xmlFreeDoc(docIn); - if(docOut != NULL) xmlFreeDoc(docOut); + if(output != NULL) { + xmlOutputBufferClose(output); + } + if(docIn != NULL) { + xmlFreeDoc(docIn); + } + if(docOut != NULL) { + xmlFreeDoc(docOut); + } return(res); } @@ -578,37 +527,27 @@ xmlSecXsApplyStylesheet(xmlSecXsltCtxPtr ctx, xmlDocPtr doc) { xsltCtx = xsltNewTransformContext(ctx->xslt, doc); if(xsltCtx == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xsltNewTransformContext", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltNewTransformContext", ctx->xslt, NULL); goto done; } /* set security prefs */ ret = xsltSetCtxtSecurityPrefs(g_xslt_default_security_prefs, xsltCtx); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xsltSetCtxtSecurityPrefs", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltSetCtxtSecurityPrefs", ctx->xslt, NULL); goto done; } res = xsltApplyStylesheetUser(ctx->xslt, doc, NULL, NULL, NULL, xsltCtx); if(res == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xsltApplyStylesheetUser", - XMLSEC_ERRORS_R_XSLT_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecXsltError("xsltApplyStylesheetUser", ctx->xslt, NULL); goto done; } done: - if(xsltCtx != NULL) xsltFreeTransformContext(xsltCtx); + if(xsltCtx != NULL) { + xsltFreeTransformContext(xsltCtx); + } return res; } |