summaryrefslogtreecommitdiff
path: root/src/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/base64.c')
-rw-r--r--src/base64.c227
1 files changed, 58 insertions, 169 deletions
diff --git a/src/base64.c b/src/base64.c
index a78f8164..cc4bbd5f 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -1,13 +1,19 @@
-/**
+/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
- * Base64 encode/decode transform and utility functions.
*
* 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:base64
+ * @Short_description: Base64 encoding/decoding functions and base64 transform implementation.
+ * @Stability: Stable
+ *
+ */
+
#include "globals.h"
#include <stdlib.h>
@@ -156,22 +162,13 @@ xmlSecBase64CtxCreate(int encode, int columns) {
*/
ctx = (xmlSecBase64CtxPtr) xmlMalloc(sizeof(xmlSecBase64Ctx));
if (ctx == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_MALLOC_FAILED,
- "sizeof(xmlSecBase64Ctx)=%d",
- (int)sizeof(xmlSecBase64Ctx));
+ xmlSecMallocError(sizeof(xmlSecBase64Ctx), NULL);
return(NULL);
}
ret = xmlSecBase64CtxInitialize(ctx, encode, columns);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxInitialize", NULL);
xmlSecBase64CtxDestroy(ctx);
return(NULL);
}
@@ -254,22 +251,14 @@ xmlSecBase64CtxUpdate(xmlSecBase64CtxPtr ctx,
ret = xmlSecBase64CtxEncode(ctx, in, inSize, &inResSize,
out, outSize, &outResSize);
if((ret < 0) || (inResSize != inSize)) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxEncode",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxEncode", NULL);
return(-1);
}
} else {
ret = xmlSecBase64CtxDecode(ctx, in, inSize, &inResSize,
out, outSize, &outResSize);
if((ret < 0) || (inResSize != inSize)) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxDecode",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxDecode", NULL);
return(-1);
}
}
@@ -302,20 +291,12 @@ xmlSecBase64CtxFinal(xmlSecBase64CtxPtr ctx,
if(ctx->encode != 0) {
ret = xmlSecBase64CtxEncodeFinal(ctx, out, outSize, &outResSize);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxEncodeFinal",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "outSize=%d", outSize);
+ xmlSecInternalError2("xmlSecBase64CtxEncodeFinal", NULL, "outSize=%d", outSize);
return(-1);
}
} else {
if(!xmlSecBase64CtxDecodeIsFinished(ctx)) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxIsFinished",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxDecodeIsFinished", NULL);
return(-1);
}
}
@@ -363,11 +344,7 @@ xmlSecBase64CtxEncodeByte(xmlSecBase64CtxPtr ctx, xmlSecByte inByte, xmlSecByte*
return(xmlSecBase64StatusConsumeAndNext);
}
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "ctx->inPos=%d", ctx->inPos);
+ xmlSecInvalidIntegerDataError("ctx->inPos", ctx->inPos, "0,1,2,3", NULL);
return(xmlSecBase64StatusFailed);
}
@@ -397,11 +374,7 @@ xmlSecBase64CtxEncodeByteFinal(xmlSecBase64CtxPtr ctx, xmlSecByte* outByte) {
return(xmlSecBase64StatusConsumeAndRepeat);
}
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "ctx->inPos=%d", ctx->inPos);
+ xmlSecInvalidIntegerDataError("ctx->inPos", ctx->inPos, "0,1,2,3", NULL);
return(xmlSecBase64StatusFailed);
}
@@ -414,35 +387,20 @@ xmlSecBase64CtxDecodeByte(xmlSecBase64CtxPtr ctx, xmlSecByte inByte, xmlSecByte*
return(xmlSecBase64StatusDone);
} if(inByte == '=') {
ctx->finished = 1;
- if(ctx->inPos < 2) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "ctx->inPos=%d", ctx->inPos);
- return(xmlSecBase64StatusFailed);
- } else if(ctx->inPos == 2) {
+ if(ctx->inPos == 2) {
++ctx->inPos;
return(xmlSecBase64StatusNext);
} else if(ctx->inPos == 3) {
ctx->inPos = 0;
return(xmlSecBase64StatusNext);
} else {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "ctx->inPos=%d", ctx->inPos);
+ xmlSecInvalidIntegerDataError("ctx->inPos", ctx->inPos, "2,3", NULL);
return(xmlSecBase64StatusFailed);
}
} else if(xmlSecIsBase64Space(inByte)) {
return(xmlSecBase64StatusNext);
} else if(!xmlSecIsBase64Char(inByte) || (ctx->finished != 0)) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "inByte=0x%02x", inByte);
+ xmlSecInvalidIntegerDataError("inByte", inByte, "base64 character", NULL);
return(xmlSecBase64StatusFailed);
}
@@ -464,27 +422,23 @@ xmlSecBase64CtxDecodeByte(xmlSecBase64CtxPtr ctx, xmlSecByte inByte, xmlSecByte*
++ctx->inPos;
return(xmlSecBase64StatusNext);
} else if(ctx->inPos == 1) {
- (*outByte) = xmlSecBase64Decode1(ctx->inByte, inByte);
+ (*outByte) = (xmlSecByte)xmlSecBase64Decode1(ctx->inByte, inByte);
ctx->inByte = inByte;
++ctx->inPos;
return(xmlSecBase64StatusConsumeAndNext);
} else if(ctx->inPos == 2) {
- (*outByte) = xmlSecBase64Decode2(ctx->inByte, inByte);
+ (*outByte) = (xmlSecByte)xmlSecBase64Decode2(ctx->inByte, inByte);
ctx->inByte = inByte;
++ctx->inPos;
return(xmlSecBase64StatusConsumeAndNext);
} else if(ctx->inPos == 3) {
- (*outByte) = xmlSecBase64Decode3(ctx->inByte, inByte);
+ (*outByte) = (xmlSecByte)xmlSecBase64Decode3(ctx->inByte, inByte);
ctx->inByte = 0;
ctx->inPos = 0;
return(xmlSecBase64StatusConsumeAndNext);
}
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "ctx->inPos=%d", ctx->inPos);
+ xmlSecInvalidIntegerDataError("ctx->inPos", ctx->inPos, "0,1,2,3", NULL);
return(xmlSecBase64StatusFailed);
}
@@ -516,11 +470,7 @@ xmlSecBase64CtxEncode(xmlSecBase64CtxPtr ctx,
case xmlSecBase64StatusNext:
case xmlSecBase64StatusDone:
case xmlSecBase64StatusFailed:
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxEncodeByte",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "status=%d", status);
+ xmlSecInternalError2("xmlSecBase64CtxEncodeByte", NULL, "status=%d", status);
return(-1);
}
}
@@ -553,21 +503,13 @@ xmlSecBase64CtxEncodeFinal(xmlSecBase64CtxPtr ctx,
break;
case xmlSecBase64StatusNext:
case xmlSecBase64StatusFailed:
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxEncodeByteFinal",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "status=%d", status);
+ xmlSecInternalError2("xmlSecBase64CtxEncodeByteFinal", NULL, "status=%d", status);
return(-1);
}
}
if(status != xmlSecBase64StatusDone) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_SIZE,
- "outBufSize=%d", outBufSize);
+ xmlSecInvalidSizeOtherError("invalid base64 buffer size", NULL);
return(-1);
}
if(outPos < outBufSize) {
@@ -609,11 +551,7 @@ xmlSecBase64CtxDecode(xmlSecBase64CtxPtr ctx,
case xmlSecBase64StatusDone:
break;
case xmlSecBase64StatusFailed:
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxDecodeByte",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "status=%d", status);
+ xmlSecInternalError2("xmlSecBase64CtxDecodeByte", NULL, "status=%d", status);
return(-1);
}
}
@@ -662,11 +600,7 @@ xmlSecBase64Encode(const xmlSecByte *buf, xmlSecSize len, int columns) {
ret = xmlSecBase64CtxInitialize(&ctx, 1, columns);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxInitialize", NULL);
return(NULL);
}
@@ -677,22 +611,16 @@ xmlSecBase64Encode(const xmlSecByte *buf, xmlSecSize len, int columns) {
}
ptr = (xmlChar*) xmlMalloc(size);
if(ptr == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_MALLOC_FAILED,
- "size=%d", size);
+ xmlSecMallocError(size, NULL);
xmlSecBase64CtxFinalize(&ctx);
return(NULL);
}
ret = xmlSecBase64CtxUpdate(&ctx, buf, len, (xmlSecByte*)ptr, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxUpdate",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "len=%d", len);
+ xmlSecInternalError3("xmlSecBase64CtxUpdate", NULL,
+ "len=%lu;size=%lu",
+ (unsigned long)len, (unsigned long)size);
xmlFree(ptr);
xmlSecBase64CtxFinalize(&ctx);
return(NULL);
@@ -701,11 +629,7 @@ xmlSecBase64Encode(const xmlSecByte *buf, xmlSecSize len, int columns) {
ret = xmlSecBase64CtxFinal(&ctx, ((xmlSecByte*)ptr) + size_update, size - size_update);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxFinal",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxFinal", NULL);
xmlFree(ptr);
xmlSecBase64CtxFinalize(&ctx);
return(NULL);
@@ -741,21 +665,13 @@ xmlSecBase64Decode(const xmlChar* str, xmlSecByte *buf, xmlSecSize len) {
ret = xmlSecBase64CtxInitialize(&ctx, 0, 0);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxInitialize", NULL);
return(-1);
}
ret = xmlSecBase64CtxUpdate(&ctx, (const xmlSecByte*)str, xmlStrlen(str), buf, len);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxUpdate",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxUpdate", NULL);
xmlSecBase64CtxFinalize(&ctx);
return(-1);
}
@@ -763,11 +679,7 @@ xmlSecBase64Decode(const xmlChar* str, xmlSecByte *buf, xmlSecSize len) {
size_update = ret;
ret = xmlSecBase64CtxFinal(&ctx, buf + size_update, len - size_update);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBase64CtxFinal",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxFinal", NULL);
xmlSecBase64CtxFinalize(&ctx);
return(-1);
}
@@ -873,11 +785,8 @@ xmlSecBase64Initialize(xmlSecTransformPtr transform) {
transform->operation = xmlSecTransformOperationDecode;
ret = xmlSecBase64CtxInitialize(ctx, 0, xmlSecBase64GetDefaultLineSize());
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBase64CtxInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxInitialize",
+ xmlSecTransformGetName(transform));
return(-1);
}
@@ -933,11 +842,9 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
}
ret = xmlSecBufferSetMaxSize(out, outSize + outLen);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", outSize + outLen);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize",
+ xmlSecTransformGetName(transform),
+ "size=%d", outSize + outLen);
return(-1);
}
@@ -946,11 +853,8 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
xmlSecBufferGetData(out) + outSize,
outLen);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBase64CtxUpdate",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxUpdate",
+ xmlSecTransformGetName(transform));
return(-1);
}
outLen = ret;
@@ -958,22 +862,18 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
/* set correct size */
ret = xmlSecBufferSetSize(out, outSize + outLen);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBufferSetSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", outSize + outLen);
+ xmlSecInternalError2("xmlSecBufferSetSize",
+ xmlSecTransformGetName(transform),
+ "size=%d", outSize + outLen);
return(-1);
}
/* remove chunk from input */
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);
}
}
@@ -983,22 +883,17 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
ret = xmlSecBufferSetMaxSize(out, outSize + 16);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBufferSetMaxSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", outSize + 16);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize",
+ xmlSecTransformGetName(transform),
+ "size=%d", outSize + 16);
return(-1);
}
/* add from ctx buffer */
ret = xmlSecBase64CtxFinal(ctx, xmlSecBufferGetData(out) + outSize, 16);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBase64CtxFinal",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBase64CtxFinal",
+ xmlSecTransformGetName(transform));
return(-1);
}
outLen = ret;
@@ -1006,11 +901,9 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
/* set correct size */
ret = xmlSecBufferSetSize(out, outSize + outLen);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- "xmlSecBufferSetSize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", outSize + outLen);
+ xmlSecInternalError2("xmlSecBufferSetSize",
+ xmlSecTransformGetName(transform),
+ "size=%d", outSize + outLen);
return(-1);
}
transform->status = xmlSecTransformStatusFinished;
@@ -1021,11 +914,7 @@ xmlSecBase64Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPt
xmlSecAssert2(xmlSecBufferGetSize(in) == 0, -1);
break;
default:
- xmlSecError(XMLSEC_ERRORS_HERE,
- xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
- NULL,
- XMLSEC_ERRORS_R_INVALID_STATUS,
- "status=%d", transform->status);
+ xmlSecInvalidTransfromStatusError(transform);
return(-1);
}
return(0);