Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 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 = Buffer
14
exports.INSPECT_MAX_BYTES = 50
15
Buffer.poolSize = 8192 // not used by this implementation
16
17
var kMaxLength = 0x3fffffff
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
* Note:
28
*
29
* - Implementation must support adding new properties to `Uint8Array` instances.
30
* Firefox 4-29 lacked support, fixed in Firefox 30+.
31
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
32
*
33
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
34
*
35
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
36
* incorrect length in some situations.
37
*
38
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will
39
* get the Object implementation, which is slower but will work correctly.
40
*/
41
Buffer.TYPED_ARRAY_SUPPORT = (function () {
42
try {
43
var buf = new ArrayBuffer(0)
44
var arr = new Uint8Array(buf)
45
arr.foo = function () { return 42 }
46
return 42 === arr.foo() && // typed array instances can be augmented
47
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
48
new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
49
} catch (e) {
50
return false
51
}
52
})()
53
54
/**
55
* Class: Buffer
56
* =============
57
*
58
* The Buffer constructor returns instances of `Uint8Array` that are augmented
59
* with function properties for all the node `Buffer` API functions. We use
60
* `Uint8Array` so that square bracket notation works as expected -- it returns
61
* a single octet.
62
*
63
* By augmenting the instances, we can avoid modifying the `Uint8Array`
64
* prototype.
65
*/
66
function Buffer (subject, encoding, noZero) {
67
if (!(this instanceof Buffer))
68
return new Buffer(subject, encoding, noZero)
69
70
var type = typeof subject
71
72
// Find the length
73
var length
74
if (type === 'number')
75
length = subject > 0 ? subject >>> 0 : 0
76
else if (type === 'string') {
77
if (encoding === 'base64')
78
subject = base64clean(subject)
79
length = Buffer.byteLength(subject, encoding)
80
} else if (type === 'object' && subject !== null) { // assume object is array-like
81
if (subject.type === 'Buffer' && isArray(subject.data))
82
subject = subject.data
83
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
84
} else
85
throw new TypeError('must start with number, buffer, array or string')
86
87
if (this.length > kMaxLength)
88
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
89
'size: 0x' + kMaxLength.toString(16) + ' bytes')
90
91
var buf
92
if (Buffer.TYPED_ARRAY_SUPPORT) {
93
// Preferred: Return an augmented `Uint8Array` instance for best performance
94
buf = Buffer._augment(new Uint8Array(length))
95
} else {
96
// Fallback: Return THIS instance of Buffer (created by `new`)
97
buf = this
98
buf.length = length
99
buf._isBuffer = true
100
}
101
102
var i
103
if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
104
// Speed optimization -- use set if we're copying from a typed array
105
buf._set(subject)
106
} else if (isArrayish(subject)) {
107
// Treat array-ish objects as a byte array
108
if (Buffer.isBuffer(subject)) {
109
for (i = 0; i < length; i++)
110
buf[i] = subject.readUInt8(i)
111
} else {
112
for (i = 0; i < length; i++)
113
buf[i] = ((subject[i] % 256) + 256) % 256
114
}
115
} else if (type === 'string') {
116
buf.write(subject, 0, encoding)
117
} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {
118
for (i = 0; i < length; i++) {
119
buf[i] = 0
120
}
121
}
122
123
return buf
124
}
125
126
Buffer.isBuffer = function (b) {
127
return !!(b != null && b._isBuffer)
128
}
129
130
Buffer.compare = function (a, b) {
131
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b))
132
throw new TypeError('Arguments must be Buffers')
133
134
var x = a.length
135
var y = b.length
136
for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {}
137
if (i !== len) {
138
x = a[i]
139
y = b[i]
140
}
141
if (x < y) return -1
142
if (y < x) return 1
143
return 0
144
}
145
146
Buffer.isEncoding = function (encoding) {
147
switch (String(encoding).toLowerCase()) {
148
case 'hex':
149
case 'utf8':
150
case 'utf-8':
151
case 'ascii':
152
case 'binary':
153
case 'base64':
154
case 'raw':
155
case 'ucs2':
156
case 'ucs-2':
157
case 'utf16le':
158
case 'utf-16le':
159
return true
160
default:
161
return false
162
}
163
}
164
165
Buffer.concat = function (list, totalLength) {
166
if (!isArray(list)) throw new TypeError('Usage: Buffer.concat(list[, length])')
167
168
if (list.length === 0) {
169
return new Buffer(0)
170
} else if (list.length === 1) {
171
return list[0]
172
}
173
174
var i
175
if (totalLength === undefined) {
176
totalLength = 0
177
for (i = 0; i < list.length; i++) {
178
totalLength += list[i].length
179
}
180
}
181
182
var buf = new Buffer(totalLength)
183
var pos = 0
184
for (i = 0; i < list.length; i++) {
185
var item = list[i]
186
item.copy(buf, pos)
187
pos += item.length
188
}
189
return buf
190
}
191
192
Buffer.byteLength = function (str, encoding) {
193
var ret
194
str = str + ''
195
switch (encoding || 'utf8') {
196
case 'ascii':
197
case 'binary':
198
case 'raw':
199
ret = str.length
200
break
201
case 'ucs2':
202
case 'ucs-2':
203
case 'utf16le':
204
case 'utf-16le':
205
ret = str.length * 2
206
break
207
case 'hex':
208
ret = str.length >>> 1
209
break
210
case 'utf8':
211
case 'utf-8':
212
ret = utf8ToBytes(str).length
213
break
214
case 'base64':
215
ret = base64ToBytes(str).length
216
break
217
default:
218
ret = str.length
219
}
220
return ret
221
}
222
223
// pre-set for values that may exist in the future
224
Buffer.prototype.length = undefined
225
Buffer.prototype.parent = undefined
226
227
// toString(encoding, start=0, end=buffer.length)
228
Buffer.prototype.toString = function (encoding, start, end) {
229
var loweredCase = false
230
231
start = start >>> 0
232
end = end === undefined || end === Infinity ? this.length : end >>> 0
233
234
if (!encoding) encoding = 'utf8'
235
if (start < 0) start = 0
236
if (end > this.length) end = this.length
237
if (end <= start) return ''
238
239
while (true) {
240
switch (encoding) {
241
case 'hex':
242
return hexSlice(this, start, end)
243
244
case 'utf8':
245
case 'utf-8':
246
return utf8Slice(this, start, end)
247
248
case 'ascii':
249
return asciiSlice(this, start, end)
250
251
case 'binary':
252
return binarySlice(this, start, end)
253
254
case 'base64':
255
return base64Slice(this, start, end)
256
257
case 'ucs2':
258
case 'ucs-2':
259
case 'utf16le':
260
case 'utf-16le':
261
return utf16leSlice(this, start, end)
262
263
default:
264
if (loweredCase)
265
throw new TypeError('Unknown encoding: ' + encoding)
266
encoding = (encoding + '').toLowerCase()
267
loweredCase = true
268
}
269
}
270
}
271
272
Buffer.prototype.equals = function (b) {
273
if(!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
274
return Buffer.compare(this, b) === 0
275
}
276
277
Buffer.prototype.inspect = function () {
278
var str = ''
279
var max = exports.INSPECT_MAX_BYTES
280
if (this.length > 0) {
281
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
282
if (this.length > max)
283
str += ' ... '
284
}
285
return '<Buffer ' + str + '>'
286
}
287
288
Buffer.prototype.compare = function (b) {
289
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
290
return Buffer.compare(this, b)
291
}
292
293
// `get` will be removed in Node 0.13+
294
Buffer.prototype.get = function (offset) {
295
console.log('.get() is deprecated. Access using array indexes instead.')
296
return this.readUInt8(offset)
297
}
298
299
// `set` will be removed in Node 0.13+
300
Buffer.prototype.set = function (v, offset) {
301
console.log('.set() is deprecated. Access using array indexes instead.')
302
return this.writeUInt8(v, offset)
303
}
304
305
function hexWrite (buf, string, offset, length) {
306
offset = Number(offset) || 0
307
var remaining = buf.length - offset
308
if (!length) {
309
length = remaining
310
} else {
311
length = Number(length)
312
if (length > remaining) {
313
length = remaining
314
}
315
}
316
317
// must be an even number of digits
318
var strLen = string.length
319
if (strLen % 2 !== 0) throw new Error('Invalid hex string')
320
321
if (length > strLen / 2) {
322
length = strLen / 2
323
}
324
for (var i = 0; i < length; i++) {
325
var byte = parseInt(string.substr(i * 2, 2), 16)
326
if (isNaN(byte)) throw new Error('Invalid hex string')
327
buf[offset + i] = byte
328
}
329
return i
330
}
331
332
function utf8Write (buf, string, offset, length) {
333
var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length)
334
return charsWritten
335
}
336
337
function asciiWrite (buf, string, offset, length) {
338
var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length)
339
return charsWritten
340
}
341
342
function binaryWrite (buf, string, offset, length) {
343
return asciiWrite(buf, string, offset, length)
344
}
345
346
function base64Write (buf, string, offset, length) {
347
var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)
348
return charsWritten
349
}
350
351
function utf16leWrite (buf, string, offset, length) {
352
var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length, 2)
353
return charsWritten
354
}
355
356
Buffer.prototype.write = function (string, offset, length, encoding) {
357
// Support both (string, offset, length, encoding)
358
// and the legacy (string, encoding, offset, length)
359
if (isFinite(offset)) {
360
if (!isFinite(length)) {
361
encoding = length
362
length = undefined
363
}
364
} else { // legacy
365
var swap = encoding
366
encoding = offset
367
offset = length
368
length = swap
369
}
370
371
offset = Number(offset) || 0
372
var remaining = this.length - offset
373
if (!length) {
374
length = remaining
375
} else {
376
length = Number(length)
377
if (length > remaining) {
378
length = remaining
379
}
380
}
381
encoding = String(encoding || 'utf8').toLowerCase()
382
383
var ret
384
switch (encoding) {
385
case 'hex':
386
ret = hexWrite(this, string, offset, length)
387
break
388
case 'utf8':
389
case 'utf-8':
390
ret = utf8Write(this, string, offset, length)
391
break
392
case 'ascii':
393
ret = asciiWrite(this, string, offset, length)
394
break
395
case 'binary':
396
ret = binaryWrite(this, string, offset, length)
397
break
398
case 'base64':
399
ret = base64Write(this, string, offset, length)
400
break
401
case 'ucs2':
402
case 'ucs-2':
403
case 'utf16le':
404
case 'utf-16le':
405
ret = utf16leWrite(this, string, offset, length)
406
break
407
default:
408
throw new TypeError('Unknown encoding: ' + encoding)
409
}
410
return ret
411
}
412
413
Buffer.prototype.toJSON = function () {
414
return {
415
type: 'Buffer',
416
data: Array.prototype.slice.call(this._arr || this, 0)
417
}
418
}
419
420
function base64Slice (buf, start, end) {
421
if (start === 0 && end === buf.length) {
422
return base64.fromByteArray(buf)
423
} else {
424
return base64.fromByteArray(buf.slice(start, end))
425
}
426
}
427
428
function utf8Slice (buf, start, end) {
429
var res = ''
430
var tmp = ''
431
end = Math.min(buf.length, end)
432
433
for (var i = start; i < end; i++) {
434
if (buf[i] <= 0x7F) {
435
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
436
tmp = ''
437
} else {
438
tmp += '%' + buf[i].toString(16)
439
}
440
}
441
442
return res + decodeUtf8Char(tmp)
443
}
444
445
function asciiSlice (buf, start, end) {
446
var ret = ''
447
end = Math.min(buf.length, end)
448
449
for (var i = start; i < end; i++) {
450
ret += String.fromCharCode(buf[i])
451
}
452
return ret
453
}
454
455
function binarySlice (buf, start, end) {
456
return asciiSlice(buf, start, end)
457
}
458
459
function hexSlice (buf, start, end) {
460
var len = buf.length
461
462
if (!start || start < 0) start = 0
463
if (!end || end < 0 || end > len) end = len
464
465
var out = ''
466
for (var i = start; i < end; i++) {
467
out += toHex(buf[i])
468
}
469
return out
470
}
471
472
function utf16leSlice (buf, start, end) {
473
var bytes = buf.slice(start, end)
474
var res = ''
475
for (var i = 0; i < bytes.length; i += 2) {
476
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
477
}
478
return res
479
}
480
481
Buffer.prototype.slice = function (start, end) {
482
var len = this.length
483
start = ~~start
484
end = end === undefined ? len : ~~end
485
486
if (start < 0) {
487
start += len;
488
if (start < 0)
489
start = 0
490
} else if (start > len) {
491
start = len
492
}
493
494
if (end < 0) {
495
end += len
496
if (end < 0)
497
end = 0
498
} else if (end > len) {
499
end = len
500
}
501
502
if (end < start)
503
end = start
504
505
if (Buffer.TYPED_ARRAY_SUPPORT) {
506
return Buffer._augment(this.subarray(start, end))
507
} else {
508
var sliceLen = end - start
509
var newBuf = new Buffer(sliceLen, undefined, true)
510
for (var i = 0; i < sliceLen; i++) {
511
newBuf[i] = this[i + start]
512
}
513
return newBuf
514
}
515
}
516
517
/*
518
* Need to make sure that buffer isn't trying to write out of bounds.
519
*/
520
function checkOffset (offset, ext, length) {
521
if ((offset % 1) !== 0 || offset < 0)
522
throw new RangeError('offset is not uint')
523
if (offset + ext > length)
524
throw new RangeError('Trying to access beyond buffer length')
525
}
526
527
Buffer.prototype.readUInt8 = function (offset, noAssert) {
528
if (!noAssert)
529
checkOffset(offset, 1, this.length)
530
return this[offset]
531
}
532
533
Buffer.prototype.readUInt16LE = function (offset, noAssert) {
534
if (!noAssert)
535
checkOffset(offset, 2, this.length)
536
return this[offset] | (this[offset + 1] << 8)
537
}
538
539
Buffer.prototype.readUInt16BE = function (offset, noAssert) {
540
if (!noAssert)
541
checkOffset(offset, 2, this.length)
542
return (this[offset] << 8) | this[offset + 1]
543
}
544
545
Buffer.prototype.readUInt32LE = function (offset, noAssert) {
546
if (!noAssert)
547
checkOffset(offset, 4, this.length)
548
549
return ((this[offset]) |
550
(this[offset + 1] << 8) |
551
(this[offset + 2] << 16)) +
552
(this[offset + 3] * 0x1000000)
553
}
554
555
Buffer.prototype.readUInt32BE = function (offset, noAssert) {
556
if (!noAssert)
557
checkOffset(offset, 4, this.length)
558
559
return (this[offset] * 0x1000000) +
560
((this[offset + 1] << 16) |
561
(this[offset + 2] << 8) |
562
this[offset + 3])
563
}
564
565
Buffer.prototype.readInt8 = function (offset, noAssert) {
566
if (!noAssert)
567
checkOffset(offset, 1, this.length)
568
if (!(this[offset] & 0x80))
569
return (this[offset])
570
return ((0xff - this[offset] + 1) * -1)
571
}
572
573
Buffer.prototype.readInt16LE = function (offset, noAssert) {
574
if (!noAssert)
575
checkOffset(offset, 2, this.length)
576
var val = this[offset] | (this[offset + 1] << 8)
577
return (val & 0x8000) ? val | 0xFFFF0000 : val
578
}
579
580
Buffer.prototype.readInt16BE = function (offset, noAssert) {
581
if (!noAssert)
582
checkOffset(offset, 2, this.length)
583
var val = this[offset + 1] | (this[offset] << 8)
584
return (val & 0x8000) ? val | 0xFFFF0000 : val
585
}
586
587
Buffer.prototype.readInt32LE = function (offset, noAssert) {
588
if (!noAssert)
589
checkOffset(offset, 4, this.length)
590
591
return (this[offset]) |
592
(this[offset + 1] << 8) |
593
(this[offset + 2] << 16) |
594
(this[offset + 3] << 24)
595
}
596
597
Buffer.prototype.readInt32BE = function (offset, noAssert) {
598
if (!noAssert)
599
checkOffset(offset, 4, this.length)
600
601
return (this[offset] << 24) |
602
(this[offset + 1] << 16) |
603
(this[offset + 2] << 8) |
604
(this[offset + 3])
605
}
606
607
Buffer.prototype.readFloatLE = function (offset, noAssert) {
608
if (!noAssert)
609
checkOffset(offset, 4, this.length)
610
return ieee754.read(this, offset, true, 23, 4)
611
}
612
613
Buffer.prototype.readFloatBE = function (offset, noAssert) {
614
if (!noAssert)
615
checkOffset(offset, 4, this.length)
616
return ieee754.read(this, offset, false, 23, 4)
617
}
618
619
Buffer.prototype.readDoubleLE = function (offset, noAssert) {
620
if (!noAssert)
621
checkOffset(offset, 8, this.length)
622
return ieee754.read(this, offset, true, 52, 8)
623
}
624
625
Buffer.prototype.readDoubleBE = function (offset, noAssert) {
626
if (!noAssert)
627
checkOffset(offset, 8, this.length)
628
return ieee754.read(this, offset, false, 52, 8)
629
}
630
631
function checkInt (buf, value, offset, ext, max, min) {
632
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance')
633
if (value > max || value < min) throw new TypeError('value is out of bounds')
634
if (offset + ext > buf.length) throw new TypeError('index out of range')
635
}
636
637
Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {
638
value = +value
639
offset = offset >>> 0
640
if (!noAssert)
641
checkInt(this, value, offset, 1, 0xff, 0)
642
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
643
this[offset] = value
644
return offset + 1
645
}
646
647
function objectWriteUInt16 (buf, value, offset, littleEndian) {
648
if (value < 0) value = 0xffff + value + 1
649
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
650
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
651
(littleEndian ? i : 1 - i) * 8
652
}
653
}
654
655
Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) {
656
value = +value
657
offset = offset >>> 0
658
if (!noAssert)
659
checkInt(this, value, offset, 2, 0xffff, 0)
660
if (Buffer.TYPED_ARRAY_SUPPORT) {
661
this[offset] = value
662
this[offset + 1] = (value >>> 8)
663
} else objectWriteUInt16(this, value, offset, true)
664
return offset + 2
665
}
666
667
Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) {
668
value = +value
669
offset = offset >>> 0
670
if (!noAssert)
671
checkInt(this, value, offset, 2, 0xffff, 0)
672
if (Buffer.TYPED_ARRAY_SUPPORT) {
673
this[offset] = (value >>> 8)
674
this[offset + 1] = value
675
} else objectWriteUInt16(this, value, offset, false)
676
return offset + 2
677
}
678
679
function objectWriteUInt32 (buf, value, offset, littleEndian) {
680
if (value < 0) value = 0xffffffff + value + 1
681
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
682
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
683
}
684
}
685
686
Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) {
687
value = +value
688
offset = offset >>> 0
689
if (!noAssert)
690
checkInt(this, value, offset, 4, 0xffffffff, 0)
691
if (Buffer.TYPED_ARRAY_SUPPORT) {
692
this[offset + 3] = (value >>> 24)
693
this[offset + 2] = (value >>> 16)
694
this[offset + 1] = (value >>> 8)
695
this[offset] = value
696
} else objectWriteUInt32(this, value, offset, true)
697
return offset + 4
698
}
699
700
Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) {
701
value = +value
702
offset = offset >>> 0
703
if (!noAssert)
704
checkInt(this, value, offset, 4, 0xffffffff, 0)
705
if (Buffer.TYPED_ARRAY_SUPPORT) {
706
this[offset] = (value >>> 24)
707
this[offset + 1] = (value >>> 16)
708
this[offset + 2] = (value >>> 8)
709
this[offset + 3] = value
710
} else objectWriteUInt32(this, value, offset, false)
711
return offset + 4
712
}
713
714
Buffer.prototype.writeInt8 = function (value, offset, noAssert) {
715
value = +value
716
offset = offset >>> 0
717
if (!noAssert)
718
checkInt(this, value, offset, 1, 0x7f, -0x80)
719
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
720
if (value < 0) value = 0xff + value + 1
721
this[offset] = value
722
return offset + 1
723
}
724
725
Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {
726
value = +value
727
offset = offset >>> 0
728
if (!noAssert)
729
checkInt(this, value, offset, 2, 0x7fff, -0x8000)
730
if (Buffer.TYPED_ARRAY_SUPPORT) {
731
this[offset] = value
732
this[offset + 1] = (value >>> 8)
733
} else objectWriteUInt16(this, value, offset, true)
734
return offset + 2
735
}
736
737
Buffer.prototype.writeInt16BE = function (value, offset, noAssert) {
738
value = +value
739
offset = offset >>> 0
740
if (!noAssert)
741
checkInt(this, value, offset, 2, 0x7fff, -0x8000)
742
if (Buffer.TYPED_ARRAY_SUPPORT) {
743
this[offset] = (value >>> 8)
744
this[offset + 1] = value
745
} else objectWriteUInt16(this, value, offset, false)
746
return offset + 2
747
}
748
749
Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
750
value = +value
751
offset = offset >>> 0
752
if (!noAssert)
753
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
754
if (Buffer.TYPED_ARRAY_SUPPORT) {
755
this[offset] = value
756
this[offset + 1] = (value >>> 8)
757
this[offset + 2] = (value >>> 16)
758
this[offset + 3] = (value >>> 24)
759
} else objectWriteUInt32(this, value, offset, true)
760
return offset + 4
761
}
762
763
Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {
764
value = +value
765
offset = offset >>> 0
766
if (!noAssert)
767
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
768
if (value < 0) value = 0xffffffff + value + 1
769
if (Buffer.TYPED_ARRAY_SUPPORT) {
770
this[offset] = (value >>> 24)
771
this[offset + 1] = (value >>> 16)
772
this[offset + 2] = (value >>> 8)
773
this[offset + 3] = value
774
} else objectWriteUInt32(this, value, offset, false)
775
return offset + 4
776
}
777
778
function checkIEEE754 (buf, value, offset, ext, max, min) {
779
if (value > max || value < min) throw new TypeError('value is out of bounds')
780
if (offset + ext > buf.length) throw new TypeError('index out of range')
781
}
782
783
function writeFloat (buf, value, offset, littleEndian, noAssert) {
784
if (!noAssert)
785
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
786
ieee754.write(buf, value, offset, littleEndian, 23, 4)
787
return offset + 4
788
}
789
790
Buffer.prototype.writeFloatLE = function (value, offset, noAssert) {
791
return writeFloat(this, value, offset, true, noAssert)
792
}
793
794
Buffer.prototype.writeFloatBE = function (value, offset, noAssert) {
795
return writeFloat(this, value, offset, false, noAssert)
796
}
797
798
function writeDouble (buf, value, offset, littleEndian, noAssert) {
799
if (!noAssert)
800
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
801
ieee754.write(buf, value, offset, littleEndian, 52, 8)
802
return offset + 8
803
}
804
805
Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) {
806
return writeDouble(this, value, offset, true, noAssert)
807
}
808
809
Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) {
810
return writeDouble(this, value, offset, false, noAssert)
811
}
812
813
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
814
Buffer.prototype.copy = function (target, target_start, start, end) {
815
var source = this
816
817
if (!start) start = 0
818
if (!end && end !== 0) end = this.length
819
if (!target_start) target_start = 0
820
821
// Copy 0 bytes; we're done
822
if (end === start) return
823
if (target.length === 0 || source.length === 0) return
824
825
// Fatal error conditions
826
if (end < start) throw new TypeError('sourceEnd < sourceStart')
827
if (target_start < 0 || target_start >= target.length)
828
throw new TypeError('targetStart out of bounds')
829
if (start < 0 || start >= source.length) throw new TypeError('sourceStart out of bounds')
830
if (end < 0 || end > source.length) throw new TypeError('sourceEnd out of bounds')
831
832
// Are we oob?
833
if (end > this.length)
834
end = this.length
835
if (target.length - target_start < end - start)
836
end = target.length - target_start + start
837
838
var len = end - start
839
840
if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
841
for (var i = 0; i < len; i++) {
842
target[i + target_start] = this[i + start]
843
}
844
} else {
845
target._set(this.subarray(start, start + len), target_start)
846
}
847
}
848
849
// fill(value, start=0, end=buffer.length)
850
Buffer.prototype.fill = function (value, start, end) {
851
if (!value) value = 0
852
if (!start) start = 0
853
if (!end) end = this.length
854
855
if (end < start) throw new TypeError('end < start')
856
857
// Fill 0 bytes; we're done
858
if (end === start) return
859
if (this.length === 0) return
860
861
if (start < 0 || start >= this.length) throw new TypeError('start out of bounds')
862
if (end < 0 || end > this.length) throw new TypeError('end out of bounds')
863
864
var i
865
if (typeof value === 'number') {
866
for (i = start; i < end; i++) {
867
this[i] = value
868
}
869
} else {
870
var bytes = utf8ToBytes(value.toString())
871
var len = bytes.length
872
for (i = start; i < end; i++) {
873
this[i] = bytes[i % len]
874
}
875
}
876
877
return this
878
}
879
880
/**
881
* Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.
882
* Added in Node 0.12. Only available in browsers that support ArrayBuffer.
883
*/
884
Buffer.prototype.toArrayBuffer = function () {
885
if (typeof Uint8Array !== 'undefined') {
886
if (Buffer.TYPED_ARRAY_SUPPORT) {
887
return (new Buffer(this)).buffer
888
} else {
889
var buf = new Uint8Array(this.length)
890
for (var i = 0, len = buf.length; i < len; i += 1) {
891
buf[i] = this[i]
892
}
893
return buf.buffer
894
}
895
} else {
896
throw new TypeError('Buffer.toArrayBuffer not supported in this browser')
897
}
898
}
899
900
// HELPER FUNCTIONS
901
// ================
902
903
var BP = Buffer.prototype
904
905
/**
906
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods
907
*/
908
Buffer._augment = function (arr) {
909
arr.constructor = Buffer
910
arr._isBuffer = true
911
912
// save reference to original Uint8Array get/set methods before overwriting
913
arr._get = arr.get
914
arr._set = arr.set
915
916
// deprecated, will be removed in node 0.13+
917
arr.get = BP.get
918
arr.set = BP.set
919
920
arr.write = BP.write
921
arr.toString = BP.toString
922
arr.toLocaleString = BP.toString
923
arr.toJSON = BP.toJSON
924
arr.equals = BP.equals
925
arr.compare = BP.compare
926
arr.copy = BP.copy
927
arr.slice = BP.slice
928
arr.readUInt8 = BP.readUInt8
929
arr.readUInt16LE = BP.readUInt16LE
930
arr.readUInt16BE = BP.readUInt16BE
931
arr.readUInt32LE = BP.readUInt32LE
932
arr.readUInt32BE = BP.readUInt32BE
933
arr.readInt8 = BP.readInt8
934
arr.readInt16LE = BP.readInt16LE
935
arr.readInt16BE = BP.readInt16BE
936
arr.readInt32LE = BP.readInt32LE
937
arr.readInt32BE = BP.readInt32BE
938
arr.readFloatLE = BP.readFloatLE
939
arr.readFloatBE = BP.readFloatBE
940
arr.readDoubleLE = BP.readDoubleLE
941
arr.readDoubleBE = BP.readDoubleBE
942
arr.writeUInt8 = BP.writeUInt8
943
arr.writeUInt16LE = BP.writeUInt16LE
944
arr.writeUInt16BE = BP.writeUInt16BE
945
arr.writeUInt32LE = BP.writeUInt32LE
946
arr.writeUInt32BE = BP.writeUInt32BE
947
arr.writeInt8 = BP.writeInt8
948
arr.writeInt16LE = BP.writeInt16LE
949
arr.writeInt16BE = BP.writeInt16BE
950
arr.writeInt32LE = BP.writeInt32LE
951
arr.writeInt32BE = BP.writeInt32BE
952
arr.writeFloatLE = BP.writeFloatLE
953
arr.writeFloatBE = BP.writeFloatBE
954
arr.writeDoubleLE = BP.writeDoubleLE
955
arr.writeDoubleBE = BP.writeDoubleBE
956
arr.fill = BP.fill
957
arr.inspect = BP.inspect
958
arr.toArrayBuffer = BP.toArrayBuffer
959
960
return arr
961
}
962
963
var INVALID_BASE64_RE = /[^+\/0-9A-z]/g
964
965
function base64clean (str) {
966
// Node strips out invalid characters like \n and \t from the string, base64-js does not
967
str = stringtrim(str).replace(INVALID_BASE64_RE, '')
968
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
969
while (str.length % 4 !== 0) {
970
str = str + '='
971
}
972
return str
973
}
974
975
function stringtrim (str) {
976
if (str.trim) return str.trim()
977
return str.replace(/^\s+|\s+$/g, '')
978
}
979
980
function isArrayish (subject) {
981
return isArray(subject) || Buffer.isBuffer(subject) ||
982
subject && typeof subject === 'object' &&
983
typeof subject.length === 'number'
984
}
985
986
function toHex (n) {
987
if (n < 16) return '0' + n.toString(16)
988
return n.toString(16)
989
}
990
991
function utf8ToBytes (str) {
992
var byteArray = []
993
for (var i = 0; i < str.length; i++) {
994
var b = str.charCodeAt(i)
995
if (b <= 0x7F) {
996
byteArray.push(b)
997
} else {
998
var start = i
999
if (b >= 0xD800 && b <= 0xDFFF) i++
1000
var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%')
1001
for (var j = 0; j < h.length; j++) {
1002
byteArray.push(parseInt(h[j], 16))
1003
}
1004
}
1005
}
1006
return byteArray
1007
}
1008
1009
function asciiToBytes (str) {
1010
var byteArray = []
1011
for (var i = 0; i < str.length; i++) {
1012
// Node's code seems to be doing this and not & 0x7F..
1013
byteArray.push(str.charCodeAt(i) & 0xFF)
1014
}
1015
return byteArray
1016
}
1017
1018
function utf16leToBytes (str) {
1019
var c, hi, lo
1020
var byteArray = []
1021
for (var i = 0; i < str.length; i++) {
1022
c = str.charCodeAt(i)
1023
hi = c >> 8
1024
lo = c % 256
1025
byteArray.push(lo)
1026
byteArray.push(hi)
1027
}
1028
1029
return byteArray
1030
}
1031
1032
function base64ToBytes (str) {
1033
return base64.toByteArray(str)
1034
}
1035
1036
function blitBuffer (src, dst, offset, length, unitSize) {
1037
if (unitSize) length -= length % unitSize;
1038
for (var i = 0; i < length; i++) {
1039
if ((i + offset >= dst.length) || (i >= src.length))
1040
break
1041
dst[i + offset] = src[i]
1042
}
1043
return i
1044
}
1045
1046
function decodeUtf8Char (str) {
1047
try {
1048
return decodeURIComponent(str)
1049
} catch (err) {
1050
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
1051
}
1052
}
1053
1054