@js-ak/excel-toolbox 1.2.6 → 1.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/build/cjs/lib/zip/create-sync.js +32 -32
- package/build/cjs/lib/zip/create.js +32 -32
- package/build/cjs/lib/zip/read-sync.js +60 -12
- package/build/cjs/lib/zip/read.js +60 -12
- package/build/cjs/lib/zip/utils/crc-32.js +76 -0
- package/build/cjs/lib/zip/utils/dos-time.js +50 -0
- package/build/cjs/lib/zip/utils/find-data-descriptor.js +29 -0
- package/build/cjs/lib/zip/utils/index.js +20 -0
- package/build/cjs/lib/zip/utils/to-bytes.js +37 -0
- package/build/esm/lib/zip/create-sync.js +1 -1
- package/build/esm/lib/zip/create.js +1 -1
- package/build/esm/lib/zip/read-sync.js +27 -12
- package/build/esm/lib/zip/read.js +27 -12
- package/build/esm/lib/zip/utils/crc-32.js +73 -0
- package/build/esm/lib/zip/utils/dos-time.js +47 -0
- package/build/esm/lib/zip/utils/find-data-descriptor.js +26 -0
- package/build/esm/lib/zip/utils/index.js +4 -0
- package/build/esm/lib/zip/utils/to-bytes.js +34 -0
- package/build/types/lib/zip/utils/crc-32.d.ts +15 -0
- package/build/types/lib/zip/utils/dos-time.d.ts +25 -0
- package/build/types/lib/zip/utils/find-data-descriptor.d.ts +15 -0
- package/build/types/lib/zip/utils/index.d.ts +4 -0
- package/build/types/lib/zip/utils/to-bytes.d.ts +20 -0
- package/package.json +1 -1
- package/build/cjs/lib/zip/utils.js +0 -157
- package/build/esm/lib/zip/utils.js +0 -152
- package/build/types/lib/zip/utils.d.ts +0 -58
@@ -1,152 +0,0 @@
|
|
1
|
-
import { Buffer } from "node:buffer";
|
2
|
-
/**
|
3
|
-
* Precomputed CRC-32 lookup table for optimized checksum calculation.
|
4
|
-
* The table is generated using the standard IEEE 802.3 (Ethernet) polynomial:
|
5
|
-
* 0xEDB88320 (reversed representation of 0x04C11DB7).
|
6
|
-
*
|
7
|
-
* The table is immediately invoked and cached as a constant for performance,
|
8
|
-
* following the common implementation pattern for CRC algorithms.
|
9
|
-
*/
|
10
|
-
const crcTable = (() => {
|
11
|
-
// Create a typed array for better performance with 256 32-bit unsigned integers
|
12
|
-
const table = new Uint32Array(256);
|
13
|
-
// Generate table entries for all possible byte values (0-255)
|
14
|
-
for (let i = 0; i < 256; i++) {
|
15
|
-
let crc = i; // Initialize with current byte value
|
16
|
-
// Process each bit (8 times)
|
17
|
-
for (let j = 0; j < 8; j++) {
|
18
|
-
/*
|
19
|
-
* CRC division algorithm:
|
20
|
-
* 1. If LSB is set (crc & 1), XOR with polynomial
|
21
|
-
* 2. Right-shift by 1 (unsigned)
|
22
|
-
*
|
23
|
-
* The polynomial 0xEDB88320 is:
|
24
|
-
* - Bit-reversed version of 0x04C11DB7
|
25
|
-
* - Uses reflected input/output algorithm
|
26
|
-
*/
|
27
|
-
crc = crc & 1
|
28
|
-
? 0xedb88320 ^ (crc >>> 1) // XOR with polynomial if LSB is set
|
29
|
-
: crc >>> 1; // Just shift right if LSB is not set
|
30
|
-
}
|
31
|
-
// Store final 32-bit value (>>> 0 ensures unsigned 32-bit representation)
|
32
|
-
table[i] = crc >>> 0;
|
33
|
-
}
|
34
|
-
return table;
|
35
|
-
})();
|
36
|
-
/**
|
37
|
-
* Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
|
38
|
-
* This implementation uses a precomputed lookup table for optimal performance.
|
39
|
-
*
|
40
|
-
* The algorithm follows these characteristics:
|
41
|
-
* - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
|
42
|
-
* - Initial value: 0xFFFFFFFF (inverted by ~0)
|
43
|
-
* - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
|
44
|
-
* - Input and output reflection: Yes
|
45
|
-
*
|
46
|
-
* @param {Buffer} buf - The input buffer to calculate checksum for
|
47
|
-
* @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
|
48
|
-
*/
|
49
|
-
export function crc32(buf) {
|
50
|
-
// Initialize CRC with all 1's (0xFFFFFFFF) using bitwise NOT
|
51
|
-
let crc = ~0;
|
52
|
-
// Process each byte in the buffer
|
53
|
-
for (let i = 0; i < buf.length; i++) {
|
54
|
-
/*
|
55
|
-
* CRC update algorithm steps:
|
56
|
-
* 1. XOR current CRC with next byte (lowest 8 bits)
|
57
|
-
* 2. Use result as index in precomputed table (0-255)
|
58
|
-
* 3. XOR the table value with right-shifted CRC (8 bits)
|
59
|
-
*
|
60
|
-
* The operation breakdown:
|
61
|
-
* - (crc ^ buf[i]) - XOR with next byte
|
62
|
-
* - & 0xff - Isolate lowest 8 bits
|
63
|
-
* - crc >>> 8 - Shift CRC right by 8 bits (unsigned)
|
64
|
-
* - ^ crcTable[...] - XOR with precomputed table value
|
65
|
-
*/
|
66
|
-
crc = (crc >>> 8) ^ crcTable[(crc ^ buf[i]) & 0xff];
|
67
|
-
}
|
68
|
-
/*
|
69
|
-
* Final processing:
|
70
|
-
* 1. Invert all bits (~crc) to match standard CRC-32 output
|
71
|
-
* 2. Convert to unsigned 32-bit integer (>>> 0)
|
72
|
-
*/
|
73
|
-
return ~crc >>> 0;
|
74
|
-
}
|
75
|
-
/**
|
76
|
-
* Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
|
77
|
-
* as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
|
78
|
-
*
|
79
|
-
* The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
|
80
|
-
* the following bit layout:
|
81
|
-
*
|
82
|
-
* Time portion (2 bytes/16 bits):
|
83
|
-
* - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
|
84
|
-
* - Bits 05-10: Minutes (0-59)
|
85
|
-
* - Bits 11-15: Hours (0-23)
|
86
|
-
*
|
87
|
-
* Date portion (2 bytes/16 bits):
|
88
|
-
* - Bits 00-04: Day (1-31)
|
89
|
-
* - Bits 05-08: Month (1-12)
|
90
|
-
* - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
|
91
|
-
*
|
92
|
-
* @param {Date} date - The JavaScript Date object to convert
|
93
|
-
* @returns {Buffer} - 4-byte Buffer containing:
|
94
|
-
* - Bytes 0-1: DOS time (hours, minutes, seconds/2)
|
95
|
-
* - Bytes 2-3: DOS date (year-1980, month, day)
|
96
|
-
* @throws {RangeError} - If the date is before 1980 or after 2107
|
97
|
-
*/
|
98
|
-
export function dosTime(date) {
|
99
|
-
// Pack time components into 2 bytes (16 bits):
|
100
|
-
// - Hours (5 bits) shifted left 11 positions (bits 11-15)
|
101
|
-
// - Minutes (6 bits) shifted left 5 positions (bits 5-10)
|
102
|
-
// - Seconds/2 (5 bits) in least significant bits (bits 0-4)
|
103
|
-
const time = (date.getHours() << 11) | // Hours occupy bits 11-15
|
104
|
-
(date.getMinutes() << 5) | // Minutes occupy bits 5-10
|
105
|
-
(Math.floor(date.getSeconds() / 2)); // Seconds/2 occupy bits 0-4
|
106
|
-
// Pack date components into 2 bytes (16 bits):
|
107
|
-
// - (Year-1980) (7 bits) shifted left 9 positions (bits 9-15)
|
108
|
-
// - Month (4 bits) shifted left 5 positions (bits 5-8)
|
109
|
-
// - Day (5 bits) in least significant bits (bits 0-4)
|
110
|
-
const day = ((date.getFullYear() - 1980) << 9) | // Years since 1980 (bits 9-15)
|
111
|
-
((date.getMonth() + 1) << 5) | // Month 1-12 (bits 5-8)
|
112
|
-
date.getDate(); // Day 1-31 (bits 0-4)
|
113
|
-
// Combine both 2-byte values into a single 4-byte Buffer
|
114
|
-
// Note: Using little-endian byte order for each 2-byte segment
|
115
|
-
return Buffer.from([
|
116
|
-
...toBytes(time, 2), // Convert time to 2 bytes (LSB first)
|
117
|
-
...toBytes(day, 2), // Convert date to 2 bytes (LSB first)
|
118
|
-
]);
|
119
|
-
}
|
120
|
-
/**
|
121
|
-
* Converts a numeric value into a fixed-length Buffer representation,
|
122
|
-
* storing the value in little-endian format with right-padding of zeros.
|
123
|
-
*
|
124
|
-
* This is particularly useful for binary protocols or file formats that
|
125
|
-
* require fixed-width numeric fields.
|
126
|
-
*
|
127
|
-
* @param {number} value - The numeric value to convert to bytes.
|
128
|
-
* Note: JavaScript numbers are IEEE 754 doubles, but only the
|
129
|
-
* integer portion will be used (up to 53-bit precision).
|
130
|
-
* @param {number} len - The desired length of the output Buffer in bytes.
|
131
|
-
* Must be a positive integer.
|
132
|
-
* @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
|
133
|
-
* 1. The value's bytes in little-endian order (least significant byte first)
|
134
|
-
* 2. Zero padding in any remaining higher-order bytes
|
135
|
-
* @throws {RangeError} - If the value requires more bytes than `len` to represent
|
136
|
-
* (though this is currently not explicitly checked)
|
137
|
-
*/
|
138
|
-
export function toBytes(value, len) {
|
139
|
-
// Allocate a new Buffer of the requested length, automatically zero-filled
|
140
|
-
const buf = Buffer.alloc(len);
|
141
|
-
// Process each byte position from least significant to most significant
|
142
|
-
for (let i = 0; i < len; i++) {
|
143
|
-
// Store the least significant byte of the current value
|
144
|
-
buf[i] = value & 0xff; // Mask to get bottom 8 bits
|
145
|
-
// Right-shift the value by 8 bits to process the next byte
|
146
|
-
// Note: This uses unsigned right shift (>>> would be signed)
|
147
|
-
value >>= 8;
|
148
|
-
// If the loop completes with value != 0, we've overflowed the buffer length,
|
149
|
-
// but this isn't currently checked/handled
|
150
|
-
}
|
151
|
-
return buf;
|
152
|
-
}
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { Buffer } from "node:buffer";
|
2
|
-
/**
|
3
|
-
* Computes a CRC-32 checksum for the given Buffer using the standard IEEE 802.3 polynomial.
|
4
|
-
* This implementation uses a precomputed lookup table for optimal performance.
|
5
|
-
*
|
6
|
-
* The algorithm follows these characteristics:
|
7
|
-
* - Polynomial: 0xEDB88320 (reversed representation of 0x04C11DB7)
|
8
|
-
* - Initial value: 0xFFFFFFFF (inverted by ~0)
|
9
|
-
* - Final XOR value: 0xFFFFFFFF (achieved by inverting the result)
|
10
|
-
* - Input and output reflection: Yes
|
11
|
-
*
|
12
|
-
* @param {Buffer} buf - The input buffer to calculate checksum for
|
13
|
-
* @returns {number} - The 32-bit unsigned CRC-32 checksum (0x00000000 to 0xFFFFFFFF)
|
14
|
-
*/
|
15
|
-
export declare function crc32(buf: Buffer): number;
|
16
|
-
/**
|
17
|
-
* Converts a JavaScript Date object to a 4-byte Buffer in MS-DOS date/time format
|
18
|
-
* as specified in the ZIP file format specification (PKZIP APPNOTE.TXT).
|
19
|
-
*
|
20
|
-
* The MS-DOS date/time format packs both date and time into 4 bytes (32 bits) with
|
21
|
-
* the following bit layout:
|
22
|
-
*
|
23
|
-
* Time portion (2 bytes/16 bits):
|
24
|
-
* - Bits 00-04: Seconds divided by 2 (0-29, representing 0-58 seconds)
|
25
|
-
* - Bits 05-10: Minutes (0-59)
|
26
|
-
* - Bits 11-15: Hours (0-23)
|
27
|
-
*
|
28
|
-
* Date portion (2 bytes/16 bits):
|
29
|
-
* - Bits 00-04: Day (1-31)
|
30
|
-
* - Bits 05-08: Month (1-12)
|
31
|
-
* - Bits 09-15: Year offset from 1980 (0-127, representing 1980-2107)
|
32
|
-
*
|
33
|
-
* @param {Date} date - The JavaScript Date object to convert
|
34
|
-
* @returns {Buffer} - 4-byte Buffer containing:
|
35
|
-
* - Bytes 0-1: DOS time (hours, minutes, seconds/2)
|
36
|
-
* - Bytes 2-3: DOS date (year-1980, month, day)
|
37
|
-
* @throws {RangeError} - If the date is before 1980 or after 2107
|
38
|
-
*/
|
39
|
-
export declare function dosTime(date: Date): Buffer;
|
40
|
-
/**
|
41
|
-
* Converts a numeric value into a fixed-length Buffer representation,
|
42
|
-
* storing the value in little-endian format with right-padding of zeros.
|
43
|
-
*
|
44
|
-
* This is particularly useful for binary protocols or file formats that
|
45
|
-
* require fixed-width numeric fields.
|
46
|
-
*
|
47
|
-
* @param {number} value - The numeric value to convert to bytes.
|
48
|
-
* Note: JavaScript numbers are IEEE 754 doubles, but only the
|
49
|
-
* integer portion will be used (up to 53-bit precision).
|
50
|
-
* @param {number} len - The desired length of the output Buffer in bytes.
|
51
|
-
* Must be a positive integer.
|
52
|
-
* @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
|
53
|
-
* 1. The value's bytes in little-endian order (least significant byte first)
|
54
|
-
* 2. Zero padding in any remaining higher-order bytes
|
55
|
-
* @throws {RangeError} - If the value requires more bytes than `len` to represent
|
56
|
-
* (though this is currently not explicitly checked)
|
57
|
-
*/
|
58
|
-
export declare function toBytes(value: number, len: number): Buffer;
|