| ... | ... | @@ -77,7 +77,7 @@ pub fn decodeAlloc( |
| 77 | 77 | |
| 78 | 78 | var read_count: usize = 0; |
| 79 | 79 | while (read_count < src.len) { |
| 80 | | read_count += try decodeZstandardFrameArrayList( |
| 80 | read_count += try decodeFrameArrayList( |
| 81 | 81 | allocator, |
| 82 | 82 | &result, |
| 83 | 83 | src[read_count..], |
| ... | ... | @@ -140,8 +140,7 @@ pub const DecodedFrame = union(enum) { |
| 140 | 140 | }; |
| 141 | 141 | |
| 142 | 142 | /// Decodes the frame at the start of `src` into `dest`. Returns the number of |
| 143 | | /// bytes read from `src` and the decoded bytes for a Zstandard frame, or the |
| 144 | | /// frame header for a Skippable frame. |
| 143 | /// bytes read from `src`. |
| 145 | 144 | /// |
| 146 | 145 | /// Errors returned: |
| 147 | 146 | /// - `error.BadMagic` if the first 4 bytes of `src` is not a valid magic |
| ... | ... | @@ -160,30 +159,24 @@ pub const DecodedFrame = union(enum) { |
| 160 | 159 | /// - an error in `block.Error` if there are errors decoding a block |
| 161 | 160 | /// - `error.SkippableSizeTooLarge` if the frame is skippable and reports a |
| 162 | 161 | /// size greater than `src.len` |
| 163 | | pub fn decodeFrameAlloc( |
| 162 | pub fn decodeFrameArrayList( |
| 164 | 163 | allocator: Allocator, |
| 164 | dest: *std.ArrayList(u8), |
| 165 | 165 | src: []const u8, |
| 166 | 166 | verify_checksum: bool, |
| 167 | 167 | window_size_max: usize, |
| 168 | | ) !DecodedFrame { |
| 168 | ) !usize { |
| 169 | 169 | var fbs = std.io.fixedBufferStream(src); |
| 170 | 170 | const reader = fbs.reader(); |
| 171 | 171 | const magic = try reader.readIntLittle(u32); |
| 172 | 172 | switch (try frameType(magic)) { |
| 173 | | .zstandard => return .{ |
| 174 | | .zstandard = try decodeZstandardFrameAlloc(allocator, src, verify_checksum, window_size_max), |
| 175 | | }, |
| 173 | .zstandard => return decodeZstandardFrameArrayList(allocator, dest, src, verify_checksum, window_size_max), |
| 176 | 174 | .skippable => { |
| 177 | 175 | const content_size = try fbs.reader().readIntLittle(u32); |
| 178 | 176 | if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge; |
| 179 | 177 | const read_count = @as(usize, content_size) + 8; |
| 180 | 178 | if (read_count > src.len) return error.SkippableSizeTooLarge; |
| 181 | | return .{ |
| 182 | | .skippable = .{ |
| 183 | | .magic_number = magic, |
| 184 | | .frame_size = content_size, |
| 185 | | }, |
| 186 | | }; |
| 179 | return read_count; |
| 187 | 180 | }, |
| 188 | 181 | } |
| 189 | 182 | } |
| ... | ... | @@ -319,11 +312,9 @@ pub const FrameContext = struct { |
| 319 | 312 | } |
| 320 | 313 | }; |
| 321 | 314 | |
| 322 | | /// Decode a Zstandard from from `src` and return the decompressed bytes and the |
| 323 | | /// number of bytes read; see `decodeZstandardFrame()`. `allocator` is used to |
| 324 | | /// allocate both the returned slice and internal buffers used during decoding. |
| 325 | | /// The first four bytes of `src` must be the magic number for a Zstandard |
| 326 | | /// frame. |
| 315 | /// Decode a Zstandard from from `src` and return number of bytes read; see |
| 316 | /// `decodeZstandardFrame()`. The first four bytes of `src` must be the magic |
| 317 | /// number for a Zstandard frame. |
| 327 | 318 | /// |
| 328 | 319 | /// Errors returned: |
| 329 | 320 | /// - `error.WindowSizeUnknown` if the frame does not have a valid window size |
| ... | ... | @@ -340,25 +331,6 @@ pub const FrameContext = struct { |
| 340 | 331 | /// - an error in `block.Error` if there are errors decoding a block |
| 341 | 332 | /// - `error.BadContentSize` if the content size declared by the frame does |
| 342 | 333 | /// not equal the size of decompressed data |
| 343 | | pub fn decodeZstandardFrameAlloc( |
| 344 | | allocator: Allocator, |
| 345 | | src: []const u8, |
| 346 | | verify_checksum: bool, |
| 347 | | window_size_max: usize, |
| 348 | | ) (error{OutOfMemory} || FrameContext.Error || FrameError)!DecodeResult { |
| 349 | | var result = std.ArrayList(u8).init(allocator); |
| 350 | | errdefer result.deinit(); |
| 351 | | const read_count = try decodeZstandardFrameArrayList( |
| 352 | | allocator, |
| 353 | | &result, |
| 354 | | src, |
| 355 | | verify_checksum, |
| 356 | | window_size_max, |
| 357 | | ); |
| 358 | | return DecodeResult{ .bytes = try result.toOwnedSlice(), .read_count = read_count }; |
| 359 | | } |
| 360 | | |
| 361 | | /// Decode a ZStandard frame into `dest`; see `decodeZStandardFrameAlloc()`. |
| 362 | 334 | pub fn decodeZstandardFrameArrayList( |
| 363 | 335 | allocator: Allocator, |
| 364 | 336 | dest: *std.ArrayList(u8), |