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
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.
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 ***** */
39 #include "prlog.h" /* For PR_NOT_REACHED */
40 #include "prmem.h" /* for malloc / PR_MALLOC */
42 #include <string.h> /* for strlen */
44 static unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49 const unsigned char *src,
53 PRUint32 b32 = (PRUint32)0;
56 for( i = 0; i < 3; i++ )
59 b32 |= (PRUint32)src[i];
62 for( i = 0; i < 4; i++ )
64 dest[i] = base[ (PRUint32)((b32>>j) & 0x3F) ];
74 const unsigned char *src,
78 dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ];
79 dest[1] = base[ (PRUint32)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)) ];
80 dest[2] = base[ (PRUint32)((src[1] & 0x0F) << 2) ];
81 dest[3] = (unsigned char)'=';
88 const unsigned char *src,
92 dest[0] = base[ (PRUint32)((src[0]>>2) & 0x3F) ];
93 dest[1] = base[ (PRUint32)((src[0] & 0x03) << 4) ];
94 dest[2] = (unsigned char)'=';
95 dest[3] = (unsigned char)'=';
102 const unsigned char *src,
109 encode3to4(src, dest);
118 encode2to4(src, dest);
121 encode1to4(src, dest);
126 PR_NOT_REACHED("coding error");
135 * If the destination argument is NULL, a return buffer is
136 * allocated, and the data therein will be null-terminated.
137 * If the destination argument is not NULL, it is assumed to
138 * be of sufficient size, and the contents will not be null-
139 * terminated by this routine.
141 * Returns null if the allocation fails.
154 size_t len = strlen(src);
156 /* Detect truncation. */
163 if( (char *)0 == dest )
166 /* Ensure all PRUint32 values stay within range. */
167 if( srclen > (PR_UINT32_MAX/4) * 3 )
171 destlen = ((srclen + 2)/3) * 4;
172 dest = (char *)PR_MALLOC(destlen + 1);
173 if( (char *)0 == dest )
177 dest[ destlen ] = (char)0; /* null terminate */
180 encode((const unsigned char *)src, srclen, (unsigned char *)dest);
190 if( (c >= (unsigned char)'A') && (c <= (unsigned char)'Z') )
192 return (PRInt32)(c - (unsigned char)'A');
194 else if( (c >= (unsigned char)'a') && (c <= (unsigned char)'z') )
196 return ((PRInt32)(c - (unsigned char)'a') +26);
198 else if( (c >= (unsigned char)'0') && (c <= (unsigned char)'9') )
200 return ((PRInt32)(c - (unsigned char)'0') +52);
202 else if( (unsigned char)'+' == c )
206 else if( (unsigned char)'/' == c )
219 const unsigned char *src,
223 PRUint32 b32 = (PRUint32)0;
227 for( i = 0; i < 4; i++ )
229 bits = codetovalue(src[i]);
239 dest[0] = (unsigned char)((b32 >> 16) & 0xFF);
240 dest[1] = (unsigned char)((b32 >> 8) & 0xFF);
241 dest[2] = (unsigned char)((b32 ) & 0xFF);
249 const unsigned char *src,
253 PRUint32 b32 = (PRUint32)0;
257 bits = codetovalue(src[0]);
263 b32 = (PRUint32)bits;
266 bits = codetovalue(src[1]);
272 b32 |= (PRUint32)bits;
275 bits = codetovalue(src[2]);
281 ubits = (PRUint32)bits;
284 dest[0] = (unsigned char)((b32 >> 8) & 0xFF);
285 dest[1] = (unsigned char)((b32 ) & 0xFF);
293 const unsigned char *src,
301 bits = codetovalue(src[0]);
307 ubits = (PRUint32)bits;
310 bits = codetovalue(src[1]);
316 ubits = (PRUint32)bits;
319 dest[0] = (unsigned char)b32;
327 const unsigned char *src,
336 rv = decode4to3(src, dest);
337 if( PR_SUCCESS != rv )
350 rv = decode3to2(src, dest);
353 rv = decode2to1(src, dest);
362 PR_NOT_REACHED("coding error");
371 * If the destination argument is NULL, a return buffer is
372 * allocated and the data therein will be null-terminated.
373 * If the destination argument is not null, it is assumed
374 * to be of sufficient size, and the data will not be null-
375 * terminated by this routine.
377 * Returns null if the allocation fails, or if the source string is
390 PRBool allocated = PR_FALSE;
392 if( (char *)0 == src )
399 size_t len = strlen(src);
401 /* Detect truncation. */
408 if( srclen && (0 == (srclen & 3)) )
410 if( (char)'=' == src[ srclen-1 ] )
412 if( (char)'=' == src[ srclen-2 ] )
423 if( (char *)0 == dest )
425 /* The following computes ((srclen * 3) / 4) without overflow. */
426 PRUint32 destlen = (srclen / 4) * 3 + ((srclen % 4) * 3) / 4;
427 dest = (char *)PR_MALLOC(destlen + 1);
428 if( (char *)0 == dest )
432 dest[ destlen ] = (char)0; /* null terminate */
436 status = decode((const unsigned char *)src, srclen, (unsigned char *)dest);
437 if( PR_SUCCESS != status )
439 if( PR_TRUE == allocated )