variable length sequence, first bit indicating end of sequence, preon
-
12-11-2019 - |
Pregunta
How would you parse a variable length sequence of bytes where first bit (BigEndian) indicates if another byte is following using Preon?
Example
byte[] bytecode = new byte[] {
(byte) 0xf2, (byte) 0xbf, (byte) 0xbf, (byte) 0xbf, (byte) 0x50
};
Notes
- first bit that indicates the next is discarded in final payload
- version of Preon used for this post was 1.1
Result bytes (in decimal)
{ 114, 63, 63, 63, 80 }
Already tried
@BoundList + @Choices(with conditions)
Limbo exp lang doesn't support method calls, so you can't detect end of stream (previous needs to have sign 1 and current block needs to be the last, i.e. sign needs to be 0)
Recursive approach with @If
public static class Entry {
@BoundNumber(size = "1", byteOrder = ByteOrder.BigEndian)
private byte hasNext;
@BoundNumber(size = "7", byteOrder = ByteOrder.BigEndian)
private byte payload;
@If("hasNext > 0")
@BoundNumber(size = "1", byteOrder = ByteOrder.BigEndian)
private byte hasNext1;
@If("hasNext > 0")
@BoundNumber(size = "7", byteOrder = ByteOrder.BigEndian)
private byte payload1;
@If("hasNext1 > 0")
@BoundObject
private Entry nextEntry;
@Override
public String toString() {
return hasNext > 0 ? String.valueOf(payload) : String.valueOf(payload)
+ ", " + String.valueOf(payload1);
}
//...
}
For some reason, for example mentioned above, Preon will only parse 2 instances of Entry (parent and child) even when there should be 3.
Thanks.
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow