Skip to content

Commit

Permalink
#44 refactoring and fixed read of struct in JBBPParser
Browse files Browse the repository at this point in the history
  • Loading branch information
raydac committed Nov 3, 2024
1 parent 2bc5394 commit f3b8c44
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
3 changes: 2 additions & 1 deletion jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream,
boolean endStructureNotMet = true;

while (endStructureNotMet && positionAtCompiledBlock.get() < compiled.length) {
if (inStream.isArrayLimitDetected() ||
if (inStream.isDetectedArrayLimit() ||
(!inStream.hasAvailableData() && (flags & FLAG_SKIP_REMAINING_FIELDS_IF_EOF) != 0)) {
// Break reading because the ignore flag for EOF has been set or reached limit for whole stream array read
break;
Expand Down Expand Up @@ -628,6 +628,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream,

if (JBBPArraySizeLimiter.isBreakReadWholeStream(list.size(),
arraySizeLimiter)) {
inStream.setDetectedArrayLimit(true);
break;
}

Expand Down
61 changes: 37 additions & 24 deletions jbbp/src/main/java/com/igormaznitsa/jbbp/io/JBBPBitInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public class JBBPBitInputStream extends FilterInputStream implements JBBPCountab
private long markedByteCounter;

/**
* Internal flag shows that read stopped for reach of array limiter.
* Internal flag shows that read stopped for whole stream array read limit reach.
*
* @since 2.1.0
*/
private boolean arrayLimitDetected;
private boolean detectedArrayLimit;

/**
* Flag shows that read of array was stopped for array limiter restrictions.
Expand All @@ -82,8 +84,19 @@ public class JBBPBitInputStream extends FilterInputStream implements JBBPCountab
* @see JBBPArraySizeLimiter
* @since 2.1.0
*/
public boolean isArrayLimitDetected() {
return this.arrayLimitDetected;
public boolean isDetectedArrayLimit() {
return this.detectedArrayLimit;
}

/**
* Set value for array limit detected flag. It is important to set the flag for correct processing of parsing.
*
* @param value true or false.
* @see com.igormaznitsa.jbbp.JBBPParser
* @since 2.1.0
*/
public void setDetectedArrayLimit(final boolean value) {
this.detectedArrayLimit = value;
}

/**
Expand Down Expand Up @@ -135,7 +148,7 @@ public boolean[] readBoolArray(final int items) throws IOException {
public boolean[] readBoolArray(final int items,
final JBBPArraySizeLimiter arraySizeLimiter)
throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
byte[] buffer;
if (items < 0) {
Expand All @@ -149,7 +162,7 @@ public boolean[] readBoolArray(final int items,
pos += read;

if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
final int limit = arraySizeLimiter.getArrayItemsLimit();
if (limit < 0) {
pos = Math.min(pos, Math.abs(limit));
Expand Down Expand Up @@ -346,7 +359,7 @@ private byte[] internalReadArray(
final JBBPArraySizeLimiter streamLimiter
) throws IOException {
final boolean readByteArray = bitNumber == null;
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
byte[] buffer = new byte[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -363,7 +376,7 @@ private byte[] internalReadArray(
}
buffer[pos++] = (byte) next;
if (isBreakReadWholeStream(pos, streamLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -429,7 +442,7 @@ public short[] readShortArray(final int items, final JBBPByteOrder byteOrder) th
public short[] readShortArray(final int items, final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter)
throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
short[] buffer = new short[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -443,7 +456,7 @@ public short[] readShortArray(final int items, final JBBPByteOrder byteOrder,
}
buffer[pos++] = (short) next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -500,7 +513,7 @@ public long[] readUIntArray(
final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter
) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
long[] buffer = new long[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -514,7 +527,7 @@ public long[] readUIntArray(
}
buffer[pos++] = next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -569,7 +582,7 @@ public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder) th
public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter)
throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
char[] buffer = new char[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -583,7 +596,7 @@ public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder,
}
buffer[pos++] = (char) next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -635,7 +648,7 @@ public int[] readIntArray(final int items, final JBBPByteOrder byteOrder) throws
*/
public int[] readIntArray(final int items, final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
int[] buffer = new int[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -649,7 +662,7 @@ public int[] readIntArray(final int items, final JBBPByteOrder byteOrder,
}
buffer[pos++] = next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -706,7 +719,7 @@ public float[] readFloatArray(
final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter
) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
float[] buffer = new float[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -720,7 +733,7 @@ public float[] readFloatArray(
}
buffer[pos++] = next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -1072,7 +1085,7 @@ public long[] readLongArray(
final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter
) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
long[] buffer = new long[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -1086,7 +1099,7 @@ public long[] readLongArray(
}
buffer[pos++] = next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -1205,7 +1218,7 @@ public double[] readDoubleArray(
final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter
) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
double[] buffer = new double[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -1219,7 +1232,7 @@ public double[] readDoubleArray(
}
buffer[pos++] = Double.longBitsToDouble(next);
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down Expand Up @@ -1416,7 +1429,7 @@ public String[] readStringArray(
final JBBPByteOrder byteOrder,
final JBBPArraySizeLimiter arraySizeLimiter
) throws IOException {
this.arrayLimitDetected = false;
this.setDetectedArrayLimit(false);
int pos = 0;
if (items < 0) {
String[] buffer = new String[INITIAL_ARRAY_BUFFER_SIZE];
Expand All @@ -1430,7 +1443,7 @@ public String[] readStringArray(
}
buffer[pos++] = next;
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
this.arrayLimitDetected = true;
this.setDetectedArrayLimit(true);
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions jbbp/src/test/java/com/igormaznitsa/jbbp/JBBPParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,10 @@ public void testLimitedWholeStreamArrayRead() throws Exception {
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "long [_] test;", null, null);
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "floatj [_] test;", null, null);
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "doublej [_] test;", null, null);
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "doublej [_] test;", null, null);
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128),
"test [_] { byte a; byte b; byte c; byte d;}", null, null);

testArrayLimiter_2elementsLimit(
TestUtils.makeStringArray(JBBPByteOrder.BIG_ENDIAN, "hello", "world", "one", "two", "three",
"four"), "stringj [_] test;", null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,13 +993,13 @@ private void testWholeStreamArrayRead(
) throws Exception {
final Pair<JBBPBitInputStream, Integer> readWholeData = readWhole.getData();
assertEquals(expectedReadWholeSize, readWholeData.getRight());
assertFalse(readWholeData.getLeft().isArrayLimitDetected());
assertFalse(readWholeData.getLeft().isDetectedArrayLimit());

assertThrows(JBBPReachedArraySizeLimitException.class, readWholeWithException::getData);

final Pair<JBBPBitInputStream, Integer> readWholeLimitedData = readWholeLimited.getData();
assertEquals(expectedReadLimitedSize, readWholeLimitedData.getRight());
assertTrue(readWholeLimitedData.getLeft().isArrayLimitDetected());
assertTrue(readWholeLimitedData.getLeft().isDetectedArrayLimit());
}

@Test
Expand Down

0 comments on commit f3b8c44

Please sign in to comment.