Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
/*!
2
* The buffer module from node.js, for the browser.
3
*
4
* @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
5
* @license MIT
6
*/
7
8
var base64 = require('base64-js')
9
var ieee754 = require('ieee754')
10
var isArray = require('is-array')
11
12
exports.Buffer = Buffer
13
exports.SlowBuffer = SlowBuffer
14
exports.INSPECT_MAX_BYTES = 50
15
Buffer.poolSize = 8192 // not used by this implementation
16
17
var kMaxLength = 0x3fffffff
18
var rootParent = {}
19
20
/**
21
* If `Buffer.TYPED_ARRAY_SUPPORT`:
22
* === true Use Uint8Array implementation (fastest)
23
* === false Use Object implementation (most compatible, even IE6)
24
*
25
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
26
* Opera 11.6+, iOS 4.2+.
27
*
28
* Note:
29
*
30
* - Implementation must support adding new properties to `Uint8Array` instances.
31
* Firefox 4-29 lacked support, fixed in Firefox 30+.
32
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
33
*
34
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
35
*
36
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
37
* incorrect length in some situations.
38
*
39
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will
40
* get the Object implementation, which is slower but will work correctly.
41
*/
42
Buffer.TYPED_ARRAY_SUPPORT = (function () {
43
try {
44
var buf = new ArrayBuffer(0)
45
var arr = new Uint8Array(buf)
46
arr.foo = function () { return 42 }
47
return arr.foo() === 42 && // typed array instances can be augmented
48
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
49
new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
50
} catch (e) {
51
return false
52
}
53
})()
54
55
/**
56
* Class: Buffer
57
* =============
58
*
59
* The Buffer constructor returns instances of `Uint8Array` that are augmented
60
* with function properties for all the node `Buffer` API functions. We use
61
* `Uint8Array` so that square bracket notation works as expected -- it returns
62
* a single octet.
63
*
64
* By augmenting the instances, we can avoid modifying the `Uint8Array`
65
* prototype.
66
*/
67
function Buffer (arg) {
68
if (!(this instanceof Buffer)) {
69
// Avoid going through an ArgumentsAdaptorTrampoline in the common case.
70
if (arguments.length > 1) return new Buffer(arg, arguments[1])
71
return new Buffer(arg)
72
}
73
74
this.length = 0
75
this.parent = undefined
76
77
// Common case.
78
if (typeof arg === 'number') {
79
return fromNumber(this, arg)
80
}
81
82
// Slightly less common case.
83
if (typeof arg === 'string') {
84
return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8')
85
}
86
87
// Unusual.
88
return fromObject(this, arg)
89
}
90
91
function fromNumber (that, length) {
92
that = allocate(that, length < 0 ? 0 : checked(length) | 0)
93
if (!Buffer.TYPED_ARRAY_SUPPORT) {
94
for (var i = 0; i < length; i++) {
95
that[i] = 0
96
}
97
}
98
return that
99
}
100
101
function fromString (that, string, encoding) {
102
if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8'
103
104
// Assumption: byteLength() return value is always < kMaxLength.
105
var length = byteLength(string, encoding) | 0
106
that = allocate(that, length)
107
108
that.write(string, encoding)
109
return that
110
}
111
112
function fromObject (that, object) {
113
if (Buffer.isBuffer(object)) return fromBuffer(that, object)
114
115
if (isArray(object)) return fromArray(that, object)
116
117
if (object == null) {
118
throw new TypeError('must start with number, buffer, array or string')
119
}
120
121
if (typeof ArrayBuffer !== 'undefined' && object.buffer instanceof ArrayBuffer) {
122
return fromTypedArray(that, object)
123
}
124
125
if (object.length) return fromArrayLike(that, object)
126
127
return fromJsonObject(that, object)
128
}
129
130
function fromBuffer (that, buffer) {
131
var length = checked(buffer.length) | 0
132
that = allocate(that, length)
133
buffer.copy(that, 0, 0, length)
134
return that
135
}
136
137
function fromArray (that, array) {
138
var length = checked(array.length) | 0
139
that = allocate(that, length)
140
for (var i = 0; i < length; i += 1) {
141
that[i] = array[i] & 255
142
}
143
return that
144
}
145
146
// Duplicate of fromArray() to keep fromArray() monomorphic.
147
function fromTypedArray (that, array) {
148
var length = checked(array.length) | 0
149
that = allocate(that, length)
150
// Truncating the elements is probably not what people expect from typed
151
// arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior
152
// of the old Buffer constructor.
153
for (var i = 0; i < length; i += 1) {
154
that[i] = array[i] & 255
155
}
156
return that
157
}
158
159
function fromArrayLike (that, array) {
160
var length = checked(array.length) | 0
161
that = allocate(that, length)
162
for (var i = 0; i < length; i += 1) {
163
that[i] = array[i] & 255
164
}
165
return that
166
}
167
168
// Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object.
169
// Returns a zero-length buffer for inputs that don't conform to the spec.
170
function fromJsonObject (that, object) {
171
var array
172
var length = 0
173
174
if (object.type === 'Buffer' && isArray(object.data)) {
175
array = object.data
176
length = checked(array.length) | 0
177
}
178
that = allocate(that, length)
179
180
for (var i = 0; i < length; i += 1) {
181
that[i] = array[i] & 255
182
}
183
return that
184
}
185
186
function allocate (that, length) {
187
if (Buffer.TYPED_ARRAY_SUPPORT) {
188
// Return an augmented `Uint8Array` instance, for best performance
189
that = Buffer._augment(new Uint8Array(length))
190
} else {
191
// Fallback: Return an object instance of the Buffer class
192
that.length = length
193
that._isBuffer = true
194
}
195
196
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1
197
if (fromPool) that.parent = rootParent
198
199
return that
200
}
201
202
function checked (length) {
203
// Note: cannot use `length < kMaxLength` here because that fails when
204
// length is NaN (which is otherwise coerced to zero.)
205
if (length >= kMaxLength) {
206
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
207
'size: 0x' + kMaxLength.toString(16) + ' bytes')
208
}
209
return length | 0
210
}
211
212
function SlowBuffer (subject, encoding) {
213
if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding)
214
215
var buf = new Buffer(subject, encoding)
216
delete buf.parent
217
return buf
218
}
219
220
Buffer.isBuffer = function isBuffer (b) {
221
return !!(b != null && b._isBuffer)
222
}
223
224
Buffer.compare = function compare (a, b) {
225
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
226
throw new TypeError('Arguments must be Buffers')
227
}
228
229
if (a === b) return 0
230
231
var x = a.length
232
var y = b.length
233
234
var i = 0
235
var len = Math.min(x, y)
236
while (i < len) {
237
if (a[i] !== b[i]) break
238
239
++i
240
}
241
242
if (i !== len) {
243
x = a[i]
244
y = b[i]
245
}
246
247
if (x < y) return -1
248
if (y < x) return 1
249
return 0
250
}
251
252
Buffer.isEncoding = function isEncoding (encoding) {
253
switch (String(encoding).toLowerCase()) {
254
case 'hex':
255
case 'utf8':
256
case 'utf-8':
257
case 'ascii':
258
case 'binary':
259
case 'base64':
260
case 'raw':
261
case 'ucs2':
262
case 'ucs-2':
263
case 'utf16le':
264
case 'utf-16le':
265
return true
266
default:
267
return false
268
}
269
}
270
271
Buffer.concat = function concat (list, length) {
272
if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.')
273
274
if (list.length === 0) {
275
return new Buffer(0)
276
} else if (list.length === 1) {
277
return list[0]
278
}
279
280
var i
281
if (length === undefined) {
282
length = 0
283
for (i = 0; i < list.length; i++) {
284
length += list[i].length
285
}
286
}
287
288
var buf = new Buffer(length)
289
var pos = 0
290
for (i = 0; i < list.length; i++) {
291
var item = list[i]
292
item.copy(buf, pos)
293
pos += item.length
294
}
295
return buf
296
}
297
298
function byteLength (string, encoding) {
299
if (typeof string !== 'string') string = String(string)
300
301
if (string.length === 0) return 0
302
303
switch (encoding || 'utf8') {
304
case 'ascii':
305
case 'binary':
306
case 'raw':
307
return string.length
308
case 'ucs2':
309
case 'ucs-2':
310
case 'utf16le':
311
case 'utf-16le':
312
return string.length * 2
313
case 'hex':
314
return string.length >>> 1
315
case 'utf8':
316
case 'utf-8':
317
return utf8ToBytes(string).length
318
case 'base64':
319
return base64ToBytes(string).length
320
default:
321
return string.length
322
}
323
}
324
Buffer.byteLength = byteLength
325
326
// pre-set for values that may exist in the future
327
Buffer.prototype.length = undefined
328
Buffer.prototype.parent = undefined
329
330
// toString(encoding, start=0, end=buffer.length)
331
Buffer.prototype.toString = function toString (encoding, start, end) {
332
var loweredCase = false
333
334
start = start | 0
335
end = end === undefined || end === Infinity ? this.length : end | 0
336
337
if (!encoding) encoding = 'utf8'
338
if (start < 0) start = 0
339
if (end > this.length) end = this.length
340
if (end <= start) return ''
341
342
while (true) {
343
switch (encoding) {
344
case 'hex':
345
return hexSlice(this, start, end)
346
347
case 'utf8':
348
case 'utf-8':
349
return utf8Slice(this, start, end)
350
351
case 'ascii':
352
return asciiSlice(this, start, end)
353
354
case 'binary':
355
return binarySlice(this, start, end)
356
357
case 'base64':
358
return base64Slice(this, start, end)
359
360
case 'ucs2':
361
case 'ucs-2':
362
case 'utf16le':
363
case 'utf-16le':
364
return utf16leSlice(this, start, end)
365
366
default:
367
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
368
encoding = (encoding + '').toLowerCase()
369
loweredCase = true
370
}
371
}
372
}
373
374
Buffer.prototype.equals = function equals (b) {
375
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
376
if (this === b) return true
377
return Buffer.compare(this, b) === 0
378
}
379
380
Buffer.prototype.inspect = function inspect () {
381
var str = ''
382
var max = exports.INSPECT_MAX_BYTES
383
if (this.length > 0) {
384
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
385
if (this.length > max) str += ' ... '
386
}
387
return '<Buffer ' + str + '>'
388
}
389
390
Buffer.prototype.compare = function compare (b) {
391
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
392
if (this === b) return 0
393
return Buffer.compare(this, b)
394
}
395
396
Buffer.prototype.indexOf = function indexOf (val, byteOffset) {
397
if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff
398
else if (byteOffset < -0x80000000) byteOffset = -0x80000000
399
byteOffset >>= 0
400
401
if (this.length === 0) return -1
402
if (byteOffset >= this.length) return -1
403
404
// Negative offsets start from the end of the buffer
405
if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0)
406
407
if (typeof val === 'string') {
408
if (val.length === 0) return -1 // special case: looking for empty string always fails
409
return String.prototype.indexOf.call(this, val, byteOffset)
410
}
411
if (Buffer.isBuffer(val)) {
412
return arrayIndexOf(this, val, byteOffset)
413
}
414
if (typeof val === 'number') {
415
if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') {
416
return Uint8Array.prototype.indexOf.call(this, val, byteOffset)
417
}
418
return arrayIndexOf(this, [ val ], byteOffset)
419
}
420
421
function arrayIndexOf (arr, val, byteOffset) {
422
var foundIndex = -1
423
for (var i = 0; byteOffset + i < arr.length; i++) {
424
if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) {
425
if (foundIndex === -1) foundIndex = i
426
if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex
427
} else {
428
foundIndex = -1
429
}
430
}
431
return -1
432
}
433
434
throw new TypeError('val must be string, number or Buffer')
435
}
436
437
// `get` will be removed in Node 0.13+
438
Buffer.prototype.get = function get (offset) {
439
console.log('.get() is deprecated. Access using array indexes instead.')
440
return this.readUInt8(offset)
441
}
442
443
// `set` will be removed in Node 0.13+
444
Buffer.prototype.set = function set (v, offset) {
445
console.log('.set() is deprecated. Access using array indexes instead.')
446
return this.writeUInt8(v, offset)
447
}
448
449
function hexWrite (buf, string, offset, length) {
450
offset = Number(offset) || 0
451
var remaining = buf.length - offset
452
if (!length) {
453
length = remaining
454
} else {
455
length = Number(length)
456
if (length > remaining) {
457
length = remaining
458
}
459
}
460
461
// must be an even number of digits
462
var strLen = string.length
463
if (strLen % 2 !== 0) throw new Error('Invalid hex string')
464
465
if (length > strLen / 2) {
466
length = strLen / 2
467
}
468
for (var i = 0; i < length; i++) {
469
var parsed = parseInt(string.substr(i * 2, 2), 16)
470
if (isNaN(parsed)) throw new Error('Invalid hex string')
471
buf[offset + i] = parsed
472
}
473
return i
474
}
475
476
function utf8Write (buf, string, offset, length) {
477
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
478
}
479
480
function asciiWrite (buf, string, offset, length) {
481
return blitBuffer(asciiToBytes(string), buf, offset, length)
482
}
483
484
function binaryWrite (buf, string, offset, length) {
485
return asciiWrite(buf, string, offset, length)
486
}
487
488
function base64Write (buf, string, offset, length) {
489
return blitBuffer(base64ToBytes(string), buf, offset, length)
490
}
491
492
function ucs2Write (buf, string, offset, length) {
493
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
494
}
495
496
Buffer.prototype.write = function write (string, offset, length, encoding) {
497
// Buffer#write(string)
498
if (offset === undefined) {
499
encoding = 'utf8'
500
length = this.length
501
offset = 0
502
// Buffer#write(string, encoding)
503
} else if (length === undefined && typeof offset === 'string') {
504
encoding = offset
505
length = this.length
506
offset = 0
507
// Buffer#write(string, offset[, length][, encoding])
508
} else if (isFinite(offset)) {
509
offset = offset | 0
510
if (isFinite(length)) {
511
length = length | 0
512
if (encoding === undefined) encoding = 'utf8'
513
} else {
514
encoding = length
515
length = undefined
516
}
517
// legacy write(string, encoding, offset, length) - remove in v0.13
518
} else {
519
var swap = encoding
520
encoding = offset
521
offset = length | 0
522
length = swap
523
}
524
525
var remaining = this.length - offset
526
if (length === undefined || length > remaining) length = remaining
527
528
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
529
throw new RangeError('attempt to write outside buffer bounds')
530
}
531
532
if (!encoding) encoding = 'utf8'
533
534
var loweredCase = false
535
for (;;) {
536
switch (encoding) {
537
case 'hex':
538
return hexWrite(this, string, offset, length)
539
540
case 'utf8':
541
case 'utf-8':
542
return utf8Write(this, string, offset, length)
543
544
case 'ascii':
545
return asciiWrite(this, string, offset, length)
546
547
case 'binary':
548
return binaryWrite(this, string, offset, length)
549
550
case 'base64':
551
// Warning: maxLength not taken into account in base64Write
552
return base64Write(this, string, offset, length)
553
554
case 'ucs2':
555
case 'ucs-2':
556
case 'utf16le':
557
case 'utf-16le':
558
return ucs2Write(this, string, offset, length)
559
560
default:
561
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
562
encoding = ('' + encoding).toLowerCase()
563
loweredCase = true
564
}
565
}
566
}
567
568
Buffer.prototype.toJSON = function toJSON () {
569
return {
570
type: 'Buffer',
571
data: Array.prototype.slice.call(this._arr || this, 0)
572
}
573
}
574
575
function base64Slice (buf, start, end) {
576
if (start === 0 && end === buf.length) {
577
return base64.fromByteArray(buf)
578
} else {
579
return base64.fromByteArray(buf.slice(start, end))
580
}
581
}
582
583
function utf8Slice (buf, start, end) {
584
var res = ''
585
var tmp = ''
586
end = Math.min(buf.length, end)
587
588
for (var i = start; i < end; i++) {
589
if (buf[i] <= 0x7F) {
590
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
591
tmp = ''
592
} else {
593
tmp += '%' + buf[i].toString(16)
594
}
595
}
596
597
return res + decodeUtf8Char(tmp)
598
}
599
600
function asciiSlice (buf, start, end) {
601
var ret = ''
602
end = Math.min(buf.length, end)
603
604
for (var i = start; i < end; i++) {
605
ret += String.fromCharCode(buf[i] & 0x7F)
606
}
607
return ret
608
}
609
610
function binarySlice (buf, start, end) {
611
var ret = ''
612
end = Math.min(buf.length, end)
613
614
for (var i = start; i < end; i++) {
615
ret += String.fromCharCode(buf[i])
616
}
617
return ret
618
}
619
620
function hexSlice (buf, start, end) {
621
var len = buf.length
622
623
if (!start || start < 0) start = 0
624
if (!end || end < 0 || end > len) end = len
625
626
var out = ''
627
for (var i = start; i < end; i++) {
628
out += toHex(buf[i])
629
}
630
return out
631
}
632
633
function utf16leSlice (buf, start, end) {
634
var bytes = buf.slice(start, end)
635
var res = ''
636
for (var i = 0; i < bytes.length; i += 2) {
637
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
638
}
639
return res
640
}
641
642
Buffer.prototype.slice = function slice (start, end) {
643
var len = this.length
644
start = ~~start
645
end = end === undefined ? len : ~~end
646
647
if (start < 0) {
648
start += len
649
if (start < 0) start = 0
650
} else if (start > len) {
651
start = len
652
}
653
654
if (end < 0) {
655
end += len
656
if (end < 0) end = 0
657
} else if (end > len) {
658
end = len
659
}
660
661
if (end < start) end = start
662
663
var newBuf
664
if (Buffer.TYPED_ARRAY_SUPPORT) {
665
newBuf = Buffer._augment(this.subarray(start, end))
666
} else {
667
var sliceLen = end - start
668
newBuf = new Buffer(sliceLen, undefined)
669
for (var i = 0; i < sliceLen; i++) {
670
newBuf[i] = this[i + start]
671
}
672
}
673
674
if (newBuf.length) newBuf.parent = this.parent || this
675
676
return newBuf
677
}
678
679
/*
680
* Need to make sure that buffer isn't trying to write out of bounds.
681
*/
682
function checkOffset (offset, ext, length) {
683
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
684
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
685
}
686
687
Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
688
offset = offset | 0
689
byteLength = byteLength | 0
690
if (!noAssert) checkOffset(offset, byteLength, this.length)
691
692
var val = this[offset]
693
var mul = 1
694
var i = 0
695
while (++i < byteLength && (mul *= 0x100)) {
696
val += this[offset + i] * mul
697
}
698
699
return val
700
}
701
702
Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
703
offset = offset | 0
704
byteLength = byteLength | 0
705
if (!noAssert) {
706
checkOffset(offset, byteLength, this.length)
707
}
708
709
var val = this[offset + --byteLength]
710
var mul = 1
711
while (byteLength > 0 && (mul *= 0x100)) {
712
val += this[offset + --byteLength] * mul
713
}
714
715
return val
716
}
717
718
Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
719
if (!noAssert) checkOffset(offset, 1, this.length)
720
return this[offset]
721
}
722
723
Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
724
if (!noAssert) checkOffset(offset, 2, this.length)
725
return this[offset] | (this[offset + 1] << 8)
726
}
727
728
Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
729
if (!noAssert) checkOffset(offset, 2, this.length)
730
return (this[offset] << 8) | this[offset + 1]
731
}
732
733
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
734
if (!noAssert) checkOffset(offset, 4, this.length)
735
736
return ((this[offset]) |
737
(this[offset + 1] << 8) |
738
(this[offset + 2] << 16)) +
739
(this[offset + 3] * 0x1000000)
740
}
741
742
Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
743
if (!noAssert) checkOffset(offset, 4, this.length)
744
745
return (this[offset] * 0x1000000) +
746
((this[offset + 1] << 16) |
747
(this[offset + 2] << 8) |
748
this[offset + 3])
749
}
750
751
Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
752
offset = offset | 0
753
byteLength = byteLength | 0
754
if (!noAssert) checkOffset(offset, byteLength, this.length)
755
756
var val = this[offset]
757
var mul = 1
758
var i = 0
759
while (++i < byteLength && (mul *= 0x100)) {
760
val += this[offset + i] * mul
761
}
762
mul *= 0x80
763
764
if (val >= mul) val -= Math.pow(2, 8 * byteLength)
765
766
return val
767
}
768
769
Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
770
offset = offset | 0
771
byteLength = byteLength | 0
772
if (!noAssert) checkOffset(offset, byteLength, this.length)
773
774
var i = byteLength
775
var mul = 1
776
var val = this[offset + --i]
777
while (i > 0 && (mul *= 0x100)) {
778
val += this[offset + --i] * mul
779
}
780
mul *= 0x80
781
782
if (val >= mul) val -= Math.pow(2, 8 * byteLength)
783
784
return val
785
}
786
787
Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
788
if (!noAssert) checkOffset(offset, 1, this.length)
789
if (!(this[offset] & 0x80)) return (this[offset])
790
return ((0xff - this[offset] + 1) * -1)
791
}
792
793
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
794
if (!noAssert) checkOffset(offset, 2, this.length)
795
var val = this[offset] | (this[offset + 1] << 8)
796
return (val & 0x8000) ? val | 0xFFFF0000 : val
797
}
798
799
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
800
if (!noAssert) checkOffset(offset, 2, this.length)
801
var val = this[offset + 1] | (this[offset] << 8)
802
return (val & 0x8000) ? val | 0xFFFF0000 : val
803
}
804
805
Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
806
if (!noAssert) checkOffset(offset, 4, this.length)
807
808
return (this[offset]) |
809
(this[offset + 1] << 8) |
810
(this[offset + 2] << 16) |
811
(this[offset + 3] << 24)
812
}
813
814
Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
815
if (!noAssert) checkOffset(offset, 4, this.length)
816
817
return (this[offset] << 24) |
818
(this[offset + 1] << 16) |
819
(this[offset + 2] << 8) |
820
(this[offset + 3])
821
}
822
823
Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
824
if (!noAssert) checkOffset(offset, 4, this.length)
825
return ieee754.read(this, offset, true, 23, 4)
826
}
827
828
Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
829
if (!noAssert) checkOffset(offset, 4, this.length)
830
return ieee754.read(this, offset, false, 23, 4)
831
}
832
833
Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
834
if (!noAssert) checkOffset(offset, 8, this.length)
835
return ieee754.read(this, offset, true, 52, 8)
836
}
837
838
Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
839
if (!noAssert) checkOffset(offset, 8, this.length)
840
return ieee754.read(this, offset, false, 52, 8)
841
}
842
843
function checkInt (buf, value, offset, ext, max, min) {
844
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance')
845
if (value > max || value < min) throw new RangeError('value is out of bounds')
846
if (offset + ext > buf.length) throw new RangeError('index out of range')
847
}
848
849
Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
850
value = +value
851
offset = offset | 0
852
byteLength = byteLength | 0
853
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
854
855
var mul = 1
856
var i = 0
857
this[offset] = value & 0xFF
858
while (++i < byteLength && (mul *= 0x100)) {
859
this[offset + i] = (value / mul) & 0xFF
860
}
861
862
return offset + byteLength
863
}
864
865
Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
866
value = +value
867
offset = offset | 0
868
byteLength = byteLength | 0
869
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
870
871
var i = byteLength - 1
872
var mul = 1
873
this[offset + i] = value & 0xFF
874
while (--i >= 0 && (mul *= 0x100)) {
875
this[offset + i] = (value / mul) & 0xFF
876
}
877
878
return offset + byteLength
879
}
880
881
Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
882
value = +value
883
offset = offset | 0
884
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
885
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
886
this[offset] = value
887
return offset + 1
888
}
889
890
function objectWriteUInt16 (buf, value, offset, littleEndian) {
891
if (value < 0) value = 0xffff + value + 1
892
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
893
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
894
(littleEndian ? i : 1 - i) * 8
895
}
896
}
897
898
Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
899
value = +value
900
offset = offset | 0
901
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
902
if (Buffer.TYPED_ARRAY_SUPPORT) {
903
this[offset] = value
904
this[offset + 1] = (value >>> 8)
905
} else {
906
objectWriteUInt16(this, value, offset, true)
907
}
908
return offset + 2
909
}
910
911
Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
912
value = +value
913
offset = offset | 0
914
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
915
if (Buffer.TYPED_ARRAY_SUPPORT) {
916
this[offset] = (value >>> 8)
917
this[offset + 1] = value
918
} else {
919
objectWriteUInt16(this, value, offset, false)
920
}
921
return offset + 2
922
}
923
924
function objectWriteUInt32 (buf, value, offset, littleEndian) {
925
if (value < 0) value = 0xffffffff + value + 1
926
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
927
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
928
}
929
}
930
931
Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
932
value = +value
933
offset = offset | 0
934
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
935
if (Buffer.TYPED_ARRAY_SUPPORT) {
936
this[offset + 3] = (value >>> 24)
937
this[offset + 2] = (value >>> 16)
938
this[offset + 1] = (value >>> 8)
939
this[offset] = value
940
} else {
941
objectWriteUInt32(this, value, offset, true)
942
}
943
return offset + 4
944
}
945
946
Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
947
value = +value
948
offset = offset | 0
949
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
950
if (Buffer.TYPED_ARRAY_SUPPORT) {
951
this[offset] = (value >>> 24)
952
this[offset + 1] = (value >>> 16)
953
this[offset + 2] = (value >>> 8)
954
this[offset + 3] = value
955
} else {
956
objectWriteUInt32(this, value, offset, false)
957
}
958
return offset + 4
959
}
960
961
Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
962
value = +value
963
offset = offset | 0
964
if (!noAssert) {
965
var limit = Math.pow(2, 8 * byteLength - 1)
966
967
checkInt(this, value, offset, byteLength, limit - 1, -limit)
968
}
969
970
var i = 0
971
var mul = 1
972
var sub = value < 0 ? 1 : 0
973
this[offset] = value & 0xFF
974
while (++i < byteLength && (mul *= 0x100)) {
975
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
976
}
977
978
return offset + byteLength
979
}
980
981
Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
982
value = +value
983
offset = offset | 0
984
if (!noAssert) {
985
var limit = Math.pow(2, 8 * byteLength - 1)
986
987
checkInt(this, value, offset, byteLength, limit - 1, -limit)
988
}
989
990
var i = byteLength - 1
991
var mul = 1
992
var sub = value < 0 ? 1 : 0
993
this[offset + i] = value & 0xFF
994
while (--i >= 0 && (mul *= 0x100)) {
995
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
996
}
997
998
return offset + byteLength
999
}
1000
1001
Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
1002
value = +value
1003
offset = offset | 0
1004
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
1005
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
1006
if (value < 0) value = 0xff + value + 1
1007
this[offset] = value
1008
return offset + 1
1009
}
1010
1011
Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
1012
value = +value
1013
offset = offset | 0
1014
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1015
if (Buffer.TYPED_ARRAY_SUPPORT) {
1016
this[offset] = value
1017
this[offset + 1] = (value >>> 8)
1018
} else {
1019
objectWriteUInt16(this, value, offset, true)
1020
}
1021
return offset + 2
1022
}
1023
1024
Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
1025
value = +value
1026
offset = offset | 0
1027
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1028
if (Buffer.TYPED_ARRAY_SUPPORT) {
1029
this[offset] = (value >>> 8)
1030
this[offset + 1] = value
1031
} else {
1032
objectWriteUInt16(this, value, offset, false)
1033
}
1034
return offset + 2
1035
}
1036
1037
Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
1038
value = +value
1039
offset = offset | 0
1040
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1041
if (Buffer.TYPED_ARRAY_SUPPORT) {
1042
this[offset] = value
1043
this[offset + 1] = (value >>> 8)
1044
this[offset + 2] = (value >>> 16)
1045
this[offset + 3] = (value >>> 24)
1046
} else {
1047
objectWriteUInt32(this, value, offset, true)
1048
}
1049
return offset + 4
1050
}
1051
1052
Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
1053
value = +value
1054
offset = offset | 0
1055
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1056
if (value < 0) value = 0xffffffff + value + 1
1057
if (Buffer.TYPED_ARRAY_SUPPORT) {
1058
this[offset] = (value >>> 24)
1059
this[offset + 1] = (value >>> 16)
1060
this[offset + 2] = (value >>> 8)
1061
this[offset + 3] = value
1062
} else {
1063
objectWriteUInt32(this, value, offset, false)
1064
}
1065
return offset + 4
1066
}
1067
1068
function checkIEEE754 (buf, value, offset, ext, max, min) {
1069
if (value > max || value < min) throw new RangeError('value is out of bounds')
1070
if (offset + ext > buf.length) throw new RangeError('index out of range')
1071
if (offset < 0) throw new RangeError('index out of range')
1072
}
1073
1074
function writeFloat (buf, value, offset, littleEndian, noAssert) {
1075
if (!noAssert) {
1076
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
1077
}
1078
ieee754.write(buf, value, offset, littleEndian, 23, 4)
1079
return offset + 4
1080
}
1081
1082
Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
1083
return writeFloat(this, value, offset, true, noAssert)
1084
}
1085
1086
Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
1087
return writeFloat(this, value, offset, false, noAssert)
1088
}
1089
1090
function writeDouble (buf, value, offset, littleEndian, noAssert) {
1091
if (!noAssert) {
1092
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
1093
}
1094
ieee754.write(buf, value, offset, littleEndian, 52, 8)
1095
return offset + 8
1096
}
1097
1098
Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
1099
return writeDouble(this, value, offset, true, noAssert)
1100
}
1101
1102
Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
1103
return writeDouble(this, value, offset, false, noAssert)
1104
}
1105
1106
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
1107
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
1108
if (!start) start = 0
1109
if (!end && end !== 0) end = this.length
1110
if (targetStart >= target.length) targetStart = target.length
1111
if (!targetStart) targetStart = 0
1112
if (end > 0 && end < start) end = start
1113
1114
// Copy 0 bytes; we're done
1115
if (end === start) return 0
1116
if (target.length === 0 || this.length === 0) return 0
1117
1118
// Fatal error conditions
1119
if (targetStart < 0) {
1120
throw new RangeError('targetStart out of bounds')
1121
}
1122
if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
1123
if (end < 0) throw new RangeError('sourceEnd out of bounds')
1124
1125
// Are we oob?
1126
if (end > this.length) end = this.length
1127
if (target.length - targetStart < end - start) {
1128
end = target.length - targetStart + start
1129
}
1130
1131
var len = end - start
1132
1133
if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
1134
for (var i = 0; i < len; i++) {
1135
target[i + targetStart] = this[i + start]
1136
}
1137
} else {
1138
target._set(this.subarray(start, start + len), targetStart)
1139
}
1140
1141
return len
1142
}
1143
1144
// fill(value, start=0, end=buffer.length)
1145
Buffer.prototype.fill = function fill (value, start, end) {
1146
if (!value) value = 0
1147
if (!start) start = 0
1148
if (!end) end = this.length
1149
1150
if (end < start) throw new RangeError('end < start')
1151
1152
// Fill 0 bytes; we're done
1153
if (end === start) return
1154
if (this.length === 0) return
1155
1156
if (start < 0 || start >= this.length) throw new RangeError('start out of bounds')
1157
if (end < 0 || end > this.length) throw new RangeError('end out of bounds')
1158
1159
var i
1160
if (typeof value === 'number') {
1161
for (i = start; i < end; i++) {
1162
this[i] = value
1163
}
1164
} else {
1165
var bytes = utf8ToBytes(value.toString())
1166
var len = bytes.length
1167
for (i = start; i < end; i++) {
1168
this[i] = bytes[i % len]
1169
}
1170
}
1171
1172
return this
1173
}
1174
1175
/**
1176
* Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.
1177
* Added in Node 0.12. Only available in browsers that support ArrayBuffer.
1178
*/
1179
Buffer.prototype.toArrayBuffer = function toArrayBuffer () {
1180
if (typeof Uint8Array !== 'undefined') {
1181
if (Buffer.TYPED_ARRAY_SUPPORT) {
1182
return (new Buffer(this)).buffer
1183
} else {
1184
var buf = new Uint8Array(this.length)
1185
for (var i = 0, len = buf.length; i < len; i += 1) {
1186
buf[i] = this[i]
1187
}
1188
return buf.buffer
1189
}
1190
} else {
1191
throw new TypeError('Buffer.toArrayBuffer not supported in this browser')
1192
}
1193
}
1194
1195
// HELPER FUNCTIONS
1196
// ================
1197
1198
var BP = Buffer.prototype
1199
1200
/**
1201
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods
1202
*/
1203
Buffer._augment = function _augment (arr) {
1204
arr.constructor = Buffer
1205
arr._isBuffer = true
1206
1207
// save reference to original Uint8Array set method before overwriting
1208
arr._set = arr.set
1209
1210
// deprecated, will be removed in node 0.13+
1211
arr.get = BP.get
1212
arr.set = BP.set
1213
1214
arr.write = BP.write
1215
arr.toString = BP.toString
1216
arr.toLocaleString = BP.toString
1217
arr.toJSON = BP.toJSON
1218
arr.equals = BP.equals
1219
arr.compare = BP.compare
1220
arr.indexOf = BP.indexOf
1221
arr.copy = BP.copy
1222
arr.slice = BP.slice
1223
arr.readUIntLE = BP.readUIntLE
1224
arr.readUIntBE = BP.readUIntBE
1225
arr.readUInt8 = BP.readUInt8
1226
arr.readUInt16LE = BP.readUInt16LE
1227
arr.readUInt16BE = BP.readUInt16BE
1228
arr.readUInt32LE = BP.readUInt32LE
1229
arr.readUInt32BE = BP.readUInt32BE
1230
arr.readIntLE = BP.readIntLE
1231
arr.readIntBE = BP.readIntBE
1232
arr.readInt8 = BP.readInt8
1233
arr.readInt16LE = BP.readInt16LE
1234
arr.readInt16BE = BP.readInt16BE
1235
arr.readInt32LE = BP.readInt32LE
1236
arr.readInt32BE = BP.readInt32BE
1237
arr.readFloatLE = BP.readFloatLE
1238
arr.readFloatBE = BP.readFloatBE
1239
arr.readDoubleLE = BP.readDoubleLE
1240
arr.readDoubleBE = BP.readDoubleBE
1241
arr.writeUInt8 = BP.writeUInt8
1242
arr.writeUIntLE = BP.writeUIntLE
1243
arr.writeUIntBE = BP.writeUIntBE
1244
arr.writeUInt16LE = BP.writeUInt16LE
1245
arr.writeUInt16BE = BP.writeUInt16BE
1246
arr.writeUInt32LE = BP.writeUInt32LE
1247
arr.writeUInt32BE = BP.writeUInt32BE
1248
arr.writeIntLE = BP.writeIntLE
1249
arr.writeIntBE = BP.writeIntBE
1250
arr.writeInt8 = BP.writeInt8
1251
arr.writeInt16LE = BP.writeInt16LE
1252
arr.writeInt16BE = BP.writeInt16BE
1253
arr.writeInt32LE = BP.writeInt32LE
1254
arr.writeInt32BE = BP.writeInt32BE
1255
arr.writeFloatLE = BP.writeFloatLE
1256
arr.writeFloatBE = BP.writeFloatBE
1257
arr.writeDoubleLE = BP.writeDoubleLE
1258
arr.writeDoubleBE = BP.writeDoubleBE
1259
arr.fill = BP.fill
1260
arr.inspect = BP.inspect
1261
arr.toArrayBuffer = BP.toArrayBuffer
1262
1263
return arr
1264
}
1265
1266
var INVALID_BASE64_RE = /[^+\/0-9A-z\-]/g
1267
1268
function base64clean (str) {
1269
// Node strips out invalid characters like \n and \t from the string, base64-js does not
1270
str = stringtrim(str).replace(INVALID_BASE64_RE, '')
1271
// Node converts strings with length < 2 to ''
1272
if (str.length < 2) return ''
1273
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
1274
while (str.length % 4 !== 0) {
1275
str = str + '='
1276
}
1277
return str
1278
}
1279
1280
function stringtrim (str) {
1281
if (str.trim) return str.trim()
1282
return str.replace(/^\s+|\s+$/g, '')
1283
}
1284
1285
function toHex (n) {
1286
if (n < 16) return '0' + n.toString(16)
1287
return n.toString(16)
1288
}
1289
1290
function utf8ToBytes (string, units) {
1291
units = units || Infinity
1292
var codePoint
1293
var length = string.length
1294
var leadSurrogate = null
1295
var bytes = []
1296
var i = 0
1297
1298
for (; i < length; i++) {
1299
codePoint = string.charCodeAt(i)
1300
1301
// is surrogate component
1302
if (codePoint > 0xD7FF && codePoint < 0xE000) {
1303
// last char was a lead
1304
if (leadSurrogate) {
1305
// 2 leads in a row
1306
if (codePoint < 0xDC00) {
1307
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1308
leadSurrogate = codePoint
1309
continue
1310
} else {
1311
// valid surrogate pair
1312
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
1313
leadSurrogate = null
1314
}
1315
} else {
1316
// no lead yet
1317
1318
if (codePoint > 0xDBFF) {
1319
// unexpected trail
1320
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1321
continue
1322
} else if (i + 1 === length) {
1323
// unpaired lead
1324
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1325
continue
1326
} else {
1327
// valid lead
1328
leadSurrogate = codePoint
1329
continue
1330
}
1331
}
1332
} else if (leadSurrogate) {
1333
// valid bmp char, but last char was a lead
1334
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1335
leadSurrogate = null
1336
}
1337
1338
// encode utf8
1339
if (codePoint < 0x80) {
1340
if ((units -= 1) < 0) break
1341
bytes.push(codePoint)
1342
} else if (codePoint < 0x800) {
1343
if ((units -= 2) < 0) break
1344
bytes.push(
1345
codePoint >> 0x6 | 0xC0,
1346
codePoint & 0x3F | 0x80
1347
)
1348
} else if (codePoint < 0x10000) {
1349
if ((units -= 3) < 0) break
1350
bytes.push(
1351
codePoint >> 0xC | 0xE0,
1352
codePoint >> 0x6 & 0x3F | 0x80,
1353
codePoint & 0x3F | 0x80
1354
)
1355
} else if (codePoint < 0x200000) {
1356
if ((units -= 4) < 0) break
1357
bytes.push(
1358
codePoint >> 0x12 | 0xF0,
1359
codePoint >> 0xC & 0x3F | 0x80,
1360
codePoint >> 0x6 & 0x3F | 0x80,
1361
codePoint & 0x3F | 0x80
1362
)
1363
} else {
1364
throw new Error('Invalid code point')
1365
}
1366
}
1367
1368
return bytes
1369
}
1370
1371
function asciiToBytes (str) {
1372
var byteArray = []
1373
for (var i = 0; i < str.length; i++) {
1374
// Node's code seems to be doing this and not & 0x7F..
1375
byteArray.push(str.charCodeAt(i) & 0xFF)
1376
}
1377
return byteArray
1378
}
1379
1380
function utf16leToBytes (str, units) {
1381
var c, hi, lo
1382
var byteArray = []
1383
for (var i = 0; i < str.length; i++) {
1384
if ((units -= 2) < 0) break
1385
1386
c = str.charCodeAt(i)
1387
hi = c >> 8
1388
lo = c % 256
1389
byteArray.push(lo)
1390
byteArray.push(hi)
1391
}
1392
1393
return byteArray
1394
}
1395
1396
function base64ToBytes (str) {
1397
return base64.toByteArray(base64clean(str))
1398
}
1399
1400
function blitBuffer (src, dst, offset, length) {
1401
for (var i = 0; i < length; i++) {
1402
if ((i + offset >= dst.length) || (i >= src.length)) break
1403
dst[i + offset] = src[i]
1404
}
1405
return i
1406
}
1407
1408
function decodeUtf8Char (str) {
1409
try {
1410
return decodeURIComponent(str)
1411
} catch (err) {
1412
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
1413
}
1414
}
1415
1416