@@ -14,15 +14,14 @@ const t = require(__dirname + '/structure-types.js')
1414const util = require ( 'util' )
1515
1616const NOT_LONG_ENOUGH = 'Buffer is not long enough'
17- //Reads a 4-byte length buffer
18- function readLengthBuffer ( buffer , offset ) {
19- try {
20- return {
21- value : new DataView ( buffer ) . getUint32 ( offset ) ,
22- length : 4
23- }
17+ function readFlexInt ( buffer , offset ) {
18+ assert . assert ( buffer . byteLength > offset , NOT_LONG_ENOUGH )
19+ const length = flexInt . getByteCount ( new Uint8Array ( buffer ) [ offset ] )
20+ assert . assert ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH )
21+ return {
22+ value : flexInt . readValueBuffer ( buffer . slice ( offset , offset + length ) ) ,
23+ length
2424 }
25- catch ( e ) { assert . fail ( NOT_LONG_ENOUGH ) }
2625}
2726//Types whose type bytes are only 1 byte long (the type byte)
2827const SINGLE_BYTE_TYPES = new Set ( [
@@ -78,9 +77,7 @@ function makeBaseValue(type, count) {
7877 return { }
7978 }
8079 /*istanbul ignore next*/
81- default : {
82- throw new Error ( 'Invalid type for base value: ' + util . inspect ( type ) )
83- }
80+ default : assert . fail ( 'Invalid type for base value: ' + util . inspect ( type ) )
8481 }
8582}
8683//Counterpart for writeBooleans() in structure-types.js
@@ -145,11 +142,11 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
145142 break
146143 }
147144 case t . BigIntType : {
148- length = 2
149- assert . assert ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH )
150- const dataView = new DataView ( buffer )
151- const bytes = dataView . getUint16 ( offset )
145+ const lengthInt = readFlexInt ( buffer , offset )
146+ const bytes = lengthInt . value
147+ ; ( { length} = lengthInt )
152148 assert . assert ( buffer . byteLength >= offset + length + bytes , NOT_LONG_ENOUGH )
149+ const dataView = new DataView ( buffer )
153150 if ( bytes ) value = String ( dataView . getInt8 ( offset + length ) )
154151 else value = '0'
155152 for ( let byte = 1 ; byte < bytes ; byte ++ ) {
@@ -187,11 +184,11 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
187184 break
188185 }
189186 case t . BigUnsignedIntType : {
190- length = 2
191- assert . assert ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH )
192- const dataView = new DataView ( buffer )
193- const bytes = dataView . getUint16 ( offset )
187+ const lengthInt = readFlexInt ( buffer , offset )
188+ const bytes = lengthInt . value
189+ ; ( { length} = lengthInt )
194190 assert . assert ( buffer . byteLength >= offset + length + bytes , NOT_LONG_ENOUGH )
191+ const dataView = new DataView ( buffer )
195192 value = '0'
196193 for ( let byte = 0 ; byte < bytes ; byte ++ ) {
197194 if ( byte ) value = strint . mul ( value , strint . BYTE_SHIFT ) //after the first byte, shift everything left one byte before adding
@@ -201,12 +198,7 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
201198 break
202199 }
203200 case t . FlexUnsignedIntType : {
204- length = 1
205- assert . assert ( buffer . byteLength > offset , NOT_LONG_ENOUGH )
206- const bytes = flexInt . getByteCount ( new Uint8Array ( buffer ) [ offset ] )
207- assert . assert ( buffer . byteLength >= offset + bytes , NOT_LONG_ENOUGH )
208- value = flexInt . readValueBuffer ( buffer . slice ( offset , offset + bytes ) )
209- length += bytes
201+ ( { value, length} = readFlexInt ( buffer , offset ) )
210202 break
211203 }
212204 case t . DayType : {
@@ -241,12 +233,12 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
241233 assert . assert ( buffer . byteLength > offset , NOT_LONG_ENOUGH )
242234 const readByte = new Uint8Array ( buffer ) [ offset ]
243235 assert . assert ( readByte === 0x00 || readByte === 0xFF , '0x' + readByte . toString ( 16 ) + ' is an invalid Boolean value' )
244- value = ! ! readByte //convert integer to boolean
236+ value = Boolean ( readByte )
245237 break
246238 }
247239 case t . BooleanArrayType : {
248- const arrayLength = readLengthBuffer ( buffer , offset )
249- length = arrayLength . length
240+ const arrayLength = readFlexInt ( buffer , offset )
241+ ; ( { length} = arrayLength )
250242 const booleans = readBooleans ( { buffer, offset : offset + length , count : arrayLength . value , baseValue} )
251243 length += booleans . length
252244 ; ( { value} = booleans )
@@ -273,7 +265,7 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
273265 break
274266 }
275267 case t . OctetsType : {
276- const octetsLength = readLengthBuffer ( buffer , offset )
268+ const octetsLength = readFlexInt ( buffer , offset )
277269 ; ( { length} = octetsLength )
278270 const finalLength = length + octetsLength . value
279271 assert . assert ( buffer . byteLength >= offset + finalLength , NOT_LONG_ENOUGH )
@@ -302,19 +294,20 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
302294 break
303295 }
304296 case t . ArrayType : {
305- const arrayLength = readLengthBuffer ( buffer , offset )
306- length = arrayLength . length
307- value = baseValue || makeBaseValue ( type , arrayLength . value )
308- for ( let i = 0 ; i < value . length ; i ++ ) {
297+ const arrayLengthInt = readFlexInt ( buffer , offset )
298+ const arrayLength = arrayLengthInt . value
299+ ; ( { length} = arrayLengthInt )
300+ value = baseValue || makeBaseValue ( type , arrayLength )
301+ for ( let i = 0 ; i < arrayLength ; i ++ ) {
309302 const element = consumeValue ( { buffer, pointerStart, offset : offset + length , type : type . type } )
310303 length += element . length
311304 value [ i ] = element . value
312305 }
313306 break
314307 }
315308 case t . SetType : {
316- const size = readLengthBuffer ( buffer , offset )
317- length = size . length
309+ const size = readFlexInt ( buffer , offset )
310+ ; ( { length} = size )
318311 value = baseValue || makeBaseValue ( type )
319312 for ( let i = 0 ; i < size . value ; i ++ ) {
320313 const element = consumeValue ( { buffer, pointerStart, offset : offset + length , type : type . type } )
@@ -324,8 +317,8 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
324317 break
325318 }
326319 case t . MapType : {
327- const size = readLengthBuffer ( buffer , offset )
328- length = size . length
320+ const size = readFlexInt ( buffer , offset )
321+ ; ( { length} = size )
329322 value = baseValue || makeBaseValue ( type )
330323 for ( let i = 0 ; i < size . value ; i ++ ) {
331324 const keyElement = consumeValue ( { buffer, pointerStart, offset : offset + length , type : type . keyType } )
@@ -383,7 +376,7 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
383376 length += consumeValue ( { buffer, pointerStart, offset : offset + length , type : subType , baseValue : value } ) . length
384377 }
385378 else {
386- const indexOffset = readLengthBuffer ( buffer , offset + length )
379+ const indexOffset = readFlexInt ( buffer , offset + length )
387380 const target = offset + length - indexOffset . value
388381 value = readRecursives . get ( buffer ) . get ( target )
389382 assert . assert ( value !== undefined , 'Cannot find target at ' + String ( target ) )
@@ -405,9 +398,10 @@ function consumeValue({buffer, pointerStart, offset, type, baseValue}) {
405398 break
406399 }
407400 case t . PointerType : {
408- const location = readLengthBuffer ( buffer , offset )
409- length = location . length
410- value = consumeValue ( { buffer, pointerStart, offset : pointerStart + location . value , type : type . type } ) . value
401+ length = 4
402+ assert . assert ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH )
403+ const location = new DataView ( buffer ) . getUint32 ( offset )
404+ value = consumeValue ( { buffer, pointerStart, offset : pointerStart + location , type : type . type } ) . value
411405 break
412406 }
413407 default :
@@ -550,9 +544,9 @@ function consumeType(typeBuffer, offset) {
550544 break
551545 }
552546 case t . RecursiveType . _value : {
553- assert . assert ( typeBuffer . byteLength >= offset + length + 2 , NOT_LONG_ENOUGH )
554- const id = new DataView ( typeBuffer ) . getUint16 ( offset + length )
555- length += 2
547+ const idInt = readFlexInt ( typeBuffer , offset + length )
548+ const id = idInt . value
549+ length += idInt . length
556550 let bufferRecursiveNames = recursiveNames . get ( typeBuffer )
557551 let recursiveName
558552 if ( bufferRecursiveNames ) recursiveName = bufferRecursiveNames . get ( id ) //see whether this type was previously read
@@ -594,11 +588,9 @@ function consumeType(typeBuffer, offset) {
594588 break
595589 }
596590 case t . REPEATED_TYPE : {
597- const newLength = length + 2
598- assert . assert ( typeBuffer . byteLength >= offset + newLength , NOT_LONG_ENOUGH )
599- const locationOffset = new DataView ( typeBuffer ) . getUint16 ( offset + length )
600- ; ( { value} = consumeType ( typeBuffer , offset + length - locationOffset ) )
601- length = newLength
591+ const locationOffset = readFlexInt ( typeBuffer , offset + length )
592+ ; ( { value} = consumeType ( typeBuffer , offset + length - locationOffset . value ) )
593+ length += locationOffset . length
602594 break
603595 }
604596 default :
0 commit comments