단일 멤버 메모리 GZip 검증기를 추가함
- 작성자
- 시로사키 하나
- 작성 시각
- 커밋
e1ffd7bbffc358b6e53358682a93bf3ce075e892- 부모
- 018e759
변경된 파일 9개
+709-0| @@ -0,0 +1,16 @@ | ||
| 1 | 추가: # This file is automatically @generated by Cargo. | |
| 2 | 추가: # It is not intended for manual editing. | |
| 3 | 추가: version = 4 | |
| 4 | 추가: | |
| 5 | 추가: [[package]] | |
| 6 | 추가: name = "zipelo-memory-gzip-reader" | |
| 7 | 추가: version = "0.1.0" | |
| 8 | 추가: dependencies = [ | |
| 9 | 추가: "zlib-rs", | |
| 10 | 추가: ] | |
| 11 | 추가: | |
| 12 | 추가: [[package]] | |
| 13 | 추가: name = "zlib-rs" | |
| 14 | 추가: version = "0.6.8" | |
| 15 | 추가: source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 16 | 추가: checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" |
| @@ -0,0 +1,14 @@ | ||
| 1 | 추가: [package] | |
| 2 | 추가: name = "zipelo-memory-gzip-reader" | |
| 3 | 추가: version = "0.1.0" | |
| 4 | 추가: edition = "2024" | |
| 5 | 추가: license = "MIT" | |
| 6 | 추가: publish = false | |
| 7 | 추가: | |
| 8 | 추가: [lib] | |
| 9 | 추가: path = "src/lib.rs" | |
| 10 | 추가: | |
| 11 | 추가: [dependencies] | |
| 12 | 추가: zlib-rs = { version = "=0.6.8", default-features = false, features = ["rust-allocator"] } | |
| 13 | 추가: | |
| 14 | 추가: [workspace] |
| @@ -0,0 +1,21 @@ | ||
| 1 | 추가: MIT License | |
| 2 | 추가: | |
| 3 | 추가: Copyright (c) 2026 Team-LJY contributors | |
| 4 | 추가: | |
| 5 | 추가: Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 6 | 추가: of this software and associated documentation files (the "Software"), to deal | |
| 7 | 추가: in the Software without restriction, including without limitation the rights | |
| 8 | 추가: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 9 | 추가: copies of the Software, and to permit persons to whom the Software is | |
| 10 | 추가: furnished to do so, subject to the following conditions: | |
| 11 | 추가: | |
| 12 | 추가: The above copyright notice and this permission notice shall be included in all | |
| 13 | 추가: copies or substantial portions of the Software. | |
| 14 | 추가: | |
| 15 | 추가: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 16 | 추가: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 17 | 추가: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 18 | 추가: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 19 | 추가: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 | 추가: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 21 | 추가: SOFTWARE. |
| @@ -0,0 +1,8 @@ | ||
| 1 | 추가: 511197f27b89d37c86f82ae1e8dda4229a3e5082ede9274efe8480df6e650d24 Cargo.lock | |
| 2 | 추가: ae76aededbb85cdd6c9227b4c06a1c8fef1bb7f203f1703e787b68f30ccfab6a Cargo.toml | |
| 3 | 추가: 0714945e52a4d5b560c02c9a18478519f8c4e2572cbf87bd6d52adcfb2b63c62 LICENSE | |
| 4 | 추가: 655ad3bee8c41a593c2b00cbb0a12be11e7246c41dda00319c223dd3f904910e THIRD-PARTY-NOTICES.txt | |
| 5 | 추가: 1346c0b6f0ce5221acb9aef5067b0169e08ea6752f4db41a8b9c36b10ef9b43b contract.json | |
| 6 | 추가: 3f71220451f8cd55dafdb6e6ae901572ec7bbe1be9c93d78953b44244aec6b07 dependencies.json | |
| 7 | 추가: b0d0b891a048712baba2038b8439d809a0359638f390b806f88496733753d61c src/budget.rs | |
| 8 | 추가: a865a114fdb9d79b66226387b9f0e3fa601aad29cfe65ab443c946d6cbd74062 src/lib.rs |
| @@ -0,0 +1,27 @@ | ||
| 1 | 추가: zlib-rs 0.6.8 | |
| 2 | 추가: License: Zlib | |
| 3 | 추가: Source: https://crates.io/crates/zlib-rs/0.6.8 | |
| 4 | 추가: Repository: https://github.com/trifectatechfoundation/zlib-rs | |
| 5 | 추가: Archive SHA256: b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112 | |
| 6 | 추가: LICENSE SHA256: e72111c52b7d96ebe25348dee19f0744f444d3c95ae6b1ecb6ccaecc5bce05ba | |
| 7 | 추가: The LICENSE bytes below are preserved verbatim. | |
| 8 | 추가: | |
| 9 | 추가: (C) 2024 Trifecta Tech Foundation | |
| 10 | 추가: | |
| 11 | 추가: This software is provided 'as-is', without any express or implied | |
| 12 | 추가: warranty. In no event will the authors be held liable for any damages | |
| 13 | 추가: arising from the use of this software. | |
| 14 | 추가: | |
| 15 | 추가: Permission is granted to anyone to use this software for any purpose, | |
| 16 | 추가: including commercial applications, and to alter it and redistribute it | |
| 17 | 추가: freely, subject to the following restrictions: | |
| 18 | 추가: | |
| 19 | 추가: 1. The origin of this software must not be misrepresented; you must not | |
| 20 | 추가: claim that you wrote the original software. If you use this software | |
| 21 | 추가: in a product, an acknowledgment in the product documentation would be | |
| 22 | 추가: appreciated but is not required. | |
| 23 | 추가: | |
| 24 | 추가: 2. Altered source versions must be plainly marked as such, and must not be | |
| 25 | 추가: misrepresented as being the original software. | |
| 26 | 추가: | |
| 27 | 추가: 3. This notice may not be removed or altered from any source distribution. |
| @@ -0,0 +1,137 @@ | ||
| 1 | 추가: { | |
| 2 | 추가: "schema_version": 1, | |
| 3 | 추가: "base_head": "018e7593ecb2b3a7ae0bc17a440ecf2c0de8b4dd", | |
| 4 | 추가: "csharp_reference": { | |
| 5 | 추가: "head": "f56324c98468076c57e18255ac1661a66fe26f84", | |
| 6 | 추가: "pr": 18, | |
| 7 | 추가: "paths": [ | |
| 8 | 추가: "tests/gzip-history-integration-probe/BoundaryProbe.cs", | |
| 9 | 추가: "tests/gzip-history-integration-probe/PendingValidation.cs", | |
| 10 | 추가: "tests/gzip-history-integration-probe/contract.json" | |
| 11 | 추가: ] | |
| 12 | 추가: }, | |
| 13 | 추가: "provenance": { | |
| 14 | 추가: "implementation": "New Rust implementation of the C# boundary/budget behavior. No source inclusion or dependency on another candidate. Root MIT license applies.", | |
| 15 | 추가: "api_reference": "PR33 90eafe0b4dcda137e90c78685b64abefa6f33357 experiments/rust-typed-deflate/src/typed_deflate.rs and cached zlib-rs 0.6.8 public API", | |
| 16 | 추가: "differences_from_pr33": [ | |
| 17 | 추가: "No ZIP/JSON/base64, crc32fast, cfg-if, path source inclusion, provisional sink or expected ZIP size", | |
| 18 | 추가: "Actual failed decode output charged before backend error propagation", | |
| 19 | 추가: "No 131072-call ceiling; continuation requires consumed input or produced output" | |
| 20 | 추가: ] | |
| 21 | 추가: }, | |
| 22 | 추가: "api": { | |
| 23 | 추가: "read": "read(Request { input: Box<[u8]>, selected: bool }, &mut Budget, FnMut(Checkpoint) -> Result<(), Stop<E>>) -> Result<Completed, Error<E>>", | |
| 24 | 추가: "budget": "Budget::new(Limits), limits(), usage(), elapsed(). Caller retains the same mutable Budget across retries; no reset API. Instant starts at construction.", | |
| 25 | 추가: "completed": "Boundary { header_bytes, deflate_bytes, read_ahead_bytes, inflated_bytes, crc32, member_bytes, offered_bytes }; bytes: Option<Vec<u8>>; cumulative usage snapshot", | |
| 26 | 추가: "unselected": "None output still fully decodes and verifies. Some(empty Vec) distinguishes selected empty payload.", | |
| 27 | 추가: "ownership": "Input consumed by ownership. Output stays private until successful final check after decoder drop. Failure drops all temporary output but retains budget charges. No early sink or user-controlled decoder injection." | |
| 28 | 추가: }, | |
| 29 | 추가: "format": [ | |
| 30 | 추가: "Single GZip member only; exact magic, CM=8, reserved flags clear", | |
| 31 | 추가: "Consume MTIME/XFL/OS and FTEXT without interpreting them", | |
| 32 | 추가: "FEXTRA u16 length and exact bytes; FNAME/FCOMMENT terminated byte fields, never paths or Unicode strings", | |
| 33 | 추가: "FHCRC lower 16 bits of zlib-rs CRC32 over preceding header bytes", | |
| 34 | 추가: "Raw Inflate::new(false,15); typed Status and InflateError; no dictionary supplied", | |
| 35 | 추가: "Actual StreamEnd consumed-input position defines trailer. Re-read read-ahead without duplicate input charge", | |
| 36 | 추가: "Trailer little-endian CRC32 then ISIZE modulo 2^32 then exact EOF; reject concatenated members and any trailing bytes including zero" | |
| 37 | 추가: ], | |
| 38 | 추가: "limits": { | |
| 39 | 추가: "input_per_call": 16777216, | |
| 40 | 추가: "processed_cumulative": 134217728, | |
| 41 | 추가: "selected_output_cumulative": 16777216, | |
| 42 | 추가: "metadata_cumulative": 1048576, | |
| 43 | 추가: "duration_seconds": 300, | |
| 44 | 추가: "chunk_bytes": 65536, | |
| 45 | 추가: "caller_limits": "Clamp to hard caps. u64 byte limits allow zero; zero duration invalid. Caller may only lower effective caps.", | |
| 46 | 추가: "zero": "Input=0 rejects nonempty before parsing; metadata=0 rejects first header attempt; processed=0 rejects first input charge; output=0 still allows unselected decoding and selected empty payload.", | |
| 47 | 추가: "metadata": "Each attempted header byte is charged, including FHCRC; trailer excluded. Metadata charge can survive truncated input.", | |
| 48 | 추가: "processed": "Header input + bytes offered to decoder including read-ahead + any fresh trailer bytes + all decoded bytes, selected or not. Input high-water is per call; processed survives retries.", | |
| 49 | 추가: "failed_output": "Validate total_out delta <=65536 then charge actual delta even on decoder Err. A decoded overrun remains in usage and future checks reject. Input/output/metadata charges that would exceed limits fail before increment.", | |
| 50 | 추가: "allocation": "Charge selected output before try_reserve_exact and append; failure retains charge. Buffer size <=65536. No total allocator-memory or panic/OOM recovery guarantee." | |
| 51 | 추가: }, | |
| 52 | 추가: "precedence": [ | |
| 53 | 추가: "Start checkpoint (Cancelled/Deadline/Callback(E)) then shared processed/time check then input-length cap", | |
| 54 | 추가: "Ordinary header check then metadata charge then byte check/EOF/input charge. FHCRC metadata charge precedes byte check as in C#", | |
| 55 | 추가: "BeforeInflate checkpoint/time check; new offered-input charge before decoder call", | |
| 56 | 추가: "After decoder: invalid total_out delta => Boundary; valid actual decoded charge overrun => Budget; backend Err => Backend(original InflateError); only successful backend return reaches AfterInflate checkpoint/time check", | |
| 57 | 추가: "Successful call then consumed-input validation, CRC update, selected-output charge/reserve/copy, AfterOutput checkpoint", | |
| 58 | 추가: "StreamEnd checks, trailer CRC mismatch => Crc before Size before TrailingData; final decoder drop then BeforeReturn checkpoint/time check", | |
| 59 | 추가: "Any callback Stop::Callback(E) returned unchanged. No later checkpoints after an error. Returned first error retained through normal decoder drop." | |
| 60 | 추가: ], | |
| 61 | 추가: "progress": { | |
| 62 | 추가: "rule": "Each continued decoder call must consume >0 input or produce >0 output. Refill at exhausted window only, <=65536 bytes. Empty-input drain allowed for pending output. Zero progress without StreamEnd becomes Truncated if input exhausted, otherwise NoProgress.", | |
| 63 | 추가: "finite": "Input <=16MiB; all output charged against cumulative128MiB (at most one chunk observed overrun). No repeated zero-progress calls and no arbitrary call-count ceiling.", | |
| 64 | 추가: "limitations": "Typed backend has no IsNeedingInput property; zero-progress classification uses supplied/consumed positions. Exact SharpZipLib malformed-stream consumption and failure-cost identity is not claimed." | |
| 65 | 추가: }, | |
| 66 | 추가: "checkpoint": { | |
| 67 | 추가: "stages": [ | |
| 68 | 추가: "Start", | |
| 69 | 추가: "Header", | |
| 70 | 추가: "BeforeInflate", | |
| 71 | 추가: "AfterInflate", | |
| 72 | 추가: "AfterOutput", | |
| 73 | 추가: "Trailer", | |
| 74 | 추가: "BeforeReturn" | |
| 75 | 추가: ], | |
| 76 | 추가: "ordering": "Callback first, then Instant/processed check. Stop::Cancelled takes precedence over elapsed deadline at that check. Callback failure retains E; injected Stop::Deadline maps Budget.", | |
| 77 | 추가: "granularity": "Header checked per byte, raw input and output at <=64KiB, after output and after decoder cleanup. Callback time counts; callback/codec cannot be forcibly interrupted.", | |
| 78 | 추가: "observability": "Checkpoints expose usage, not provisional output. Stage calls are Rust API seams, not a claim of identical C# token poll counts." | |
| 79 | 추가: }, | |
| 80 | 추가: "errors": { | |
| 81 | 추가: "reader": [ | |
| 82 | 추가: "InvalidRequest", | |
| 83 | 추가: "Header", | |
| 84 | 추가: "Truncated", | |
| 85 | 추가: "Boundary", | |
| 86 | 추가: "Crc", | |
| 87 | 추가: "Size", | |
| 88 | 추가: "TrailingData", | |
| 89 | 추가: "Budget", | |
| 90 | 추가: "NoProgress", | |
| 91 | 추가: "Allocation", | |
| 92 | 추가: "Cancelled" | |
| 93 | 추가: ], | |
| 94 | 추가: "backend": "InflateError re-export, preserved NeedDict { dict_id }, StreamError, DataError, MemError", | |
| 95 | 추가: "callback": "Original generic E preserved; no message matching, catch/default or blanket unwind handling", | |
| 96 | 추가: "cleanup": "zlib-rs Drop runs decoder cleanup but does not expose its return status. No external sink Dispose in this API; no claim that cleanup failures, panic or OOM are recovered." | |
| 97 | 추가: }, | |
| 98 | 추가: "tests": { | |
| 99 | 추가: "status": "No regression execution in this implementation task. Separate regression owner to be assigned after fixed source head.", | |
| 100 | 추가: "preserve": [ | |
| 101 | 추가: "Normal/header options/FHCRC/CRC/ISIZE/truncation/trailing member rejection", | |
| 102 | 추가: "Budget exact boundaries/zero limits/failure charging/shared retries/cancellation/deadline/final-result discard" | |
| 103 | 추가: ], | |
| 104 | 추가: "adapt": [ | |
| 105 | 추가: "Partial feed and drain become adapter behavior cases, no arbitrary feed size public argument", | |
| 106 | 추가: "Sink cancellation becomes checkpoint cancellation; HashSink becomes test hashing of Completed bytes", | |
| 107 | 추가: "SharpZipLib backend failure becomes original typed zlib-rs error with separately tested counter/precedence behavior" | |
| 108 | 추가: ], | |
| 109 | 추가: "not_directly_applicable": [ | |
| 110 | 추가: "Mutable backing array with owned Box input", | |
| 111 | 추가: "External sink/Dispose exception and injected inflater counter corruption", | |
| 112 | 추가: "C# assembly identity, CLI, filesystem/Stream I/O" | |
| 113 | 추가: ], | |
| 114 | 추가: "not_claimed": [ | |
| 115 | 추가: "Existing C# 184+162 test counts or equivalent passes", | |
| 116 | 추가: "Independent execution, Windows, full-product acceptance, performance or vulnerability-free dependency" | |
| 117 | 추가: ] | |
| 118 | 추가: }, | |
| 119 | 추가: "out_of_scope": [ | |
| 120 | 추가: "tar.gz/TAR interpretation", | |
| 121 | 추가: "multiple members", | |
| 122 | 추가: "disk publishing", | |
| 123 | 추가: "worker/IPC/UI", | |
| 124 | 추가: "encryption", | |
| 125 | 추가: "main merge", | |
| 126 | 추가: "C# deletion" | |
| 127 | 추가: ], | |
| 128 | 추가: "validation": { | |
| 129 | 추가: "toolchain": "Rust/Cargo 1.99.1", | |
| 130 | 추가: "build": "cargo +1.99.1 build --lib --locked --offline --manifest-path experiments/rust-memory-gzip-reader/Cargo.toml", | |
| 131 | 추가: "target_directory": "tests/memory-gzip-build/target", | |
| 132 | 추가: "build_exit": 0, | |
| 133 | 추가: "regression_tests_executed": 0, | |
| 134 | 추가: "format": "cargo +1.99.1 fmt --manifest-path experiments/rust-memory-gzip-reader/Cargo.toml", | |
| 135 | 추가: "security_advisories": "Not checked in this implementation pass; no current advisory or vulnerability-free claim" | |
| 136 | 추가: } | |
| 137 | 추가: } |
| @@ -0,0 +1,35 @@ | ||
| 1 | 추가: { | |
| 2 | 추가: "direct": [ | |
| 3 | 추가: { | |
| 4 | 추가: "name": "zlib-rs", | |
| 5 | 추가: "version": "0.6.8", | |
| 6 | 추가: "default_features": false, | |
| 7 | 추가: "features": [ | |
| 8 | 추가: "rust-allocator" | |
| 9 | 추가: ], | |
| 10 | 추가: "license": "Zlib", | |
| 11 | 추가: "source": "registry+https://github.com/rust-lang/crates.io-index", | |
| 12 | 추가: "archive_sha256": "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112", | |
| 13 | 추가: "verified_archive_source_files": { | |
| 14 | 추가: "Cargo.toml": "85e89ef7d52508403f921caba72bb16545815e928083b6ab4ffecb8bf65c96b6", | |
| 15 | 추가: "LICENSE": "e72111c52b7d96ebe25348dee19f0744f444d3c95ae6b1ecb6ccaecc5bce05ba", | |
| 16 | 추가: "src/lib.rs": "30eeb6cf4b939db03130340b4ed83432c1710085445dfab12c820b8acc90708f", | |
| 17 | 추가: "src/stable.rs": "ba6f0d5ce10aaa274b30103bc86c0766068b2dc62d7faafced1df8c782c0c703", | |
| 18 | 추가: "src/crc32.rs": "2e5fde22a4fc26bd7a9e87a8716c7c7a381d8ee2da8763e8440c8cec0049681b" | |
| 19 | 추가: } | |
| 20 | 추가: } | |
| 21 | 추가: ], | |
| 22 | 추가: "selected_external_transitive": [], | |
| 23 | 추가: "selected_features": [ | |
| 24 | 추가: "zlib-rs/rust-allocator" | |
| 25 | 추가: ], | |
| 26 | 추가: "not_selected": [ | |
| 27 | 추가: "std", | |
| 28 | 추가: "c-allocator", | |
| 29 | 추가: "arbitrary", | |
| 30 | 추가: "quickcheck", | |
| 31 | 추가: "__internal-api" | |
| 32 | 추가: ], | |
| 33 | 추가: "lock_external_packages": 1, | |
| 34 | 추가: "notice_scope": "Single selected external package for this candidate, not final all-platform product distribution notices." | |
| 35 | 추가: } |
| @@ -0,0 +1,120 @@ | ||
| 1 | 추가: use crate::Failure; | |
| 2 | 추가: use std::time::{Duration, Instant}; | |
| 3 | 추가: | |
| 4 | 추가: pub const CHUNK_SIZE: usize = 65_536; | |
| 5 | 추가: | |
| 6 | 추가: #[derive(Clone, Copy, Debug)] | |
| 7 | 추가: pub struct Limits { | |
| 8 | 추가: pub input: u64, | |
| 9 | 추가: pub processed: u64, | |
| 10 | 추가: pub output: u64, | |
| 11 | 추가: pub metadata: u64, | |
| 12 | 추가: pub duration: Duration, | |
| 13 | 추가: } | |
| 14 | 추가: | |
| 15 | 추가: impl Default for Limits { | |
| 16 | 추가: fn default() -> Self { | |
| 17 | 추가: Self { | |
| 18 | 추가: input: 16 * 1024 * 1024, | |
| 19 | 추가: processed: 128 * 1024 * 1024, | |
| 20 | 추가: output: 16 * 1024 * 1024, | |
| 21 | 추가: metadata: 1024 * 1024, | |
| 22 | 추가: duration: Duration::from_secs(300), | |
| 23 | 추가: } | |
| 24 | 추가: } | |
| 25 | 추가: } | |
| 26 | 추가: | |
| 27 | 추가: #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | |
| 28 | 추가: pub struct Usage { | |
| 29 | 추가: pub processed: u64, | |
| 30 | 추가: pub output: u64, | |
| 31 | 추가: pub metadata: u64, | |
| 32 | 추가: } | |
| 33 | 추가: | |
| 34 | 추가: #[derive(Debug)] | |
| 35 | 추가: pub struct Budget { | |
| 36 | 추가: limits: Limits, | |
| 37 | 추가: usage: Usage, | |
| 38 | 추가: started: Instant, | |
| 39 | 추가: } | |
| 40 | 추가: | |
| 41 | 추가: impl Budget { | |
| 42 | 추가: pub fn new(requested: Limits) -> Result<Self, Failure> { | |
| 43 | 추가: if requested.duration.is_zero() { | |
| 44 | 추가: return Err(Failure::InvalidRequest); | |
| 45 | 추가: } | |
| 46 | 추가: let cap = Limits::default(); | |
| 47 | 추가: Ok(Self { | |
| 48 | 추가: limits: Limits { | |
| 49 | 추가: input: requested.input.min(cap.input), | |
| 50 | 추가: processed: requested.processed.min(cap.processed), | |
| 51 | 추가: output: requested.output.min(cap.output), | |
| 52 | 추가: metadata: requested.metadata.min(cap.metadata), | |
| 53 | 추가: duration: requested.duration.min(cap.duration), | |
| 54 | 추가: }, | |
| 55 | 추가: usage: Usage::default(), | |
| 56 | 추가: started: Instant::now(), | |
| 57 | 추가: }) | |
| 58 | 추가: } | |
| 59 | 추가: | |
| 60 | 추가: pub fn limits(&self) -> Limits { | |
| 61 | 추가: self.limits | |
| 62 | 추가: } | |
| 63 | 추가: pub fn usage(&self) -> Usage { | |
| 64 | 추가: self.usage | |
| 65 | 추가: } | |
| 66 | 추가: pub fn elapsed(&self) -> Duration { | |
| 67 | 추가: self.started.elapsed() | |
| 68 | 추가: } | |
| 69 | 추가: | |
| 70 | 추가: pub(crate) fn check(&self) -> Result<(), Failure> { | |
| 71 | 추가: if self.usage.processed > self.limits.processed || self.elapsed() >= self.limits.duration { | |
| 72 | 추가: return Err(Failure::Budget); | |
| 73 | 추가: } | |
| 74 | 추가: Ok(()) | |
| 75 | 추가: } | |
| 76 | 추가: | |
| 77 | 추가: pub(crate) fn input(&mut self, count: usize) -> Result<(), Failure> { | |
| 78 | 추가: charge( | |
| 79 | 추가: &mut self.usage.processed, | |
| 80 | 추가: count as u64, | |
| 81 | 추가: self.limits.processed, | |
| 82 | 추가: ) | |
| 83 | 추가: } | |
| 84 | 추가: | |
| 85 | 추가: pub(crate) fn decoded(&mut self, count: u64) -> Result<(), Failure> { | |
| 86 | 추가: if count > CHUNK_SIZE as u64 { | |
| 87 | 추가: return Err(Failure::Boundary); | |
| 88 | 추가: } | |
| 89 | 추가: if self.usage.processed > self.limits.processed { | |
| 90 | 추가: return Err(Failure::Budget); | |
| 91 | 추가: } | |
| 92 | 추가: // 이미 생성된 출력은 한도 초과 시에도 누적함. | |
| 93 | 추가: self.usage.processed = self | |
| 94 | 추가: .usage | |
| 95 | 추가: .processed | |
| 96 | 추가: .checked_add(count) | |
| 97 | 추가: .ok_or(Failure::Boundary)?; | |
| 98 | 추가: if self.usage.processed > self.limits.processed { | |
| 99 | 추가: return Err(Failure::Budget); | |
| 100 | 추가: } | |
| 101 | 추가: Ok(()) | |
| 102 | 추가: } | |
| 103 | 추가: | |
| 104 | 추가: pub(crate) fn output(&mut self, count: usize) -> Result<(), Failure> { | |
| 105 | 추가: charge(&mut self.usage.output, count as u64, self.limits.output) | |
| 106 | 추가: } | |
| 107 | 추가: | |
| 108 | 추가: pub(crate) fn metadata(&mut self) -> Result<(), Failure> { | |
| 109 | 추가: charge(&mut self.usage.metadata, 1, self.limits.metadata) | |
| 110 | 추가: } | |
| 111 | 추가: } | |
| 112 | 추가: | |
| 113 | 추가: fn charge(value: &mut u64, count: u64, limit: u64) -> Result<(), Failure> { | |
| 114 | 추가: let next = value.checked_add(count).ok_or(Failure::Budget)?; | |
| 115 | 추가: if next > limit { | |
| 116 | 추가: return Err(Failure::Budget); | |
| 117 | 추가: } | |
| 118 | 추가: *value = next; | |
| 119 | 추가: Ok(()) | |
| 120 | 추가: } |
| @@ -0,0 +1,331 @@ | ||
| 1 | 추가: #![forbid(unsafe_code)] | |
| 2 | 추가: | |
| 3 | 추가: mod budget; | |
| 4 | 추가: pub use budget::{Budget, CHUNK_SIZE, Limits, Usage}; | |
| 5 | 추가: pub use zlib_rs::InflateError; | |
| 6 | 추가: use zlib_rs::{Inflate, InflateFlush, Status, crc32::crc32}; | |
| 7 | 추가: | |
| 8 | 추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 9 | 추가: pub enum Failure { | |
| 10 | 추가: InvalidRequest, | |
| 11 | 추가: Header, | |
| 12 | 추가: Truncated, | |
| 13 | 추가: Boundary, | |
| 14 | 추가: Crc, | |
| 15 | 추가: Size, | |
| 16 | 추가: TrailingData, | |
| 17 | 추가: Budget, | |
| 18 | 추가: NoProgress, | |
| 19 | 추가: Allocation, | |
| 20 | 추가: Cancelled, | |
| 21 | 추가: } | |
| 22 | 추가: | |
| 23 | 추가: #[derive(Debug)] | |
| 24 | 추가: pub enum Error<E> { | |
| 25 | 추가: Reader(Failure), | |
| 26 | 추가: Backend(InflateError), | |
| 27 | 추가: Callback(E), | |
| 28 | 추가: } | |
| 29 | 추가: | |
| 30 | 추가: impl<E> From<Failure> for Error<E> { | |
| 31 | 추가: fn from(value: Failure) -> Self { | |
| 32 | 추가: Self::Reader(value) | |
| 33 | 추가: } | |
| 34 | 추가: } | |
| 35 | 추가: | |
| 36 | 추가: #[derive(Debug)] | |
| 37 | 추가: pub enum Stop<E> { | |
| 38 | 추가: Cancelled, | |
| 39 | 추가: Deadline, | |
| 40 | 추가: Callback(E), | |
| 41 | 추가: } | |
| 42 | 추가: | |
| 43 | 추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 44 | 추가: pub enum Stage { | |
| 45 | 추가: Start, | |
| 46 | 추가: Header, | |
| 47 | 추가: BeforeInflate, | |
| 48 | 추가: AfterInflate, | |
| 49 | 추가: AfterOutput, | |
| 50 | 추가: Trailer, | |
| 51 | 추가: BeforeReturn, | |
| 52 | 추가: } | |
| 53 | 추가: | |
| 54 | 추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 55 | 추가: pub struct Checkpoint { | |
| 56 | 추가: pub stage: Stage, | |
| 57 | 추가: pub usage: Usage, | |
| 58 | 추가: } | |
| 59 | 추가: | |
| 60 | 추가: pub struct Request { | |
| 61 | 추가: pub input: Box<[u8]>, | |
| 62 | 추가: pub selected: bool, | |
| 63 | 추가: } | |
| 64 | 추가: | |
| 65 | 추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 66 | 추가: pub struct Boundary { | |
| 67 | 추가: pub header_bytes: usize, | |
| 68 | 추가: pub deflate_bytes: u64, | |
| 69 | 추가: pub read_ahead_bytes: usize, | |
| 70 | 추가: pub inflated_bytes: u64, | |
| 71 | 추가: pub crc32: u32, | |
| 72 | 추가: pub member_bytes: usize, | |
| 73 | 추가: pub offered_bytes: u64, | |
| 74 | 추가: } | |
| 75 | 추가: | |
| 76 | 추가: #[derive(Debug)] | |
| 77 | 추가: pub struct Completed { | |
| 78 | 추가: pub boundary: Boundary, | |
| 79 | 추가: pub bytes: Option<Vec<u8>>, | |
| 80 | 추가: pub usage: Usage, | |
| 81 | 추가: } | |
| 82 | 추가: | |
| 83 | 추가: pub fn read<E>( | |
| 84 | 추가: request: Request, | |
| 85 | 추가: budget: &mut Budget, | |
| 86 | 추가: mut checkpoint: impl FnMut(Checkpoint) -> Result<(), Stop<E>>, | |
| 87 | 추가: ) -> Result<Completed, Error<E>> { | |
| 88 | 추가: let mut context = Context { | |
| 89 | 추가: budget, | |
| 90 | 추가: checkpoint: &mut checkpoint, | |
| 91 | 추가: }; | |
| 92 | 추가: context.check(Stage::Start)?; | |
| 93 | 추가: if request.input.len() as u64 > context.budget.limits().input { | |
| 94 | 추가: return Err(Failure::Budget.into()); | |
| 95 | 추가: } | |
| 96 | 추가: let mut cursor = Cursor { | |
| 97 | 추가: input: &request.input, | |
| 98 | 추가: position: 0, | |
| 99 | 추가: high_water: 0, | |
| 100 | 추가: }; | |
| 101 | 추가: let header_bytes = header(&mut cursor, &mut context)?; | |
| 102 | 추가: let mut bytes = request.selected.then(Vec::new); | |
| 103 | 추가: let boundary = { | |
| 104 | 추가: let mut decoder = Inflate::new(false, 15); | |
| 105 | 추가: decode( | |
| 106 | 추가: &mut decoder, | |
| 107 | 추가: &mut cursor, | |
| 108 | 추가: &mut context, | |
| 109 | 추가: header_bytes, | |
| 110 | 추가: &mut bytes, | |
| 111 | 추가: )? | |
| 112 | 추가: }; | |
| 113 | 추가: // decoder 정리 이후에만 완료 결과를 노출함. | |
| 114 | 추가: context.check(Stage::BeforeReturn)?; | |
| 115 | 추가: Ok(Completed { | |
| 116 | 추가: boundary, | |
| 117 | 추가: bytes, | |
| 118 | 추가: usage: context.budget.usage(), | |
| 119 | 추가: }) | |
| 120 | 추가: } | |
| 121 | 추가: | |
| 122 | 추가: struct Context<'a, F> { | |
| 123 | 추가: budget: &'a mut Budget, | |
| 124 | 추가: checkpoint: &'a mut F, | |
| 125 | 추가: } | |
| 126 | 추가: | |
| 127 | 추가: impl<E, F: FnMut(Checkpoint) -> Result<(), Stop<E>>> Context<'_, F> { | |
| 128 | 추가: fn check(&mut self, stage: Stage) -> Result<(), Error<E>> { | |
| 129 | 추가: (self.checkpoint)(Checkpoint { | |
| 130 | 추가: stage, | |
| 131 | 추가: usage: self.budget.usage(), | |
| 132 | 추가: }) | |
| 133 | 추가: .map_err(|stop| match stop { | |
| 134 | 추가: Stop::Cancelled => Error::Reader(Failure::Cancelled), | |
| 135 | 추가: Stop::Deadline => Error::Reader(Failure::Budget), | |
| 136 | 추가: Stop::Callback(error) => Error::Callback(error), | |
| 137 | 추가: })?; | |
| 138 | 추가: self.budget.check()?; | |
| 139 | 추가: Ok(()) | |
| 140 | 추가: } | |
| 141 | 추가: } | |
| 142 | 추가: | |
| 143 | 추가: struct Cursor<'a> { | |
| 144 | 추가: input: &'a [u8], | |
| 145 | 추가: position: usize, | |
| 146 | 추가: high_water: usize, | |
| 147 | 추가: } | |
| 148 | 추가: | |
| 149 | 추가: impl Cursor<'_> { | |
| 150 | 추가: fn account(&mut self, end: usize, budget: &mut Budget) -> Result<(), Failure> { | |
| 151 | 추가: if end > self.input.len() { | |
| 152 | 추가: return Err(Failure::Boundary); | |
| 153 | 추가: } | |
| 154 | 추가: if end > self.high_water { | |
| 155 | 추가: budget.input(end - self.high_water)?; | |
| 156 | 추가: self.high_water = end; | |
| 157 | 추가: } | |
| 158 | 추가: Ok(()) | |
| 159 | 추가: } | |
| 160 | 추가: | |
| 161 | 추가: fn byte<E, F: FnMut(Checkpoint) -> Result<(), Stop<E>>>( | |
| 162 | 추가: &mut self, | |
| 163 | 추가: context: &mut Context<'_, F>, | |
| 164 | 추가: stage: Stage, | |
| 165 | 추가: ) -> Result<u8, Error<E>> { | |
| 166 | 추가: context.check(stage)?; | |
| 167 | 추가: let value = *self.input.get(self.position).ok_or(Failure::Truncated)?; | |
| 168 | 추가: let end = self.position.checked_add(1).ok_or(Failure::Boundary)?; | |
| 169 | 추가: self.account(end, context.budget)?; | |
| 170 | 추가: self.position = end; | |
| 171 | 추가: Ok(value) | |
| 172 | 추가: } | |
| 173 | 추가: } | |
| 174 | 추가: | |
| 175 | 추가: fn header<E, F: FnMut(Checkpoint) -> Result<(), Stop<E>>>( | |
| 176 | 추가: cursor: &mut Cursor<'_>, | |
| 177 | 추가: context: &mut Context<'_, F>, | |
| 178 | 추가: ) -> Result<usize, Error<E>> { | |
| 179 | 추가: let mut crc = 0; | |
| 180 | 추가: fn next<E, F: FnMut(Checkpoint) -> Result<(), Stop<E>>>( | |
| 181 | 추가: cursor: &mut Cursor<'_>, | |
| 182 | 추가: context: &mut Context<'_, F>, | |
| 183 | 추가: crc: &mut u32, | |
| 184 | 추가: ) -> Result<u8, Error<E>> { | |
| 185 | 추가: context.check(Stage::Header)?; | |
| 186 | 추가: context.budget.metadata()?; | |
| 187 | 추가: let value = cursor.byte(context, Stage::Header)?; | |
| 188 | 추가: *crc = crc32(*crc, &[value]); | |
| 189 | 추가: Ok(value) | |
| 190 | 추가: } | |
| 191 | 추가: for expected in [0x1f, 0x8b, 8] { | |
| 192 | 추가: if next(cursor, context, &mut crc)? != expected { | |
| 193 | 추가: return Err(Failure::Header.into()); | |
| 194 | 추가: } | |
| 195 | 추가: } | |
| 196 | 추가: let flags = next(cursor, context, &mut crc)?; | |
| 197 | 추가: if flags & 0xe0 != 0 { | |
| 198 | 추가: return Err(Failure::Header.into()); | |
| 199 | 추가: } | |
| 200 | 추가: for _ in 0..6 { | |
| 201 | 추가: next(cursor, context, &mut crc)?; | |
| 202 | 추가: } | |
| 203 | 추가: if flags & 4 != 0 { | |
| 204 | 추가: let low = next(cursor, context, &mut crc)?; | |
| 205 | 추가: let high = next(cursor, context, &mut crc)?; | |
| 206 | 추가: for _ in 0..u16::from_le_bytes([low, high]) { | |
| 207 | 추가: next(cursor, context, &mut crc)?; | |
| 208 | 추가: } | |
| 209 | 추가: } | |
| 210 | 추가: for mask in [8, 16] { | |
| 211 | 추가: if flags & mask != 0 { | |
| 212 | 추가: while next(cursor, context, &mut crc)? != 0 {} | |
| 213 | 추가: } | |
| 214 | 추가: } | |
| 215 | 추가: if flags & 2 != 0 { | |
| 216 | 추가: context.budget.metadata()?; | |
| 217 | 추가: let low = cursor.byte(context, Stage::Header)?; | |
| 218 | 추가: context.budget.metadata()?; | |
| 219 | 추가: let high = cursor.byte(context, Stage::Header)?; | |
| 220 | 추가: if u16::from_le_bytes([low, high]) != crc as u16 { | |
| 221 | 추가: return Err(Failure::Header.into()); | |
| 222 | 추가: } | |
| 223 | 추가: } | |
| 224 | 추가: Ok(cursor.position) | |
| 225 | 추가: } | |
| 226 | 추가: | |
| 227 | 추가: fn decode<E, F: FnMut(Checkpoint) -> Result<(), Stop<E>>>( | |
| 228 | 추가: decoder: &mut Inflate, | |
| 229 | 추가: cursor: &mut Cursor<'_>, | |
| 230 | 추가: context: &mut Context<'_, F>, | |
| 231 | 추가: header_bytes: usize, | |
| 232 | 추가: bytes: &mut Option<Vec<u8>>, | |
| 233 | 추가: ) -> Result<Boundary, Error<E>> { | |
| 234 | 추가: let mut output = [0; CHUNK_SIZE]; | |
| 235 | 추가: let mut consumed_position = header_bytes; | |
| 236 | 추가: let mut offered_end = header_bytes; | |
| 237 | 추가: let mut crc = 0; | |
| 238 | 추가: loop { | |
| 239 | 추가: context.check(Stage::BeforeInflate)?; | |
| 240 | 추가: if consumed_position == offered_end && offered_end < cursor.input.len() { | |
| 241 | 추가: offered_end = offered_end | |
| 242 | 추가: .checked_add(CHUNK_SIZE) | |
| 243 | 추가: .ok_or(Failure::Boundary)? | |
| 244 | 추가: .min(cursor.input.len()); | |
| 245 | 추가: cursor.account(offered_end, context.budget)?; | |
| 246 | 추가: cursor.position = offered_end; | |
| 247 | 추가: } | |
| 248 | 추가: let before_in = decoder.total_in(); | |
| 249 | 추가: let before_out = decoder.total_out(); | |
| 250 | 추가: let result = decoder.decompress( | |
| 251 | 추가: &cursor.input[consumed_position..offered_end], | |
| 252 | 추가: &mut output, | |
| 253 | 추가: InflateFlush::NoFlush, | |
| 254 | 추가: ); | |
| 255 | 추가: // backend 오류보다 실제 출력 증가분 검증과 과금을 먼저 수행함. | |
| 256 | 추가: let produced = decoder | |
| 257 | 추가: .total_out() | |
| 258 | 추가: .checked_sub(before_out) | |
| 259 | 추가: .ok_or(Failure::Boundary)?; | |
| 260 | 추가: context.budget.decoded(produced)?; | |
| 261 | 추가: let status = result.map_err(Error::Backend)?; | |
| 262 | 추가: context.check(Stage::AfterInflate)?; | |
| 263 | 추가: let consumed = decoder | |
| 264 | 추가: .total_in() | |
| 265 | 추가: .checked_sub(before_in) | |
| 266 | 추가: .ok_or(Failure::Boundary)?; | |
| 267 | 추가: let consumed = usize::try_from(consumed).map_err(|_| Failure::Boundary)?; | |
| 268 | 추가: if consumed > offered_end - consumed_position { | |
| 269 | 추가: return Err(Failure::Boundary.into()); | |
| 270 | 추가: } | |
| 271 | 추가: consumed_position = consumed_position | |
| 272 | 추가: .checked_add(consumed) | |
| 273 | 추가: .ok_or(Failure::Boundary)?; | |
| 274 | 추가: if decoder.total_in() != (consumed_position - header_bytes) as u64 { | |
| 275 | 추가: return Err(Failure::Boundary.into()); | |
| 276 | 추가: } | |
| 277 | 추가: let produced = produced as usize; | |
| 278 | 추가: if produced != 0 { | |
| 279 | 추가: crc = crc32(crc, &output[..produced]); | |
| 280 | 추가: if let Some(saved) = bytes { | |
| 281 | 추가: context.budget.output(produced)?; | |
| 282 | 추가: saved | |
| 283 | 추가: .try_reserve_exact(produced) | |
| 284 | 추가: .map_err(|_| Failure::Allocation)?; | |
| 285 | 추가: saved.extend_from_slice(&output[..produced]); | |
| 286 | 추가: context.check(Stage::AfterOutput)?; | |
| 287 | 추가: } | |
| 288 | 추가: } | |
| 289 | 추가: if status == Status::StreamEnd { | |
| 290 | 추가: break; | |
| 291 | 추가: } | |
| 292 | 추가: if consumed == 0 && produced == 0 { | |
| 293 | 추가: return Err(if consumed_position == cursor.input.len() { | |
| 294 | 추가: Failure::Truncated | |
| 295 | 추가: } else { | |
| 296 | 추가: Failure::NoProgress | |
| 297 | 추가: } | |
| 298 | 추가: .into()); | |
| 299 | 추가: } | |
| 300 | 추가: } | |
| 301 | 추가: context.check(Stage::Trailer)?; | |
| 302 | 추가: let read_ahead_bytes = offered_end | |
| 303 | 추가: .checked_sub(consumed_position) | |
| 304 | 추가: .ok_or(Failure::Boundary)?; | |
| 305 | 추가: cursor.position = consumed_position; | |
| 306 | 추가: let mut trailer = [0; 8]; | |
| 307 | 추가: for byte in &mut trailer { | |
| 308 | 추가: *byte = cursor.byte(context, Stage::Trailer)?; | |
| 309 | 추가: } | |
| 310 | 추가: let expected_crc = u32::from_le_bytes([trailer[0], trailer[1], trailer[2], trailer[3]]); | |
| 311 | 추가: let expected_size = u32::from_le_bytes([trailer[4], trailer[5], trailer[6], trailer[7]]); | |
| 312 | 추가: if expected_crc != crc { | |
| 313 | 추가: return Err(Failure::Crc.into()); | |
| 314 | 추가: } | |
| 315 | 추가: if expected_size != decoder.total_out() as u32 { | |
| 316 | 추가: return Err(Failure::Size.into()); | |
| 317 | 추가: } | |
| 318 | 추가: if cursor.position != cursor.input.len() { | |
| 319 | 추가: return Err(Failure::TrailingData.into()); | |
| 320 | 추가: } | |
| 321 | 추가: context.check(Stage::Trailer)?; | |
| 322 | 추가: Ok(Boundary { | |
| 323 | 추가: header_bytes, | |
| 324 | 추가: deflate_bytes: decoder.total_in(), | |
| 325 | 추가: read_ahead_bytes, | |
| 326 | 추가: inflated_bytes: decoder.total_out(), | |
| 327 | 추가: crc32: crc, | |
| 328 | 추가: member_bytes: cursor.position, | |
| 329 | 추가: offered_bytes: (offered_end - header_bytes) as u64, | |
| 330 | 추가: }) | |
| 331 | 추가: } |