Supports Chinese base64 encoding/Decoding
(Original) I wrote a simulation server yesterday to test my terminal and used base64 encoding/decoding. I used the goahead code. I found that it does not support Chinese characters. I modified it myself and solved the problem. Now I will send the code for your reference.
/*
* Base64.c -- base64 mime Encoding
*
* Copyright (c) goahead Software Inc., 1995-2000. All rights reserved.
*
* See the file "license.txt" for usage and redistribution license requirements
*
* $ ID: base64.c, v 1.1 06:50:14 archfree exp $
*/
/******************************* Description ****** ***************************/
/*
* The base64 command encodes and decodes a string in mime base64 format
*/
/******************************* Local data ***** *****************************/
/*
* Mapping of ANSI chars to base64 mime encoding alphabet (see below)
*/
# If 0
Static char map64 [] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-62,-1, -1,-1, 63,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,
-1, 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,-1,-1,-1,-1,-1,
-1, 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,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,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-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1 };
Static char alphabet64 [] = {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h ',
'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ',
'Q', 'R', 's', 't', 'U', 'V', 'w', 'x ',
'Y', 'z', 'A', 'B', 'C', 'D', 'E', 'F ',
'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's', 't', 'U', 'V ',
'W', 'x', 'y', 'z', '0', '1', '2', '3 ',
'4', '5', '6', '7', '8', '9', '+ ','/',
};
/*********************************** Code *** **********************************/
/*
* Decode a buffer from "string" and into "outbuf"
*/
Int websdecode64 (char * outbuf, char * string, int outlen)
{
Unsigned long shiftbuf;
Char * CP, * op;
Int C, I, j, shift;
OP = outbuf;
* Op = '/0 ';
CP = string;
While (* CP & * CP! = '){
/*
* Map 4 (6bit) Input bytes and store in a single long (shiftbuf)
*/
Shiftbuf = 0;
Shift = 18;
For (I = 0; I
C = map64 [* CP & 0xff];
If (C =-1 ){
# If zwhtemp
Error (e_l, e_log, T ("bad string: % s at % C index % d"), string,
C, I );
# Endif
Return-1;
}
Shiftbuf = shiftbuf | (C
Shift-= 6;
}
/*
* Interpret as 3 normal 8 bit bytes (fill in reverse order ).
* Check for potential buffer overflow before filling.
*/
-- I;
If (OP + I) >=& outbuf [outlen]) {
// Gstrcpy (outbuf, T ("string too big");]
// Strcpy (outbuf, T ("string too big "));
Strcpy (outbuf, "string too big ");
Return-1;
}
For (j = 0; j
* Op ++ = (char) (shiftbuf> (8 * (2-j) & 0xff );
}
* Op = '/0 ';
}
Return 0;
}
/*************************************** ***************************************/
/*
* Encode a buffer from "string" into "outbuf"
*/
Void websencode64 (char * outbuf, char * string, int outlen)
{
Unsigned long shiftbuf;
Char * CP, * op;
Int X, I, j, shift;
OP = outbuf;
* Op = '/0 ';
CP = string;
While (* CP ){
/*
* Take three characters and create a 24 bit number in shiftbuf
*/
Shiftbuf = 0;
For (j = 2; j> = 0 & * CP; j --, CP ++ ){
Shiftbuf | = (* CP & 0xff)
}
/*
* Now convert shiftbuf to 4 base64 letters. The I, j magic calculates
* How many letters need to be output.
*/
Shift = 18;
For (I = ++ J; I
X = (shiftbuf> shift) & 0x3f;
* Op ++ = alphabet64 [(shiftbuf> shift) & 0x3f];
Shift-= 6;
}
/*
* Pad at the end with '='
*/
While (j --> 0 ){
* Op ++ = ';
}
* Op = '/0 ';
}
}