diff options
author | Konrad Lipinski <k.lipinski2@partner.samsung.com> | 2019-06-11 16:25:51 +0200 |
---|---|---|
committer | Konrad Lipinski <k.lipinski2@samsung.com> | 2019-08-01 14:37:53 +0200 |
commit | cc6febdd37186eeea33bcbce89d79f661ee0009f (patch) | |
tree | 100542f7f8fd3b3c9548150362efe35adde47551 /src/io.c | |
parent | c40fbfa8503e7763ef630496852f4d6b5e63b58c (diff) | |
download | xmlsec1-cc6febdd37186eeea33bcbce89d79f661ee0009f.tar.gz xmlsec1-cc6febdd37186eeea33bcbce89d79f661ee0009f.tar.bz2 xmlsec1-cc6febdd37186eeea33bcbce89d79f661ee0009f.zip |
Import upstream commit c4d0493d545b99194eea1b2b058930d5a9bb91b1 (1.2.28)
Change-Id: I10f71567cb140be223923e1cd0b5895e366ac23e
Diffstat (limited to 'src/io.c')
-rw-r--r-- | src/io.c | 118 |
1 files changed, 44 insertions, 74 deletions
@@ -1,4 +1,4 @@ -/** +/* * XML Security Library (http://www.aleksey.com/xmlsec). * * Input uri transform and utility functions. @@ -8,11 +8,17 @@ * * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved. */ +/** + * SECTION:io + * @Short_description: Input/output functions. + * @Stability: Stable + * + */ + #include "globals.h" #include <stdlib.h> #include <string.h> -#include <errno.h> #include <libxml/uri.h> #include <libxml/tree.h> @@ -33,7 +39,6 @@ #include <xmlsec/io.h> #include <xmlsec/errors.h> -#define ERR_BUF_SIZE 1024 /******************************************************************* * @@ -63,12 +68,7 @@ xmlSecIOCallbackCreate(xmlInputMatchCallback matchFunc, xmlInputOpenCallback ope /* Allocate a new xmlSecIOCallback and fill the fields. */ callbacks = (xmlSecIOCallbackPtr)xmlMalloc(sizeof(xmlSecIOCallback)); if(callbacks == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - NULL, - XMLSEC_ERRORS_R_MALLOC_FAILED, - "sizeof(xmlSecIOCallback)=%d", - (int)sizeof(xmlSecIOCallback)); + xmlSecMallocError(sizeof(xmlSecIOCallback), NULL); return(NULL); } memset(callbacks, 0, sizeof(xmlSecIOCallback)); @@ -127,9 +127,10 @@ xmlSecIOCallbackPtrListFind(xmlSecPtrListPtr list, const char* uri) { xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecIOCallbackPtrListId), NULL); xmlSecAssert2(uri != NULL, NULL); + /* Search from the end of the list to ensure the newly added entries are picked up first */ size = xmlSecPtrListGetSize(list); - for(i = 0; i < size; ++i) { - callbacks = (xmlSecIOCallbackPtr)xmlSecPtrListGetItem(list, i); + for(i = size; i > 0; --i) { + callbacks = (xmlSecIOCallbackPtr)xmlSecPtrListGetItem(list, i - 1); xmlSecAssert2(callbacks != NULL, NULL); xmlSecAssert2(callbacks->matchcallback != NULL, NULL); @@ -156,29 +157,31 @@ xmlSecIOInit(void) { ret = xmlSecPtrListInitialize(&xmlSecAllIOCallbacks, xmlSecIOCallbackPtrListId); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListPtrInitialize", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecPtrListInitialize", NULL); return(-1); } +#ifdef LIBXML_FTP_ENABLED + xmlNanoFTPInit(); +#endif /* LIBXML_FTP_ENABLED */ + #ifdef LIBXML_HTTP_ENABLED xmlNanoHTTPInit(); #endif /* LIBXML_HTTP_ENABLED */ -#ifdef LIBXML_FTP_ENABLED - xmlNanoFTPInit(); -#endif /* LIBXML_FTP_ENABLED */ + ret = xmlSecIORegisterDefaultCallbacks(); + if(ret < 0) { + xmlSecInternalError("xmlSecIORegisterDefaultCallbacks", NULL); + return(-1); + } - return(xmlSecIORegisterDefaultCallbacks()); + return(0); } /** * xmlSecIOShutdown: * - * The IO clenaup (called from #xmlSecShutdown function). + * The IO cleanup (called from #xmlSecShutdown function). * Applications should not call this function directly. */ void @@ -228,21 +231,13 @@ xmlSecIORegisterCallbacks(xmlInputMatchCallback matchFunc, callbacks = xmlSecIOCallbackCreate(matchFunc, openFunc, readFunc, closeFunc); if(callbacks == NULL) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecIOCallbackCreate", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecIOCallbackCreate", NULL); return(-1); } ret = xmlSecPtrListAdd(&xmlSecAllIOCallbacks, callbacks); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecPtrListAdd", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - XMLSEC_ERRORS_NO_MESSAGE); + xmlSecInternalError("xmlSecPtrListAdd", NULL); xmlSecIOCallbackDestroy(callbacks); return(-1); } @@ -261,15 +256,19 @@ int xmlSecIORegisterDefaultCallbacks(void) { int ret; + /* Callbacks added later are picked up first */ + ret = xmlSecIORegisterCallbacks(xmlFileMatch, xmlFileOpen, + xmlFileRead, xmlFileClose); + if(ret < 0) { + xmlSecInternalError("xmlSecIORegisterCallbacks(file)", NULL); + return(-1); + } + #ifdef LIBXML_HTTP_ENABLED ret = xmlSecIORegisterCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, xmlIOHTTPRead, xmlIOHTTPClose); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecIORegisterCallbacks", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "http"); + xmlSecInternalError("xmlSecIORegisterCallbacks(http)", NULL); return(-1); } #endif /* LIBXML_HTTP_ENABLED */ @@ -278,26 +277,12 @@ xmlSecIORegisterDefaultCallbacks(void) { ret = xmlSecIORegisterCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, xmlIOFTPRead, xmlIOFTPClose); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecIORegisterCallbacks", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "ftp"); + xmlSecInternalError("xmlSecIORegisterCallbacks(ftp)", NULL); return(-1); } #endif /* LIBXML_FTP_ENABLED */ - ret = xmlSecIORegisterCallbacks(xmlFileMatch, xmlFileOpen, - xmlFileRead, xmlFileClose); - if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - NULL, - "xmlSecIORegisterCallbacks", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "file"); - return(-1); - } - + /* done */ return(0); } @@ -421,16 +406,9 @@ xmlSecTransformInputURIOpen(xmlSecTransformPtr transform, const xmlChar *uri) { } } - char buf[ERR_BUF_SIZE]; if((ctx->clbks == NULL) || (ctx->clbksCtx == NULL)) { - strerror_r(errno, buf, sizeof(buf)); - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "opencallback", - XMLSEC_ERRORS_R_IO_FAILED, - "uri=%s;error=%s", - xmlSecErrorsSafeString(uri), - buf); + xmlSecInternalError2("ctx->clbks->opencallback", xmlSecTransformGetName(transform), + "uri=%s", xmlSecErrorsSafeString(uri)); return(-1); } @@ -442,7 +420,7 @@ xmlSecTransformInputURIOpen(xmlSecTransformPtr transform, const xmlChar *uri) { * xmlSecTransformInputURIClose: * @transform: the pointer to IO transform. * - * Closes the given @transform and frees up resourses. + * Closes the given @transform and frees up resources. * * Returns: 0 on success or a negative value otherwise. */ @@ -491,11 +469,9 @@ xmlSecTransformInputURIFinalize(xmlSecTransformPtr transform) { ret = xmlSecTransformInputURIClose(transform); if(ret < 0) { - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "xmlSecTransformInputURIClose", - XMLSEC_ERRORS_R_XMLSEC_FAILED, - "ret=%d", ret); + xmlSecInternalError2("xmlSecTransformInputURIClose", + xmlSecTransformGetName(transform), + "ret=%d", ret); /* ignore the error */ /* return; */ } @@ -523,13 +499,7 @@ xmlSecTransformInputURIPopBin(xmlSecTransformPtr transform, xmlSecByte* data, if((ctx->clbksCtx != NULL) && (ctx->clbks != NULL) && (ctx->clbks->readcallback != NULL)) { ret = (ctx->clbks->readcallback)(ctx->clbksCtx, (char*)data, (int)maxDataSize); if(ret < 0) { - char buf[ERR_BUF_SIZE]; - strerror_r(errno, buf, sizeof(buf)); - xmlSecError(XMLSEC_ERRORS_HERE, - xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), - "readcallback", - XMLSEC_ERRORS_R_IO_FAILED, - "error=%s", buf); + xmlSecInternalError("ctx->clbks->readcallback", xmlSecTransformGetName(transform)); return(-1); } (*dataSize) = ret; |