-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenPGP-v5.3.0.js2gs.diff
94 lines (91 loc) · 4.49 KB
/
OpenPGP-v5.3.0.js2gs.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--- OpenPGP-v5.3.0.js 2022-06-10 22:40:00.653227017 +0200
+++ OpenPGP-v5.3.0.gs 2022-06-10 22:40:00.645227073 +0200
@@ -1688,12 +1688,21 @@
* @returns {Uint8Array|ReadableStream} A valid squence of utf8 bytes.
*/
encodeUTF8: function (str) {
- const encoder = new TextEncoder('utf-8');
- // eslint-disable-next-line no-inner-declarations
- function process(value, lastChunk = false) {
- return encoder.encode(value, { stream: !lastChunk });
+ // [OpenPGP.gs] There's no TextEncoder class in Google Apps Script
+ if (typeof TextEncoder === 'undefined') {
+ // eslint-disable-next-line no-inner-declarations
+ function process(value, lastChunk = false) {
+ return util.stringToUint8Array(value); // FIXME
+ }
+ return transform(str, process, () => process('', true));
+ } else {
+ const encoder = new TextEncoder('utf-8');
+ // eslint-disable-next-line no-inner-declarations
+ function process(value, lastChunk = false) {
+ return encoder.encode(value, { stream: !lastChunk });
+ }
+ return transform(str, process, () => process('', true));
}
- return transform(str, process, () => process('', true));
},
/**
@@ -1702,12 +1711,21 @@
* @returns {String|ReadableStream} A native javascript string.
*/
decodeUTF8: function (utf8) {
- const decoder = new TextDecoder('utf-8');
- // eslint-disable-next-line no-inner-declarations
- function process(value, lastChunk = false) {
- return decoder.decode(value, { stream: !lastChunk });
+ // [OpenPGP.gs] There's no TextDecoder class in Google Apps Script
+ if (typeof TextDecoder === 'undefined') {
+ // eslint-disable-next-line no-inner-declarations
+ function process(value, lastChunk = false) {
+ return util.uint8ArrayToString(value); // FIXME
+ }
+ return transform(utf8, process, () => process(new Uint8Array(), true));
+ } else {
+ const decoder = new TextDecoder('utf-8');
+ // eslint-disable-next-line no-inner-declarations
+ function process(value, lastChunk = false) {
+ return decoder.decode(value, { stream: !lastChunk });
+ }
+ return transform(utf8, process, () => process(new Uint8Array(), true));
}
- return transform(utf8, process, () => process(new Uint8Array(), true));
},
/**
@@ -2116,8 +2134,35 @@
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
};
} else {
- encodeChunk = buf => btoa(util.uint8ArrayToString(buf));
- decodeChunk = str => util.stringToUint8Array(atob(str));
+ // [OpenPGP.gs] There's no btoa() or atob() in Google Apps Script
+ if (typeof btoa === 'undefined') {
+ encodeChunk = function(input) {
+ const base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ input = util.uint8ArrayToString(input);
+ for (var output = "", i = 0; i < input.length; i += 3) {
+ const inBytes = input.slice(i, i+3);
+ output += inBytes.length < 0 ? "=" : base64chars.charAt(inBytes.charCodeAt(0) >> 2);
+ output += inBytes.length < 1 ? "=" : base64chars.charAt(((inBytes.charCodeAt(0) & 3) << 4) | (inBytes.charCodeAt(1) >> 4));
+ output += inBytes.length < 2 ? "=" : base64chars.charAt(((inBytes.charCodeAt(1) & 15) << 2) | (inBytes.charCodeAt(2) >> 6));
+ output += inBytes.length < 3 ? "=" : base64chars.charAt(inBytes.charCodeAt(2) & 63);
+ }
+ return output;
+ };
+ decodeChunk = function(input) {
+ const base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
+ for (var output = "", i = 0; i < input.length; i += 4) {
+ const inBytes = input.slice(i ,i+4);
+ if (inBytes[1] != '=') output += String.fromCharCode((base64chars.indexOf(inBytes[0]) << 2) | (base64chars.indexOf(inBytes[1]) >> 4));
+ if (inBytes[2] != '=') output += String.fromCharCode(((base64chars.indexOf(inBytes[1]) & 15) << 4) | (base64chars.indexOf(inBytes[2]) >> 2));
+ if (inBytes[3] != '=') output += String.fromCharCode(((base64chars.indexOf(inBytes[2]) & 3) << 6) | base64chars.indexOf(inBytes[3]));
+ }
+ return util.stringToUint8Array(output);
+ };
+ } else {
+ encodeChunk = buf => btoa(util.uint8ArrayToString(buf));
+ decodeChunk = str => util.stringToUint8Array(atob(str));
+ }
}
/**