Menu/60_Utils/02_Bean/beans/std/base64.bean
2019-07-02 03:32:09 +00:00

172 lines
7.5 KiB
Plaintext
Executable File

module std+, base64 {
private var pr2six(256) byte = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
func base64decode_len(bufcoded. char) int {
var bufin. byte = bufcoded;
while pr2six[.bufin] <= 63;
bufin++;
var nprbytes int = bufin - bufcoded;
var nbytesdecoded int = ((nprbytes + 3) / 4) * 3;
return nbytesdecoded + 1;
}
func base64decode(bufplain. char, bufcoded. char) int {
var bufin. byte = bufcoded;
while pr2six[.bufin] <= 63;
bufin++;
var nprbytes int = bufin - bufcoded;
var nbytesdecoded int = ((nprbytes + 3) / 4) * 3;
var bufout. byte = bufplain;
bufin = bufcoded;
while nprbytes > 4 {
.bufout++ = pr2six[.bufin] << 2 | pr2six[bufin[1]] >> 4;
.bufout++ = pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2;
.bufout++ = pr2six[bufin[2]] << 6 | pr2six[bufin[3]];
bufin += 4;
nprbytes -= 4;
}
if nprbytes > 1;
.bufout++ = pr2six[.bufin] << 2 | pr2six[bufin[1]] >> 4;
if nprbytes > 2;
.bufout++ = pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2;
if nprbytes > 3;
.bufout++ = pr2six[bufin[2]] << 6 | pr2six[bufin[3]];
.bufout = '\0';
nbytesdecoded -= (4 - nprbytes) & 3;
return nbytesdecoded;
}
func base64encode_len(len int) int {
return ((len + 2) / 3 * 4) + 1;
}
func base64encode(encoded. char, str. char, len int) int {
static basis64() char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var i int;
var p. char = encoded;
for i = 0; i < len - 2; i += 3 {
.p++ = basis64[(str[i] >> 2) & 0x3f];
.p++ = basis64[((str[i] & 0x3) << 4) |
(int(str[i + 1] & 0xf0) >> 4)];
.p++ = basis64[((str[i + 1] & 0xf) << 2) |
(int(str[i + 2] & 0xc0) >> 6)];
.p++ = basis64[str[i + 2] & 0x3f];
}
if i < len {
.p++ = basis64[(str[i] >> 2) & 0x3f];
if i == (len - 1) {
.p++ = basis64[((str[i] & 0x3) << 4)];
.p++ = '=';
else
.p++ = basis64[((str[i] & 0x3) << 4) |
(int(str[i + 1] & 0xf0) >> 4)];
.p++ = basis64[((str[i + 1] & 0xf) << 2)];
}
.p++ = '=';
}
.p++ = '\0';
return p - encoded;
}
}
/*
* Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/