• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #include "base64.h"
17 
18 static uint8 alphabet_map[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
19 static uint8 reverse_map[] =
20 {
21 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
22 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
23 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
24 	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
25 	255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
26 	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
27 	255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
28 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
29 };
30 
base64_decode(const uint8 * code,uint32 code_len,char * str)31 int base64_decode(const uint8* code, uint32 code_len, char* str)
32 {
33 	uint8 plain[1024];
34 	assert((code_len & 0x03) == 0); // 如果它的条件返回错误,则终止程序执行。4的倍数。
35 
36 	uint32 i, j = 0;
37 	uint8 quad[4];
38 	for (i = 0; i < code_len; i += 4)
39 	{
40 		for (uint32 k = 0; k < 4; k++)
41 		{
42 			quad[k] = reverse_map[code[i + k]]; // 分组,每组四个分别依次转换为base64表内的十进制数
43 		}
44 
45 		assert(quad[0] < 64 && quad[1] < 64);
46 
47 		plain[j++] = (quad[0] << 2) | (quad[1] >> 4); // 取出第一个字符对应base64表的十进制数的前6位与第二个字符对应base64表的十进制数的前2位进行组合
48 
49 		if (quad[2] >= 64)
50 			break;
51 		else if (quad[3] >= 64)
52 		{
53 			plain[j++] = (quad[1] << 4) | (quad[2] >> 2); // 取出第二个字符对应base64表的十进制数的后4位与第三个字符对应base64表的十进制数的前4位进行组合
54 			break;
55 		}
56 		else
57 		{
58 			plain[j++] = (quad[1] << 4) | (quad[2] >> 2);
59 			plain[j++] = (quad[2] << 6) | quad[3]; // 取出第三个字符对应base64表的十进制数的后2位与第4个字符进行组合
60 		}
61 	}
62 	plain[j] = 0;
63 	strcpy(str, (char*)plain);
64 	return j;
65 }
66