1
1
const PATH_LENGTH = 7 ;
2
2
3
- export function decodeBase64 ( str ) {
4
- const binString = atob ( str ) ;
5
- const bytes = Uint8Array . from ( binString , ( m ) => m . codePointAt ( 0 ) ) ;
6
- return new TextDecoder ( ) . decode ( bytes ) ;
3
+
4
+ // Base64 编码函数
5
+ export function encodeBase64 ( input ) {
6
+ const encoder = new TextEncoder ( ) ;
7
+ const utf8Array = encoder . encode ( input ) ;
8
+ let binaryString = '' ;
9
+ for ( const byte of utf8Array ) {
10
+ binaryString += String . fromCharCode ( byte ) ;
11
+ }
12
+ return base64FromBinary ( binaryString ) ;
13
+ }
14
+
15
+ // Base64 解码函数
16
+ export function decodeBase64 ( input ) {
17
+ const binaryString = base64ToBinary ( input ) ;
18
+ const bytes = new Uint8Array ( binaryString . length ) ;
19
+ for ( let i = 0 ; i < binaryString . length ; i ++ ) {
20
+ bytes [ i ] = binaryString . charCodeAt ( i ) ;
21
+ }
22
+ const decoder = new TextDecoder ( ) ;
23
+ return decoder . decode ( bytes ) ;
7
24
}
8
- export function encodeBase64 ( str ) {
9
- return btoa ( str ) ;
25
+
26
+ // 将二进制字符串转换为 Base64(编码)
27
+ export function base64FromBinary ( binaryString ) {
28
+ const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
29
+ let base64String = '' ;
30
+ let padding = '' ;
31
+
32
+ const remainder = binaryString . length % 3 ;
33
+ if ( remainder > 0 ) {
34
+ padding = '=' . repeat ( 3 - remainder ) ;
35
+ binaryString += '\0' . repeat ( 3 - remainder ) ;
36
+ }
37
+
38
+ for ( let i = 0 ; i < binaryString . length ; i += 3 ) {
39
+ const bytes = [
40
+ binaryString . charCodeAt ( i ) ,
41
+ binaryString . charCodeAt ( i + 1 ) ,
42
+ binaryString . charCodeAt ( i + 2 )
43
+ ] ;
44
+ const base64Index1 = bytes [ 0 ] >> 2 ;
45
+ const base64Index2 = ( ( bytes [ 0 ] & 3 ) << 4 ) | ( bytes [ 1 ] >> 4 ) ;
46
+ const base64Index3 = ( ( bytes [ 1 ] & 15 ) << 2 ) | ( bytes [ 2 ] >> 6 ) ;
47
+ const base64Index4 = bytes [ 2 ] & 63 ;
48
+
49
+ base64String += base64Chars [ base64Index1 ] +
50
+ base64Chars [ base64Index2 ] +
51
+ base64Chars [ base64Index3 ] +
52
+ base64Chars [ base64Index4 ] ;
53
+ }
54
+
55
+ return base64String . slice ( 0 , base64String . length - padding . length ) + padding ;
10
56
}
11
57
58
+ // 将 Base64 转换为二进制字符串(解码)
59
+ export function base64ToBinary ( base64String ) {
60
+ const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
61
+ let binaryString = '' ;
62
+ base64String = base64String . replace ( / = + $ / , '' ) ; // 去掉末尾的 '='
63
+
64
+ for ( let i = 0 ; i < base64String . length ; i += 4 ) {
65
+ const bytes = [
66
+ base64Chars . indexOf ( base64String [ i ] ) ,
67
+ base64Chars . indexOf ( base64String [ i + 1 ] ) ,
68
+ base64Chars . indexOf ( base64String [ i + 2 ] ) ,
69
+ base64Chars . indexOf ( base64String [ i + 3 ] )
70
+ ] ;
71
+ const byte1 = ( bytes [ 0 ] << 2 ) | ( bytes [ 1 ] >> 4 ) ;
72
+ const byte2 = ( ( bytes [ 1 ] & 15 ) << 4 ) | ( bytes [ 2 ] >> 2 ) ;
73
+ const byte3 = ( ( bytes [ 2 ] & 3 ) << 6 ) | bytes [ 3 ] ;
74
+
75
+ if ( bytes [ 1 ] !== - 1 ) binaryString += String . fromCharCode ( byte1 ) ;
76
+ if ( bytes [ 2 ] !== - 1 ) binaryString += String . fromCharCode ( byte2 ) ;
77
+ if ( bytes [ 3 ] !== - 1 ) binaryString += String . fromCharCode ( byte3 ) ;
78
+ }
79
+
80
+ return binaryString ;
81
+ }
12
82
export function DeepCopy ( obj ) {
13
83
if ( obj === null || typeof obj !== 'object' ) {
14
84
return obj ;
@@ -92,4 +162,4 @@ export function parseServerInfo(serverInfo) {
92
162
...( params . host && { 'headers' : { 'host' : params . host } } ) ,
93
163
service_name : params . serviceName ?? undefined ,
94
164
} ;
95
- }
165
+ }
0 commit comments