///|
/// Decodes a whole raw DEFLATE stream held in memory, with the preset
/// `dictionary` if the stream was compressed with one, as `Decoder` does.
/// `max_output` caps the decoded size (unlimited by default) and raises
/// `LimitExceeded({ resource: Output, .. })` past it; bytes after the final
/// block raise `TrailingData`.
pub fn decompress(
  input : BytesView,
  dictionary? : BytesView,
  max_output? : Int32,
) -> Bytes raise @codec.CodecError {
  let decoder = Decoder(dictionary?)
  let chunk = FixedArray::make(1 << 16, b'\x00')
  let out = Buffer()
  let mut rest = input
  for ;; {
    let status = decoder.step_into(rest, chunk, end=true)
    rest = rest[decoder.last_consumed():]
    let produced = decoder.last_produced()
    // `out` holds at most `limit` bytes, so this cannot overflow.
    if max_output is Some(limit) && produced > limit - out.length() {
      raise CodecError(
        LimitExceeded({
          resource: Output,
          needed: None,
          limit: limit.max(0).to_uint64(),
        }),
        "the output exceeds \{limit} bytes",
      )
    }
    out.write_bytesview(Bytes::from_array(chunk[:produced])[:])
    if status is Done {
      return out.to_bytes()
    }
  }
}

///|
/// Compresses `input` into a whole raw DEFLATE stream, at `level` (0 to 9,
/// clamped, with -1 as 6; 6 by default), with the preset `dictionary` if
/// given and a window of `1 << window_bits` bytes, as `Encoder` does.
pub fn compress(
  input : BytesView,
  level~ : Int32 = 6,
  dictionary? : BytesView,
  window_bits~ : Int32 = 15,
) -> Bytes {
  let encoder = Encoder(level~, dictionary?, window_bits~)
  let chunk = FixedArray::make(1 << 16, b'\x00')
  let out = Buffer()
  let mut rest = input
  for ;; {
    let status = encoder.step_into(rest, chunk, action=Finish)
    rest = rest[encoder.last_consumed():]
    out.write_bytesview(Bytes::from_array(chunk[:encoder.last_produced()])[:])
    if status is Done {
      return out.to_bytes()
    }
  }
}