Skip to content

Commit 1ef9b42

Browse files
committed
feat: base64 解码时支持二进制模式
1 parent ff002ea commit 1ef9b42

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

Workflow/decode.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

Workflow/info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
<key>variablesdontexport</key>
245245
<array/>
246246
<key>version</key>
247-
<string>1.0.0</string>
247+
<string>1.0.1</string>
248248
<key>webaddress</key>
249249
<string>https://github.com/CYJB/Alfred-Workflow-Encode_Decode</string>
250250
</dict>

Workflow/utils/base64.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function encodeUTF8(char, output) {
5959
/**
6060
* 解码指定 Base64 字符串,与 atob 等价。
6161
*/
62-
exports.decode = function (input) {
62+
exports.decode = function (input, utf8) {
6363
// 忽略空白字符 http://whatwg.org/html/common-microsyntaxes.html#space-character
6464
input = input.replace(/[\t\n\f\r ]/g, '');
6565
let len = input.length;
@@ -79,7 +79,12 @@ exports.decode = function (input) {
7979
char = TABLE.indexOf(input.charAt(i));
8080
value = bitCounter % 4 ? (value << 6) + char : char;
8181
if (bitCounter++ % 4) {
82-
decodeUTF8(0xFF & value >> (-2 * bitCounter & 6), output);
82+
const ch = 0xFF & value >> (-2 * bitCounter & 6);
83+
if (utf8) {
84+
decodeUTF8(ch, output);
85+
} else {
86+
output.push(String.fromCharCode(ch));
87+
}
8388
}
8489
}
8590
return output.join('');

0 commit comments

Comments
 (0)