• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <build/build_config.h>
49 
50 #undef WORDS_BIGENDIAN
51 #if !defined(ARCH_CPU_LITTLE_ENDIAN)
52 #define WORDS_BIGENDIAN 1
53 #endif
54 
55 #include "modp_b64_data.h"
56 
57 #define BADCHAR 0x01FFFFFF
58 
59 /**
60  * you can control if we use padding by commenting out this
61  * next line.  However, I highly recommend you use padding and not
62  * using it should only be for compatability with a 3rd party.
63  * Also, 'no padding' is not tested!
64  */
65 #define DOPAD 1
66 
67 /*
68  * if we aren't doing padding
69  * set the pad character to NULL
70  */
71 #ifndef DOPAD
72 #undef CHARPAD
73 #define CHARPAD '\0'
74 #endif
75 
modp_b64_encode(char * dest,const char * str,size_t len)76 size_t modp_b64_encode(char* dest, const char* str, size_t len)
77 {
78     size_t i = 0;
79     uint8_t* p = (uint8_t*) dest;
80 
81     /* unsigned here is important! */
82     uint8_t t1, t2, t3;
83 
84     if (len > 2) {
85         for (; i < len - 2; i += 3) {
86             t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
87             *p++ = e0[t1];
88             *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
89             *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
90             *p++ = e2[t3];
91         }
92     }
93 
94     switch (len - i) {
95     case 0:
96         break;
97     case 1:
98         t1 = str[i];
99         *p++ = e0[t1];
100         *p++ = e1[(t1 & 0x03) << 4];
101         *p++ = CHARPAD;
102         *p++ = CHARPAD;
103         break;
104     default: /* case 2 */
105         t1 = str[i]; t2 = str[i+1];
106         *p++ = e0[t1];
107         *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
108         *p++ = e2[(t2 & 0x0F) << 2];
109         *p++ = CHARPAD;
110     }
111 
112     *p = '\0';
113     return p - (uint8_t*)dest;
114 }
115 
116 #ifdef WORDS_BIGENDIAN   /* BIG ENDIAN -- SUN / IBM / MOTOROLA */
modp_b64_decode(char * dest,const char * src,size_t len)117 size_t modp_b64_decode(char* dest, const char* src, size_t len)
118 {
119     if (len == 0) return 0;
120 
121 #ifdef DOPAD
122     /* if padding is used, then the message must be at least
123        4 chars and be a multiple of 4.
124        there can be at most 2 pad chars at the end */
125     if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR;
126     if (src[len-1] == CHARPAD) {
127         len--;
128         if (src[len -1] == CHARPAD) {
129             len--;
130         }
131     }
132 #endif  /* DOPAD */
133 
134     size_t i;
135     int leftover = len % 4;
136     size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
137 
138     uint8_t* p = (uint8_t*) dest;
139     uint32_t x = 0;
140     uint32_t* destInt = (uint32_t*) p;
141     uint32_t* srcInt = (uint32_t*) src;
142     uint32_t y = *srcInt++;
143     for (i = 0; i < chunks; ++i) {
144         x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
145             d2[y >> 8 & 0xff] | d3[y & 0xff];
146 
147         if (x >= BADCHAR)  return MODP_B64_ERROR;
148         *destInt = x << 8;
149         p += 3;
150         destInt = (uint32_t*)p;
151         y = *srcInt++;
152     }
153 
154     switch (leftover) {
155     case 0:
156         x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
157             d2[y >>  8 & 0xff] | d3[y & 0xff];
158         if (x >= BADCHAR)  return MODP_B64_ERROR;
159         *p++ = ((uint8_t*)&x)[1];
160         *p++ = ((uint8_t*)&x)[2];
161         *p = ((uint8_t*)&x)[3];
162         return (chunks+1)*3;
163     case 1:
164         x = d3[y >> 24];
165         *p =  (uint8_t)x;
166         break;
167     case 2:
168         x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff];
169         *p =  (uint8_t)(x >> 4);
170         break;
171     default:  /* case 3 */
172         x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 +
173             d3[(y >> 8) & 0xff];
174         *p++ = (uint8_t) (x >> 10);
175         *p = (uint8_t) (x >> 2);
176         break;
177     }
178 
179     if (x >= BADCHAR) return MODP_B64_ERROR;
180     return 3*chunks + (6*leftover)/8;
181 }
182 
183 #else /* LITTLE  ENDIAN -- INTEL AND FRIENDS */
184 
modp_b64_decode(char * dest,const char * src,size_t len)185 size_t modp_b64_decode(char* dest, const char* src, size_t len)
186 {
187     if (len == 0) return 0;
188 
189 #ifdef DOPAD
190     /*
191      * if padding is used, then the message must be at least
192      * 4 chars and be a multiple of 4
193      */
194     if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR; /* error */
195     /* there can be at most 2 pad chars at the end */
196     if (src[len-1] == CHARPAD) {
197         len--;
198         if (src[len -1] == CHARPAD) {
199             len--;
200         }
201     }
202 #endif
203 
204     size_t i;
205     int leftover = len % 4;
206     size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
207 
208     uint8_t* p = (uint8_t*)dest;
209     uint32_t x = 0;
210     const uint8_t* y = (uint8_t*)src;
211     for (i = 0; i < chunks; ++i, y += 4) {
212         x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
213         if (x >= BADCHAR) return MODP_B64_ERROR;
214         *p++ =  ((uint8_t*)(&x))[0];
215         *p++ =  ((uint8_t*)(&x))[1];
216         *p++ =  ((uint8_t*)(&x))[2];
217     }
218 
219     switch (leftover) {
220     case 0:
221         x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
222 
223         if (x >= BADCHAR) return MODP_B64_ERROR;
224         *p++ =  ((uint8_t*)(&x))[0];
225         *p++ =  ((uint8_t*)(&x))[1];
226         *p =    ((uint8_t*)(&x))[2];
227         return (chunks+1)*3;
228         break;
229     case 1:  /* with padding this is an impossible case */
230         x = d0[y[0]];
231         *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
232         break;
233     case 2: // * case 2, 1  output byte */
234         x = d0[y[0]] | d1[y[1]];
235         *p = *((uint8_t*)(&x)); // i.e. first char
236         break;
237     default: /* case 3, 2 output bytes */
238         x = d0[y[0]] | d1[y[1]] | d2[y[2]];  /* 0x3c */
239         *p++ =  ((uint8_t*)(&x))[0];
240         *p =  ((uint8_t*)(&x))[1];
241         break;
242     }
243 
244     if (x >= BADCHAR) return MODP_B64_ERROR;
245 
246     return 3*chunks + (6*leftover)/8;
247 }
248 
249 #endif  /* if bigendian / else / endif */
250