re-work strict/relaxed base64 decoding implementation

Instead of one API function with an option parameter, provide two API
functions.
Instead of defaulting to strict decoding, default to relaxed decoding.
This commit is contained in:
Steffen Jaeckel
2017-02-17 11:18:58 +01:00
committed by Karel Miko
parent 53359ccfc6
commit c1dd1cbe30
7 changed files with 51 additions and 35 deletions
+34 -11
View File
@@ -71,9 +71,14 @@ static const unsigned char map_base64url[256] = {
255, 255, 255, 255 };
#endif /* LTC_BASE64_URL */
enum {
relaxed = 0,
strict = 1
};
static int _base64_decode_internal(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
const unsigned char *map, int strict)
const unsigned char *map, int is_strict)
{
unsigned long t, x, y, z;
unsigned char c;
@@ -87,7 +92,7 @@ static int _base64_decode_internal(const unsigned char *in, unsigned long inlen
for (x = y = z = t = 0; x < inlen; x++) {
c = map[in[x]&0xFF];
if (c == 255) {
if (strict)
if (is_strict)
return CRYPT_INVALID_PACKET;
else
continue;
@@ -117,7 +122,7 @@ static int _base64_decode_internal(const unsigned char *in, unsigned long inlen
}
}
if (y != 0) {
if (y == 1 || map != map_base64url || strict == 1) return CRYPT_INVALID_PACKET;
if (y == 1 || map != map_base64url || is_strict == 1) return CRYPT_INVALID_PACKET;
t = t << (6 * (4 - y));
if (z + y - 1 > *outlen) return CRYPT_BUFFER_OVERFLOW;
if (y >= 2) out[z++] = (unsigned char) ((t >> 16) & 255);
@@ -129,18 +134,31 @@ static int _base64_decode_internal(const unsigned char *in, unsigned long inlen
#if defined(LTC_BASE64)
/**
base64 decode a block of memory
Relaxed base64 decode a block of memory
@param in The base64 data to decode
@param inlen The length of the base64 data
@param out [out] The destination of the binary decoded data
@param outlen [in/out] The max size and resulting size of the decoded data
@param strict Strict[1] or relaxed[0] decoding of the input
@return CRYPT_OK if successful
*/
int base64_decode_ex(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int strict)
int base64_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen)
{
return _base64_decode_internal(in, inlen, out, outlen, map_base64, strict);
return _base64_decode_internal(in, inlen, out, outlen, map_base64, relaxed);
}
/**
Strict base64 decode a block of memory
@param in The base64 data to decode
@param inlen The length of the base64 data
@param out [out] The destination of the binary decoded data
@param outlen [in/out] The max size and resulting size of the decoded data
@return CRYPT_OK if successful
*/
int base64_strict_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen)
{
return _base64_decode_internal(in, inlen, out, outlen, map_base64, strict);
}
#endif /* LTC_BASE64 */
@@ -151,11 +169,16 @@ int base64_decode_ex(const unsigned char *in, unsigned long inlen,
@param inlen The length of the base64 data
@param out [out] The destination of the binary decoded data
@param outlen [in/out] The max size and resulting size of the decoded data
@param strict Strict[1] or relaxed[0] decoding of the input
@return CRYPT_OK if successful
*/
int base64url_decode_ex(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int strict)
int base64url_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen)
{
return _base64_decode_internal(in, inlen, out, outlen, map_base64url, relaxed);
}
int base64url_strict_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen)
{
return _base64_decode_internal(in, inlen, out, outlen, map_base64url, strict);
}
-3
View File
@@ -337,9 +337,6 @@ const char *crypt_build_settings =
#if defined(LTC_BASE64_URL)
" BASE64-URL-SAFE "
#endif
#if defined(LTC_BASE64) || defined(LTC_BASE64_URL)
" "NAME_VALUE(LTC_BASE64_STRICT)" "
#endif
#if defined(LTC_CRC32)
" CRC32 "
#endif
-4
View File
@@ -89,10 +89,6 @@ static const crypt_constant _crypt_constants[] = {
{"LTC_CTR_MODE", 0},
#endif
#if defined(LTC_BASE64) || defined(LTC_BASE64_URL)
_C_STRINGIFY(LTC_BASE64_STRICT),
#endif
_C_STRINGIFY(MAXBLOCKSIZE),
_C_STRINGIFY(TAB_SIZE),
_C_STRINGIFY(ARGTYPE),