1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 Compliant base64 encoder donated by Wayne Scott (wscott@bitmover.com)
14 base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
18 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
20 #if defined(LTC_BASE64)
21 static const char * const codes_base64 =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 #endif /* LTC_BASE64 */
25 #if defined(LTC_BASE64_URL)
26 static const char * const codes_base64url =
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
28 #endif /* LTC_BASE64_URL */
30 static int _base64_encode_internal(const unsigned char *in, unsigned long inlen,
31 unsigned char *out, unsigned long *outlen,
32 const char *codes, int pad)
34 unsigned long i, len2, leven;
37 LTC_ARGCHK(in != NULL);
38 LTC_ARGCHK(out != NULL);
39 LTC_ARGCHK(outlen != NULL);
41 /* valid output size ? */
42 len2 = 4 * ((inlen + 2) / 3);
43 if (*outlen < len2 + 1) {
45 return CRYPT_BUFFER_OVERFLOW;
48 leven = 3*(inlen / 3);
49 for (i = 0; i < leven; i += 3) {
50 *p++ = codes[(in[0] >> 2) & 0x3F];
51 *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
52 *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
53 *p++ = codes[in[2] & 0x3F];
56 /* Pad it if necessary... */
59 unsigned b = (i+1 < inlen) ? in[1] : 0;
61 *p++ = codes[(a >> 2) & 0x3F];
62 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
64 *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
68 if (i+1 < inlen) *p++ = codes[(((b & 0xf) << 2)) & 0x3F];
72 /* append a NULL byte */
76 *outlen = (unsigned long)(p - out);
80 #if defined(LTC_BASE64)
82 base64 Encode a buffer (NUL terminated)
83 @param in The input buffer to encode
84 @param inlen The length of the input buffer
85 @param out [out] The destination of the base64 encoded data
86 @param outlen [in/out] The max size and resulting size
87 @return CRYPT_OK if successful
89 int base64_encode(const unsigned char *in, unsigned long inlen,
90 unsigned char *out, unsigned long *outlen)
92 return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
94 #endif /* LTC_BASE64 */
97 #if defined(LTC_BASE64_URL)
99 base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated)
100 @param in The input buffer to encode
101 @param inlen The length of the input buffer
102 @param out [out] The destination of the base64 encoded data
103 @param outlen [in/out] The max size and resulting size
104 @return CRYPT_OK if successful
106 int base64url_encode(const unsigned char *in, unsigned long inlen,
107 unsigned char *out, unsigned long *outlen)
109 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
112 int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
113 unsigned char *out, unsigned long *outlen)
115 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);
117 #endif /* LTC_BASE64_URL */
122 /* ref: $Format:%D$ */
123 /* git commit: $Format:%H$ */
124 /* commit time: $Format:%ai$ */