// Test Platform: cygwin GCC in Win2k has been compiled, and no problems have been found in the test. (If you find any problems, please let me know. Thank you !)
// Called database: strlen () (in fact, you can write a similar function to get the length)
/*
**************************************** **************************************** *************************
* Kunming XXXX Co., Ltd.
* Technical R & D department
*
* (C) Copyright 2005-2006, kmajian
* All Rights Reserved
*
* Base-64 codec program file
*
* File: base64.c
* By: kmajian
* Date: 2006-6-23
**************************************** **************************************** *************************
*/
# Include "config. H"
# Include "base64.h"
# Define ch_empty 0xff
Static const uint8 basephosphatase [] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
/*
**************************************** **************************************** *************************
* Base64 encoding
*
* Function name: base64enc
* Function Description: encode a specific memory block as base64 and store it to the destination string.
* Call module: None
* Parameter: * sout target Character Buffer
** The pin must be converted to a base64 encoded memory block.
* Ilen memory block Length
* Return: the length of the uint16 encoding.
**************************************** **************************************** *************************
*/
Uint16 base64enc (void * pin, uint16 ilen, char * sout)
{
Uint8 * pbuf = (uint8 *) PIN;
Uint8 swibuf [4];
Uint16 I, N;
Uint16 ireturn = 0;
Uint16 ilenm3 = ilen % 3;
Uint16 ilend3 = ilen/3;
Uint16 ilend3p;
If (ilenm3 = 0) // If ilenm3 is equal to 0, ilend3 is ilend3; otherwise, it is (ilend3 + 1)
Ilend3p = ilend3;
Else
Ilend3p = ilend3;
If (ilen <1)
{
Return ireturn;
}
For (I = 0; I <ilend3p; I ++)
{
Swibuf [0] = (* pbuf)> 2; // shift the first character to the right two places to obtain Target Characters
Swibuf [1] = (* pbuf) <4) & 0x30; // move the first character four places to the left, and then match 0x30,
// Obtain 5th and 6 Characters of the second target character
Pbuf ++;
Swibuf [1] = swibuf [1] + (* pbuf)> 4); // shift the second character four places to the right, and then add it to the obtained destination five or six digits
// Obtain the second target character
Swibuf [2] = (* pbuf) <2) & 0x3c; // move the second character two places to the left, and then match it with 0x3c,
// Obtain the 3rd, 4, 5, and 6 Characters of the second target character
Pbuf ++;
Swibuf [2] = swibuf [2] + (* pbuf)> 6); // shift the third character to the right of 6 characters, then
// Add bits to obtain the third target character
Swibuf [3] = (* pbuf) & 0x3f; // obtain the fourth target character from the third character and 0x3f.
Pbuf ++;
For (n = 0; n <4; n ++) // obtain the required base64 encoding.
{
* Sout = basephosphatase [swibuf [N];
Sout ++;
Ireturn ++;
}
}
Switch (ilenm3) // encode less than three characters
{
Case 1: swibuf [0] = (* pbuf)> 2;
Swibuf [1] = (* pbuf) <4) & 0x30;
Swibuf [2] = ';
Swibuf [3] = ';
For (n = 0; n <4; n ++)
{
If (swibuf [N] = ')
{
* Sout = ';
}
Else
{
* Sout = basephosphatase [swibuf [N];
}
Sout ++;
Ireturn ++;
}
Break;
Case 2: swibuf [0] = (* pbuf)> 2;
Swibuf [1] = (* pbuf) <4) & 0x30;
Pbuf ++;
Swibuf [1] = swibuf [1] + (* pbuf)> 4 );
Swibuf [2] = (* pbuf) <2) & 0x3c;
Swibuf [3] = ';
For (n = 0; n <4; n ++)
{
If (swibuf [N] = ')
{
* Sout = ';
}
Else
{
* Sout = basephosphatase [swibuf [N];
}
Sout ++;
Ireturn ++;
}
Break;
Default: break;
}
* Sout = '/0 ';
Return ireturn;
}
/*
**************************************** **************************************** *************************
* Base64 Decoding
*
* Function name: base64dec
* Function Description: decodes the sent string in base64 mode and stores it in the destination buffer zone.
* Call module: None
* Parameter: * sin source string
** Pout output memory block
* Return: length of the decoded content of uint16
**************************************** **************************************** *************************
*/
Uint16 base64dec (const char * sin, void * const pout)
{
Uint8 * outbuf = (uint8 *) pout;
Uint8 ctemp;
Uint8 cbuf [3];
Uint16 ireturn = 0;
Uint16 I, N;
Uint16 clen;
Uint16 Alen;
Clen = strlen (SIN );
If (clen % 4 )! = 0)
{
Return ireturn;
}
Alen = clen/4;
For (I = 0; I <Alen; I ++)
{
Ctemp = getb64char (* sin );
Sin ++;
Cbuf [0] = ctemp <2;
Ctemp = getb64char (* sin );
Sin ++;
Cbuf [0] = cbuf [0] + (ctemp> 4 );
Cbuf [1] = ctemp <4;
Ctemp = getb64char (* sin );
Sin ++;
If (ctemp = ch_empty)
{
* Outbuf = cbuf [0];
Outbuf ++;
* Outbuf = cbuf [1];
Ireturn = ireturn + 2;
Return ireturn;
}
Else
{
Cbuf [1] = cbuf [1] + (ctemp> 2 );
Cbuf [2] = ctemp <6;
}
Ctemp = getb64char (* sin );
Sin ++;
If (ctemp = ch_empty)
{
* Outbuf = cbuf [0];
Outbuf ++;
* Outbuf = cbuf [1];
Outbuf ++;
* Outbuf = cbuf [2];
Ireturn = ireturn + 3;
Return ireturn;
}
Else
{
Cbuf [2] = cbuf [2] + ctemp;
}
For (n = 0; n <3; n ++)
{
* Outbuf = cbuf [N];
Outbuf ++;
Ireturn ++;
}
}
Return ireturn;
}
// Obtain the base64 encoding Value
Uint8 getb64char (const uint8 ch)
{
Uint8 N;
If (CH = ')
{
Return ch_empty;
}
Else
{
For (n = 0; n <strlen (basealkaline); N ++)
{
If (CH = basephosphatase [N])
{
Break;
}
}
Return N;
}
}
/*
**************************************** **************************************** *************************
* End
**************************************** **************************************** *************************
*/