Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / nsprpub / lib / libc / src / base64.c
blob234d60d8fbf440588b63d8f1c6c4373ce04bbdb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "plbase64.h"
39 #include "prlog.h" /* For PR_NOT_REACHED */
40 #include "prmem.h" /* for malloc / PR_MALLOC */
41 #include "plstr.h" /* for PL_strlen */
43 static unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45 static void
46 encode3to4
48 const unsigned char *src,
49 unsigned char *dest
52 PRUint32 b32 = (PRUint32)0;
53 PRIntn i, j = 18;
55 for( i = 0; i < 3; i++ )
57 b32 <<= 8;
58 b32 |= (PRUint32)src[i];
61 for( i = 0; i < 4; i++ )
63 dest[i] = base[ (PRUint32)((b32>>j) & 0x3F) ];
64 j -= 6;
67 return;
70 static void
71 encode2to4
73 const unsigned char *src,
74 unsigned char *dest
77 dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ];
78 dest[1] = base[ (PRUint32)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)) ];
79 dest[2] = base[ (PRUint32)((src[1] & 0x0F) << 2) ];
80 dest[3] = (unsigned char)'=';
81 return;
84 static void
85 encode1to4
87 const unsigned char *src,
88 unsigned char *dest
91 dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ];
92 dest[1] = base[ (PRUint32)((src[0] & 0x03) << 4) ];
93 dest[2] = (unsigned char)'=';
94 dest[3] = (unsigned char)'=';
95 return;
98 static void
99 encode
101 const unsigned char *src,
102 PRUint32 srclen,
103 unsigned char *dest
106 while( srclen >= 3 )
108 encode3to4(src, dest);
109 src += 3;
110 dest += 4;
111 srclen -= 3;
114 switch( srclen )
116 case 2:
117 encode2to4(src, dest);
118 break;
119 case 1:
120 encode1to4(src, dest);
121 break;
122 case 0:
123 break;
124 default:
125 PR_NOT_REACHED("coding error");
128 return;
132 * PL_Base64Encode
134 * If the destination argument is NULL, a return buffer is
135 * allocated, and the data therein will be null-terminated.
136 * If the destination argument is not NULL, it is assumed to
137 * be of sufficient size, and the contents will not be null-
138 * terminated by this routine.
140 * Returns null if the allocation fails.
143 PR_IMPLEMENT(char *)
144 PL_Base64Encode
146 const char *src,
147 PRUint32 srclen,
148 char *dest
151 if( 0 == srclen )
153 srclen = PL_strlen(src);
156 if( (char *)0 == dest )
158 PRUint32 destlen = ((srclen + 2)/3) * 4;
159 dest = (char *)PR_MALLOC(destlen + 1);
160 if( (char *)0 == dest )
162 return (char *)0;
164 dest[ destlen ] = (char)0; /* null terminate */
167 encode((const unsigned char *)src, srclen, (unsigned char *)dest);
168 return dest;
171 static PRInt32
172 codetovalue
174 unsigned char c
177 if( (c >= (unsigned char)'A') && (c <= (unsigned char)'Z') )
179 return (PRInt32)(c - (unsigned char)'A');
181 else if( (c >= (unsigned char)'a') && (c <= (unsigned char)'z') )
183 return ((PRInt32)(c - (unsigned char)'a') +26);
185 else if( (c >= (unsigned char)'0') && (c <= (unsigned char)'9') )
187 return ((PRInt32)(c - (unsigned char)'0') +52);
189 else if( (unsigned char)'+' == c )
191 return (PRInt32)62;
193 else if( (unsigned char)'/' == c )
195 return (PRInt32)63;
197 else
199 return -1;
203 static PRStatus
204 decode4to3
206 const unsigned char *src,
207 unsigned char *dest
210 PRUint32 b32 = (PRUint32)0;
211 PRInt32 bits;
212 PRIntn i;
214 for( i = 0; i < 4; i++ )
216 bits = codetovalue(src[i]);
217 if( bits < 0 )
219 return PR_FAILURE;
222 b32 <<= 6;
223 b32 |= bits;
226 dest[0] = (unsigned char)((b32 >> 16) & 0xFF);
227 dest[1] = (unsigned char)((b32 >> 8) & 0xFF);
228 dest[2] = (unsigned char)((b32 ) & 0xFF);
230 return PR_SUCCESS;
233 static PRStatus
234 decode3to2
236 const unsigned char *src,
237 unsigned char *dest
240 PRUint32 b32 = (PRUint32)0;
241 PRInt32 bits;
242 PRUint32 ubits;
244 bits = codetovalue(src[0]);
245 if( bits < 0 )
247 return PR_FAILURE;
250 b32 = (PRUint32)bits;
251 b32 <<= 6;
253 bits = codetovalue(src[1]);
254 if( bits < 0 )
256 return PR_FAILURE;
259 b32 |= (PRUint32)bits;
260 b32 <<= 4;
262 bits = codetovalue(src[2]);
263 if( bits < 0 )
265 return PR_FAILURE;
268 ubits = (PRUint32)bits;
269 b32 |= (ubits >> 2);
271 dest[0] = (unsigned char)((b32 >> 8) & 0xFF);
272 dest[1] = (unsigned char)((b32 ) & 0xFF);
274 return PR_SUCCESS;
277 static PRStatus
278 decode2to1
280 const unsigned char *src,
281 unsigned char *dest
284 PRUint32 b32;
285 PRUint32 ubits;
286 PRInt32 bits;
288 bits = codetovalue(src[0]);
289 if( bits < 0 )
291 return PR_FAILURE;
294 ubits = (PRUint32)bits;
295 b32 = (ubits << 2);
297 bits = codetovalue(src[1]);
298 if( bits < 0 )
300 return PR_FAILURE;
303 ubits = (PRUint32)bits;
304 b32 |= (ubits >> 4);
306 dest[0] = (unsigned char)b32;
308 return PR_SUCCESS;
311 static PRStatus
312 decode
314 const unsigned char *src,
315 PRUint32 srclen,
316 unsigned char *dest
319 PRStatus rv;
321 while( srclen >= 4 )
323 rv = decode4to3(src, dest);
324 if( PR_SUCCESS != rv )
326 return PR_FAILURE;
329 src += 4;
330 dest += 3;
331 srclen -= 4;
334 switch( srclen )
336 case 3:
337 rv = decode3to2(src, dest);
338 break;
339 case 2:
340 rv = decode2to1(src, dest);
341 break;
342 case 1:
343 rv = PR_FAILURE;
344 break;
345 case 0:
346 rv = PR_SUCCESS;
347 break;
348 default:
349 PR_NOT_REACHED("coding error");
352 return rv;
356 * PL_Base64Decode
358 * If the destination argument is NULL, a return buffer is
359 * allocated and the data therein will be null-terminated.
360 * If the destination argument is not null, it is assumed
361 * to be of sufficient size, and the data will not be null-
362 * terminated by this routine.
364 * Returns null if the allocation fails, or if the source string is
365 * not well-formed.
368 PR_IMPLEMENT(char *)
369 PL_Base64Decode
371 const char *src,
372 PRUint32 srclen,
373 char *dest
376 PRStatus status;
377 PRBool allocated = PR_FALSE;
379 if( (char *)0 == src )
381 return (char *)0;
384 if( 0 == srclen )
386 srclen = PL_strlen(src);
389 if( srclen && (0 == (srclen & 3)) )
391 if( (char)'=' == src[ srclen-1 ] )
393 if( (char)'=' == src[ srclen-2 ] )
395 srclen -= 2;
397 else
399 srclen -= 1;
404 if( (char *)0 == dest )
406 PRUint32 destlen = ((srclen * 3) / 4);
407 dest = (char *)PR_MALLOC(destlen + 1);
408 if( (char *)0 == dest )
410 return (char *)0;
412 dest[ destlen ] = (char)0; /* null terminate */
413 allocated = PR_TRUE;
416 status = decode((const unsigned char *)src, srclen, (unsigned char *)dest);
417 if( PR_SUCCESS != status )
419 if( PR_TRUE == allocated )
421 PR_DELETE(dest);
424 return (char *)0;
427 return dest;