跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

base64.js

107 行 · 4.2 KB历史下载
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5// From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
6
7import * as Log from './util/logging.js';
8
9export default {
10 /* Convert data (an array of integers) to a Base64 string. */
11 toBase64Table: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''),
12 base64Pad: '=',
13
14 encode(data) {
15 "use strict";
16 if (typeof data.toBase64 === 'function') {
17 return data.toBase64();
18 }
19 let result = '';
20 const length = data.length;
21 const lengthpad = (length % 3);
22 // Convert every three bytes to 4 ascii characters.
23
24 for (let i = 0; i < (length - 2); i += 3) {
25 result += this.toBase64Table[data[i] >> 2];
26 result += this.toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)];
27 result += this.toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)];
28 result += this.toBase64Table[data[i + 2] & 0x3f];
29 }
30
31 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
32 const j = length - lengthpad;
33 if (lengthpad === 2) {
34 result += this.toBase64Table[data[j] >> 2];
35 result += this.toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)];
36 result += this.toBase64Table[(data[j + 1] & 0x0f) << 2];
37 result += this.toBase64Table[64];
38 } else if (lengthpad === 1) {
39 result += this.toBase64Table[data[j] >> 2];
40 result += this.toBase64Table[(data[j] & 0x03) << 4];
41 result += this.toBase64Table[64];
42 result += this.toBase64Table[64];
43 }
44
45 return result;
46 },
47
48 /* Convert Base64 data to a string */
49 /* eslint-disable comma-spacing */
50 toBinaryTable: [
51 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
53 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
54 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
55 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
56 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
57 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
58 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
59 ],
60 /* eslint-enable comma-spacing */
61
62 decode(data, offset = 0) {
63 let dataLength = data.indexOf('=') - offset;
64 if (dataLength < 0) { dataLength = data.length - offset; }
65
66 /* Every four characters is 3 resulting numbers */
67 const resultLength = (dataLength >> 2) * 3 + Math.floor((dataLength % 4) / 1.5);
68 const result = new Array(resultLength);
69
70 // Convert one by one.
71
72 let leftbits = 0; // number of bits decoded, but yet to be appended
73 let leftdata = 0; // bits decoded, but yet to be appended
74 for (let idx = 0, i = offset; i < data.length; i++) {
75 const c = this.toBinaryTable[data.charCodeAt(i) & 0x7f];
76 const padding = (data.charAt(i) === this.base64Pad);
77 // Skip illegal characters and whitespace
78 if (c === -1) {
79 Log.Error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
80 continue;
81 }
82
83 // Collect data into leftdata, update bitcount
84 leftdata = (leftdata << 6) | c;
85 leftbits += 6;
86
87 // If we have 8 or more bits, append 8 bits to the result
88 if (leftbits >= 8) {
89 leftbits -= 8;
90 // Append if not padding.
91 if (!padding) {
92 result[idx++] = (leftdata >> leftbits) & 0xff;
93 }
94 leftdata &= (1 << leftbits) - 1;
95 }
96 }
97
98 // If there are any bits left, the base64 string was corrupted
99 if (leftbits) {
100 const err = new Error('Corrupted base64 string');
101 err.name = 'Base64-Error';
102 throw err;
103 }
104
105 return result;
106 }
107}; /* End of Base64 namespace */