-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-encode.js
245 lines (210 loc) · 6.08 KB
/
string-encode.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Convert different types of JavaScript String to/from Uint8Array.
*
* @author Dumitru Uzun (DUzun.Me)
* @version 0.2.2
*/
/*requires Uint8Array*/
/*globals escape, unescape, encodeURI, decodeURIComponent, btoa*/
export const chr = String.fromCharCode;
export function ord(chr) {
return chr.charCodeAt(0);
}
export function buffer2bin(buf) {
buf = view8(buf);
return chr.apply(String, buf);
}
/**
* Get the hex representation of a buffer (TypedArray)
*
* @requires String.prototype.padStart()
*
* @param {TypedArray} buf Uint8Array is desirable, cause it is consistent regardless of the endianness
*
* @return {String} The hex representation of the buf
*/
export function buffer2hex(buf) {
const bpe = buf.BYTES_PER_ELEMENT << 1;
return buf.reduce((r, c) => r += (c >>> 0).toString(16).padStart(bpe,'0'), '');
}
export function buffer2str(buf, asUtf8) {
if(typeof buf == 'string') return buf;
buf = buffer2bin(buf);
if (asUtf8 !== false && !isASCII(buf)) {
if(asUtf8) {
buf = utf8Decode(buf);
} else if(asUtf8 == undefined) {
try {
buf = utf8Decode(buf);
} catch(err) {}
}
}
return buf;
}
export function str2buffer(str, asUtf8) {
str = String(str);
if(asUtf8 == undefined) {
// Some guessing
asUtf8 = hasMultibyte(str); // || !isASCII(str)
}
if (asUtf8) {
str = utf8Encode(str);
}
// Smaller x2
// return new Uint8Array(String(str).split('').map(ord));
// Faster x3-4
let len = str.length;
const buf = new Uint8Array(len);
while(len--) buf[len] = str.charCodeAt(len);
return buf;
}
const nonHexDigitRE = /[^0-9a-f]/g;
/**
* Read a hex string into a buffer (Uint8Array), ignoring non-hex chars.
*
* @param {String} str
*
* @return {Uint8Array}
*/
export function hex2buffer(str) {
str = str.replace(nonHexDigitRE, '');
const len = str.length;
const ret = new Uint8Array((len + 1) >>> 1);
for(let i=0; i<len; i+=2) {
ret[i >>> 1] = parseInt(str.slice(i, i+2), 16);
}
return ret;
}
/**
* This method is a replacement of Buffer.toString(enc)
* for Browser, where Buffer is not available.
*
* @requires btoa
*
* @this {Uint8Array}
*
* @param {String} enc 'binary' | 'hex' | 'base64' | 'utf8' | undefined
*
* @return {String}
*/
export function toString(enc) {
// The Node.js equivalent would be something like:
// if(typeof Buffer == 'function') {
// if(enc === false) enc = 'binary';
// if(enc === true) enc = 'utf8';
// return Buffer.from(this.buffer, this.byteOffset, this.byteLength).toString(enc);
// }
switch(enc) {
case false:
case 'binary': return buffer2bin(this);
case 'hex': return buffer2hex(this);
case 'base64': return btoa(buffer2bin(this));
case 'utf8': enc = true; break;
}
return buffer2str(this, enc);
}
export function view8(buf, start, len) {
// If buf is a Buffer, we still want to make it an Uint8Array
if(!start && !len && buf instanceof Uint8Array && !buf.copy) return buf;
start = start >>> 0;
if(len == undefined) len = buf.byteLength - start;
return new Uint8Array(buf.buffer, buf.byteOffset+start, len);
}
let _isLittleEndian;
export function isLittleEndian() {
if(_isLittleEndian !== undefined) return _isLittleEndian;
_isLittleEndian = !!(new Uint8Array(new Uint16Array([1]).buffer)[0]);
isLittleEndian = () => _isLittleEndian;
return _isLittleEndian;
}
export function switchEndianness32(i) {
return (i&0xFF)<<24
| (i&0xFF00)<<8
| i>>8&0xFF00
| i>>24&0xFF
;
}
export function guessEncoding(str) {
if(hasMultibyte(str)) return 'mb';
// @todo: test which is faster, utf8bytes() or RegExp
if(isHEX(str)) return 'hex';
// if(isASCII(str)) return 'ascii';
// if(isUTF8(str)) return 'utf8';
let mbLen = utf8bytes(str);
if(mbLen) return 'utf8';
if(mbLen === 0) return 'ascii';
if(mbLen === false) {
mbLen = utf8bytes(str, true);
if(mbLen) return '~utf8'; // UTF8, but with async characters at the edges
}
return 'binary';
}
const hasMultibyteRE = /([^\x00-\xFF])/;
const isHEXRE = /^[0-9a-f\s]*$/i;
const isASCIIRE = /^[\x00-\x7F]*$/;
const isUTF8RE = /^(?:[\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]|[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])*$/;
export function hasMultibyte(str) {
let m = hasMultibyteRE.exec(str);
return m ? m[1] : false;
}
export function isBinary(str) {
return !hasMultibyte(str);
}
export function isASCII(str) {
return isASCIIRE.test(str);
}
export function isUTF8(str) {
return isUTF8RE.test(str);
}
export function isHEX(str) {
return isHEXRE.test(str);
}
export function utf8bytes(str, allowAsyncChars) {
var l = str.length
, i = 0
, u = 0
, c
, a = -1
, asy = +!!allowAsyncChars
;
for( ; i < l ; ) {
c = str.charCodeAt(i++);
if( c < 0x80 ) continue; // ASCII
if( 0xFF <= c ) return false; // has multi-byte
// async UTF8 character
if( (c & 0xC0) == 0x80 ) {
// Ignore async UTF8 characters at the beginning
if(asy == i) {
++u;
++asy;
continue;
}
return false;
}
// Check sync UTF8 bytes
a = (c & 0xE0) !== 0xC0
? (c & 0xF0) !== 0xE0
? (c & 0xF8) !== 0xF0
? false
: 3
: 2
: 1
;
if(!a) return false; // Not an ASCII, nor sync UTF8 bytes
for( ; (u += 1) && a-- && i < l; ) {
c = str.charCodeAt(i++);
if((c & 0xC0) !== 0x80) {
return false; // Not an ASCII, nor sync UTF8 bytes
}
}
}
// Ignore async UTF8 characters at the end
if(~a && !allowAsyncChars) return false;
return u;
}
export function utf8Encode(str) {
return unescape(encodeURI(str));
}
export function utf8Decode(str) {
return decodeURIComponent(escape(str));
}