| ... | ... | @@ -168,8 +168,11 @@ pub const DecodeState = struct { |
| 168 | 168 | const data = table[@field(self, @tagName(choice)).state]; |
| 169 | 169 | const T = @TypeOf(@field(self, @tagName(choice))).State; |
| 170 | 170 | const bits_summand = try bit_reader.readBitsNoEof(T, data.bits); |
| 171 | | const next_state = data.baseline + bits_summand; |
| 172 | | @field(self, @tagName(choice)).state = @intCast(@TypeOf(@field(self, @tagName(choice))).State, next_state); |
| 171 | const next_state = std.math.cast( |
| 172 | @TypeOf(@field(self, @tagName(choice))).State, |
| 173 | data.baseline + bits_summand, |
| 174 | ) orelse return error.MalformedFseBits; |
| 175 | @field(self, @tagName(choice)).state = next_state; |
| 173 | 176 | }, |
| 174 | 177 | } |
| 175 | 178 | } |
| ... | ... | @@ -1045,10 +1048,10 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1045 | 1048 | const even_data = entries[even_state]; |
| 1046 | 1049 | var read_bits: usize = 0; |
| 1047 | 1050 | const even_bits = try huff_bits.readBits(u32, even_data.bits, &read_bits); |
| 1048 | | weights[i] = @intCast(u4, even_data.symbol); |
| 1051 | weights[i] = std.math.cast(u4, even_data.symbol) orelse return error.MalformedHuffmanTree; |
| 1049 | 1052 | i += 1; |
| 1050 | 1053 | if (read_bits < even_data.bits) { |
| 1051 | | weights[i] = @intCast(u4, entries[odd_state].symbol); |
| 1054 | weights[i] = std.math.cast(u4, entries[odd_state].symbol) orelse return error.MalformedHuffmanTree; |
| 1052 | 1055 | log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] }); |
| 1053 | 1056 | i += 1; |
| 1054 | 1057 | break; |
| ... | ... | @@ -1058,11 +1061,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1058 | 1061 | read_bits = 0; |
| 1059 | 1062 | const odd_data = entries[odd_state]; |
| 1060 | 1063 | const odd_bits = try huff_bits.readBits(u32, odd_data.bits, &read_bits); |
| 1061 | | weights[i] = @intCast(u4, odd_data.symbol); |
| 1064 | weights[i] = std.math.cast(u4, odd_data.symbol) orelse return error.MalformedHuffmanTree; |
| 1062 | 1065 | i += 1; |
| 1063 | 1066 | if (read_bits < odd_data.bits) { |
| 1064 | 1067 | if (i == 256) return error.MalformedHuffmanTree; |
| 1065 | | weights[i] = @intCast(u4, entries[even_state].symbol); |
| 1068 | weights[i] = std.math.cast(u4, entries[even_state].symbol) orelse return error.MalformedHuffmanTree; |
| 1066 | 1069 | log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] }); |
| 1067 | 1070 | i += 1; |
| 1068 | 1071 | break; |
| ... | ... | @@ -1100,9 +1103,9 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H |
| 1100 | 1103 | log.debug("weight power sum = {d}", .{weight_power_sum}); |
| 1101 | 1104 | |
| 1102 | 1105 | // advance to next power of two (even if weight_power_sum is a power of 2) |
| 1103 | | max_number_of_bits = @intCast(u4, std.math.log2_int(u16, weight_power_sum) + 1); |
| 1106 | max_number_of_bits = std.math.log2_int(u16, weight_power_sum) + 1; |
| 1104 | 1107 | const next_power_of_two = @as(u16, 1) << max_number_of_bits; |
| 1105 | | weights[symbol_count - 1] = @intCast(u4, std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1); |
| 1108 | weights[symbol_count - 1] = std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1; |
| 1106 | 1109 | log.debug("weights[{d}] = {d}", .{ symbol_count - 1, weights[symbol_count - 1] }); |
| 1107 | 1110 | |
| 1108 | 1111 | var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined; |
| ... | ... | @@ -1367,7 +1370,7 @@ fn decodeFseTable( |
| 1367 | 1370 | while (accumulated_probability < total_probability) { |
| 1368 | 1371 | // WARNING: The RFC in poorly worded, and would suggest std.math.log2_int_ceil is correct here, |
| 1369 | 1372 | // but power of two (remaining probabilities + 1) need max bits set to 1 more. |
| 1370 | | const max_bits = @intCast(u4, std.math.log2_int(u16, total_probability - accumulated_probability + 1)) + 1; |
| 1373 | const max_bits = std.math.log2_int(u16, total_probability - accumulated_probability + 1) + 1; |
| 1371 | 1374 | const small = try bit_reader.readBitsNoEof(u16, max_bits - 1); |
| 1372 | 1375 | |
| 1373 | 1376 | const cutoff = (@as(u16, 1) << max_bits) - 1 - (total_probability - accumulated_probability + 1); |