64BASE编解码(转载)
2012-09-28 22:08
330 查看
case 9 Stress:
All files in a working directory encoded/decoded
and compared with file comparison utility to
ensure that multiple runs do not cause problems
such as exhausting file handles, tmp storage, etc.
-------------
Syntax, operation and failure:
All options/switches tested. Performs as
expected.
case 10:
No Args -- Shows Usage Screen
Return Code 1 (Invalid Syntax)
case 11:
One Arg (invalid) -- Shows Usage Screen
Return Code 1 (Invalid Syntax)
case 12:
One Arg Help (-?) -- Shows detailed Usage Screen.
Return Code 0 (Success -- help request is valid).
case 13:
One Arg Help (-h) -- Shows detailed Usage Screen.
Return Code 0 (Success -- help request is valid).
case 14:
One Arg (valid) -- Uses stdin/stdout (filter)
Return Code 0 (Sucess)
case 15:
Two Args (invalid file) -- shows system error.
Return Code 2 (File Error)
case 16:
Encode non-existent file -- shows system error.
Return Code 2 (File Error)
case 17:
Out of disk space -- shows system error.
Return Code 3 (File I/O Error)
-------------
Compile/Regression test:
gcc compiled binary under Cygwin
Microsoft Visual Studio under Windows 2000
Microsoft Version 6.0 C under Windows 2000
DEPENDENCIES: None
LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
VERSION HISTORY:
Bob Trower 08/04/01 -- Create Version 0.00.00B
LAST MODIFIED: cheungmine@gmail.com 2009-08-17
\*******************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#define B64_DEF_LINE_SIZE 72
#define B64_MIN_LINE_SIZE 4
/*
** Translation Table as described in RFC1113
*/
static
const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
** Translation Table to decode (created by author)
*/
static
const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
/*
** encodeblock
**
** encode 3 8-bit binary bytes as 4 '6-bit' characters
*/
static
void encodeblock( unsigned
char
in[3], unsigned
char
out[4],
int len )
{
out[0]
= cb64[
in[0]
>> 2 ];
out[1]
= cb64[ ((in[0]
& 0x03)
<< 4)
| ((in[1]
& 0xf0)
>> 4) ];
out[2]
= (unsigned
char) (len
> 1
? cb64[ ((in[1]
& 0x0f)
<< 2)
| ((in[2]
& 0xc0)
>> 6) ] :
'=');
out[3]
= (unsigned
char) (len
> 2
? cb64[
in[2]
& 0x3f ] :
'=');
}
/*
** encode
**
** base64 encode a stream adding padding and line breaks as per spec.
*/
static
void b64_encode_stream( FILE
*infile, FILE
*outfile,
int linesize )
{
unsigned char
in[3],
out[4];
int i, len, blocksout
= 0;
while(
!feof( infile ) ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
in[i]
= (unsigned
char) getc( infile );
if(
!feof( infile ) ) {
len++;
}
else {
in[i]
= 0;
}
}
if( len ) {
encodeblock( in,
out, len );
for( i
= 0; i
< 4; i++ ) {
putc( out[i], outfile );
}
blocksout++;
}
if( blocksout
>= (linesize/4)
|| feof( infile ) ) {
if( blocksout ) {
fprintf( outfile, "\r\n" );
}
blocksout =
0;
}
}
}
/*
** encode
**
** base64 encode bytes
** by cheungmine
** outstr is allocated by caller
** length of encoded string is returned (not including end closing '\0')
*/
static
int b64_encode_string(
const unsigned
char
*instr, int inlen, unsigned
char* outstr )
{
unsigned char
in[3],
out[4];
int inc=0, outc=0,
i, len;
/* only size of buffer required
*/
if (!outstr){
while( inc
< inlen ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
if( inc++<inlen ){
len++;
}
}
if( len ) {
outc +=
4;
}
}
return outc;
}
assert(outstr);
while( inc
< inlen ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
in[i]
= instr[inc];
if( inc++<inlen ){
len++;
}
else {
in[i]
= 0;
}
}
if( len ) {
encodeblock( in,
out, len );
for( i
= 0; i
< 4; i++ ) {
outstr[outc++]
= out[i];
}
}
}
return outc;
}
/*
** decodeblock
**
** decode 4 '6-bit' characters into 3 8-bit binary bytes
*/
static
void decodeblock( unsigned
char
in[4], unsigned
char
out[3] )
{
out[
0 ] = (unsigned
char ) (in[0]
<< 2
| in[1]
>> 4);
out[
1 ] = (unsigned
char ) (in[1]
<< 4
| in[2]
>> 2);
out[
2 ] = (unsigned
char ) (((in[2]
<< 6)
& 0xc0)
| in[3]);
}
/*
** decode
**
** decode a base64 encoded stream discarding padding, line breaks and noise
*/
static
void b64_decode_stream( FILE
*infile, FILE
*outfile )
{
unsigned char
in[4],
out[3], v;
int i, len;
while(
!feof( infile ) ) {
for( len
= 0, i
= 0; i
< 4
&& !feof( infile ); i++ ) {
v =
0;
while(
!feof( infile )
&& v ==
0 ) {
v = (unsigned
char) getc( infile );
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if(
!feof( infile ) ) {
len++;
if( v ) {
in[ i ]
= (unsigned
char) (v -
1);
}
}
else {
in[i]
= 0;
}
}
if( len ) {
decodeblock( in,
out );
for( i
= 0; i
< len
- 1; i++ ) {
putc( out[i], outfile );
}
}
}
}
/*
* add by cheungmine
*/
static
int b64_decode_string(
const unsigned
char
*instr, int inlen, unsigned
char
*outstr )
{
unsigned char
in[4],
out[3], v;
int inc=0, outc=0,
i, len;
/* only size of buffer required
*/
if (!outstr){
while( inc
< inlen ) {
for( len
= 0, i
= 0; i
< 4
&& inc<inlen; i++ ) {
v =
0;
while( (inc<inlen)
&& (v==0) ) {
v = instr[inc++];
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if( inc<inlen ) {
len++;
}
}
if( len>1 ) {
outc += (len-1);
}
}
return outc;
}
assert(outstr);
while( inc
< inlen ) {
for( len
= 0, i
= 0; i
< 4
&& inc<inlen; i++ ) {
v =
0;
while( (inc<inlen)
&& (v==0) ) {
v = instr[inc++];
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if( inc<inlen ) {
len++;
if( v ) {
in[ i ]
= (unsigned
char) (v -
1);
}
}
else {
in[i]
= 0;
}
}
if( len ) {
decodeblock( in,
out );
for( i
= 0; i
< len
- 1; i++ ) {
outstr[outc++]
= out[i];
}
}
}
return outc;
}
/*
** returnable errors
**
** Error codes returned to the operating system.
**
*/
#define B64_SYNTAX_ERROR 1
#define B64_FILE_ERROR 2
#define B64_FILE_IO_ERROR 3
#define B64_ERROR_OUT_CLOSE 4
#define B64_LINE_SIZE_TO_MIN 5
/*
** b64_message
**
** Gather text messages in one place.
**
*/
static
char *b64_message(
int errcode )
{
#define B64_MAX_MESSAGES 6
char
*msgs[ B64_MAX_MESSAGES ]
= {
"b64:000:Invalid Message Code.",
"b64:001:Syntax Error -- check help for usage.",
"b64:002:File Error Opening/Creating Files.",
"b64:003:File I/O Error -- Note: output file not removed.",
"b64:004:Error on output file close.",
"b64:004:linesize set to minimum."
};
char
*msg = msgs[
0 ];
if( errcode
> 0
&& errcode
< B64_MAX_MESSAGES ) {
msg = msgs[ errcode ];
}
return( msg );
}
/*
* Example by cheungmine
*
int main()
{
int size_out;
int size_ret;
char *b64_out;
char *b64_outB;
char b64_in[] = "hello world shanghai";
// Base64 编码字符串
size_out = b64_encode_string((const unsigned char*)b64_in, (int)strlen(b64_in), 0);
b64_out = (char*) malloc(size_out+1);
size_ret = b64_encode_string((const unsigned char*)b64_in, (int)strlen(b64_in), (unsigned char*)b64_out);
b64_out[size_ret] = 0;
assert(size_ret==size_out);
// Base64 解码字符串
size_out = b64_decode_string((const unsigned char*)b64_out, size_ret, 0);
b64_outB = (char*) malloc(size_out+1);
size_ret = b64_decode_string((const unsigned char*)b64_out, size_ret, (unsigned char*)b64_outB);
b64_outB[size_ret] = 0;
assert(size_ret==size_out);
assert(strcmp(b64_outB, b64_in)==0);
free(b64_out);
free(b64_outB);
return 0;
}
*/
All files in a working directory encoded/decoded
and compared with file comparison utility to
ensure that multiple runs do not cause problems
such as exhausting file handles, tmp storage, etc.
-------------
Syntax, operation and failure:
All options/switches tested. Performs as
expected.
case 10:
No Args -- Shows Usage Screen
Return Code 1 (Invalid Syntax)
case 11:
One Arg (invalid) -- Shows Usage Screen
Return Code 1 (Invalid Syntax)
case 12:
One Arg Help (-?) -- Shows detailed Usage Screen.
Return Code 0 (Success -- help request is valid).
case 13:
One Arg Help (-h) -- Shows detailed Usage Screen.
Return Code 0 (Success -- help request is valid).
case 14:
One Arg (valid) -- Uses stdin/stdout (filter)
Return Code 0 (Sucess)
case 15:
Two Args (invalid file) -- shows system error.
Return Code 2 (File Error)
case 16:
Encode non-existent file -- shows system error.
Return Code 2 (File Error)
case 17:
Out of disk space -- shows system error.
Return Code 3 (File I/O Error)
-------------
Compile/Regression test:
gcc compiled binary under Cygwin
Microsoft Visual Studio under Windows 2000
Microsoft Version 6.0 C under Windows 2000
DEPENDENCIES: None
LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
VERSION HISTORY:
Bob Trower 08/04/01 -- Create Version 0.00.00B
LAST MODIFIED: cheungmine@gmail.com 2009-08-17
\*******************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#define B64_DEF_LINE_SIZE 72
#define B64_MIN_LINE_SIZE 4
/*
** Translation Table as described in RFC1113
*/
static
const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
** Translation Table to decode (created by author)
*/
static
const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
/*
** encodeblock
**
** encode 3 8-bit binary bytes as 4 '6-bit' characters
*/
static
void encodeblock( unsigned
char
in[3], unsigned
char
out[4],
int len )
{
out[0]
= cb64[
in[0]
>> 2 ];
out[1]
= cb64[ ((in[0]
& 0x03)
<< 4)
| ((in[1]
& 0xf0)
>> 4) ];
out[2]
= (unsigned
char) (len
> 1
? cb64[ ((in[1]
& 0x0f)
<< 2)
| ((in[2]
& 0xc0)
>> 6) ] :
'=');
out[3]
= (unsigned
char) (len
> 2
? cb64[
in[2]
& 0x3f ] :
'=');
}
/*
** encode
**
** base64 encode a stream adding padding and line breaks as per spec.
*/
static
void b64_encode_stream( FILE
*infile, FILE
*outfile,
int linesize )
{
unsigned char
in[3],
out[4];
int i, len, blocksout
= 0;
while(
!feof( infile ) ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
in[i]
= (unsigned
char) getc( infile );
if(
!feof( infile ) ) {
len++;
}
else {
in[i]
= 0;
}
}
if( len ) {
encodeblock( in,
out, len );
for( i
= 0; i
< 4; i++ ) {
putc( out[i], outfile );
}
blocksout++;
}
if( blocksout
>= (linesize/4)
|| feof( infile ) ) {
if( blocksout ) {
fprintf( outfile, "\r\n" );
}
blocksout =
0;
}
}
}
/*
** encode
**
** base64 encode bytes
** by cheungmine
** outstr is allocated by caller
** length of encoded string is returned (not including end closing '\0')
*/
static
int b64_encode_string(
const unsigned
char
*instr, int inlen, unsigned
char* outstr )
{
unsigned char
in[3],
out[4];
int inc=0, outc=0,
i, len;
/* only size of buffer required
*/
if (!outstr){
while( inc
< inlen ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
if( inc++<inlen ){
len++;
}
}
if( len ) {
outc +=
4;
}
}
return outc;
}
assert(outstr);
while( inc
< inlen ) {
len =
0;
for( i
= 0; i
< 3; i++ ) {
in[i]
= instr[inc];
if( inc++<inlen ){
len++;
}
else {
in[i]
= 0;
}
}
if( len ) {
encodeblock( in,
out, len );
for( i
= 0; i
< 4; i++ ) {
outstr[outc++]
= out[i];
}
}
}
return outc;
}
/*
** decodeblock
**
** decode 4 '6-bit' characters into 3 8-bit binary bytes
*/
static
void decodeblock( unsigned
char
in[4], unsigned
char
out[3] )
{
out[
0 ] = (unsigned
char ) (in[0]
<< 2
| in[1]
>> 4);
out[
1 ] = (unsigned
char ) (in[1]
<< 4
| in[2]
>> 2);
out[
2 ] = (unsigned
char ) (((in[2]
<< 6)
& 0xc0)
| in[3]);
}
/*
** decode
**
** decode a base64 encoded stream discarding padding, line breaks and noise
*/
static
void b64_decode_stream( FILE
*infile, FILE
*outfile )
{
unsigned char
in[4],
out[3], v;
int i, len;
while(
!feof( infile ) ) {
for( len
= 0, i
= 0; i
< 4
&& !feof( infile ); i++ ) {
v =
0;
while(
!feof( infile )
&& v ==
0 ) {
v = (unsigned
char) getc( infile );
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if(
!feof( infile ) ) {
len++;
if( v ) {
in[ i ]
= (unsigned
char) (v -
1);
}
}
else {
in[i]
= 0;
}
}
if( len ) {
decodeblock( in,
out );
for( i
= 0; i
< len
- 1; i++ ) {
putc( out[i], outfile );
}
}
}
}
/*
* add by cheungmine
*/
static
int b64_decode_string(
const unsigned
char
*instr, int inlen, unsigned
char
*outstr )
{
unsigned char
in[4],
out[3], v;
int inc=0, outc=0,
i, len;
/* only size of buffer required
*/
if (!outstr){
while( inc
< inlen ) {
for( len
= 0, i
= 0; i
< 4
&& inc<inlen; i++ ) {
v =
0;
while( (inc<inlen)
&& (v==0) ) {
v = instr[inc++];
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if( inc<inlen ) {
len++;
}
}
if( len>1 ) {
outc += (len-1);
}
}
return outc;
}
assert(outstr);
while( inc
< inlen ) {
for( len
= 0, i
= 0; i
< 4
&& inc<inlen; i++ ) {
v =
0;
while( (inc<inlen)
&& (v==0) ) {
v = instr[inc++];
v = (unsigned
char) ((v
< 43
|| v
> 122)
? 0 : cd64[ v
- 43 ]);
if( v ) {
v = (unsigned
char) ((v
== '$')
? 0 : v
- 61);
}
}
if( inc<inlen ) {
len++;
if( v ) {
in[ i ]
= (unsigned
char) (v -
1);
}
}
else {
in[i]
= 0;
}
}
if( len ) {
decodeblock( in,
out );
for( i
= 0; i
< len
- 1; i++ ) {
outstr[outc++]
= out[i];
}
}
}
return outc;
}
/*
** returnable errors
**
** Error codes returned to the operating system.
**
*/
#define B64_SYNTAX_ERROR 1
#define B64_FILE_ERROR 2
#define B64_FILE_IO_ERROR 3
#define B64_ERROR_OUT_CLOSE 4
#define B64_LINE_SIZE_TO_MIN 5
/*
** b64_message
**
** Gather text messages in one place.
**
*/
static
char *b64_message(
int errcode )
{
#define B64_MAX_MESSAGES 6
char
*msgs[ B64_MAX_MESSAGES ]
= {
"b64:000:Invalid Message Code.",
"b64:001:Syntax Error -- check help for usage.",
"b64:002:File Error Opening/Creating Files.",
"b64:003:File I/O Error -- Note: output file not removed.",
"b64:004:Error on output file close.",
"b64:004:linesize set to minimum."
};
char
*msg = msgs[
0 ];
if( errcode
> 0
&& errcode
< B64_MAX_MESSAGES ) {
msg = msgs[ errcode ];
}
return( msg );
}
/*
* Example by cheungmine
*
int main()
{
int size_out;
int size_ret;
char *b64_out;
char *b64_outB;
char b64_in[] = "hello world shanghai";
// Base64 编码字符串
size_out = b64_encode_string((const unsigned char*)b64_in, (int)strlen(b64_in), 0);
b64_out = (char*) malloc(size_out+1);
size_ret = b64_encode_string((const unsigned char*)b64_in, (int)strlen(b64_in), (unsigned char*)b64_out);
b64_out[size_ret] = 0;
assert(size_ret==size_out);
// Base64 解码字符串
size_out = b64_decode_string((const unsigned char*)b64_out, size_ret, 0);
b64_outB = (char*) malloc(size_out+1);
size_ret = b64_decode_string((const unsigned char*)b64_out, size_ret, (unsigned char*)b64_outB);
b64_outB[size_ret] = 0;
assert(size_ret==size_out);
assert(strcmp(b64_outB, b64_in)==0);
free(b64_out);
free(b64_outB);
return 0;
}
*/
相关文章推荐
- 64BASE编解码(转载)
- 64BASE编解码
- 文本文件BASE-64编码解码
- UrlEncode以及Base-64编解码简介
- iOS 自带的base64 编码与解码
- 图片 base 64 的编码与解码
- C# base 64图片编码解码
- base 64 编解码
- 用js编解码base64
- 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非空白字符。
- [JavaScript]URL编码和解码[转载]
- base 64 加密解密 for test demo
- Base64
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
- Base 64 Encoding 的解答
- JS前台base32加密,C#后台解码
- 寻找【System.FormatException: Base-64 字符数组的无效长度】的解决方法
- 解决“Base-64字符数组的无效长度” 的问题
- 浅谈 Data URI 与 BASE 64 编码
- DES解密时“Base-64字符数组的无效长度”