keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
864 stars 134 forks source link

[HELP] Can someone help in understanding how to parse this type of buffer? #205

Open enricovittorini opened 2 years ago

enricovittorini commented 2 years ago

Hello Guys, can someone explain how to parse a buffer with this structure?

image

I managed to do it till here:

const patParse = new Parser()
         .uint8("table_id")
         .bit1("section_syntax_indicator")
         .bit1("zero")
         .bit2("reserved")
         .bit12("section_length")
         .uint16("transport_stream_id")
         .bit2("reserved")
         .bit5("version_number")
         .bit1("current_next_indicator")
         .uint8("section_number")
         .uint8("last_section_number")

but I do not know how to manage the "for"/"if" with unknown length.

thanks for your help!

keichi commented 2 years ago

If you know the size of the buffer, you could calculate the number of elements (N) and pass it to the length or lengthInBytes option of the array parser.