From f5ceb7d11ffd98baa5fe06756370b60f2379195b Mon Sep 17 00:00:00 2001
From: daurnimator <quae@daurnimator.com>
Date: Sun, 3 Jan 2016 08:12:54 +1100
Subject: bignum.new: Allow initialisation from hex strings

---
 src/openssl.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/openssl.c b/src/openssl.c
index 71aaed4..82f3298 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -1673,7 +1673,7 @@ static _Bool f2bn(BIGNUM **bn, double f) {
 
 static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) {
 	BIGNUM **bn;
-	const char *dec;
+	const char *str;
 	size_t len;
 
 	index = lua_absindex(L, index);
@@ -1682,14 +1682,19 @@ static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) {
 	case LUA_TSTRING:
 		*lvalue = 0;
 
-		dec = lua_tolstring(L, index, &len);
+		str = lua_tolstring(L, index, &len);
 
-		luaL_argcheck(L, len > 0 && *dec, index, "invalid big number string");
+		luaL_argcheck(L, len > 0 && *str, index, "invalid big number string");
 
 		bn = prepsimple(L, BIGNUM_CLASS);
 
-		if (!BN_dec2bn(bn, dec))
-			auxL_error(L, auxL_EOPENSSL, "bignum");
+		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
+			if (!BN_hex2bn(bn, str+2))
+				auxL_error(L, auxL_EOPENSSL, "bignum");
+		} else {
+			if (!BN_dec2bn(bn, str))
+				auxL_error(L, auxL_EOPENSSL, "bignum");
+		}
 
 		lua_replace(L, index);
 
-- 
cgit v1.2.3-55-g6feb