Skip to content

Commit 3c9971f

Browse files
committed
Refactor: extract ZIP signature and filename magic numbers into constants
1 parent 2917c56 commit 3c9971f

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/constants.mbt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ pub let default_max_output_size : Int = 104857600
55
///|
66
/// Default maximum input size for decompression (1GB)
77
pub let default_max_input_size : Int = 1073741824
8+
9+
///|
10+
/// ZIP Central Directory file header signature
11+
pub let zip_cd_signature : UInt = 0x02014B50U
12+
13+
///|
14+
/// ZIP Local File Header signature
15+
pub let zip_local_signature : UInt = 0x04034B50U
16+
17+
///|
18+
/// ZIP End of Central Directory signature
19+
pub let zip_eocd_signature : UInt = 0x06054B50U
20+
21+
///|
22+
/// ZIP64 End of Central Directory locator signature
23+
pub let zip64_eocd_locator_signature : UInt = 0x06064B50U
24+
25+
///|
26+
/// Maximum allowed filename length
27+
pub let max_filename_length : Int = 4096

src/pkg.generated.mbti

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub fn gzip_sync(FixedArray[Byte], opts? : GzipOptions) -> FixedArray[Byte]
2222

2323
pub fn inflate_sync(FixedArray[Byte], opts? : InflateOptions) -> FixedArray[Byte] raise FzipError
2424

25+
pub let max_filename_length : Int
26+
2527
pub fn str_from_u8(FixedArray[Byte], latin1? : Bool, offset? : Int, len? : Int) -> String raise FzipError
2628

2729
pub fn str_to_u8(String, latin1? : Bool) -> FixedArray[Byte]
@@ -32,6 +34,14 @@ pub fn unzip_sync(FixedArray[Byte]) -> Array[(String, FixedArray[Byte])] raise F
3234

3335
pub fn unzlib_sync(FixedArray[Byte], opts? : UnzlibOptions) -> FixedArray[Byte] raise FzipError
3436

37+
pub let zip64_eocd_locator_signature : UInt
38+
39+
pub let zip_cd_signature : UInt
40+
41+
pub let zip_eocd_signature : UInt
42+
43+
pub let zip_local_signature : UInt
44+
3545
pub fn zip_sync(Array[(String, FixedArray[Byte])], opts? : ZipEntryOptions) -> FixedArray[Byte]
3646

3747
pub fn zlib_sync(FixedArray[Byte], opts? : ZlibOptions) -> FixedArray[Byte]

src/zip.mbt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ fn zh(
4545
z : Bool,
4646
) -> (Int, Int, Int, String, Int, Int) raise FzipError {
4747
let fnl = b2(d, b + 28)
48-
// Validate filename length (max 4096 bytes is reasonable)
49-
if fnl > 4096 {
48+
// Validate filename length (max `max_filename_length` bytes is reasonable)
49+
if fnl > max_filename_length {
5050
raise fzip_err(InvalidZipData, msg="zip filename too long")
5151
}
5252
// Check bounds before reading filename
@@ -143,9 +143,9 @@ fn wzh(
143143
let mut b = b
144144
// signature
145145
if ce >= 0 {
146-
wbytes(d, b, 0x02014B50)
146+
wbytes(d, b, zip_cd_signature.reinterpret_as_int())
147147
} else {
148-
wbytes(d, b, 0x04034B50)
148+
wbytes(d, b, zip_local_signature.reinterpret_as_int())
149149
}
150150
b += 4
151151
if ce >= 0 {
@@ -221,7 +221,7 @@ fn wzh(
221221
///|
222222
/// Write ZIP end of central directory record
223223
fn wzf(o : FixedArray[Byte], b : Int, c : Int, d : Int, e : Int) -> Unit {
224-
wbytes(o, b, 0x06054B50)
224+
wbytes(o, b, zip_eocd_signature.reinterpret_as_int())
225225
// skip disk numbers (4 bytes = 0)
226226
wbytes(o, b + 8, c) // entries on this disk
227227
wbytes(o, b + 10, c) // total entries
@@ -342,7 +342,7 @@ pub fn unzip_sync(
342342
let files : Array[(String, FixedArray[Byte])] = []
343343
// Find end of central directory
344344
let mut e = data.length() - 22
345-
while e >= 0 && b4(data, e) != 0x06054B50U {
345+
while e >= 0 && b4(data, e) != zip_eocd_signature {
346346
e -= 1
347347
if data.length() - e > 65558 {
348348
raise fzip_err(InvalidZipData)
@@ -418,7 +418,7 @@ pub fn unzip_list(
418418
}
419419
let infos : Array[UnzipFileInfo] = []
420420
let mut e = data.length() - 22
421-
while e >= 0 && b4(data, e) != 0x06054B50U {
421+
while e >= 0 && b4(data, e) != zip_eocd_signature {
422422
e -= 1
423423
if data.length() - e > 65558 {
424424
raise fzip_err(InvalidZipData)

0 commit comments

Comments
 (0)