comparison libtomcrypt/src/misc/base64/base64_encode.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1457:32f990cc96b1 1511:5916af64acd4
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file base64_encode.c 12 @file base64_encode.c
15 Compliant base64 encoder donated by Wayne Scott ([email protected]) 13 Compliant base64 encoder donated by Wayne Scott ([email protected])
14 base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
16 */ 15 */
17 16
18 17
19 #ifdef LTC_BASE64 18 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
20 19
21 static const char *codes = 20 #if defined(LTC_BASE64)
21 static const char * const codes_base64 =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 #endif /* LTC_BASE64 */
23 24
24 /** 25 #if defined(LTC_BASE64_URL)
25 base64 Encode a buffer (NUL terminated) 26 static const char * const codes_base64url =
26 @param in The input buffer to encode 27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
27 @param inlen The length of the input buffer 28 #endif /* LTC_BASE64_URL */
28 @param out [out] The destination of the base64 encoded data 29
29 @param outlen [in/out] The max size and resulting size 30 static int _base64_encode_internal(const unsigned char *in, unsigned long inlen,
30 @return CRYPT_OK if successful 31 unsigned char *out, unsigned long *outlen,
31 */ 32 const char *codes, int pad)
32 int base64_encode(const unsigned char *in, unsigned long inlen,
33 unsigned char *out, unsigned long *outlen)
34 { 33 {
35 unsigned long i, len2, leven; 34 unsigned long i, len2, leven;
36 unsigned char *p; 35 unsigned char *p;
37 36
38 LTC_ARGCHK(in != NULL); 37 LTC_ARGCHK(in != NULL);
59 unsigned a = in[0]; 58 unsigned a = in[0];
60 unsigned b = (i+1 < inlen) ? in[1] : 0; 59 unsigned b = (i+1 < inlen) ? in[1] : 0;
61 60
62 *p++ = codes[(a >> 2) & 0x3F]; 61 *p++ = codes[(a >> 2) & 0x3F];
63 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F]; 62 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
64 *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '='; 63 if (pad) {
65 *p++ = '='; 64 *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
65 *p++ = '=';
66 }
67 else {
68 if (i+1 < inlen) *p++ = codes[(((b & 0xf) << 2)) & 0x3F];
69 }
66 } 70 }
67 71
68 /* append a NULL byte */ 72 /* append a NULL byte */
69 *p = '\0'; 73 *p = '\0';
70 74
71 /* return ok */ 75 /* return ok */
72 *outlen = p - out; 76 *outlen = (unsigned long)(p - out);
73 return CRYPT_OK; 77 return CRYPT_OK;
74 } 78 }
79
80 #if defined(LTC_BASE64)
81 /**
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
88 */
89 int base64_encode(const unsigned char *in, unsigned long inlen,
90 unsigned char *out, unsigned long *outlen)
91 {
92 return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
93 }
94 #endif /* LTC_BASE64 */
95
96
97 #if defined(LTC_BASE64_URL)
98 /**
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
105 */
106 int base64url_encode(const unsigned char *in, unsigned long inlen,
107 unsigned char *out, unsigned long *outlen)
108 {
109 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
110 }
111
112 int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
113 unsigned char *out, unsigned long *outlen)
114 {
115 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);
116 }
117 #endif /* LTC_BASE64_URL */
75 118
76 #endif 119 #endif
77 120
78 121
79 /* $Source$ */ 122 /* ref: $Format:%D$ */
80 /* $Revision$ */ 123 /* git commit: $Format:%H$ */
81 /* $Date$ */ 124 /* commit time: $Format:%ai$ */