1
|
/*!
|
2
|
* The buffer module from node.js, for the browser.
|
3
|
*
|
4
|
* @author Feross Aboukhadijeh <http://feross.org>
|
5
|
* @license MIT
|
6
|
*/
|
7
|
/* eslint-disable no-proto */
|
8
|
|
9
|
'use strict'
|
10
|
|
11
|
var base64 = require('base64-js')
|
12
|
var ieee754 = require('ieee754')
|
13
|
var isArray = require('isarray')
|
14
|
|
15
|
exports.Buffer = Buffer
|
16
|
exports.SlowBuffer = SlowBuffer
|
17
|
exports.INSPECT_MAX_BYTES = 50
|
18
|
|
19
|
/**
|
20
|
* If `Buffer.TYPED_ARRAY_SUPPORT`:
|
21
|
* === true Use Uint8Array implementation (fastest)
|
22
|
* === false Use Object implementation (most compatible, even IE6)
|
23
|
*
|
24
|
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
|
25
|
* Opera 11.6+, iOS 4.2+.
|
26
|
*
|
27
|
* Due to various browser bugs, sometimes the Object implementation will be used even
|
28
|
* when the browser supports typed arrays.
|
29
|
*
|
30
|
* Note:
|
31
|
*
|
32
|
* - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
|
33
|
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
|
34
|
*
|
35
|
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
|
36
|
*
|
37
|
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
|
38
|
* incorrect length in some situations.
|
39
|
|
40
|
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
|
41
|
* get the Object implementation, which is slower but behaves correctly.
|
42
|
*/
|
43
|
Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
|
44
|
? global.TYPED_ARRAY_SUPPORT
|
45
|
: typedArraySupport()
|
46
|
|
47
|
/*
|
48
|
* Export kMaxLength after typed array support is determined.
|
49
|
*/
|
50
|
exports.kMaxLength = kMaxLength()
|
51
|
|
52
|
function typedArraySupport () {
|
53
|
try {
|
54
|
var arr = new Uint8Array(1)
|
55
|
arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
|
56
|
return arr.foo() === 42 && // typed array instances can be augmented
|
57
|
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
|
58
|
arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
|
59
|
} catch (e) {
|
60
|
return false
|
61
|
}
|
62
|
}
|
63
|
|
64
|
function kMaxLength () {
|
65
|
return Buffer.TYPED_ARRAY_SUPPORT
|
66
|
? 0x7fffffff
|
67
|
: 0x3fffffff
|
68
|
}
|
69
|
|
70
|
function createBuffer (that, length) {
|
71
|
if (kMaxLength() < length) {
|
72
|
throw new RangeError('Invalid typed array length')
|
73
|
}
|
74
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
75
|
// Return an augmented `Uint8Array` instance, for best performance
|
76
|
that = new Uint8Array(length)
|
77
|
that.__proto__ = Buffer.prototype
|
78
|
} else {
|
79
|
// Fallback: Return an object instance of the Buffer class
|
80
|
if (that === null) {
|
81
|
that = new Buffer(length)
|
82
|
}
|
83
|
that.length = length
|
84
|
}
|
85
|
|
86
|
return that
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* The Buffer constructor returns instances of `Uint8Array` that have their
|
91
|
* prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
|
92
|
* `Uint8Array`, so the returned instances will have all the node `Buffer` methods
|
93
|
* and the `Uint8Array` methods. Square bracket notation works as expected -- it
|
94
|
* returns a single octet.
|
95
|
*
|
96
|
* The `Uint8Array` prototype remains unmodified.
|
97
|
*/
|
98
|
|
99
|
function Buffer (arg, encodingOrOffset, length) {
|
100
|
if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
|
101
|
return new Buffer(arg, encodingOrOffset, length)
|
102
|
}
|
103
|
|
104
|
// Common case.
|
105
|
if (typeof arg === 'number') {
|
106
|
if (typeof encodingOrOffset === 'string') {
|
107
|
throw new Error(
|
108
|
'If encoding is specified then the first argument must be a string'
|
109
|
)
|
110
|
}
|
111
|
return allocUnsafe(this, arg)
|
112
|
}
|
113
|
return from(this, arg, encodingOrOffset, length)
|
114
|
}
|
115
|
|
116
|
Buffer.poolSize = 8192 // not used by this implementation
|
117
|
|
118
|
// TODO: Legacy, not needed anymore. Remove in next major version.
|
119
|
Buffer._augment = function (arr) {
|
120
|
arr.__proto__ = Buffer.prototype
|
121
|
return arr
|
122
|
}
|
123
|
|
124
|
function from (that, value, encodingOrOffset, length) {
|
125
|
if (typeof value === 'number') {
|
126
|
throw new TypeError('"value" argument must not be a number')
|
127
|
}
|
128
|
|
129
|
if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
|
130
|
return fromArrayBuffer(that, value, encodingOrOffset, length)
|
131
|
}
|
132
|
|
133
|
if (typeof value === 'string') {
|
134
|
return fromString(that, value, encodingOrOffset)
|
135
|
}
|
136
|
|
137
|
return fromObject(that, value)
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
|
142
|
* if value is a number.
|
143
|
* Buffer.from(str[, encoding])
|
144
|
* Buffer.from(array)
|
145
|
* Buffer.from(buffer)
|
146
|
* Buffer.from(arrayBuffer[, byteOffset[, length]])
|
147
|
**/
|
148
|
Buffer.from = function (value, encodingOrOffset, length) {
|
149
|
return from(null, value, encodingOrOffset, length)
|
150
|
}
|
151
|
|
152
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
153
|
Buffer.prototype.__proto__ = Uint8Array.prototype
|
154
|
Buffer.__proto__ = Uint8Array
|
155
|
if (typeof Symbol !== 'undefined' && Symbol.species &&
|
156
|
Buffer[Symbol.species] === Buffer) {
|
157
|
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
|
158
|
Object.defineProperty(Buffer, Symbol.species, {
|
159
|
value: null,
|
160
|
configurable: true
|
161
|
})
|
162
|
}
|
163
|
}
|
164
|
|
165
|
function assertSize (size) {
|
166
|
if (typeof size !== 'number') {
|
167
|
throw new TypeError('"size" argument must be a number')
|
168
|
} else if (size < 0) {
|
169
|
throw new RangeError('"size" argument must not be negative')
|
170
|
}
|
171
|
}
|
172
|
|
173
|
function alloc (that, size, fill, encoding) {
|
174
|
assertSize(size)
|
175
|
if (size <= 0) {
|
176
|
return createBuffer(that, size)
|
177
|
}
|
178
|
if (fill !== undefined) {
|
179
|
// Only pay attention to encoding if it's a string. This
|
180
|
// prevents accidentally sending in a number that would
|
181
|
// be interpretted as a start offset.
|
182
|
return typeof encoding === 'string'
|
183
|
? createBuffer(that, size).fill(fill, encoding)
|
184
|
: createBuffer(that, size).fill(fill)
|
185
|
}
|
186
|
return createBuffer(that, size)
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Creates a new filled Buffer instance.
|
191
|
* alloc(size[, fill[, encoding]])
|
192
|
**/
|
193
|
Buffer.alloc = function (size, fill, encoding) {
|
194
|
return alloc(null, size, fill, encoding)
|
195
|
}
|
196
|
|
197
|
function allocUnsafe (that, size) {
|
198
|
assertSize(size)
|
199
|
that = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
|
200
|
if (!Buffer.TYPED_ARRAY_SUPPORT) {
|
201
|
for (var i = 0; i < size; ++i) {
|
202
|
that[i] = 0
|
203
|
}
|
204
|
}
|
205
|
return that
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
|
210
|
* */
|
211
|
Buffer.allocUnsafe = function (size) {
|
212
|
return allocUnsafe(null, size)
|
213
|
}
|
214
|
/**
|
215
|
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
|
216
|
*/
|
217
|
Buffer.allocUnsafeSlow = function (size) {
|
218
|
return allocUnsafe(null, size)
|
219
|
}
|
220
|
|
221
|
function fromString (that, string, encoding) {
|
222
|
if (typeof encoding !== 'string' || encoding === '') {
|
223
|
encoding = 'utf8'
|
224
|
}
|
225
|
|
226
|
if (!Buffer.isEncoding(encoding)) {
|
227
|
throw new TypeError('"encoding" must be a valid string encoding')
|
228
|
}
|
229
|
|
230
|
var length = byteLength(string, encoding) | 0
|
231
|
that = createBuffer(that, length)
|
232
|
|
233
|
var actual = that.write(string, encoding)
|
234
|
|
235
|
if (actual !== length) {
|
236
|
// Writing a hex string, for example, that contains invalid characters will
|
237
|
// cause everything after the first invalid character to be ignored. (e.g.
|
238
|
// 'abxxcd' will be treated as 'ab')
|
239
|
that = that.slice(0, actual)
|
240
|
}
|
241
|
|
242
|
return that
|
243
|
}
|
244
|
|
245
|
function fromArrayLike (that, array) {
|
246
|
var length = array.length < 0 ? 0 : checked(array.length) | 0
|
247
|
that = createBuffer(that, length)
|
248
|
for (var i = 0; i < length; i += 1) {
|
249
|
that[i] = array[i] & 255
|
250
|
}
|
251
|
return that
|
252
|
}
|
253
|
|
254
|
function fromArrayBuffer (that, array, byteOffset, length) {
|
255
|
array.byteLength // this throws if `array` is not a valid ArrayBuffer
|
256
|
|
257
|
if (byteOffset < 0 || array.byteLength < byteOffset) {
|
258
|
throw new RangeError('\'offset\' is out of bounds')
|
259
|
}
|
260
|
|
261
|
if (array.byteLength < byteOffset + (length || 0)) {
|
262
|
throw new RangeError('\'length\' is out of bounds')
|
263
|
}
|
264
|
|
265
|
if (byteOffset === undefined && length === undefined) {
|
266
|
array = new Uint8Array(array)
|
267
|
} else if (length === undefined) {
|
268
|
array = new Uint8Array(array, byteOffset)
|
269
|
} else {
|
270
|
array = new Uint8Array(array, byteOffset, length)
|
271
|
}
|
272
|
|
273
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
274
|
// Return an augmented `Uint8Array` instance, for best performance
|
275
|
that = array
|
276
|
that.__proto__ = Buffer.prototype
|
277
|
} else {
|
278
|
// Fallback: Return an object instance of the Buffer class
|
279
|
that = fromArrayLike(that, array)
|
280
|
}
|
281
|
return that
|
282
|
}
|
283
|
|
284
|
function fromObject (that, obj) {
|
285
|
if (Buffer.isBuffer(obj)) {
|
286
|
var len = checked(obj.length) | 0
|
287
|
that = createBuffer(that, len)
|
288
|
|
289
|
if (that.length === 0) {
|
290
|
return that
|
291
|
}
|
292
|
|
293
|
obj.copy(that, 0, 0, len)
|
294
|
return that
|
295
|
}
|
296
|
|
297
|
if (obj) {
|
298
|
if ((typeof ArrayBuffer !== 'undefined' &&
|
299
|
obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
|
300
|
if (typeof obj.length !== 'number' || isnan(obj.length)) {
|
301
|
return createBuffer(that, 0)
|
302
|
}
|
303
|
return fromArrayLike(that, obj)
|
304
|
}
|
305
|
|
306
|
if (obj.type === 'Buffer' && isArray(obj.data)) {
|
307
|
return fromArrayLike(that, obj.data)
|
308
|
}
|
309
|
}
|
310
|
|
311
|
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
312
|
}
|
313
|
|
314
|
function checked (length) {
|
315
|
// Note: cannot use `length < kMaxLength()` here because that fails when
|
316
|
// length is NaN (which is otherwise coerced to zero.)
|
317
|
if (length >= kMaxLength()) {
|
318
|
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
319
|
'size: 0x' + kMaxLength().toString(16) + ' bytes')
|
320
|
}
|
321
|
return length | 0
|
322
|
}
|
323
|
|
324
|
function SlowBuffer (length) {
|
325
|
if (+length != length) { // eslint-disable-line eqeqeq
|
326
|
length = 0
|
327
|
}
|
328
|
return Buffer.alloc(+length)
|
329
|
}
|
330
|
|
331
|
Buffer.isBuffer = function isBuffer (b) {
|
332
|
return !!(b != null && b._isBuffer)
|
333
|
}
|
334
|
|
335
|
Buffer.compare = function compare (a, b) {
|
336
|
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
|
337
|
throw new TypeError('Arguments must be Buffers')
|
338
|
}
|
339
|
|
340
|
if (a === b) return 0
|
341
|
|
342
|
var x = a.length
|
343
|
var y = b.length
|
344
|
|
345
|
for (var i = 0, len = Math.min(x, y); i < len; ++i) {
|
346
|
if (a[i] !== b[i]) {
|
347
|
x = a[i]
|
348
|
y = b[i]
|
349
|
break
|
350
|
}
|
351
|
}
|
352
|
|
353
|
if (x < y) return -1
|
354
|
if (y < x) return 1
|
355
|
return 0
|
356
|
}
|
357
|
|
358
|
Buffer.isEncoding = function isEncoding (encoding) {
|
359
|
switch (String(encoding).toLowerCase()) {
|
360
|
case 'hex':
|
361
|
case 'utf8':
|
362
|
case 'utf-8':
|
363
|
case 'ascii':
|
364
|
case 'latin1':
|
365
|
case 'binary':
|
366
|
case 'base64':
|
367
|
case 'ucs2':
|
368
|
case 'ucs-2':
|
369
|
case 'utf16le':
|
370
|
case 'utf-16le':
|
371
|
return true
|
372
|
default:
|
373
|
return false
|
374
|
}
|
375
|
}
|
376
|
|
377
|
Buffer.concat = function concat (list, length) {
|
378
|
if (!isArray(list)) {
|
379
|
throw new TypeError('"list" argument must be an Array of Buffers')
|
380
|
}
|
381
|
|
382
|
if (list.length === 0) {
|
383
|
return Buffer.alloc(0)
|
384
|
}
|
385
|
|
386
|
var i
|
387
|
if (length === undefined) {
|
388
|
length = 0
|
389
|
for (i = 0; i < list.length; ++i) {
|
390
|
length += list[i].length
|
391
|
}
|
392
|
}
|
393
|
|
394
|
var buffer = Buffer.allocUnsafe(length)
|
395
|
var pos = 0
|
396
|
for (i = 0; i < list.length; ++i) {
|
397
|
var buf = list[i]
|
398
|
if (!Buffer.isBuffer(buf)) {
|
399
|
throw new TypeError('"list" argument must be an Array of Buffers')
|
400
|
}
|
401
|
buf.copy(buffer, pos)
|
402
|
pos += buf.length
|
403
|
}
|
404
|
return buffer
|
405
|
}
|
406
|
|
407
|
function byteLength (string, encoding) {
|
408
|
if (Buffer.isBuffer(string)) {
|
409
|
return string.length
|
410
|
}
|
411
|
if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
|
412
|
(ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
|
413
|
return string.byteLength
|
414
|
}
|
415
|
if (typeof string !== 'string') {
|
416
|
string = '' + string
|
417
|
}
|
418
|
|
419
|
var len = string.length
|
420
|
if (len === 0) return 0
|
421
|
|
422
|
// Use a for loop to avoid recursion
|
423
|
var loweredCase = false
|
424
|
for (;;) {
|
425
|
switch (encoding) {
|
426
|
case 'ascii':
|
427
|
case 'latin1':
|
428
|
case 'binary':
|
429
|
return len
|
430
|
case 'utf8':
|
431
|
case 'utf-8':
|
432
|
case undefined:
|
433
|
return utf8ToBytes(string).length
|
434
|
case 'ucs2':
|
435
|
case 'ucs-2':
|
436
|
case 'utf16le':
|
437
|
case 'utf-16le':
|
438
|
return len * 2
|
439
|
case 'hex':
|
440
|
return len >>> 1
|
441
|
case 'base64':
|
442
|
return base64ToBytes(string).length
|
443
|
default:
|
444
|
if (loweredCase) return utf8ToBytes(string).length // assume utf8
|
445
|
encoding = ('' + encoding).toLowerCase()
|
446
|
loweredCase = true
|
447
|
}
|
448
|
}
|
449
|
}
|
450
|
Buffer.byteLength = byteLength
|
451
|
|
452
|
function slowToString (encoding, start, end) {
|
453
|
var loweredCase = false
|
454
|
|
455
|
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only
|
456
|
// property of a typed array.
|
457
|
|
458
|
// This behaves neither like String nor Uint8Array in that we set start/end
|
459
|
// to their upper/lower bounds if the value passed is out of range.
|
460
|
// undefined is handled specially as per ECMA-262 6th Edition,
|
461
|
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
|
462
|
if (start === undefined || start < 0) {
|
463
|
start = 0
|
464
|
}
|
465
|
// Return early if start > this.length. Done here to prevent potential uint32
|
466
|
// coercion fail below.
|
467
|
if (start > this.length) {
|
468
|
return ''
|
469
|
}
|
470
|
|
471
|
if (end === undefined || end > this.length) {
|
472
|
end = this.length
|
473
|
}
|
474
|
|
475
|
if (end <= 0) {
|
476
|
return ''
|
477
|
}
|
478
|
|
479
|
// Force coersion to uint32. This will also coerce falsey/NaN values to 0.
|
480
|
end >>>= 0
|
481
|
start >>>= 0
|
482
|
|
483
|
if (end <= start) {
|
484
|
return ''
|
485
|
}
|
486
|
|
487
|
if (!encoding) encoding = 'utf8'
|
488
|
|
489
|
while (true) {
|
490
|
switch (encoding) {
|
491
|
case 'hex':
|
492
|
return hexSlice(this, start, end)
|
493
|
|
494
|
case 'utf8':
|
495
|
case 'utf-8':
|
496
|
return utf8Slice(this, start, end)
|
497
|
|
498
|
case 'ascii':
|
499
|
return asciiSlice(this, start, end)
|
500
|
|
501
|
case 'latin1':
|
502
|
case 'binary':
|
503
|
return latin1Slice(this, start, end)
|
504
|
|
505
|
case 'base64':
|
506
|
return base64Slice(this, start, end)
|
507
|
|
508
|
case 'ucs2':
|
509
|
case 'ucs-2':
|
510
|
case 'utf16le':
|
511
|
case 'utf-16le':
|
512
|
return utf16leSlice(this, start, end)
|
513
|
|
514
|
default:
|
515
|
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
516
|
encoding = (encoding + '').toLowerCase()
|
517
|
loweredCase = true
|
518
|
}
|
519
|
}
|
520
|
}
|
521
|
|
522
|
// The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
|
523
|
// Buffer instances.
|
524
|
Buffer.prototype._isBuffer = true
|
525
|
|
526
|
function swap (b, n, m) {
|
527
|
var i = b[n]
|
528
|
b[n] = b[m]
|
529
|
b[m] = i
|
530
|
}
|
531
|
|
532
|
Buffer.prototype.swap16 = function swap16 () {
|
533
|
var len = this.length
|
534
|
if (len % 2 !== 0) {
|
535
|
throw new RangeError('Buffer size must be a multiple of 16-bits')
|
536
|
}
|
537
|
for (var i = 0; i < len; i += 2) {
|
538
|
swap(this, i, i + 1)
|
539
|
}
|
540
|
return this
|
541
|
}
|
542
|
|
543
|
Buffer.prototype.swap32 = function swap32 () {
|
544
|
var len = this.length
|
545
|
if (len % 4 !== 0) {
|
546
|
throw new RangeError('Buffer size must be a multiple of 32-bits')
|
547
|
}
|
548
|
for (var i = 0; i < len; i += 4) {
|
549
|
swap(this, i, i + 3)
|
550
|
swap(this, i + 1, i + 2)
|
551
|
}
|
552
|
return this
|
553
|
}
|
554
|
|
555
|
Buffer.prototype.swap64 = function swap64 () {
|
556
|
var len = this.length
|
557
|
if (len % 8 !== 0) {
|
558
|
throw new RangeError('Buffer size must be a multiple of 64-bits')
|
559
|
}
|
560
|
for (var i = 0; i < len; i += 8) {
|
561
|
swap(this, i, i + 7)
|
562
|
swap(this, i + 1, i + 6)
|
563
|
swap(this, i + 2, i + 5)
|
564
|
swap(this, i + 3, i + 4)
|
565
|
}
|
566
|
return this
|
567
|
}
|
568
|
|
569
|
Buffer.prototype.toString = function toString () {
|
570
|
var length = this.length | 0
|
571
|
if (length === 0) return ''
|
572
|
if (arguments.length === 0) return utf8Slice(this, 0, length)
|
573
|
return slowToString.apply(this, arguments)
|
574
|
}
|
575
|
|
576
|
Buffer.prototype.equals = function equals (b) {
|
577
|
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
|
578
|
if (this === b) return true
|
579
|
return Buffer.compare(this, b) === 0
|
580
|
}
|
581
|
|
582
|
Buffer.prototype.inspect = function inspect () {
|
583
|
var str = ''
|
584
|
var max = exports.INSPECT_MAX_BYTES
|
585
|
if (this.length > 0) {
|
586
|
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
|
587
|
if (this.length > max) str += ' ... '
|
588
|
}
|
589
|
return '<Buffer ' + str + '>'
|
590
|
}
|
591
|
|
592
|
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
|
593
|
if (!Buffer.isBuffer(target)) {
|
594
|
throw new TypeError('Argument must be a Buffer')
|
595
|
}
|
596
|
|
597
|
if (start === undefined) {
|
598
|
start = 0
|
599
|
}
|
600
|
if (end === undefined) {
|
601
|
end = target ? target.length : 0
|
602
|
}
|
603
|
if (thisStart === undefined) {
|
604
|
thisStart = 0
|
605
|
}
|
606
|
if (thisEnd === undefined) {
|
607
|
thisEnd = this.length
|
608
|
}
|
609
|
|
610
|
if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
|
611
|
throw new RangeError('out of range index')
|
612
|
}
|
613
|
|
614
|
if (thisStart >= thisEnd && start >= end) {
|
615
|
return 0
|
616
|
}
|
617
|
if (thisStart >= thisEnd) {
|
618
|
return -1
|
619
|
}
|
620
|
if (start >= end) {
|
621
|
return 1
|
622
|
}
|
623
|
|
624
|
start >>>= 0
|
625
|
end >>>= 0
|
626
|
thisStart >>>= 0
|
627
|
thisEnd >>>= 0
|
628
|
|
629
|
if (this === target) return 0
|
630
|
|
631
|
var x = thisEnd - thisStart
|
632
|
var y = end - start
|
633
|
var len = Math.min(x, y)
|
634
|
|
635
|
var thisCopy = this.slice(thisStart, thisEnd)
|
636
|
var targetCopy = target.slice(start, end)
|
637
|
|
638
|
for (var i = 0; i < len; ++i) {
|
639
|
if (thisCopy[i] !== targetCopy[i]) {
|
640
|
x = thisCopy[i]
|
641
|
y = targetCopy[i]
|
642
|
break
|
643
|
}
|
644
|
}
|
645
|
|
646
|
if (x < y) return -1
|
647
|
if (y < x) return 1
|
648
|
return 0
|
649
|
}
|
650
|
|
651
|
// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
|
652
|
// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
|
653
|
//
|
654
|
// Arguments:
|
655
|
// - buffer - a Buffer to search
|
656
|
// - val - a string, Buffer, or number
|
657
|
// - byteOffset - an index into `buffer`; will be clamped to an int32
|
658
|
// - encoding - an optional encoding, relevant is val is a string
|
659
|
// - dir - true for indexOf, false for lastIndexOf
|
660
|
function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
|
661
|
// Empty buffer means no match
|
662
|
if (buffer.length === 0) return -1
|
663
|
|
664
|
// Normalize byteOffset
|
665
|
if (typeof byteOffset === 'string') {
|
666
|
encoding = byteOffset
|
667
|
byteOffset = 0
|
668
|
} else if (byteOffset > 0x7fffffff) {
|
669
|
byteOffset = 0x7fffffff
|
670
|
} else if (byteOffset < -0x80000000) {
|
671
|
byteOffset = -0x80000000
|
672
|
}
|
673
|
byteOffset = +byteOffset // Coerce to Number.
|
674
|
if (isNaN(byteOffset)) {
|
675
|
// byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
|
676
|
byteOffset = dir ? 0 : (buffer.length - 1)
|
677
|
}
|
678
|
|
679
|
// Normalize byteOffset: negative offsets start from the end of the buffer
|
680
|
if (byteOffset < 0) byteOffset = buffer.length + byteOffset
|
681
|
if (byteOffset >= buffer.length) {
|
682
|
if (dir) return -1
|
683
|
else byteOffset = buffer.length - 1
|
684
|
} else if (byteOffset < 0) {
|
685
|
if (dir) byteOffset = 0
|
686
|
else return -1
|
687
|
}
|
688
|
|
689
|
// Normalize val
|
690
|
if (typeof val === 'string') {
|
691
|
val = Buffer.from(val, encoding)
|
692
|
}
|
693
|
|
694
|
// Finally, search either indexOf (if dir is true) or lastIndexOf
|
695
|
if (Buffer.isBuffer(val)) {
|
696
|
// Special case: looking for empty string/buffer always fails
|
697
|
if (val.length === 0) {
|
698
|
return -1
|
699
|
}
|
700
|
return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
|
701
|
} else if (typeof val === 'number') {
|
702
|
val = val & 0xFF // Search for a byte value [0-255]
|
703
|
if (Buffer.TYPED_ARRAY_SUPPORT &&
|
704
|
typeof Uint8Array.prototype.indexOf === 'function') {
|
705
|
if (dir) {
|
706
|
return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
|
707
|
} else {
|
708
|
return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
|
709
|
}
|
710
|
}
|
711
|
return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
|
712
|
}
|
713
|
|
714
|
throw new TypeError('val must be string, number or Buffer')
|
715
|
}
|
716
|
|
717
|
function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
|
718
|
var indexSize = 1
|
719
|
var arrLength = arr.length
|
720
|
var valLength = val.length
|
721
|
|
722
|
if (encoding !== undefined) {
|
723
|
encoding = String(encoding).toLowerCase()
|
724
|
if (encoding === 'ucs2' || encoding === 'ucs-2' ||
|
725
|
encoding === 'utf16le' || encoding === 'utf-16le') {
|
726
|
if (arr.length < 2 || val.length < 2) {
|
727
|
return -1
|
728
|
}
|
729
|
indexSize = 2
|
730
|
arrLength /= 2
|
731
|
valLength /= 2
|
732
|
byteOffset /= 2
|
733
|
}
|
734
|
}
|
735
|
|
736
|
function read (buf, i) {
|
737
|
if (indexSize === 1) {
|
738
|
return buf[i]
|
739
|
} else {
|
740
|
return buf.readUInt16BE(i * indexSize)
|
741
|
}
|
742
|
}
|
743
|
|
744
|
var i
|
745
|
if (dir) {
|
746
|
var foundIndex = -1
|
747
|
for (i = byteOffset; i < arrLength; i++) {
|
748
|
if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
|
749
|
if (foundIndex === -1) foundIndex = i
|
750
|
if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
|
751
|
} else {
|
752
|
if (foundIndex !== -1) i -= i - foundIndex
|
753
|
foundIndex = -1
|
754
|
}
|
755
|
}
|
756
|
} else {
|
757
|
if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
|
758
|
for (i = byteOffset; i >= 0; i--) {
|
759
|
var found = true
|
760
|
for (var j = 0; j < valLength; j++) {
|
761
|
if (read(arr, i + j) !== read(val, j)) {
|
762
|
found = false
|
763
|
break
|
764
|
}
|
765
|
}
|
766
|
if (found) return i
|
767
|
}
|
768
|
}
|
769
|
|
770
|
return -1
|
771
|
}
|
772
|
|
773
|
Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
|
774
|
return this.indexOf(val, byteOffset, encoding) !== -1
|
775
|
}
|
776
|
|
777
|
Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
|
778
|
return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
|
779
|
}
|
780
|
|
781
|
Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
|
782
|
return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
|
783
|
}
|
784
|
|
785
|
function hexWrite (buf, string, offset, length) {
|
786
|
offset = Number(offset) || 0
|
787
|
var remaining = buf.length - offset
|
788
|
if (!length) {
|
789
|
length = remaining
|
790
|
} else {
|
791
|
length = Number(length)
|
792
|
if (length > remaining) {
|
793
|
length = remaining
|
794
|
}
|
795
|
}
|
796
|
|
797
|
// must be an even number of digits
|
798
|
var strLen = string.length
|
799
|
if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
|
800
|
|
801
|
if (length > strLen / 2) {
|
802
|
length = strLen / 2
|
803
|
}
|
804
|
for (var i = 0; i < length; ++i) {
|
805
|
var parsed = parseInt(string.substr(i * 2, 2), 16)
|
806
|
if (isNaN(parsed)) return i
|
807
|
buf[offset + i] = parsed
|
808
|
}
|
809
|
return i
|
810
|
}
|
811
|
|
812
|
function utf8Write (buf, string, offset, length) {
|
813
|
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
|
814
|
}
|
815
|
|
816
|
function asciiWrite (buf, string, offset, length) {
|
817
|
return blitBuffer(asciiToBytes(string), buf, offset, length)
|
818
|
}
|
819
|
|
820
|
function latin1Write (buf, string, offset, length) {
|
821
|
return asciiWrite(buf, string, offset, length)
|
822
|
}
|
823
|
|
824
|
function base64Write (buf, string, offset, length) {
|
825
|
return blitBuffer(base64ToBytes(string), buf, offset, length)
|
826
|
}
|
827
|
|
828
|
function ucs2Write (buf, string, offset, length) {
|
829
|
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
|
830
|
}
|
831
|
|
832
|
Buffer.prototype.write = function write (string, offset, length, encoding) {
|
833
|
// Buffer#write(string)
|
834
|
if (offset === undefined) {
|
835
|
encoding = 'utf8'
|
836
|
length = this.length
|
837
|
offset = 0
|
838
|
// Buffer#write(string, encoding)
|
839
|
} else if (length === undefined && typeof offset === 'string') {
|
840
|
encoding = offset
|
841
|
length = this.length
|
842
|
offset = 0
|
843
|
// Buffer#write(string, offset[, length][, encoding])
|
844
|
} else if (isFinite(offset)) {
|
845
|
offset = offset | 0
|
846
|
if (isFinite(length)) {
|
847
|
length = length | 0
|
848
|
if (encoding === undefined) encoding = 'utf8'
|
849
|
} else {
|
850
|
encoding = length
|
851
|
length = undefined
|
852
|
}
|
853
|
// legacy write(string, encoding, offset, length) - remove in v0.13
|
854
|
} else {
|
855
|
throw new Error(
|
856
|
'Buffer.write(string, encoding, offset[, length]) is no longer supported'
|
857
|
)
|
858
|
}
|
859
|
|
860
|
var remaining = this.length - offset
|
861
|
if (length === undefined || length > remaining) length = remaining
|
862
|
|
863
|
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
|
864
|
throw new RangeError('Attempt to write outside buffer bounds')
|
865
|
}
|
866
|
|
867
|
if (!encoding) encoding = 'utf8'
|
868
|
|
869
|
var loweredCase = false
|
870
|
for (;;) {
|
871
|
switch (encoding) {
|
872
|
case 'hex':
|
873
|
return hexWrite(this, string, offset, length)
|
874
|
|
875
|
case 'utf8':
|
876
|
case 'utf-8':
|
877
|
return utf8Write(this, string, offset, length)
|
878
|
|
879
|
case 'ascii':
|
880
|
return asciiWrite(this, string, offset, length)
|
881
|
|
882
|
case 'latin1':
|
883
|
case 'binary':
|
884
|
return latin1Write(this, string, offset, length)
|
885
|
|
886
|
case 'base64':
|
887
|
// Warning: maxLength not taken into account in base64Write
|
888
|
return base64Write(this, string, offset, length)
|
889
|
|
890
|
case 'ucs2':
|
891
|
case 'ucs-2':
|
892
|
case 'utf16le':
|
893
|
case 'utf-16le':
|
894
|
return ucs2Write(this, string, offset, length)
|
895
|
|
896
|
default:
|
897
|
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
|
898
|
encoding = ('' + encoding).toLowerCase()
|
899
|
loweredCase = true
|
900
|
}
|
901
|
}
|
902
|
}
|
903
|
|
904
|
Buffer.prototype.toJSON = function toJSON () {
|
905
|
return {
|
906
|
type: 'Buffer',
|
907
|
data: Array.prototype.slice.call(this._arr || this, 0)
|
908
|
}
|
909
|
}
|
910
|
|
911
|
function base64Slice (buf, start, end) {
|
912
|
if (start === 0 && end === buf.length) {
|
913
|
return base64.fromByteArray(buf)
|
914
|
} else {
|
915
|
return base64.fromByteArray(buf.slice(start, end))
|
916
|
}
|
917
|
}
|
918
|
|
919
|
function utf8Slice (buf, start, end) {
|
920
|
end = Math.min(buf.length, end)
|
921
|
var res = []
|
922
|
|
923
|
var i = start
|
924
|
while (i < end) {
|
925
|
var firstByte = buf[i]
|
926
|
var codePoint = null
|
927
|
var bytesPerSequence = (firstByte > 0xEF) ? 4
|
928
|
: (firstByte > 0xDF) ? 3
|
929
|
: (firstByte > 0xBF) ? 2
|
930
|
: 1
|
931
|
|
932
|
if (i + bytesPerSequence <= end) {
|
933
|
var secondByte, thirdByte, fourthByte, tempCodePoint
|
934
|
|
935
|
switch (bytesPerSequence) {
|
936
|
case 1:
|
937
|
if (firstByte < 0x80) {
|
938
|
codePoint = firstByte
|
939
|
}
|
940
|
break
|
941
|
case 2:
|
942
|
secondByte = buf[i + 1]
|
943
|
if ((secondByte & 0xC0) === 0x80) {
|
944
|
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
|
945
|
if (tempCodePoint > 0x7F) {
|
946
|
codePoint = tempCodePoint
|
947
|
}
|
948
|
}
|
949
|
break
|
950
|
case 3:
|
951
|
secondByte = buf[i + 1]
|
952
|
thirdByte = buf[i + 2]
|
953
|
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
|
954
|
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
|
955
|
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
|
956
|
codePoint = tempCodePoint
|
957
|
}
|
958
|
}
|
959
|
break
|
960
|
case 4:
|
961
|
secondByte = buf[i + 1]
|
962
|
thirdByte = buf[i + 2]
|
963
|
fourthByte = buf[i + 3]
|
964
|
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
|
965
|
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
|
966
|
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
|
967
|
codePoint = tempCodePoint
|
968
|
}
|
969
|
}
|
970
|
}
|
971
|
}
|
972
|
|
973
|
if (codePoint === null) {
|
974
|
// we did not generate a valid codePoint so insert a
|
975
|
// replacement char (U+FFFD) and advance only 1 byte
|
976
|
codePoint = 0xFFFD
|
977
|
bytesPerSequence = 1
|
978
|
} else if (codePoint > 0xFFFF) {
|
979
|
// encode to utf16 (surrogate pair dance)
|
980
|
codePoint -= 0x10000
|
981
|
res.push(codePoint >>> 10 & 0x3FF | 0xD800)
|
982
|
codePoint = 0xDC00 | codePoint & 0x3FF
|
983
|
}
|
984
|
|
985
|
res.push(codePoint)
|
986
|
i += bytesPerSequence
|
987
|
}
|
988
|
|
989
|
return decodeCodePointsArray(res)
|
990
|
}
|
991
|
|
992
|
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
|
993
|
// the lowest limit is Chrome, with 0x10000 args.
|
994
|
// We go 1 magnitude less, for safety
|
995
|
var MAX_ARGUMENTS_LENGTH = 0x1000
|
996
|
|
997
|
function decodeCodePointsArray (codePoints) {
|
998
|
var len = codePoints.length
|
999
|
if (len <= MAX_ARGUMENTS_LENGTH) {
|
1000
|
return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
|
1001
|
}
|
1002
|
|
1003
|
// Decode in chunks to avoid "call stack size exceeded".
|
1004
|
var res = ''
|
1005
|
var i = 0
|
1006
|
while (i < len) {
|
1007
|
res += String.fromCharCode.apply(
|
1008
|
String,
|
1009
|
codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
|
1010
|
)
|
1011
|
}
|
1012
|
return res
|
1013
|
}
|
1014
|
|
1015
|
function asciiSlice (buf, start, end) {
|
1016
|
var ret = ''
|
1017
|
end = Math.min(buf.length, end)
|
1018
|
|
1019
|
for (var i = start; i < end; ++i) {
|
1020
|
ret += String.fromCharCode(buf[i] & 0x7F)
|
1021
|
}
|
1022
|
return ret
|
1023
|
}
|
1024
|
|
1025
|
function latin1Slice (buf, start, end) {
|
1026
|
var ret = ''
|
1027
|
end = Math.min(buf.length, end)
|
1028
|
|
1029
|
for (var i = start; i < end; ++i) {
|
1030
|
ret += String.fromCharCode(buf[i])
|
1031
|
}
|
1032
|
return ret
|
1033
|
}
|
1034
|
|
1035
|
function hexSlice (buf, start, end) {
|
1036
|
var len = buf.length
|
1037
|
|
1038
|
if (!start || start < 0) start = 0
|
1039
|
if (!end || end < 0 || end > len) end = len
|
1040
|
|
1041
|
var out = ''
|
1042
|
for (var i = start; i < end; ++i) {
|
1043
|
out += toHex(buf[i])
|
1044
|
}
|
1045
|
return out
|
1046
|
}
|
1047
|
|
1048
|
function utf16leSlice (buf, start, end) {
|
1049
|
var bytes = buf.slice(start, end)
|
1050
|
var res = ''
|
1051
|
for (var i = 0; i < bytes.length; i += 2) {
|
1052
|
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
|
1053
|
}
|
1054
|
return res
|
1055
|
}
|
1056
|
|
1057
|
Buffer.prototype.slice = function slice (start, end) {
|
1058
|
var len = this.length
|
1059
|
start = ~~start
|
1060
|
end = end === undefined ? len : ~~end
|
1061
|
|
1062
|
if (start < 0) {
|
1063
|
start += len
|
1064
|
if (start < 0) start = 0
|
1065
|
} else if (start > len) {
|
1066
|
start = len
|
1067
|
}
|
1068
|
|
1069
|
if (end < 0) {
|
1070
|
end += len
|
1071
|
if (end < 0) end = 0
|
1072
|
} else if (end > len) {
|
1073
|
end = len
|
1074
|
}
|
1075
|
|
1076
|
if (end < start) end = start
|
1077
|
|
1078
|
var newBuf
|
1079
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1080
|
newBuf = this.subarray(start, end)
|
1081
|
newBuf.__proto__ = Buffer.prototype
|
1082
|
} else {
|
1083
|
var sliceLen = end - start
|
1084
|
newBuf = new Buffer(sliceLen, undefined)
|
1085
|
for (var i = 0; i < sliceLen; ++i) {
|
1086
|
newBuf[i] = this[i + start]
|
1087
|
}
|
1088
|
}
|
1089
|
|
1090
|
return newBuf
|
1091
|
}
|
1092
|
|
1093
|
/*
|
1094
|
* Need to make sure that buffer isn't trying to write out of bounds.
|
1095
|
*/
|
1096
|
function checkOffset (offset, ext, length) {
|
1097
|
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
|
1098
|
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
|
1099
|
}
|
1100
|
|
1101
|
Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
|
1102
|
offset = offset | 0
|
1103
|
byteLength = byteLength | 0
|
1104
|
if (!noAssert) checkOffset(offset, byteLength, this.length)
|
1105
|
|
1106
|
var val = this[offset]
|
1107
|
var mul = 1
|
1108
|
var i = 0
|
1109
|
while (++i < byteLength && (mul *= 0x100)) {
|
1110
|
val += this[offset + i] * mul
|
1111
|
}
|
1112
|
|
1113
|
return val
|
1114
|
}
|
1115
|
|
1116
|
Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
|
1117
|
offset = offset | 0
|
1118
|
byteLength = byteLength | 0
|
1119
|
if (!noAssert) {
|
1120
|
checkOffset(offset, byteLength, this.length)
|
1121
|
}
|
1122
|
|
1123
|
var val = this[offset + --byteLength]
|
1124
|
var mul = 1
|
1125
|
while (byteLength > 0 && (mul *= 0x100)) {
|
1126
|
val += this[offset + --byteLength] * mul
|
1127
|
}
|
1128
|
|
1129
|
return val
|
1130
|
}
|
1131
|
|
1132
|
Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
|
1133
|
if (!noAssert) checkOffset(offset, 1, this.length)
|
1134
|
return this[offset]
|
1135
|
}
|
1136
|
|
1137
|
Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
|
1138
|
if (!noAssert) checkOffset(offset, 2, this.length)
|
1139
|
return this[offset] | (this[offset + 1] << 8)
|
1140
|
}
|
1141
|
|
1142
|
Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
|
1143
|
if (!noAssert) checkOffset(offset, 2, this.length)
|
1144
|
return (this[offset] << 8) | this[offset + 1]
|
1145
|
}
|
1146
|
|
1147
|
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
|
1148
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1149
|
|
1150
|
return ((this[offset]) |
|
1151
|
(this[offset + 1] << 8) |
|
1152
|
(this[offset + 2] << 16)) +
|
1153
|
(this[offset + 3] * 0x1000000)
|
1154
|
}
|
1155
|
|
1156
|
Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
|
1157
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1158
|
|
1159
|
return (this[offset] * 0x1000000) +
|
1160
|
((this[offset + 1] << 16) |
|
1161
|
(this[offset + 2] << 8) |
|
1162
|
this[offset + 3])
|
1163
|
}
|
1164
|
|
1165
|
Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
|
1166
|
offset = offset | 0
|
1167
|
byteLength = byteLength | 0
|
1168
|
if (!noAssert) checkOffset(offset, byteLength, this.length)
|
1169
|
|
1170
|
var val = this[offset]
|
1171
|
var mul = 1
|
1172
|
var i = 0
|
1173
|
while (++i < byteLength && (mul *= 0x100)) {
|
1174
|
val += this[offset + i] * mul
|
1175
|
}
|
1176
|
mul *= 0x80
|
1177
|
|
1178
|
if (val >= mul) val -= Math.pow(2, 8 * byteLength)
|
1179
|
|
1180
|
return val
|
1181
|
}
|
1182
|
|
1183
|
Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
|
1184
|
offset = offset | 0
|
1185
|
byteLength = byteLength | 0
|
1186
|
if (!noAssert) checkOffset(offset, byteLength, this.length)
|
1187
|
|
1188
|
var i = byteLength
|
1189
|
var mul = 1
|
1190
|
var val = this[offset + --i]
|
1191
|
while (i > 0 && (mul *= 0x100)) {
|
1192
|
val += this[offset + --i] * mul
|
1193
|
}
|
1194
|
mul *= 0x80
|
1195
|
|
1196
|
if (val >= mul) val -= Math.pow(2, 8 * byteLength)
|
1197
|
|
1198
|
return val
|
1199
|
}
|
1200
|
|
1201
|
Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
|
1202
|
if (!noAssert) checkOffset(offset, 1, this.length)
|
1203
|
if (!(this[offset] & 0x80)) return (this[offset])
|
1204
|
return ((0xff - this[offset] + 1) * -1)
|
1205
|
}
|
1206
|
|
1207
|
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
|
1208
|
if (!noAssert) checkOffset(offset, 2, this.length)
|
1209
|
var val = this[offset] | (this[offset + 1] << 8)
|
1210
|
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
1211
|
}
|
1212
|
|
1213
|
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
|
1214
|
if (!noAssert) checkOffset(offset, 2, this.length)
|
1215
|
var val = this[offset + 1] | (this[offset] << 8)
|
1216
|
return (val & 0x8000) ? val | 0xFFFF0000 : val
|
1217
|
}
|
1218
|
|
1219
|
Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
|
1220
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1221
|
|
1222
|
return (this[offset]) |
|
1223
|
(this[offset + 1] << 8) |
|
1224
|
(this[offset + 2] << 16) |
|
1225
|
(this[offset + 3] << 24)
|
1226
|
}
|
1227
|
|
1228
|
Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
|
1229
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1230
|
|
1231
|
return (this[offset] << 24) |
|
1232
|
(this[offset + 1] << 16) |
|
1233
|
(this[offset + 2] << 8) |
|
1234
|
(this[offset + 3])
|
1235
|
}
|
1236
|
|
1237
|
Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
|
1238
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1239
|
return ieee754.read(this, offset, true, 23, 4)
|
1240
|
}
|
1241
|
|
1242
|
Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
|
1243
|
if (!noAssert) checkOffset(offset, 4, this.length)
|
1244
|
return ieee754.read(this, offset, false, 23, 4)
|
1245
|
}
|
1246
|
|
1247
|
Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
|
1248
|
if (!noAssert) checkOffset(offset, 8, this.length)
|
1249
|
return ieee754.read(this, offset, true, 52, 8)
|
1250
|
}
|
1251
|
|
1252
|
Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
|
1253
|
if (!noAssert) checkOffset(offset, 8, this.length)
|
1254
|
return ieee754.read(this, offset, false, 52, 8)
|
1255
|
}
|
1256
|
|
1257
|
function checkInt (buf, value, offset, ext, max, min) {
|
1258
|
if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
|
1259
|
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
|
1260
|
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
1261
|
}
|
1262
|
|
1263
|
Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
|
1264
|
value = +value
|
1265
|
offset = offset | 0
|
1266
|
byteLength = byteLength | 0
|
1267
|
if (!noAssert) {
|
1268
|
var maxBytes = Math.pow(2, 8 * byteLength) - 1
|
1269
|
checkInt(this, value, offset, byteLength, maxBytes, 0)
|
1270
|
}
|
1271
|
|
1272
|
var mul = 1
|
1273
|
var i = 0
|
1274
|
this[offset] = value & 0xFF
|
1275
|
while (++i < byteLength && (mul *= 0x100)) {
|
1276
|
this[offset + i] = (value / mul) & 0xFF
|
1277
|
}
|
1278
|
|
1279
|
return offset + byteLength
|
1280
|
}
|
1281
|
|
1282
|
Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
|
1283
|
value = +value
|
1284
|
offset = offset | 0
|
1285
|
byteLength = byteLength | 0
|
1286
|
if (!noAssert) {
|
1287
|
var maxBytes = Math.pow(2, 8 * byteLength) - 1
|
1288
|
checkInt(this, value, offset, byteLength, maxBytes, 0)
|
1289
|
}
|
1290
|
|
1291
|
var i = byteLength - 1
|
1292
|
var mul = 1
|
1293
|
this[offset + i] = value & 0xFF
|
1294
|
while (--i >= 0 && (mul *= 0x100)) {
|
1295
|
this[offset + i] = (value / mul) & 0xFF
|
1296
|
}
|
1297
|
|
1298
|
return offset + byteLength
|
1299
|
}
|
1300
|
|
1301
|
Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
|
1302
|
value = +value
|
1303
|
offset = offset | 0
|
1304
|
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
|
1305
|
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
|
1306
|
this[offset] = (value & 0xff)
|
1307
|
return offset + 1
|
1308
|
}
|
1309
|
|
1310
|
function objectWriteUInt16 (buf, value, offset, littleEndian) {
|
1311
|
if (value < 0) value = 0xffff + value + 1
|
1312
|
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
|
1313
|
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
|
1314
|
(littleEndian ? i : 1 - i) * 8
|
1315
|
}
|
1316
|
}
|
1317
|
|
1318
|
Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
|
1319
|
value = +value
|
1320
|
offset = offset | 0
|
1321
|
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
|
1322
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1323
|
this[offset] = (value & 0xff)
|
1324
|
this[offset + 1] = (value >>> 8)
|
1325
|
} else {
|
1326
|
objectWriteUInt16(this, value, offset, true)
|
1327
|
}
|
1328
|
return offset + 2
|
1329
|
}
|
1330
|
|
1331
|
Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
|
1332
|
value = +value
|
1333
|
offset = offset | 0
|
1334
|
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
|
1335
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1336
|
this[offset] = (value >>> 8)
|
1337
|
this[offset + 1] = (value & 0xff)
|
1338
|
} else {
|
1339
|
objectWriteUInt16(this, value, offset, false)
|
1340
|
}
|
1341
|
return offset + 2
|
1342
|
}
|
1343
|
|
1344
|
function objectWriteUInt32 (buf, value, offset, littleEndian) {
|
1345
|
if (value < 0) value = 0xffffffff + value + 1
|
1346
|
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
|
1347
|
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
|
1348
|
}
|
1349
|
}
|
1350
|
|
1351
|
Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
|
1352
|
value = +value
|
1353
|
offset = offset | 0
|
1354
|
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
|
1355
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1356
|
this[offset + 3] = (value >>> 24)
|
1357
|
this[offset + 2] = (value >>> 16)
|
1358
|
this[offset + 1] = (value >>> 8)
|
1359
|
this[offset] = (value & 0xff)
|
1360
|
} else {
|
1361
|
objectWriteUInt32(this, value, offset, true)
|
1362
|
}
|
1363
|
return offset + 4
|
1364
|
}
|
1365
|
|
1366
|
Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
|
1367
|
value = +value
|
1368
|
offset = offset | 0
|
1369
|
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
|
1370
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1371
|
this[offset] = (value >>> 24)
|
1372
|
this[offset + 1] = (value >>> 16)
|
1373
|
this[offset + 2] = (value >>> 8)
|
1374
|
this[offset + 3] = (value & 0xff)
|
1375
|
} else {
|
1376
|
objectWriteUInt32(this, value, offset, false)
|
1377
|
}
|
1378
|
return offset + 4
|
1379
|
}
|
1380
|
|
1381
|
Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
|
1382
|
value = +value
|
1383
|
offset = offset | 0
|
1384
|
if (!noAssert) {
|
1385
|
var limit = Math.pow(2, 8 * byteLength - 1)
|
1386
|
|
1387
|
checkInt(this, value, offset, byteLength, limit - 1, -limit)
|
1388
|
}
|
1389
|
|
1390
|
var i = 0
|
1391
|
var mul = 1
|
1392
|
var sub = 0
|
1393
|
this[offset] = value & 0xFF
|
1394
|
while (++i < byteLength && (mul *= 0x100)) {
|
1395
|
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
|
1396
|
sub = 1
|
1397
|
}
|
1398
|
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
|
1399
|
}
|
1400
|
|
1401
|
return offset + byteLength
|
1402
|
}
|
1403
|
|
1404
|
Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
|
1405
|
value = +value
|
1406
|
offset = offset | 0
|
1407
|
if (!noAssert) {
|
1408
|
var limit = Math.pow(2, 8 * byteLength - 1)
|
1409
|
|
1410
|
checkInt(this, value, offset, byteLength, limit - 1, -limit)
|
1411
|
}
|
1412
|
|
1413
|
var i = byteLength - 1
|
1414
|
var mul = 1
|
1415
|
var sub = 0
|
1416
|
this[offset + i] = value & 0xFF
|
1417
|
while (--i >= 0 && (mul *= 0x100)) {
|
1418
|
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
|
1419
|
sub = 1
|
1420
|
}
|
1421
|
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
|
1422
|
}
|
1423
|
|
1424
|
return offset + byteLength
|
1425
|
}
|
1426
|
|
1427
|
Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
|
1428
|
value = +value
|
1429
|
offset = offset | 0
|
1430
|
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
|
1431
|
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
|
1432
|
if (value < 0) value = 0xff + value + 1
|
1433
|
this[offset] = (value & 0xff)
|
1434
|
return offset + 1
|
1435
|
}
|
1436
|
|
1437
|
Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
|
1438
|
value = +value
|
1439
|
offset = offset | 0
|
1440
|
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
|
1441
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1442
|
this[offset] = (value & 0xff)
|
1443
|
this[offset + 1] = (value >>> 8)
|
1444
|
} else {
|
1445
|
objectWriteUInt16(this, value, offset, true)
|
1446
|
}
|
1447
|
return offset + 2
|
1448
|
}
|
1449
|
|
1450
|
Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
|
1451
|
value = +value
|
1452
|
offset = offset | 0
|
1453
|
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
|
1454
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1455
|
this[offset] = (value >>> 8)
|
1456
|
this[offset + 1] = (value & 0xff)
|
1457
|
} else {
|
1458
|
objectWriteUInt16(this, value, offset, false)
|
1459
|
}
|
1460
|
return offset + 2
|
1461
|
}
|
1462
|
|
1463
|
Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
|
1464
|
value = +value
|
1465
|
offset = offset | 0
|
1466
|
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
|
1467
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1468
|
this[offset] = (value & 0xff)
|
1469
|
this[offset + 1] = (value >>> 8)
|
1470
|
this[offset + 2] = (value >>> 16)
|
1471
|
this[offset + 3] = (value >>> 24)
|
1472
|
} else {
|
1473
|
objectWriteUInt32(this, value, offset, true)
|
1474
|
}
|
1475
|
return offset + 4
|
1476
|
}
|
1477
|
|
1478
|
Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
|
1479
|
value = +value
|
1480
|
offset = offset | 0
|
1481
|
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
|
1482
|
if (value < 0) value = 0xffffffff + value + 1
|
1483
|
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
1484
|
this[offset] = (value >>> 24)
|
1485
|
this[offset + 1] = (value >>> 16)
|
1486
|
this[offset + 2] = (value >>> 8)
|
1487
|
this[offset + 3] = (value & 0xff)
|
1488
|
} else {
|
1489
|
objectWriteUInt32(this, value, offset, false)
|
1490
|
}
|
1491
|
return offset + 4
|
1492
|
}
|
1493
|
|
1494
|
function checkIEEE754 (buf, value, offset, ext, max, min) {
|
1495
|
if (offset + ext > buf.length) throw new RangeError('Index out of range')
|
1496
|
if (offset < 0) throw new RangeError('Index out of range')
|
1497
|
}
|
1498
|
|
1499
|
function writeFloat (buf, value, offset, littleEndian, noAssert) {
|
1500
|
if (!noAssert) {
|
1501
|
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
|
1502
|
}
|
1503
|
ieee754.write(buf, value, offset, littleEndian, 23, 4)
|
1504
|
return offset + 4
|
1505
|
}
|
1506
|
|
1507
|
Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
|
1508
|
return writeFloat(this, value, offset, true, noAssert)
|
1509
|
}
|
1510
|
|
1511
|
Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
|
1512
|
return writeFloat(this, value, offset, false, noAssert)
|
1513
|
}
|
1514
|
|
1515
|
function writeDouble (buf, value, offset, littleEndian, noAssert) {
|
1516
|
if (!noAssert) {
|
1517
|
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
|
1518
|
}
|
1519
|
ieee754.write(buf, value, offset, littleEndian, 52, 8)
|
1520
|
return offset + 8
|
1521
|
}
|
1522
|
|
1523
|
Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
|
1524
|
return writeDouble(this, value, offset, true, noAssert)
|
1525
|
}
|
1526
|
|
1527
|
Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
|
1528
|
return writeDouble(this, value, offset, false, noAssert)
|
1529
|
}
|
1530
|
|
1531
|
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
|
1532
|
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
|
1533
|
if (!start) start = 0
|
1534
|
if (!end && end !== 0) end = this.length
|
1535
|
if (targetStart >= target.length) targetStart = target.length
|
1536
|
if (!targetStart) targetStart = 0
|
1537
|
if (end > 0 && end < start) end = start
|
1538
|
|
1539
|
// Copy 0 bytes; we're done
|
1540
|
if (end === start) return 0
|
1541
|
if (target.length === 0 || this.length === 0) return 0
|
1542
|
|
1543
|
// Fatal error conditions
|
1544
|
if (targetStart < 0) {
|
1545
|
throw new RangeError('targetStart out of bounds')
|
1546
|
}
|
1547
|
if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
|
1548
|
if (end < 0) throw new RangeError('sourceEnd out of bounds')
|
1549
|
|
1550
|
// Are we oob?
|
1551
|
if (end > this.length) end = this.length
|
1552
|
if (target.length - targetStart < end - start) {
|
1553
|
end = target.length - targetStart + start
|
1554
|
}
|
1555
|
|
1556
|
var len = end - start
|
1557
|
var i
|
1558
|
|
1559
|
if (this === target && start < targetStart && targetStart < end) {
|
1560
|
// descending copy from end
|
1561
|
for (i = len - 1; i >= 0; --i) {
|
1562
|
target[i + targetStart] = this[i + start]
|
1563
|
}
|
1564
|
} else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
|
1565
|
// ascending copy from start
|
1566
|
for (i = 0; i < len; ++i) {
|
1567
|
target[i + targetStart] = this[i + start]
|
1568
|
}
|
1569
|
} else {
|
1570
|
Uint8Array.prototype.set.call(
|
1571
|
target,
|
1572
|
this.subarray(start, start + len),
|
1573
|
targetStart
|
1574
|
)
|
1575
|
}
|
1576
|
|
1577
|
return len
|
1578
|
}
|
1579
|
|
1580
|
// Usage:
|
1581
|
// buffer.fill(number[, offset[, end]])
|
1582
|
// buffer.fill(buffer[, offset[, end]])
|
1583
|
// buffer.fill(string[, offset[, end]][, encoding])
|
1584
|
Buffer.prototype.fill = function fill (val, start, end, encoding) {
|
1585
|
// Handle string cases:
|
1586
|
if (typeof val === 'string') {
|
1587
|
if (typeof start === 'string') {
|
1588
|
encoding = start
|
1589
|
start = 0
|
1590
|
end = this.length
|
1591
|
} else if (typeof end === 'string') {
|
1592
|
encoding = end
|
1593
|
end = this.length
|
1594
|
}
|
1595
|
if (val.length === 1) {
|
1596
|
var code = val.charCodeAt(0)
|
1597
|
if (code < 256) {
|
1598
|
val = code
|
1599
|
}
|
1600
|
}
|
1601
|
if (encoding !== undefined && typeof encoding !== 'string') {
|
1602
|
throw new TypeError('encoding must be a string')
|
1603
|
}
|
1604
|
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
|
1605
|
throw new TypeError('Unknown encoding: ' + encoding)
|
1606
|
}
|
1607
|
} else if (typeof val === 'number') {
|
1608
|
val = val & 255
|
1609
|
}
|
1610
|
|
1611
|
// Invalid ranges are not set to a default, so can range check early.
|
1612
|
if (start < 0 || this.length < start || this.length < end) {
|
1613
|
throw new RangeError('Out of range index')
|
1614
|
}
|
1615
|
|
1616
|
if (end <= start) {
|
1617
|
return this
|
1618
|
}
|
1619
|
|
1620
|
start = start >>> 0
|
1621
|
end = end === undefined ? this.length : end >>> 0
|
1622
|
|
1623
|
if (!val) val = 0
|
1624
|
|
1625
|
var i
|
1626
|
if (typeof val === 'number') {
|
1627
|
for (i = start; i < end; ++i) {
|
1628
|
this[i] = val
|
1629
|
}
|
1630
|
} else {
|
1631
|
var bytes = Buffer.isBuffer(val)
|
1632
|
? val
|
1633
|
: utf8ToBytes(new Buffer(val, encoding).toString())
|
1634
|
var len = bytes.length
|
1635
|
for (i = 0; i < end - start; ++i) {
|
1636
|
this[i + start] = bytes[i % len]
|
1637
|
}
|
1638
|
}
|
1639
|
|
1640
|
return this
|
1641
|
}
|
1642
|
|
1643
|
// HELPER FUNCTIONS
|
1644
|
// ================
|
1645
|
|
1646
|
var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g
|
1647
|
|
1648
|
function base64clean (str) {
|
1649
|
// Node strips out invalid characters like \n and \t from the string, base64-js does not
|
1650
|
str = stringtrim(str).replace(INVALID_BASE64_RE, '')
|
1651
|
// Node converts strings with length < 2 to ''
|
1652
|
if (str.length < 2) return ''
|
1653
|
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
|
1654
|
while (str.length % 4 !== 0) {
|
1655
|
str = str + '='
|
1656
|
}
|
1657
|
return str
|
1658
|
}
|
1659
|
|
1660
|
function stringtrim (str) {
|
1661
|
if (str.trim) return str.trim()
|
1662
|
return str.replace(/^\s+|\s+$/g, '')
|
1663
|
}
|
1664
|
|
1665
|
function toHex (n) {
|
1666
|
if (n < 16) return '0' + n.toString(16)
|
1667
|
return n.toString(16)
|
1668
|
}
|
1669
|
|
1670
|
function utf8ToBytes (string, units) {
|
1671
|
units = units || Infinity
|
1672
|
var codePoint
|
1673
|
var length = string.length
|
1674
|
var leadSurrogate = null
|
1675
|
var bytes = []
|
1676
|
|
1677
|
for (var i = 0; i < length; ++i) {
|
1678
|
codePoint = string.charCodeAt(i)
|
1679
|
|
1680
|
// is surrogate component
|
1681
|
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
1682
|
// last char was a lead
|
1683
|
if (!leadSurrogate) {
|
1684
|
// no lead yet
|
1685
|
if (codePoint > 0xDBFF) {
|
1686
|
// unexpected trail
|
1687
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
1688
|
continue
|
1689
|
} else if (i + 1 === length) {
|
1690
|
// unpaired lead
|
1691
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
1692
|
continue
|
1693
|
}
|
1694
|
|
1695
|
// valid lead
|
1696
|
leadSurrogate = codePoint
|
1697
|
|
1698
|
continue
|
1699
|
}
|
1700
|
|
1701
|
// 2 leads in a row
|
1702
|
if (codePoint < 0xDC00) {
|
1703
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
1704
|
leadSurrogate = codePoint
|
1705
|
continue
|
1706
|
}
|
1707
|
|
1708
|
// valid surrogate pair
|
1709
|
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
|
1710
|
} else if (leadSurrogate) {
|
1711
|
// valid bmp char, but last char was a lead
|
1712
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
1713
|
}
|
1714
|
|
1715
|
leadSurrogate = null
|
1716
|
|
1717
|
// encode utf8
|
1718
|
if (codePoint < 0x80) {
|
1719
|
if ((units -= 1) < 0) break
|
1720
|
bytes.push(codePoint)
|
1721
|
} else if (codePoint < 0x800) {
|
1722
|
if ((units -= 2) < 0) break
|
1723
|
bytes.push(
|
1724
|
codePoint >> 0x6 | 0xC0,
|
1725
|
codePoint & 0x3F | 0x80
|
1726
|
)
|
1727
|
} else if (codePoint < 0x10000) {
|
1728
|
if ((units -= 3) < 0) break
|
1729
|
bytes.push(
|
1730
|
codePoint >> 0xC | 0xE0,
|
1731
|
codePoint >> 0x6 & 0x3F | 0x80,
|
1732
|
codePoint & 0x3F | 0x80
|
1733
|
)
|
1734
|
} else if (codePoint < 0x110000) {
|
1735
|
if ((units -= 4) < 0) break
|
1736
|
bytes.push(
|
1737
|
codePoint >> 0x12 | 0xF0,
|
1738
|
codePoint >> 0xC & 0x3F | 0x80,
|
1739
|
codePoint >> 0x6 & 0x3F | 0x80,
|
1740
|
codePoint & 0x3F | 0x80
|
1741
|
)
|
1742
|
} else {
|
1743
|
throw new Error('Invalid code point')
|
1744
|
}
|
1745
|
}
|
1746
|
|
1747
|
return bytes
|
1748
|
}
|
1749
|
|
1750
|
function asciiToBytes (str) {
|
1751
|
var byteArray = []
|
1752
|
for (var i = 0; i < str.length; ++i) {
|
1753
|
// Node's code seems to be doing this and not & 0x7F..
|
1754
|
byteArray.push(str.charCodeAt(i) & 0xFF)
|
1755
|
}
|
1756
|
return byteArray
|
1757
|
}
|
1758
|
|
1759
|
function utf16leToBytes (str, units) {
|
1760
|
var c, hi, lo
|
1761
|
var byteArray = []
|
1762
|
for (var i = 0; i < str.length; ++i) {
|
1763
|
if ((units -= 2) < 0) break
|
1764
|
|
1765
|
c = str.charCodeAt(i)
|
1766
|
hi = c >> 8
|
1767
|
lo = c % 256
|
1768
|
byteArray.push(lo)
|
1769
|
byteArray.push(hi)
|
1770
|
}
|
1771
|
|
1772
|
return byteArray
|
1773
|
}
|
1774
|
|
1775
|
function base64ToBytes (str) {
|
1776
|
return base64.toByteArray(base64clean(str))
|
1777
|
}
|
1778
|
|
1779
|
function blitBuffer (src, dst, offset, length) {
|
1780
|
for (var i = 0; i < length; ++i) {
|
1781
|
if ((i + offset >= dst.length) || (i >= src.length)) break
|
1782
|
dst[i + offset] = src[i]
|
1783
|
}
|
1784
|
return i
|
1785
|
}
|
1786
|
|
1787
|
function isnan (val) {
|
1788
|
return val !== val // eslint-disable-line no-self-compare
|
1789
|
}
|