buffer: add generic functions for (u)int ops
[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 buffer = process.binding('buffer');
23 var smalloc = process.binding('smalloc');
24 var util = require('util');
25 var alloc = smalloc.alloc;
26 var truncate = smalloc.truncate;
27 var sliceOnto = smalloc.sliceOnto;
28 var kMaxLength = smalloc.kMaxLength;
29 var internal = {};
30
31 exports.Buffer = Buffer;
32 exports.SlowBuffer = SlowBuffer;
33 exports.INSPECT_MAX_BYTES = 50;
34
35
36 Buffer.poolSize = 8 * 1024;
37 var poolSize, poolOffset, allocPool;
38
39
40 function createPool() {
41   poolSize = Buffer.poolSize;
42   allocPool = alloc({}, poolSize);
43   poolOffset = 0;
44 }
45 createPool();
46
47
48 function Buffer(subject, encoding) {
49   if (!util.isBuffer(this))
50     return new Buffer(subject, encoding);
51
52   if (util.isNumber(subject))
53     this.length = subject > 0 ? subject >>> 0 : 0;
54   else if (util.isString(subject))
55     this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
56   else if (util.isObject(subject)) {
57     if (subject.type === 'Buffer' && util.isArray(subject.data))
58       subject = subject.data;
59
60     this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
61   } else
62     throw new TypeError('must start with number, buffer, array or string');
63
64   if (this.length > kMaxLength) {
65     throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
66                          'size: 0x' + kMaxLength.toString(16) + ' bytes');
67   }
68
69   this.parent = undefined;
70   if (this.length <= (Buffer.poolSize >>> 1) && this.length > 0) {
71     if (this.length > poolSize - poolOffset)
72       createPool();
73     this.parent = sliceOnto(allocPool,
74                             this,
75                             poolOffset,
76                             poolOffset + this.length);
77     poolOffset += this.length;
78   } else {
79     alloc(this, this.length);
80   }
81
82   if (!util.isNumber(subject)) {
83     if (util.isString(subject)) {
84       // In the case of base64 it's possible that the size of the buffer
85       // allocated was slightly too large. In this case we need to rewrite
86       // the length to the actual length written.
87       var len = this.write(subject, encoding);
88
89       // Buffer was truncated after decode, realloc internal ExternalArray
90       if (len !== this.length) {
91         this.length = len;
92         truncate(this, this.length);
93       }
94     } else {
95       if (util.isBuffer(subject))
96         subject.copy(this, 0, 0, this.length);
97       else if (util.isNumber(subject.length) || util.isArray(subject))
98         for (var i = 0; i < this.length; i++)
99           this[i] = subject[i];
100     }
101   }
102 }
103
104
105 function SlowBuffer(length) {
106   length = length >>> 0;
107   if (length > kMaxLength) {
108     throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
109                          'size: 0x' + kMaxLength.toString(16) + ' bytes');
110   }
111   var b = new NativeBuffer(length);
112   alloc(b, length);
113   return b;
114 }
115
116
117 // Bypass all checks for instantiating unallocated Buffer required for
118 // Objects created in C++. Significantly faster than calling the Buffer
119 // function.
120 function NativeBuffer(length) {
121   this.length = length >>> 0;
122   // Set this to keep the object map the same.
123   this.parent = undefined;
124 }
125 NativeBuffer.prototype = Buffer.prototype;
126
127
128 // add methods to Buffer prototype
129 buffer.setupBufferJS(NativeBuffer, internal);
130
131
132 // Static methods
133
134 Buffer.isBuffer = function isBuffer(b) {
135   return util.isBuffer(b);
136 };
137
138
139 Buffer.compare = function compare(a, b) {
140   if (!(a instanceof Buffer) ||
141       !(b instanceof Buffer))
142     throw new TypeError('Arguments must be Buffers');
143
144   return internal.compare(a, b);
145 };
146
147
148 Buffer.isEncoding = function(encoding) {
149   switch ((encoding + '').toLowerCase()) {
150     case 'hex':
151     case 'utf8':
152     case 'utf-8':
153     case 'ascii':
154     case 'binary':
155     case 'base64':
156     case 'ucs2':
157     case 'ucs-2':
158     case 'utf16le':
159     case 'utf-16le':
160     case 'raw':
161       return true;
162
163     default:
164       return false;
165   }
166 };
167
168
169 Buffer.concat = function(list, length) {
170   if (!util.isArray(list))
171     throw new TypeError('Usage: Buffer.concat(list[, length])');
172
173   if (util.isUndefined(length)) {
174     length = 0;
175     for (var i = 0; i < list.length; i++)
176       length += list[i].length;
177   } else {
178     length = length >>> 0;
179   }
180
181   if (list.length === 0)
182     return new Buffer(0);
183   else if (list.length === 1)
184     return list[0];
185
186   var buffer = new Buffer(length);
187   var pos = 0;
188   for (var i = 0; i < list.length; i++) {
189     var buf = list[i];
190     buf.copy(buffer, pos);
191     pos += buf.length;
192   }
193
194   return buffer;
195 };
196
197
198 Buffer.byteLength = function(str, enc) {
199   var ret;
200   str = str + '';
201   switch (enc) {
202     case 'ascii':
203     case 'binary':
204     case 'raw':
205       ret = str.length;
206       break;
207     case 'ucs2':
208     case 'ucs-2':
209     case 'utf16le':
210     case 'utf-16le':
211       ret = str.length * 2;
212       break;
213     case 'hex':
214       ret = str.length >>> 1;
215       break;
216     default:
217       ret = internal.byteLength(str, enc);
218   }
219   return ret;
220 };
221
222
223 // toString(encoding, start=0, end=buffer.length)
224 Buffer.prototype.toString = function(encoding, start, end) {
225   var loweredCase = false;
226
227   start = start >>> 0;
228   end = util.isUndefined(end) || end === Infinity ? this.length : end >>> 0;
229
230   if (!encoding) encoding = 'utf8';
231   if (start < 0) start = 0;
232   if (end > this.length) end = this.length;
233   if (end <= start) return '';
234
235   while (true) {
236     switch (encoding) {
237       case 'hex':
238         return this.hexSlice(start, end);
239
240       case 'utf8':
241       case 'utf-8':
242         return this.utf8Slice(start, end);
243
244       case 'ascii':
245         return this.asciiSlice(start, end);
246
247       case 'binary':
248         return this.binarySlice(start, end);
249
250       case 'base64':
251         return this.base64Slice(start, end);
252
253       case 'ucs2':
254       case 'ucs-2':
255       case 'utf16le':
256       case 'utf-16le':
257         return this.ucs2Slice(start, end);
258
259       default:
260         if (loweredCase)
261           throw new TypeError('Unknown encoding: ' + encoding);
262         encoding = (encoding + '').toLowerCase();
263         loweredCase = true;
264     }
265   }
266 };
267
268
269 Buffer.prototype.equals = function equals(b) {
270   if (!(b instanceof Buffer))
271     throw new TypeError('Argument must be a Buffer');
272
273   return internal.compare(this, b) === 0;
274 };
275
276
277 // Inspect
278 Buffer.prototype.inspect = function inspect() {
279   var str = '';
280   var max = exports.INSPECT_MAX_BYTES;
281   if (this.length > 0) {
282     str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
283     if (this.length > max)
284       str += ' ... ';
285   }
286   return '<' + this.constructor.name + ' ' + str + '>';
287 };
288
289
290 Buffer.prototype.compare = function compare(b) {
291   if (!(b instanceof Buffer))
292     throw new TypeError('Argument must be a Buffer');
293
294   return internal.compare(this, b);
295 };
296
297
298 Buffer.prototype.fill = function fill(val, start, end) {
299   start = start >> 0;
300   end = (end === undefined) ? this.length : end >> 0;
301
302   if (start < 0 || end > this.length)
303     throw new RangeError('out of range index');
304   if (end <= start)
305     return this;
306
307   if (typeof val !== 'string') {
308     val = val >>> 0;
309   } else if (val.length === 1) {
310     var code = val.charCodeAt(0);
311     if (code < 256)
312       val = code;
313   }
314
315   internal.fill(this, val, start, end);
316
317   return this;
318 };
319
320
321 // XXX remove in v0.13
322 Buffer.prototype.get = util.deprecate(function get(offset) {
323   offset = ~~offset;
324   if (offset < 0 || offset >= this.length)
325     throw new RangeError('index out of range');
326   return this[offset];
327 }, '.get() is deprecated. Access using array indexes instead.');
328
329
330 // XXX remove in v0.13
331 Buffer.prototype.set = util.deprecate(function set(offset, v) {
332   offset = ~~offset;
333   if (offset < 0 || offset >= this.length)
334     throw new RangeError('index out of range');
335   return this[offset] = v;
336 }, '.set() is deprecated. Set using array indexes instead.');
337
338
339 // TODO(trevnorris): fix these checks to follow new standard
340 // write(string, offset = 0, length = buffer.length, encoding = 'utf8')
341 var writeWarned = false;
342 var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
343                ' Use write(string[, offset[, length]][, encoding]) instead.';
344 Buffer.prototype.write = function(string, offset, length, encoding) {
345   // Buffer#write(string);
346   if (util.isUndefined(offset)) {
347     encoding = 'utf8';
348     length = this.length;
349     offset = 0;
350
351   // Buffer#write(string, encoding)
352   } else if (util.isUndefined(length) && util.isString(offset)) {
353     encoding = offset;
354     length = this.length;
355     offset = 0;
356
357   // Buffer#write(string, offset[, length][, encoding])
358   } else if (isFinite(offset)) {
359     offset = offset >>> 0;
360     if (isFinite(length)) {
361       length = length >>> 0;
362       if (util.isUndefined(encoding))
363         encoding = 'utf8';
364     } else {
365       encoding = length;
366       length = undefined;
367     }
368
369   // XXX legacy write(string, encoding, offset, length) - remove in v0.13
370   } else {
371     if (!writeWarned) {
372       if (process.throwDeprecation)
373         throw new Error(writeMsg);
374       else if (process.traceDeprecation)
375         console.trace(writeMsg);
376       else
377         console.error(writeMsg);
378       writeWarned = true;
379     }
380
381     var swap = encoding;
382     encoding = offset;
383     offset = length >>> 0;
384     length = swap;
385   }
386
387   var remaining = this.length - offset;
388   if (util.isUndefined(length) || length > remaining)
389     length = remaining;
390
391   encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
392
393   if (string.length > 0 && (length < 0 || offset < 0))
394     throw new RangeError('attempt to write outside buffer bounds');
395
396   var ret;
397   switch (encoding) {
398     case 'hex':
399       ret = this.hexWrite(string, offset, length);
400       break;
401
402     case 'utf8':
403     case 'utf-8':
404       ret = this.utf8Write(string, offset, length);
405       break;
406
407     case 'ascii':
408       ret = this.asciiWrite(string, offset, length);
409       break;
410
411     case 'binary':
412       ret = this.binaryWrite(string, offset, length);
413       break;
414
415     case 'base64':
416       // Warning: maxLength not taken into account in base64Write
417       ret = this.base64Write(string, offset, length);
418       break;
419
420     case 'ucs2':
421     case 'ucs-2':
422     case 'utf16le':
423     case 'utf-16le':
424       ret = this.ucs2Write(string, offset, length);
425       break;
426
427     default:
428       throw new TypeError('Unknown encoding: ' + encoding);
429   }
430
431   return ret;
432 };
433
434
435 Buffer.prototype.toJSON = function() {
436   return {
437     type: 'Buffer',
438     data: Array.prototype.slice.call(this, 0)
439   };
440 };
441
442
443 // TODO(trevnorris): currently works like Array.prototype.slice(), which
444 // doesn't follow the new standard for throwing on out of range indexes.
445 Buffer.prototype.slice = function(start, end) {
446   var len = this.length;
447   start = ~~start;
448   end = util.isUndefined(end) ? len : ~~end;
449
450   if (start < 0) {
451     start += len;
452     if (start < 0)
453       start = 0;
454   } else if (start > len) {
455     start = len;
456   }
457
458   if (end < 0) {
459     end += len;
460     if (end < 0)
461       end = 0;
462   } else if (end > len) {
463     end = len;
464   }
465
466   if (end < start)
467     end = start;
468
469   var buf = new NativeBuffer();
470   sliceOnto(this, buf, start, end);
471   buf.length = end - start;
472   if (buf.length > 0)
473     buf.parent = util.isUndefined(this.parent) ? this : this.parent;
474
475   return buf;
476 };
477
478
479 function checkOffset(offset, ext, length) {
480   if (offset + ext > length)
481     throw new RangeError('index out of range');
482 }
483
484
485 Buffer.prototype.readUIntLE = function(offset, byteLength, noAssert) {
486   offset = offset >>> 0;
487   byteLength = byteLength >>> 0;
488   if (!noAssert)
489     checkOffset(offset, byteLength, this.length);
490
491   var val = this[offset];
492   var mul = 1;
493   var i = 0;
494   while (++i < byteLength && (mul *= 0x100))
495     val += this[offset + i] * mul;
496
497   return val;
498 };
499
500
501 Buffer.prototype.readUIntBE = function(offset, byteLength, noAssert) {
502   offset = offset >>> 0;
503   byteLength = byteLength >>> 0;
504   if (!noAssert)
505     checkOffset(offset, byteLength, this.length);
506
507   var val = this[offset + --byteLength];
508   var mul = 1;
509   while (byteLength > 0 && (mul *= 0x100))
510     val += this[offset + --byteLength] * mul;
511
512   return val;
513 };
514
515
516 Buffer.prototype.readUInt8 = function(offset, noAssert) {
517   offset = offset >>> 0;
518   if (!noAssert)
519     checkOffset(offset, 1, this.length);
520   return this[offset];
521 };
522
523
524 Buffer.prototype.readUInt16LE = function(offset, noAssert) {
525   offset = offset >>> 0;
526   if (!noAssert)
527     checkOffset(offset, 2, this.length);
528   return this[offset] | (this[offset + 1] << 8);
529 };
530
531
532 Buffer.prototype.readUInt16BE = function(offset, noAssert) {
533   offset = offset >>> 0;
534   if (!noAssert)
535     checkOffset(offset, 2, this.length);
536   return (this[offset] << 8) | this[offset + 1];
537 };
538
539
540 Buffer.prototype.readUInt32LE = function(offset, noAssert) {
541   offset = offset >>> 0;
542   if (!noAssert)
543     checkOffset(offset, 4, this.length);
544
545   return ((this[offset]) |
546       (this[offset + 1] << 8) |
547       (this[offset + 2] << 16)) +
548       (this[offset + 3] * 0x1000000);
549 };
550
551
552 Buffer.prototype.readUInt32BE = function(offset, noAssert) {
553   offset = offset >>> 0;
554   if (!noAssert)
555     checkOffset(offset, 4, this.length);
556
557   return (this[offset] * 0x1000000) +
558       ((this[offset + 1] << 16) |
559       (this[offset + 2] << 8) |
560       this[offset + 3]);
561 };
562
563
564 Buffer.prototype.readIntLE = function(offset, byteLength, noAssert) {
565   offset = offset >>> 0;
566   byteLength = byteLength >>> 0;
567   if (!noAssert)
568     checkOffset(offset, byteLength, this.length);
569
570   var val = this[offset];
571   var mul = 1;
572   var i = 0;
573   while (++i < byteLength && (mul *= 0x100))
574     val += this[offset + i] * mul;
575   mul *= 0x80;
576
577   if (val >= mul)
578     val -= Math.pow(2, 8 * byteLength);
579
580   return val;
581 };
582
583
584 Buffer.prototype.readIntBE = function(offset, byteLength, noAssert) {
585   offset = offset >>> 0;
586   byteLength = byteLength >>> 0;
587   if (!noAssert)
588     checkOffset(offset, byteLength, this.length);
589
590   var i = byteLength;
591   var mul = 1;
592   var val = this[offset + --i];
593   while (i > 0 && (mul *= 0x100))
594     val += this[offset + --i] * mul;
595   mul *= 0x80;
596
597   if (val >= mul)
598     val -= Math.pow(2, 8 * byteLength);
599
600   return val;
601 };
602
603
604 Buffer.prototype.readInt8 = function(offset, noAssert) {
605   offset = offset >>> 0;
606   if (!noAssert)
607     checkOffset(offset, 1, this.length);
608   var val = this[offset];
609   return !(val & 0x80) ? val : (0xff - val + 1) * -1;
610 };
611
612
613 Buffer.prototype.readInt16LE = function(offset, noAssert) {
614   offset = offset >>> 0;
615   if (!noAssert)
616     checkOffset(offset, 2, this.length);
617   var val = this[offset] | (this[offset + 1] << 8);
618   return (val & 0x8000) ? val | 0xFFFF0000 : val;
619 };
620
621
622 Buffer.prototype.readInt16BE = function(offset, noAssert) {
623   offset = offset >>> 0;
624   if (!noAssert)
625     checkOffset(offset, 2, this.length);
626   var val = this[offset + 1] | (this[offset] << 8);
627   return (val & 0x8000) ? val | 0xFFFF0000 : val;
628 };
629
630
631 Buffer.prototype.readInt32LE = function(offset, noAssert) {
632   offset = offset >>> 0;
633   if (!noAssert)
634     checkOffset(offset, 4, this.length);
635
636   return (this[offset]) |
637       (this[offset + 1] << 8) |
638       (this[offset + 2] << 16) |
639       (this[offset + 3] << 24);
640 };
641
642
643 Buffer.prototype.readInt32BE = function(offset, noAssert) {
644   offset = offset >>> 0;
645   if (!noAssert)
646     checkOffset(offset, 4, this.length);
647
648   return (this[offset] << 24) |
649       (this[offset + 1] << 16) |
650       (this[offset + 2] << 8) |
651       (this[offset + 3]);
652 };
653
654
655 Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
656   offset = offset >>> 0;
657   if (!noAssert)
658     checkOffset(offset, 4, this.length);
659   return internal.readFloatLE(this, offset);
660 };
661
662
663 Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
664   offset = offset >>> 0;
665   if (!noAssert)
666     checkOffset(offset, 4, this.length);
667   return internal.readFloatBE(this, offset);
668 };
669
670
671 Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
672   offset = offset >>> 0;
673   if (!noAssert)
674     checkOffset(offset, 8, this.length);
675   return internal.readDoubleLE(this, offset);
676 };
677
678
679 Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
680   offset = offset >>> 0;
681   if (!noAssert)
682     checkOffset(offset, 8, this.length);
683   return internal.readDoubleBE(this, offset);
684 };
685
686
687 function checkInt(buffer, value, offset, ext, max, min) {
688   if (!(buffer instanceof Buffer))
689     throw new TypeError('buffer must be a Buffer instance');
690   if (value > max || value < min)
691     throw new TypeError('value is out of bounds');
692   if (offset + ext > buffer.length)
693     throw new RangeError('index out of range');
694 }
695
696
697 Buffer.prototype.writeUIntLE = function(value, offset, byteLength, noAssert) {
698   value = +value;
699   offset = offset >>> 0;
700   byteLength = byteLength >>> 0;
701   if (!noAssert)
702     checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0);
703
704   var mul = 1;
705   var i = 0;
706   this[offset] = value;
707   while (++i < byteLength && (mul *= 0x100))
708     this[offset + i] = (value / mul) >>> 0;
709
710   return offset + byteLength;
711 };
712
713
714 Buffer.prototype.writeUIntBE = function(value, offset, byteLength, noAssert) {
715   value = +value;
716   offset = offset >>> 0;
717   byteLength = byteLength >>> 0;
718   if (!noAssert)
719     checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0);
720
721   var i = byteLength - 1;
722   var mul = 1;
723   this[offset + i] = value;
724   while (--i >= 0 && (mul *= 0x100))
725     this[offset + i] = (value / mul) >>> 0;
726
727   return offset + byteLength;
728 };
729
730
731 Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
732   value = +value;
733   offset = offset >>> 0;
734   if (!noAssert)
735     checkInt(this, value, offset, 1, 0xff, 0);
736   this[offset] = value;
737   return offset + 1;
738 };
739
740
741 Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
742   value = +value;
743   offset = offset >>> 0;
744   if (!noAssert)
745     checkInt(this, value, offset, 2, 0xffff, 0);
746   this[offset] = value;
747   this[offset + 1] = (value >>> 8);
748   return offset + 2;
749 };
750
751
752 Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
753   value = +value;
754   offset = offset >>> 0;
755   if (!noAssert)
756     checkInt(this, value, offset, 2, 0xffff, 0);
757   this[offset] = (value >>> 8);
758   this[offset + 1] = value;
759   return offset + 2;
760 };
761
762
763 Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
764   value = +value;
765   offset = offset >>> 0;
766   if (!noAssert)
767     checkInt(this, value, offset, 4, 0xffffffff, 0);
768   this[offset + 3] = (value >>> 24);
769   this[offset + 2] = (value >>> 16);
770   this[offset + 1] = (value >>> 8);
771   this[offset] = value;
772   return offset + 4;
773 };
774
775
776 Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
777   value = +value;
778   offset = offset >>> 0;
779   if (!noAssert)
780     checkInt(this, value, offset, 4, 0xffffffff, 0);
781   this[offset] = (value >>> 24);
782   this[offset + 1] = (value >>> 16);
783   this[offset + 2] = (value >>> 8);
784   this[offset + 3] = value;
785   return offset + 4;
786 };
787
788
789 Buffer.prototype.writeIntLE = function(value, offset, byteLength, noAssert) {
790   value = +value;
791   offset = offset >>> 0;
792   if (!noAssert) {
793     checkInt(this,
794              value,
795              offset,
796              byteLength,
797              Math.pow(2, 8 * byteLength - 1) - 1,
798              -Math.pow(2, 8 * byteLength - 1));
799   }
800
801   var i = 0;
802   var mul = 1;
803   var sub = value < 0 ? 1 : 0;
804   this[offset] = value;
805   while (++i < byteLength && (mul *= 0x100))
806     this[offset + i] = ((value / mul) >> 0) - sub;
807
808   return offset + byteLength;
809 };
810
811
812 Buffer.prototype.writeIntBE = function(value, offset, byteLength, noAssert) {
813   value = +value;
814   offset = offset >>> 0;
815   if (!noAssert) {
816     checkInt(this,
817              value,
818              offset,
819              byteLength,
820              Math.pow(2, 8 * byteLength - 1) - 1,
821              -Math.pow(2, 8 * byteLength - 1));
822   }
823
824   var i = byteLength - 1;
825   var mul = 1;
826   var sub = value < 0 ? 1 : 0;
827   this[offset + i] = value;
828   while (--i >= 0 && (mul *= 0x100))
829     this[offset + i] = ((value / mul) >> 0) - sub;
830
831   return offset + byteLength;
832 };
833
834
835 Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
836   value = +value;
837   offset = offset >>> 0;
838   if (!noAssert)
839     checkInt(this, value, offset, 1, 0x7f, -0x80);
840   this[offset] = value;
841   return offset + 1;
842 };
843
844
845 Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
846   value = +value;
847   offset = offset >>> 0;
848   if (!noAssert)
849     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
850   this[offset] = value;
851   this[offset + 1] = (value >>> 8);
852   return offset + 2;
853 };
854
855
856 Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
857   value = +value;
858   offset = offset >>> 0;
859   if (!noAssert)
860     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
861   this[offset] = (value >>> 8);
862   this[offset + 1] = value;
863   return offset + 2;
864 };
865
866
867 Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
868   value = +value;
869   offset = offset >>> 0;
870   if (!noAssert)
871     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
872   this[offset] = value;
873   this[offset + 1] = (value >>> 8);
874   this[offset + 2] = (value >>> 16);
875   this[offset + 3] = (value >>> 24);
876   return offset + 4;
877 };
878
879
880 Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
881   value = +value;
882   offset = offset >>> 0;
883   if (!noAssert)
884     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
885   this[offset] = (value >>> 24);
886   this[offset + 1] = (value >>> 16);
887   this[offset + 2] = (value >>> 8);
888   this[offset + 3] = value;
889   return offset + 4;
890 };
891
892
893 function checkFloat(buffer, value, offset, ext) {
894   if (!(buffer instanceof Buffer))
895     throw new TypeError('buffer must be a Buffer instance');
896   if (offset + ext > buffer.length)
897     throw new RangeError('index out of range');
898 }
899
900
901 Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) {
902   val = +val;
903   offset = offset >>> 0;
904   if (!noAssert)
905     checkFloat(this, val, offset, 4);
906   internal.writeFloatLE(this, val, offset);
907   return offset + 4;
908 };
909
910
911 Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) {
912   val = +val;
913   offset = offset >>> 0;
914   if (!noAssert)
915     checkFloat(this, val, offset, 4);
916   internal.writeFloatBE(this, val, offset);
917   return offset + 4;
918 };
919
920
921 Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) {
922   val = +val;
923   offset = offset >>> 0;
924   if (!noAssert)
925     checkFloat(this, val, offset, 8);
926   internal.writeDoubleLE(this, val, offset);
927   return offset + 8;
928 };
929
930
931 Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
932   val = +val;
933   offset = offset >>> 0;
934   if (!noAssert)
935     checkFloat(this, val, offset, 8);
936   internal.writeDoubleBE(this, val, offset);
937   return offset + 8;
938 };