-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.js
More file actions
201 lines (197 loc) · 6.06 KB
/
bytes.js
File metadata and controls
201 lines (197 loc) · 6.06 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// custom bytes
function Bytes(any) {
// const typeof_any = any === null ? 'NULL' : (typeof any);
const array = [];
if (Array.isArray(any)) {
any.map(Bytes).forEach(function (e) {
array.push(...String(e).split(/,/g));
});
array.forEach(function (value, index, array) {
array[index] = Number(value);
});
array.forEach(function (number) {
if (!Number.isSafeInteger(number) || number < 0) {
throw new Error(`only positive Safe Integers allowed. (${number}) is not a positive safe integer`);
}
});
} else if ((typeof any) === 'number') {
array.push(any);
} else {
array.push(...[...String(any)].map(function (self) {
return self.charCodeAt(0);
}));
}
const result = Bytes.to8BitArray(array);
if (!new.target) {
return result;
}
this.array = result;
}
Bytes.prototype.btoa = function () {
const self = this;
let binary = this.array.map(function (number) {
return Bytes.toBinaryString(number).padStart(8, '0');
});
binary = this.constructor.chunkString(binary.join(''), 6);
binary = binary.map(function (string) {
return string.join('').padEnd(6, '0');
}).join().replace(/,0{6}$/, '').split(/,/g);
binary = binary.map(function (sandboxedInteger) {
return self.constructor.b64Table[parseInt(sandboxedInteger, 2)];
});
return Bytes.padEvenly(binary.join(''), 4, '=');
};
Bytes.btoa = function (string) {
return (new Bytes(String(string))).btoa();
};
Bytes.atob = function (base64) {
base64 = String(base64);
if (base64.length % 4 !== 0) {
throw new Error(`Invalid length of base64 (${base64.length});`);
}
base64 = base64.replace(/=+$/, '');
const decodedBytes = [];
for (let i = 0; i < base64.length; i++) {
decodedBytes.push(Bytes.a64Table[base64[i]]);
}
let binaryString = decodedBytes.map(function (value) {
return Number(value).toString(2).padStart(6, '0');
}).join('');
const outputBytes = [];
while (binaryString.length >= 8) {
outputBytes.push(parseInt(binaryString.slice(0, 8), 2));
binaryString = binaryString.slice(8);
}
return new Bytes(outputBytes);
};
Bytes.prototype.toCodepointString = function () {
return String.fromCharCode(...this.array);
};
Bytes.prototype.hexDump = function () {
return this.array.map(function (element) {
return Number(element).toString(16);
}).join();
};
Bytes.toBinaryString = function (number) {
if (isNaN(number)) {
throw new Error('number is NaN');
}
number = Math.abs(Number(number));
const binary = [];
while (number > 0) {
binary.push(number % 2);
number = Math.floor(number / 2);
}
return binary.reverse().join().replace(/\D/g, '');
}
Bytes.chunkString = function (string, number) {
string = String(string);
number = Number(number);
const array = [[]];
Bytes.assert(number === number, 'number is NaN');
let index = 0, indexedArray = 0;
for (const string1 of Array.from(string)) {
array[indexedArray].push(string1);
if (++index % number === 0) {
array[++indexedArray] = [];
}
}
return array;
};
Bytes.chunk = function (item, number) {
number = Number(number);
const array = [[]];
Bytes.assert(number === number, 'number is NaN');
let index = 0, indexedArray = 0;
for (const string1 of item) {
array[indexedArray].push(string1);
if (++index % number === 0) {
array[++indexedArray] = [];
}
}
return array;
};
Bytes.to8BitArray = function (numbers) {
const result = [];
for (let num of numbers) {
num = Number(num);
const bytes = [];
while (num > 0) {
bytes.push(num & 0xFF);
num = num >> 8;
}
result.push(...bytes.reverse());
}
return result;
};
Bytes.assert = function (boolean, error) {
if (!boolean) {
throw new Error(error);
}
};
Bytes.isPrimitive = function (o) {
if (o === null) return true;
switch (typeof o) {
case "object":
case "function":
return false;
default:
return true;
}
};
Bytes.toPrimitive = function (O) {
if (Bytes.isPrimitive(O)) return O;
let x = Object(O);
let n = 0;
while (!Bytes.isPrimitive(x)) {
const o = Object(x);
if (Symbol.toPrimitive in o) {
const result = o[Symbol.toPrimitive]('default');
if (Bytes.isPrimitive(result)) x = result;
}
if ('valueOf' in o) {
const result = o.valueOf();
if (Bytes.isPrimitive(result)) x = result;
}
if ('toString' in o) {
const result = o.toString();
if (Bytes.isPrimitive(result)) x = result;
}
if ((++n) > 800) throw new Error('precaution recursion');
}
return x;
};
Bytes.B64Table = function () {
const B64Table = {};
for (let x = 0; x < 64; x++) {
if (x < 26) {
B64Table[x] = String.fromCharCode(x + 65);
} else if (x < 52) {
B64Table[x] = String.fromCharCode((x - 26) + 97);
} else if (x < 62) {
B64Table[x] = String.fromCharCode((x - 52) + 48);
} else if (x === 62) {
B64Table[x] = '+';
} else if (x === 63) {
B64Table[x] = '/';
}
}
return B64Table;
};
Bytes.a64Table = (function (object) {
const a64Table = {};
for (const [x, entry] of Object.entries(object)) {
a64Table[entry] = x;
}
return a64Table;
})(Bytes.b64Table = Bytes.B64Table());
Bytes.padEvenly = function (string, multipleOf, padWith = '=') {
string = String(string);
multipleOf = Number(multipleOf);
if (Number.isNaN(multipleOf) || multipleOf <= 0) {
throw new Error("multipleOf must be a positive number");
}
let remainder = string.length % multipleOf;
let padLength = remainder === 0 ? 0 : multipleOf - remainder;
return string + String(padWith).repeat(padLength);
};