summaryrefslogtreecommitdiff
path: root/src/bn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bn.c')
-rw-r--r--src/bn.c230
1 files changed, 57 insertions, 173 deletions
diff --git a/src/bn.c b/src/bn.c
index 48fb990a..6085eb27 100644
--- a/src/bn.c
+++ b/src/bn.c
@@ -1,7 +1,6 @@
-/**
+/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
- * Big Numbers.
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
@@ -9,6 +8,13 @@
* Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
* Copyright (C) 2003 Cordys R&D BV, All rights reserved.
*/
+/**
+ * SECTION:bn
+ * @Short_description: Big numbers support functions.
+ * @Stability: Stable
+ *
+ */
+
#include "globals.h"
#include <stdlib.h>
@@ -197,23 +203,19 @@ xmlSecBnFromString(xmlSecBnPtr bn, const xmlChar* str, xmlSecSize base) {
*/
ret = xmlSecBufferSetMaxSize(bn, xmlSecBufferGetSize(bn) + len / 2 + 1 + 1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnRevLookupTable",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", len / 2 + 1);
+ xmlSecInternalError2("xmlSecBufferSetMaxSize", NULL, "size=%d", len / 2 + 1);
return (-1);
}
/* figure out if it is positive or negative number */
- positive = 1;
+ positive = 1; /* no sign, positive by default */
i = 0;
while(i < len) {
ch = str[i++];
/* skip spaces */
if(isspace(ch)) {
- continue;
+ continue;
}
/* check if it is + or - */
@@ -225,64 +227,37 @@ xmlSecBnFromString(xmlSecBnPtr bn, const xmlChar* str, xmlSecSize base) {
break;
}
- /* otherwise, it must be start of the number */
- nn = xmlSecBnLookupTable[ch];
- if((nn >= 0) && ((xmlSecSize)nn < base)) {
- xmlSecAssert2(i > 0, -1);
-
- /* no sign, positive by default */
- positive = 1;
- --i; /* make sure that we will look at this character in next loop */
- break;
- } else {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "char=%c;base=%d",
- ch, base);
- return (-1);
- }
+ /* otherwise, it must be start of the number, make sure that we will look
+ * at this character in next loop */
+ xmlSecAssert2(i > 0, -1);
+ --i;
+ break;
}
/* now parse the number itself */
while(i < len) {
ch = str[i++];
if(isspace(ch)) {
- continue;
+ continue;
}
- xmlSecAssert2(ch < sizeof(xmlSecBnLookupTable) / sizeof(xmlSecBnLookupTable[0]), -1);
nn = xmlSecBnLookupTable[ch];
- if((nn < 0) || ((xmlSecSize)nn > base)) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_INVALID_DATA,
- "char=%c;base=%d",
- ch, base);
- return (-1);
+ if((nn < 0) || ((xmlSecSize)nn >= base)) {
+ xmlSecInvalidIntegerDataError2("char", nn, "base", base, "0 <= char < base", NULL);
+ return (-1);
}
ret = xmlSecBnMul(bn, base);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnMul",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "base=%d", base);
- return (-1);
+ xmlSecInternalError2("xmlSecBnMul", NULL, "base=%d", base);
+ return (-1);
}
ret = xmlSecBnAdd(bn, nn);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnAdd",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "base=%d", base);
- return (-1);
-}
+ xmlSecInternalError2("xmlSecBnAdd", NULL, "base=%d", base);
+ return (-1);
+ }
}
/* check if we need to add 00 prefix, do this for empty bn too */
@@ -292,11 +267,7 @@ xmlSecBnFromString(xmlSecBnPtr bn, const xmlChar* str, xmlSecSize base) {
ch = 0;
ret = xmlSecBufferPrepend(bn, &ch, 1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferPrepend",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "base=%d", base);
+ xmlSecInternalError2("xmlSecBufferPrepend", NULL, "base=%d", base);
return (-1);
}
}
@@ -311,11 +282,7 @@ xmlSecBnFromString(xmlSecBnPtr bn, const xmlChar* str, xmlSecSize base) {
ret = xmlSecBnAdd(bn, 1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnAdd",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "base=%d", base);
+ xmlSecInternalError2("xmlSecBnAdd", NULL, "base=%d", base);
return (-1);
}
}
@@ -354,21 +321,13 @@ xmlSecBnToString(xmlSecBnPtr bn, xmlSecSize base) {
size = xmlSecBufferGetSize(bn);
ret = xmlSecBnInitialize(&bn2, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnCreate",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBnInitialize", NULL, "size=%d", size);
return (NULL);
}
ret = xmlSecBnSetData(&bn2, data, size);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnSetData",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBnSetData", NULL, "size=%d", size);
xmlSecBnFinalize(&bn2);
return (NULL);
}
@@ -380,11 +339,7 @@ xmlSecBnToString(xmlSecBnPtr bn, xmlSecSize base) {
/* subtract 1 and do 2's compliment */
ret = xmlSecBnAdd(&bn2, -1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnAdd",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", size);
+ xmlSecInternalError2("xmlSecBnAdd", NULL, "size=%d", size);
xmlSecBnFinalize(&bn2);
return (NULL);
}
@@ -405,11 +360,7 @@ xmlSecBnToString(xmlSecBnPtr bn, xmlSecSize base) {
len = 8 * size + 1 + 1;
res = (xmlChar*)xmlMalloc(len + 1);
if(res == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- NULL,
- XMLSEC_ERRORS_R_MALLOC_FAILED,
- "len=%d", len);
+ xmlSecMallocError(len + 1, NULL);
xmlSecBnFinalize(&bn2);
return (NULL);
}
@@ -417,11 +368,7 @@ xmlSecBnToString(xmlSecBnPtr bn, xmlSecSize base) {
for(i = 0; (xmlSecBufferGetSize(&bn2) > 0) && (i < len); i++) {
if(xmlSecBnDiv(&bn2, base, &nn) < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnDiv",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "base=%d", base);
+ xmlSecInternalError2("xmlSecBnDiv", NULL, "base=%d", base);
xmlFree(res);
xmlSecBnFinalize(&bn2);
return (NULL);
@@ -432,7 +379,8 @@ xmlSecBnToString(xmlSecBnPtr bn, xmlSecSize base) {
xmlSecAssert2(i < len, NULL);
/* we might have '0' at the beggining, remove it but keep one zero */
- for(len = i; (len > 1) && (res[len - 1] == '0'); len--);
+ for(len = i; (len > 1) && (res[len - 1] == '0'); len--) {
+ }
res[len] = '\0';
/* add "-" for negative numbers */
@@ -549,11 +497,7 @@ xmlSecBnMul(xmlSecBnPtr bn, int multiplier) {
ret = xmlSecBufferPrepend(bn, &ch, 1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferPrepend",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=1");
+ xmlSecInternalError2("xmlSecBufferPrepend", NULL, "size=%d", 1);
return (-1);
}
}
@@ -592,7 +536,7 @@ xmlSecBnDiv(xmlSecBnPtr bn, int divider, int* mod) {
xmlSecAssert2(data != NULL, -1);
over = over * 256 + data[i];
- data[i] = over / divider;
+ data[i] = (xmlSecByte)(over / divider);
over = over % divider;
}
(*mod) = over;
@@ -608,11 +552,7 @@ xmlSecBnDiv(xmlSecBnPtr bn, int divider, int* mod) {
if(i > 0) {
ret = xmlSecBufferRemoveHead(bn, i);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferRemoveHead",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=%d", i);
+ xmlSecInternalError2("xmlSecBufferRemoveHead", NULL, "size=%d", i);
return (-1);
}
}
@@ -659,11 +599,7 @@ xmlSecBnAdd(xmlSecBnPtr bn, int delta) {
ret = xmlSecBufferPrepend(bn, &ch, 1);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferPrepend",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- "size=1");
+ xmlSecInternalError2("xmlSecBufferPrepend", NULL, "size=%d", 1);
return (-1);
}
}
@@ -676,7 +612,7 @@ xmlSecBnAdd(xmlSecBnPtr bn, int delta) {
data[i] = 0;
over = (over - tmp) / 256;
} else {
- data[i] = tmp - over;
+ data[i] = (xmlSecByte)(tmp - over);
over = 0;
}
}
@@ -823,7 +759,7 @@ xmlSecBnCompareReverse(xmlSecBnPtr bn, const xmlSecByte* data, xmlSecSize dataSi
/**
* xmlSecBnGetNodeValue:
* @bn: the pointer to BN.
- * @cur: the poitner to an XML node.
+ * @cur: the pointer to an XML node.
* @format: the BN format.
* @reverse: if set then reverse read buffer after reading.
*
@@ -843,31 +779,19 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
case xmlSecBnBase64:
ret = xmlSecBufferBase64NodeContentRead(bn, cur);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferBase64NodeContentRead",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBufferBase64NodeContentRead", NULL);
return(-1);
}
break;
case xmlSecBnHex:
content = xmlNodeGetContent(cur);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlNodeGetContent",
- XMLSEC_ERRORS_R_XML_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecXmlError("xmlNodeGetContent", NULL);
return(-1);
}
ret = xmlSecBnFromHexString(bn, content);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnFromHexString",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnFromHexString", NULL);
xmlFree(content);
return(-1);
}
@@ -876,20 +800,12 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
case xmlSecBnDec:
content = xmlNodeGetContent(cur);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlNodeGetContent",
- XMLSEC_ERRORS_R_XML_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecXmlError("xmlNodeGetContent", NULL);
return(-1);
}
ret = xmlSecBnFromDecString(bn, content);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnFromDecString",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnFromDecString", NULL);
xmlFree(content);
return(-1);
}
@@ -900,11 +816,7 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
if(reverse != 0) {
ret = xmlSecBnReverse(bn);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnReverse",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnReverse", NULL);
return(-1);
}
}
@@ -914,7 +826,7 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
/**
* xmlSecBnSetNodeValue:
* @bn: the pointer to BN.
- * @cur: the poitner to an XML node.
+ * @cur: the pointer to an XML node.
* @format: the BN format.
* @reverse: the flag that indicates whether to reverse the buffer before writing.
* @addLineBreaks: the flag; it is equal to 1 then linebreaks will be added before and after new buffer content.
@@ -934,39 +846,27 @@ xmlSecBnSetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
if(reverse != 0) {
ret = xmlSecBnReverse(bn);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnReverse",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnReverse", NULL);
return(-1);
}
}
if(addLineBreaks) {
- xmlNodeAddContent(cur, xmlSecStringCR);
+ xmlNodeAddContent(cur, xmlSecGetDefaultLineFeed());
}
switch(format) {
case xmlSecBnBase64:
ret = xmlSecBufferBase64NodeContentWrite(bn, cur, xmlSecBase64GetDefaultLineSize());
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBufferBase64NodeContentWrite",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBufferBase64NodeContentWrite", NULL);
return(-1);
}
break;
case xmlSecBnHex:
content = xmlSecBnToHexString(bn);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnToHexString",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnToHexString", NULL);
xmlFree(content);
return(-1);
}
@@ -976,11 +876,7 @@ xmlSecBnSetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
case xmlSecBnDec:
content = xmlSecBnToDecString(bn);
if(content == NULL) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnToDecString",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnToDecString", NULL);
xmlFree(content);
return(-1);
}
@@ -990,7 +886,7 @@ xmlSecBnSetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
}
if(addLineBreaks) {
- xmlNodeAddContent(cur, xmlSecStringCR);
+ xmlNodeAddContent(cur, xmlSecGetDefaultLineFeed());
}
return(0);
@@ -1000,7 +896,7 @@ xmlSecBnSetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
* xmlSecBnBlobSetNodeValue:
* @data: the pointer to BN blob.
* @dataSize: the size of BN blob.
- * @cur: the poitner to an XML node.
+ * @cur: the pointer to an XML node.
* @format: the BN format.
* @reverse: the flag that indicates whether to reverse the buffer before writing.
* @addLineBreaks: if the flag is equal to 1 then
@@ -1023,32 +919,20 @@ xmlSecBnBlobSetNodeValue(const xmlSecByte* data, xmlSecSize dataSize,
ret = xmlSecBnInitialize(&bn, dataSize);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnInitialize",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnInitialize", NULL);
return(-1);
}
ret = xmlSecBnSetData(&bn, data, dataSize);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnSetData",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnSetData", NULL);
xmlSecBnFinalize(&bn);
return(-1);
}
ret = xmlSecBnSetNodeValue(&bn, cur, format, reverse, addLineBreaks);
if(ret < 0) {
- xmlSecError(XMLSEC_ERRORS_HERE,
- NULL,
- "xmlSecBnSetNodeValue",
- XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecInternalError("xmlSecBnSetNodeValue", NULL);
xmlSecBnFinalize(&bn);
return(-1);
}