// Test Platform: Cygwin GCC compiled under Win2K, no problem (if you find it, please tell me, thank you!) // Call: strlen () (actually writes a similar function function Take the length)
/ ************************************************** *********************************************************** ******* Kunming XXXX Co., Ltd. * Technical Research and Development Department ** (c) Copyright 2005-2006, Kmajian * All Rights Reserved ** File: Base64.c * BY : kmajian * Date: 2006-6-23 **************************************************** *********************************************************** **************** / # include "config.h" #include "base64.h"
#define ch_empty 0xffStatic const uint8 baseAlp [] = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /"; / ********************************************************************************************************************************************************************************************************* *********************************************************** *********************** BASE64 encoding ** function name: base64enc * function description: encoding a specific memory block as Base64 encoding, deposited in a target string * Call Module: None * Message: * Sout Target Character Buffer * * Pin Need to transition to Base64 encoded memory block * ilen memory block length * Return: UINT16 Generates the length of the encoding *********** *********************************************************** *************************************************************************** / UINT16 BASE64ENC (Void * Pin, uint16 iln, char * sout) {uint8 * pbuf = (uint8 *) Pin; uint8 swibuf [4]; uint16 i, n; uint16 Ireturn = 0; uint16 irenm3 = ilen% 3; uint16 ilnd3 = Ilen / 3 UINT16 ILEND3P; if (ilenm3 == 0) // If ilenm3 is equal to 0, Ilend3p is Ilend3, otherwise (Ilend3 1) Ilend3p = Ilend3; Else Ilend3p = Ilend3; IF (Ilen <1) {return Ireturn;} for (i = 0; i
// Get the third, 4th, 5th, 6-bit PBUF of the second target character; Swibuf [2] = Swibuf [2] (* PBUF) >> 6); // Put the third character right 6 Bit, then add to the obtained target 3, 4, 5, 6 // digits, get the third target character SWIBUF [3] = (* PBUF) & 0x3f; // The third character is between the 0x3f, get Fourth target character PBUF ; for (n = 0; n <4; n ) // acquired Base64 code {* sout = baseAlp [Swibuf [n]]; sout ; Ireturn ;}} switch (ilenm3) // Code processing of 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 = baseAlp [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 = baseAlp [Swibuf [n]];} Sout ; Ireturn ;} Break; default: Break;} * Sout = '/ 0'; returni ireturn;}
/ ************************************************** *********************************************************** ******** Base64 Decoded ** Function Name: Base64Dec * Function Description: Press the feed-in-the-thirds to decode, deploy the destination buffer * call module: no * parameter: * SIN source string * * Pout output memory block * Return: UINT16 decoded the length of the content ******************************************************* *********************************************************** ********************** / 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
Outbuf ; ireTurn ;}} Return IReturn;} // Get base64 encoded value uint8 getB64char (const uint8 ch) {uint8 n; if (ch == '=') {return ch_empty;} else {for (n = 0; n / ************************************************** *********************************************************** ******* * ************************************************** *********************************************************** *************** /