@naeemo/capnp 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -909,6 +909,7 @@ var Segment = class Segment {
909
909
  */
910
910
  getWord(wordOffset) {
911
911
  const byteOffset = wordOffset * WORD_SIZE;
912
+ if (byteOffset + 8 > this._size) throw new Error(`Offset ${wordOffset} is outside the bounds of the segment (${this.wordCount} words)`);
912
913
  const low = BigInt(this.view.getUint32(byteOffset, true));
913
914
  return BigInt(this.view.getUint32(byteOffset + 4, true)) << BigInt(32) | low;
914
915
  }
@@ -917,6 +918,7 @@ var Segment = class Segment {
917
918
  */
918
919
  setWord(wordOffset, value) {
919
920
  const byteOffset = wordOffset * WORD_SIZE;
921
+ if (byteOffset + 8 > this.buffer.byteLength) throw new Error(`Offset ${wordOffset} is outside the bounds of the segment`);
920
922
  this.view.setUint32(byteOffset, Number(value & BigInt(4294967295)), true);
921
923
  this.view.setUint32(byteOffset + 4, Number(value >> BigInt(32)), true);
922
924
  }
@@ -1162,73 +1164,113 @@ var StructReader = class StructReader {
1162
1164
  getBool(bitOffset) {
1163
1165
  const byteOffset = Math.floor(bitOffset / 8);
1164
1166
  const bitInByte = bitOffset % 8;
1165
- return (this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset) & 1 << bitInByte) !== 0;
1167
+ const segment = this.message.getSegment(this.segmentIndex);
1168
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1169
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1170
+ if (dataOffset < 0 || dataOffset >= dataSectionEnd) return false;
1171
+ return (segment.dataView.getUint8(dataOffset) & 1 << bitInByte) !== 0;
1166
1172
  }
1167
1173
  /**
1168
1174
  * 获取 int8 字段
1169
1175
  */
1170
1176
  getInt8(byteOffset) {
1171
- return this.message.getSegment(this.segmentIndex).dataView.getInt8(this.wordOffset * WORD_SIZE + byteOffset);
1177
+ const segment = this.message.getSegment(this.segmentIndex);
1178
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1179
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1180
+ if (dataOffset < 0 || dataOffset >= dataSectionEnd) return 0;
1181
+ return segment.dataView.getInt8(dataOffset);
1172
1182
  }
1173
1183
  /**
1174
1184
  * 获取 int16 字段
1175
1185
  */
1176
1186
  getInt16(byteOffset) {
1177
- return this.message.getSegment(this.segmentIndex).dataView.getInt16(this.wordOffset * WORD_SIZE + byteOffset, true);
1187
+ const segment = this.message.getSegment(this.segmentIndex);
1188
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1189
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1190
+ if (dataOffset < 0 || dataOffset + 2 > dataSectionEnd) return 0;
1191
+ return segment.dataView.getInt16(dataOffset, true);
1178
1192
  }
1179
1193
  /**
1180
1194
  * 获取 int32 字段
1181
1195
  */
1182
1196
  getInt32(byteOffset) {
1183
- return this.message.getSegment(this.segmentIndex).dataView.getInt32(this.wordOffset * WORD_SIZE + byteOffset, true);
1197
+ const segment = this.message.getSegment(this.segmentIndex);
1198
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1199
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1200
+ if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
1201
+ return segment.dataView.getInt32(dataOffset, true);
1184
1202
  }
1185
1203
  /**
1186
1204
  * 获取 int64 字段
1187
1205
  */
1188
1206
  getInt64(byteOffset) {
1189
1207
  const segment = this.message.getSegment(this.segmentIndex);
1190
- const offset = this.wordOffset * WORD_SIZE + byteOffset;
1191
- const low = BigInt(segment.dataView.getUint32(offset, true));
1192
- return BigInt(segment.dataView.getInt32(offset + 4, true)) << BigInt(32) | low;
1208
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1209
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1210
+ if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return BigInt(0);
1211
+ const low = BigInt(segment.dataView.getUint32(dataOffset, true));
1212
+ return BigInt(segment.dataView.getInt32(dataOffset + 4, true)) << BigInt(32) | low;
1193
1213
  }
1194
1214
  /**
1195
1215
  * 获取 uint8 字段
1196
1216
  */
1197
1217
  getUint8(byteOffset) {
1198
- return this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset);
1218
+ const segment = this.message.getSegment(this.segmentIndex);
1219
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1220
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1221
+ if (dataOffset < 0 || dataOffset >= dataSectionEnd) return 0;
1222
+ return segment.dataView.getUint8(dataOffset);
1199
1223
  }
1200
1224
  /**
1201
1225
  * 获取 uint16 字段
1202
1226
  */
1203
1227
  getUint16(byteOffset) {
1204
- return this.message.getSegment(this.segmentIndex).dataView.getUint16(this.wordOffset * WORD_SIZE + byteOffset, true);
1228
+ const segment = this.message.getSegment(this.segmentIndex);
1229
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1230
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1231
+ if (dataOffset < 0 || dataOffset + 2 > dataSectionEnd) return 0;
1232
+ return segment.dataView.getUint16(dataOffset, true);
1205
1233
  }
1206
1234
  /**
1207
1235
  * 获取 uint32 字段
1208
1236
  */
1209
1237
  getUint32(byteOffset) {
1210
- return this.message.getSegment(this.segmentIndex).dataView.getUint32(this.wordOffset * WORD_SIZE + byteOffset, true);
1238
+ const segment = this.message.getSegment(this.segmentIndex);
1239
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1240
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1241
+ if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
1242
+ return segment.dataView.getUint32(dataOffset, true);
1211
1243
  }
1212
1244
  /**
1213
1245
  * 获取 uint64 字段
1214
1246
  */
1215
1247
  getUint64(byteOffset) {
1216
1248
  const segment = this.message.getSegment(this.segmentIndex);
1217
- const offset = this.wordOffset * WORD_SIZE + byteOffset;
1218
- const low = BigInt(segment.dataView.getUint32(offset, true));
1219
- return BigInt(segment.dataView.getUint32(offset + 4, true)) << BigInt(32) | low;
1249
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1250
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1251
+ if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return BigInt(0);
1252
+ const low = BigInt(segment.dataView.getUint32(dataOffset, true));
1253
+ return BigInt(segment.dataView.getUint32(dataOffset + 4, true)) << BigInt(32) | low;
1220
1254
  }
1221
1255
  /**
1222
1256
  * 获取 float32 字段
1223
1257
  */
1224
1258
  getFloat32(byteOffset) {
1225
- return this.message.getSegment(this.segmentIndex).dataView.getFloat32(this.wordOffset * WORD_SIZE + byteOffset, true);
1259
+ const segment = this.message.getSegment(this.segmentIndex);
1260
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1261
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1262
+ if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
1263
+ return segment.dataView.getFloat32(dataOffset, true);
1226
1264
  }
1227
1265
  /**
1228
1266
  * 获取 float64 字段
1229
1267
  */
1230
1268
  getFloat64(byteOffset) {
1231
- return this.message.getSegment(this.segmentIndex).dataView.getFloat64(this.wordOffset * WORD_SIZE + byteOffset, true);
1269
+ const segment = this.message.getSegment(this.segmentIndex);
1270
+ const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
1271
+ const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
1272
+ if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return 0;
1273
+ return segment.dataView.getFloat64(dataOffset, true);
1232
1274
  }
1233
1275
  /**
1234
1276
  * 获取文本字段
@@ -1273,13 +1315,18 @@ var StructReader = class StructReader {
1273
1315
  let actualStructSize = structSize;
1274
1316
  const segment = this.message.getSegment(segmentIndex);
1275
1317
  if (listPtr.elementSize === ElementSize.COMPOSITE) {
1276
- const tagWord = segment.getWord(targetOffset);
1277
- elementCount = Number(tagWord & BigInt(4294967295));
1278
- actualStructSize = {
1279
- dataWords: Number(tagWord >> BigInt(32) & BigInt(65535)),
1280
- pointerCount: Number(tagWord >> BigInt(48) & BigInt(65535))
1281
- };
1282
- targetOffset += 1;
1318
+ if (targetOffset < 0 || targetOffset >= segment.wordCount) return;
1319
+ try {
1320
+ const tagWord = segment.getWord(targetOffset);
1321
+ elementCount = Number(tagWord & BigInt(4294967295));
1322
+ actualStructSize = {
1323
+ dataWords: Number(tagWord >> BigInt(32) & BigInt(65535)),
1324
+ pointerCount: Number(tagWord >> BigInt(48) & BigInt(65535))
1325
+ };
1326
+ targetOffset += 1;
1327
+ } catch {
1328
+ return;
1329
+ }
1283
1330
  }
1284
1331
  return new ListReader(this.message, segmentIndex, listPtr.elementSize, elementCount, actualStructSize, targetOffset);
1285
1332
  }