summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c137
1 files changed, 38 insertions, 99 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 55a95dda..cc0da65f 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1,19 +1,24 @@
-/**
+/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
- * Memory buffer.
*
* 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:buffer
+ * @Short_description:Binary memory buffer functions.
+ * @Stability: Stable
+ *
+ */
+
#include "globals.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#include <errno.h>
#include <libxml/tree.h>
@@ -50,7 +55,7 @@ xmlSecBufferSetDefaultAllocMode(xmlSecAllocMode defAllocMode, xmlSecSize defInit
* xmlSecBufferCreate:
* @size: the intial size.
*
- * Allocates and initalizes new memory buffer with given size.
+ * Allocates and initializes new memory buffer with given size.
* Caller is responsible for calling #xmlSecBufferDestroy function
* to free the buffer.
*
@@ -63,21 +68,13 @@ xmlSecBufferCreate(xmlSecSize size) {
buf = (xmlSecBufferPtr)xmlMalloc(sizeof(xmlSecBuffer));
if(buf == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_MALLOC_FAILED,
- "sizeof(xmlSecBuffer)=%d", (int)sizeof(xmlSecBuffer));
+ xmlSecMallocError(sizeof(xmlSecBuffer), NULL);
return(NULL);
}
ret = xmlSecBufferInitialize(buf, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBufferInitialize", NULL, "size=%d", size);
xmlSecBufferDestroy(buf);
return(NULL);
}
@@ -88,7 +85,7 @@ xmlSecBufferCreate(xmlSecSize size) {
* xmlSecBufferDestroy:
* @buf: the pointer to buffer object.
*
- * Desrtoys buffer object created with #xmlSecBufferCreate function.
+ * Destroys buffer object created with #xmlSecBufferCreate function.
*/
void
xmlSecBufferDestroy(xmlSecBufferPtr buf) {
@@ -123,7 +120,7 @@ xmlSecBufferInitialize(xmlSecBufferPtr buf, xmlSecSize size) {
* xmlSecBufferFinalize:
* @buf: the pointer to buffer object.
*
- * Frees allocated resource for a buffer intialized with #xmlSecBufferInitialize
+ * Frees allocated resource for a buffer initialized with #xmlSecBufferInitialize
* function.
*/
void
@@ -193,11 +190,7 @@ xmlSecBufferSetData(xmlSecBufferPtr buf, const xmlSecByte* data, xmlSecSize size
ret = xmlSecBufferSetMaxSize(buf, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize", NULL, "size=%d", size);
return(-1);
}
@@ -241,11 +234,7 @@ xmlSecBufferSetSize(xmlSecBufferPtr buf, xmlSecSize size) {
ret = xmlSecBufferSetMaxSize(buf, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize", NULL, "size=%d", size);
return(-1);
}
@@ -309,11 +298,7 @@ xmlSecBufferSetMaxSize(xmlSecBufferPtr buf, xmlSecSize size) {
newData = (xmlSecByte*)xmlMalloc(newSize);
}
if(newData == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_MALLOC_FAILED,
- "size=%d", newSize);
+ xmlSecMallocError(newSize, NULL);
return(-1);
}
@@ -349,11 +334,7 @@ xmlSecBufferAppend(xmlSecBufferPtr buf, const xmlSecByte* data, xmlSecSize size)
ret = xmlSecBufferSetMaxSize(buf, buf->size + size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", buf->size + size);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize", NULL, "size=%d", buf->size + size);
return(-1);
}
@@ -385,11 +366,7 @@ xmlSecBufferPrepend(xmlSecBufferPtr buf, const xmlSecByte* data, xmlSecSize size
ret = xmlSecBufferSetMaxSize(buf, buf->size + size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", buf->size + size);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize", NULL, "size=%d", buf->size + size);
return(-1);
}
@@ -466,48 +443,34 @@ xmlSecBufferRemoveTail(xmlSecBufferPtr buf, xmlSecSize size) {
int
xmlSecBufferReadFile(xmlSecBufferPtr buf, const char* filename) {
xmlSecByte buffer[1024];
- FILE* f;
- int ret, len;
+ FILE* f = NULL;
+ size_t len;
+ int ret;
xmlSecAssert2(buf != NULL, -1);
xmlSecAssert2(filename != NULL, -1);
+#ifndef _MSC_VER
f = fopen(filename, "rb");
+#else
+ fopen_s(&f, filename, "rb");
+#endif /* _MSC_VER */
if(f == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "fopen",
- XMLSEC_ERRORS_R_IO_FAILED,
- "filename=%s;errno=%d",
- xmlSecErrorsSafeString(filename),
- errno);
+ xmlSecIOError("fopen", filename, NULL);
return(-1);
}
- while(1) {
+ while(!feof(f)) {
len = fread(buffer, 1, sizeof(buffer), f);
- if(len == 0) {
- break;
- }else if(len < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "fread",
- XMLSEC_ERRORS_R_IO_FAILED,
- "filename=%s;errno=%d",
- xmlSecErrorsSafeString(filename),
- errno);
+ if(ferror(f)) {
+ xmlSecIOError("fread", filename, NULL);
fclose(f);
return(-1);
}
- ret = xmlSecBufferAppend(buf, buffer, len);
+ ret = xmlSecBufferAppend(buf, buffer, XMLSEC_SIZE_BAD_CAST(len));
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferAppend",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d",
- len);
+ xmlSecInternalError2("xmlSecBufferAppend", NULL, "size=%d", XMLSEC_SIZE_BAD_CAST(len));
fclose(f);
return(-1);
}
@@ -538,33 +501,21 @@ xmlSecBufferBase64NodeContentRead(xmlSecBufferPtr buf, xmlNodePtr node) {
content = xmlNodeGetContent(node);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- xmlSecErrorsSafeString(xmlSecNodeGetName(node)),
- XMLSEC_ERRORS_R_INVALID_NODE_CONTENT,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInvalidNodeContentError(node, NULL, "empty");
return(-1);
}
/* base64 decode size is less than input size */
ret = xmlSecBufferSetMaxSize(buf, xmlStrlen(content));
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBufferSetMaxSize", NULL);
xmlFree(content);
return(-1);
}
ret = xmlSecBase64Decode(content, xmlSecBufferGetData(buf), xmlSecBufferGetMaxSize(buf));
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64Decode",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64Decode", NULL);
xmlFree(content);
return(-1);
}
@@ -572,11 +523,7 @@ xmlSecBufferBase64NodeContentRead(xmlSecBufferPtr buf, xmlNodePtr node) {
ret = xmlSecBufferSetSize(buf, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferSetSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBufferSetSize", NULL, "size=%d", size);
xmlFree(content);
return(-1);
}
@@ -589,7 +536,7 @@ xmlSecBufferBase64NodeContentRead(xmlSecBufferPtr buf, xmlNodePtr node) {
* xmlSecBufferBase64NodeContentWrite:
* @buf: the pointer to buffer object.
* @node: the pointer to a node.
- * @columns: the max line size fro base64 encoded data.
+ * @columns: the max line size for base64 encoded data.
*
* Sets the content of the @node to the base64 encoded buffer data.
*
@@ -604,11 +551,7 @@ xmlSecBufferBase64NodeContentWrite(xmlSecBufferPtr buf, xmlNodePtr node, int col
content = xmlSecBase64Encode(xmlSecBufferGetData(buf), xmlSecBufferGetSize(buf), columns);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64Encode",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64Encode", NULL);
return(-1);
}
xmlNodeAddContent(node, content);
@@ -654,11 +597,7 @@ xmlSecBufferIOWrite(xmlSecBufferPtr buf, const xmlSecByte *data, xmlSecSize size
ret = xmlSecBufferAppend(buf, data, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferAppend",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBufferAppend", NULL, "size=%d", size);
return(-1);
}