|
40 | 40 | import java.util.List; |
41 | 41 | import java.util.Locale; |
42 | 42 | import java.util.concurrent.atomic.AtomicLong; |
| 43 | +import java.util.function.BiFunction; |
43 | 44 |
|
44 | 45 | /** |
45 | 46 | * Misc auxiliary methods to be used in the framework. |
@@ -1132,14 +1133,32 @@ public static void traceData(final InputStream inStream, |
1132 | 1133 |
|
1133 | 1134 | /** |
1134 | 1135 | * Allows to calculate maximum static array size provided by script. It doesn't calculate any expressions, so that <b>byte [1000*1000] a;</b> will not be detected. |
| 1136 | + * <b>Default size of non-static struct arrays will be recognized as 1.</b> |
1135 | 1137 | * |
1136 | 1138 | * @param script script to be processed, must not be null |
1137 | 1139 | * @param customFieldTypeProcessor custom field type processor if needed, can be null if no custom types in use |
1138 | 1140 | * @return calculated biggest static array size with embedded structure awareness |
1139 | 1141 | * @since 3.0.0 |
1140 | 1142 | */ |
1141 | 1143 | public static long findMaxStaticArraySize(final String script, |
1142 | | - final JBBPCustomFieldTypeProcessor customFieldTypeProcessor) { |
| 1144 | + final JBBPCustomFieldTypeProcessor customFieldTypeProcessor) { |
| 1145 | + return findMaxStaticArraySize(script, customFieldTypeProcessor, (fieldName, wholeStream) -> 1); |
| 1146 | + } |
| 1147 | + |
| 1148 | + /** |
| 1149 | + * Allows to calculate maximum static array size provided by script. It doesn't calculate any expressions, so that <b>byte [1000*1000] a;</b> will not be detected. |
| 1150 | + * |
| 1151 | + * @param script script to be processed, must not be null |
| 1152 | + * @param customFieldTypeProcessor custom field type processor if needed, can be null if no custom types in use |
| 1153 | + * @param expectedStructArraySizeSupplier supplier of default size for structures which size is noe static but calculable, |
| 1154 | + * it is a function which gets name info for named structure fields or null for anonymous fields |
| 1155 | + * and the flag that a whole stream should be read, as result it should return integer value of approximate expected size. |
| 1156 | + * @return calculated biggest static array size with embedded structure awareness |
| 1157 | + * @since 3.0.1 |
| 1158 | + */ |
| 1159 | + public static long findMaxStaticArraySize(final String script, |
| 1160 | + final JBBPCustomFieldTypeProcessor customFieldTypeProcessor, |
| 1161 | + final BiFunction<JBBPNamedFieldInfo, Boolean, Integer> expectedStructArraySizeSupplier) { |
1143 | 1162 |
|
1144 | 1163 | final AtomicLong maxFound = new AtomicLong(); |
1145 | 1164 | final JBBPCompiledBlock compiledBlock = |
@@ -1227,18 +1246,20 @@ public void visitVarField(int offsetInCompiledBlock, JBBPNamedFieldInfo nullable |
1227 | 1246 | } |
1228 | 1247 |
|
1229 | 1248 | @Override |
1230 | | - public void visitStructureStart(int offsetInCompiledBlock, |
1231 | | - JBBPByteOrder byteOrder, |
1232 | | - boolean readWholeStream, |
1233 | | - JBBPNamedFieldInfo nullableNameFieldInfo, |
1234 | | - JBBPIntegerValueEvaluator nullableArraySize) { |
| 1249 | + public void visitStructureStart(finalint offsetInCompiledBlock, |
| 1250 | + finalJBBPByteOrder byteOrder, |
| 1251 | + finalboolean readWholeStream, |
| 1252 | + finalJBBPNamedFieldInfo nullableNameFieldInfo, |
| 1253 | + finalJBBPIntegerValueEvaluator nullableArraySize) { |
1235 | 1254 | if (readWholeStream) { |
1236 | | - structSizeStack.add(1); |
| 1255 | + structSizeStack.add( |
| 1256 | + expectedStructArraySizeSupplier.apply(nullableNameFieldInfo, readWholeStream)); |
1237 | 1257 | } else { |
1238 | 1258 | final Integer staticSize = |
1239 | 1259 | extractStaticArraySize(offsetInCompiledBlock, nullableArraySize); |
1240 | 1260 | if (staticSize == null) { |
1241 | | - structSizeStack.add(1); |
| 1261 | + structSizeStack.add( |
| 1262 | + expectedStructArraySizeSupplier.apply(nullableNameFieldInfo, readWholeStream)); |
1242 | 1263 | } else { |
1243 | 1264 | processSize(staticSize); |
1244 | 1265 | structSizeStack.add(staticSize); |
|
0 commit comments