@@ -434,113 +434,45 @@ Buffer.prototype.readUInt8 = function(offset, noAssert) {
434434} ;
435435
436436
437- function readUInt16 ( buffer , offset , isBigEndian ) {
438- var val = 0 ;
439- if ( isBigEndian ) {
440- val = buffer [ offset ] << 8 ;
441- val |= buffer [ offset + 1 ] ;
442- } else {
443- val = buffer [ offset ] ;
444- val |= buffer [ offset + 1 ] << 8 ;
445- }
446- return val ;
447- }
448-
449-
450437Buffer . prototype . readUInt16LE = function ( offset , noAssert ) {
451438 offset = offset >>> 0 ;
452439 if ( ! noAssert )
453440 checkOffset ( offset , 2 , this . length ) ;
454- return readUInt16 ( this , offset , false , noAssert ) ;
441+ return this [ offset ] | ( this [ offset + 1 ] << 8 ) ;
455442} ;
456443
457444
458445Buffer . prototype . readUInt16BE = function ( offset , noAssert ) {
459446 offset = offset >>> 0 ;
460447 if ( ! noAssert )
461448 checkOffset ( offset , 2 , this . length ) ;
462- return readUInt16 ( this , offset , true , noAssert ) ;
449+ return ( this [ offset ] << 8 ) | this [ offset + 1 ] ;
463450} ;
464451
465452
466- function readUInt32 ( buffer , offset , isBigEndian ) {
467- var val = 0 ;
468- if ( isBigEndian ) {
469- val = buffer [ offset + 1 ] << 16 ;
470- val |= buffer [ offset + 2 ] << 8 ;
471- val |= buffer [ offset + 3 ] ;
472- val = val + ( buffer [ offset ] << 24 >>> 0 ) ;
473- } else {
474- val = buffer [ offset + 2 ] << 16 ;
475- val |= buffer [ offset + 1 ] << 8 ;
476- val |= buffer [ offset ] ;
477- val = val + ( buffer [ offset + 3 ] << 24 >>> 0 ) ;
478- }
479- return val ;
480- }
481-
482-
483453Buffer . prototype . readUInt32LE = function ( offset , noAssert ) {
484454 offset = offset >>> 0 ;
485455 if ( ! noAssert )
486456 checkOffset ( offset , 4 , this . length ) ;
487- return readUInt32 ( this , offset , false ) ;
457+
458+ return ( ( this [ offset ] ) |
459+ ( this [ offset + 1 ] << 8 ) |
460+ ( this [ offset + 2 ] << 16 ) ) +
461+ ( this [ offset + 3 ] * 0x1000000 ) ;
488462} ;
489463
490464
491465Buffer . prototype . readUInt32BE = function ( offset , noAssert ) {
492466 offset = offset >>> 0 ;
493467 if ( ! noAssert )
494468 checkOffset ( offset , 4 , this . length ) ;
495- return readUInt32 ( this , offset , true ) ;
496- } ;
497469
470+ return ( this [ offset ] * 0x1000000 ) +
471+ ( ( this [ offset + 1 ] << 16 ) |
472+ ( this [ offset + 2 ] << 8 ) ) |
473+ ( this [ offset + 3 ] ) ;
474+ } ;
498475
499- /*
500- * Signed integer types, yay team! A reminder on how two's complement actually
501- * works. The first bit is the signed bit, i.e. tells us whether or not the
502- * number should be positive or negative. If the two's complement value is
503- * positive, then we're done, as it's equivalent to the unsigned representation.
504- *
505- * Now if the number is positive, you're pretty much done, you can just leverage
506- * the unsigned translations and return those. Unfortunately, negative numbers
507- * aren't quite that straightforward.
508- *
509- * At first glance, one might be inclined to use the traditional formula to
510- * translate binary numbers between the positive and negative values in two's
511- * complement. (Though it doesn't quite work for the most negative value)
512- * Mainly:
513- * - invert all the bits
514- * - add one to the result
515- *
516- * Of course, this doesn't quite work in Javascript. Take for example the value
517- * of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
518- * course, Javascript will do the following:
519- *
520- * > ~0xff80
521- * -65409
522- *
523- * Whoh there, Javascript, that's not quite right. But wait, according to
524- * Javascript that's perfectly correct. When Javascript ends up seeing the
525- * constant 0xff80, it has no notion that it is actually a signed number. It
526- * assumes that we've input the unsigned value 0xff80. Thus, when it does the
527- * binary negation, it casts it into a signed value, (positive 0xff80). Then
528- * when you perform binary negation on that, it turns it into a negative number.
529- *
530- * Instead, we're going to have to use the following general formula, that works
531- * in a rather Javascript friendly way. I'm glad we don't support this kind of
532- * weird numbering scheme in the kernel.
533- *
534- * (BIT-MAX - (unsigned)val + 1) * -1
535- *
536- * The astute observer, may think that this doesn't make sense for 8-bit numbers
537- * (really it isn't necessary for them). However, when you get 16-bit numbers,
538- * you do. Let's go back to our prior example and see how this will look:
539- *
540- * (0xffff - 0xff80 + 1) * -1
541- * (0x007f + 1) * -1
542- * (0x0080) * -1
543- */
544476
545477Buffer . prototype . readInt8 = function ( offset , noAssert ) {
546478 offset = offset >>> 0 ;
@@ -551,47 +483,45 @@ Buffer.prototype.readInt8 = function(offset, noAssert) {
551483} ;
552484
553485
554- function readInt16 ( buffer , offset , isBigEndian ) {
555- var val = readUInt16 ( buffer , offset , isBigEndian ) ;
556- return ! ( val & 0x8000 ) ? val : ( 0xffff - val + 1 ) * - 1 ;
557- }
558-
559-
560486Buffer . prototype . readInt16LE = function ( offset , noAssert ) {
561487 offset = offset >>> 0 ;
562488 if ( ! noAssert )
563489 checkOffset ( offset , 2 , this . length ) ;
564- return readInt16 ( this , offset , false ) ;
490+ var val = this [ offset ] | ( this [ offset + 1 ] << 8 ) ;
491+ return ( val & 0x8000 ) ? val | 0xFFFF0000 : val ;
565492} ;
566493
567494
568495Buffer . prototype . readInt16BE = function ( offset , noAssert ) {
569496 offset = offset >>> 0 ;
570497 if ( ! noAssert )
571498 checkOffset ( offset , 2 , this . length ) ;
572- return readInt16 ( this , offset , true ) ;
499+ var val = this [ offset + 1 ] | ( this [ offset ] << 8 ) ;
500+ return ( val & 0x8000 ) ? val | 0xFFFF0000 : val ;
573501} ;
574502
575503
576- function readInt32 ( buffer , offset , isBigEndian ) {
577- var val = readUInt32 ( buffer , offset , isBigEndian ) ;
578- return ! ( val & 0x80000000 ) ? val : ( 0xffffffff - val + 1 ) * - 1 ;
579- }
580-
581-
582504Buffer . prototype . readInt32LE = function ( offset , noAssert ) {
583505 offset = offset >>> 0 ;
584506 if ( ! noAssert )
585507 checkOffset ( offset , 4 , this . length ) ;
586- return readInt32 ( this , offset , false ) ;
508+
509+ return ( this [ offset ] ) |
510+ ( this [ offset + 1 ] << 8 ) |
511+ ( this [ offset + 2 ] << 16 ) |
512+ ( this [ offset + 3 ] << 24 ) ;
587513} ;
588514
589515
590516Buffer . prototype . readInt32BE = function ( offset , noAssert ) {
591517 offset = offset >>> 0 ;
592518 if ( ! noAssert )
593519 checkOffset ( offset , 4 , this . length ) ;
594- return readInt32 ( this , offset , true ) ;
520+
521+ return ( this [ offset ] << 24 ) |
522+ ( this [ offset + 1 ] << 16 ) |
523+ ( this [ offset + 2 ] << 8 ) |
524+ ( this [ offset + 3 ] ) ;
595525} ;
596526
597527
@@ -615,24 +545,14 @@ Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
615545} ;
616546
617547
618- function writeUInt16 ( buffer , value , offset , isBigEndian ) {
619- if ( isBigEndian ) {
620- buffer [ offset ] = ( value & 0xff00 ) >>> 8 ;
621- buffer [ offset + 1 ] = value & 0x00ff ;
622- } else {
623- buffer [ offset + 1 ] = ( value & 0xff00 ) >>> 8 ;
624- buffer [ offset ] = value & 0x00ff ;
625- }
626- return offset + 2 ;
627- }
628-
629-
630548Buffer . prototype . writeUInt16LE = function ( value , offset , noAssert ) {
631549 value = + value ;
632550 offset = offset >>> 0 ;
633551 if ( ! noAssert )
634552 checkInt ( this , value , offset , 2 , 0xffff , 0 ) ;
635- return writeUInt16 ( this , value , offset , false ) ;
553+ this [ offset ] = value ;
554+ this [ offset + 1 ] = ( value >>> 8 ) ;
555+ return offset + 2 ;
636556} ;
637557
638558
@@ -641,32 +561,22 @@ Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
641561 offset = offset >>> 0 ;
642562 if ( ! noAssert )
643563 checkInt ( this , value , offset , 2 , 0xffff , 0 ) ;
644- return writeUInt16 ( this , value , offset , true ) ;
564+ this [ offset ] = ( value >>> 8 ) ;
565+ this [ offset + 1 ] = value ;
566+ return offset + 2 ;
645567} ;
646568
647569
648- function writeUInt32 ( buffer , value , offset , isBigEndian ) {
649- if ( isBigEndian ) {
650- buffer [ offset ] = ( value >>> 24 ) & 0xff ;
651- buffer [ offset + 1 ] = ( value >>> 16 ) & 0xff ;
652- buffer [ offset + 2 ] = ( value >>> 8 ) & 0xff ;
653- buffer [ offset + 3 ] = value & 0xff ;
654- } else {
655- buffer [ offset + 3 ] = ( value >>> 24 ) & 0xff ;
656- buffer [ offset + 2 ] = ( value >>> 16 ) & 0xff ;
657- buffer [ offset + 1 ] = ( value >>> 8 ) & 0xff ;
658- buffer [ offset ] = value & 0xff ;
659- }
660- return offset + 4 ;
661- }
662-
663-
664570Buffer . prototype . writeUInt32LE = function ( value , offset , noAssert ) {
665571 value = + value ;
666572 offset = offset >>> 0 ;
667573 if ( ! noAssert )
668574 checkInt ( this , value , offset , 4 , 0xffffffff , 0 ) ;
669- return writeUInt32 ( this , value , offset , false ) ;
575+ this [ offset + 3 ] = ( value >>> 24 ) ;
576+ this [ offset + 2 ] = ( value >>> 16 ) ;
577+ this [ offset + 1 ] = ( value >>> 8 ) ;
578+ this [ offset ] = value ;
579+ return offset + 4 ;
670580} ;
671581
672582
@@ -675,9 +585,14 @@ Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
675585 offset = offset >>> 0 ;
676586 if ( ! noAssert )
677587 checkInt ( this , value , offset , 4 , 0xffffffff , 0 ) ;
678- return writeUInt32 ( this , value , offset , true ) ;
588+ this [ offset ] = ( value >>> 24 ) ;
589+ this [ offset + 1 ] = ( value >>> 16 ) ;
590+ this [ offset + 2 ] = ( value >>> 8 ) ;
591+ this [ offset + 3 ] = value ;
592+ return offset + 4 ;
679593} ;
680594
595+
681596Buffer . prototype . writeInt8 = function ( value , offset , noAssert ) {
682597 value = + value ;
683598 offset = offset >>> 0 ;
@@ -693,7 +608,9 @@ Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
693608 offset = offset >>> 0 ;
694609 if ( ! noAssert )
695610 checkInt ( this , value , offset , 2 , 0x7fff , - 0x8000 ) ;
696- return writeUInt16 ( this , value , offset , false ) ;
611+ this [ offset ] = value ;
612+ this [ offset + 1 ] = ( value >>> 8 ) ;
613+ return offset + 2 ;
697614} ;
698615
699616
@@ -702,7 +619,9 @@ Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
702619 offset = offset >>> 0 ;
703620 if ( ! noAssert )
704621 checkInt ( this , value , offset , 2 , 0x7fff , - 0x8000 ) ;
705- return writeUInt16 ( this , value , offset , true ) ;
622+ this [ offset ] = ( value >>> 8 ) ;
623+ this [ offset + 1 ] = value ;
624+ return offset + 2 ;
706625} ;
707626
708627
@@ -711,7 +630,11 @@ Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
711630 offset = offset >>> 0 ;
712631 if ( ! noAssert )
713632 checkInt ( this , value , offset , 4 , 0x7fffffff , - 0x80000000 ) ;
714- return writeUInt32 ( this , value , offset , false ) ;
633+ this [ offset ] = value ;
634+ this [ offset + 1 ] = ( value >>> 8 ) ;
635+ this [ offset + 2 ] = ( value >>> 16 ) ;
636+ this [ offset + 3 ] = ( value >>> 24 ) ;
637+ return offset + 4 ;
715638} ;
716639
717640
@@ -720,5 +643,9 @@ Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
720643 offset = offset >>> 0 ;
721644 if ( ! noAssert )
722645 checkInt ( this , value , offset , 4 , 0x7fffffff , - 0x80000000 ) ;
723- return writeUInt32 ( this , value , offset , true ) ;
646+ this [ offset ] = ( value >>> 24 ) ;
647+ this [ offset + 1 ] = ( value >>> 16 ) ;
648+ this [ offset + 2 ] = ( value >>> 8 ) ;
649+ this [ offset + 3 ] = value ;
650+ return offset + 4 ;
724651} ;
0 commit comments