| Total Complexity | 6 |
| Complexity/F | 1.5 |
| Lines of Code | 34 |
| Function Count | 4 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | |||
| 2 | /** |
||
| 3 | * ID3 header size uses only 7 bits of a byte, bit shift is needed. |
||
| 4 | * @returns Return a Buffer of 4 bytes with the encoded size |
||
| 5 | */ |
||
| 6 | export function encodeSize(size: number) { |
||
| 7 | const byte_3 = size & 0x7F |
||
| 8 | const byte_2 = (size >> 7) & 0x7F |
||
| 9 | const byte_1 = (size >> 14) & 0x7F |
||
| 10 | const byte_0 = (size >> 21) & 0x7F |
||
| 11 | return Buffer.from([byte_0, byte_1, byte_2, byte_3]) |
||
| 12 | } |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Decode the encoded size from an ID3 header. |
||
| 16 | */ |
||
| 17 | export function decodeSize(encodedSize: Buffer) { |
||
| 18 | return ( |
||
| 19 | (encodedSize[0] << 21) + |
||
| 20 | (encodedSize[1] << 14) + |
||
| 21 | (encodedSize[2] << 7) + |
||
| 22 | encodedSize[3] |
||
| 23 | ) |
||
| 24 | } |
||
| 25 | |||
| 26 | export function getFrameSize(buffer: Buffer, decode: boolean, version: number) { |
||
| 27 | const decodeBytes = version > 2 ? |
||
| 28 | [buffer[4], buffer[5], buffer[6], buffer[7]] : |
||
| 29 | [buffer[3], buffer[4], buffer[5]] |
||
| 30 | if (decode) { |
||
| 31 | return decodeSize(Buffer.from(decodeBytes)) |
||
| 32 | } |
||
| 33 | return Buffer.from(decodeBytes).readUIntBE(0, decodeBytes.length) |
||
| 34 | } |
||
| 35 |