Path: blob/master/libs/tomcrypt/src/misc/base64/base64_encode.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file base64_encode.c12Compliant base64 encoder donated by Wayne Scott ([email protected])13base64 URL Safe variant (RFC 4648 section 5) by Karel Miko14*/151617#if defined(LTC_BASE64) || defined (LTC_BASE64_URL)1819#if defined(LTC_BASE64)20static const char * const codes_base64 =21"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";22#endif /* LTC_BASE64 */2324#if defined(LTC_BASE64_URL)25static const char * const codes_base64url =26"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";27#endif /* LTC_BASE64_URL */2829static int _base64_encode_internal(const unsigned char *in, unsigned long inlen,30unsigned char *out, unsigned long *outlen,31const char *codes, int pad)32{33unsigned long i, len2, leven;34unsigned char *p;3536LTC_ARGCHK(in != NULL);37LTC_ARGCHK(out != NULL);38LTC_ARGCHK(outlen != NULL);3940/* valid output size ? */41len2 = 4 * ((inlen + 2) / 3);42if (*outlen < len2 + 1) {43*outlen = len2 + 1;44return CRYPT_BUFFER_OVERFLOW;45}46p = out;47leven = 3*(inlen / 3);48for (i = 0; i < leven; i += 3) {49*p++ = codes[(in[0] >> 2) & 0x3F];50*p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];51*p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];52*p++ = codes[in[2] & 0x3F];53in += 3;54}55/* Pad it if necessary... */56if (i < inlen) {57unsigned a = in[0];58unsigned b = (i+1 < inlen) ? in[1] : 0;5960*p++ = codes[(a >> 2) & 0x3F];61*p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];62if (pad) {63*p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';64*p++ = '=';65}66else {67if (i+1 < inlen) *p++ = codes[(((b & 0xf) << 2)) & 0x3F];68}69}7071/* append a NULL byte */72*p = '\0';7374/* return ok */75*outlen = (unsigned long)(p - out);76return CRYPT_OK;77}7879#if defined(LTC_BASE64)80/**81base64 Encode a buffer (NUL terminated)82@param in The input buffer to encode83@param inlen The length of the input buffer84@param out [out] The destination of the base64 encoded data85@param outlen [in/out] The max size and resulting size86@return CRYPT_OK if successful87*/88int base64_encode(const unsigned char *in, unsigned long inlen,89unsigned char *out, unsigned long *outlen)90{91return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);92}93#endif /* LTC_BASE64 */949596#if defined(LTC_BASE64_URL)97/**98base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated)99@param in The input buffer to encode100@param inlen The length of the input buffer101@param out [out] The destination of the base64 encoded data102@param outlen [in/out] The max size and resulting size103@return CRYPT_OK if successful104*/105int base64url_encode(const unsigned char *in, unsigned long inlen,106unsigned char *out, unsigned long *outlen)107{108return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);109}110111int base64url_strict_encode(const unsigned char *in, unsigned long inlen,112unsigned char *out, unsigned long *outlen)113{114return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);115}116#endif /* LTC_BASE64_URL */117118#endif119120121