boko 0.2.0

Fast ebook conversion library for EPUB and Kindle formats
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::io;

pub const NULL_INDEX: u32 = 0xFFFFFFFF;

/// MOBI Header (Record 0)
#[derive(Debug, Clone)]
#[allow(dead_code)] // Fields are part of MOBI format spec, useful for debugging
pub struct MobiHeader {
    pub compression: Compression,
    pub text_record_count: u16,
    pub text_record_size: u16,
    pub encryption: u16,
    pub mobi_type: u32,
    pub encoding: Encoding,
    pub mobi_version: u32,
    pub first_image_index: u32,
    pub title: String,
    pub language: u32,
    pub exth_flags: u32,
    pub extra_data_flags: u16,
    // HUFF/CDIC indices (for Huffman compression)
    pub huff_record_index: u32,
    pub huff_record_count: u32,
    // KF8 indices
    pub skel_index: u32,
    pub div_index: u32,
    pub oth_index: u32,
    pub fdst_index: u32,
    pub fdst_count: u32,
    pub ncx_index: u32,
    // Raw header for EXTH parsing
    pub header_length: u32,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Compression {
    None,
    PalmDoc,
    Huffman,
    Unknown(u16),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Encoding {
    Cp1252,
    Utf8,
    Unknown(u32),
}

impl MobiHeader {
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        if data.len() < 16 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "MOBI header too short",
            ));
        }

        let compression = match u16::from_be_bytes([data[0], data[1]]) {
            1 => Compression::None,
            2 => Compression::PalmDoc,
            0x4448 => Compression::Huffman, // "DH"
            n => Compression::Unknown(n),
        };

        let text_record_count = u16::from_be_bytes([data[8], data[9]]);
        let text_record_size = u16::from_be_bytes([data[10], data[11]]);
        let encryption = u16::from_be_bytes([data[12], data[13]]);

        // Check if this is a minimal header
        if data.len() <= 16 {
            return Ok(Self {
                compression,
                text_record_count,
                text_record_size,
                encryption,
                mobi_type: 0,
                encoding: Encoding::Cp1252,
                mobi_version: 1,
                first_image_index: NULL_INDEX,
                title: String::new(),
                language: 0,
                exth_flags: 0,
                extra_data_flags: 0,
                huff_record_index: NULL_INDEX,
                huff_record_count: 0,
                skel_index: NULL_INDEX,
                div_index: NULL_INDEX,
                oth_index: NULL_INDEX,
                fdst_index: NULL_INDEX,
                fdst_count: 0,
                ncx_index: NULL_INDEX,
                header_length: 0,
            });
        }

        let header_length = u32::from_be_bytes([data[20], data[21], data[22], data[23]]);
        let mobi_type = u32::from_be_bytes([data[24], data[25], data[26], data[27]]);
        let codepage = u32::from_be_bytes([data[28], data[29], data[30], data[31]]);

        let encoding = match codepage {
            1252 => Encoding::Cp1252,
            65001 => Encoding::Utf8,
            n => Encoding::Unknown(n),
        };

        // Title offset and length at 0x54-0x5C
        let title = if data.len() >= 0x5C {
            let title_offset =
                u32::from_be_bytes([data[0x54], data[0x55], data[0x56], data[0x57]]) as usize;
            let title_length =
                u32::from_be_bytes([data[0x58], data[0x59], data[0x5A], data[0x5B]]) as usize;
            if title_offset + title_length <= data.len() {
                String::from_utf8_lossy(&data[title_offset..title_offset + title_length])
                    .to_string()
            } else {
                String::new()
            }
        } else {
            String::new()
        };

        let language = if data.len() >= 0x60 {
            u32::from_be_bytes([data[0x5C], data[0x5D], data[0x5E], data[0x5F]])
        } else {
            0
        };

        let mobi_version = if data.len() >= 0x6C {
            u32::from_be_bytes([data[0x68], data[0x69], data[0x6A], data[0x6B]])
        } else {
            1
        };

        let first_image_index = if data.len() >= 0x70 {
            u32::from_be_bytes([data[0x6C], data[0x6D], data[0x6E], data[0x6F]])
        } else {
            NULL_INDEX
        };

        // HUFF/CDIC indices at 0x70 and 0x74
        let (huff_record_index, huff_record_count) = if data.len() >= 0x78 {
            (
                u32::from_be_bytes([data[0x70], data[0x71], data[0x72], data[0x73]]),
                u32::from_be_bytes([data[0x74], data[0x75], data[0x76], data[0x77]]),
            )
        } else {
            (NULL_INDEX, 0)
        };

        let exth_flags = if data.len() >= 0x84 {
            u32::from_be_bytes([data[0x80], data[0x81], data[0x82], data[0x83]])
        } else {
            0
        };

        let extra_data_flags = if data.len() >= 0xF4 && header_length >= 0xE4 {
            u16::from_be_bytes([data[0xF2], data[0xF3]])
        } else {
            0
        };

        // KF8 indices (MOBI version 8)
        let (skel_index, div_index, oth_index) = if mobi_version == 8 && data.len() >= 0x108 {
            (
                u32::from_be_bytes([data[0xFC], data[0xFD], data[0xFE], data[0xFF]]),
                u32::from_be_bytes([data[0xF8], data[0xF9], data[0xFA], data[0xFB]]),
                u32::from_be_bytes([data[0x100], data[0x101], data[0x102], data[0x103]]),
            )
        } else {
            (NULL_INDEX, NULL_INDEX, NULL_INDEX)
        };

        let (fdst_index, fdst_count) = if mobi_version == 8 && data.len() >= 0xC8 {
            (
                u32::from_be_bytes([data[0xC0], data[0xC1], data[0xC2], data[0xC3]]),
                u32::from_be_bytes([data[0xC4], data[0xC5], data[0xC6], data[0xC7]]),
            )
        } else {
            (NULL_INDEX, 0)
        };

        let ncx_index = if data.len() >= 0xF8 {
            u32::from_be_bytes([data[0xF4], data[0xF5], data[0xF6], data[0xF7]])
        } else {
            NULL_INDEX
        };

        Ok(Self {
            compression,
            text_record_count,
            text_record_size,
            encryption,
            mobi_type,
            encoding,
            mobi_version,
            first_image_index,
            title,
            language,
            exth_flags,
            extra_data_flags,
            huff_record_index,
            huff_record_count,
            skel_index,
            div_index,
            oth_index,
            fdst_index,
            fdst_count,
            ncx_index,
            header_length,
        })
    }

    pub fn has_exth(&self) -> bool {
        self.exth_flags & 0x40 != 0
    }
}

/// EXTH Header (extended metadata)
#[derive(Debug, Default)]
pub struct ExthHeader {
    pub title: Option<String>,
    pub authors: Vec<String>,
    pub publisher: Option<String>,
    pub description: Option<String>,
    pub isbn: Option<String>,
    pub asin: Option<String>,
    pub source: Option<String>,
    pub subjects: Vec<String>,
    pub pub_date: Option<String>,
    pub rights: Option<String>,
    pub cover_offset: Option<u32>,
    pub thumbnail_offset: Option<u32>,
    pub language: Option<String>,
    pub kf8_boundary: Option<u32>,
}

impl ExthHeader {
    pub fn parse(data: &[u8], encoding: Encoding) -> io::Result<Self> {
        if data.len() < 12 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "EXTH header too short",
            ));
        }

        if &data[0..4] != b"EXTH" {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Invalid EXTH signature",
            ));
        }

        let _header_length = u32::from_be_bytes([data[4], data[5], data[6], data[7]]);
        let record_count = u32::from_be_bytes([data[8], data[9], data[10], data[11]]);

        let mut exth = ExthHeader::default();
        let mut pos = 12;

        let decode = |bytes: &[u8]| -> String {
            match encoding {
                Encoding::Utf8 => String::from_utf8_lossy(bytes).to_string(),
                _ => {
                    // CP1252 - just use lossy UTF-8 for now
                    String::from_utf8_lossy(bytes).to_string()
                }
            }
        };

        for _ in 0..record_count {
            if pos + 8 > data.len() {
                break;
            }

            let record_type =
                u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
            let record_len =
                u32::from_be_bytes([data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]])
                    as usize;

            if pos + record_len > data.len() {
                break;
            }

            let content = &data[pos + 8..pos + record_len];

            match record_type {
                100 => exth.authors.push(decode(content).trim().to_string()),
                101 => exth.publisher = Some(decode(content).trim().to_string()),
                103 => exth.description = Some(decode(content).trim().to_string()),
                104 => exth.isbn = Some(decode(content).trim().to_string()),
                105 => {
                    for subject in decode(content).split(';') {
                        let s = subject.trim().to_string();
                        if !s.is_empty() {
                            exth.subjects.push(s);
                        }
                    }
                }
                106 => exth.pub_date = Some(decode(content).trim().to_string()),
                109 => exth.rights = Some(decode(content).trim().to_string()),
                112 => exth.source = Some(decode(content).trim().to_string()),
                113 => exth.asin = Some(decode(content).trim().to_string()),
                121 => {
                    if content.len() >= 4 {
                        let val =
                            u32::from_be_bytes([content[0], content[1], content[2], content[3]]);
                        if val != NULL_INDEX {
                            exth.kf8_boundary = Some(val);
                        }
                    }
                }
                201 => {
                    if content.len() >= 4 {
                        let val =
                            u32::from_be_bytes([content[0], content[1], content[2], content[3]]);
                        if val != NULL_INDEX {
                            exth.cover_offset = Some(val);
                        }
                    }
                }
                202 => {
                    if content.len() >= 4 {
                        let val =
                            u32::from_be_bytes([content[0], content[1], content[2], content[3]]);
                        if val != NULL_INDEX {
                            exth.thumbnail_offset = Some(val);
                        }
                    }
                }
                503 => exth.title = Some(decode(content).trim().to_string()),
                524 => exth.language = Some(decode(content).trim().to_string()),
                _ => {}
            }

            pos += record_len;
        }

        Ok(exth)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compression_types() {
        assert_eq!(
            match 2u16.to_be_bytes() {
                [0, 2] => Compression::PalmDoc,
                _ => Compression::None,
            },
            Compression::PalmDoc
        );
    }

    #[test]
    fn test_mobi_header_parse_minimal() {
        // Minimal 16-byte header
        let mut data = vec![0u8; 16];
        data[0..2].copy_from_slice(&2u16.to_be_bytes()); // PalmDoc compression
        data[8..10].copy_from_slice(&10u16.to_be_bytes()); // text_record_count
        data[10..12].copy_from_slice(&4096u16.to_be_bytes()); // text_record_size

        let header = MobiHeader::parse(&data).unwrap();
        assert_eq!(header.compression, Compression::PalmDoc);
        assert_eq!(header.text_record_count, 10);
        assert_eq!(header.text_record_size, 4096);
        assert_eq!(header.encoding, Encoding::Cp1252); // default
    }

    #[test]
    fn test_mobi_header_parse_with_encoding() {
        let mut data = vec![0u8; 32];
        data[0..2].copy_from_slice(&1u16.to_be_bytes()); // No compression
        data[28..32].copy_from_slice(&65001u32.to_be_bytes()); // UTF-8 codepage

        let header = MobiHeader::parse(&data).unwrap();
        assert_eq!(header.compression, Compression::None);
        assert_eq!(header.encoding, Encoding::Utf8);
    }

    #[test]
    fn test_mobi_header_huffman_compression() {
        let mut data = vec![0u8; 32];
        data[0..2].copy_from_slice(&0x4448u16.to_be_bytes()); // "DH" = Huffman

        let header = MobiHeader::parse(&data).unwrap();
        assert_eq!(header.compression, Compression::Huffman);
    }

    #[test]
    fn test_mobi_header_has_exth() {
        let mut data = vec![0u8; 0x84];
        // Set EXTH flag (bit 6 of exth_flags at offset 0x80)
        data[0x80..0x84].copy_from_slice(&0x40u32.to_be_bytes());

        let header = MobiHeader::parse(&data).unwrap();
        assert!(header.has_exth());

        // Without flag
        data[0x80..0x84].copy_from_slice(&0u32.to_be_bytes());
        let header = MobiHeader::parse(&data).unwrap();
        assert!(!header.has_exth());
    }

    #[test]
    fn test_mobi_header_too_short() {
        let data = vec![0u8; 10];
        assert!(MobiHeader::parse(&data).is_err());
    }

    #[test]
    fn test_exth_header_parse() {
        let mut data = Vec::new();
        data.extend_from_slice(b"EXTH"); // signature
        data.extend_from_slice(&100u32.to_be_bytes()); // header length
        data.extend_from_slice(&2u32.to_be_bytes()); // 2 records

        // Record 1: Author (type 100)
        let author = b"Test Author";
        data.extend_from_slice(&100u32.to_be_bytes()); // type
        data.extend_from_slice(&(8 + author.len() as u32).to_be_bytes()); // length
        data.extend_from_slice(author);

        // Record 2: Title (type 503)
        let title = b"Test Title";
        data.extend_from_slice(&503u32.to_be_bytes()); // type
        data.extend_from_slice(&(8 + title.len() as u32).to_be_bytes()); // length
        data.extend_from_slice(title);

        let exth = ExthHeader::parse(&data, Encoding::Utf8).unwrap();
        assert_eq!(exth.authors, vec!["Test Author"]);
        assert_eq!(exth.title, Some("Test Title".to_string()));
    }

    #[test]
    fn test_exth_header_cover_offset() {
        let mut data = Vec::new();
        data.extend_from_slice(b"EXTH");
        data.extend_from_slice(&20u32.to_be_bytes()); // header length
        data.extend_from_slice(&1u32.to_be_bytes()); // 1 record

        // Cover offset record (type 201)
        data.extend_from_slice(&201u32.to_be_bytes()); // type
        data.extend_from_slice(&12u32.to_be_bytes()); // length (8 + 4)
        data.extend_from_slice(&42u32.to_be_bytes()); // cover offset value

        let exth = ExthHeader::parse(&data, Encoding::Utf8).unwrap();
        assert_eq!(exth.cover_offset, Some(42));
    }

    #[test]
    fn test_exth_header_invalid_signature() {
        let data = b"NOTEXTH_____";
        assert!(ExthHeader::parse(data, Encoding::Utf8).is_err());
    }

    #[test]
    fn test_exth_header_too_short() {
        let data = b"EXTH";
        assert!(ExthHeader::parse(data, Encoding::Utf8).is_err());
    }
}