riff-helpers.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findFourCC = void 0;
  6. var _byteHelpers = require("./byte-helpers.js");
  7. var CONSTANTS = {
  8. LIST: (0, _byteHelpers.toUint8)([0x4c, 0x49, 0x53, 0x54]),
  9. RIFF: (0, _byteHelpers.toUint8)([0x52, 0x49, 0x46, 0x46]),
  10. WAVE: (0, _byteHelpers.toUint8)([0x57, 0x41, 0x56, 0x45])
  11. };
  12. var normalizePath = function normalizePath(path) {
  13. if (typeof path === 'string') {
  14. return (0, _byteHelpers.stringToBytes)(path);
  15. }
  16. if (typeof path === 'number') {
  17. return path;
  18. }
  19. return path;
  20. };
  21. var normalizePaths = function normalizePaths(paths) {
  22. if (!Array.isArray(paths)) {
  23. return [normalizePath(paths)];
  24. }
  25. return paths.map(function (p) {
  26. return normalizePath(p);
  27. });
  28. };
  29. var findFourCC = function findFourCC(bytes, paths) {
  30. paths = normalizePaths(paths);
  31. bytes = (0, _byteHelpers.toUint8)(bytes);
  32. var results = [];
  33. if (!paths.length) {
  34. // short-circuit the search for empty paths
  35. return results;
  36. }
  37. var i = 0;
  38. while (i < bytes.length) {
  39. var type = bytes.subarray(i, i + 4);
  40. var size = (bytes[i + 7] << 24 | bytes[i + 6] << 16 | bytes[i + 5] << 8 | bytes[i + 4]) >>> 0; // skip LIST/RIFF and get the actual type
  41. if ((0, _byteHelpers.bytesMatch)(type, CONSTANTS.LIST) || (0, _byteHelpers.bytesMatch)(type, CONSTANTS.RIFF) || (0, _byteHelpers.bytesMatch)(type, CONSTANTS.WAVE)) {
  42. type = bytes.subarray(i + 8, i + 12);
  43. i += 4;
  44. size -= 4;
  45. }
  46. var data = bytes.subarray(i + 8, i + 8 + size);
  47. if ((0, _byteHelpers.bytesMatch)(type, paths[0])) {
  48. if (paths.length === 1) {
  49. // this is the end of the path and we've found the box we were
  50. // looking for
  51. results.push(data);
  52. } else {
  53. // recursively search for the next box along the path
  54. var subresults = findFourCC(data, paths.slice(1));
  55. if (subresults.length) {
  56. results = results.concat(subresults);
  57. }
  58. }
  59. }
  60. i += 8 + data.length;
  61. } // we've finished searching all of bytes
  62. return results;
  63. };
  64. exports.findFourCC = findFourCC;