// Snappy's raw format (format_description.txt): the uncompressed length as
// a varint, then elements: literals and copies of 1 to 64 bytes from up to
// 2^32 - 1 bytes back. The encoder works as snappy's CompressFragment does,
// on fragments of 64 KiB with a hash table of up to 2^14 entries.

///|
/// The fragments the encoder compresses apart, and the framing format's
/// most uncompressed bytes in a chunk.
const BLOCK_SIZE : Int32 = 65536

///|
/// The most output a raw stream can describe per byte of it: a copy of 64
/// bytes takes 3 (a 2-byte offset), so no stream of `n` bytes writes more
/// than `22 * n`.
const MAX_EXPANSION : Int64 = 22

///|
fn corrupt(message : String) -> @codec.CodecError {
  @codec.CodecError(Corrupt, message)
}

///|
/// The uncompressed length at the start of a raw stream, and the bytes its
/// varint takes: at most 5, for a length below 2^32.
fn read_length(input : BytesView) -> (Int64, Int32) raise @codec.CodecError {
  let mut value = 0L
  for i in 0..<5 {
    guard i < input.length() else {
      raise @codec.CodecError(Truncated, "the input ends inside the length")
    }
    let b = input[i].to_int64()
    value = value | ((b & 0x7FL) << (7 * i))
    if (b & 0x80L) == 0L {
      guard value <= 0xFFFFFFFFL else {
        raise corrupt("a length of 2^32 or more")
      }
      return (value, i + 1)
    }
  }
  raise corrupt("a length of more than 5 bytes")
}

///|
/// The error at `op`, recorded in `written`: only error paths take it.
fn stopped(
  written : Ref[Int32],
  op : Int32,
  message : String,
) -> @codec.CodecError {
  written.val = op
  corrupt(message)
}

///|
/// Decodes the elements of `src` into `dst[start:end]`, which they must fill
/// exactly; a copy reaches back no further than `start`. On an error,
/// `written` holds where the bytes decoded before it end, so that a
/// streaming decoder hands them out first, as the contract has it.
fn decode_elements(
  src : BytesView,
  dst : FixedArray[Byte],
  start : Int32,
  end : Int32,
  written : Ref[Int32],
) -> Unit raise @codec.CodecError {
  let n = src.length()
  let mut ip = 0
  let mut op = start
  while ip < n {
    // The loop runs only while ip is within src.
    let tag = src.unsafe_get(ip).to_int()
    ip += 1
    if (tag & 3) == 0 {
      let mut size_code = (tag >> 2).reinterpret_as_uint()
      if size_code >= 60U {
        let k = (tag >> 2) - 59
        guard k <= n - ip else {
          raise stopped(written, op, "a literal's length cut short")
        }
        // The preceding guard bounds all k length bytes.
        size_code = 0U
        for j in 0..= 16 && end - op >= 16 {
        // Both ranges fit. Only the first len bytes become output.
        @copy16.copy16_from_view(dst, op, src, ip)
      } else {
        @blit.from_view(dst, op, src[ip:ip + len])
      }
      ip += len
      op += len
      continue
    }
    let mut len = 0
    let mut offset = 0
    match tag & 3 {
      1 => {
        guard ip < n else { raise stopped(written, op, "a copy cut short") }
        // ip < n, so the one-byte offset is readable.
        offset = (tag >> 5 << 8) | src.unsafe_get(ip).to_int()
        ip += 1
        len = 4 + ((tag >> 2) & 7)
      }
      2 => {
        guard n - ip >= 2 else {
          raise stopped(written, op, "a copy cut short")
        }
        // The preceding guard bounds both offset bytes.
        offset = src.unsafe_get(ip).to_int() |
          (src.unsafe_get(ip + 1).to_int() << 8)
        ip += 2
        len = 1 + (tag >> 2)
      }
      _ => {
        guard n - ip >= 4 else {
          raise stopped(written, op, "a copy cut short")
        }
        // The preceding guard bounds all four offset bytes.
        offset = src.unsafe_get(ip).to_int() |
          (src.unsafe_get(ip + 1).to_int() << 8) |
          (src.unsafe_get(ip + 2).to_int() << 16) |
          (src.unsafe_get(ip + 3).to_int() << 24)
        ip += 4
        len = 1 + (tag >> 2)
      }
    }
    // A four-byte offset with its high bit set exceeds any possible history
    // and is rejected by the positive-offset check.
    guard offset > 0 && offset <= op - start else {
      raise stopped(written, op, "a copy from before the start of the data")
    }
    guard len <= end - op else {
      raise stopped(written, op, "a copy past the length")
    }
    let from = op - offset
    if offset >= len {
      if len <= 16 && offset >= 16 && end - op >= 16 {
        // The offset bounds the source. Only len bytes become output.
        @copy16.copy16(dst, op, from)
      } else {
        @blit.bytes(dst, op, dst, from, len)
      }
    } else {
      // Overlapping: the copy repeats bytes it writes.
      @blit.forward(dst, op, from, len)
    }
    op += len
  }
  guard op == end else {
    raise stopped(written, op, "fewer bytes than the length says")
  }
}

///|
/// Decodes a whole raw Snappy stream. `max_output` caps the decoded length
/// (2^31 - 1 by default, what an array holds): past it the stream raises
/// `LimitExceeded({ resource: Output, .. })` before anything is allocated.
/// A length that the stream's bytes cannot make raises `Corrupt`, before
/// anything is allocated too.
pub fn decompress_raw(
  input : BytesView,
  max_output? : Int32,
) -> Bytes raise @codec.CodecError {
  let (length, used) = read_length(input)
  let limit = match max_output {
    Some(m) => m.max(0).to_int64()
    None => 0x7FFFFFFFL
  }
  guard length <= limit else {
    raise @codec.CodecError(
      LimitExceeded({
        resource: Output,
        needed: Some(length.reinterpret_as_uint64()),
        limit: limit.reinterpret_as_uint64(),
      }),
      "the stream says \{length} bytes, over \{limit}",
    )
  }
  let rest = input[used:]
  guard length <= MAX_EXPANSION * rest.length().to_int64() else {
    raise corrupt("a length the stream's bytes cannot make")
  }
  let out = FixedArray::make(length.to_int(), b'\x00')
  decode_elements(rest, out, 0, out.length(), Ref(0))
  // The array is exactly the output and has no mutable alias after return.
  out.unsafe_reinterpret_as_bytes()
}

///|
/// The most bytes `compress_raw` writes for `n` bytes of input: snappy's
/// `MaxCompressedLength`, `32 + n + n / 6`. A negative `n` counts as 0.
pub fn raw_compress_bound(n : Int64) -> Int64 {
  let n = n.max(0L)
  guard n <= 0x3FFFFFFFFFFFFFFFL else { return 0x7FFFFFFFFFFFFFFFL }
  32L + n + n / 6L
}

///|
/// Compresses `input` into a whole raw Snappy stream, as snappy's
/// `Compress` does: the length, then each 64 KiB fragment on its own.
/// Inputs over 1,840,700,242 bytes abort: the output is allocated at its
/// bound first, and theirs passes 2^31 - 1 bytes, the most an array holds.
pub fn compress_raw(input : BytesView) -> Bytes {
  let bound = raw_compress_bound(input.length().to_int64())
  guard bound <= 0x7FFFFFFFL else {
    abort(
      "compress_raw: \{input.length()} bytes, whose bound passes 2^31 - 1 bytes",
    )
  }
  let out = Writer::new(bound.to_int())
  out.varint(input.length())
  let table = FixedArray::make(1 << MAX_HASH_BITS, (0 : UInt16))
  let mut start = 0
  while start < input.length() {
    let end = (start + BLOCK_SIZE).min(input.length())
    compress_fragment(input, start, end, out, table)
    start = end
  }
  out.to_bytes()
}