xref: /aosp_15_r20/external/cronet/third_party/modp_b64/modp_b64.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 /**
4  * \file
5  * <PRE>
6  * MODP_B64 - High performance base64 encoder/decoder
7  * Version 1.3 -- 17-Mar-2006
8  * http://modp.com/release/base64
9  *
10  * Copyright &copy; 2005, 2006  Nick Galbreath -- nickg [at] modp [dot] com
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are
15  * met:
16  *
17  *   Redistributions of source code must retain the above copyright
18  *   notice, this list of conditions and the following disclaimer.
19  *
20  *   Redistributions in binary form must reproduce the above copyright
21  *   notice, this list of conditions and the following disclaimer in the
22  *   documentation and/or other materials provided with the distribution.
23  *
24  *   Neither the name of the modp.com nor the names of its
25  *   contributors may be used to endorse or promote products derived from
26  *   this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * This is the standard "new" BSD license:
41  * http://www.opensource.org/licenses/bsd-license.php
42  * </PRE>
43  */
44 
45 /* public header */
46 #include "modp_b64.h"
47 
48 #include "modp_b64_data.h"
49 
50 #define BADCHAR 0x01FFFFFF
51 
modp_b64_encode_data(char * dest,const char * str,size_t len)52 size_t modp_b64_encode_data(char* dest, const char* str, size_t len)
53 {
54     size_t i = 0;
55     uint8_t* p = (uint8_t*) dest;
56 
57     /* unsigned here is important! */
58     uint8_t t1, t2, t3;
59 
60     if (len > 2) {
61         for (; i < len - 2; i += 3) {
62             t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
63             *p++ = e0[t1];
64             *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
65             *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
66             *p++ = e2[t3];
67         }
68     }
69 
70     switch (len - i) {
71     case 0:
72         break;
73     case 1:
74         t1 = str[i];
75         *p++ = e0[t1];
76         *p++ = e1[(t1 & 0x03) << 4];
77         *p++ = CHARPAD;
78         *p++ = CHARPAD;
79         break;
80     default: /* case 2 */
81         t1 = str[i]; t2 = str[i+1];
82         *p++ = e0[t1];
83         *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
84         *p++ = e2[(t2 & 0x0F) << 2];
85         *p++ = CHARPAD;
86     }
87 
88     return p - (uint8_t*)dest;
89 }
90 
modp_b64_encode(char * dest,const char * str,size_t len)91 size_t modp_b64_encode(char* dest, const char* str, size_t len) {
92   size_t output_size = modp_b64_encode_data(dest, str, len);
93   dest[output_size] = '\0';
94   return output_size;
95 }
96 
do_decode_padding(const char * src,size_t len,ModpDecodePolicy policy)97 size_t do_decode_padding(const char* src, size_t len, ModpDecodePolicy policy) {
98   if (policy == ModpDecodePolicy::kNoPaddingValidation) {
99     while (len > 0 && src[len - 1] == CHARPAD) {
100         len--;
101     }
102   } else {
103     const size_t remainder = len % 4;
104     if (policy == ModpDecodePolicy::kStrict && (remainder != 0 || len < 4))
105       return MODP_B64_ERROR;
106     if (remainder == 0) {
107       if (src[len - 1] == CHARPAD) {
108         len--;
109         if (src[len - 1] == CHARPAD) {
110           len--;
111         }
112       }
113     }
114   }
115   return len % 4 == 1 ? MODP_B64_ERROR : len;
116 }
117 
modp_b64_decode(char * dest,const char * src,size_t len,ModpDecodePolicy policy)118 size_t modp_b64_decode(char* dest, const char* src, size_t len, ModpDecodePolicy policy)
119 {
120     if (len != 0)
121       len = do_decode_padding(src, len, policy);
122 
123     if (len == 0 || len == MODP_B64_ERROR)
124       return len;
125 
126     size_t i;
127     int leftover = len % 4;
128     size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
129 
130     uint8_t* p = (uint8_t*)dest;
131     uint32_t x = 0;
132     const uint8_t* y = (uint8_t*)src;
133     for (i = 0; i < chunks; ++i, y += 4) {
134         x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
135         if (x >= BADCHAR) return MODP_B64_ERROR;
136         *p++ =  ((uint8_t*)(&x))[0];
137         *p++ =  ((uint8_t*)(&x))[1];
138         *p++ =  ((uint8_t*)(&x))[2];
139     }
140 
141     switch (leftover) {
142     case 0:
143         x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
144 
145         if (x >= BADCHAR) return MODP_B64_ERROR;
146         *p++ =  ((uint8_t*)(&x))[0];
147         *p++ =  ((uint8_t*)(&x))[1];
148         *p =    ((uint8_t*)(&x))[2];
149         return (chunks+1)*3;
150     case 1:  /* with padding this is an impossible case */
151         x = d0[y[0]];
152         *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
153         break;
154     case 2: // * case 2, 1  output byte */
155         x = d0[y[0]] | d1[y[1]];
156         *p = *((uint8_t*)(&x)); // i.e. first char
157         break;
158     default: /* case 3, 2 output bytes */
159         x = d0[y[0]] | d1[y[1]] | d2[y[2]];  /* 0x3c */
160         *p++ =  ((uint8_t*)(&x))[0];
161         *p =  ((uint8_t*)(&x))[1];
162         break;
163     }
164 
165     if (x >= BADCHAR) return MODP_B64_ERROR;
166 
167     return 3*chunks + (6*leftover)/8;
168 }
169