base64: add define LTC_BASE64_URL, make _internal functions static

This commit is contained in:
Steffen Jaeckel 2013-11-24 22:11:44 +01:00
parent 947fe41bbb
commit d78aa37c10
4 changed files with 32 additions and 12 deletions

View File

@ -383,6 +383,8 @@
/* Various tidbits of modern neatoness */ /* Various tidbits of modern neatoness */
#define LTC_BASE64 #define LTC_BASE64
/* ... and it's URL safe version */
#define LTC_BASE64_URL
/* Keep LTC_NO_HKDF for compatibility reasons /* Keep LTC_NO_HKDF for compatibility reasons
* superseeded by LTC_NO_MISC*/ * superseeded by LTC_NO_MISC*/

View File

@ -7,11 +7,13 @@ int base64_decode(const unsigned char *in, unsigned long len,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
#endif #endif
#ifdef LTC_BASE64_URL
int base64url_encode(const unsigned char *in, unsigned long len, int base64url_encode(const unsigned char *in, unsigned long len,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
int base64url_decode(const unsigned char *in, unsigned long len, int base64url_decode(const unsigned char *in, unsigned long len,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
#endif
/* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */ /* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */
#ifdef LTC_HKDF #ifdef LTC_HKDF

View File

@ -17,8 +17,9 @@
*/ */
#ifdef LTC_BASE64 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
#if defined(LTC_BASE64)
static const unsigned char map_base64[256] = { static const unsigned char map_base64[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
@ -42,7 +43,9 @@ static const unsigned char map_base64[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255 }; 255, 255, 255, 255 };
#endif /* LTC_BASE64 */
#if defined(LTC_BASE64_URL)
static const unsigned char map_base64url[256] = { static const unsigned char map_base64url[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
@ -66,8 +69,9 @@ static const unsigned char map_base64url[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255 }; 255, 255, 255, 255 };
#endif /* LTC_BASE64_URL */
int base64_decode_internal(const unsigned char *in, unsigned long inlen, static int _base64_decode_internal(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
const unsigned char *map) const unsigned char *map)
{ {
@ -114,6 +118,7 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen,
return CRYPT_OK; return CRYPT_OK;
} }
#if defined(LTC_BASE64)
/** /**
base64 decode a block of memory base64 decode a block of memory
@param in The base64 data to decode @param in The base64 data to decode
@ -125,9 +130,11 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen,
int base64_decode(const unsigned char *in, unsigned long inlen, int base64_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
return base64_decode_internal(in, inlen, out, outlen, map_base64); return _base64_decode_internal(in, inlen, out, outlen, map_base64);
} }
#endif /* LTC_BASE64 */
#if defined(LTC_BASE64_URL)
/** /**
base64 (URL Safe, RFC 4648 section 5) decode a block of memory base64 (URL Safe, RFC 4648 section 5) decode a block of memory
@param in The base64 data to decode @param in The base64 data to decode
@ -139,8 +146,9 @@ int base64_decode(const unsigned char *in, unsigned long inlen,
int base64url_decode(const unsigned char *in, unsigned long inlen, int base64url_decode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
return base64_decode_internal(in, inlen, out, outlen, map_base64url); return _base64_decode_internal(in, inlen, out, outlen, map_base64url);
} }
#endif /* LTC_BASE64_URL */
#endif #endif

View File

@ -17,15 +17,19 @@
*/ */
#ifdef LTC_BASE64 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
#if defined(LTC_BASE64)
static const char *codes_base64 = static const char *codes_base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#endif /* LTC_BASE64 */
#if defined(LTC_BASE64_URL)
static const char *codes_base64url = static const char *codes_base64url =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
#endif /* LTC_BASE64_URL */
int base64_encode_internal(const unsigned char *in, unsigned long inlen, static int _base64_encode_internal(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
const char *codes, int pad) const char *codes, int pad)
{ {
@ -75,6 +79,7 @@ int base64_encode_internal(const unsigned char *in, unsigned long inlen,
return CRYPT_OK; return CRYPT_OK;
} }
#if defined(LTC_BASE64)
/** /**
base64 Encode a buffer (NUL terminated) base64 Encode a buffer (NUL terminated)
@param in The input buffer to encode @param in The input buffer to encode
@ -86,10 +91,12 @@ int base64_encode_internal(const unsigned char *in, unsigned long inlen,
int base64_encode(const unsigned char *in, unsigned long inlen, int base64_encode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
return base64_encode_internal(in, inlen, out, outlen, codes_base64, 1); return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
} }
#endif /* LTC_BASE64 */
#if defined(LTC_BASE64_URL)
/** /**
base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated) base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated)
@param in The input buffer to encode @param in The input buffer to encode
@ -101,8 +108,9 @@ int base64_encode(const unsigned char *in, unsigned long inlen,
int base64url_encode(const unsigned char *in, unsigned long inlen, int base64url_encode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
return base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0); return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
} }
#endif /* LTC_BASE64_URL */
#endif #endif