This repository has been archived by the owner on May 24, 2021. It is now read-only.
forked from sindresorhus/file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
56 lines (43 loc) · 1.67 KB
/
util.js
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
'use strict';
exports.stringToBytes = string => [...string].map(character => character.charCodeAt(0));
const uint8ArrayUtf8ByteString = (array, start, end) => {
return String.fromCharCode(...array.slice(start, end));
};
exports.tarHeaderChecksumMatches = buffer => { // Does not check if checksum field characters are valid
if (buffer.length < 512) { // `tar` header size, cannot compute checksum without it
return false;
}
const MASK_8TH_BIT = 0x80;
let sum = 256; // Intitalize sum, with 256 as sum of 8 spaces in checksum field
let signedBitSum = 0; // Initialize signed bit sum
for (let i = 0; i < 148; i++) {
const byte = buffer[i];
sum += byte;
signedBitSum += byte & MASK_8TH_BIT; // Add signed bit to signed bit sum
}
// Skip checksum field
for (let i = 156; i < 512; i++) {
const byte = buffer[i];
sum += byte;
signedBitSum += byte & MASK_8TH_BIT; // Add signed bit to signed bit sum
}
const readSum = parseInt(uint8ArrayUtf8ByteString(buffer, 148, 154), 8); // Read sum in header
// Some implementations compute checksum incorrectly using signed bytes
return (
// Checksum in header equals the sum we calculated
readSum === sum ||
// Checksum in header equals sum we calculated plus signed-to-unsigned delta
readSum === (sum - (signedBitSum << 1))
);
};
exports.uint8ArrayUtf8ByteString = uint8ArrayUtf8ByteString;
/**
ID3 UINT32 sync-safe tokenizer token.
28 bits (representing up to 256MB) integer, the msb is 0 to avoid "false syncsignals".
*/
exports.uint32SyncSafeToken = {
get: (buffer, offset) => {
return (buffer[offset + 3] & 0x7F) | ((buffer[offset + 2]) << 7) | ((buffer[offset + 1]) << 14) | ((buffer[offset]) << 21);
},
len: 4
};