//// Enough of the zip format to find one file in a pod and read it. //// //// Not a zip library. A pod archive holds a handful of entries, spine writes //// them **stored** (uncompressed) so that the outer zstd frame does the //// compressing once over the whole thing, and what is wanted from it here is //// one named entry. //// //// Deflate is *refused rather than mishandled*. Erlang's `zlib` could //// inflate it, but a library that says it read an entry it in fact skipped is //// worse than one that says it cannot, and spine's own archives do not need //// it. import gleam/bit_array import gleam/int import gleam/list import gleam/result import gleam/string pub type Entry { Entry( name: String, /// 0 stored, 8 deflate method: Int, compressed: Int, uncompressed: Int, local_offset: Int, ) } const eocd_sig = 0x06054b50 const cd_sig = 0x02014b50 const local_sig = 0x04034b50 /// The entries, from the central directory at the end of the file. pub fn entries(bytes: BitArray) -> Result(List(Entry), String) { let size = bit_array.byte_size(bytes) use _ <- result.try(case size >= 22 { True -> Ok(Nil) False -> Error("too short to be a zip archive") }) use eocd <- result.try(find_eocd(bytes, size - 22, lowest(size))) use count <- result.try(u16(bytes, eocd + 10)) use offset <- result.try(u32(bytes, eocd + 16)) read_central(bytes, offset, count, []) } /// The end of central directory record is last, and may be followed by a /// comment of up to 65535 bytes, so it is searched for backwards. fn find_eocd(bytes: BitArray, at: Int, lowest: Int) -> Result(Int, String) { case at < lowest { True -> Error( "no end of central directory record; it does not look like a zip archive", ) False -> case u32(bytes, at) { Ok(sig) if sig == eocd_sig -> Ok(at) _ -> find_eocd(bytes, at - 1, lowest) } } } fn lowest(size: Int) -> Int { case size > 22 + 65_535 { True -> size - 22 - 65_535 False -> 0 } } fn read_central( bytes: BitArray, at: Int, remaining: Int, acc: List(Entry), ) -> Result(List(Entry), String) { case remaining { 0 -> Ok(list.reverse(acc)) _ -> { use sig <- result.try(u32(bytes, at)) use _ <- result.try(case sig == cd_sig { True -> Ok(Nil) False -> Error( "central directory entry " <> int.to_string(list.length(acc)) <> " is malformed", ) }) use method <- result.try(u16(bytes, at + 10)) use compressed <- result.try(u32(bytes, at + 20)) use uncompressed <- result.try(u32(bytes, at + 24)) use name_len <- result.try(u16(bytes, at + 28)) use extra_len <- result.try(u16(bytes, at + 30)) use cmnt_len <- result.try(u16(bytes, at + 32)) use local_offset <- result.try(u32(bytes, at + 42)) use name <- result.try(text(bytes, at + 46, name_len)) let entry = Entry( name: name, method: method, compressed: compressed, uncompressed: uncompressed, local_offset: local_offset, ) read_central( bytes, at + 46 + name_len + extra_len + cmnt_len, remaining - 1, [entry, ..acc], ) } } } /// One entry's bytes, from its local header. pub fn read(bytes: BitArray, entry: Entry) -> Result(BitArray, String) { use sig <- result.try(u32(bytes, entry.local_offset)) use _ <- result.try(case sig == local_sig { True -> Ok(Nil) False -> Error(entry.name <> ": local header is malformed") }) use name_len <- result.try(u16(bytes, entry.local_offset + 26)) use extra_len <- result.try(u16(bytes, entry.local_offset + 28)) use _ <- result.try(case entry.method { 0 -> Ok(Nil) m -> Error( entry.name <> ": compression method " <> int.to_string(m) <> "; this reader takes only stored entries, which is what spine writes", ) }) let start = entry.local_offset + 30 + name_len + extra_len case bit_array.slice(bytes, start, entry.compressed) { Ok(slice) -> Ok(slice) Error(_) -> Error(entry.name <> ": runs past the end of the archive") } } /// The entries whose names end this way, in archive order. Used to find /// media/abstraction/..ssp without knowing the uid. pub fn find_suffix(entries: List(Entry), suffix: String) -> List(Entry) { list.filter(entries, fn(e) { string.ends_with(e.name, suffix) }) } fn u16(bytes: BitArray, at: Int) -> Result(Int, String) { case bit_array.slice(bytes, at, 2) { Ok(<>) -> Ok(v) _ -> Error("read past the end of the archive at " <> int.to_string(at)) } } fn u32(bytes: BitArray, at: Int) -> Result(Int, String) { case bit_array.slice(bytes, at, 4) { Ok(<>) -> Ok(v) _ -> Error("read past the end of the archive at " <> int.to_string(at)) } } fn text(bytes: BitArray, at: Int, len: Int) -> Result(String, String) { case bit_array.slice(bytes, at, len) { Ok(slice) -> bit_array.to_string(slice) |> result.replace_error("an entry name is not valid UTF-8") Error(_) -> Error("an entry name runs past the end of the archive") } }