1 /**
2  * Copyright (c) 2006-2018 Apple Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16 
17 #include "base64.h"
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 // base64 tables
23 static char basis_64[] =
24     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
25 static signed char index_64[128] =
26 {
27     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
28     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
29     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
30     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
31     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
32     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
33     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
34     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
35 };
36 #define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
37 
38 // base64_encode    :    base64 encode
39 //
40 // value            :    data to encode
41 // vlen             :    length of data
42 // (result)         :    new char[] - c-str of result
base64_encode(const unsigned char * value,size_t vlen)43 char *base64_encode(const unsigned char *value, size_t vlen)
44 {
45     char *result = (char *)malloc((vlen * 4) / 3 + 5);
46     if (result == NULL)
47     {
48         return NULL;
49     }
50     char *out = result;
51     while (vlen >= 3)
52     {
53         *out++ = basis_64[value[0] >> 2];
54         *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
55         *out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
56         *out++ = basis_64[value[2] & 0x3F];
57         value += 3;
58         vlen -= 3;
59     }
60     if (vlen > 0)
61     {
62         *out++ = basis_64[value[0] >> 2];
63         unsigned char oval = (value[0] << 4) & 0x30;
64         if (vlen > 1) oval |= value[1] >> 4;
65         *out++ = basis_64[oval];
66         *out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
67         *out++ = '=';
68     }
69     *out = '\0';
70 
71     return result;
72 }
73 
74 // base64_decode    :    base64 decode
75 //
76 // value            :    c-str to decode
77 // rlen             :    length of decoded result
78 // (result)         :    new unsigned char[] - decoded result
base64_decode(const char * value,size_t * rlen)79 unsigned char *base64_decode(const char *value, size_t *rlen)
80 {
81     *rlen = 0;
82     int c1, c2, c3, c4;
83 
84     size_t vlen = strlen(value);
85     unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
86     if (result == NULL)
87     {
88         return NULL;
89     }
90     unsigned char *out = result;
91 
92     while (1) {
93         if (value[0]==0) {
94             return result;
95         }
96         c1 = value[0];
97         if (CHAR64(c1) == -1) {
98             goto base64_decode_error;;
99         }
100         c2 = value[1];
101         if (CHAR64(c2) == -1) {
102             goto base64_decode_error;;
103         }
104         c3 = value[2];
105         if ((c3 != '=') && (CHAR64(c3) == -1)) {
106             goto base64_decode_error;;
107         }
108         c4 = value[3];
109         if ((c4 != '=') && (CHAR64(c4) == -1)) {
110             goto base64_decode_error;;
111         }
112 
113         value += 4;
114         *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
115         *rlen += 1;
116 
117         if (c3 != '=') {
118             *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
119             *rlen += 1;
120 
121             if (c4 != '=') {
122                 *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
123                 *rlen += 1;
124             }
125         }
126     }
127 
128 base64_decode_error:
129     *result = 0;
130     *rlen = 0;
131 
132     return result;
133 }
134