C Implementation of base64 decoding and urlenocde encoding and decoding

Source: Internet
Author: User

Author: jsufcz


/* Base64 encoding function */

Int base64 (char * s, char * D)
{
Char charset [64] = {
'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', '+ ','/'};
Unsigned char in [3];
Unsigned char out [4];
Int CNT = 0;

If (! S |! D) return 0;

For (; * s! = 0 ;)
{
If (CNT + 4> 76)
{
* D ++ = '/N ';
CNT = 0;
}
If (strlen (s)> = 3)
{
In [0] = * s;
In [1] = * (S + 1 );
In [2] = * (S + 2 );
Out [0] = in [0]> 2;
Out [1] = (in [0] & 0x03) <4 | (in [1] & 0xf0)> 4;
Out [2] = (in [1] & 0x0f) <2 | (in [2] & 0xc0)> 6;
Out [3] = in [2] & 0x3f;
* D = charset [out [0];
* (D + 1) = charset [out [1];
* (D + 2) = charset [out [2];
* (D + 3) = charset [out [3];
S + = 3;
D + = 4;
}
Else if (strlen (S) = 1)
{
In [0] = * s;
Out [0] = in [0]> 2;
Out [1] = (in [0] & 0x03) <4 | 0;
* D = charset [out [0];
* (D + 1) = charset [out [1];
* (D + 2) = ';
* (D + 3) = ';
S + = 1;
D + = 4;
}
Else if (strlen (S) = 2)
{
In [0] = * s;
In [1] = * (S + 1 );
Out [0] = in [0]> 2;
Out [1] = (in [0] & 0x03) <4 | (in [1] & 0xf0)> 4;
Out [2] = (in [1] & 0x0f) <2 | 0;
* D = charset [out [0];
* (D + 1) = charset [out [1];
* (D + 2) = charset [out [2];
* (D + 3) = ';
S + = 2;
D + = 4;
}
CNT + = 4;
}
* D = '/0 ';
Return 1;
}

/* Base64 decoding function */

Int unbase64char (char ch)
{
Char charset [64] = {
'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', '+ ','/'};
Int I;
For (I = 0; I <= 63; I ++) if (charset [I] = CH) break;
Return I;
}

Int unbase64 (char * s, char * D)
{
Unsigned char in [4];
Unsigned char out [3];

If (! S |! D) return 0;

For (; * s! = 0 ;)
{
If (* s = '/N') s ++;
In [0] = s [0];
In [1] = s [1];
In [2] = s [2];
In [3] = s [3];
If (in [2]! = '& In [3]! = ')
{
In [0] = unbase64char (in [0]);
In [1] = unbase64char (in [1]);
In [2] = unbase64char (in [2]);
In [3] = unbase64char (in [3]);
Out [0] = in [0] <2 | (in [1] & 0x30)> 4;
Out [1] = (in [1] & 0x0f) <4 | (in [2] & 0x3c)> 2;
Out [2] = (in [2] & 0x03) <6 | in [3] & 0x3f;
D [0] = out [0];
D [1] = out [1];
D [2] = out [2];
S + = 4;
D + = 3;
}
Else if (in [2] = '& in [3] = ')
{
In [0] = unbase64char (in [0]);
In [1] = unbase64char (in [1]);
Out [0] = in [0] <2 | (in [1] & 0x30)> 4;
D [0] = out [0];
S + = 4;
D + = 1;
}
Else if (in [2]! = '& In [3] = ')
{
In [0] = unbase64char (in [0]);
In [1] = unbase64char (in [1]);
In [2] = unbase64char (in [2]);
Out [0] = in [0] <2 | (in [1] & 0x30)> 4;
Out [1] = (in [1] & 0x0f) <4 | (in [2] & 0x3c)> 2;
D [0] = out [0];
D [1] = out [1];
S + = 4;
D + = 2;
}
}
* D = '/0 ';
Return 1;
}

/* Unencode URL encoding function */
/*
Note that when processing Chinese characters, the compiler automatically reads
Or two characters. At this time, the unsigned char * can be forcibly used to read a character. You can check
Usage of the surface ist ()
*/

Int ist (char ch)
{
// Unsigned char * P = (unsigned char *) & Ch;
// If (* P = ''| * P = '%' | * P = '. '| * P ='/'| * P> 126) return 1;
// Else return 0;
If (CH = ''| CH = '%' | CH = '. '| CH ='/'| ch & 0x80) return 1;
Else return 0;
}

Int encode (char * s, char * D)
{
If (! S |! D) return 0;
For (; * s! = 0; s ++)
{
Unsigned char * P = (unsigned char *) S;
If (* P = '')
{
* D = '+ ';
D ++;
}
Else if (IST (* p ))
{
Char A [3];
* D = '% ';
Sprintf (a, "% 02x", * P );
* (D + 1) = A [0];
* (D + 2) = A [1];
D + = 3;
}
Else
{
* D = * P;
D ++;
}
}
* D = 0;
Return 1;
}

/* Unencode URL Decoding function */

Int unencode (char * s, char * D)
{
If (! S |! D) return 0;
For (; * s! = 0; s ++)
{
If (* s = '+ ')
{
* D = '';
D ++;
}
Else if (* s = '% ')
{
Int code;
If (sscanf (S + 1, "% 02x", & code )! = 1) code = '? ';
* D = code;
S + = 2;
D ++;
}
Else
{
* D = * s;
D ++;
}
}
* D = 0;
Return 1;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.