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