1
1
/* The MIT License (MIT)
2
2
*
3
- * Copyright 2015-2017 Peter A. Bigot
3
+ * Copyright 2015-2018 Peter A. Bigot
4
4
*
5
5
* Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
* of this software and associated documentation files (the "Software"), to deal
@@ -1166,18 +1166,26 @@ class Sequence extends Layout {
1166
1166
* @param {string } [property] - initializer for {@link
1167
1167
* Layout#property|property}.
1168
1168
*
1169
+ * @param {Boolean } [decodePrefixes] - initializer for {@link
1170
+ * Structure#decodePrefixes|property}.
1171
+ *
1169
1172
* @throws {Error } - if `fields` contains an unnamed variable-length
1170
1173
* layout.
1171
1174
*
1172
1175
* @constructor
1173
1176
* @augments {Layout }
1174
1177
*/
1175
1178
class Structure extends Layout {
1176
- constructor ( fields , property ) {
1179
+ constructor ( fields , property , decodePrefixes ) {
1177
1180
if ( ! ( Array . isArray ( fields )
1178
1181
&& fields . reduce ( ( acc , v ) => acc && ( v instanceof Layout ) , true ) ) ) {
1179
1182
throw new TypeError ( 'fields must be array of Layout instances' ) ;
1180
1183
}
1184
+ if ( ( 'boolean' === typeof property )
1185
+ && ( undefined === decodePrefixes ) ) {
1186
+ decodePrefixes = property ;
1187
+ property = undefined ;
1188
+ }
1181
1189
1182
1190
/* Verify absence of unnamed variable-length fields. */
1183
1191
for ( const fd of fields ) {
@@ -1205,6 +1213,17 @@ class Structure extends Layout {
1205
1213
*
1206
1214
* @type {Layout[] } */
1207
1215
this . fields = fields ;
1216
+
1217
+ /** Control behavior of {@link Layout#decode|decode()} given short
1218
+ * buffers.
1219
+ *
1220
+ * In some situations a structure many be extended with additional
1221
+ * fields over time, with older installations providing only a
1222
+ * prefix of the full structure. If this property is `true`
1223
+ * decoding will accept those buffers and leave subsequent fields
1224
+ * undefined, as long as the buffer ends at a field boundary.
1225
+ * Defaults to `false`. */
1226
+ this . decodePrefixes = ! ! decodePrefixes ;
1208
1227
}
1209
1228
1210
1229
/** @override */
@@ -1239,6 +1258,10 @@ class Structure extends Layout {
1239
1258
dest [ fd . property ] = fd . decode ( b , offset ) ;
1240
1259
}
1241
1260
offset += fd . getSpan ( b , offset ) ;
1261
+ if ( this . decodePrefixes
1262
+ && ( b . length === offset ) ) {
1263
+ break ;
1264
+ }
1242
1265
}
1243
1266
return dest ;
1244
1267
}
@@ -1920,9 +1943,12 @@ function fixBitwiseResult(v) {
1920
1943
* {@link UInt|UInt} (or {@link UIntBE|UIntBE}) that is no more than 4
1921
1944
* bytes wide.
1922
1945
*
1923
- * @param {bool } msb - `true` if the bit numbering starts at the most
1924
- * significant bit of the containing word; `false` (default) if it
1925
- * starts at the least significant bit of the containing word.
1946
+ * @param {bool } [msb] - `true` if the bit numbering starts at the
1947
+ * most significant bit of the containing word; `false` (default) if
1948
+ * it starts at the least significant bit of the containing word. If
1949
+ * the parameter at this position is a string and `property` is
1950
+ * `undefined` the value of this argument will instead be used as the
1951
+ * value of `property`.
1926
1952
*
1927
1953
* @param {string } [property] - initializer for {@link
1928
1954
* Layout#property|property}.
@@ -1936,6 +1962,11 @@ class BitStructure extends Layout {
1936
1962
|| ( word instanceof UIntBE ) ) ) {
1937
1963
throw new TypeError ( 'word must be a UInt or UIntBE layout' ) ;
1938
1964
}
1965
+ if ( ( 'string' === typeof msb )
1966
+ && ( undefined === property ) ) {
1967
+ property = msb ;
1968
+ msb = undefined ;
1969
+ }
1939
1970
if ( 4 < word . span ) {
1940
1971
throw new RangeError ( 'word cannot exceed 32 bits' ) ;
1941
1972
}
@@ -2557,7 +2588,7 @@ exports.f64 = (property => new Double(property));
2557
2588
exports . f64be = ( property => new DoubleBE ( property ) ) ;
2558
2589
2559
2590
/** Factory for {@link Structure|Structure} values. */
2560
- exports . struct = ( ( fields , property ) => new Structure ( fields , property ) ) ;
2591
+ exports . struct = ( ( fields , property , decodePrefixes ) => new Structure ( fields , property , decodePrefixes ) ) ;
2561
2592
2562
2593
/** Factory for {@link BitStructure|BitStructure} values. */
2563
2594
exports . bits = ( ( word , msb , property ) => new BitStructure ( word , msb , property ) ) ;
0 commit comments