From 4fa973840f0a0e2cbb16f5e3341c0625793af2ed Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 05:56:56 +0900 Subject: refactor(php-shim): drop pack/unpack in favor of direct byte handling The only callers were trivial fixed-format uses: reading the first four hash bytes as a native int, splitting in_addr byte strings, and building a constant ZIP EOCD record. Each site now does the byte manipulation directly, so the general-purpose shims are no longer needed. Co-Authored-By: Claude Fable 5 --- .../src/dependency_resolver/generic_rule.rs | 17 +-- .../src/dependency_resolver/multi_conflict_rule.rs | 15 +-- .../shirabe/src/package/archiver/phar_archiver.rs | 26 ++-- .../shirabe/src/package/archiver/zip_archiver.rs | 24 ++-- crates/shirabe/src/util/no_proxy_pattern.rs | 137 ++++++++------------- 5 files changed, 78 insertions(+), 141 deletions(-) (limited to 'crates/shirabe/src') diff --git a/crates/shirabe/src/dependency_resolver/generic_rule.rs b/crates/shirabe/src/dependency_resolver/generic_rule.rs index 328a770d..8b64dc46 100644 --- a/crates/shirabe/src/dependency_resolver/generic_rule.rs +++ b/crates/shirabe/src/dependency_resolver/generic_rule.rs @@ -2,7 +2,7 @@ use super::rule::ReasonData; use crate::dependency_resolver::{Rule, RuleBase}; -use shirabe_php_shim::{PHP_VERSION_ID, RuntimeException, hash_raw, unpack}; +use shirabe_php_shim::{PHP_VERSION_ID, RuntimeException, hash_raw}; #[derive(Debug)] pub struct GenericRule { @@ -42,19 +42,8 @@ impl GenericRule { "sha1" }; let binary = hash_raw(algo, &joined); - let data = unpack("ihash", &binary); - match data { - Some(map) => { - if let Some(val) = map.get("hash") { - Ok(val.as_int().unwrap_or(0)) - } else { - Err(RuntimeException { - message: format!("Failed unpacking: {}", joined), - code: 0, - } - .into()) - } - } + match binary.get(..4) { + Some(chunk) => Ok(i32::from_ne_bytes(chunk.try_into().unwrap()) as i64), None => Err(RuntimeException { message: format!("Failed unpacking: {}", joined), code: 0, diff --git a/crates/shirabe/src/dependency_resolver/multi_conflict_rule.rs b/crates/shirabe/src/dependency_resolver/multi_conflict_rule.rs index bfbfc82a..0936dd0c 100644 --- a/crates/shirabe/src/dependency_resolver/multi_conflict_rule.rs +++ b/crates/shirabe/src/dependency_resolver/multi_conflict_rule.rs @@ -57,19 +57,8 @@ impl MultiConflictRule { "sha1" }; let binary = hash_raw(algo, &format!("c:{}", joined)); - let data = shirabe_php_shim::unpack("ihash", &binary); - match data { - Some(map) => { - if let Some(val) = map.get("hash") { - Ok(val.as_int().unwrap_or(0)) - } else { - Err(RuntimeException { - message: format!("Failed unpacking: {}", joined), - code: 0, - } - .into()) - } - } + match binary.get(..4) { + Some(chunk) => Ok(i32::from_ne_bytes(chunk.try_into().unwrap()) as i64), None => Err(RuntimeException { message: format!("Failed unpacking: {}", joined), code: 0, diff --git a/crates/shirabe/src/package/archiver/phar_archiver.rs b/crates/shirabe/src/package/archiver/phar_archiver.rs index 55e4cb95..fbab8c67 100644 --- a/crates/shirabe/src/package/archiver/phar_archiver.rs +++ b/crates/shirabe/src/package/archiver/phar_archiver.rs @@ -5,8 +5,8 @@ use crate::package::archiver::ArchivableFilesFinder; use crate::package::archiver::ArchiverInterface; use indexmap::IndexMap; use shirabe_php_shim::{ - FilesystemIterator, Phar, PharData, PhpMixed, RuntimeException, bzcompress, file_exists, - file_put_contents, function_exists, gzcompress, pack, str_repeat, strrpos, unlink, + FilesystemIterator, Phar, PharData, RuntimeException, bzcompress, file_exists, + file_put_contents, function_exists, gzcompress, str_repeat, strrpos, unlink, }; fn formats() -> IndexMap<&'static str, i64> { @@ -88,19 +88,15 @@ impl ArchiverInterface for PharArchiver { file_put_contents(&target, &str_repeat("\0", 10240).into_bytes()); } else if format == "zip" { // create minimal valid ZIP file (Empty Central Directory + End of Central Directory record) - let eocd = pack( - "VvvvvVVv", - &[ - PhpMixed::Int(0x06054b50), // End of central directory signature - PhpMixed::Int(0), // Number of this disk - PhpMixed::Int(0), // Disk where central directory starts - PhpMixed::Int(0), // Number of central directory records on this disk - PhpMixed::Int(0), // Total number of central directory records - PhpMixed::Int(0), // Size of central directory (bytes) - PhpMixed::Int(0), // Offset of start of central directory - PhpMixed::Int(0), // Comment length - ], - ); + let mut eocd = Vec::with_capacity(22); + eocd.extend_from_slice(&0x06054b50u32.to_le_bytes()); // End of central directory signature + eocd.extend_from_slice(&0u16.to_le_bytes()); // Number of this disk + eocd.extend_from_slice(&0u16.to_le_bytes()); // Disk where central directory starts + eocd.extend_from_slice(&0u16.to_le_bytes()); // Number of central directory records on this disk + eocd.extend_from_slice(&0u16.to_le_bytes()); // Total number of central directory records + eocd.extend_from_slice(&0u32.to_le_bytes()); // Size of central directory (bytes) + eocd.extend_from_slice(&0u32.to_le_bytes()); // Offset of start of central directory + eocd.extend_from_slice(&0u16.to_le_bytes()); // Comment length file_put_contents(&target, &eocd); } else if format == "tar.gz" || format == "tar.bz2" { let compress_algo = *compress_formats.get(format.as_str()).unwrap(); diff --git a/crates/shirabe/src/package/archiver/zip_archiver.rs b/crates/shirabe/src/package/archiver/zip_archiver.rs index 41201b56..bf0144fb 100644 --- a/crates/shirabe/src/package/archiver/zip_archiver.rs +++ b/crates/shirabe/src/package/archiver/zip_archiver.rs @@ -6,7 +6,7 @@ use crate::util::Filesystem; use crate::util::Platform; use indexmap::IndexMap; use shirabe_php_shim::{ - PhpMixed, RuntimeException, ZipArchive, class_exists, fileperms, method_exists, pack, realpath, + PhpMixed, RuntimeException, ZipArchive, class_exists, fileperms, method_exists, realpath, }; use std::path::PathBuf; @@ -93,19 +93,15 @@ impl ArchiverInterface for ZipArchiver { if zip.close() { if !std::path::Path::new(&target).exists() { // create minimal valid ZIP file (Empty Central Directory + End of Central Directory record) - let eocd = pack( - "VvvvvVVv", - &[ - PhpMixed::Int(0x06054b50), // End of central directory signature - PhpMixed::Int(0), // Number of this disk - PhpMixed::Int(0), // Disk where central directory starts - PhpMixed::Int(0), // Number of central directory records on this disk - PhpMixed::Int(0), // Total number of central directory records - PhpMixed::Int(0), // Size of central directory (bytes) - PhpMixed::Int(0), // Offset of start of central directory - PhpMixed::Int(0), // Comment length - ], - ); + let mut eocd = Vec::with_capacity(22); + eocd.extend_from_slice(&0x06054b50u32.to_le_bytes()); // End of central directory signature + eocd.extend_from_slice(&0u16.to_le_bytes()); // Number of this disk + eocd.extend_from_slice(&0u16.to_le_bytes()); // Disk where central directory starts + eocd.extend_from_slice(&0u16.to_le_bytes()); // Number of central directory records on this disk + eocd.extend_from_slice(&0u16.to_le_bytes()); // Total number of central directory records + eocd.extend_from_slice(&0u32.to_le_bytes()); // Size of central directory (bytes) + eocd.extend_from_slice(&0u32.to_le_bytes()); // Offset of start of central directory + eocd.extend_from_slice(&0u16.to_le_bytes()); // Comment length std::fs::write(&target, &eocd)?; } diff --git a/crates/shirabe/src/util/no_proxy_pattern.rs b/crates/shirabe/src/util/no_proxy_pattern.rs index a33f2451..5ece7942 100644 --- a/crates/shirabe/src/util/no_proxy_pattern.rs +++ b/crates/shirabe/src/util/no_proxy_pattern.rs @@ -5,7 +5,7 @@ use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ PHP_URL_HOST, PHP_URL_PORT, PHP_URL_SCHEME, PhpMixed, RuntimeException, array_key_exists, empty, explode, filter_var_int_with_range, filter_var_ip, inet_pton, ltrim, parse_url, - php_regex, stripos, strlen, strpbrk, strpos, substr, substr_count, unpack, + php_regex, stripos, strlen, strpbrk, strpos, substr, substr_count, }; /// Tests URLs against NO_PROXY patterns @@ -151,60 +151,38 @@ impl NoProxyPattern { /// Returns true if the target ip is in the network range pub(crate) fn match_range(&self, network: &IpData, target: &IpData) -> anyhow::Result { - let net = unpack("C*", &network.ip); - let mask = unpack("C*", network.netmask.as_deref().unwrap_or_default()); - let ip = unpack("C*", &target.ip); - let net = match net { - Some(n) => n, - None => { - return Err(RuntimeException { - message: format!( - "Could not parse network IP {}", - String::from_utf8_lossy(&network.ip) - ), - code: 0, - } - .into()); + let net = network.ip.as_slice(); + let mask = network.netmask.as_deref().unwrap_or_default(); + let ip = target.ip.as_slice(); + if net.is_empty() { + return Err(RuntimeException { + message: format!( + "Could not parse network IP {}", + String::from_utf8_lossy(net) + ), + code: 0, } - }; - let mask = match mask { - Some(m) => m, - None => { - return Err(RuntimeException { - message: format!( - "Could not parse netmask {}", - String::from_utf8_lossy(network.netmask.as_deref().unwrap_or_default()) - ), - code: 0, - } - .into()); + .into()); + } + if mask.is_empty() { + return Err(RuntimeException { + message: format!("Could not parse netmask {}", String::from_utf8_lossy(mask)), + code: 0, } - }; - let ip = match ip { - Some(i) => i, - None => { - return Err(RuntimeException { - message: format!( - "Could not parse target IP {}", - String::from_utf8_lossy(&target.ip) - ), - code: 0, - } - .into()); + .into()); + } + if ip.is_empty() { + return Err(RuntimeException { + message: format!("Could not parse target IP {}", String::from_utf8_lossy(ip)), + code: 0, } - }; + .into()); + } - // PHP: for ($i = 1; $i < 17; ++$i) - for i in 1..17 { - let net_byte = net - .get(&i.to_string()) - .and_then(|v| v.as_int()) - .unwrap_or(0); - let mask_byte = mask - .get(&i.to_string()) - .and_then(|v| v.as_int()) - .unwrap_or(0); - let ip_byte = ip.get(&i.to_string()).and_then(|v| v.as_int()).unwrap_or(0); + for i in 0..16 { + let net_byte = net.get(i).copied().unwrap_or(0); + let mask_byte = mask.get(i).copied().unwrap_or(0); + let ip_byte = ip.get(i).copied().unwrap_or(0); if (net_byte & mask_byte) != (ip_byte & mask_byte) { return Ok(false); } @@ -350,43 +328,32 @@ impl NoProxyPattern { let netmask = self.ip_get_mask(prefix, size); // Get the network from the address and mask - let mask = unpack("C*", &netmask); - let ip = unpack("C*", range_ip); - let mut net: Vec = vec![]; - let mask = match mask { - Some(m) => m, - None => { - return Err(RuntimeException { - message: format!( - "Could not parse netmask {}", - String::from_utf8_lossy(&netmask) - ), - code: 0, - } - .into()); + if netmask.is_empty() { + return Err(RuntimeException { + message: format!( + "Could not parse netmask {}", + String::from_utf8_lossy(&netmask) + ), + code: 0, } - }; - let ip = match ip { - Some(i) => i, - None => { - return Err(RuntimeException { - message: format!( - "Could not parse range IP {}", - String::from_utf8_lossy(range_ip) - ), - code: 0, - } - .into()); + .into()); + } + if range_ip.is_empty() { + return Err(RuntimeException { + message: format!( + "Could not parse range IP {}", + String::from_utf8_lossy(range_ip) + ), + code: 0, } - }; + .into()); + } - for i in 1..17 { - let ip_byte = ip.get(&i.to_string()).and_then(|v| v.as_int()).unwrap_or(0); - let mask_byte = mask - .get(&i.to_string()) - .and_then(|v| v.as_int()) - .unwrap_or(0); - net.push((ip_byte & mask_byte) as u8); + let mut net: Vec = Vec::with_capacity(16); + for i in 0..16 { + let ip_byte = range_ip.get(i).copied().unwrap_or(0); + let mask_byte = netmask.get(i).copied().unwrap_or(0); + net.push(ip_byte & mask_byte); } Ok((net, netmask)) -- cgit v1.3.1