]> pd.if.org Git - zpackage/blob - libtomcrypt/src/misc/base64/base64_encode.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / misc / base64 / base64_encode.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file base64_encode.c
13   Compliant base64 encoder donated by Wayne Scott (wscott@bitmover.com)
14   base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
15 */
16
17
18 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
19
20 #if defined(LTC_BASE64)
21 static const char * const codes_base64 =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 #endif /* LTC_BASE64 */
24
25 #if defined(LTC_BASE64_URL)
26 static const char * const codes_base64url =
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
28 #endif /* LTC_BASE64_URL */
29
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)
33 {
34    unsigned long i, len2, leven;
35    unsigned char *p;
36
37    LTC_ARGCHK(in     != NULL);
38    LTC_ARGCHK(out    != NULL);
39    LTC_ARGCHK(outlen != NULL);
40
41    /* valid output size ? */
42    len2 = 4 * ((inlen + 2) / 3);
43    if (*outlen < len2 + 1) {
44       *outlen = len2 + 1;
45       return CRYPT_BUFFER_OVERFLOW;
46    }
47    p = out;
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];
54        in += 3;
55    }
56    /* Pad it if necessary...  */
57    if (i < inlen) {
58        unsigned a = in[0];
59        unsigned b = (i+1 < inlen) ? in[1] : 0;
60
61        *p++ = codes[(a >> 2) & 0x3F];
62        *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
63        if (pad) {
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        }
70    }
71
72    /* append a NULL byte */
73    *p = '\0';
74
75    /* return ok */
76    *outlen = (unsigned long)(p - out);
77    return CRYPT_OK;
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 */
118
119 #endif
120
121
122 /* ref:         $Format:%D$ */
123 /* git commit:  $Format:%H$ */
124 /* commit time: $Format:%ai$ */