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