Files
NapCatQQ/.pnpm-store/v3/files/f9/8d99fa9d81d8299bb5cde22ec78e48d53d1a48b08356f6dd2370613e37431abd408503515465e8a2025da7a618279a1ac7cdc183369a65dfc8a5a7370f1850
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

61 lines
1.8 KiB
Plaintext

import {StringType} from 'token-types';
export function stringToBytes(string, encoding) {
if (encoding === 'utf-16le') {
const bytes = [];
for (let index = 0; index < string.length; index++) {
const code = string.charCodeAt(index); // eslint-disable-line unicorn/prefer-code-point
bytes.push(code & 0xFF, (code >> 8) & 0xFF); // High byte
}
return bytes;
}
if (encoding === 'utf-16be') {
const bytes = [];
for (let index = 0; index < string.length; index++) {
const code = string.charCodeAt(index); // eslint-disable-line unicorn/prefer-code-point
bytes.push((code >> 8) & 0xFF, code & 0xFF); // Low byte
}
return bytes;
}
return [...string].map(character => character.charCodeAt(0)); // eslint-disable-line unicorn/prefer-code-point
}
/**
Checks whether the TAR checksum is valid.
@param {Uint8Array} arrayBuffer - The TAR header `[offset ... offset + 512]`.
@param {number} offset - TAR header offset.
@returns {boolean} `true` if the TAR checksum is valid, otherwise `false`.
*/
export function tarHeaderChecksumMatches(arrayBuffer, offset = 0) {
const readSum = Number.parseInt(new StringType(6).get(arrayBuffer, 148).replace(/\0.*$/, '').trim(), 8); // Read sum in header
if (Number.isNaN(readSum)) {
return false;
}
let sum = 8 * 0x20; // Initialize signed bit sum
for (let index = offset; index < offset + 148; index++) {
sum += arrayBuffer[index];
}
for (let index = offset + 156; index < offset + 512; index++) {
sum += arrayBuffer[index];
}
return readSum === sum;
}
/**
ID3 UINT32 sync-safe tokenizer token.
28 bits (representing up to 256MB) integer, the msb is 0 to avoid "false syncsignals".
*/
export const uint32SyncSafeToken = {
get: (buffer, offset) => (buffer[offset + 3] & 0x7F) | ((buffer[offset + 2]) << 7) | ((buffer[offset + 1]) << 14) | ((buffer[offset]) << 21),
len: 4,
};