Base64 coded source code can be encoded for any type of data

xiaoxiao2021-04-08  419

// 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 > 2; // Put the first character to right to get 2 digits to one target character Swibuf [1] = (* PBUF) << 4) & 0x30; // Move the first character left, then with 0x30, // Get the second target character 5th, 6th PBUF Swibuf [1] = Swibuf [1] ((* pbuf) >> 4); // Put the second character to the right, then add the second one with the obtained target 5, 6-bit // Target character SWIBUF [2] = (* PBUF) << 2) & 0x3c; // shift the second character left, then with 0x3c,

// 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 > 4); CBUF [1] = CTEMP << 4; ctemp = getb64char (* sin); sin ; if (ctemp == ch_empty) {* Outbuf = CBUF [0]; OUTB Uf ; * 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.} else {CBUF [2] = CBUF [2] CTEMP;} for (n = 0; n <3; n ) {* OUTBUF = CBUF [N];

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

/ ************************************************** *********************************************************** ******* * ************************************************** *********************************************************** *************** /

转载请注明原文地址:https://www.9cbs.com/read-132700.html

New Post(0)