From cc952d361a9f2f2a4ad48cecd01d2b55e896eee8 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 01:38:06 -0400 Subject: [PATCH 01/14] feat(binary): binary patch types and parser * Added types representing both literal and delta Git binary patches * Added a parser for the `GIT binary patch` format. This doesn't include the patch application (which will be added in later commits) The implementation is based on * Specification from * Behavior observation of Git CLI --- src/binary/mod.rs | 338 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 339 insertions(+) create mode 100644 src/binary/mod.rs diff --git a/src/binary/mod.rs b/src/binary/mod.rs new file mode 100644 index 00000000..7f2db8fc --- /dev/null +++ b/src/binary/mod.rs @@ -0,0 +1,338 @@ +//! Git binary diffs support. +//! +//! This module provides parsing and decoding for Git's binary diff format, +//! as generated by `git diff --binary` or `git format-patch --binary`. +//! +//! Based on [DiffX Binary Diffs specification](https://diffx.org/spec/binary-diffs.html). + +use std::{fmt, ops::Range}; + +/// The type of a binary patch block. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BinaryBlockKind { + /// [Literal](https://diffx.org/spec/binary-diffs.html#git-literal-binary-diffs): + /// contains the full file content, zlib-compressed and Base85-encoded. + Literal, + /// [Delta](https://diffx.org/spec/binary-diffs.html#git-delta-binary-diffs): + /// contains delta instructions to transform one file into another. + Delta, +} + +/// A single block in a binary patch. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BinaryBlock<'a> { + /// The type of this block (literal content or delta instructions). + pub kind: BinaryBlockKind, + /// The encoded data. + pub data: BinaryData<'a>, +} + +/// A parsed binary patch. +/// +/// A binary patch contains encoded binary data that can be decoded +/// to recover the original and modified file contents. +/// +/// Git may use different encodings for each direction: +/// +/// - `literal`: full file content +/// - `delta`: instructions to transform one file into another +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum BinaryPatch<'a> { + /// A full binary patch with forward and reverse data. + /// + /// The forward block transforms original -> modified. + /// The reverse block transforms modified -> original. + /// + /// Each block can independently be either `literal` or `delta`. + Full { + /// Forward transformation (original -> modified). + forward: BinaryBlock<'a>, + /// Reverse transformation (modified -> original). + reverse: BinaryBlock<'a>, + }, + /// A Git binary diff marker. + /// + /// This represents the `Binary files a/path and b/path differ` case, + /// where git detected a binary change but didn't include the actual data. + Marker, +} + +/// Represents a single binary payload in a Git binary diff. +/// +/// For example, the following patch block +/// +/// * is parsed as `BinaryData { size: 10, data: "UcmV+l0QLU>0RjUA1qKHQ2>\`DEE&u=k" }` +/// * The line starts with a length indicator (`U` = 21 decoded bytes) +/// +/// +/// ```text +/// literal 10 +/// UcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k +/// ``` +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BinaryData<'a> { + /// Uncompressed size in bytes. + pub size: u64, + /// Raw Base85 lines with length indicators. + pub data: &'a str, +} + +/// Error type for binary patch operations. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BinaryPatchParseError { + pub(crate) kind: BinaryPatchParseErrorKind, + span: Option>, +} + +impl BinaryPatchParseError { + /// Creates a new error with the given kind and span. + pub(crate) fn new(kind: BinaryPatchParseErrorKind, span: Range) -> Self { + Self { + kind, + span: Some(span), + } + } + + /// Returns the byte range in the input where the error occurred. + pub fn span(&self) -> Option> { + self.span.clone() + } +} + +impl fmt::Display for BinaryPatchParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(span) = &self.span { + write!( + f, + "error parsing binary patch at byte {}: {}", + span.start, self.kind + ) + } else { + write!(f, "error parsing binary patch: {}", self.kind) + } + } +} + +impl std::error::Error for BinaryPatchParseError {} + +impl From for BinaryPatchParseError { + fn from(kind: BinaryPatchParseErrorKind) -> Self { + Self { kind, span: None } + } +} + +/// The kind of error that occurred when parsing a binary patch. +#[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub(crate) enum BinaryPatchParseErrorKind { + /// Missing or invalid "GIT binary patch" header. + InvalidHeader, + + /// First binary block (forward) not found. + MissingForwardBlock, + + /// Second binary block (reverse) not found. + MissingReverseBlock, + + /// No binary data available (marker-only patch). + NoBinaryData, + + /// Invalid line length indicator in Base85 data. + InvalidLineLengthIndicator, +} + +impl fmt::Display for BinaryPatchParseErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidHeader => write!(f, "invalid binary patch header"), + Self::MissingForwardBlock => write!(f, "first binary block not found"), + Self::MissingReverseBlock => write!(f, "second binary block not found"), + Self::NoBinaryData => write!(f, "no binary data available"), + Self::InvalidLineLengthIndicator => write!(f, "invalid line length indicator"), + } + } +} + +/// Simple streaming parser for binary patches. +struct BinaryParser<'a> { + input: &'a str, + offset: usize, +} + +impl<'a> BinaryParser<'a> { + fn new(input: &'a str) -> Self { + Self { input, offset: 0 } + } + + /// Creates an error with the current offset as span. + fn error(&self, kind: BinaryPatchParseErrorKind) -> BinaryPatchParseError { + BinaryPatchParseError::new(kind, self.offset..self.offset) + } + + fn next_line(&mut self) -> Option<&'a str> { + let rest = &self.input[self.offset..]; + if rest.is_empty() { + return None; + } + let (line, skip) = match rest.find('\n') { + Some(pos) => (&rest[..pos], pos + 1), + None => (rest, rest.len()), + }; + self.offset += skip; + Some(line.strip_suffix('\r').unwrap_or(line)) + } +} + +/// Parses binary patch content after git extended headers. +/// +/// Expects input starting with "GIT binary patch" line. +/// Returns the parsed patch and the number of bytes consumed. +/// +/// Format: +/// +/// ```text +/// GIT binary patch +/// +/// +/// +/// +/// +/// ``` +pub(crate) fn parse_binary_patch( + input: &str, +) -> Result<(BinaryPatch<'_>, usize), BinaryPatchParseError> { + let mut parser = BinaryParser::new(input); + + // Expect "GIT binary patch" marker + if parser.next_line() != Some("GIT binary patch") { + return Err(parser.error(BinaryPatchParseErrorKind::InvalidHeader)); + } + + // Parse first block (forward: original -> modified) + let Some(forward) = parse_binary_block(&mut parser) else { + return Err(parser.error(BinaryPatchParseErrorKind::MissingForwardBlock)); + }; + + // Parse second block (reverse: modified -> original) + let Some(reverse) = parse_binary_block(&mut parser) else { + return Err(parser.error(BinaryPatchParseErrorKind::MissingReverseBlock)); + }; + + Ok((BinaryPatch::Full { forward, reverse }, parser.offset)) +} + +/// Parses a single binary block. +/// +/// Returns a `BinaryBlock` with kind (literal/delta) and data. +fn parse_binary_block<'a>(parser: &mut BinaryParser<'a>) -> Option> { + // Parse "literal 10" or "delta 18" + let format_line = parser.next_line()?; + let (patch_type, size_str) = format_line.split_once(' ')?; + let size: u64 = size_str.parse().ok()?; + + let kind = match patch_type { + "literal" => BinaryBlockKind::Literal, + "delta" => BinaryBlockKind::Delta, + _ => return None, + }; + + // Consume Base85 lines until blank line, tracking the end of actual data. + let data_start = parser.offset; + let mut data_end = data_start; + while let Some(line) = parser.next_line() { + if line.is_empty() { + break; + } + data_end = parser.offset; + } + + // Slice the data lines, stripping the final line ending. + let data = &parser.input[data_start..data_end]; + let data = data + .strip_suffix("\r\n") + .or_else(|| data.strip_suffix('\n')) + .unwrap_or(data); + + Some(BinaryBlock { + kind, + data: BinaryData { size, data }, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_literal_format_simple() { + let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let (patch, consumed) = parse_binary_patch(input).unwrap(); + + assert_eq!(consumed, input.len()); + match &patch { + BinaryPatch::Full { forward, reverse } => { + assert_eq!(forward.kind, BinaryBlockKind::Literal); + assert_eq!(forward.data.size, 10); + assert_eq!(reverse.kind, BinaryBlockKind::Literal); + assert_eq!(reverse.data.size, 0); + } + _ => panic!("expected Full variant"), + } + } + + #[test] + fn parse_delta_format() { + let input = "GIT binary patch\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^BFm9#}av-trB0zxAOrvLx|\n\n"; + let (patch, _) = parse_binary_patch(input).unwrap(); + + match &patch { + BinaryPatch::Full { forward, reverse } => { + assert_eq!(forward.kind, BinaryBlockKind::Delta); + assert_eq!(forward.data.size, 18); + assert_eq!(reverse.kind, BinaryBlockKind::Delta); + assert_eq!(reverse.data.size, 18); + } + _ => panic!("expected Full variant"), + } + } + + #[test] + fn parse_invalid_header() { + // Without "GIT binary patch" marker, parse_binary_patch returns error + let input = "literal 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\n"; + let err = parse_binary_patch(input).unwrap_err(); + assert_eq!(err.kind, BinaryPatchParseErrorKind::InvalidHeader); + } + + #[test] + fn parse_with_crlf_line_endings() { + let input = "GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; + let (patch, consumed) = parse_binary_patch(input).unwrap(); + + assert_eq!(consumed, input.len()); + match &patch { + BinaryPatch::Full { forward, reverse } => { + assert_eq!(forward.kind, BinaryBlockKind::Literal); + assert_eq!(forward.data.size, 10); + assert_eq!(reverse.kind, BinaryBlockKind::Literal); + assert_eq!(reverse.data.size, 0); + } + _ => panic!("expected Full variant"), + } + } + + #[test] + fn parse_mixed_format() { + // Git can use different encoding for each direction + let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\n"; + let (patch, _) = parse_binary_patch(input).unwrap(); + + match &patch { + BinaryPatch::Full { forward, reverse } => { + assert_eq!(forward.kind, BinaryBlockKind::Literal); + assert_eq!(reverse.kind, BinaryBlockKind::Delta); + } + _ => panic!("expected Full variant"), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 33ebbb64..5e11a40d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -223,6 +223,7 @@ //! [`create_patch_bytes`]: fn.create_patch_bytes.html mod apply; +pub mod binary; mod diff; mod merge; mod patch; From 7c505f6a8421ff388b7e1e348d3dd29a88cb84a8 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 10:33:33 -0400 Subject: [PATCH 02/14] refactor(utils): add `Text::as_str_prefix` method Returns the longest valid UTF-8 prefix of the input. For `str` this is the entire input; for `[u8]` it truncates at the first invalid byte. This will be used at call sites where generic `T: Text` input needs to be narrowed to `&str` for parsers that only handle ASCII data (e.g. binary patch base85 content). --- src/utils.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index c4914566..e337e6cd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -144,6 +144,11 @@ pub trait Text: Eq + Hash + ToOwned { fn find(&self, needle: &str) -> Option; fn split_at(&self, mid: usize) -> (&Self, &Self); fn as_str(&self) -> Option<&str>; + /// Returns the longest valid UTF-8 prefix. + /// + /// For `str` this is the entire input. + /// For `[u8]` this returns up to the first invalid UTF-8 byte. + fn as_str_prefix(&self) -> &str; fn as_bytes(&self) -> &[u8]; #[allow(unused)] fn lines(&self) -> LineIter<'_, Self>; @@ -205,6 +210,10 @@ impl Text for str { Some(self) } + fn as_str_prefix(&self) -> &str { + self + } + fn as_bytes(&self) -> &[u8] { self.as_bytes() } @@ -259,6 +268,13 @@ impl Text for [u8] { std::str::from_utf8(self).ok() } + fn as_str_prefix(&self) -> &str { + match std::str::from_utf8(self) { + Ok(s) => s, + Err(e) => std::str::from_utf8(&self[..e.valid_up_to()]).unwrap(), + } + } + fn as_bytes(&self) -> &[u8] { self } From 26973522fff35b7a158679ba826838f087f431ac Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 10:41:00 -0400 Subject: [PATCH 03/14] feat(patch_set): wire binary diff parsing --- src/patch_set/error.rs | 11 +++++++++ src/patch_set/mod.rs | 22 ++++++++++++----- src/patch_set/parse.rs | 55 ++++++++++++++++++++++++++++++++++++------ src/patch_set/tests.rs | 7 +++--- tests/compat/common.rs | 13 +++++++--- tests/replay.rs | 4 ++- 6 files changed, 92 insertions(+), 20 deletions(-) diff --git a/src/patch_set/error.rs b/src/patch_set/error.rs index 3a8fc616..2c9116b6 100644 --- a/src/patch_set/error.rs +++ b/src/patch_set/error.rs @@ -3,6 +3,7 @@ use std::fmt; use std::ops::Range; +use crate::binary::BinaryPatchParseError; use crate::patch::ParsePatchError; /// An error returned when parsing patches fails. @@ -76,6 +77,9 @@ pub(crate) enum PatchSetParseErrorKind { /// Invalid `diff --git` path. InvalidDiffGitPath, + + /// Binary patch parsing failed. + BinaryParse(BinaryPatchParseError), } impl fmt::Display for PatchSetParseErrorKind { @@ -89,6 +93,7 @@ impl fmt::Display for PatchSetParseErrorKind { Self::CreateMissingModifiedPath => write!(f, "create patch has no modified path"), Self::InvalidFileMode(mode) => write!(f, "invalid file mode: {mode}"), Self::InvalidDiffGitPath => write!(f, "invalid diff --git path"), + Self::BinaryParse(e) => write!(f, "{e}"), } } } @@ -98,3 +103,9 @@ impl From for PatchSetParseError { PatchSetParseErrorKind::Patch(e).into() } } + +impl From for PatchSetParseError { + fn from(e: BinaryPatchParseError) -> Self { + PatchSetParseErrorKind::BinaryParse(e).into() + } +} diff --git a/src/patch_set/mod.rs b/src/patch_set/mod.rs index ed3f68e4..16ec2808 100644 --- a/src/patch_set/mod.rs +++ b/src/patch_set/mod.rs @@ -11,6 +11,7 @@ mod tests; use std::borrow::Cow; use std::fmt; +use crate::binary::BinaryPatch; use crate::utils::Text; use crate::Patch; @@ -90,7 +91,7 @@ impl ParseOptions { /// * `diff --git` headers /// * Extended headers (`new file mode`, `deleted file mode`, etc.) /// * Rename/copy detection (`rename from`/`rename to`, `copy from`/`copy to`) - /// * Binary file detection (emitted a marker by defualt) + /// * Binary file detection /// /// [git-diff-format]: https://git-scm.com/docs/diff-format pub fn gitdiff() -> Self { @@ -133,7 +134,7 @@ pub enum PatchKind<'a, T: ToOwned + ?Sized> { /// Text patch with hunks. Text(Patch<'a, T>), /// Binary patch (literal or delta encoded, or marker-only). - Binary, + Binary(BinaryPatch<'a>), } impl std::fmt::Debug for PatchKind<'_, T> @@ -144,7 +145,7 @@ where fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { PatchKind::Text(patch) => f.debug_tuple("Text").field(patch).finish(), - PatchKind::Binary => f.write_str("Binary"), + PatchKind::Binary(patch) => f.debug_tuple("Binary").field(patch).finish(), } } } @@ -154,13 +155,21 @@ impl<'a, T: ToOwned + ?Sized> PatchKind<'a, T> { pub fn as_text(&self) -> Option<&Patch<'a, T>> { match self { PatchKind::Text(patch) => Some(patch), - PatchKind::Binary => None, + PatchKind::Binary(_) => None, + } + } + + /// Returns the binary patch, or `None` if this is a text patch. + pub fn as_binary(&self) -> Option<&BinaryPatch<'a>> { + match self { + PatchKind::Binary(patch) => Some(patch), + PatchKind::Text(_) => None, } } /// Returns `true` if this is a binary diff. pub fn is_binary(&self) -> bool { - matches!(self, PatchKind::Binary) + matches!(self, PatchKind::Binary(_)) } } @@ -209,12 +218,13 @@ impl<'a, T: ToOwned + ?Sized> FilePatch<'a, T> { fn new_binary( operation: FileOperation<'a, T>, + patch: BinaryPatch<'a>, old_mode: Option, new_mode: Option, ) -> Self { Self { operation, - kind: PatchKind::Binary, + kind: PatchKind::Binary(patch), old_mode, new_mode, } diff --git a/src/patch_set/parse.rs b/src/patch_set/parse.rs index 08339da8..e552e647 100644 --- a/src/patch_set/parse.rs +++ b/src/patch_set/parse.rs @@ -4,6 +4,7 @@ use super::{ error::PatchSetParseErrorKind, FileMode, FileOperation, FilePatch, Format, ParseOptions, PatchSetParseError, }; +use crate::binary::{parse_binary_patch, BinaryPatch}; use crate::patch::parse::parse_one; use crate::utils::{escaped_filename, Text}; use crate::Patch; @@ -227,8 +228,9 @@ fn next_gitdiff_patch<'a, T: Text + ?Sized>( let (header, header_consumed) = GitHeader::parse(remaining(ps)); ps.offset += header_consumed; - // Handle binary markers ("Binary files ... differ") and binary patches ("GIT binary patch") - if header.is_binary_marker || header.is_binary_patch { + // Handle "Binary files ... differ" (no patch data) + if header.is_binary_marker { + // FIXME: error spans point at `diff --git` line, not the specific offending line let operation = match extract_file_op_binary(&header, abs_patch_start) { Ok(op) => op, Err(e) => return Some(Err(e)), @@ -240,7 +242,45 @@ fn next_gitdiff_patch<'a, T: Text + ?Sized>( return Some(Err(e)); } }; - return Some(Ok(FilePatch::new_binary(operation, old_mode, new_mode))); + return Some(Ok(FilePatch::new_binary( + operation, + BinaryPatch::Marker, + old_mode, + new_mode, + ))); + } + + // Handle "GIT binary patch" (has patch data) + if let Some(binary_patch_start) = header.binary_patch_offset { + // GitHeader::parse consumed the marker line but not the payload. + // Use the recorded offset to pass input from the marker onward. + let (_, binary_input) = ps.input.split_at(abs_patch_start + binary_patch_start); + // Binary patch data is always ASCII (base85-encoded). + let binary_input = binary_input.as_str_prefix(); + let (binary_patch, consumed) = match parse_binary_patch(binary_input) { + Ok(result) => result, + Err(e) => return Some(Err(e.into())), + }; + ps.offset = abs_patch_start + binary_patch_start + consumed; + + // FIXME: error spans point at `diff --git` line, not the specific offending line + let operation = match extract_file_op_binary(&header, abs_patch_start) { + Ok(op) => op, + Err(e) => return Some(Err(e)), + }; + let (old_mode, new_mode) = match parse_file_modes(&header) { + Ok(modes) => modes, + Err(mut e) => { + e.set_span(abs_patch_start..abs_patch_start); + return Some(Err(e)); + } + }; + return Some(Ok(FilePatch::new_binary( + operation, + binary_patch, + old_mode, + new_mode, + ))); } // `git diff` output format is stricter. @@ -328,7 +368,8 @@ struct GitHeader<'a, T: ?Sized> { /// Binary files /dev/null and b/image.png differ /// ``` is_binary_marker: bool, - /// Whether this is a binary diff with actual patch content. + /// Byte offset of `"GIT binary patch"` line relative to header input, + /// or `None` if no binary patch content was found. /// /// Observed `git diff --binary` output: /// @@ -343,7 +384,7 @@ struct GitHeader<'a, T: ?Sized> { /// literal 0 /// KcmV+b0RR6000031 /// ``` - is_binary_patch: bool, + binary_patch_offset: Option, } impl Default for GitHeader<'_, T> { @@ -359,7 +400,7 @@ impl Default for GitHeader<'_, T> { new_file_mode: None, deleted_file_mode: None, is_binary_marker: false, - is_binary_patch: false, + binary_patch_offset: None, } } } @@ -410,7 +451,7 @@ impl<'a, T: Text + ?Sized> GitHeader<'a, T> { } else if trimmed.starts_with("Binary files ") { header.is_binary_marker = true; } else if trimmed.starts_with("GIT binary patch") { - header.is_binary_patch = true; + header.binary_patch_offset = Some(consumed); } else { // Unrecognized line: End of extended headers // (typically `---`/`+++`/`@@` or trailing content). diff --git a/src/patch_set/tests.rs b/src/patch_set/tests.rs index ee7f75bb..d1ff8804 100644 --- a/src/patch_set/tests.rs +++ b/src/patch_set/tests.rs @@ -752,7 +752,8 @@ index 0000000..e69de29 } #[test] - fn binary_emits_marker() { + fn binary_marker_kept_by_default() { + // Default is Keep: binary marker is returned as BinaryPatch::Marker. let input = "\ diff --git a/img.png b/img.png Binary files a/img.png and b/img.png differ @@ -765,9 +766,9 @@ diff --git a/foo b/foo "; let patches = parse_gitdiff(input); assert_eq!(patches.len(), 2); - assert!(patches[0].patch().is_binary()); + assert!(patches[0].patch().as_binary().is_some()); assert!(patches[0].operation().is_modify()); - assert!(!patches[1].patch().is_binary()); + assert!(patches[1].patch().as_text().is_some()); } } diff --git a/tests/compat/common.rs b/tests/compat/common.rs index 1c8c8c2e..6e871b08 100644 --- a/tests/compat/common.rs +++ b/tests/compat/common.rs @@ -8,7 +8,10 @@ use std::{ sync::Once, }; -use diffy::patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet, PatchSetParseError}; +use diffy::{ + binary::BinaryPatch, + patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet, PatchSetParseError}, +}; /// Which external tool to compare against. #[derive(Clone, Copy)] @@ -411,8 +414,12 @@ pub fn apply_diffy( } fs::write(&result_path, &result).unwrap(); } - PatchKind::Binary => { - // No patch data to apply — nothing to do. + PatchKind::Binary(BinaryPatch::Marker) => { + // Dont do anything if it is just a binary patch marker. + } + PatchKind::Binary(_) => { + // Binary patch application requires the `binary` feature. + // Will be wired up when that feature is added. } } } diff --git a/tests/replay.rs b/tests/replay.rs index c98eef12..e6fbab3e 100644 --- a/tests/replay.rs +++ b/tests/replay.rs @@ -544,7 +544,9 @@ fn process_commit( ); } } - PatchKind::Binary => { + PatchKind::Binary(_) => { + // Binary patch application not yet wired up in replay tests. + // Will be done once the `binary` Cargo feature is added. skipped += 1; continue; } From 1c6a9e26ec5658a8f88aeb766ae60cd1fb78fe59 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 10:43:30 -0400 Subject: [PATCH 04/14] refactor: clippy manual_div_ceil The API was stabilized in 1.73. The lint was added in 1.93. This is required for a MSRV bump to 1.75 --- src/diff/myers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff/myers.rs b/src/diff/myers.rs index 73eb4106..502d0772 100644 --- a/src/diff/myers.rs +++ b/src/diff/myers.rs @@ -69,7 +69,7 @@ impl ::std::fmt::Display for Snake { fn max_d(len1: usize, len2: usize) -> usize { // XXX look into reducing the need to have the additional '+ 1' - (len1 + len2 + 1) / 2 + 1 + (len1 + len2).div_ceil(2) + 1 } // The divide part of a divide-and-conquer strategy. A D-path has D+1 snakes some of which may From ff60fe77d8a69b0061be46c5b6a82ff4858934bb Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 10:43:59 -0400 Subject: [PATCH 05/14] chore: dependency flate2 behind binary feature This is a preparation for binary diff application support. * Git binary patch is compressed by zlib hence flate2 * zlib-rs (which is the most performant zlib backend) requires MSRV 1.75.0+ hence the bump. --- .github/workflows/ci.yml | 2 +- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 +++- deny.toml | 1 + 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f514b37..d0ca9b80 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, beta, nightly, 1.70.0] + rust: [stable, beta, nightly, 1.75.0] steps: - uses: actions/checkout@v6 diff --git a/Cargo.lock b/Cargo.lock index ad290e3b..c726b1cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "anstream" version = "0.6.21" @@ -109,6 +115,7 @@ name = "diffy" version = "0.4.2" dependencies = [ "anstyle", + "flate2", "rayon", "snapbox", ] @@ -152,6 +159,16 @@ dependencies = [ "libredox", ] +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "miniz_oxide", + "zlib-rs", +] + [[package]] name = "getrandom" version = "0.3.4" @@ -200,6 +217,16 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" @@ -281,6 +308,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "simd-adler32" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214" + [[package]] name = "similar" version = "2.7.0" @@ -446,3 +479,9 @@ name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" + +[[package]] +name = "zlib-rs" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3be3d40e40a133f9c916ee3f9f4fa2d9d63435b5fbe1bfc6d9dae0aa0ada1513" diff --git a/Cargo.toml b/Cargo.toml index 25c6e518..68ce3ff3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,14 +9,16 @@ repository = "https://github.com/bmwill/diffy" readme = "README.md" keywords = ["diff", "patch", "merge"] categories = ["text-processing"] -rust-version = "1.70.0" +rust-version = "1.75.0" edition = "2021" [features] +binary = ["dep:flate2"] color = ["dep:anstyle"] [dependencies] anstyle = { version = "1.0.13", optional = true } +flate2 = { version = "1.1.9", optional = true, default-features = false, features = ["zlib-rs"] } [dev-dependencies] rayon = "1.10.0" diff --git a/deny.toml b/deny.toml index d495b7c5..740f35a3 100644 --- a/deny.toml +++ b/deny.toml @@ -91,6 +91,7 @@ ignore = [ allow = [ "MIT", "Apache-2.0", + "Zlib", #"Apache-2.0 WITH LLVM-exception", ] # The confidence threshold for detecting a license from license text. From d2b9720dd01251f6675141e13577f364951ad3dc Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 17 Apr 2026 10:50:14 -0400 Subject: [PATCH 06/14] feat(binary): base85/delta decode and patch application * Add base85 encoder/decoder and Git delta format decoder. * Wire them into `BinaryPatch::apply() and `apply_reverse()` for decoding zlib-compressed, base85-encoded binary payload. These are feature-gated behind the `binary` feature. --- src/binary/base85.rs | 229 +++++++++++++++++++++++++++ src/binary/delta.rs | 362 +++++++++++++++++++++++++++++++++++++++++++ src/binary/mod.rs | 210 +++++++++++++++++++++++++ 3 files changed, 801 insertions(+) create mode 100644 src/binary/base85.rs create mode 100644 src/binary/delta.rs diff --git a/src/binary/base85.rs b/src/binary/base85.rs new file mode 100644 index 00000000..182efe87 --- /dev/null +++ b/src/binary/base85.rs @@ -0,0 +1,229 @@ +//! Base85 encoding and decoding using the character set defined in [RFC 1924]. +//! +//! ## References +//! +//! * [RFC 1924] +//! * [Wikipedia: Ascii85 § RFC 1924 version](https://en.wikipedia.org/wiki/Ascii85#RFC_1924_version) +//! +//! [RFC 1924]: https://datatracker.ietf.org/doc/html/rfc1924 + +use std::fmt; + +/// Base85 character set (RFC 1924). +const ALPHABET: &[u8; 85] = b"0123456789\ + ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\ + !#$%&()*+-;<=>?@^_`{|}~"; + +/// Pre-computed lookup table for Base85 decoding. +/// +/// Maps ASCII byte value → digit value or `0xFF` for invalid characters. +/// This provides O(1) lookup. +const TABLE: [u8; 256] = { + let mut table = [0xFFu8; 256]; + let mut i = 0usize; + while i < 85 { + table[ALPHABET[i] as usize] = i as u8; + i += 1; + } + table +}; + +/// Error type for Base85 operations. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Base85Error { + /// Invalid character that is not in RFC 1924 alphabet. + InvalidCharacter(char), + /// Invalid input length for the operation. + InvalidLength, +} + +impl fmt::Display for Base85Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Base85Error::InvalidCharacter(c) => write!(f, "invalid base85 character: {:?}", c), + Base85Error::InvalidLength => write!(f, "invalid input length"), + } + } +} + +impl std::error::Error for Base85Error {} + +/// Decodes a Base85 string to the provided output. +/// +/// ## Limitations +/// +/// The input length must be a multiple of 5. +/// +/// This function does not handle padding for partial chunks. +/// When decoding data where the original byte count isn't a multiple of 4, +/// callers must handle truncation at a higher level. +/// For example, via a length indicator in Git binary patch. +pub fn decode_into(input: &str, output: &mut impl Extend) -> Result<(), Base85Error> { + let bytes = input.as_bytes(); + + if bytes.len() % 5 != 0 { + return Err(Base85Error::InvalidLength); + } + + // TODO: Use `as_chunks::<5>()` when MSRV >= 1.88 + for chunk in bytes.chunks_exact(5) { + let mut value: u32 = 0; + for &byte in chunk { + let digit = TABLE[byte as usize]; + if digit == 0xFF { + return Err(Base85Error::InvalidCharacter(byte as char)); + } + value = value * 85 + digit as u32; + } + + output.extend(value.to_be_bytes()); + } + + Ok(()) +} + +/// Encodes bytes in Base85 to the provided output. +/// +/// ## Limitations +/// +/// The input length must be a multiple of 4. +/// +/// This function does not handle padding for partial chunks. +/// Callers encoding data where the byte count isn't a multiple of 4 +/// must handle padding at a higher level. +/// For example, via a length indicator in Git binary patch format. +#[allow(dead_code)] // will be used for patch formatting +pub fn encode_into(input: &[u8], output: &mut impl Extend) -> Result<(), Base85Error> { + if input.len() % 4 != 0 { + return Err(Base85Error::InvalidLength); + } + + // TODO: Use `as_chunks::<4>()` when MSRV >= 1.88 + for chunk in input.chunks_exact(4) { + let mut value = u32::from_be_bytes(chunk.try_into().unwrap()); + + // Extract 5 base85 digits (least to most significant order) + let mut digits = [0u8; 5]; + for digit in digits.iter_mut().rev() { + *digit = ALPHABET[(value % 85) as usize]; + value /= 85; + } + output.extend(digits.iter().map(|&b| b as char)); + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn decode(input: &str) -> Result, Base85Error> { + let mut result = Vec::with_capacity((input.len() / 5) * 4); + decode_into(input, &mut result)?; + Ok(result) + } + + fn encode(input: &[u8]) -> Result { + let mut result = String::with_capacity((input.len() / 4) * 5); + encode_into(input, &mut result)?; + Ok(result) + } + + const TEST_VECTORS: &[(&[u8], &str)] = &[ + (b"", ""), + (&[0x00, 0x00, 0x00, 0x00], "00000"), + (&[0xff, 0xff, 0xff, 0xff], "|NsC0"), + // Rust ecosystem phrases + (b"Rust", "Qgw55"), + (b"Fearless concurrency", "MrC1gY-MwEAY*TCV|8+JWo~16"), + (b"memory safe!", "ZDnn5a(N(gVP<6^"), + (b"blazing fast", "Vr*f0X>MmAW?^%5"), + ( + b"zero-cost abstraction!??", + "dS!BNEn{zUbRc13b98cHV{~b6ZXrKE", + ), + ]; + + #[test] + fn table_covers_all_alphabet_chars() { + for (i, &c) in ALPHABET.iter().enumerate() { + assert_eq!( + TABLE[c as usize], i as u8, + "mismatch for char '{}' at index {}", + c as char, i + ); + } + } + + #[test] + fn table_rejects_invalid_chars() { + let invalid_chars = b" \t\n\r\"'\\[],:"; + for &c in invalid_chars { + assert_eq!( + TABLE[c as usize], 0xFF, + "char '{}' should be invalid", + c as char + ); + } + } + + #[test] + fn decode_test_vectors() { + for (bytes, encoded) in TEST_VECTORS { + let result = decode(encoded).unwrap(); + assert_eq!(&result, *bytes, "decode({:?}) failed", encoded); + } + } + + #[test] + fn encode_test_vectors() { + for (bytes, encoded) in TEST_VECTORS { + let result = encode(bytes).unwrap(); + assert_eq!(result, *encoded, "encode({:?}) failed", bytes); + } + } + + #[test] + fn decode_invalid_length() { + assert!(matches!(decode("0000"), Err(Base85Error::InvalidLength))); + assert!(matches!(decode("000"), Err(Base85Error::InvalidLength))); + assert!(matches!(decode("00"), Err(Base85Error::InvalidLength))); + assert!(matches!(decode("0"), Err(Base85Error::InvalidLength))); + } + + #[test] + fn decode_invalid_character() { + assert!(matches!( + decode("0000 "), + Err(Base85Error::InvalidCharacter(' ')) + )); + assert!(matches!( + decode("0000\""), + Err(Base85Error::InvalidCharacter('"')) + )); + } + + #[test] + fn encode_invalid_length() { + assert!(matches!(encode(&[0]), Err(Base85Error::InvalidLength))); + assert!(matches!(encode(&[0, 0]), Err(Base85Error::InvalidLength))); + assert!(matches!( + encode(&[0, 0, 0]), + Err(Base85Error::InvalidLength) + )); + assert!(matches!( + encode(&[0, 0, 0, 0, 0]), + Err(Base85Error::InvalidLength) + )); + } + + #[test] + fn round_trip() { + for (bytes, _) in TEST_VECTORS { + let encoded = encode(bytes).unwrap(); + let decoded = decode(&encoded).unwrap(); + assert_eq!(&decoded, *bytes, "round-trip failed for {:?}", bytes); + } + } +} diff --git a/src/binary/delta.rs b/src/binary/delta.rs new file mode 100644 index 00000000..fa1de268 --- /dev/null +++ b/src/binary/delta.rs @@ -0,0 +1,362 @@ +//! Git delta binary diff support. +//! +//! A delta payload contains: +//! +//! 1. Header: variable-length encoded sizes (original_size, modified_size) +//! 2. Instructions: sequence of `ADD` and `COPY` operations +//! +//! Based on Diffx's [Git Delta Binary Diffs](https://diffx.org/spec/binary-diffs.html#git-delta-binary-diffs) + +use std::fmt; + +/// Applies delta instructions to an original file, producing the modified file. +pub fn apply(original: &[u8], delta: &[u8]) -> Result, DeltaError> { + let mut cursor = DeltaCursor::new(delta); + + let header_orig_size = cursor.read_size()?; + let header_mod_size = cursor.read_size()?; + + // Validate original size + if original.len() as u64 != header_orig_size { + return Err(DeltaError::OriginalSizeMismatch { + expected: header_orig_size, + actual: original.len() as u64, + }); + } + + let mut result = Vec::with_capacity(header_mod_size as usize); + + // Process instructions until we've consumed all delta data + while !cursor.is_empty() { + let control = cursor.read_byte()?; + + if control & 0x80 != 0 { + // COPY instruction + let (src_offset, copy_len) = cursor.read_copy_params(control)?; + let src_end = src_offset + .checked_add(copy_len) + .ok_or(DeltaError::InvalidCopyRange)?; + + if src_end > original.len() { + return Err(DeltaError::CopyOutOfBounds { + offset: src_offset, + length: copy_len, + original_size: original.len(), + }); + } + + result.extend_from_slice(&original[src_offset..src_end]); + } else { + // ADD instruction + let add_len = control as usize; + let data = cursor.read_bytes(add_len)?; + result.extend_from_slice(data); + } + } + + // Validate result size + if result.len() as u64 != header_mod_size { + return Err(DeltaError::ModifiedSizeMismatch { + expected: header_mod_size, + actual: result.len() as u64, + }); + } + + Ok(result) +} + +/// Cursor for reading delta instructions. +struct DeltaCursor<'a> { + data: &'a [u8], + offset: usize, +} + +impl<'a> DeltaCursor<'a> { + fn new(data: &'a [u8]) -> Self { + Self { data, offset: 0 } + } + + fn is_empty(&self) -> bool { + self.offset >= self.data.len() + } + + fn read_byte(&mut self) -> Result { + if self.offset >= self.data.len() { + return Err(DeltaError::UnexpectedEof); + } + let byte = self.data[self.offset]; + self.offset += 1; + Ok(byte) + } + + fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], DeltaError> { + let end = self + .offset + .checked_add(len) + .ok_or(DeltaError::UnexpectedEof)?; + if end > self.data.len() { + return Err(DeltaError::UnexpectedEof); + } + let bytes = &self.data[self.offset..end]; + self.offset = end; + Ok(bytes) + } + + /// Reads a variable-length encoded size from the header. + /// + /// Format: each byte uses 7 bits for value, MSB indicates continuation. + /// Bytes are in little-endian order (LSB first). + fn read_size(&mut self) -> Result { + let mut file_len: u64 = 0; + let mut shift: u32 = 0; + + loop { + let byte = self.read_byte()?; + + // Add 7 bits of value at current shift position + let value = (byte & 0x7F) as u64; + file_len |= value.checked_shl(shift).ok_or(DeltaError::SizeOverflow)?; + + // MSB clear means this is the last byte + if byte & 0x80 == 0 { + break; + } + + shift += 7; + } + + Ok(file_len) + } + + /// Reads `COPY` instruction parameters from the control byte. + /// Returns `(src_offset, copy_len)`. + /// + /// Control byte format is `1oooosss`: + /// + /// * Bits 0-3: src_offset bytes + /// * Bits 4-6: copy_len bytes + fn read_copy_params(&mut self, control: u8) -> Result<(usize, usize), DeltaError> { + let mut src_offset: u32 = 0; + for (mask, shift) in [(0x01, 0), (0x02, 8), (0x04, 16), (0x08, 24)] { + if control & mask != 0 { + let byte = self.read_byte()? as u32; + src_offset |= byte.checked_shl(shift).ok_or(DeltaError::SizeOverflow)?; + } + } + + let mut copy_len: u32 = 0; + for (mask, shift) in [(0x10, 0), (0x20, 8), (0x40, 16)] { + if control & mask != 0 { + let byte = self.read_byte()? as u32; + copy_len |= byte.checked_shl(shift).ok_or(DeltaError::SizeOverflow)?; + } + } + + if copy_len == 0 { + // Size of 0 means 65536 + copy_len = 0x10000; + } + + Ok((src_offset as usize, copy_len as usize)) + } +} + +/// Error type for delta operations. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum DeltaError { + /// Unexpected end of delta data. + UnexpectedEof, + /// Size value overflowed during decoding. + SizeOverflow, + /// Original file size doesn't match header. + OriginalSizeMismatch { expected: u64, actual: u64 }, + /// Modified file size doesn't match header. + ModifiedSizeMismatch { expected: u64, actual: u64 }, + /// COPY instruction references out-of-bounds data. + CopyOutOfBounds { + offset: usize, + length: usize, + original_size: usize, + }, + /// COPY range calculation overflowed. + InvalidCopyRange, +} + +impl fmt::Display for DeltaError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DeltaError::UnexpectedEof => write!(f, "unexpected end of delta data"), + DeltaError::SizeOverflow => write!(f, "size value overflow"), + DeltaError::OriginalSizeMismatch { expected, actual } => { + write!( + f, + "original size mismatch: expected {expected}, got {actual}" + ) + } + DeltaError::ModifiedSizeMismatch { expected, actual } => { + write!( + f, + "modified size mismatch: expected {expected}, got {actual}" + ) + } + DeltaError::CopyOutOfBounds { + offset, + length, + original_size, + } => { + write!( + f, + "copy out of bounds: offset={offset}, length={length}, original_size={original_size}" + ) + } + DeltaError::InvalidCopyRange => write!(f, "copy range calculation overflow"), + } + } +} + +impl std::error::Error for DeltaError {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn read_size_single_byte() { + // 0x0A = 10, MSB clear = end + let data = [0x0A]; + let mut cursor = DeltaCursor::new(&data); + assert_eq!(cursor.read_size().unwrap(), 10); + } + + #[test] + fn read_size_multi_byte() { + // 0x80 | 0x01 = 1, continue; 0x02 = 2 << 7 = 256; total = 257 + let data = [0x81, 0x02]; + let mut cursor = DeltaCursor::new(&data); + assert_eq!(cursor.read_size().unwrap(), 1 + (2 << 7)); + } + + #[test] + fn apply_add_only() { + // Header: orig_size=0, mod_size=5 + // ADD 5 bytes: "hello" + let delta = [ + 0x00, // orig_size = 0 + 0x05, // mod_size = 5 + 0x05, // ADD 5 bytes + b'h', b'e', b'l', b'l', b'o', + ]; + let result = apply(&[], &delta).unwrap(); + assert_eq!(result, b"hello"); + } + + #[test] + fn apply_copy_only() { + // Header: orig_size=5, mod_size=5 + // COPY offset=0, len=5 + let delta = [ + 0x05, // orig_size = 5 + 0x05, // mod_size = 5 + 0x90, // COPY: control=0x90 (0x80 | 0x10), offset=0, size byte present + 0x05, // size = 5 + ]; + let original = b"hello"; + let result = apply(original, &delta).unwrap(); + assert_eq!(result, b"hello"); + } + + #[test] + fn apply_copy_with_offset() { + // Header: orig_size=10, mod_size=5 + // COPY offset=5, len=5 + let delta = [ + 0x0A, // orig_size = 10 + 0x05, // mod_size = 5 + 0x91, // COPY: 0x80 | 0x10 | 0x01 (offset1 + size1 present) + 0x05, // offset = 5 + 0x05, // size = 5 + ]; + let original = b"helloworld"; + let result = apply(original, &delta).unwrap(); + assert_eq!(result, b"world"); + } + + #[test] + fn apply_mixed_instructions() { + // Create "HELLO world" from "hello world" + // Header: orig_size=11, mod_size=11 + // ADD 5: "HELLO" + // COPY offset=5, len=6: " world" + let delta = [ + 0x0B, // orig_size = 11 + 0x0B, // mod_size = 11 + 0x05, // ADD 5 bytes + b'H', b'E', b'L', b'L', b'O', // "HELLO" + 0x91, // COPY: offset1 + size1 present + 0x05, // offset = 5 + 0x06, // size = 6 + ]; + let original = b"hello world"; + let result = apply(original, &delta).unwrap(); + assert_eq!(result, b"HELLO world"); + } + + #[test] + fn apply_copy_size_zero_means_65536() { + // When size bytes result in 0, it means 65536 + // Header: orig_size=65536, mod_size=65536 + // COPY offset=0, len=65536 (encoded as 0) + let original = vec![0xAB; 65536]; + let delta = [ + 0x80, 0x80, 0x04, // orig_size = 65536 (varint) + 0x80, 0x80, 0x04, // mod_size = 65536 (varint) + 0x80, // COPY: no offset bytes, no size bytes = offset 0, size 65536 + ]; + let result = apply(&original, &delta).unwrap(); + assert_eq!(result.len(), 65536); + assert_eq!(result, original); + } + + #[test] + fn error_original_size_mismatch() { + let delta = [ + 0x0A, // orig_size = 10 + 0x05, // mod_size = 5 + ]; + let original = b"short"; // only 5 bytes + let err = apply(original, &delta).unwrap_err(); + assert!(matches!( + err, + DeltaError::OriginalSizeMismatch { + expected: 10, + actual: 5 + } + )); + } + + #[test] + fn error_copy_out_of_bounds() { + let delta = [ + 0x05, // orig_size = 5 + 0x05, // mod_size = 5 + 0x91, // COPY + 0x0A, // offset = 10 (out of bounds!) + 0x05, // size = 5 + ]; + let original = b"hello"; + let err = apply(original, &delta).unwrap_err(); + assert!(matches!(err, DeltaError::CopyOutOfBounds { .. })); + } + + #[test] + fn error_unexpected_eof_in_add() { + let delta = [ + 0x00, // orig_size = 0 + 0x05, // mod_size = 5 + 0x05, // ADD 5 bytes + b'h', b'i', // only 2 bytes provided + ]; + let err = apply(&[], &delta).unwrap_err(); + assert_eq!(err, DeltaError::UnexpectedEof); + } +} diff --git a/src/binary/mod.rs b/src/binary/mod.rs index 7f2db8fc..b5cf1e48 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -5,6 +5,11 @@ //! //! Based on [DiffX Binary Diffs specification](https://diffx.org/spec/binary-diffs.html). +#[cfg(feature = "binary")] +mod base85; +#[cfg(feature = "binary")] +mod delta; + use std::{fmt, ops::Range}; /// The type of a binary patch block. @@ -54,9 +59,69 @@ pub enum BinaryPatch<'a> { /// /// This represents the `Binary files a/path and b/path differ` case, /// where git detected a binary change but didn't include the actual data. + /// + /// Calling [`apply()`](Self::apply) on this variant returns an error. Marker, } +impl<'a> BinaryPatch<'a> { + /// Applies a binary patch forward: original -> modified. + /// + /// - If the forward block is `Literal`: returns the decoded content directly. + /// - If the forward block is `Delta`: applies delta instructions to `original`. + /// + /// Unlike `git apply`, this doesn't validate the original content hash. + #[cfg(feature = "binary")] + pub fn apply(&self, original: &[u8]) -> Result, BinaryPatchParseError> { + match self { + BinaryPatch::Full { forward, .. } => Self::apply_block(forward, original), + BinaryPatch::Marker => Err(BinaryPatchParseErrorKind::NoBinaryData.into()), + } + } + + /// Applies a binary patch in reverse: modified -> original. + /// + /// - If the reverse block is `Literal`: returns the decoded content directly. + /// - If the reverse block is `Delta`: applies delta instructions to `modified`. + /// + /// Unlike `git apply`, this doesn't validate the modified content hash. + #[cfg(feature = "binary")] + pub fn apply_reverse(&self, modified: &[u8]) -> Result, BinaryPatchParseError> { + match self { + BinaryPatch::Full { reverse, .. } => Self::apply_block(reverse, modified), + BinaryPatch::Marker => Err(BinaryPatchParseErrorKind::NoBinaryData.into()), + } + } + + /// Applies a single block (either literal or delta). + #[cfg(feature = "binary")] + fn apply_block(block: &BinaryBlock<'_>, base: &[u8]) -> Result, BinaryPatchParseError> { + match block.kind { + BinaryBlockKind::Literal => Self::decode_data(&block.data), + BinaryBlockKind::Delta => { + let delta_instructions = Self::decode_data(&block.data)?; + delta::apply(base, &delta_instructions).map_err(BinaryPatchParseError::from) + } + } + } + + /// See [Decoding Logic](https://diffx.org/spec/binary-diffs.html#decoding-logic) + #[cfg(feature = "binary")] + fn decode_data(binary_data: &BinaryData<'_>) -> Result, BinaryPatchParseError> { + use std::io::Read; + + let compressed = decode_base85_lines(binary_data.data)?; + + let mut decoder = flate2::read::ZlibDecoder::new(&compressed[..]); + let mut decompressed = Vec::new(); + decoder + .read_to_end(&mut decompressed) + .map_err(|e| BinaryPatchParseErrorKind::DecompressionFailed(e.to_string()))?; + + Ok(decompressed) + } +} + /// Represents a single binary payload in a Git binary diff. /// /// For example, the following patch block @@ -115,6 +180,20 @@ impl fmt::Display for BinaryPatchParseError { impl std::error::Error for BinaryPatchParseError {} +#[cfg(feature = "binary")] +impl From for BinaryPatchParseError { + fn from(e: base85::Base85Error) -> Self { + BinaryPatchParseErrorKind::Base85(e).into() + } +} + +#[cfg(feature = "binary")] +impl From for BinaryPatchParseError { + fn from(e: delta::DeltaError) -> Self { + BinaryPatchParseErrorKind::Delta(e).into() + } +} + impl From for BinaryPatchParseError { fn from(kind: BinaryPatchParseErrorKind) -> Self { Self { kind, span: None } @@ -126,6 +205,8 @@ impl From for BinaryPatchParseError { #[non_exhaustive] pub(crate) enum BinaryPatchParseErrorKind { /// Missing or invalid "GIT binary patch" header. + // TODO: Switch to #[expect(dead_code)] when MSRV >= 1.81 + #[cfg_attr(not(feature = "binary"), allow(dead_code))] InvalidHeader, /// First binary block (forward) not found. @@ -135,10 +216,26 @@ pub(crate) enum BinaryPatchParseErrorKind { MissingReverseBlock, /// No binary data available (marker-only patch). + // TODO: Switch to #[expect(dead_code)] when MSRV >= 1.81 + #[cfg_attr(not(feature = "binary"), allow(dead_code))] NoBinaryData, /// Invalid line length indicator in Base85 data. + // TODO: Switch to #[expect(dead_code)] when MSRV >= 1.81 + #[cfg_attr(not(feature = "binary"), allow(dead_code))] InvalidLineLengthIndicator, + + /// Base85 decoding failed. + #[cfg(feature = "binary")] + Base85(base85::Base85Error), + + /// Delta application failed. + #[cfg(feature = "binary")] + Delta(delta::DeltaError), + + /// Zlib decompression failed. + #[cfg(feature = "binary")] + DecompressionFailed(String), } impl fmt::Display for BinaryPatchParseErrorKind { @@ -149,6 +246,12 @@ impl fmt::Display for BinaryPatchParseErrorKind { Self::MissingReverseBlock => write!(f, "second binary block not found"), Self::NoBinaryData => write!(f, "no binary data available"), Self::InvalidLineLengthIndicator => write!(f, "invalid line length indicator"), + #[cfg(feature = "binary")] + Self::Base85(e) => write!(f, "{e}"), + #[cfg(feature = "binary")] + Self::Delta(e) => write!(f, "{e}"), + #[cfg(feature = "binary")] + Self::DecompressionFailed(msg) => write!(f, "decompression failed: {msg}"), } } } @@ -259,6 +362,62 @@ fn parse_binary_block<'a>(parser: &mut BinaryParser<'a>) -> Option` +/// +/// From [5.1.1. Binary Payloads](https://diffx.org/spec/binary-diffs.html#binary-payloads): +/// +/// > Each line represents up to 52 bytes of pre-encoded data. +/// > There may be an unlimited number of lines. +/// > They contain the following fields: +/// > +/// > * `len_c` is a line length character. +/// > This encodes the length of the (pre-encoded) data written on this line. +/// > * `data` is Base85-encoded data for this line. +#[cfg(feature = "binary")] +fn decode_base85_lines(data: &str) -> Result, BinaryPatchParseError> { + // A rough estimate: In Base85, 5 chars -> 4 bytes + let mut result = Vec::with_capacity(data.len() * 4 / 5); + + for line in data.lines() { + if line.is_empty() { + continue; + } + + let line_bytes = line.as_bytes(); + + let length = decode_line_length(line_bytes[0]) + .ok_or(BinaryPatchParseErrorKind::InvalidLineLengthIndicator)?; + let encoded = &line[1..]; + let start = result.len(); + base85::decode_into(encoded, &mut result)?; + result.truncate(start + length); + } + + Ok(result) +} + +/// Decodes a line length character to its numeric value. +/// +/// From [Line Length Characters](https://diffx.org/spec/binary-diffs.html#line-length-characters): +/// +/// > Each encoded line in a binary diff payload is prefixed by a line length character. +/// > This encodes the length of the compressed (but not encoded) data for the line. +/// > +/// > Line length characters always represent a value between 1 and 52: +/// > +/// > * A value of A-Z represents a number between 1..26. +/// > * A value of a-z represents a number between 27..52. +#[cfg(feature = "binary")] +fn decode_line_length(c: u8) -> Option { + match c { + b'A'..=b'Z' => Some((c - b'A' + 1) as usize), + b'a'..=b'z' => Some((c - b'a' + 27) as usize), + _ => None, + } +} + #[cfg(test)] mod tests { use super::*; @@ -336,3 +495,54 @@ mod tests { } } } + +#[cfg(test)] +#[cfg(feature = "binary")] +mod apply_tests { + use super::*; + + #[test] + fn decode_line_length_uppercase() { + assert_eq!(decode_line_length(b'A'), Some(1)); + assert_eq!(decode_line_length(b'B'), Some(2)); + assert_eq!(decode_line_length(b'Z'), Some(26)); + } + + #[test] + fn decode_line_length_lowercase() { + assert_eq!(decode_line_length(b'a'), Some(27)); + assert_eq!(decode_line_length(b'b'), Some(28)); + assert_eq!(decode_line_length(b'z'), Some(52)); + } + + #[test] + fn decode_line_length_invalid() { + assert_eq!(decode_line_length(b'0'), None); + assert_eq!(decode_line_length(b'!'), None); + assert_eq!(decode_line_length(b' '), None); + } + + #[test] + fn apply_literal_patch() { + let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let (patch, _) = parse_binary_patch(input).unwrap(); + + let modified = patch.apply(&[]).unwrap(); + assert_eq!(modified.len(), 10); + assert_eq!(modified, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + + let original = patch.apply_reverse(&[]).unwrap(); + assert_eq!(original.len(), 0); + } + + #[test] + fn apply_with_crlf_line_endings() { + let input = "GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; + let (patch, _) = parse_binary_patch(input).unwrap(); + + let modified = patch.apply(&[]).unwrap(); + assert_eq!(modified, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + let original = patch.apply_reverse(&[]).unwrap(); + assert_eq!(original.len(), 0); + } +} From d43d756e0d2bf28c9b9095e1d0f19ec5aa801f7f Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:23:02 -0400 Subject: [PATCH 07/14] test(compat): binary patch tests Now compat tests require `binary` Cargo feature. --- .github/workflows/ci.yml | 1 + Cargo.toml | 4 + tests/compat/common.rs | 45 +++++---- .../git/binary_and_text_mixed/in/foo.patch | 16 ++++ .../git/binary_and_text_mixed/in/image.png | 2 + .../git/binary_and_text_mixed/in/text.txt | 1 + .../git/binary_and_text_mixed/out/image.png | Bin 0 -> 10 bytes .../git/binary_and_text_mixed/out/text.txt | 1 + tests/compat/git/binary_delta/in/foo.patch | 9 ++ tests/compat/git/binary_delta/in/large.bin | Bin 0 -> 5120 bytes tests/compat/git/binary_delta/out/large.bin | Bin 0 -> 5120 bytes .../binary_delta_wrong_original/in/foo.patch | 9 ++ .../binary_delta_wrong_original/in/large.bin | 1 + tests/compat/git/binary_literal/in/foo.patch | 10 ++ tests/compat/git/binary_literal/out/small.bin | Bin 0 -> 10 bytes .../binary_literal_wrong_original/in/file.bin | 1 + .../in/foo.patch | 8 ++ .../out/file.bin | Bin 0 -> 10 bytes .../binary_mixed_delta_literal/in/favicon.png | Bin 0 -> 2919 bytes .../binary_mixed_delta_literal/in/foo.patch | 86 ++++++++++++++++++ .../out/favicon.png | Bin 0 -> 1125 bytes tests/compat/git/mod.rs | 67 +++++++++++++- tests/compat/main.rs | 10 +- 23 files changed, 248 insertions(+), 23 deletions(-) create mode 100644 tests/compat/git/binary_and_text_mixed/in/foo.patch create mode 100644 tests/compat/git/binary_and_text_mixed/in/image.png create mode 100644 tests/compat/git/binary_and_text_mixed/in/text.txt create mode 100644 tests/compat/git/binary_and_text_mixed/out/image.png create mode 100644 tests/compat/git/binary_and_text_mixed/out/text.txt create mode 100644 tests/compat/git/binary_delta/in/foo.patch create mode 100644 tests/compat/git/binary_delta/in/large.bin create mode 100644 tests/compat/git/binary_delta/out/large.bin create mode 100644 tests/compat/git/binary_delta_wrong_original/in/foo.patch create mode 100644 tests/compat/git/binary_delta_wrong_original/in/large.bin create mode 100644 tests/compat/git/binary_literal/in/foo.patch create mode 100644 tests/compat/git/binary_literal/out/small.bin create mode 100644 tests/compat/git/binary_literal_wrong_original/in/file.bin create mode 100644 tests/compat/git/binary_literal_wrong_original/in/foo.patch create mode 100644 tests/compat/git/binary_literal_wrong_original/out/file.bin create mode 100644 tests/compat/git/binary_mixed_delta_literal/in/favicon.png create mode 100644 tests/compat/git/binary_mixed_delta_literal/in/foo.patch create mode 100644 tests/compat/git/binary_mixed_delta_literal/out/favicon.png diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0ca9b80..481f8788 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: - run: rustup toolchain install ${{ matrix.rust }} --profile minimal - run: cargo +${{ matrix.rust }} check --all-targets --all-features - run: cargo +${{ matrix.rust }} test + - run: cargo +${{ matrix.rust }} test -F binary lint: runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 68ce3ff3..42637f69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,7 @@ snapbox = { version = "0.6.24", features = ["dir"] } [[example]] name = "patch_formatter" required-features = ["color"] + +[[test]] +name = "compat" +required-features = ["binary"] diff --git a/tests/compat/common.rs b/tests/compat/common.rs index 6e871b08..e8c3b12e 100644 --- a/tests/compat/common.rs +++ b/tests/compat/common.rs @@ -9,7 +9,7 @@ use std::{ }; use diffy::{ - binary::BinaryPatch, + binary::{BinaryPatch, BinaryPatchParseError}, patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet, PatchSetParseError}, }; @@ -298,7 +298,7 @@ fn print_patch_version() { pub enum TestError { Parse(PatchSetParseError), Apply(diffy::ApplyError), - Io(std::io::Error), + Binary(BinaryPatchParseError), } impl std::fmt::Display for TestError { @@ -306,7 +306,7 @@ impl std::fmt::Display for TestError { match self { TestError::Parse(e) => write!(f, "parse error: {e}"), TestError::Apply(e) => write!(f, "apply error: {e}"), - TestError::Io(e) => write!(f, "io error: {e}"), + TestError::Binary(e) => write!(f, "binary patch error: {e}"), } } } @@ -397,29 +397,40 @@ pub fn apply_diffy( } }; + let read_original = || { + if let Some(name) = original_name { + let original_path = in_dir.join(bytes_to_path(name)); + fs::read(&original_path).unwrap_or_default() + } else { + Vec::new() + } + }; + + let write_modified = |result: &[u8]| { + let result_path = output_dir.join(bytes_to_path(target_name)); + if let Some(parent) = result_path.parent() { + fs::create_dir_all(parent).unwrap(); + } + fs::write(&result_path, result).unwrap(); + }; + match file_patch.patch() { PatchKind::Text(patch) => { - let original = if let Some(name) = original_name { - let original_path = in_dir.join(bytes_to_path(name)); - fs::read(&original_path).map_err(TestError::Io)? - } else { - Vec::new() - }; + let original = read_original(); let result = diffy::apply_bytes(&original, patch).map_err(TestError::Apply)?; - let result_path = output_dir.join(bytes_to_path(target_name)); - if let Some(parent) = result_path.parent() { - fs::create_dir_all(parent).unwrap(); - } - fs::write(&result_path, &result).unwrap(); + write_modified(&result); } PatchKind::Binary(BinaryPatch::Marker) => { // Dont do anything if it is just a binary patch marker. } - PatchKind::Binary(_) => { - // Binary patch application requires the `binary` feature. - // Will be wired up when that feature is added. + PatchKind::Binary(patch) => { + let original = read_original(); + + let result = patch.apply(&original).map_err(TestError::Binary)?; + + write_modified(&result); } } } diff --git a/tests/compat/git/binary_and_text_mixed/in/foo.patch b/tests/compat/git/binary_and_text_mixed/in/foo.patch new file mode 100644 index 00000000..9ae73cb9 --- /dev/null +++ b/tests/compat/git/binary_and_text_mixed/in/foo.patch @@ -0,0 +1,16 @@ +diff --git a/image.png b/image.png +index 0dd1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+_E~=Q8w3gLUqfCZbIdPhFd>$87JNxB|Mv5o8Iq4rO z9{uM_RA*6!3I`Jw_kWtMLW>*hhC1-O$QaYTtBy~k{^0kqyjLZJ=_1q2wItMCx} zA3EIUBEw)qU>R4wg)aJ(GDeCshXQr`T%IE;=yrYok~UNT7Y^&+;R)bP`biKpV*weBQk2c(_sD5lif zC2MKzR~%}yh(Grk3JHW^Bk%D1o(^+!=E_3 zn7S^}Y^)$5e_mFPiD=~qdIV{c+dm@#R?|PY@9c&yVusFF)+GyXOdnftGM0VqAdKHU z5=+q3I^puAIyw-fn~`2DB7+5h`c{#<6w!xMi7&_BBDdz%F=EZjamcz!Z0B94s#7y8 z>wTX>=!?n)1WQYVq*-SbF`@#jNv32D)}VP8CLc6XQRI~w>GJb}452Hpgu&<5o8*VT zi<$l^&c`DH03?v~g)O7Wv_2Ow$-or2_zy4@#|jLBaf1sQep142KTO`*v_x z$N=RsAzD5zwawueOGUJ?3jT}z<03j8 iFeF}$w^8qC1SDUCXp$k{m_!>&X-}m2V55dcq+r238RXvp literal 0 HcmV?d00001 diff --git a/tests/compat/git/binary_delta/out/large.bin b/tests/compat/git/binary_delta/out/large.bin new file mode 100644 index 0000000000000000000000000000000000000000..a8a025a84f32c7552841b683361d03d3e171294d GIT binary patch literal 5120 zcmV+b6#whcY*DIPbx_e&SH6(E=m?lE#2}*s*|N=_Pn6rew1;C{e>&C2V_V*#WK--S zBVwa$Qc>CVe~2gp%FIc0GITmkOWRdRQEMmVnHk@)n0}jy)YRZ2E>S4v)yQrWOqy%C zdgG!0|Ns9^f2Us8A}W>x>EtwIF$OTi6@1sv;(4 zSerdbxr+=xZ*CHL1$$Y~T$p4Ql#onXq!jP!-SBRm?3{^{)&p}f(rnh>tgKb z0#OkH>rALj017{~{J53G%CxL`Dy*DE6{&tSlxro?3Fc|vT#w|asfzO_Ul0Z*R^kdE#G>fnm!7AlT4LsQiQA1JQJ$~VH7UW}xT0Ar zR{}HtO#4SxnK5{FQyL3u3R?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XQRI~w>GJb}452Hpgu&<5o8*VT zi<$l^&c`DH03?v~g)O7Wv_2Ow$-or2_zy4@#|jLBaf1sQep142KTO`*v_x z$N=RsAzD5zwawueOGUJ?3jT}z<03j8 iFeF}$w^8qC1SDUCXp$k{m_!>&X-}m2V55dcq+r3GG3TWK literal 0 HcmV?d00001 diff --git a/tests/compat/git/binary_delta_wrong_original/in/foo.patch b/tests/compat/git/binary_delta_wrong_original/in/foo.patch new file mode 100644 index 00000000..9abf334b --- /dev/null +++ b/tests/compat/git/binary_delta_wrong_original/in/foo.patch @@ -0,0 +1,9 @@ +diff --git a/large.bin b/large.bin +index ffba9ca51637158e8f46f0a2a9014372778eeef4..a8a025a84f32c7552841b683361d03d3e171294d 100644 +GIT binary patch +delta 15 +ZcmV+q0Pz2SD1a!CWCZ{J|NpUQm=imI2nhfH + +delta 15 +ZcmV+q0Pz2SD1a!CWCQ_9%OJ66m=h@q1w#M; + diff --git a/tests/compat/git/binary_delta_wrong_original/in/large.bin b/tests/compat/git/binary_delta_wrong_original/in/large.bin new file mode 100644 index 00000000..0d6307ba --- /dev/null +++ b/tests/compat/git/binary_delta_wrong_original/in/large.bin @@ -0,0 +1 @@ +WRONG CONTENT \ No newline at end of file diff --git a/tests/compat/git/binary_literal/in/foo.patch b/tests/compat/git/binary_literal/in/foo.patch new file mode 100644 index 00000000..61ea8bfc --- /dev/null +++ b/tests/compat/git/binary_literal/in/foo.patch @@ -0,0 +1,10 @@ +diff --git a/small.bin b/small.bin +new file mode 100644 +index 0000000000000000000000000000000000000000..df93f5f3f72487244976c34e85525cf445016566 +GIT binary patch +literal 10 +UcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k + +literal 0 +KcmV+b0RR6000031 + diff --git a/tests/compat/git/binary_literal/out/small.bin b/tests/compat/git/binary_literal/out/small.bin new file mode 100644 index 0000000000000000000000000000000000000000..df93f5f3f72487244976c34e85525cf445016566 GIT binary patch literal 10 RcmZQzWMXDvWn<^y1ONc904@Lk literal 0 HcmV?d00001 diff --git a/tests/compat/git/binary_literal_wrong_original/in/file.bin b/tests/compat/git/binary_literal_wrong_original/in/file.bin new file mode 100644 index 00000000..605cc407 --- /dev/null +++ b/tests/compat/git/binary_literal_wrong_original/in/file.bin @@ -0,0 +1 @@ +WRONG CONTENT - not matching the hash in patch \ No newline at end of file diff --git a/tests/compat/git/binary_literal_wrong_original/in/foo.patch b/tests/compat/git/binary_literal_wrong_original/in/foo.patch new file mode 100644 index 00000000..d3e39d97 --- /dev/null +++ b/tests/compat/git/binary_literal_wrong_original/in/foo.patch @@ -0,0 +1,8 @@ +diff --git a/file.bin b/file.bin +index df93f5f3f72487244976c34e85525cf445016566..a8a025a84f32c7552841b683361d03d3e171294d 100644 +GIT binary patch +literal 10 +UcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k + +literal 10 +UcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k diff --git a/tests/compat/git/binary_literal_wrong_original/out/file.bin b/tests/compat/git/binary_literal_wrong_original/out/file.bin new file mode 100644 index 0000000000000000000000000000000000000000..df93f5f3f72487244976c34e85525cf445016566 GIT binary patch literal 10 RcmZQzWMXDvWn<^y1ONc904@Lk literal 0 HcmV?d00001 diff --git a/tests/compat/git/binary_mixed_delta_literal/in/favicon.png b/tests/compat/git/binary_mixed_delta_literal/in/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..5109c1de8bea744180448b347fb44ddef23b90f4 GIT binary patch literal 2919 zcmai030P8T7skEJu&Hdcv?0>65vtoO=gysTc){dn_8A;mgY3Mwv7J;8=I#3|L3_2=X>9CzO#MjlI-o}Hd}k4HUfc| zO?4;xz;Be|o}mdpp_rIq1VX(S^!1nc(>w`mA>W+E5rzWha=r-05eTA#T*P9B0}@my z5C#fJ=%K5nXcWjHq5Z6AIGV@>;DPQjVt^jw<;#uoG@8VBUyC+h;}WB70)unGigVUEN8lP&?d-=HgyFX$qkP$GnQLeX>ppKUP> zf1wDc@9QdZmgs9_8ts46`TWnqfFzVCI1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/in/foo.patch b/tests/compat/git/binary_mixed_delta_literal/in/foo.patch new file mode 100644 index 00000000..7f52479d --- /dev/null +++ b/tests/compat/git/binary_mixed_delta_literal/in/foo.patch @@ -0,0 +1,86 @@ +diff --git a/favicon.png b/favicon.png +index 5109c1de8bea744180448b347fb44ddef23b90f4..69b8613ce1506e1c92b864f91cc46df06348c62b 100644 +GIT binary patch +delta 1115 +zcmV-h1f=`t7Uc+#8Gi%-007x@vVQ;o00d`2O+f$vv5yP +zfP?@5`Tzg`fam}Kbua(`>RI+y?e7jT@qQ9J+u00Lr5M??VshmXv^00009a7bBm +z000DZ000DZ0m8#+P5=M^2XskIMF-{q4Gj?pmKmEP0008eNq +zS*l<~QA$1JAY?<@4F!`%H{0#bjE9}fZuU?Ayt&Np_x{ZHeczk+=q00aIO{BOMm>NW +zGb^R2YFbUrw129ilv&5_?R*#op%sCs1zv6w<_>da(*43C8z5GS?N}X +z{^V`QxQx)j*!`A8b;E|$ExHA=4hUt88|nlQ7tEk9hz=YqEbOPg@0Wz`s +zmN!~(9dV=u?I~Mvh)g?mp_)jf?D@Nr{DQJb>GW45GWc;+ig_8yt0(H +zchI)mFn`!(GAY|t|C#flvV9n{2WCVfKibO<1Qi=LY`EQ$z;UqGqDW*0&cq!s6My4; +zPXgEE5_G3UA~k24J=gwQb;>^P#8wX*Y*kE)MBeQ{FfT7}!Y4_A1FwN^c9)O_GgkHj~RdK~NC^L5i~IUXh_ +z95o@^6-U;DqjIVuk>ys^VPVp56?GT72DqTE5?dt$r77DZ?T}ZyX3UaF&vYMYG>7U( +z=6{7=Hn2lG;1!X1AGZ(HW&;lyamBia;`62jOIGoOLtacMykp3iKdiS0Xh_|vJ_mY~ +zb4{&%v9!zfYAbcDwo>=i)=iqrbxKXm)PpS7rrhUx{~ymQhCvo@p6LJp03~!qSaf7z +zbY(hYa%Ew3WdJfTGBPbNF)cAOR53F;F@HBYFgPnPFgh?WYdBZ^0000bbVXQnWMOn= +zI&E)cX=Zrz>%8FWQ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9Z5{cG8F|O!TM*a<%mH87Q8-w?emyNME%f#E;q&>pbLS2d6B8Uec8uxiX~xIL +zQ^j*gMBf4I1foDBQ{-RT#+^tE!_dKl2X*u2P0i2GOOo#2zpwoKe3h1#>dBKQO5*Y3 +z$J)PtzYN20XJZcd;!TDP;60!s^KAyZ*Q-sfow1`GNR3!H+vQ6&eW(=@9S?8tp-3K5WupmOjqW>G);Ew*g<1s +zBYXDj!LlsE;V=~y6+C_VG%5Dd&U7sG>@MJY7O&;n&z@CjX{nlx}S9Ri@dyqq&<&d}A>#oXN73Y%R7yqlE$CFylTLxTnf2c5{gGLQ`& +zIdVj?Sj^FL>((vR)YPQPbR>3Yr1gZLaJO0X!YUx +zE2XA(xfmWEroMjRXwcu^&%nTdThy`bTtRblbH)s`x3?=C4l5iEYiMXl`}Xbg%tR;@ +za`aSJSG)Pn6*Q3;CIo2j-n|(eaCbp88dY&|ahhCHQyXdqOH;gI!vu@%7>Cz=h +zQb$L}QqQgg@YxQ01I)WCWGUFadpE0Bug+F{9%uzf{uF)%UIQ&ji9jGgVPRoLO-Z9` +z)~w<3<;$EteVWP1$ptcFV`D_4(bP{r0>8O+O)YsRi&dyFfd-g1? +zuNTAk__(fKy{d_c3EjJQPpej~N=+C +z_VnHZ85ff*DC1)CCKx34yZ>!9ONDzmeMRQwwANG8TfpZmT+dtW=Vh(u{{qBcL;Juw +R3Jw4O002ovPDHLkV1icrt||Zk + diff --git a/tests/compat/git/binary_mixed_delta_literal/out/favicon.png b/tests/compat/git/binary_mixed_delta_literal/out/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..69b8613ce1506e1c92b864f91cc46df06348c62b GIT binary patch literal 1125 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE0wix1Z>k4UEa{HEjtmSN`?>!lvVtU&J%W50 z7^>757#dm_7=8hT8eT9klo~KFyh>nTu$sZZAYL$MSD+10f+@+{-G$+Qd;gjJKptm- zM`SV3z!DH1*13q>1OJmp-&A)3@J_A;A0V7u0bkfJ)-`Krrb zGey1(YybBo_r#8#3kPrfo#tA4xb3R$F4j$a9H~9huUPE$JQDstTM~O>^@Ps(;>UH<3Fx0=LBZUre@8723HjVToWun1;~KVtGY7SF1E7liM5< zHa%KaZ1y+v;MF5PX0dXM#bh1)zI}?P`JCclPp)GktoyD%Uv%1HzQp-VwViVJr}FN4 zBVAi3pdsabJ2zzio=sD>mtWX++u%m3k>>5t|1&=?+*B*EnLW)#$^O=9J{D1Fvz#4w zCmkrSML-}_v8Imc2?OP1;|%KWmLM+u&^dKy+fI{C57UY0UhRg-3U_ zKl;3k)jRBCi*uZh#-8L8Gwj!FXV37syULEeYD%&1+S-jgUC&wB|>?y4oO5hW>!C8<`)MX5lF!N|bKNY}tn*U&h` z(Adh*+{(a0+rYrez#!Wq{4a`z-29Zxv`X9>q*C7l^C^QQ$cEtjw370~qEv?R@^Zb* zyzJuS#DY}4{G#;P?`))iio&ZxB1(c1%M}WW^3yVNQWZ)n3sMy_3rdn17%JvG{=~yk z7^b0d%K!8k&!<5Q%*xz)$=t%q!rqfbn1vNw8cYtSFe`5kQ8<0$%84Uqj>sHgKi%N5 cz)O$emAGKZCnwXXKr0wLUHx3vIVCg!0EmFw6951J literal 0 HcmV?d00001 diff --git a/tests/compat/git/mod.rs b/tests/compat/git/mod.rs index b294cb11..66687a5a 100644 --- a/tests/compat/git/mod.rs +++ b/tests/compat/git/mod.rs @@ -184,7 +184,7 @@ fn fail_prefix_no_slash() { Case::git("fail_prefix_no_slash") .strip(1) .expect_success(false) - .expect_diffy_error(snapbox::str!["io error: No such file or directory (os error 2)"]) + .expect_diffy_error(snapbox::str!["apply error: error applying hunk #1"]) .expect_external_error(snapbox::str![[r#" error: git diff header lacks filename information when removing 1 leading pathname component (line 5) @@ -214,6 +214,71 @@ error: patch fragment without header at line 11: @@ -7,3 +7,3 @@ .run(); } +// Mixed binary and text patch. +// +// Both git apply and diffy should apply both the binary and text changes. +#[test] +fn binary_and_text_mixed() { + Case::git("binary_and_text_mixed").strip(1).run(); +} + +// Binary patch in literal format (new file creation). +#[test] +fn binary_literal() { + Case::git("binary_literal").strip(1).run(); +} + +// Binary patch in delta format (modify existing file). +#[test] +fn binary_delta() { + Case::git("binary_delta").strip(1).run(); +} + +// Binary literal patch applied to wrong original content. +// +// diffy succeeds (literal format doesn't need the original); git rejects. +#[test] +fn binary_literal_wrong_original() { + Case::git("binary_literal_wrong_original") + .strip(1) + .expect_compat(false) + .expect_external_error(snapbox::str![[r#" +error: corrupt binary patch at line 9: +error: No valid patches in input (allow with "--allow-empty") + +"#]]) + .run(); +} + +// Binary delta patch applied to wrong original content. +// Both diffy and git fail, but for different reasons (see snapshots). +#[test] +fn binary_delta_wrong_original() { + Case::git("binary_delta_wrong_original") + .strip(1) + .expect_success(false) + .expect_diffy_error(snapbox::str!["binary patch error: error parsing binary patch: original size mismatch: expected 5120, got 13"]) + .expect_external_error(snapbox::str![[r#" +error: the patch applies to 'large.bin' (0d6307ba5442d0fdfe89dd3d78f82604fe0c0d80), which does not match the current contents. +error: large.bin: patch does not apply + +"#]]) + .run(); +} + +// Binary patch with mixed delta/literal format. +// +// Git can choose different encodings for forward and reverse transformations +// based on which is more efficient. This patch has: +// - forward (original -> modified): delta +// - reverse (modified -> original): literal +// +// From rust-lang/rust@ad46af24 (favicon-32x32.png update). +#[test] +fn binary_mixed_delta_literal() { + Case::git("binary_mixed_delta_literal").strip(1).run(); +} + // Multi-file patch with junk/preamble text between different files. // // git apply behavior: Ignores content between `diff --git` boundaries. diff --git a/tests/compat/main.rs b/tests/compat/main.rs index e35ed079..2c0afe7c 100644 --- a/tests/compat/main.rs +++ b/tests/compat/main.rs @@ -20,25 +20,25 @@ //! //! ```sh //! # Run all compat tests -//! cargo test --test compat +//! cargo test --test compat -F binary //! //! # Run with reference tool comparison (CI mode) -//! CI=1 cargo test --test compat +//! CI=1 cargo test --test compat -F binary //! //! # For Nix users, run this to ensure you have GNU patch -//! CI=1 nix shell nixpkgs#gnupatch -c cargo test --test compat +//! CI=1 nix shell nixpkgs#gnupatch -c cargo test --test compat -F binary //! ``` //! //! ## Regenerating snapshots //! //! ```sh -//! SNAPSHOTS=overwrite cargo test --test compat +//! SNAPSHOTS=overwrite cargo test --test compat -F binary //! ``` //! //! ## Adding new test cases //! //! 1. Create `case_name/in/` with input file(s) and `foo.patch` -//! 2. Run `SNAPSHOTS=overwrite cargo test --test compat` to generate `out/` +//! 2. Run `SNAPSHOTS=overwrite cargo test --test compat -F binary` to generate `out/` //! 3. Add `#[test] fn case_name() { Case::{gnu_patch,git}(...).run(); }` in the module //! //! For failure tests, use `.expect_success(false)` and skip step 2. From b13571401897b10dfc57172085ae6c4b156783d5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:23:10 -0400 Subject: [PATCH 08/14] test(replay): wire binary patch into replay tests Now replay tests require `binary` Cargo feature. --- .github/workflows/replay.yml | 2 +- Cargo.toml | 4 +++ tests/replay.rs | 63 ++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/.github/workflows/replay.yml b/.github/workflows/replay.yml index 08384d61..48368fce 100644 --- a/.github/workflows/replay.yml +++ b/.github/workflows/replay.yml @@ -67,7 +67,7 @@ jobs: exit 1 fi - run: rustup toolchain install stable --profile minimal - - run: cargo test --release --test replay -- --ignored --nocapture + - run: cargo test --release --test replay -F binary -- --ignored --nocapture env: DIFFY_TEST_REPO: ${{ inputs.repo_url == '' && '.' || 'target/test-repo' }} DIFFY_TEST_COMMITS: ${{ inputs.commits }} diff --git a/Cargo.toml b/Cargo.toml index 42637f69..125a3c72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,7 @@ required-features = ["color"] [[test]] name = "compat" required-features = ["binary"] + +[[test]] +name = "replay" +required-features = ["binary"] diff --git a/tests/replay.rs b/tests/replay.rs index e6fbab3e..48f64e68 100644 --- a/tests/replay.rs +++ b/tests/replay.rs @@ -6,7 +6,7 @@ //! ## Usage //! //! ```console -//! $ cargo test --test replay -- --ignored --nocapture +//! $ cargo test --test replay -F binary -- --ignored --nocapture //! ``` //! //! ## Environment Variables @@ -59,7 +59,10 @@ use std::{ sync::Mutex, }; -use diffy::patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet}; +use diffy::{ + binary::BinaryPatch, + patch_set::{FileOperation, ParseOptions, PatchKind, PatchSet}, +}; use rayon::prelude::*; /// Persistent `git cat-file --batch` process for fast object lookups. @@ -331,13 +334,10 @@ fn process_commit( // UniDiff format cannot express pure renames (no ---/+++ headers). // Use `--no-renames` to represent them as delete + create instead. - // GitDiff mode handles renames via extended headers natively. + // GitDiff mode uses `--binary` to get actual binary patch data. let diff_output = match mode { TestMode::UniDiff => git_bytes(repo, &["diff", "--no-renames", parent, child]), - // TODO: pass `--binary` once binary patch support lands, - // so binary files get actual delta/literal data instead of - // "Binary files differ" markers. - TestMode::GitDiff => git_bytes(repo, &["diff", parent, child]), + TestMode::GitDiff => git_bytes(repo, &["diff", "--binary", parent, child]), }; if diff_output.is_empty() { @@ -387,9 +387,7 @@ fn process_commit( text_files + type_changes } TestMode::GitDiff => { - // Can't use `--numstat` for GitDiff: it shows `-\t-\t` for both - // actual binary diffs AND pure binary renames (100% similarity). - // Use `--raw` for total count instead. + // With `--binary`, all files including binary ones have patch data. let raw = git(repo, &["diff", "--raw", parent, child]); let (mut total, mut type_changes) = (0, 0); for line in raw.lines().filter(|l| !l.is_empty()) { @@ -544,12 +542,51 @@ fn process_commit( ); } } - PatchKind::Binary(_) => { - // Binary patch application not yet wired up in replay tests. - // Will be done once the `binary` Cargo feature is added. + PatchKind::Binary(BinaryPatch::Marker) => { + // `Binary files differ` marker - no actual data to apply skipped += 1; continue; } + PatchKind::Binary(patch) => { + // Get content as bytes + let base_content = if let Some(path) = base_path { + let Some(content) = cat.get(parent, path) else { + skipped += 1; + continue; + }; + content + } else { + Vec::new() + }; + + let expected_content = if let Some(path) = target_path { + let Some(content) = cat.get(child, path) else { + skipped += 1; + continue; + }; + content + } else { + Vec::new() + }; + + let result = match patch.apply(&base_content) { + Ok(r) => r, + Err(e) => { + panic!( + "Failed to apply binary patch at {parent_short}..{child_short} for {desc}: {e}" + ); + } + }; + + if result != expected_content { + panic!( + "Binary content mismatch at {parent_short}..{child_short} for {desc}\n\n\ + Expected {} bytes, got {} bytes", + expected_content.len(), + result.len() + ); + } + } } applied += 1; From 3622334be256c691473bd178ad113d5377eaa506 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:13:36 -0400 Subject: [PATCH 09/14] test(binary): add zero control byte delta test Assert current behavior where 0x00 control byte falls through to ADD which is a no-op. `git apply` rejects this as "unexpected delta opcode 0". --- src/binary/delta.rs | 21 +++++++++++++++ .../git/binary_delta_zero_control/in/file.bin | 1 + .../binary_delta_zero_control/in/foo.patch | 9 +++++++ .../binary_delta_zero_control/out/file.bin | 1 + tests/compat/git/mod.rs | 26 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 tests/compat/git/binary_delta_zero_control/in/file.bin create mode 100644 tests/compat/git/binary_delta_zero_control/in/foo.patch create mode 100644 tests/compat/git/binary_delta_zero_control/out/file.bin diff --git a/src/binary/delta.rs b/src/binary/delta.rs index fa1de268..b9ee1354 100644 --- a/src/binary/delta.rs +++ b/src/binary/delta.rs @@ -359,4 +359,25 @@ mod tests { let err = apply(&[], &delta).unwrap_err(); assert_eq!(err, DeltaError::UnexpectedEof); } + + #[test] + fn zero_control_byte_is_add_zero() { + // Same delta layout as compat fixture `binary_delta_zero_control`: + // hello -> hellX with zero control byte (0x00) between COPY and ADD. + // + // Currently 0x00 falls through to ADD with length 0 (a no-op), + // so the delta applies successfully. git apply rejects this. + let delta = [ + 0x05, // orig_size = 5 + 0x05, // mod_size = 5 + 0x91, // COPY: 0x80 | 0x10 | 0x01 (offset1 + size1 present) + 0x00, // offset=0 + 0x04, // len=4 ("hell") + 0x00, // zero control byte — currently treated as ADD(0) + 0x01, b'X', // ADD 1 byte: 'X' + ]; + let original = b"hello"; + let result = apply(original, &delta).unwrap(); + assert_eq!(result, b"hellX"); + } } diff --git a/tests/compat/git/binary_delta_zero_control/in/file.bin b/tests/compat/git/binary_delta_zero_control/in/file.bin new file mode 100644 index 00000000..b6fc4c62 --- /dev/null +++ b/tests/compat/git/binary_delta_zero_control/in/file.bin @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/tests/compat/git/binary_delta_zero_control/in/foo.patch b/tests/compat/git/binary_delta_zero_control/in/foo.patch new file mode 100644 index 00000000..373cf385 --- /dev/null +++ b/tests/compat/git/binary_delta_zero_control/in/foo.patch @@ -0,0 +1,9 @@ +diff --git a/file.bin b/file.bin +index b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0..959ff41a016f2a808d4f2b71f559d510416a020d 100644 +GIT binary patch +delta 8 +Pc${Npoyfq#z!(7l1S0_X + +delta 7 +Oc${Npoyfq#m=6F0ngI|1 + diff --git a/tests/compat/git/binary_delta_zero_control/out/file.bin b/tests/compat/git/binary_delta_zero_control/out/file.bin new file mode 100644 index 00000000..959ff41a --- /dev/null +++ b/tests/compat/git/binary_delta_zero_control/out/file.bin @@ -0,0 +1 @@ +hellX \ No newline at end of file diff --git a/tests/compat/git/mod.rs b/tests/compat/git/mod.rs index 66687a5a..6ff2c9e9 100644 --- a/tests/compat/git/mod.rs +++ b/tests/compat/git/mod.rs @@ -279,6 +279,32 @@ fn binary_mixed_delta_literal() { Case::git("binary_mixed_delta_literal").strip(1).run(); } +// Binary delta with a zero control byte (0x00) in the instruction stream. +// +// Hand-crafted fixture: `hello` -> `hellX`. Forward delta instructions: +// +// - 0x05 orig_size = 5 +// - 0x05 mod_size = 5 +// - 0x91 0x00 0x04 COPY offset=0, len=4 ("hell") +// - 0x00 zero control byte +// - 0x01 0x58 ADD 1 byte: 'X' +// +// git apply rejects this (`error: unexpected delta opcode 0`). +// diffy currently treats 0x00 as ADD(0) (a no-op) and succeeds. +#[test] +fn binary_delta_zero_control() { + Case::git("binary_delta_zero_control") + .strip(1) + .expect_compat(false) + .expect_external_error(snapbox::str![[r#" +error: unexpected delta opcode 0 +error: binary patch does not apply to 'file.bin' +error: file.bin: patch does not apply + +"#]]) + .run(); +} + // Multi-file patch with junk/preamble text between different files. // // git apply behavior: Ignores content between `diff --git` boundaries. From 99042b7c48754661d577b5059f57038ee45ade98 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:14:45 -0400 Subject: [PATCH 10/14] fix(binary): reject zero control byte in delta --- src/binary/delta.rs | 17 ++++++++++------- .../git/binary_delta_zero_control/out/file.bin | 1 - tests/compat/git/mod.rs | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) delete mode 100644 tests/compat/git/binary_delta_zero_control/out/file.bin diff --git a/src/binary/delta.rs b/src/binary/delta.rs index b9ee1354..7580e92d 100644 --- a/src/binary/delta.rs +++ b/src/binary/delta.rs @@ -46,6 +46,9 @@ pub fn apply(original: &[u8], delta: &[u8]) -> Result, DeltaError> { } result.extend_from_slice(&original[src_offset..src_end]); + } else if control == 0 { + // `git apply` rejects this as "unexpected delta opcode 0". + return Err(DeltaError::UnexpectedOpcode); } else { // ADD instruction let add_len = control as usize; @@ -180,6 +183,8 @@ pub enum DeltaError { }, /// COPY range calculation overflowed. InvalidCopyRange, + /// Unexpected delta opcode (control byte 0x00). + UnexpectedOpcode, } impl fmt::Display for DeltaError { @@ -210,6 +215,7 @@ impl fmt::Display for DeltaError { ) } DeltaError::InvalidCopyRange => write!(f, "copy range calculation overflow"), + DeltaError::UnexpectedOpcode => write!(f, "unexpected delta opcode 0"), } } } @@ -361,23 +367,20 @@ mod tests { } #[test] - fn zero_control_byte_is_add_zero() { + fn error_unexpected_opcode() { // Same delta layout as compat fixture `binary_delta_zero_control`: // hello -> hellX with zero control byte (0x00) between COPY and ADD. - // - // Currently 0x00 falls through to ADD with length 0 (a no-op), - // so the delta applies successfully. git apply rejects this. let delta = [ 0x05, // orig_size = 5 0x05, // mod_size = 5 0x91, // COPY: 0x80 | 0x10 | 0x01 (offset1 + size1 present) 0x00, // offset=0 0x04, // len=4 ("hell") - 0x00, // zero control byte — currently treated as ADD(0) + 0x00, // zero control byte (unexpected opcode) 0x01, b'X', // ADD 1 byte: 'X' ]; let original = b"hello"; - let result = apply(original, &delta).unwrap(); - assert_eq!(result, b"hellX"); + let err = apply(original, &delta).unwrap_err(); + assert_eq!(err, DeltaError::UnexpectedOpcode); } } diff --git a/tests/compat/git/binary_delta_zero_control/out/file.bin b/tests/compat/git/binary_delta_zero_control/out/file.bin deleted file mode 100644 index 959ff41a..00000000 --- a/tests/compat/git/binary_delta_zero_control/out/file.bin +++ /dev/null @@ -1 +0,0 @@ -hellX \ No newline at end of file diff --git a/tests/compat/git/mod.rs b/tests/compat/git/mod.rs index 6ff2c9e9..ff32e073 100644 --- a/tests/compat/git/mod.rs +++ b/tests/compat/git/mod.rs @@ -288,14 +288,14 @@ fn binary_mixed_delta_literal() { // - 0x91 0x00 0x04 COPY offset=0, len=4 ("hell") // - 0x00 zero control byte // - 0x01 0x58 ADD 1 byte: 'X' -// -// git apply rejects this (`error: unexpected delta opcode 0`). -// diffy currently treats 0x00 as ADD(0) (a no-op) and succeeds. #[test] fn binary_delta_zero_control() { Case::git("binary_delta_zero_control") .strip(1) - .expect_compat(false) + .expect_success(false) + .expect_diffy_error(snapbox::str![[ + "binary patch error: error parsing binary patch: unexpected delta opcode 0" + ]]) .expect_external_error(snapbox::str![[r#" error: unexpected delta opcode 0 error: binary patch does not apply to 'file.bin' From ceb0dfb5db2944bffdea8175aa98c590c204d3dc Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:35:53 -0400 Subject: [PATCH 11/14] test(binary): add literal size mismatch test Declared literal size doesn't match decompressed data. `git apply` rejects this; diffy currently doesn't check. --- src/binary/mod.rs | 11 +++++++++++ .../binary_literal_size_mismatch/in/foo.patch | 10 ++++++++++ .../binary_literal_size_mismatch/out/small.bin | Bin 0 -> 10 bytes tests/compat/git/mod.rs | 16 ++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 tests/compat/git/binary_literal_size_mismatch/in/foo.patch create mode 100644 tests/compat/git/binary_literal_size_mismatch/out/small.bin diff --git a/src/binary/mod.rs b/src/binary/mod.rs index b5cf1e48..aa71f324 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -535,6 +535,17 @@ mod apply_tests { assert_eq!(original.len(), 0); } + #[test] + fn literal_size_mismatch() { + // Declared size 99 but actual decompressed data is 10 bytes. + // Currently accepted — no size validation after decompression. + let input = "GIT binary patch\nliteral 99\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let (patch, _) = parse_binary_patch(input).unwrap(); + + let modified = patch.apply(&[]).unwrap(); + assert_eq!(modified.len(), 10); + } + #[test] fn apply_with_crlf_line_endings() { let input = "GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; diff --git a/tests/compat/git/binary_literal_size_mismatch/in/foo.patch b/tests/compat/git/binary_literal_size_mismatch/in/foo.patch new file mode 100644 index 00000000..4fbee5a3 --- /dev/null +++ b/tests/compat/git/binary_literal_size_mismatch/in/foo.patch @@ -0,0 +1,10 @@ +diff --git a/small.bin b/small.bin +new file mode 100644 +index 0000000000000000000000000000000000000000..df93f5f3f72487244976c34e85525cf445016566 +GIT binary patch +literal 99 +UcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k + +literal 0 +KcmV+b0RR6000031 + diff --git a/tests/compat/git/binary_literal_size_mismatch/out/small.bin b/tests/compat/git/binary_literal_size_mismatch/out/small.bin new file mode 100644 index 0000000000000000000000000000000000000000..df93f5f3f72487244976c34e85525cf445016566 GIT binary patch literal 10 RcmZQzWMXDvWn<^y1ONc904@Lk literal 0 HcmV?d00001 diff --git a/tests/compat/git/mod.rs b/tests/compat/git/mod.rs index ff32e073..7498a3c2 100644 --- a/tests/compat/git/mod.rs +++ b/tests/compat/git/mod.rs @@ -250,6 +250,22 @@ error: No valid patches in input (allow with "--allow-empty") .run(); } +// Declared literal size (99) doesn't match actual decompressed data (10 bytes). +// +// git apply rejects this; diffy currently ignores the declared size. +#[test] +fn binary_literal_size_mismatch() { + Case::git("binary_literal_size_mismatch") + .strip(1) + .expect_compat(false) + .expect_external_error(snapbox::str![[r#" +error: corrupt binary patch at line 7: +error: No valid patches in input (allow with "--allow-empty") + +"#]]) + .run(); +} + // Binary delta patch applied to wrong original content. // Both diffy and git fail, but for different reasons (see snapshots). #[test] From d506a5ed6709ac71a685fbbaa83c10010c848366 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 13:37:19 -0400 Subject: [PATCH 12/14] fix(binary): check decompressed size against declared After this, both git-apply and diffy reject. --- src/binary/mod.rs | 30 ++++++++++++++++-- .../out/small.bin | Bin 10 -> 0 bytes tests/compat/git/mod.rs | 8 +++-- 3 files changed, 32 insertions(+), 6 deletions(-) delete mode 100644 tests/compat/git/binary_literal_size_mismatch/out/small.bin diff --git a/src/binary/mod.rs b/src/binary/mod.rs index aa71f324..d82fe3bb 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -118,6 +118,14 @@ impl<'a> BinaryPatch<'a> { .read_to_end(&mut decompressed) .map_err(|e| BinaryPatchParseErrorKind::DecompressionFailed(e.to_string()))?; + if decompressed.len() as u64 != binary_data.size { + return Err(BinaryPatchParseErrorKind::DecompressedSizeMismatch { + expected: binary_data.size, + actual: decompressed.len() as u64, + } + .into()); + } + Ok(decompressed) } } @@ -236,6 +244,10 @@ pub(crate) enum BinaryPatchParseErrorKind { /// Zlib decompression failed. #[cfg(feature = "binary")] DecompressionFailed(String), + + /// Decompressed size doesn't match declared size. + #[cfg(feature = "binary")] + DecompressedSizeMismatch { expected: u64, actual: u64 }, } impl fmt::Display for BinaryPatchParseErrorKind { @@ -252,6 +264,13 @@ impl fmt::Display for BinaryPatchParseErrorKind { Self::Delta(e) => write!(f, "{e}"), #[cfg(feature = "binary")] Self::DecompressionFailed(msg) => write!(f, "decompression failed: {msg}"), + #[cfg(feature = "binary")] + Self::DecompressedSizeMismatch { expected, actual } => { + write!( + f, + "decompressed size mismatch: expected {expected}, got {actual}" + ) + } } } } @@ -538,12 +557,17 @@ mod apply_tests { #[test] fn literal_size_mismatch() { // Declared size 99 but actual decompressed data is 10 bytes. - // Currently accepted — no size validation after decompression. let input = "GIT binary patch\nliteral 99\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; let (patch, _) = parse_binary_patch(input).unwrap(); - let modified = patch.apply(&[]).unwrap(); - assert_eq!(modified.len(), 10); + let err = patch.apply(&[]).unwrap_err(); + assert!(matches!( + err.kind, + BinaryPatchParseErrorKind::DecompressedSizeMismatch { + expected: 99, + actual: 10 + } + )); } #[test] diff --git a/tests/compat/git/binary_literal_size_mismatch/out/small.bin b/tests/compat/git/binary_literal_size_mismatch/out/small.bin deleted file mode 100644 index df93f5f3f72487244976c34e85525cf445016566..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10 RcmZQzWMXDvWn<^y1ONc904@Lk diff --git a/tests/compat/git/mod.rs b/tests/compat/git/mod.rs index 7498a3c2..51b7cd8b 100644 --- a/tests/compat/git/mod.rs +++ b/tests/compat/git/mod.rs @@ -251,13 +251,15 @@ error: No valid patches in input (allow with "--allow-empty") } // Declared literal size (99) doesn't match actual decompressed data (10 bytes). -// -// git apply rejects this; diffy currently ignores the declared size. +// Both git apply and diffy reject this. #[test] fn binary_literal_size_mismatch() { Case::git("binary_literal_size_mismatch") .strip(1) - .expect_compat(false) + .expect_success(false) + .expect_diffy_error(snapbox::str![[ + "binary patch error: error parsing binary patch: decompressed size mismatch: expected 99, got 10" + ]]) .expect_external_error(snapbox::str![[r#" error: corrupt binary patch at line 7: error: No valid patches in input (allow with "--allow-empty") From c5f8447db84e893705471ae35002c3e0c3cf7551 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 18 Apr 2026 14:11:13 -0400 Subject: [PATCH 13/14] fix(binary): cap pre-allocation to prevent OOM A malformed delta header claiming a huge modified size could cause an instant OOM. Cap the pre-allocation at 64 KiB, and let the vec grow organically from actual instructions. --- src/binary/delta.rs | 2 +- src/binary/mod.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/binary/delta.rs b/src/binary/delta.rs index 7580e92d..7ab42bf6 100644 --- a/src/binary/delta.rs +++ b/src/binary/delta.rs @@ -24,7 +24,7 @@ pub fn apply(original: &[u8], delta: &[u8]) -> Result, DeltaError> { }); } - let mut result = Vec::with_capacity(header_mod_size as usize); + let mut result = Vec::with_capacity(header_mod_size.min(super::MAX_PREALLOC) as usize); // Process instructions until we've consumed all delta data while !cursor.is_empty() { diff --git a/src/binary/mod.rs b/src/binary/mod.rs index d82fe3bb..6c85d35e 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -12,6 +12,13 @@ mod delta; use std::{fmt, ops::Range}; +/// Cap preallocation when the size comes from untrusted input. +/// +/// This prevents instant OOM from a bogus header. +/// The vec grows as needed if the actual output is larger. +#[cfg(feature = "binary")] +const MAX_PREALLOC: u64 = 64 * 1024; // 64 KiB + /// The type of a binary patch block. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BinaryBlockKind { @@ -113,7 +120,7 @@ impl<'a> BinaryPatch<'a> { let compressed = decode_base85_lines(binary_data.data)?; let mut decoder = flate2::read::ZlibDecoder::new(&compressed[..]); - let mut decompressed = Vec::new(); + let mut decompressed = Vec::with_capacity(binary_data.size.min(MAX_PREALLOC) as usize); decoder .read_to_end(&mut decompressed) .map_err(|e| BinaryPatchParseErrorKind::DecompressionFailed(e.to_string()))?; From 54f4e9516f23b98cbdaebee62a16a510708b2df3 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sun, 19 Apr 2026 19:33:25 -0500 Subject: [PATCH 14/14] binary: convert parse_binary_patch to take &[u8] --- src/binary/base85.rs | 10 +++----- src/binary/mod.rs | 55 +++++++++++++++++++++--------------------- src/patch_set/parse.rs | 4 +-- src/utils.rs | 16 ------------ 4 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/binary/base85.rs b/src/binary/base85.rs index 182efe87..62cb832b 100644 --- a/src/binary/base85.rs +++ b/src/binary/base85.rs @@ -58,15 +58,13 @@ impl std::error::Error for Base85Error {} /// When decoding data where the original byte count isn't a multiple of 4, /// callers must handle truncation at a higher level. /// For example, via a length indicator in Git binary patch. -pub fn decode_into(input: &str, output: &mut impl Extend) -> Result<(), Base85Error> { - let bytes = input.as_bytes(); - - if bytes.len() % 5 != 0 { +pub fn decode_into(input: &[u8], output: &mut impl Extend) -> Result<(), Base85Error> { + if input.len() % 5 != 0 { return Err(Base85Error::InvalidLength); } // TODO: Use `as_chunks::<5>()` when MSRV >= 1.88 - for chunk in bytes.chunks_exact(5) { + for chunk in input.chunks_exact(5) { let mut value: u32 = 0; for &byte in chunk { let digit = TABLE[byte as usize]; @@ -120,7 +118,7 @@ mod tests { fn decode(input: &str) -> Result, Base85Error> { let mut result = Vec::with_capacity((input.len() / 5) * 4); - decode_into(input, &mut result)?; + decode_into(input.as_bytes(), &mut result)?; Ok(result) } diff --git a/src/binary/mod.rs b/src/binary/mod.rs index 6c85d35e..e41226d8 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -141,7 +141,7 @@ impl<'a> BinaryPatch<'a> { /// /// For example, the following patch block /// -/// * is parsed as `BinaryData { size: 10, data: "UcmV+l0QLU>0RjUA1qKHQ2>\`DEE&u=k" }` +/// * is parsed as `BinaryData { size: 10, data: b"UcmV+l0QLU>0RjUA1qKHQ2>\`DEE&u=k" }` /// * The line starts with a length indicator (`U` = 21 decoded bytes) /// /// @@ -154,7 +154,7 @@ pub struct BinaryData<'a> { /// Uncompressed size in bytes. pub size: u64, /// Raw Base85 lines with length indicators. - pub data: &'a str, + pub data: &'a [u8], } /// Error type for binary patch operations. @@ -284,12 +284,12 @@ impl fmt::Display for BinaryPatchParseErrorKind { /// Simple streaming parser for binary patches. struct BinaryParser<'a> { - input: &'a str, + input: &'a [u8], offset: usize, } impl<'a> BinaryParser<'a> { - fn new(input: &'a str) -> Self { + fn new(input: &'a [u8]) -> Self { Self { input, offset: 0 } } @@ -298,17 +298,17 @@ impl<'a> BinaryParser<'a> { BinaryPatchParseError::new(kind, self.offset..self.offset) } - fn next_line(&mut self) -> Option<&'a str> { + fn next_line(&mut self) -> Option<&'a [u8]> { let rest = &self.input[self.offset..]; if rest.is_empty() { return None; } - let (line, skip) = match rest.find('\n') { + let (line, skip) = match rest.iter().position(|&b| b == b'\n') { Some(pos) => (&rest[..pos], pos + 1), None => (rest, rest.len()), }; self.offset += skip; - Some(line.strip_suffix('\r').unwrap_or(line)) + Some(line.strip_suffix(b"\r").unwrap_or(line)) } } @@ -328,12 +328,12 @@ impl<'a> BinaryParser<'a> { /// /// ``` pub(crate) fn parse_binary_patch( - input: &str, + input: &[u8], ) -> Result<(BinaryPatch<'_>, usize), BinaryPatchParseError> { let mut parser = BinaryParser::new(input); // Expect "GIT binary patch" marker - if parser.next_line() != Some("GIT binary patch") { + if parser.next_line() != Some(b"GIT binary patch".as_slice()) { return Err(parser.error(BinaryPatchParseErrorKind::InvalidHeader)); } @@ -356,12 +356,14 @@ pub(crate) fn parse_binary_patch( fn parse_binary_block<'a>(parser: &mut BinaryParser<'a>) -> Option> { // Parse "literal 10" or "delta 18" let format_line = parser.next_line()?; - let (patch_type, size_str) = format_line.split_once(' ')?; + let space = format_line.iter().position(|&b| b == b' ')?; + let (patch_type, rest) = format_line.split_at(space); + let size_str = std::str::from_utf8(&rest[1..]).ok()?; let size: u64 = size_str.parse().ok()?; let kind = match patch_type { - "literal" => BinaryBlockKind::Literal, - "delta" => BinaryBlockKind::Delta, + b"literal" => BinaryBlockKind::Literal, + b"delta" => BinaryBlockKind::Delta, _ => return None, }; @@ -378,8 +380,8 @@ fn parse_binary_block<'a>(parser: &mut BinaryParser<'a>) -> Option(parser: &mut BinaryParser<'a>) -> Option This encodes the length of the (pre-encoded) data written on this line. /// > * `data` is Base85-encoded data for this line. #[cfg(feature = "binary")] -fn decode_base85_lines(data: &str) -> Result, BinaryPatchParseError> { +fn decode_base85_lines(data: &[u8]) -> Result, BinaryPatchParseError> { // A rough estimate: In Base85, 5 chars -> 4 bytes let mut result = Vec::with_capacity(data.len() * 4 / 5); - for line in data.lines() { + for line in data.split(|&b| b == b'\n') { + let line = line.strip_suffix(b"\r".as_slice()).unwrap_or(line); if line.is_empty() { continue; } - let line_bytes = line.as_bytes(); - - let length = decode_line_length(line_bytes[0]) + let length = decode_line_length(line[0]) .ok_or(BinaryPatchParseErrorKind::InvalidLineLengthIndicator)?; let encoded = &line[1..]; let start = result.len(); @@ -450,7 +451,7 @@ mod tests { #[test] fn parse_literal_format_simple() { - let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let input = b"GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; let (patch, consumed) = parse_binary_patch(input).unwrap(); assert_eq!(consumed, input.len()); @@ -467,7 +468,7 @@ mod tests { #[test] fn parse_delta_format() { - let input = "GIT binary patch\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^BFm9#}av-trB0zxAOrvLx|\n\n"; + let input = b"GIT binary patch\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^BFm9#}av-trB0zxAOrvLx|\n\n"; let (patch, _) = parse_binary_patch(input).unwrap(); match &patch { @@ -484,14 +485,14 @@ mod tests { #[test] fn parse_invalid_header() { // Without "GIT binary patch" marker, parse_binary_patch returns error - let input = "literal 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\n"; + let input = b"literal 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\n"; let err = parse_binary_patch(input).unwrap_err(); assert_eq!(err.kind, BinaryPatchParseErrorKind::InvalidHeader); } #[test] fn parse_with_crlf_line_endings() { - let input = "GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; + let input = b"GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; let (patch, consumed) = parse_binary_patch(input).unwrap(); assert_eq!(consumed, input.len()); @@ -509,7 +510,7 @@ mod tests { #[test] fn parse_mixed_format() { // Git can use different encoding for each direction - let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\n"; + let input = b"GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\ndelta 18\nccmV+t0PX*P2!IH%^Z^9`00000v-trB0x!=5aR2}S\n\n"; let (patch, _) = parse_binary_patch(input).unwrap(); match &patch { @@ -550,7 +551,7 @@ mod apply_tests { #[test] fn apply_literal_patch() { - let input = "GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let input = b"GIT binary patch\nliteral 10\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; let (patch, _) = parse_binary_patch(input).unwrap(); let modified = patch.apply(&[]).unwrap(); @@ -564,7 +565,7 @@ mod apply_tests { #[test] fn literal_size_mismatch() { // Declared size 99 but actual decompressed data is 10 bytes. - let input = "GIT binary patch\nliteral 99\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; + let input = b"GIT binary patch\nliteral 99\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\n\nliteral 0\nKcmV+b0RR6000031\n\n"; let (patch, _) = parse_binary_patch(input).unwrap(); let err = patch.apply(&[]).unwrap_err(); @@ -579,7 +580,7 @@ mod apply_tests { #[test] fn apply_with_crlf_line_endings() { - let input = "GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; + let input = b"GIT binary patch\r\nliteral 10\r\nUcmV+l0QLU>0RjUA1qKHQ2>`DEE&u=k\r\n\r\nliteral 0\r\nKcmV+b0RR6000031\r\n\r\n"; let (patch, _) = parse_binary_patch(input).unwrap(); let modified = patch.apply(&[]).unwrap(); diff --git a/src/patch_set/parse.rs b/src/patch_set/parse.rs index e552e647..a72b8234 100644 --- a/src/patch_set/parse.rs +++ b/src/patch_set/parse.rs @@ -255,9 +255,7 @@ fn next_gitdiff_patch<'a, T: Text + ?Sized>( // GitHeader::parse consumed the marker line but not the payload. // Use the recorded offset to pass input from the marker onward. let (_, binary_input) = ps.input.split_at(abs_patch_start + binary_patch_start); - // Binary patch data is always ASCII (base85-encoded). - let binary_input = binary_input.as_str_prefix(); - let (binary_patch, consumed) = match parse_binary_patch(binary_input) { + let (binary_patch, consumed) = match parse_binary_patch(binary_input.as_bytes()) { Ok(result) => result, Err(e) => return Some(Err(e.into())), }; diff --git a/src/utils.rs b/src/utils.rs index e337e6cd..c4914566 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -144,11 +144,6 @@ pub trait Text: Eq + Hash + ToOwned { fn find(&self, needle: &str) -> Option; fn split_at(&self, mid: usize) -> (&Self, &Self); fn as_str(&self) -> Option<&str>; - /// Returns the longest valid UTF-8 prefix. - /// - /// For `str` this is the entire input. - /// For `[u8]` this returns up to the first invalid UTF-8 byte. - fn as_str_prefix(&self) -> &str; fn as_bytes(&self) -> &[u8]; #[allow(unused)] fn lines(&self) -> LineIter<'_, Self>; @@ -210,10 +205,6 @@ impl Text for str { Some(self) } - fn as_str_prefix(&self) -> &str { - self - } - fn as_bytes(&self) -> &[u8] { self.as_bytes() } @@ -268,13 +259,6 @@ impl Text for [u8] { std::str::from_utf8(self).ok() } - fn as_str_prefix(&self) -> &str { - match std::str::from_utf8(self) { - Ok(s) => s, - Err(e) => std::str::from_utf8(&self[..e.valid_up_to()]).unwrap(), - } - } - fn as_bytes(&self) -> &[u8] { self }