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 */
41 #include "plstr.h" /* for PL_strlen */
43 static unsigned char *base
= (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
48 const unsigned char *src
,
52 PRUint32 b32
= (PRUint32
)0;
55 for( i
= 0; i
< 3; i
++ )
58 b32
|= (PRUint32
)src
[i
];
61 for( i
= 0; i
< 4; i
++ )
63 dest
[i
] = base
[ (PRUint32
)((b32
>>j
) & 0x3F) ];
73 const unsigned char *src
,
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)'=';
87 const unsigned char *src
,
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)'=';
101 const unsigned char *src
,
108 encode3to4(src
, dest
);
117 encode2to4(src
, dest
);
120 encode1to4(src
, dest
);
125 PR_NOT_REACHED("coding error");
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.
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
)
164 dest
[ destlen
] = (char)0; /* null terminate */
167 encode((const unsigned char *)src
, srclen
, (unsigned char *)dest
);
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
)
193 else if( (unsigned char)'/' == c
)
206 const unsigned char *src
,
210 PRUint32 b32
= (PRUint32
)0;
214 for( i
= 0; i
< 4; i
++ )
216 bits
= codetovalue(src
[i
]);
226 dest
[0] = (unsigned char)((b32
>> 16) & 0xFF);
227 dest
[1] = (unsigned char)((b32
>> 8) & 0xFF);
228 dest
[2] = (unsigned char)((b32
) & 0xFF);
236 const unsigned char *src
,
240 PRUint32 b32
= (PRUint32
)0;
244 bits
= codetovalue(src
[0]);
250 b32
= (PRUint32
)bits
;
253 bits
= codetovalue(src
[1]);
259 b32
|= (PRUint32
)bits
;
262 bits
= codetovalue(src
[2]);
268 ubits
= (PRUint32
)bits
;
271 dest
[0] = (unsigned char)((b32
>> 8) & 0xFF);
272 dest
[1] = (unsigned char)((b32
) & 0xFF);
280 const unsigned char *src
,
288 bits
= codetovalue(src
[0]);
294 ubits
= (PRUint32
)bits
;
297 bits
= codetovalue(src
[1]);
303 ubits
= (PRUint32
)bits
;
306 dest
[0] = (unsigned char)b32
;
314 const unsigned char *src
,
323 rv
= decode4to3(src
, dest
);
324 if( PR_SUCCESS
!= rv
)
337 rv
= decode3to2(src
, dest
);
340 rv
= decode2to1(src
, dest
);
349 PR_NOT_REACHED("coding error");
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
377 PRBool allocated
= PR_FALSE
;
379 if( (char *)0 == src
)
386 srclen
= PL_strlen(src
);
389 if( srclen
&& (0 == (srclen
& 3)) )
391 if( (char)'=' == src
[ srclen
-1 ] )
393 if( (char)'=' == src
[ srclen
-2 ] )
404 if( (char *)0 == dest
)
406 PRUint32 destlen
= ((srclen
* 3) / 4);
407 dest
= (char *)PR_MALLOC(destlen
+ 1);
408 if( (char *)0 == dest
)
412 dest
[ destlen
] = (char)0; /* null terminate */
416 status
= decode((const unsigned char *)src
, srclen
, (unsigned char *)dest
);
417 if( PR_SUCCESS
!= status
)
419 if( PR_TRUE
== allocated
)