1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
//// 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/<uid>.<lang>.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(<<v:little-size(16)>>) -> 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(<<v:little-size(32)>>) -> 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")
}
}
|