helpers.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseByteMap = void 0;
  4. function parseByteMap(stream, map, elements, start = null, length = null) {
  5. if (start === null) {
  6. start = 0;
  7. }
  8. if (start > (stream.length - 1)) {
  9. return [];
  10. }
  11. if (length === null) {
  12. length = stream.length - start;
  13. }
  14. if (length > (stream.length - start)) {
  15. length = stream.length - start;
  16. }
  17. let dataView;
  18. if ((start == 0) && (length == stream.length)) {
  19. dataView = stream.view;
  20. }
  21. else {
  22. dataView = new Uint8Array(stream.buffer, start, length);
  23. }
  24. const resultArray = new Array(elements);
  25. let elementsCount = 0;
  26. let count = 0;
  27. const mapLength = map.length;
  28. while (count < length) {
  29. let structureLength = 0;
  30. resultArray[elementsCount] = {};
  31. for (let i = 0; i < mapLength; i++) {
  32. if (map[i].maxlength == 0) {
  33. if ("defaultValue" in map[i]) {
  34. (resultArray[elementsCount])[map[i].name] = map[i].defaultValue;
  35. }
  36. continue;
  37. }
  38. const array = new Uint8Array(map[i].maxlength);
  39. for (let j = 0; j < map[i].maxlength; j++) {
  40. array[j] = dataView[count++];
  41. }
  42. const result = (map[i].func)(array);
  43. if (result.status == (-1)) {
  44. if (resultArray.length == 1) {
  45. return [];
  46. }
  47. return resultArray.slice(0, resultArray.length - 1);
  48. }
  49. if (map[i].type != "check") {
  50. (resultArray[elementsCount])[map[i].name] = result.value;
  51. }
  52. count -= (map[i].maxlength - result.length);
  53. structureLength += result.length;
  54. }
  55. (resultArray[elementsCount++]).structureLength = structureLength;
  56. }
  57. return resultArray;
  58. }
  59. exports.parseByteMap = parseByteMap;