///|
/// Just enough DER to read and write the two SPNEGO structures that carry an
/// NTLM token. Definite lengths only, which is what every implementation sends;
/// BER's indefinite form is rejected rather than guessed at.
pub suberror DerError {
  Truncated(Int)
  BadTag(Int, Int)
  BadLength(Int)
  MissingToken
}

///|
fn length_bytes(length : Int) -> Bytes {
  if length < 0x80 {
    let out = FixedArray::make(1, b'\x00')
    out[0] = length.to_byte()
    return FixedArray::unsafe_reinterpret_as_bytes(out)
  }
  if length < 0x100 {
    let out = FixedArray::make(2, b'\x00')
    out[0] = b'\x81'
    out[1] = length.to_byte()
    return FixedArray::unsafe_reinterpret_as_bytes(out)
  }
  if length < 0x10000 {
    let out = FixedArray::make(3, b'\x00')
    out[0] = b'\x82'
    out[1] = (length >> 8).to_byte()
    out[2] = length.to_byte()
    return FixedArray::unsafe_reinterpret_as_bytes(out)
  }
  let out = FixedArray::make(4, b'\x00')
  out[0] = b'\x83'
  out[1] = ((length >> 16) & 0xFF).to_byte()
  out[2] = ((length >> 8) & 0xFF).to_byte()
  out[3] = (length & 0xFF).to_byte()
  FixedArray::unsafe_reinterpret_as_bytes(out)
}

///|
/// Tag, then length, then content.
pub fn tlv(tag : Int, content : Bytes) -> Bytes {
  let head = FixedArray::make(1, b'\x00')
  head[0] = (tag & 0xFF).to_byte()
  @crypto.concat(
    @crypto.concat(
      FixedArray::unsafe_reinterpret_as_bytes(head),
      length_bytes(content.length()),
    ),
    content,
  )
}

///|
pub fn sequence(content : Bytes) -> Bytes {
  tlv(0x30, content)
}

///|
pub fn octet_string(content : Bytes) -> Bytes {
  tlv(0x04, content)
}

///|
pub fn enumerated(value : Int) -> Bytes {
  let out = FixedArray::make(1, b'\x00')
  out[0] = (value & 0xFF).to_byte()
  tlv(0x0A, FixedArray::unsafe_reinterpret_as_bytes(out))
}

///|
/// `[n]` in constructed form, which is what every SPNEGO field uses.
pub fn context(n : Int, content : Bytes) -> Bytes {
  tlv(0xA0 | (n & 0x1F), content)
}

///|
pub fn oid(raw : Bytes) -> Bytes {
  tlv(0x06, raw)
}

///|
pub struct Tlv {
  tag : Int
  /// Offset of the first content byte.
  content : Int
  length : Int
}

///|
pub fn Tlv::tag(self : Tlv) -> Int {
  self.tag
}

///|
pub fn Tlv::content(self : Tlv) -> Int {
  self.content
}

///|
pub fn Tlv::length(self : Tlv) -> Int {
  self.length
}

///|
/// Read one element header at `at`. The content itself is not copied; callers
/// slice it out when they need it.
pub fn read_tlv(data : Bytes, at : Int) -> Tlv raise DerError {
  if at + 2 > data.length() {
    raise Truncated(at + 2)
  }
  let tag = data[at].to_int()
  let first = data[at + 1].to_int()
  let (header, length) = if first < 0x80 {
    (2, first)
  } else if first == 0x81 {
    if at + 3 > data.length() {
      raise Truncated(at + 3)
    }
    (3, data[at + 2].to_int())
  } else if first == 0x82 {
    if at + 4 > data.length() {
      raise Truncated(at + 4)
    }
    (4, (data[at + 2].to_int() << 8) | data[at + 3].to_int())
  } else if first == 0x83 {
    if at + 5 > data.length() {
      raise Truncated(at + 5)
    }
    (
      5,
      (data[at + 2].to_int() << 16) |
      (data[at + 3].to_int() << 8) |
      data[at + 4].to_int(),
    )
  } else {
    raise BadLength(first)
  }
  if at + header + length > data.length() {
    raise Truncated(at + header + length)
  }
  { tag, content: at + header, length, }
}