Skip to content

Commit 9eb4cdb

Browse files
committed
improved reference, added a test
1 parent f8489f9 commit 9eb4cdb

File tree

5 files changed

+283
-38
lines changed

5 files changed

+283
-38
lines changed

.projectKnowledge/JBBP_Types.mmd

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
Mind Map generated by NB MindMap plugin
2+
> __version__=`1.0`,showJumps=`true`
3+
---
4+
5+
# Fields
6+
7+
## Primitive<br/>types
8+
9+
### allowed types
10+
> fillColor=`#FF9999`
11+
12+
13+
#### bit\[:1\.\.7\]
14+
> fillColor=`#9999FF`
15+
16+
17+
##### by default <br/>is 1 bit
18+
> fillColor=`#FF66FF`
19+
20+
21+
#### bool
22+
> fillColor=`#9999FF`
23+
24+
25+
##### 8 bit field
26+
> fillColor=`#99FFFF`
27+
28+
29+
###### FALSE \(==0\)
30+
> fillColor=`#CCFFCC`
31+
32+
33+
###### TRUE \(\!=0\)
34+
> fillColor=`#CCFFCC`
35+
36+
37+
#### byte
38+
> fillColor=`#9999FF`
39+
40+
41+
##### 8 bit signed field
42+
> fillColor=`#99FFFF`
43+
44+
45+
#### ubyte
46+
> fillColor=`#9999FF`
47+
48+
49+
##### 8 bit unsigned field
50+
> fillColor=`#99FFFF`
51+
52+
53+
#### short
54+
> fillColor=`#9999FF`
55+
56+
57+
##### 16 bit signed field
58+
> fillColor=`#99FFFF`
59+
60+
61+
#### ushort
62+
> fillColor=`#9999FF`
63+
64+
65+
##### 16 bit unsigned field
66+
> fillColor=`#99FFFF`
67+
68+
69+
#### int
70+
> fillColor=`#9999FF`
71+
72+
73+
##### 32 bit signed field
74+
> fillColor=`#99FFFF`
75+
76+
77+
#### long
78+
> fillColor=`#9999FF`
79+
80+
81+
##### 64 bit signed field
82+
> fillColor=`#99FFFF`
83+
84+
85+
### format
86+
> fillColor=`#FF9999`
87+
88+
89+
#### \<field\_type\> \[\<field\_name\>\];
90+
> fillColor=`#FFCC00`
91+
92+
93+
##### int;
94+
> fillColor=`#99FF99`
95+
96+
97+
##### int data;
98+
> fillColor=`#99FF99`
99+
100+
101+
## Complex types
102+
103+
### Arrays
104+
> fillColor=`#9999FF`
105+
106+
107+
#### \<field\_type\> '\['\<array\_length\>|\<expression\>|\_'\]' \[\<array\_name\>\];
108+
> fillColor=`#FFCC00`,topicLinkUID=`152971A8B02A`
109+
110+
111+
##### \<array\_length\> is hardcoded <br/>number of items
112+
> fillColor=`#6666FF`,textColor=`#CCFFCC`
113+
114+
115+
###### bit:3 \[1244\];
116+
> fillColor=`#99FF99`
117+
118+
119+
##### \<expression\> is some expression<br/>provides length of the array
120+
> fillColor=`#6666FF`,textColor=`#CCFFCC`
121+
122+
123+
###### int length; byte \[length\*2\] data;
124+
> fillColor=`#99FF99`
125+
126+
127+
##### '\_' means read whole stream <br/>untill the stream end
128+
> fillColor=`#6666FF`,textColor=`#CCFFCC`
129+
130+
131+
###### It can be placed only<br/>for the end item in script
132+
> fillColor=`#FF3300`,textColor=`#FFFFFF`
133+
134+
135+
###### long header; byte \[\_\] allData;
136+
> fillColor=`#99FF99`
137+
138+
139+
### Structures
140+
> fillColor=`#9999FF`
141+
142+
143+
#### \[\<structure\_name\>\]\['\['array\_length|expression|\_'\]'\]\{ \<fields\>\.\.\. \}
144+
> fillColor=`#FFCC00`
145+
146+
- TOPIC
147+
<pre>152971A8B02A</pre>
148+
149+
##### \{bit;\}
150+
> fillColor=`#99FF99`
151+
152+
153+
##### header \{ubyte sections; ubyte datalen;\}<br/>sections \[header\.sections\]\{byte \[header\.datalen\] data;\}
154+
> fillColor=`#99FF99`
155+
156+
157+
## Special
158+
> leftSide=`true`
159+
160+
161+
### Align stream
162+
> fillColor=`#9999FF`
163+
164+
165+
#### align\[:number\_of\_bytes|\(expression\_in\_parentheses\)\]
166+
> fillColor=`#FFCC00`
167+
168+
169+
##### align; byte \[123\];
170+
> fillColor=`#99FF99`,leftSide=`true`
171+
172+
173+
##### align:4; byte\[123\];
174+
> fillColor=`#99FF99`,leftSide=`true`
175+
176+
177+
##### align:\($$\+45\); int data;
178+
> fillColor=`#99FF99`,leftSide=`true`
179+
180+
181+
#### by default<br/>align to 1 byte
182+
> fillColor=`#FF66FF`
183+
184+
185+
#### Skips bytes to align the byte<br/>counter to needed value
186+
> fillColor=`#FFFFCC`
187+
188+
- TOPIC
189+
<pre>1529753E90BA</pre>
190+
191+
### Byte counter
192+
> fillColor=`#FF9999`,topicLinkUID=`1529753E90BA`
193+
194+
195+
#### $$ in expressions
196+
> fillColor=`#FF9999`
197+
198+
199+
### Skip bytes
200+
> fillColor=`#9999FF`
201+
202+
203+
#### Skips needed number of bytes
204+
> fillColor=`#FFFFCC`
205+
206+
- TOPIC
207+
<pre>1529753E90BA</pre>
208+
209+
#### by default<br/>skip 1 byte
210+
> fillColor=`#FF66FF`
211+
212+
213+
#### skip\[:number\_of\_bytes|\(expression\_in\_parentheses\)\]
214+
> fillColor=`#FFCC00`
215+
216+
217+
##### skip; int;
218+
> fillColor=`#99FF99`
219+
220+
221+
##### skip:34; byte;
222+
> fillColor=`#99FF99`,leftSide=`true`
223+
224+
225+
##### skip:\($$\*2\); int data;
226+
> fillColor=`#99FF99`,leftSide=`true`
227+
228+
229+
## Byte order
230+
> leftSide=`true`
231+
232+
233+
### \[\<|\>\]\<filed\_type\>\.\.\.
234+
> fillColor=`#FFCC33`
235+
236+
237+
#### \>
238+
> fillColor=`#FF99FF`
239+
240+
241+
##### Big\-Endinan
242+
243+
###### \>int \[26\] data;
244+
> fillColor=`#99FF99`
245+
246+
247+
##### It is the default value and<br/>can be omitted
248+
> fillColor=`#FF6666`
249+
250+
251+
#### \<
252+
> fillColor=`#FF99FF`
253+
254+
255+
##### Little\-Endian
256+
> leftSide=`true`
257+
258+
259+
###### \<long data;
260+
> fillColor=`#99FF99`
261+

README.md

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@ Change log
4040

4141
Compatibility
4242
=============
43-
The Framework supports Java platform since Java SE 1.5+, its inside mapping mechanism developed in such manner to be compatible and with Android platform (as of Android 2.0) so that the framework is also Android compatible one (2.0+). It doesn't use any NIO and usage of the non-standard sun.misc.unsafe class is isolated through reflection mechanism. The Library doesn't have any external dependencies.
43+
The Framework is compatible with:
44+
* Java 1.5+
45+
* Android 2.0+
46+
For mapping sometime it uses reflection.
4447

45-
How to use with Maven
48+
Maven dependency
4649
======================
47-
The Framework is published in the Maven Central thus it can be added as a dependency into a project
50+
The Framework is published in the Maven Central and can be easily added as a dependency into a maven project
4851
```
4952
<dependency>
5053
<groupId>com.igormaznitsa</groupId>
5154
<artifactId>jbbp</artifactId>
5255
<version>1.2.0</version>
5356
</dependency>
5457
```
55-
also the precompiled jar, javadoc and sources can be downloaded manually from [the Maven central.](http://search.maven.org/#browse|808871750)
58+
the precompiled library jar, javadoc and sources also can be downloaded directly from [the Maven central.](http://search.maven.org/#browse|808871750)
5659

5760
Hello world
5861
============
@@ -104,40 +107,9 @@ The Example will print in console the text below
104107
; END : Flags
105108
;--------------------------------------------------------------------------------
106109
```
107-
Format of a field
110+
Fields
108111
==================
109-
Each field can be just a field or an array of fields, also it can be anonymous one or named one.
110-
111-
[<|>]field_type [[array_size|expression|_]] [field_name] ;
112-
113-
The first char shows the byte order to be used for parsing of the field (if the field is a multi-byte one) and can be omitted (and in the case the default order for the parser will be used). The Parser allows to parse a field as Big-Endinan one (for '>' prefix, it is the default state so that it can be omitted) and Little-Endian one (for '<' prefix). [You can read about endianness in wikipedia.](http://en.wikipedia.org/wiki/Endianness)
114-
115-
**The Field name will be normalized to its lower-case representation, so that keep in your mind that names are case insensitive.**
116-
117-
Supported data types and commands
118-
==================================
119-
- **bit[:(number_of_bits|expression_in_parentheses)]** - a bit field of fixed size (1..7 bits), by default 1
120-
- **byte** - a signed byte field (8 bits)
121-
- **ubyte** - a unsigned byte field (8 bits)
122-
- **bool** - a boolean field (1 byte)
123-
- **short** - a signed short field (2 bytes)
124-
- **ushort**- a unsigned short field (2 bytes)
125-
- **int** - an integer field (4 bytes)
126-
- **long** - a long field (8 bytes)
127-
- **align[:(number_of_bytes|expression_in_parentheses)]** - align the counter for number of bytes, by default 1. NB: It works relative to the current read byte counter!
128-
- **skip[:(number_of_bytes|expression_in_parentheses)]** - skip number of bytes, by default 1
129-
- **var[:(numeric_value|expression_in_parentheses)]** - a var field which should be read through an external processor defined by the user
130-
- **reset$$** - reset the input stream read byte counter, it is very useful for relative alignment operations
131-
132-
__expression_in_parentheses - means (expression)__
133-
134-
Structures
135-
===========
136-
Fields can be collected in structures, the format of structure definition:
137-
```
138-
[structure_name] [[array_size|expression|_]] { fields... }
139-
```
140-
Structures can contain another structures and every field inside a structure has path in the format ***structure_name.field_name*** but if you want use field names inside the same structure then use their names without name of structure.
112+
![JBBP field format, types and examples](https://github.com/raydac/java-binary-block-parser/blob/master/docs/jbbp_fields.png)
141113

142114
Expressions
143115
============

docs/jbbp_fields.png

127 KB
Loading

src/test/java/com/igormaznitsa/jbbp/JBBPParserTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.EOFException;
2828
import java.io.IOException;
2929
import static org.junit.Assert.*;
30-
import org.junit.Ignore;
3130
import org.junit.Test;
3231

3332
public class JBBPParserTest {

src/test/java/com/igormaznitsa/jbbp/model/JBBPFieldStructTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,17 @@ public void testMapTo_Object() throws Exception {
210210
assertEquals(3, mapped.c);
211211
}
212212

213+
@Test
214+
public void testInterStructFieldReferences() throws Exception {
215+
final JBBPParser parser = JBBPParser.prepare("header {ubyte sections; ubyte datalen;} sections [header.sections]{byte[header.datalen] data;}");
216+
217+
final JBBPFieldArrayStruct sections = parser.parse(new byte[]{3, 2, 1, 2, 3, 4, 5, 6}).findFieldForNameAndType("sections", JBBPFieldArrayStruct.class);
218+
assertEquals(3, sections.size());
219+
for(int i=0;i<3;i++){
220+
JBBPFieldArrayByte data = sections.getElementAt(i).findFieldForNameAndType("data", JBBPFieldArrayByte.class);
221+
final int base = i*2;
222+
assertArrayEquals(new byte []{(byte)(base+1), (byte)(base+2)}, data.getArray());
223+
}
224+
}
225+
213226
}

0 commit comments

Comments
 (0)