| | | 1 | | namespace DeltaSharp; |
| | | 2 | | |
| | | 3 | | /// <summary>Provides output-size-limited patch application for <see cref="IDeltaApply"/> implementations.</summary> |
| | | 4 | | public static class DeltaApplyExtensions |
| | | 5 | | { |
| | | 6 | | /// <summary>Applies a patch after enforcing an output-size limit in bytes.</summary> |
| | | 7 | | /// <param name="apply">The applier for the patch's format and checksum algorithm.</param> |
| | | 8 | | /// <param name="source">The original source bytes against which the patch was created.</param> |
| | | 9 | | /// <param name="delta">The encoded patch, including its output-length header and final checksum.</param> |
| | | 10 | | /// <param name="maxOutputLength">The maximum permitted target length in bytes; zero permits only empty output.</par |
| | | 11 | | /// <returns>The reconstructed target in newly allocated memory and its verified checksum.</returns> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// The declared length is checked before allocating the output buffer. The limit bounds the target |
| | | 14 | | /// allocation, not total memory usage or processing time. Validation is delegated to |
| | | 15 | | /// <see cref="IDeltaApply.Apply(ReadOnlySpan{byte}, ReadOnlySpan{byte}, Memory{byte})"/>. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <exception cref="ArgumentNullException"><paramref name="apply"/> is null.</exception> |
| | | 18 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxOutputLength"/> is negative.</exception> |
| | | 19 | | /// <exception cref="DeltaApplyException"> |
| | | 20 | | /// The declared length exceeds the limit, with <see cref="DeltaApplyException.Error"/> set to |
| | | 21 | | /// <see cref="DeltaApplyException.DeltaApplyExceptionError.OutputLengthLimitExceeded"/>, |
| | | 22 | | /// or the applier rejects the patch during validation. |
| | | 23 | | /// </exception> |
| | | 24 | | /// <exception cref="Format.DeltaReaderException">The format reader rejects malformed command syntax.</exception> |
| | | 25 | | public static DeltaResult Apply(this IDeltaApply apply, ReadOnlySpan<byte> source, |
| | | 26 | | ReadOnlySpan<byte> delta, int maxOutputLength) |
| | | 27 | | { |
| | 650 | 28 | | if (apply is null) |
| | 0 | 29 | | throw new ArgumentNullException(nameof(apply)); |
| | 650 | 30 | | if (maxOutputLength < 0) |
| | 30 | 31 | | throw new ArgumentOutOfRangeException(nameof(maxOutputLength)); |
| | | 32 | | |
| | 620 | 33 | | var outputLength = apply.GetOutputLength(delta); |
| | 590 | 34 | | if (outputLength > (ulong)maxOutputLength) |
| | 250 | 35 | | throw new DeltaApplyException(DeltaApplyException.DeltaApplyExceptionError.OutputLengthLimitExceeded); |
| | | 36 | | |
| | | 37 | | #if NETSTANDARD2_0 |
| | 68 | 38 | | var buffer = new byte[(int)outputLength]; |
| | | 39 | | #else |
| | 272 | 40 | | var buffer = GC.AllocateUninitializedArray<byte>((int)outputLength); |
| | | 41 | | #endif |
| | 340 | 42 | | return apply.Apply(source, delta, buffer.AsMemory()); |
| | | 43 | | } |
| | | 44 | | } |