1+ #include <Windows.h>
2+ #include <stdio.h>
3+ #include <string.h>
4+ #include <tchar.h>
5+ // 全局常量定义
6+ const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
7+ const char padding_char = '=' ;
8+
9+ /*编码代码
10+ * const unsigned char * sourcedata, 源数组
11+ * char * base64 ,码字保存
12+ */
13+ int Base64EncodeA (const unsigned char * sourcedata , char * base64 )
14+ {
15+ int i = 0 , j = 0 ;
16+ unsigned char trans_index = 0 ; // 索引是8位,但是高两位都为0
17+ const int datalength = strlen ((const char * )sourcedata );
18+ for (; i < datalength ; i += 3 ) {
19+ // 每三个一组,进行编码
20+ // 要编码的数字的第一个
21+ trans_index = ((sourcedata [i ] >> 2 ) & 0x3f );
22+ base64 [j ++ ] = base64char [(int )trans_index ];
23+ // 第二个
24+ trans_index = ((sourcedata [i ] << 4 ) & 0x30 );
25+ if (i + 1 < datalength ) {
26+ trans_index |= ((sourcedata [i + 1 ] >> 4 ) & 0x0f );
27+ base64 [j ++ ] = base64char [(int )trans_index ];
28+ }
29+ else {
30+ base64 [j ++ ] = base64char [(int )trans_index ];
31+
32+ base64 [j ++ ] = padding_char ;
33+
34+ base64 [j ++ ] = padding_char ;
35+
36+ break ; // 超出总长度,可以直接break
37+ }
38+ // 第三个
39+ trans_index = ((sourcedata [i + 1 ] << 2 ) & 0x3c );
40+ if (i + 2 < datalength ) { // 有的话需要编码2个
41+ trans_index |= ((sourcedata [i + 2 ] >> 6 ) & 0x03 );
42+ base64 [j ++ ] = base64char [(int )trans_index ];
43+
44+ trans_index = sourcedata [i + 2 ] & 0x3f ;
45+ base64 [j ++ ] = base64char [(int )trans_index ];
46+ }
47+ else {
48+ base64 [j ++ ] = base64char [(int )trans_index ];
49+
50+ base64 [j ++ ] = padding_char ;
51+
52+ break ;
53+ }
54+ }
55+
56+ base64 [j ] = '\0' ;
57+
58+ return 0 ;
59+ }
60+
61+ /** 在字符串中查询特定字符位置索引
62+ * const char *str ,字符串
63+ * char c,要查找的字符
64+ */
65+ inline int num_strchr (const char * str , char c ) //
66+ {
67+ const char * pindex = strchr (str , c );
68+ if (NULL == pindex ) {
69+ return -1 ;
70+ }
71+ return pindex - str ;
72+ }
73+ /* 解码
74+ * const char * base64 码字
75+ * unsigned char * dedata, 解码恢复的数据
76+ */
77+ int Base64DecodeA (const char * base64 , unsigned char * dedata )
78+ {
79+ int i = 0 , j = 0 ;
80+ int trans [4 ] = { 0 ,0 ,0 ,0 };
81+ for (; base64 [i ] != '\0' ; i += 4 ) {
82+ // 每四个一组,译码成三个字符
83+ trans [0 ] = num_strchr (base64char , base64 [i ]);
84+ trans [1 ] = num_strchr (base64char , base64 [i + 1 ]);
85+ // 1/3
86+ dedata [j ++ ] = ((trans [0 ] << 2 ) & 0xfc ) | ((trans [1 ] >> 4 ) & 0x03 );
87+
88+ if (base64 [i + 2 ] == '=' ) {
89+ continue ;
90+ }
91+ else {
92+ trans [2 ] = num_strchr (base64char , base64 [i + 2 ]);
93+ }
94+ // 2/3
95+ dedata [j ++ ] = ((trans [1 ] << 4 ) & 0xf0 ) | ((trans [2 ] >> 2 ) & 0x0f );
96+
97+ if (base64 [i + 3 ] == '=' ) {
98+ continue ;
99+ }
100+ else {
101+ trans [3 ] = num_strchr (base64char , base64 [i + 3 ]);
102+ }
103+
104+ // 3/3
105+ dedata [j ++ ] = ((trans [2 ] << 6 ) & 0xc0 ) | (trans [3 ] & 0x3f );
106+ }
107+
108+ dedata [j ] = '\0' ;
109+
110+ return 0 ;
111+ }
112+
113+
114+
115+
116+ int Base64EncodeW (WCHAR s [], WCHAR b64 [])
117+ {
118+ char * cs , * cb64 ;
119+ int len = WideCharToMultiByte (CP_ACP , 0 ,s ,_tcslen (s ),0 , 0 , 0 , 0 );
120+ cs = malloc (len + 2 );
121+ cb64 = malloc ((len + 2 )<<1 );//够保险了
122+ WideCharToMultiByte (CP_ACP , 0 , s , _tcslen (s ), cs , len , 0 , 0 );
123+ cs [len ] = 0 ;
124+ Base64EncodeA (cs , cb64 );
125+ int lenc64 = strlen (cb64 );
126+ MultiByteToWideChar (CP_ACP , 0 , cb64 , lenc64 , b64 , lenc64 );
127+ b64 [lenc64 ] = '\0' ;
128+ free (cs );
129+ free (cb64 );
130+ return 0 ;
131+ }
132+
133+ int Base64DecodeW (WCHAR b64 [], WCHAR s [])
134+ {
135+ char * cs , * cb64 ;
136+ int len = WideCharToMultiByte (CP_ACP , 0 , b64 , _tcslen (b64 ), 0 , 0 , 0 , 0 );
137+ cs = malloc (len + 2 );//够保险了
138+ cb64 = malloc (len + 2 );
139+ WideCharToMultiByte (CP_ACP , 0 , b64 , len , cb64 , len , 0 , 0 );
140+ cb64 [len ] = '\0' ;
141+ cb64 [len + 1 ] = '\0' ;
142+ Base64DecodeA (cb64 , cs );
143+ int lencs = strlen (cs );
144+ MultiByteToWideChar (CP_ACP , 0 , cs , lencs , s , lencs );
145+ s [lencs ] = '\0' ;
146+ free (cs );
147+ free (cb64 );
148+ return 0 ;
149+ }
0 commit comments