use anyhow::{Result, bail};
use serde::Serialize;
use std::fmt;
pub const MAGIC: &[u8] = b"HTLB\x02";
const MAGIC_V1: &[u8] = b"HTLB\x01";
pub fn format_version(bytes: &[u8]) -> Option<u8> {
if bytes.starts_with(MAGIC) {
Some(2)
} else if bytes.starts_with(MAGIC_V1) {
Some(1)
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Bytecode,
Source,
}
#[derive(Debug, Clone)]
pub struct Module {
pub name: String,
pub kind: Kind,
pub payload: Vec<u8>,
}
#[derive(Debug, Clone, Default)]
pub struct Bundle {
pub entry: String,
pub fingerprint: Vec<u8>,
pub htl_version: String,
pub host_modules: Vec<String>,
pub modules: Vec<Module>,
}
impl Bundle {
pub fn is_bundle(bytes: &[u8]) -> bool {
bytes.starts_with(MAGIC) || bytes.starts_with(MAGIC_V1)
}
pub fn module(&self, name: &str) -> Option<&Module> {
self.modules.iter().find(|m| m.name == name)
}
pub fn encode(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.extend_from_slice(MAGIC);
put_bytes(&mut buf, &self.fingerprint);
put_bytes(&mut buf, self.htl_version.as_bytes());
put_bytes(&mut buf, self.entry.as_bytes());
buf.extend_from_slice(&(self.host_modules.len() as u32).to_le_bytes());
for h in &self.host_modules {
put_bytes(&mut buf, h.as_bytes());
}
buf.extend_from_slice(&(self.modules.len() as u32).to_le_bytes());
for m in &self.modules {
buf.push(match m.kind {
Kind::Bytecode => 0,
Kind::Source => 1,
});
put_bytes(&mut buf, m.name.as_bytes());
put_bytes(&mut buf, &m.payload);
}
buf
}
pub fn decode(bytes: &[u8]) -> Result<Self> {
if bytes.starts_with(MAGIC_V1) {
return Self::decode_v1(&bytes[MAGIC_V1.len()..]);
}
if !bytes.starts_with(MAGIC) {
bail!("not an htl bundle (bad magic)");
}
let mut cur = &bytes[MAGIC.len()..];
let fingerprint = take_bytes(&mut cur)?.to_vec();
let htl_version = String::from_utf8(take_bytes(&mut cur)?.to_vec())?;
let entry = String::from_utf8(take_bytes(&mut cur)?.to_vec())?;
let n = take_u32(&mut cur)? as usize;
let mut host_modules = Vec::with_capacity(n);
for _ in 0..n {
host_modules.push(String::from_utf8(take_bytes(&mut cur)?.to_vec())?);
}
let count = take_u32(&mut cur)? as usize;
let mut modules = Vec::with_capacity(count);
for _ in 0..count {
let kind = match take_u8(&mut cur)? {
0 => Kind::Bytecode,
1 => Kind::Source,
k => bail!("unknown module kind {k} in bundle"),
};
let name = String::from_utf8(take_bytes(&mut cur)?.to_vec())?;
let payload = take_bytes(&mut cur)?.to_vec();
modules.push(Module {
name,
kind,
payload,
});
}
Ok(Self {
entry,
fingerprint,
htl_version,
host_modules,
modules,
})
}
fn decode_v1(mut cur: &[u8]) -> Result<Self> {
let entry = String::from_utf8(take_bytes(&mut cur)?.to_vec())?;
let count = take_u32(&mut cur)? as usize;
let mut modules = Vec::with_capacity(count);
for _ in 0..count {
let name = String::from_utf8(take_bytes(&mut cur)?.to_vec())?;
let payload = take_bytes(&mut cur)?.to_vec();
modules.push(Module {
name,
kind: Kind::Bytecode,
payload,
});
}
Ok(Self {
entry,
modules,
..Default::default()
})
}
}
fn put_bytes(buf: &mut Vec<u8>, b: &[u8]) {
buf.extend_from_slice(&(b.len() as u32).to_le_bytes());
buf.extend_from_slice(b);
}
fn take_u8(cur: &mut &[u8]) -> Result<u8> {
if cur.is_empty() {
bail!("truncated bundle");
}
let b = cur[0];
*cur = &cur[1..];
Ok(b)
}
fn take_u32(cur: &mut &[u8]) -> Result<u32> {
if cur.len() < 4 {
bail!("truncated bundle");
}
let n = u32::from_le_bytes([cur[0], cur[1], cur[2], cur[3]]);
*cur = &cur[4..];
Ok(n)
}
fn take_bytes<'a>(cur: &mut &'a [u8]) -> Result<&'a [u8]> {
let n = take_u32(cur)? as usize;
if cur.len() < n {
bail!("truncated bundle");
}
let (head, rest) = cur.split_at(n);
*cur = rest;
Ok(head)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LuaHeader {
pub version: String,
pub format: u8,
pub instruction_bytes: u8,
pub integer_bytes: u8,
pub number_bytes: u8,
pub endian: &'static str,
}
impl LuaHeader {
pub fn parse(fp: &[u8]) -> Option<Self> {
if fp.len() < 23 {
return None;
}
let ver = fp[4];
Some(Self {
version: format!("{}.{}", ver >> 4, ver & 0xf),
format: fp[5],
instruction_bytes: fp[12],
integer_bytes: fp[13],
number_bytes: fp[14],
endian: if fp[15] == 0x78 { "little" } else { "big" },
})
}
}
impl fmt::Display for LuaHeader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Lua {}, format {}, {}/{}/{}, {}-endian",
self.version,
self.format,
self.instruction_bytes,
self.integer_bytes,
self.number_bytes,
self.endian
)
}
}
pub fn describe_fingerprint(fp: &[u8]) -> String {
match LuaHeader::parse(fp) {
Some(h) => h.to_string(),
None => format!("{} byte(s)", fp.len()),
}
}