std/lzw: change set_literal_width to set_quirk

This commit is contained in:
Nigel Tao committed 2023-04-15 09:59:40 +10:00
1 parent 6e84987d33
commit d8c23a3a66
7 files changed
+83 -62

No files matched your search

+1 -1
View File
@@ -899,7 +899,7 @@ pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reade
if lw > 8 {
return "#bad literal width"
}
this.lzw.set_literal_width!(lw: lw as base.u32)
this.lzw.set_quirk!(key: lzw.QUIRK_LITERAL_WIDTH_PLUS_ONE, value: (1 + lw) as base.u64)
this.previous_lzw_decode_ended_abruptly = true
}
+14 -11
View File
@@ -28,11 +28,11 @@ pri status "#internal error: inconsistent I/O"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
pub struct decoder? implements base.io_transformer(
// set_literal_width_arg is 1 plus the saved argument passed to
// set_literal_width. This is assigned to the literal_width field at the
// start of transform_io. During that method, calling set_literal_width
// will change set_literal_width_arg but not literal_width.
set_literal_width_arg : base.u32[..= 9],
// pending_literal_width_plus_one is 1 plus the saved argument passed
// to set_quirk. This is assigned to the literal_width field at the
// start of transform_io. During that method, calling set_quirk will
// change pending_literal_width_plus_one but not literal_width.
pending_literal_width_plus_one : base.u32[..= 9],
// read_from state that does not change during a decode call.
literal_width : base.u32[..= 8],
@@ -71,13 +71,16 @@ pub struct decoder? implements base.io_transformer(
)
pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
if args.key == QUIRK_LITERAL_WIDTH_PLUS_ONE {
if args.value > 9 {
return base."#bad argument"
}
this.pending_literal_width_plus_one = args.value as base.u32
return ok
}
return base."#unsupported option"
}
pub func decoder.set_literal_width!(lw: base.u32[..= 8]) {
this.set_literal_width_arg = args.lw + 1
}
pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
}
@@ -87,8 +90,8 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
// Initialize read_from state.
this.literal_width = 8
if this.set_literal_width_arg > 0 {
this.literal_width = this.set_literal_width_arg - 1
if this.pending_literal_width_plus_one > 0 {
this.literal_width = this.pending_literal_width_plus_one - 1
}
this.clear_code = (1 as base.u32) << this.literal_width
this.end_code = this.clear_code + 1
+28
View File
@@ -0,0 +1,28 @@
// Copyright 2023 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// --------
// Quirks are discussed in (/doc/note/quirks.md).
//
// The base38 encoding of "lzw " is 0x1417A8. Left shifting by 10 gives
// 0x505E_A000.
pri const QUIRKS_BASE : base.u32 = 0x505E_A000
// --------
// When this quirk is set (to a value in the range 0 ..= 9), a positive value
// is one more than the initial number of bits of a literal code. Zero means to
// use the default literal width, 8.
pub const QUIRK_LITERAL_WIDTH_PLUS_ONE : base.u32 = 0x505E_A000 | 0x00