buffer: fix sign overflow in `readUIn32BE`
[platform/upstream/nodejs.git] / lib / buffer.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var SlowBuffer = process.binding('buffer').SlowBuffer;
23 var assert = require('assert');
24
25 exports.INSPECT_MAX_BYTES = 50;
26
27 // Make SlowBuffer inherit from Buffer.
28 // This is an exception to the rule that __proto__ is not allowed in core.
29 SlowBuffer.prototype.__proto__ = Buffer.prototype;
30
31
32 function clamp(index, len, defaultValue) {
33   if (typeof index !== 'number') return defaultValue;
34   index = ~~index;  // Coerce to integer.
35   if (index >= len) return len;
36   if (index >= 0) return index;
37   index += len;
38   if (index >= 0) return index;
39   return 0;
40 }
41
42
43 function toHex(n) {
44   if (n < 16) return '0' + n.toString(16);
45   return n.toString(16);
46 }
47
48
49 SlowBuffer.prototype.toString = function(encoding, start, end) {
50   encoding = String(encoding || 'utf8').toLowerCase();
51   start = +start || 0;
52   if (typeof end !== 'number') end = this.length;
53
54   // Fastpath empty strings
55   if (+end == start) {
56     return '';
57   }
58
59   switch (encoding) {
60     case 'hex':
61       return this.hexSlice(start, end);
62
63     case 'utf8':
64     case 'utf-8':
65       return this.utf8Slice(start, end);
66
67     case 'ascii':
68       return this.asciiSlice(start, end);
69
70     case 'binary':
71       return this.binarySlice(start, end);
72
73     case 'base64':
74       return this.base64Slice(start, end);
75
76     case 'ucs2':
77     case 'ucs-2':
78     case 'utf16le':
79     case 'utf-16le':
80       return this.ucs2Slice(start, end);
81
82     default:
83       throw new TypeError('Unknown encoding: ' + encoding);
84   }
85 };
86
87
88 SlowBuffer.prototype.write = function(string, offset, length, encoding) {
89   // Support both (string, offset, length, encoding)
90   // and the legacy (string, encoding, offset, length)
91   if (isFinite(offset)) {
92     if (!isFinite(length)) {
93       encoding = length;
94       length = undefined;
95     }
96   } else {  // legacy
97     var swap = encoding;
98     encoding = offset;
99     offset = length;
100     length = swap;
101   }
102
103   offset = +offset || 0;
104   var remaining = this.length - offset;
105   if (!length) {
106     length = remaining;
107   } else {
108     length = +length;
109     if (length > remaining) {
110       length = remaining;
111     }
112   }
113   encoding = String(encoding || 'utf8').toLowerCase();
114
115   switch (encoding) {
116     case 'hex':
117       return this.hexWrite(string, offset, length);
118
119     case 'utf8':
120     case 'utf-8':
121       return this.utf8Write(string, offset, length);
122
123     case 'ascii':
124       return this.asciiWrite(string, offset, length);
125
126     case 'binary':
127       return this.binaryWrite(string, offset, length);
128
129     case 'base64':
130       return this.base64Write(string, offset, length);
131
132     case 'ucs2':
133     case 'ucs-2':
134     case 'utf16le':
135     case 'utf-16le':
136       return this.ucs2Write(string, offset, length);
137
138     default:
139       throw new TypeError('Unknown encoding: ' + encoding);
140   }
141 };
142
143
144 // slice(start, end)
145 SlowBuffer.prototype.slice = function(start, end) {
146   var len = this.length;
147   start = clamp(start, len, 0);
148   end = clamp(end, len, len);
149   return new Buffer(this, end - start, start);
150 };
151
152
153 var zeroBuffer = new SlowBuffer(0);
154
155 // Buffer
156 function Buffer(subject, encoding, offset) {
157   if (!(this instanceof Buffer)) {
158     return new Buffer(subject, encoding, offset);
159   }
160
161   var type;
162
163   // Are we slicing?
164   if (typeof offset === 'number') {
165     if (!Buffer.isBuffer(subject)) {
166       throw new TypeError('First argument must be a Buffer when slicing');
167     }
168
169     this.length = +encoding > 0 ? Math.ceil(encoding) : 0;
170     this.parent = subject.parent ? subject.parent : subject;
171     this.offset = offset;
172   } else {
173     // Find the length
174     switch (type = typeof subject) {
175       case 'number':
176         this.length = +subject > 0 ? Math.ceil(subject) : 0;
177         break;
178
179       case 'string':
180         this.length = Buffer.byteLength(subject, encoding);
181         break;
182
183       case 'object': // Assume object is array-ish
184         this.length = +subject.length > 0 ? Math.ceil(subject.length) : 0;
185         break;
186
187       default:
188         throw new TypeError('First argument needs to be a number, ' +
189                             'array or string.');
190     }
191
192     if (this.length > Buffer.poolSize) {
193       // Big buffer, just alloc one.
194       this.parent = new SlowBuffer(this.length);
195       this.offset = 0;
196
197     } else if (this.length > 0) {
198       // Small buffer.
199       if (!pool || pool.length - pool.used < this.length) allocPool();
200       this.parent = pool;
201       this.offset = pool.used;
202       // Align on 8 byte boundary to avoid alignment issues on ARM.
203       pool.used = (pool.used + this.length + 7) & ~7;
204
205     } else {
206       // Zero-length buffer
207       this.parent = zeroBuffer;
208       this.offset = 0;
209     }
210
211     // optimize by branching logic for new allocations
212     if (typeof subject !== 'number') {
213       if (type === 'string') {
214         // We are a string
215         this.length = this.write(subject, 0, encoding);
216       // if subject is buffer then use built-in copy method
217       } else if (Buffer.isBuffer(subject)) {
218         if (subject.parent)
219           subject.parent.copy(this.parent,
220                               this.offset,
221                               subject.offset,
222                               this.length + subject.offset);
223         else
224           subject.copy(this.parent, this.offset, 0, this.length);
225       } else if (isArrayIsh(subject)) {
226         for (var i = 0; i < this.length; i++)
227           this.parent[i + this.offset] = subject[i];
228       }
229     }
230   }
231
232   SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
233 }
234
235 function isArrayIsh(subject) {
236   return Array.isArray(subject) ||
237          subject && typeof subject === 'object' &&
238          typeof subject.length === 'number';
239 }
240
241 exports.SlowBuffer = SlowBuffer;
242 exports.Buffer = Buffer;
243
244
245 Buffer.isEncoding = function(encoding) {
246   switch (encoding && encoding.toLowerCase()) {
247     case 'hex':
248     case 'utf8':
249     case 'utf-8':
250     case 'ascii':
251     case 'binary':
252     case 'base64':
253     case 'ucs2':
254     case 'ucs-2':
255     case 'utf16le':
256     case 'utf-16le':
257     case 'raw':
258       return true;
259
260     default:
261       return false;
262   }
263 };
264
265
266
267 Buffer.poolSize = 8 * 1024;
268 var pool;
269
270 function allocPool() {
271   pool = new SlowBuffer(Buffer.poolSize);
272   pool.used = 0;
273 }
274
275
276 // Static methods
277 Buffer.isBuffer = function isBuffer(b) {
278   return b instanceof Buffer;
279 };
280
281
282 // Inspect
283 Buffer.prototype.inspect = function inspect() {
284   var out = [],
285       len = this.length,
286       name = this.constructor.name;
287
288   for (var i = 0; i < len; i++) {
289     out[i] = toHex(this[i]);
290     if (i == exports.INSPECT_MAX_BYTES) {
291       out[i + 1] = '...';
292       break;
293     }
294   }
295
296   return '<' + name + ' ' + out.join(' ') + '>';
297 };
298
299
300 Buffer.prototype.get = function get(offset) {
301   if (offset < 0 || offset >= this.length)
302     throw new RangeError('offset is out of bounds');
303   return this.parent[this.offset + offset];
304 };
305
306
307 Buffer.prototype.set = function set(offset, v) {
308   if (offset < 0 || offset >= this.length)
309     throw new RangeError('offset is out of bounds');
310   return this.parent[this.offset + offset] = v;
311 };
312
313
314 // write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
315 Buffer.prototype.write = function(string, offset, length, encoding) {
316   // Support both (string, offset, length, encoding)
317   // and the legacy (string, encoding, offset, length)
318   if (isFinite(offset)) {
319     if (!isFinite(length)) {
320       encoding = length;
321       length = undefined;
322     }
323   } else {  // legacy
324     var swap = encoding;
325     encoding = offset;
326     offset = length;
327     length = swap;
328   }
329
330   offset = +offset || 0;
331   var remaining = this.length - offset;
332   if (!length) {
333     length = remaining;
334   } else {
335     length = +length;
336     if (length > remaining) {
337       length = remaining;
338     }
339   }
340   encoding = String(encoding || 'utf8').toLowerCase();
341
342   if (string.length > 0 && (length < 0 || offset < 0))
343     throw new RangeError('attempt to write beyond buffer bounds');
344
345   var ret;
346   switch (encoding) {
347     case 'hex':
348       ret = this.parent.hexWrite(string, this.offset + offset, length);
349       break;
350
351     case 'utf8':
352     case 'utf-8':
353       ret = this.parent.utf8Write(string, this.offset + offset, length);
354       break;
355
356     case 'ascii':
357       ret = this.parent.asciiWrite(string, this.offset + offset, length);
358       break;
359
360     case 'binary':
361       ret = this.parent.binaryWrite(string, this.offset + offset, length);
362       break;
363
364     case 'base64':
365       // Warning: maxLength not taken into account in base64Write
366       ret = this.parent.base64Write(string, this.offset + offset, length);
367       break;
368
369     case 'ucs2':
370     case 'ucs-2':
371     case 'utf16le':
372     case 'utf-16le':
373       ret = this.parent.ucs2Write(string, this.offset + offset, length);
374       break;
375
376     default:
377       throw new TypeError('Unknown encoding: ' + encoding);
378   }
379
380   Buffer._charsWritten = SlowBuffer._charsWritten;
381
382   return ret;
383 };
384
385
386 Buffer.prototype.toJSON = function() {
387   return Array.prototype.slice.call(this, 0);
388 };
389
390
391 // toString(encoding, start=0, end=buffer.length)
392 Buffer.prototype.toString = function(encoding, start, end) {
393   encoding = String(encoding || 'utf8').toLowerCase();
394
395   if (typeof start !== 'number' || start < 0) {
396     start = 0;
397   } else if (start > this.length) {
398     start = this.length;
399   }
400
401   if (typeof end !== 'number' || end > this.length) {
402     end = this.length;
403   } else if (end < 0) {
404     end = 0;
405   }
406
407   start = start + this.offset;
408   end = end + this.offset;
409
410   switch (encoding) {
411     case 'hex':
412       return this.parent.hexSlice(start, end);
413
414     case 'utf8':
415     case 'utf-8':
416       return this.parent.utf8Slice(start, end);
417
418     case 'ascii':
419       return this.parent.asciiSlice(start, end);
420
421     case 'binary':
422       return this.parent.binarySlice(start, end);
423
424     case 'base64':
425       return this.parent.base64Slice(start, end);
426
427     case 'ucs2':
428     case 'ucs-2':
429     case 'utf16le':
430     case 'utf-16le':
431       return this.parent.ucs2Slice(start, end);
432
433     default:
434       throw new TypeError('Unknown encoding: ' + encoding);
435   }
436 };
437
438
439 // byteLength
440 Buffer.byteLength = SlowBuffer.byteLength;
441
442
443 // fill(value, start=0, end=buffer.length)
444 Buffer.prototype.fill = function fill(value, start, end) {
445   value || (value = 0);
446   start || (start = 0);
447   end || (end = this.length);
448
449   if (typeof value === 'string') {
450     value = value.charCodeAt(0);
451   }
452   if (typeof value !== 'number' || isNaN(value)) {
453     throw new TypeError('value is not a number');
454   }
455
456   if (end < start) throw new RangeError('end < start');
457
458   // Fill 0 bytes; we're done
459   if (end === start) return 0;
460   if (this.length == 0) return 0;
461
462   if (start < 0 || start >= this.length) {
463     throw new RangeError('start out of bounds');
464   }
465
466   if (end < 0 || end > this.length) {
467     throw new RangeError('end out of bounds');
468   }
469
470   return this.parent.fill(value,
471                           start + this.offset,
472                           end + this.offset);
473 };
474
475
476 Buffer.concat = function(list, length) {
477   if (!Array.isArray(list)) {
478     throw new TypeError('Usage: Buffer.concat(list, [length])');
479   }
480
481   if (list.length === 0) {
482     return new Buffer(0);
483   } else if (list.length === 1) {
484     return list[0];
485   }
486
487   if (typeof length !== 'number') {
488     length = 0;
489     for (var i = 0; i < list.length; i++) {
490       var buf = list[i];
491       length += buf.length;
492     }
493   }
494
495   var buffer = new Buffer(length);
496   var pos = 0;
497   for (var i = 0; i < list.length; i++) {
498     var buf = list[i];
499     buf.copy(buffer, pos);
500     pos += buf.length;
501   }
502   return buffer;
503 };
504
505
506
507
508 // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
509 Buffer.prototype.copy = function(target, target_start, start, end) {
510   // set undefined/NaN or out of bounds values equal to their default
511   if (!(target_start >= 0)) target_start = 0;
512   if (!(start >= 0)) start = 0;
513   if (!(end < this.length)) end = this.length;
514
515   // Copy 0 bytes; we're done
516   if (end === start ||
517       target.length === 0 ||
518       this.length === 0 ||
519       start > this.length)
520     return 0;
521
522   if (end < start)
523     throw new RangeError('sourceEnd < sourceStart');
524
525   if (target_start >= target.length)
526     throw new RangeError('targetStart out of bounds');
527
528   if (target.length - target_start < end - start)
529     end = target.length - target_start + start;
530
531   return this.parent.copy(target.parent || target,
532                           target_start + (target.offset || 0),
533                           start + this.offset,
534                           end + this.offset);
535 };
536
537
538 // slice(start, end)
539 Buffer.prototype.slice = function(start, end) {
540   var len = this.length;
541   start = clamp(start, len, 0);
542   end = clamp(end, len, len);
543   return new Buffer(this.parent, end - start, start + this.offset);
544 };
545
546
547 // Legacy methods for backwards compatibility.
548
549 Buffer.prototype.utf8Slice = function(start, end) {
550   return this.toString('utf8', start, end);
551 };
552
553 Buffer.prototype.binarySlice = function(start, end) {
554   return this.toString('binary', start, end);
555 };
556
557 Buffer.prototype.asciiSlice = function(start, end) {
558   return this.toString('ascii', start, end);
559 };
560
561 Buffer.prototype.utf8Write = function(string, offset) {
562   return this.write(string, offset, 'utf8');
563 };
564
565 Buffer.prototype.binaryWrite = function(string, offset) {
566   return this.write(string, offset, 'binary');
567 };
568
569 Buffer.prototype.asciiWrite = function(string, offset) {
570   return this.write(string, offset, 'ascii');
571 };
572
573
574 /*
575  * Need to make sure that buffer isn't trying to write out of bounds.
576  * This check is far too slow internally for fast buffers.
577  */
578 function checkOffset(offset, ext, length) {
579   if ((offset % 1) !== 0 || offset < 0)
580     throw new RangeError('offset is not uint');
581   if (offset + ext > length)
582     throw new RangeError('Trying to access beyond buffer length');
583 }
584
585
586 Buffer.prototype.readUInt8 = function(offset, noAssert) {
587   if (!noAssert)
588     checkOffset(offset, 1, this.length);
589   return this[offset];
590 };
591
592
593 Buffer.prototype.readUInt16LE = function(offset, noAssert) {
594   if (!noAssert)
595     checkOffset(offset, 2, this.length);
596   return this[offset] | (this[offset + 1] << 8);
597 };
598
599
600 Buffer.prototype.readUInt16BE = function(offset, noAssert) {
601   if (!noAssert)
602     checkOffset(offset, 2, this.length);
603   return (this[offset] << 8) | this[offset + 1];
604 };
605
606
607 Buffer.prototype.readUInt32LE = function(offset, noAssert) {
608   if (!noAssert)
609     checkOffset(offset, 4, this.length);
610
611   return ((this[offset]) |
612       (this[offset + 1] << 8) |
613       (this[offset + 2] << 16)) +
614       (this[offset + 3] * 0x1000000);
615 };
616
617
618 Buffer.prototype.readUInt32BE = function(offset, noAssert) {
619   if (!noAssert)
620     checkOffset(offset, 4, this.length);
621
622   return (this[offset] * 0x1000000) +
623       ((this[offset + 1] << 16) |
624       (this[offset + 2] << 8) |
625       this[offset + 3]);
626 };
627
628
629 Buffer.prototype.readInt8 = function(offset, noAssert) {
630   if (!noAssert)
631     checkOffset(offset, 1, this.length);
632   if (!(this[offset] & 0x80))
633     return (this[offset]);
634   return ((0xff - this[offset] + 1) * -1);
635 };
636
637
638 Buffer.prototype.readInt16LE = function(offset, noAssert) {
639   if (!noAssert)
640     checkOffset(offset, 2, this.length);
641   var val = this[offset] | (this[offset + 1] << 8);
642   return (val & 0x8000) ? val | 0xFFFF0000 : val;
643 };
644
645
646 Buffer.prototype.readInt16BE = function(offset, noAssert) {
647   if (!noAssert)
648     checkOffset(offset, 2, this.length);
649   var val = this[offset + 1] | (this[offset] << 8);
650   return (val & 0x8000) ? val | 0xFFFF0000 : val;
651 };
652
653
654 Buffer.prototype.readInt32LE = function(offset, noAssert) {
655   if (!noAssert)
656     checkOffset(offset, 4, this.length);
657
658   return (this[offset]) |
659       (this[offset + 1] << 8) |
660       (this[offset + 2] << 16) |
661       (this[offset + 3] << 24);
662 };
663
664
665 Buffer.prototype.readInt32BE = function(offset, noAssert) {
666   if (!noAssert)
667     checkOffset(offset, 4, this.length);
668
669   return (this[offset] << 24) |
670       (this[offset + 1] << 16) |
671       (this[offset + 2] << 8) |
672       (this[offset + 3]);
673 };
674
675 Buffer.prototype.readFloatLE = function(offset, noAssert) {
676   if (!noAssert)
677     checkOffset(offset, 4, this.length);
678   return this.parent.readFloatLE(this.offset + offset, !!noAssert);
679 };
680
681
682 Buffer.prototype.readFloatBE = function(offset, noAssert) {
683   if (!noAssert)
684     checkOffset(offset, 4, this.length);
685   return this.parent.readFloatBE(this.offset + offset, !!noAssert);
686 };
687
688
689 Buffer.prototype.readDoubleLE = function(offset, noAssert) {
690   if (!noAssert)
691     checkOffset(offset, 8, this.length);
692   return this.parent.readDoubleLE(this.offset + offset, !!noAssert);
693 };
694
695
696 Buffer.prototype.readDoubleBE = function(offset, noAssert) {
697   if (!noAssert)
698     checkOffset(offset, 8, this.length);
699   return this.parent.readDoubleBE(this.offset + offset, !!noAssert);
700 };
701
702
703 function checkInt(buffer, value, offset, ext, max, min) {
704   if ((value % 1) !== 0 || value > max || value < min)
705     throw TypeError('value is out of bounds');
706   if ((offset % 1) !== 0 || offset < 0)
707     throw TypeError('offset is not uint');
708   if (offset + ext > buffer.length || buffer.length + offset < 0)
709     throw RangeError('Trying to write outside buffer length');
710 }
711
712
713 Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
714   if (!noAssert)
715     checkInt(this, value, offset, 1, 0xff, 0);
716   this[offset] = value;
717 };
718
719
720 Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
721   if (!noAssert)
722     checkInt(this, value, offset, 2, 0xffff, 0);
723   this[offset] = value;
724   this[offset + 1] = (value >>> 8);
725 };
726
727
728 Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
729   if (!noAssert)
730     checkInt(this, value, offset, 2, 0xffff, 0);
731   this[offset] = (value >>> 8);
732   this[offset + 1] = value;
733 };
734
735
736 Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
737   if (!noAssert)
738     checkInt(this, value, offset, 4, 0xffffffff, 0);
739   this[offset + 3] = (value >>> 24);
740   this[offset + 2] = (value >>> 16);
741   this[offset + 1] = (value >>> 8);
742   this[offset] = value;
743 };
744
745
746 Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
747   if (!noAssert)
748     checkInt(this, value, offset, 4, 0xffffffff, 0);
749   this[offset] = (value >>> 24);
750   this[offset + 1] = (value >>> 16);
751   this[offset + 2] = (value >>> 8);
752   this[offset + 3] = value;
753 };
754
755
756 Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
757   if (!noAssert)
758     checkInt(this, value, offset, 1, 0x7f, -0x80);
759   if (value < 0) value = 0xff + value + 1;
760   this[offset] = value;
761 };
762
763
764 Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
765   if (!noAssert)
766     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
767   this[offset] = value;
768   this[offset + 1] = (value >>> 8);
769 };
770
771
772 Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
773   if (!noAssert)
774     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
775   this[offset] = (value >>> 8);
776   this[offset + 1] = value;
777 };
778
779
780 Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
781   if (!noAssert)
782     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
783   this[offset] = value;
784   this[offset + 1] = (value >>> 8);
785   this[offset + 2] = (value >>> 16);
786   this[offset + 3] = (value >>> 24);
787 };
788
789
790 Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
791   if (!noAssert)
792     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
793   this[offset] = (value >>> 24);
794   this[offset + 1] = (value >>> 16);
795   this[offset + 2] = (value >>> 8);
796   this[offset + 3] = value;
797 };
798
799
800 Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
801   if (!noAssert)
802     checkOffset(offset, 4, this.length);
803   this.parent.writeFloatLE(value, this.offset + offset, !!noAssert);
804 };
805
806
807 Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
808   if (!noAssert)
809     checkOffset(offset, 4, this.length);
810   this.parent.writeFloatBE(value, this.offset + offset, !!noAssert);
811 };
812
813
814 Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
815   if (!noAssert)
816     checkOffset(offset, 8, this.length);
817   this.parent.writeDoubleLE(value, this.offset + offset, !!noAssert);
818 };
819
820
821 Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
822   if (!noAssert)
823     checkOffset(offset, 8, this.length);
824   this.parent.writeDoubleBE(value, this.offset + offset, !!noAssert);
825 };