• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 #ifndef OHOS_SHARING_RTSP_URI_H
17 #define OHOS_SHARING_RTSP_URI_H
18 
19 #include <cstdint>
20 namespace OHOS {
21 namespace Sharing {
22 
23 static uint8_t alphabetMap[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 static uint8_t reverseMap[] = {
25     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
26     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62,
27     255, 255, 255, 63,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  255, 255, 255, 255, 255, 255, 255, 0,
28     1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,
29     23,  24,  25,  255, 255, 255, 255, 255, 255, 26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
30     39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  255, 255, 255, 255, 255};
31 
32 class Base64 {
33 public:
Encode(const char * text,uint32_t text_len,uint8_t * encode)34     static uint32_t Encode(const char *text, uint32_t text_len, uint8_t *encode)
35     {
36         uint32_t i = 0;
37         uint32_t j = 0;
38         for (i = 0, j = 0; i + 3 <= text_len; i += 3) { // 3: read offset
39             encode[j++] = alphabetMap[text[i] >> 2]; // 2: map to alphabetMap
40             encode[j++] = alphabetMap[((text[i] << 4) & 0x30) | (text[i + 1] >> 4)];  // 4: map to alphabetMap
41             encode[j++] = alphabetMap[((text[i + 1] << 2) & 0x3c) | (text[i + 2] >> 6)]; // 2: alphabetMap, 6: offset
42             encode[j++] = alphabetMap[text[i + 2] & 0x3f]; // 2: map to alphabetMap
43         }
44 
45         if (i < text_len) {
46             uint32_t tail = text_len - i;
47             if (tail == 1) {
48                 encode[j++] = alphabetMap[text[i] >> 2]; // 2: map to alphabetMap
49                 encode[j++] = alphabetMap[(text[i] << 4) & 0x30]; // 4: map to alphabetMap
50                 encode[j++] = '=';
51                 encode[j++] = '=';
52             } else {
53                 encode[j++] = alphabetMap[text[i] >> 2]; // 2: map to alphabetMap
54                 encode[j++] = alphabetMap[((text[i] << 4) & 0x30) | (text[i + 1] >> 4)]; // 4: map to alphabetMap
55                 encode[j++] = alphabetMap[(text[i + 1] << 2) & 0x3c]; // 2: map to alphabetMap
56                 encode[j++] = '=';
57             }
58         }
59 
60         return j;
61     }
62 
Decode(const char * code,uint32_t code_len,uint8_t * plain)63     static uint32_t Decode(const char *code, uint32_t code_len, uint8_t *plain)
64     {
65         if ((code_len & 0x03) != 0) {
66             return 0;
67         }
68 
69         uint32_t i;
70         uint32_t j = 0;
71         uint8_t quad[4];
72         for (i = 0; i < code_len; i += 4) { // 4: read offset
73             for (uint32_t k = 0; k < 4; k++) { // 4: read offset
74                 quad[k] = reverseMap[(int32_t)code[i + k]];
75             }
76 
77             if (quad[0] > 63 && quad[1] > 63) { // 63: limit
78                 break;
79             }
80 
81             plain[j++] = (quad[0] << 2) | (quad[1] >> 4); // 2: fix offset, 4: fix offset
82 
83             if (quad[2] >= 64) { // 2: fix offset, 64: limit
84                 break;
85             } else if (quad[3] >= 64) { // 3: fix offset, 64: limit
86                 plain[j++] = (quad[1] << 4) | (quad[2] >> 2); // 2: fix offset, 4: fix offset
87                 break;
88             } else {
89                 plain[j++] = (quad[1] << 4) | (quad[2] >> 2); // 2: fix offset, 4: fix offset
90                 plain[j++] = (quad[2] << 6) | quad[3]; // 6: fix offset, 4: fix offset, 2: fix offset, 3: fix offset
91             }
92         }
93 
94         return j;
95     }
96 };
97 
98 } // namespace Sharing
99 } // namespace OHOS
100 #endif // OHOS_SHARING_RTSP_SDP_H