@@ -219,4 +219,134 @@ describe("Specific bugs testing", function() {
219219 } ) ;
220220 } ) ;
221221 } ) ;
222+
223+ describe ( "Issue #19 Little endianess incorrect" , function ( ) {
224+ let binaryLiteral = function ( s ) {
225+ var i ;
226+ var bytes = [ ] ;
227+
228+ s = s . replace ( / \s / g, "" ) ;
229+ for ( i = 0 ; i < s . length ; i += 8 ) {
230+ bytes . push ( parseInt ( s . slice ( i , i + 8 ) , 2 ) ) ;
231+ }
232+
233+ return Buffer . from ( bytes ) ;
234+ } ;
235+ it ( "should parse 4-byte-length bit field sequence wit little endian" , function ( ) {
236+ let buf = binaryLiteral ( "0000000000001111 1010000110100010" ) ; // 000F A1A2
237+
238+ // Parsed as two uint16 with little-endian (BYTES order)
239+ let parser1 = new Parser ( ) . uint16le ( "a" ) . uint16le ( "b" ) ;
240+
241+ // Parsed as two 16 bits fields with little-endian
242+ let parser2 = new Parser ( )
243+ . endianess ( "little" )
244+ . bit16 ( "a" )
245+ . bit16 ( "b" ) ;
246+
247+ let parsed1 = parser1 . parse ( buf ) ;
248+ let parsed2 = parser2 . parse ( buf ) ;
249+
250+ assert . deepEqual ( parsed1 , {
251+ a : 0x0f00 , // 000F
252+ b : 0xa2a1 // A1A2
253+ } ) ;
254+
255+ assert . deepEqual ( parsed2 , {
256+ a : 0xa1a2 , // last 16 bits (but value coded as BE)
257+ b : 0x000f // first 16 bits (but value coded as BE)
258+ } ) ;
259+
260+ /* This is a little confusing. The endianess with bits fields affect the order of fields */
261+ } ) ;
262+ it ( "should encode bit ranges with little endian correctly" , function ( ) {
263+ let bigParser = Parser . start ( )
264+ . endianess ( "big" )
265+ . bit4 ( "a" )
266+ . bit1 ( "b" )
267+ . bit1 ( "c" )
268+ . bit1 ( "d" )
269+ . bit1 ( "e" )
270+ . uint16 ( "f" )
271+ . array ( "g" , { type : "uint8" , readUntil : "eof" } ) ;
272+ let littleParser = Parser . start ( )
273+ . endianess ( "little" )
274+ . bit4 ( "a" )
275+ . bit1 ( "b" )
276+ . bit1 ( "c" )
277+ . bit1 ( "d" )
278+ . bit1 ( "e" )
279+ . uint16 ( "f" )
280+ . array ( "g" , { type : "uint8" , readUntil : "eof" } ) ;
281+ // Parser definition for a symetric encoding/decoding of little-endian bit fields
282+ let little2Parser = Parser . start ( )
283+ . endianess ( "little" )
284+ . encoderSetOptions ( { bitEndianess : true } )
285+ . bit4 ( "a" )
286+ . bit1 ( "b" )
287+ . bit1 ( "c" )
288+ . bit1 ( "d" )
289+ . bit1 ( "e" )
290+ . uint16 ( "f" )
291+ . array ( "g" , { type : "uint8" , readUntil : "eof" } ) ;
292+
293+ let data = binaryLiteral (
294+ "0011 0 1 0 1 0000000011111111 00000001 00000010 00000011"
295+ ) ; // 35 00FF 01 02 03
296+ // in big endian: 3 0 1 0 1 00FF 1 2 3
297+ // in little endian: 3 0 1 0 1 FF00 1 2 3
298+ // LE with encoderBitEndianess option:
299+ // 5 1 1 0 0 FF00 1 2 3
300+
301+ //let bigDecoded = bigParser.parse(data);
302+ //let littleDecoded = littleParser.parse(data);
303+ let little2Decoded = little2Parser . parse ( data ) ;
304+
305+ //console.log(bigDecoded);
306+ //console.log(littleDecoded);
307+ //console.log(little2Decoded);
308+
309+ let big = {
310+ a : 3 ,
311+ b : 0 ,
312+ c : 1 ,
313+ d : 0 ,
314+ e : 1 ,
315+ f : 0x00ff ,
316+ g : [ 1 , 2 , 3 ]
317+ } ;
318+ let little = {
319+ a : 3 ,
320+ b : 0 ,
321+ c : 1 ,
322+ d : 0 ,
323+ e : 1 ,
324+ f : 0xff00 ,
325+ g : [ 1 , 2 , 3 ]
326+ } ;
327+ let little2 = {
328+ a : 5 ,
329+ b : 1 ,
330+ c : 1 ,
331+ d : 0 ,
332+ e : 0 ,
333+ f : 0xff00 ,
334+ g : [ 1 , 2 , 3 ]
335+ } ;
336+
337+ assert . deepEqual ( little2Decoded , little2 ) ;
338+
339+ let bigEncoded = bigParser . encode ( big ) ;
340+ let littleEncoded = littleParser . encode ( little ) ;
341+ let little2Encoded = little2Parser . encode ( little2 ) ;
342+
343+ //console.log(bigEncoded);
344+ //console.log(littleEncoded);
345+ //console.log(little2Encoded);
346+
347+ assert . deepEqual ( bigEncoded , data ) ;
348+ assert . deepEqual ( littleEncoded , data ) ;
349+ assert . deepEqual ( little2Encoded , data ) ;
350+ } ) ;
351+ } ) ;
222352} ) ;
0 commit comments