2017-07-28 17:53:08 +00:00
var _ _toBuffer = function ( bufs /*:Array<Array<RawBytes> >*/ ) /*:RawBytes*/ { var x = [ ] ; for ( var i = 0 ; i < bufs [ 0 ] . length ; ++ i ) { x . push . apply ( x , bufs [ 0 ] [ i ] ) ; } return x ; } ;
var _ _ _toBuffer = _ _toBuffer ;
2017-09-14 21:14:22 +00:00
var _ _utf16le = function ( b /*:RawBytes|CFBlob*/ , s /*:number*/ , e /*:number*/ ) /*:string*/ { var ss /*:Array<string>*/ = [ ] ; for ( var i = s ; i < e ; i += 2 ) ss . push ( String . fromCharCode ( _ _readUInt16LE ( b , i ) ) ) ; return ss . join ( "" ) . replace ( chr0 , '' ) ; } ;
2017-07-28 17:53:08 +00:00
var _ _ _utf16le = _ _utf16le ;
var _ _hexlify = function ( b /*:RawBytes|CFBlob*/ , s /*:number*/ , l /*:number*/ ) /*:string*/ { var ss /*:Array<string>*/ = [ ] ; for ( var i = s ; i < s + l ; ++ i ) ss . push ( ( "0" + b [ i ] . toString ( 16 ) ) . slice ( - 2 ) ) ; return ss . join ( "" ) ; } ;
var _ _ _hexlify = _ _hexlify ;
var _ _bconcat = function ( bufs /*:Array<RawBytes>*/ ) /*:RawBytes*/ {
if ( Array . isArray ( bufs [ 0 ] ) /*:: && bufs[0] instanceof Array*/ ) return /*::(*/ [ ] . concat . apply ( [ ] , bufs ) /*:: :any)*/ ;
var maxlen = 0 , i = 0 ;
for ( i = 0 ; i < bufs . length ; ++ i ) maxlen += bufs [ i ] . length ;
var o = new Uint8Array ( maxlen ) ;
for ( i = 0 , maxlen = 0 ; i < bufs . length ; maxlen += bufs [ i ] . length , ++ i ) o . set ( bufs [ i ] , maxlen ) ;
return o ;
} ;
var bconcat = _ _bconcat ;
2014-06-24 04:00:39 +00:00
2013-11-26 15:56:58 +00:00
2017-09-14 21:14:22 +00:00
if ( has _buf /*:: && typeof Buffer !== 'undefined'*/ ) {
2017-07-28 17:53:08 +00:00
_ _utf16le = function ( b /*:RawBytes|CFBlob*/ , s /*:number*/ , e /*:number*/ ) /*:string*/ {
if ( ! Buffer . isBuffer ( b ) /*:: || !(b instanceof Buffer)*/ ) return _ _ _utf16le ( b , s , e ) ;
2017-09-14 21:14:22 +00:00
return b . toString ( 'utf16le' , s , e ) . replace ( chr0 , '' ) /*.replace(chr1,'!')*/ ;
2014-05-20 17:43:03 +00:00
} ;
2017-07-28 17:53:08 +00:00
_ _hexlify = function ( b /*:RawBytes|CFBlob*/ , s /*:number*/ , l /*:number*/ ) /*:string*/ { return Buffer . isBuffer ( b ) /*:: && b instanceof Buffer*/ ? b . toString ( 'hex' , s , s + l ) : _ _ _hexlify ( b , s , l ) ; } ;
_ _toBuffer = function ( bufs /*:Array<Array<RawBytes>>*/ ) /*:RawBytes*/ { return ( bufs [ 0 ] . length > 0 && Buffer . isBuffer ( bufs [ 0 ] [ 0 ] ) ) ? Buffer . concat ( ( bufs [ 0 ] /*:any*/ ) ) : _ _ _toBuffer ( bufs ) ; } ;
2018-07-08 08:08:15 +00:00
s2a = function ( s /*:string*/ ) /*:RawBytes*/ { return Buffer _from ( s , "binary" ) ; } ;
2017-07-28 17:53:08 +00:00
bconcat = function ( bufs /*:Array<RawBytes>*/ ) /*:RawBytes*/ { return Buffer . isBuffer ( bufs [ 0 ] ) ? Buffer . concat ( /*::(*/ bufs /*:: :any)*/ ) : _ _bconcat ( bufs ) ; } ;
2013-11-26 15:56:58 +00:00
}
2017-07-28 17:53:08 +00:00
var _ _readUInt8 = function ( b /*:RawBytes|CFBlob*/ , idx /*:number*/ ) /*:number*/ { return b [ idx ] ; } ;
var _ _readUInt16LE = function ( b /*:RawBytes|CFBlob*/ , idx /*:number*/ ) /*:number*/ { return b [ idx + 1 ] * ( 1 << 8 ) + b [ idx ] ; } ;
var _ _readInt16LE = function ( b /*:RawBytes|CFBlob*/ , idx /*:number*/ ) /*:number*/ { var u = b [ idx + 1 ] * ( 1 << 8 ) + b [ idx ] ; return ( u < 0x8000 ) ? u : ( 0xffff - u + 1 ) * - 1 ; } ;
var _ _readUInt32LE = function ( b /*:RawBytes|CFBlob*/ , idx /*:number*/ ) /*:number*/ { return b [ idx + 3 ] * ( 1 << 24 ) + ( b [ idx + 2 ] << 16 ) + ( b [ idx + 1 ] << 8 ) + b [ idx ] ; } ;
var _ _readInt32LE = function ( b /*:RawBytes|CFBlob*/ , idx /*:number*/ ) /*:number*/ { return ( b [ idx + 3 ] << 24 ) + ( b [ idx + 2 ] << 16 ) + ( b [ idx + 1 ] << 8 ) + b [ idx ] ; } ;
2013-11-26 15:56:58 +00:00
2017-07-28 17:53:08 +00:00
function ReadShift ( size /*:number*/ , t /*:?string*/ ) /*:number|string*/ {
var oI /*:: :number = 0*/ , oS /*:: :string = ""*/ , type = 0 ;
2013-11-26 15:56:58 +00:00
switch ( size ) {
2014-06-24 04:00:39 +00:00
case 1 : oI = _ _readUInt8 ( this , this . l ) ; break ;
case 2 : oI = ( t !== 'i' ? _ _readUInt16LE : _ _readInt16LE ) ( this , this . l ) ; break ;
case 4 : oI = _ _readInt32LE ( this , this . l ) ; break ;
case 16 : type = 2 ; oS = _ _hexlify ( this , this . l , size ) ;
2013-11-26 15:56:58 +00:00
}
2017-09-14 21:14:22 +00:00
this . l += size ; if ( type === 0 ) return oI ; return oS ;
}
var _ _writeUInt32LE = function ( b /*:RawBytes|CFBlob*/ , val /*:number*/ , idx /*:number*/ ) /*:void*/ { b [ idx ] = ( val & 0xFF ) ; b [ idx + 1 ] = ( ( val >>> 8 ) & 0xFF ) ; b [ idx + 2 ] = ( ( val >>> 16 ) & 0xFF ) ; b [ idx + 3 ] = ( ( val >>> 24 ) & 0xFF ) ; } ;
var _ _writeInt32LE = function ( b /*:RawBytes|CFBlob*/ , val /*:number*/ , idx /*:number*/ ) /*:void*/ { b [ idx ] = ( val & 0xFF ) ; b [ idx + 1 ] = ( ( val >> 8 ) & 0xFF ) ; b [ idx + 2 ] = ( ( val >> 16 ) & 0xFF ) ; b [ idx + 3 ] = ( ( val >> 24 ) & 0xFF ) ; } ;
function WriteShift ( t /*:number*/ , val /*:string|number*/ , f /*:?string*/ ) /*:any*/ {
var size = 0 , i = 0 ;
switch ( f ) {
case "hex" : for ( ; i < t ; ++ i ) {
/*:: if(typeof val !== "string") throw new Error("unreachable"); */
this [ this . l ++ ] = parseInt ( val . slice ( 2 * i , 2 * i + 2 ) , 16 ) || 0 ;
} return this ;
case "utf16le" :
/*:: if(typeof val !== "string") throw new Error("unreachable"); */
var end /*:number*/ = this . l + t ;
for ( i = 0 ; i < Math . min ( val . length , t ) ; ++ i ) {
var cc = val . charCodeAt ( i ) ;
this [ this . l ++ ] = cc & 0xff ;
this [ this . l ++ ] = cc >> 8 ;
}
while ( this . l < end ) this [ this . l ++ ] = 0 ;
return this ;
}
/*:: if(typeof val !== "number") throw new Error("unreachable"); */
switch ( t ) {
case 1 : size = 1 ; this [ this . l ] = val & 0xFF ; break ;
case 2 : size = 2 ; this [ this . l ] = val & 0xFF ; val >>>= 8 ; this [ this . l + 1 ] = val & 0xFF ; break ;
case 4 : size = 4 ; _ _writeUInt32LE ( this , val , this . l ) ; break ;
case - 4 : size = 4 ; _ _writeInt32LE ( this , val , this . l ) ; break ;
}
this . l += size ; return this ;
2013-11-26 15:56:58 +00:00
}
2017-07-28 17:53:08 +00:00
function CheckField ( hexstr /*:string*/ , fld /*:string*/ ) /*:void*/ {
2014-06-24 04:00:39 +00:00
var m = _ _hexlify ( this , this . l , hexstr . length >> 1 ) ;
2017-03-30 21:34:37 +00:00
if ( m !== hexstr ) throw new Error ( fld + 'Expected ' + hexstr + ' saw ' + m ) ;
2014-06-24 04:00:39 +00:00
this . l += hexstr . length >> 1 ;
2013-11-26 15:56:58 +00:00
}
2017-07-28 17:53:08 +00:00
function prep _blob ( blob /*:CFBlob*/ , pos /*:number*/ ) /*:void*/ {
2014-06-24 04:00:39 +00:00
blob . l = pos ;
2017-07-28 17:53:08 +00:00
blob . read _shift = /*::(*/ ReadShift /*:: :any)*/ ;
2013-11-26 15:56:58 +00:00
blob . chk = CheckField ;
2017-09-14 21:14:22 +00:00
blob . write _shift = WriteShift ;
}
function new _buf ( sz /*:number*/ ) /*:any*/ {
var o /*:CFBlob*/ = ( new _raw _buf ( sz ) /*:any*/ ) ;
prep _blob ( o , 0 ) ;
return o ;
2013-11-26 15:56:58 +00:00
}