forked from akhenakh/gozim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zim.go
323 lines (277 loc) · 7.03 KB
/
zim.go
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package zim
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"strings"
"syscall"
)
const (
zimHeader = 72173914
)
// ZimReader keep tracks of everything related to ZIM reading
type ZimReader struct {
f *os.File
ArticleCount uint32
clusterCount uint32
urlPtrPos uint64
titlePtrPos uint64
clusterPtrPos uint64
mimeListPos uint64
mainPage uint32
layoutPage uint32
mimeTypeList []string
mmap []byte
}
// create a new zim reader
func NewReader(path string, mmap bool) (*ZimReader, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
z := ZimReader{f: f, mainPage: 0xffffffff, layoutPage: 0xffffffff}
fi, err := f.Stat()
if err != nil {
return nil, err
}
size := fi.Size()
if mmap {
// we need a multiple of page size bigger than the file
pc := size / int64(os.Getpagesize())
totalMmap := pc*int64(os.Getpagesize()) + int64(os.Getpagesize())
if (size % int64(os.Getpagesize())) == 0 {
totalMmap = size
}
mmap, err := syscall.Mmap(int(f.Fd()), 0, int(totalMmap), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, err
}
z.mmap = mmap
}
err = z.readFileHeaders()
return &z, err
}
// Return an ordered list of mime types present in the ZIM file
func (z *ZimReader) MimeTypes() []string {
if len(z.mimeTypeList) != 0 {
return z.mimeTypeList
}
var s []string
// assume mime list fit in 2k
b, err := z.bytesRangeAt(z.mimeListPos, z.mimeListPos+2048)
if err != nil {
return s
}
bbuf := bytes.NewBuffer(b)
for {
line, err := bbuf.ReadBytes('\x00')
if err != nil && err != io.EOF {
return s
}
// a line of 1 is a line containing only \x00 and it's the marker for the
// end of mime types list
if len(line) == 1 {
break
}
s = append(s, strings.TrimRight(string(line), "\x00"))
}
z.mimeTypeList = s
return s
}
// list all articles, using url index, contained in a zim file
// note that this is a slow implementation, a real iterator is faster
// you are not suppose to use this method on big zim files, use indexes
func (z *ZimReader) ListArticles() <-chan *Article {
ch := make(chan *Article, 10)
go func() {
var idx uint32
// starting at 1 to avoid "con" entry
var start uint32 = 1
for idx = start; idx < z.ArticleCount; idx++ {
art, err := z.ArticleAtURLIdx(idx)
if err != nil {
continue
}
if art == nil {
//TODO: deal with redirect continue
}
ch <- art
}
close(ch)
}()
return ch
}
// list all title pointer, Titles by position contained in a zim file
// Titles are pointers to URLpos index, usefull for indexing cause smaller to store: uint32
// note that this is a slow implementation, a real iterator is faster
// you are not suppose to use this method on big zim files prefer ListTitlesPtrIterator to build your index
func (z *ZimReader) ListTitlesPtr() <-chan uint32 {
ch := make(chan uint32, 10)
go func() {
var pos uint64
var count uint32
for pos = z.titlePtrPos; count < z.ArticleCount; pos += 4 {
idx, err := readInt32(z.bytesRangeAt(pos, pos+4))
if err != nil {
continue
}
ch <- idx
count++
}
close(ch)
}()
return ch
}
// list all title pointer, Titles by position contained in a zim file
// Titles are pointers to URLpos index, usefull for indexing cause smaller to store: uint32
func (z *ZimReader) ListTitlesPtrIterator(cb func(uint32)) {
var count uint32
for pos := z.titlePtrPos; count < z.ArticleCount; pos += 4 {
idx, err := readInt32(z.bytesRangeAt(pos, pos+4))
if err != nil {
continue
}
cb(idx)
count++
}
}
// return the article at the exact url not using any index
func (z *ZimReader) GetPageNoIndex(url string) (*Article, error) {
// starting at 1 to avoid "con" entry
var start uint32
stop := z.ArticleCount
a := new(Article)
for {
pos := (start + stop) / 2
offset, err := z.OffsetAtURLIdx(pos)
if err != nil {
return nil, err
}
err = z.FillArticleAt(a, offset)
if err != nil {
return nil, err
}
if a.FullURL() == url {
return a, nil
}
if a.FullURL() > url {
stop = pos
} else {
start = pos
}
if stop-start == 1 {
break
}
}
return nil, errors.New("article not found")
}
// get the offset pointing to Article at pos in the URL idx
func (z *ZimReader) OffsetAtURLIdx(idx uint32) (uint64, error) {
offset := z.urlPtrPos + uint64(idx)*8
return readInt64(z.bytesRangeAt(offset, offset+8))
}
// Close & cleanup the zimreader
func (z *ZimReader) Close() error {
return z.f.Close()
}
func (z *ZimReader) String() string {
fi, err := z.f.Stat()
if err != nil {
return "corrupted zim"
}
return fmt.Sprintf("Size: %d, ArticleCount: %d urlPtrPos: 0x%x titlePtrPos: 0x%x mimeListPos: 0x%x clusterPtrPos: 0x%x\nMimeTypes: %v",
fi.Size(), z.ArticleCount, z.urlPtrPos, z.titlePtrPos, z.mimeListPos, z.clusterPtrPos, z.MimeTypes())
}
// getBytesRangeAt returns bytes from start to end
// it's needed to abstract mmap usages rather than read directly on the mmap slices
func (z *ZimReader) bytesRangeAt(start, end uint64) ([]byte, error) {
if len(z.mmap) > 0 {
return z.mmap[start:end], nil
}
buf := make([]byte, end-start)
n, err := z.f.ReadAt(buf, int64(start))
if err != nil {
return nil, err
}
if n != int(end-start) {
return nil, errors.New("can't read enough bytes")
}
return buf, nil
}
// populate the ZimReader structs with headers
func (z *ZimReader) readFileHeaders() error {
// checking for file type
v, err := readInt32(z.bytesRangeAt(0, 0+4))
if err != nil || v != zimHeader {
return errors.New("not a ZIM file")
}
// checking for version
v, err = readInt32(z.bytesRangeAt(4, 4+4))
if err != nil || v != 5 {
return errors.New("unsupported version, 5 only")
}
// checking for articles count
v, err = readInt32(z.bytesRangeAt(24, 24+4))
if err != nil {
return err
}
z.ArticleCount = v
// checking for cluster count
v, err = readInt32(z.bytesRangeAt(28, 28+4))
if err != nil {
return err
}
z.clusterCount = v
// checking for urlPtrPos
vb, err := readInt64(z.bytesRangeAt(32, 32+8))
if err != nil {
return err
}
z.urlPtrPos = vb
// checking for titlePtrPos
vb, err = readInt64(z.bytesRangeAt(40, 40+8))
if err != nil {
return err
}
z.titlePtrPos = vb
// checking for clusterPtrPos
vb, err = readInt64(z.bytesRangeAt(48, 48+8))
if err != nil {
return err
}
z.clusterPtrPos = vb
// checking for mimeListPos
vb, err = readInt64(z.bytesRangeAt(56, 56+8))
if err != nil {
return err
}
z.mimeListPos = vb
// checking for mainPage
v, err = readInt32(z.bytesRangeAt(64, 64+4))
if err != nil {
return err
}
z.mainPage = v
// checking for layoutPage
v, err = readInt32(z.bytesRangeAt(68, 68+4))
if err != nil {
return err
}
z.layoutPage = v
z.MimeTypes()
return nil
}
// return start and end offsets for cluster at index idx
func (z *ZimReader) clusterOffsetsAtIdx(idx uint32) (start, end uint64, err error) {
offset := z.clusterPtrPos + (uint64(idx) * 8)
start, err = readInt64(z.bytesRangeAt(offset, offset+8))
if err != nil {
return
}
offset = z.clusterPtrPos + (uint64(idx+1) * 8)
end, err = readInt64(z.bytesRangeAt(offset, offset+8))
end--
return
}