@@ -54,16 +54,30 @@ function run(argv) {
5454 } ) ;
5555 }
5656 try {
57- const base64Decode = decodeBase64 ( text ) ;
57+ const base64Decode = decodeBase64 ( text , true ) ;
5858 items . push ( {
5959 uid : 'Base64' ,
6060 icon : {
6161 path : './icons/Decode_Base64.png' ,
6262 } ,
63- title : base64Decode ,
63+ title : encodeControl ( base64Decode ) ,
6464 subtitle : '解码 Base64' ,
6565 arg : base64Decode ,
6666 } ) ;
67+ // 检查是否存在不可见字符,范围为 Cc(排除 \b\t\n\v\f\r)、Cf、Cn、Cs、M 和 Z。
68+ if ( / [ \x00 - \x07 \x0E - \x1F \x7F - \x9F ] | \p{ Cf} | \p{ Cn} | \p{ Cs} | \p{ M} | \p{ Z} / u. test ( base64Decode ) ) {
69+ // 需要重新解码一次,不再处理 UTF-8
70+ let binaryDecode = encodeBinary ( decodeBase64 ( text , false ) ) ;
71+ items . push ( {
72+ uid : 'Base64_2' ,
73+ icon : {
74+ path : './icons/Decode_Base64.png' ,
75+ } ,
76+ title : binaryDecode ,
77+ subtitle : '解码 Base64(二进制)' ,
78+ arg : binaryDecode ,
79+ } ) ;
80+ }
6781 } catch ( ignored ) { }
6882 if ( items . length === 0 ) {
6983 items . push ( {
@@ -75,3 +89,38 @@ function run(argv) {
7589 }
7690 return JSON . stringify ( { items : items } ) ;
7791}
92+
93+ /**
94+ * 控制字符的映射表。
95+ */
96+ const controlMap = {
97+ '\b' : '\\b' ,
98+ '\t' : '\\t' ,
99+ '\n' : '\\n' ,
100+ '\v' : '\\v' ,
101+ '\f' : '\\f' ,
102+ '\r' : '\\r' ,
103+ } ;
104+
105+ /**
106+ * 编码部分控制字符 \b\t\n\v\f\r。
107+ * @param {string } str 要编码的字符串。
108+ */
109+ function encodeControl ( str ) {
110+ return str . replace ( / [ \b \t \n \v \f \r ] / g, ch => controlMap [ ch ] ) ;
111+ }
112+
113+ /**
114+ * 编码为二进制。
115+ * @param {string } str 要编码的字符串。
116+ */
117+ function encodeBinary ( str ) {
118+ const result = [ ] ;
119+ for ( let i = 0 ; i < str . length ; i ++ ) {
120+ if ( i > 0 && i % 8 === 0 ) {
121+ result . push ( ' ' ) ;
122+ }
123+ result . push ( str . charCodeAt ( i ) . toString ( 16 ) . padStart ( 2 , '0' ) ) ;
124+ }
125+ return result . join ( '' ) ;
126+ }
0 commit comments