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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/////+
////- Name: SisuDoc Spine, Doc Reform [a part of]
//// - Description: documents, structuring, processing, publishing, search
//// - static content generator
////
//// - Author: Ralph Amissah
//// [ralph.amissah@gmail.com]
////
//// - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah, All Rights Reserved.
////
//// - License: AGPL 3 or later:
////
//// Spine (SiSU), a framework for document structuring, publishing and
//// search
////
//// Copyright (C) Ralph Amissah
////
//// This program is free software: you can redistribute it and/or modify it
//// under the terms of the GNU AFERO General Public License as published by the
//// Free Software Foundation, either version 3 of the License, or (at your
//// option) any later version.
////
//// This program is distributed in the hope that it will be useful, but WITHOUT
//// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
//// more details.
////
//// You should have received a copy of the GNU General Public License along with
//// this program. If not, see [https://www.gnu.org/licenses/].
////
//// If you have Internet connection, the latest version of the AGPL should be
//// available at these locations:
//// [https://www.fsf.org/licensing/licenses/agpl.html]
//// [https://www.gnu.org/licenses/agpl.html]
////
//// - Spine (by Doc Reform, related to SiSU) uses standard:
//// - docReform markup syntax
//// - standard SiSU markup syntax with modified headers and minor modifications
//// - docReform object numbering
//// - standard SiSU object citation numbering & system
////
//// - Homepages:
//// [https://www.sisudoc.org]
//// [https://www.doc-reform.org]
////
//// - Git
//// [https://git.sisudoc.org/]
////
////+/
//// 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")
}
}
|