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