helpers.js 1.8 KB

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