| | | 1 | | using DeltaSharp.Format; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using System.Xml; |
| | | 8 | | using static DeltaSharp.DeltaApplyException; |
| | | 9 | | |
| | | 10 | | namespace DeltaSharp; |
| | | 11 | | |
| | | 12 | | |
| | | 13 | | /// <summary>Provides matching patch creation and application services for a delta format.</summary> |
| | | 14 | | public interface IDelta |
| | | 15 | | { |
| | | 16 | | /// <summary>Gets the service that applies patches in this format.</summary> |
| | | 17 | | IDeltaApply Apply { get; } |
| | | 18 | | /// <summary>Gets the service that creates patches in this format.</summary> |
| | | 19 | | IDeltaCreate Create { get; } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary>Creates and applies Fossil-format patches using the Fossil additive checksum.</summary> |
| | | 23 | | public class DeltaFossil: IDelta |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc cref="IDelta.Apply"/> |
| | 7680 | 26 | | public IDeltaApply Apply { get; } |
| | | 27 | | /// <inheritdoc cref="IDelta.Create"/> |
| | 2680 | 28 | | public IDeltaCreate Create { get; } |
| | | 29 | | |
| | | 30 | | /// <summary>Initializes Fossil services with exhaustive source-match search for patch creation.</summary> |
| | 150 | 31 | | public DeltaFossil() : this(DeltaSearchMode.Exhaustive) |
| | | 32 | | { |
| | 150 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Initializes Fossil services with the specified source-match search mode for patch creation.</summary> |
| | | 36 | | /// <param name="searchMode">The search-effort policy for the creation service; does not affect patch application.</ |
| | | 37 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | 180 | 38 | | public DeltaFossil(DeltaSearchMode searchMode) |
| | | 39 | | { |
| | 180 | 40 | | Apply = new DeltaApply<FossilDeltaReader, DeltaChecksum>(); |
| | 180 | 41 | | Create = new DeltaCreate<FossilDeltaWriter, DeltaChecksum>(new DeltaChecksum(), searchMode); |
| | 170 | 42 | | } |
| | | 43 | | |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary>Creates and applies DeltaSharp binary patches using the Fossil additive checksum.</summary> |
| | | 47 | | public class DeltaBinary : IDelta |
| | | 48 | | { |
| | | 49 | | /// <inheritdoc cref="IDelta.Apply"/> |
| | | 50 | | public IDeltaApply Apply { get; } |
| | | 51 | | /// <inheritdoc cref="IDelta.Create"/> |
| | | 52 | | public IDeltaCreate Create { get; } |
| | | 53 | | |
| | | 54 | | /// <summary>Initializes binary services with exhaustive source-match search for patch creation.</summary> |
| | | 55 | | public DeltaBinary() : this(DeltaSearchMode.Exhaustive) |
| | | 56 | | { |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary>Initializes binary services with the specified source-match search mode for patch creation.</summary> |
| | | 60 | | /// <param name="searchMode">The search-effort policy for the creation service; does not affect patch application.</ |
| | | 61 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | | 62 | | public DeltaBinary(DeltaSearchMode searchMode) |
| | | 63 | | { |
| | | 64 | | Apply = new DeltaApply<BinaryDeltaReader, DeltaChecksum>(); |
| | | 65 | | Create = new DeltaCreate<BinaryDeltaWriter, DeltaChecksum>(new DeltaChecksum(), searchMode); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary>Creates and applies DeltaSharp ASCII patches using the Fossil additive checksum.</summary> |
| | | 70 | | /// <remarks>Command headers are ASCII text, but inserted payloads may contain arbitrary binary bytes.</remarks> |
| | | 71 | | public class DeltaASCII : IDelta |
| | | 72 | | { |
| | | 73 | | /// <inheritdoc cref="IDelta.Apply"/> |
| | | 74 | | public IDeltaApply Apply { get; } |
| | | 75 | | /// <inheritdoc cref="IDelta.Create"/> |
| | | 76 | | public IDeltaCreate Create { get; } |
| | | 77 | | |
| | | 78 | | /// <summary>Initializes ASCII services with exhaustive source-match search for patch creation.</summary> |
| | | 79 | | public DeltaASCII() : this(DeltaSearchMode.Exhaustive) |
| | | 80 | | { |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary>Initializes ASCII services with the specified source-match search mode for patch creation.</summary> |
| | | 84 | | /// <param name="searchMode">The search-effort policy for the creation service; does not affect patch application.</ |
| | | 85 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | | 86 | | public DeltaASCII(DeltaSearchMode searchMode) |
| | | 87 | | { |
| | | 88 | | Apply = new DeltaApply<ASCIIDeltaReader, DeltaChecksum>(); |
| | | 89 | | Create = new DeltaCreate<ASCIIDeltaWriter, DeltaChecksum>(new DeltaChecksum(), searchMode); |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary>Provides shared services and convenience methods for binary, ASCII, and Fossil patches.</summary> |
| | | 94 | | /// <remarks> |
| | | 95 | | /// All built-in formats use the Fossil additive target checksum. Match the creation and application |
| | | 96 | | /// formats, and supply the original source when applying a patch. Creation defaults to |
| | | 97 | | /// <see cref="DeltaSearchMode.Exhaustive"/>; use an explicit mode to bound source-match candidates. |
| | | 98 | | /// </remarks> |
| | | 99 | | public static class DeltaSharp |
| | | 100 | | { |
| | | 101 | | /// <summary>Gets shared binary-format services with exhaustive source-match search for creation.</summary> |
| | | 102 | | public static IDelta Binary { get; } |
| | | 103 | | /// <summary>Gets shared Fossil-format services with exhaustive source-match search for creation.</summary> |
| | | 104 | | public static IDelta Fossil { get; } |
| | | 105 | | /// <summary>Gets shared ASCII-format services with exhaustive source-match search for creation.</summary> |
| | | 106 | | public static IDelta ASCII { get; } |
| | | 107 | | |
| | | 108 | | private static readonly IDeltaCreate BoundedBinaryCreator = |
| | | 109 | | new DeltaCreate<BinaryDeltaWriter, DeltaChecksum>(new DeltaChecksum(), DeltaSearchMode.Bounded); |
| | | 110 | | private static readonly IDeltaCreate BoundedFossilCreator = |
| | | 111 | | new DeltaCreate<FossilDeltaWriter, DeltaChecksum>(new DeltaChecksum(), DeltaSearchMode.Bounded); |
| | | 112 | | private static readonly IDeltaCreate BoundedAsciiCreator = |
| | | 113 | | new DeltaCreate<ASCIIDeltaWriter, DeltaChecksum>(new DeltaChecksum(), DeltaSearchMode.Bounded); |
| | | 114 | | |
| | | 115 | | static DeltaSharp() |
| | | 116 | | { |
| | | 117 | | Binary = new DeltaBinary(); |
| | | 118 | | Fossil = new DeltaFossil(); |
| | | 119 | | ASCII = new DeltaASCII(); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary>Creates a binary patch using exhaustive source-match search.</summary> |
| | | 123 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 124 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 125 | | /// <returns>The binary patch bytes and the Fossil additive checksum of the target.</returns> |
| | | 126 | | public static DeltaResult CreateBinary(ReadOnlySpan<byte> source,ReadOnlySpan<byte> target) |
| | | 127 | | { |
| | | 128 | | return Binary.Create.Create(source, target); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary>Creates a binary patch using the selected source-match search mode.</summary> |
| | | 132 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 133 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 134 | | /// <param name="searchMode">The search-effort policy for this call.</param> |
| | | 135 | | /// <returns>The binary patch bytes and the Fossil additive checksum of the target.</returns> |
| | | 136 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | | 137 | | public static DeltaResult CreateBinary(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target, DeltaSearchMode searchM |
| | | 138 | | => SelectCreator(searchMode, Binary.Create, BoundedBinaryCreator).Create(source, target); |
| | | 139 | | |
| | | 140 | | /// <summary>Creates a Fossil patch using exhaustive source-match search.</summary> |
| | | 141 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 142 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 143 | | /// <returns>The Fossil patch bytes and the Fossil additive checksum of the target.</returns> |
| | | 144 | | public static DeltaResult CreateFossil(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target) |
| | | 145 | | { |
| | | 146 | | return Fossil.Create.Create(source, target); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary>Creates a Fossil patch using the selected source-match search mode.</summary> |
| | | 150 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 151 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 152 | | /// <param name="searchMode">The search-effort policy for this call.</param> |
| | | 153 | | /// <returns>The Fossil patch bytes and the Fossil additive checksum of the target.</returns> |
| | | 154 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | | 155 | | public static DeltaResult CreateFossil(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target, DeltaSearchMode searchM |
| | | 156 | | => SelectCreator(searchMode, Fossil.Create, BoundedFossilCreator).Create(source, target); |
| | | 157 | | |
| | | 158 | | /// <summary>Creates an ASCII patch using exhaustive source-match search.</summary> |
| | | 159 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 160 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 161 | | /// <returns>The ASCII patch bytes, including raw insert payloads, and the Fossil additive checksum of the target.</ |
| | | 162 | | public static DeltaResult CreateASCII(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target) |
| | | 163 | | { |
| | | 164 | | return ASCII.Create.Create(source, target); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary>Creates an ASCII patch using the selected source-match search mode.</summary> |
| | | 168 | | /// <param name="source">The original bytes that will be available when applying the patch; may be empty.</param> |
| | | 169 | | /// <param name="target">The desired reconstructed bytes; may be empty.</param> |
| | | 170 | | /// <param name="searchMode">The search-effort policy for this call.</param> |
| | | 171 | | /// <returns>The ASCII patch bytes, including raw insert payloads, and the Fossil additive checksum of the target.</ |
| | | 172 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="searchMode"/> is not a defined search mode.</excep |
| | | 173 | | public static DeltaResult CreateASCII(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target, DeltaSearchMode searchMo |
| | | 174 | | => SelectCreator(searchMode, ASCII.Create, BoundedAsciiCreator).Create(source, target); |
| | | 175 | | |
| | | 176 | | private static IDeltaCreate SelectCreator(DeltaSearchMode searchMode, IDeltaCreate exhaustive, IDeltaCreate bounded) |
| | | 177 | | => searchMode switch |
| | | 178 | | { |
| | | 179 | | DeltaSearchMode.Exhaustive => exhaustive, |
| | | 180 | | DeltaSearchMode.Bounded => bounded, |
| | | 181 | | _ => throw new ArgumentOutOfRangeException(nameof(searchMode), searchMode, "Unknown search mode.") |
| | | 182 | | }; |
| | | 183 | | |
| | | 184 | | /// <summary>Applies a binary patch and verifies the target checksum.</summary> |
| | | 185 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 186 | | /// <param name="delta">The binary patch to apply.</param> |
| | | 187 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 188 | | /// <remarks>Allocates the declared target length before full validation. Use the overload with a size limit to cap |
| | | 189 | | /// <exception cref="DeltaApplyException">The patch is incomplete or fails structural, length, source-range, or chec |
| | | 190 | | /// <exception cref="DeltaReaderException">The binary reader rejects malformed command syntax.</exception> |
| | | 191 | | public static DeltaResult ApplyBinary(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta) |
| | | 192 | | { |
| | | 193 | | return Binary.Apply.Apply(source, delta); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <inheritdoc cref="IDeltaApply.Apply(ReadOnlySpan{byte}, ReadOnlySpan{byte}, Memory{byte})"/> |
| | | 197 | | public static DeltaResult ApplyBinaryInto(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, Memory<byte> output) |
| | | 198 | | => Binary.Apply.Apply(source, delta, output); |
| | | 199 | | |
| | | 200 | | /// <summary>Applies a binary patch with an output-size limit checked before allocation.</summary> |
| | | 201 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 202 | | /// <param name="delta">The binary patch to apply.</param> |
| | | 203 | | /// <param name="maxOutputLength">The maximum target length in bytes; zero permits only empty output.</param> |
| | | 204 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 205 | | /// <remarks>The limit bounds the target allocation, not total memory or processing time. Bytes after the first vali |
| | | 206 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxOutputLength"/> is negative.</exception> |
| | | 207 | | /// <exception cref="DeltaApplyException">The declared length exceeds the limit, or the patch is incomplete or fails |
| | | 208 | | /// <exception cref="DeltaReaderException">The binary reader rejects malformed command syntax.</exception> |
| | | 209 | | public static DeltaResult ApplyBinary(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, int maxOutputLength) |
| | | 210 | | => Binary.Apply.Apply(source, delta, maxOutputLength); |
| | | 211 | | |
| | | 212 | | /// <summary>Applies a Fossil patch and verifies the target checksum.</summary> |
| | | 213 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 214 | | /// <param name="delta">The Fossil patch to apply.</param> |
| | | 215 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 216 | | /// <remarks>Allocates the declared target length before full validation. Use the overload with a size limit to cap |
| | | 217 | | /// <exception cref="DeltaApplyException">The patch is incomplete or fails structural, length, source-range, or chec |
| | | 218 | | /// <exception cref="DeltaReaderException">The Fossil reader rejects malformed syntax or a numeric operand exceeding |
| | | 219 | | public static DeltaResult ApplyFossil(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta) |
| | | 220 | | { |
| | | 221 | | return Fossil.Apply.Apply(source, delta); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <inheritdoc cref="IDeltaApply.Apply(ReadOnlySpan{byte}, ReadOnlySpan{byte}, Memory{byte})"/> |
| | | 225 | | public static DeltaResult ApplyFossilInto(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, Memory<byte> output) |
| | | 226 | | => Fossil.Apply.Apply(source, delta, output); |
| | | 227 | | |
| | | 228 | | /// <summary>Applies a Fossil patch with an output-size limit checked before allocation.</summary> |
| | | 229 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 230 | | /// <param name="delta">The Fossil patch to apply.</param> |
| | | 231 | | /// <param name="maxOutputLength">The maximum target length in bytes; zero permits only empty output.</param> |
| | | 232 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 233 | | /// <remarks>The limit bounds the target allocation, not total memory or processing time. Bytes after the first vali |
| | | 234 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxOutputLength"/> is negative.</exception> |
| | | 235 | | /// <exception cref="DeltaApplyException">The declared length exceeds the limit, or the patch is incomplete or fails |
| | | 236 | | /// <exception cref="DeltaReaderException">The Fossil reader rejects malformed syntax or a numeric operand exceeding |
| | | 237 | | public static DeltaResult ApplyFossil(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, int maxOutputLength) |
| | | 238 | | => Fossil.Apply.Apply(source, delta, maxOutputLength); |
| | | 239 | | |
| | | 240 | | /// <summary>Applies an ASCII patch and verifies the target checksum.</summary> |
| | | 241 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 242 | | /// <param name="delta">The ASCII patch to apply, including any raw insert payloads.</param> |
| | | 243 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 244 | | /// <remarks>Allocates the declared target length before full validation. Use the overload with a size limit to cap |
| | | 245 | | /// <exception cref="DeltaApplyException">The patch is incomplete or fails structural, length, source-range, or chec |
| | | 246 | | /// <exception cref="DeltaReaderException">The ASCII reader rejects malformed command syntax.</exception> |
| | | 247 | | public static DeltaResult ApplyASCII(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta) |
| | | 248 | | { |
| | | 249 | | return ASCII.Apply.Apply(source, delta); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <inheritdoc cref="IDeltaApply.Apply(ReadOnlySpan{byte}, ReadOnlySpan{byte}, Memory{byte})"/> |
| | | 253 | | public static DeltaResult ApplyASCIIInto(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, Memory<byte> output) |
| | | 254 | | => ASCII.Apply.Apply(source, delta, output); |
| | | 255 | | |
| | | 256 | | /// <summary>Applies an ASCII patch with an output-size limit checked before allocation.</summary> |
| | | 257 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 258 | | /// <param name="delta">The ASCII patch to apply, including any raw insert payloads.</param> |
| | | 259 | | /// <param name="maxOutputLength">The maximum target length in bytes; zero permits only empty output.</param> |
| | | 260 | | /// <returns>The reconstructed target in newly allocated memory and its Fossil additive checksum.</returns> |
| | | 261 | | /// <remarks>The limit bounds the target allocation, not total memory or processing time. Bytes after the first vali |
| | | 262 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxOutputLength"/> is negative.</exception> |
| | | 263 | | /// <exception cref="DeltaApplyException">The declared length exceeds the limit, or the patch is incomplete or fails |
| | | 264 | | /// <exception cref="DeltaReaderException">The ASCII reader rejects malformed command syntax.</exception> |
| | | 265 | | public static DeltaResult ApplyASCII(ReadOnlySpan<byte> source, ReadOnlySpan<byte> delta, int maxOutputLength) |
| | | 266 | | => ASCII.Apply.Apply(source, delta, maxOutputLength); |
| | | 267 | | } |