| ... | @@ -41,7 +41,7 @@ pub fn decodeFrameType(source: anytype) error{ BadMagic, EndOfStream }!frame.Kin | ... | @@ -41,7 +41,7 @@ pub fn decodeFrameType(source: anytype) error{ BadMagic, EndOfStream }!frame.Kin |
| 41 | /// Errors returned: | 41 | /// Errors returned: |
| 42 | /// - `error.BadMagic` if `magic` is not a valid magic number. | 42 | /// - `error.BadMagic` if `magic` is not a valid magic number. |
| 43 | pub fn frameType(magic: u32) error{BadMagic}!frame.Kind { | 43 | pub fn frameType(magic: u32) error{BadMagic}!frame.Kind { |
| 44 | return if (magic == frame.ZStandard.magic_number) | 44 | return if (magic == frame.Zstandard.magic_number) |
| 45 | .zstandard | 45 | .zstandard |
| 46 | else if (isSkippableMagic(magic)) | 46 | else if (isSkippableMagic(magic)) |
| 47 | .skippable | 47 | .skippable |
| ... | @@ -79,7 +79,7 @@ pub fn decodeFrame( | ... | @@ -79,7 +79,7 @@ pub fn decodeFrame( |
| 79 | ) !ReadWriteCount { | 79 | ) !ReadWriteCount { |
| 80 | var fbs = std.io.fixedBufferStream(src); | 80 | var fbs = std.io.fixedBufferStream(src); |
| 81 | return switch (try decodeFrameType(fbs.reader())) { | 81 | return switch (try decodeFrameType(fbs.reader())) { |
| 82 | .zstandard => decodeZStandardFrame(dest, src, verify_checksum), | 82 | .zstandard => decodeZstandardFrame(dest, src, verify_checksum), |
| 83 | .skippable => ReadWriteCount{ | 83 | .skippable => ReadWriteCount{ |
| 84 | .read_count = try fbs.reader().readIntLittle(u32) + 8, | 84 | .read_count = try fbs.reader().readIntLittle(u32) + 8, |
| 85 | .write_count = 0, | 85 | .write_count = 0, |
| ... | @@ -126,7 +126,7 @@ pub fn decodeFrameAlloc( | ... | @@ -126,7 +126,7 @@ pub fn decodeFrameAlloc( |
| 126 | const magic = try reader.readIntLittle(u32); | 126 | const magic = try reader.readIntLittle(u32); |
| 127 | return switch (try frameType(magic)) { | 127 | return switch (try frameType(magic)) { |
| 128 | .zstandard => .{ | 128 | .zstandard => .{ |
| 129 | .zstandard = try decodeZStandardFrameAlloc(allocator, src, verify_checksum, window_size_max), | 129 | .zstandard = try decodeZstandardFrameAlloc(allocator, src, verify_checksum, window_size_max), |
| 130 | }, | 130 | }, |
| 131 | .skippable => .{ | 131 | .skippable => .{ |
| 132 | .skippable = .{ | 132 | .skippable = .{ |
| ... | @@ -166,17 +166,17 @@ const FrameError = error{ | ... | @@ -166,17 +166,17 @@ const FrameError = error{ |
| 166 | /// - `error.UnusedBitSet` if the unused bit of the frame header is set | 166 | /// - `error.UnusedBitSet` if the unused bit of the frame header is set |
| 167 | /// - `error.EndOfStream` if `src` does not contain a complete frame | 167 | /// - `error.EndOfStream` if `src` does not contain a complete frame |
| 168 | /// - an error in `block.Error` if there are errors decoding a block | 168 | /// - an error in `block.Error` if there are errors decoding a block |
| 169 | pub fn decodeZStandardFrame( | 169 | pub fn decodeZstandardFrame( |
| 170 | dest: []u8, | 170 | dest: []u8, |
| 171 | src: []const u8, | 171 | src: []const u8, |
| 172 | verify_checksum: bool, | 172 | verify_checksum: bool, |
| 173 | ) (error{ UnknownContentSizeUnsupported, ContentTooLarge } || FrameError)!ReadWriteCount { | 173 | ) (error{ UnknownContentSizeUnsupported, ContentTooLarge } || FrameError)!ReadWriteCount { |
| 174 | assert(readInt(u32, src[0..4]) == frame.ZStandard.magic_number); | 174 | assert(readInt(u32, src[0..4]) == frame.Zstandard.magic_number); |
| 175 | var consumed_count: usize = 4; | 175 | var consumed_count: usize = 4; |
| 176 | | 176 | |
| 177 | var fbs = std.io.fixedBufferStream(src[consumed_count..]); | 177 | var fbs = std.io.fixedBufferStream(src[consumed_count..]); |
| 178 | var source = fbs.reader(); | 178 | var source = fbs.reader(); |
| 179 | const frame_header = try decodeZStandardHeader(source); | 179 | const frame_header = try decodeZstandardHeader(source); |
| 180 | consumed_count += fbs.pos; | 180 | consumed_count += fbs.pos; |
| 181 | | 181 | |
| 182 | if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported; | 182 | if (frame_header.descriptor.dictionary_id_flag != 0) return error.DictionaryIdFlagUnsupported; |
| ... | @@ -218,7 +218,7 @@ pub const FrameContext = struct { | ... | @@ -218,7 +218,7 @@ pub const FrameContext = struct { |
| 218 | /// - `error.WindowSizeUnknown` if the frame does not have a valid window size | 218 | /// - `error.WindowSizeUnknown` if the frame does not have a valid window size |
| 219 | /// - `error.WindowTooLarge` if the window size is larger than | 219 | /// - `error.WindowTooLarge` if the window size is larger than |
| 220 | pub fn init( | 220 | pub fn init( |
| 221 | frame_header: frame.ZStandard.Header, | 221 | frame_header: frame.Zstandard.Header, |
| 222 | window_size_max: usize, | 222 | window_size_max: usize, |
| 223 | verify_checksum: bool, | 223 | verify_checksum: bool, |
| 224 | ) Error!FrameContext { | 224 | ) Error!FrameContext { |
| ... | @@ -241,7 +241,7 @@ pub const FrameContext = struct { | ... | @@ -241,7 +241,7 @@ pub const FrameContext = struct { |
| 241 | }; | 241 | }; |
| 242 | | 242 | |
| 243 | /// Decode a Zstandard from from `src` and return the decompressed bytes and the | 243 | /// Decode a Zstandard from from `src` and return the decompressed bytes and the |
| 244 | /// number of bytes read; see `decodeZStandardFrame()`. `allocator` is used to | 244 | /// number of bytes read; see `decodeZstandardFrame()`. `allocator` is used to |
| 245 | /// allocate both the returned slice and internal buffers used during decoding. | 245 | /// allocate both the returned slice and internal buffers used during decoding. |
| 246 | /// The first four bytes of `src` must be the magic number for a Zstandard | 246 | /// The first four bytes of `src` must be the magic number for a Zstandard |
| 247 | /// frame. | 247 | /// frame. |
| ... | @@ -259,20 +259,20 @@ pub const FrameContext = struct { | ... | @@ -259,20 +259,20 @@ pub const FrameContext = struct { |
| 259 | /// - `error.EndOfStream` if `src` does not contain a complete frame | 259 | /// - `error.EndOfStream` if `src` does not contain a complete frame |
| 260 | /// - `error.OutOfMemory` if `allocator` cannot allocate enough memory | 260 | /// - `error.OutOfMemory` if `allocator` cannot allocate enough memory |
| 261 | /// - an error in `block.Error` if there are errors decoding a block | 261 | /// - an error in `block.Error` if there are errors decoding a block |
| 262 | pub fn decodeZStandardFrameAlloc( | 262 | pub fn decodeZstandardFrameAlloc( |
| 263 | allocator: Allocator, | 263 | allocator: Allocator, |
| 264 | src: []const u8, | 264 | src: []const u8, |
| 265 | verify_checksum: bool, | 265 | verify_checksum: bool, |
| 266 | window_size_max: usize, | 266 | window_size_max: usize, |
| 267 | ) (error{OutOfMemory} || FrameContext.Error || FrameError)!DecodeResult { | 267 | ) (error{OutOfMemory} || FrameContext.Error || FrameError)!DecodeResult { |
| 268 | var result = std.ArrayList(u8).init(allocator); | 268 | var result = std.ArrayList(u8).init(allocator); |
| 269 | assert(readInt(u32, src[0..4]) == frame.ZStandard.magic_number); | 269 | assert(readInt(u32, src[0..4]) == frame.Zstandard.magic_number); |
| 270 | var consumed_count: usize = 4; | 270 | var consumed_count: usize = 4; |
| 271 | | 271 | |
| 272 | var frame_context = context: { | 272 | var frame_context = context: { |
| 273 | var fbs = std.io.fixedBufferStream(src[consumed_count..]); | 273 | var fbs = std.io.fixedBufferStream(src[consumed_count..]); |
| 274 | var source = fbs.reader(); | 274 | var source = fbs.reader(); |
| 275 | const frame_header = try decodeZStandardHeader(source); | 275 | const frame_header = try decodeZstandardHeader(source); |
| 276 | consumed_count += fbs.pos; | 276 | consumed_count += fbs.pos; |
| 277 | break :context try FrameContext.init(frame_header, window_size_max, verify_checksum); | 277 | break :context try FrameContext.init(frame_header, window_size_max, verify_checksum); |
| 278 | }; | 278 | }; |
| ... | @@ -371,7 +371,7 @@ pub fn decodeSkippableHeader(src: *const [8]u8) frame.Skippable.Header { | ... | @@ -371,7 +371,7 @@ pub fn decodeSkippableHeader(src: *const [8]u8) frame.Skippable.Header { |
| 371 | | 371 | |
| 372 | /// Returns the window size required to decompress a frame, or `null` if it | 372 | /// Returns the window size required to decompress a frame, or `null` if it |
| 373 | /// cannot be determined (which indicates a malformed frame header). | 373 | /// cannot be determined (which indicates a malformed frame header). |
| 374 | pub fn frameWindowSize(header: frame.ZStandard.Header) ?u64 { | 374 | pub fn frameWindowSize(header: frame.Zstandard.Header) ?u64 { |
| 375 | if (header.window_descriptor) |descriptor| { | 375 | if (header.window_descriptor) |descriptor| { |
| 376 | const exponent = (descriptor & 0b11111000) >> 3; | 376 | const exponent = (descriptor & 0b11111000) >> 3; |
| 377 | const mantissa = descriptor & 0b00000111; | 377 | const mantissa = descriptor & 0b00000111; |
| ... | @@ -389,8 +389,8 @@ const InvalidBit = error{ UnusedBitSet, ReservedBitSet }; | ... | @@ -389,8 +389,8 @@ const InvalidBit = error{ UnusedBitSet, ReservedBitSet }; |
| 389 | /// - `error.UnusedBitSet` if the unused bits of the header are set | 389 | /// - `error.UnusedBitSet` if the unused bits of the header are set |
| 390 | /// - `error.ReservedBitSet` if the reserved bits of the header are set | 390 | /// - `error.ReservedBitSet` if the reserved bits of the header are set |
| 391 | /// - `error.EndOfStream` if `source` does not contain a complete header | 391 | /// - `error.EndOfStream` if `source` does not contain a complete header |
| 392 | pub fn decodeZStandardHeader(source: anytype) (error{EndOfStream} || InvalidBit)!frame.ZStandard.Header { | 392 | pub fn decodeZstandardHeader(source: anytype) (error{EndOfStream} || InvalidBit)!frame.Zstandard.Header { |
| 393 | const descriptor = @bitCast(frame.ZStandard.Header.Descriptor, try source.readByte()); | 393 | const descriptor = @bitCast(frame.Zstandard.Header.Descriptor, try source.readByte()); |
| 394 | | 394 | |
| 395 | if (descriptor.unused) return error.UnusedBitSet; | 395 | if (descriptor.unused) return error.UnusedBitSet; |
| 396 | if (descriptor.reserved) return error.ReservedBitSet; | 396 | if (descriptor.reserved) return error.ReservedBitSet; |
| ... | @@ -414,7 +414,7 @@ pub fn decodeZStandardHeader(source: anytype) (error{EndOfStream} || InvalidBit) | ... | @@ -414,7 +414,7 @@ pub fn decodeZStandardHeader(source: anytype) (error{EndOfStream} || InvalidBit) |
| 414 | if (field_size == 2) content_size.? += 256; | 414 | if (field_size == 2) content_size.? += 256; |
| 415 | } | 415 | } |
| 416 | | 416 | |
| 417 | const header = frame.ZStandard.Header{ | 417 | const header = frame.Zstandard.Header{ |
| 418 | .descriptor = descriptor, | 418 | .descriptor = descriptor, |
| 419 | .window_descriptor = window_descriptor, | 419 | .window_descriptor = window_descriptor, |
| 420 | .dictionary_id = dictionary_id, | 420 | .dictionary_id = dictionary_id, |