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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! An implementation of [MOBI](https://wiki.mobileread.com/wiki/MOBI) format data parsing and manipulation, written in Rust.
//!
//! The code is available on [GitHub](https://github.com/vv9k/mobi-rs)
//!
//! License: [*MIT*](https://github.com/vv9k/mobi-rs/blob/master/license)
//!
//! ## Examples
//! ### Access basic info
//! ```no_run
//! use mobi::{Mobi, MobiError};
//! fn main() -> Result<(), MobiError> {
//!     let book = vec![0, 0, 0];
//!     // You can either create a Mobi struct from a slice
//!     let m = Mobi::new(&book)?;
//!     // Or from an instance of io::Read
//!     let stdin = std::io::stdin();
//!     let mut handle = stdin.lock();
//!     let m = Mobi::from_read(&mut handle)?;
//!     // Or from filesystem
//!     let m = Mobi::from_path("/some/path/to/book.mobi")?;
//!
//!     // Access metadata
//!     let title = m.title();
//!     let author = m.author().unwrap_or_default();
//!     let publisher = m.publisher().unwrap_or_default();
//!     let desc = m.description().unwrap_or_default();
//!     let isbn = m.isbn().unwrap_or_default();
//!     let pub_date = m.publish_date().unwrap_or_default();
//!     let contributor = m.contributor().unwrap_or_default();
//!
//!     // Access Headers
//!     let metadata = &m.metadata;
//!     let header = &metadata.header; // Normal Header
//!     let pdheader = &metadata.palmdoc; // PalmDOC Header
//!     let mheader = &metadata.mobi; // MOBI Header
//!     let exth = &metadata.exth; // Extra Header
//!
//!     // Access content
//!     let content = m.content_as_string();
//!
//!     Ok(())
//! }
//! ```

/// Module with headers from book containg more extracted data not
/// available through public API.
pub mod headers;
pub mod record;
pub use crate::headers::MobiMetadata;
pub(crate) mod book;
pub(crate) mod compression;
pub(crate) mod reader;
pub(crate) mod writer;

use compression::huff;
use headers::{Compression, Encryption, Language, MobiType, TextEncoding};
pub(crate) use reader::Reader;
use record::{RawRecord, RawRecords};
pub(crate) use writer::Writer;

#[cfg(feature = "time")]
use chrono::NaiveDateTime;
use std::{fs::File, io, io::BufReader, ops::Range, path::Path};
use thiserror::Error;

pub type MobiResult<T> = std::result::Result<T, MobiError>;

#[derive(Debug, Error)]
pub enum MobiError {
    #[error(transparent)]
    IoError(#[from] std::io::Error),
    #[error(transparent)]
    MetadataParseError(#[from] headers::MetadataParseError),
    #[error(transparent)]
    DecodeError(#[from] record::DecodeError),
    #[error(transparent)]
    HuffmanError(#[from] huff::HuffmanError),
}

#[derive(Debug, Default)]
/// Structure that holds parsed ebook information and contents
pub struct Mobi {
    pub content: Vec<u8>,
    pub metadata: MobiMetadata,
}

impl Mobi {
    /// Construct a Mobi object from a slice of bytes
    pub fn new<B: AsRef<Vec<u8>>>(bytes: B) -> MobiResult<Mobi> {
        Mobi::from_reader(&mut Reader::new(std::io::Cursor::new(bytes.as_ref())))
    }

    /// Construct a Mobi object from passed file path
    pub fn from_path<P: AsRef<Path>>(file_path: P) -> MobiResult<Mobi> {
        let mut reader = Reader::new(BufReader::new(File::open(file_path)?));
        Mobi::from_reader(&mut reader)
    }

    /// Construct a Mobi object from an object that implements a Read trait
    pub fn from_read<R: io::Read>(reader: R) -> MobiResult<Mobi> {
        Mobi::from_reader(&mut Reader::new(reader))
    }

    fn from_reader<R: io::Read>(reader: &mut Reader<R>) -> MobiResult<Mobi> {
        let metadata = MobiMetadata::from_reader(reader)?;
        Ok(Mobi {
            content: {
                let mut buf = vec![0; reader.position()];
                reader.read_to_end(&mut buf)?;
                buf
            },
            metadata,
        })
    }

    #[allow(dead_code)]
    fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
        let mut w = Writer::new(writer);

        self.metadata.write_into(&mut w)?;

        let first_offset = self.metadata.records.records[1].offset as usize;
        let fill = first_offset - w.bytes_written();
        w.write_be(vec![0; fill])?;
        // TODO: Consider record compression and everything else.
        w.write_be(&self.content[first_offset..])
    }

    /// Returns an author of this book
    pub fn author(&self) -> Option<String> {
        self.metadata.author()
    }

    /// Returns this books publisher
    pub fn publisher(&self) -> Option<String> {
        self.metadata.publisher()
    }

    /// Returns description record if such exists
    pub fn description(&self) -> Option<String> {
        self.metadata.description()
    }

    /// Returns isbn record if such exists
    pub fn isbn(&self) -> Option<String> {
        self.metadata.isbn()
    }

    /// Returns publish_date record if such exists
    pub fn publish_date(&self) -> Option<String> {
        self.metadata.publish_date()
    }

    /// Returns contributor record if such exists
    pub fn contributor(&self) -> Option<String> {
        self.metadata.contributor()
    }

    /// Returns title record if such exists
    pub fn title(&self) -> String {
        self.metadata.title()
    }

    /// Returns text encoding used in ebook
    pub fn text_encoding(&self) -> TextEncoding {
        self.metadata.text_encoding()
    }

    /// Returns type of this ebook
    pub fn mobi_type(&self) -> MobiType {
        self.metadata.mobi_type()
    }

    /// Returns language of the ebook
    pub fn language(&self) -> Language {
        self.metadata.language()
    }

    #[cfg(feature = "time")]
    /// Returns creation datetime
    /// This field is only available using `time` feature
    pub fn created_datetime(&self) -> NaiveDateTime {
        self.metadata.created_datetime()
    }

    #[cfg(feature = "time")]
    /// Returns modification datetime
    /// This field is only available using `time` feature
    pub fn mod_datetime(&self) -> NaiveDateTime {
        self.metadata.mod_datetime()
    }

    #[cfg(not(feature = "time"))]
    /// Returns creation time as u32 timestamp
    pub fn created_time(&self) -> u32 {
        self.metadata.created_time()
    }

    #[cfg(not(feature = "time"))]
    /// Returns last modification time as u32 timestamp
    pub fn mod_time(&self) -> u32 {
        self.metadata.mod_time()
    }

    /// Returns compression method used on this file
    pub fn compression(&self) -> Compression {
        self.metadata.compression()
    }
    /// Returns encryption method used on this file
    pub fn encryption(&self) -> Encryption {
        self.metadata.encryption()
    }

    /// Returns the readable reacord range - from first content record to first
    /// non book index.
    pub fn readable_records_range(&self) -> Range<usize> {
        self.metadata.mobi.first_content_record as usize
            ..self.metadata.mobi.first_non_book_index as usize
    }

    /// Returns raw records that contain compressed, encrypted and encoded content slices.
    pub fn raw_records(&self) -> RawRecords {
        self.metadata.records.parse(&self.content)
    }

    /// Returns all records classified as image records.
    pub fn image_records(&self) -> Vec<RawRecord> {
        self.raw_records()
            .range(self.metadata.mobi.first_image_index as usize..)
            .iter()
            .copied()
            .filter(|record| record.is_image_record())
            .collect()
    }

    fn palmdoc_string_lossy(&self) -> String {
        let encoding = self.text_encoding();
        self.raw_records()
            .range(self.readable_records_range())
            .iter()
            .map(|record| record.decompress_palmdoc().to_string_lossy(encoding))
            .collect()
    }

    fn palmdoc_string(&self) -> MobiResult<String> {
        let encoding = self.text_encoding();
        let mut s = String::new();

        for record in self.raw_records().range(self.readable_records_range()) {
            let content = record.decompress_palmdoc().to_string(encoding)?;
            s.push_str(&content);
        }
        Ok(s)
    }

    fn no_compression_string_lossy(&self) -> String {
        let encoding = self.text_encoding();
        self.raw_records()
            .range(self.readable_records_range())
            .iter()
            .map(|r| record::content_to_string_lossy(r.content, encoding))
            .collect()
    }

    fn no_compression_string(&self) -> MobiResult<String> {
        let encoding = self.text_encoding();
        let mut s = String::new();
        for record in self.raw_records().range(self.readable_records_range()) {
            let content = record::content_to_string(record.content, encoding)?;
            s.push_str(&content);
        }
        Ok(s)
    }

    fn huff_data(&self) -> MobiResult<Vec<Vec<u8>>> {
        let records = self.raw_records();
        let huff_start = self.metadata.mobi.first_huff_record as usize;
        let huff_count = self.metadata.mobi.huff_record_count as usize;
        let huffs: Vec<_> = records
            .range(huff_start..huff_start + huff_count)
            .iter()
            .map(|record| record.content)
            .collect();

        let sections: Vec<_> = records
            .range(self.readable_records_range())
            .iter()
            .map(|record| record.content)
            .collect();

        Ok(huff::decompress(&huffs, &sections)?)
    }

    fn huff_string_lossy(&self) -> MobiResult<String> {
        let encoding = self.text_encoding();
        let mut s = String::new();
        let data = self.huff_data()?;

        for section in data {
            let content = record::content_to_string_lossy(&section, encoding);
            s.push_str(&content);
        }
        Ok(s)
    }

    fn huff_string(&self) -> MobiResult<String> {
        let encoding = self.text_encoding();
        let mut s = String::new();
        let data = self.huff_data()?;

        for section in data {
            let content = record::content_to_string(&section, encoding)?;
            s.push_str(&content);
        }
        Ok(s)
    }

    /// Returns all readable records content decompressed as a String.
    /// There are only two supported encodings in mobi format (UTF8, WIN1252)
    /// and both are losely converted by this function
    pub fn content_as_string_lossy(&self) -> String {
        match self.compression() {
            Compression::No => self.no_compression_string_lossy(),
            Compression::PalmDoc => self.palmdoc_string_lossy(),
            Compression::Huff => self.huff_string_lossy().unwrap_or_default(),
        }
    }

    /// Returns all readable records content decompressed as a String.
    /// This function is a strict version returning error on first encountered
    /// decoding error.
    pub fn content_as_string(&self) -> MobiResult<String> {
        match self.compression() {
            Compression::No => self.no_compression_string(),
            Compression::PalmDoc => self.palmdoc_string(),
            Compression::Huff => self.huff_string(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_no_records() {
        let bytes = [
            173, 21, 58, 173, 252, 173, 173, 173, 173, 173, 173, 173, 165, 173, 173, 173, 0, 0, 0,
            255, 255, 255, 255, 255, 255, 255, 139, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 173, 173, 173, 173, 0, 0, 0, 0,
            0, 0, 0, 173, 173, 173, 33, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 70, 70, 70, 70, 70, 70, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            173, 162, 162, 162, 173, 173, 84, 255,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_backwards_cursor_1() {
        let bytes = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 21, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 236, 0, 0, 3, 0, 173, 173, 173,
            173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 162, 162, 162, 173, 162,
            255, 255, 255, 5, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 121, 121, 121, 121, 121, 121,
            121, 121, 121, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244,
            244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 3, 0, 0, 0, 0, 0,
            0, 0, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244,
            244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 244, 121, 121, 0, 193,
            0, 0, 0, 0, 0, 65, 0, 0, 64, 0, 0, 0, 0, 0, 0, 10,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_backwards_cursor_2() {
        let bytes = [
            0, 0, 0, 0, 0, 0, 0, 198, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 193, 2, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_offset_mismatch() {
        let bytes = [
            0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 231, 0,
            2, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 65, 0, 0, 0, 62, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 10,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_offset_overflow() {
        let bytes = [
            0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 65, 0,
            0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 193, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 251, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 245, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_small_record() {
        let bytes = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            50, 3, 128, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 65, 65, 65, 65, 65, 65, 65,
            65, 0, 0, 0, 0, 0, 0, 50, 3, 128, 0, 0, 0, 0, 0, 0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 50, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 65, 65, 65, 65, 65, 65, 65,
            65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
            65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
            65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
            65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 231, 0, 2, 0, 0, 0, 0,
            193, 0, 0, 0, 254, 255, 0, 173, 173, 173, 173, 0, 0, 0, 0, 0, 0, 0, 173, 173, 173, 33,
            173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 0, 0, 0, 0, 0, 0, 0, 48, 48, 48,
            48, 48, 48, 48, 48, 48, 48, 49, 0, 10,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_subtract_overflow() {
        let bytes = [
            211, 147, 90, 255, 64, 255, 211, 211, 211, 88, 84, 77, 79, 66, 73, 77, 79, 66, 73, 10,
            1, 23, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45, 84, 69, 88,
            84, 77, 79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10, 211, 61, 45, 84, 69, 88, 84, 77,
            79, 66, 73, 77, 79, 66, 73, 10, 1, 23, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255,
            211, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 0, 0, 248, 255,
            255, 255, 23, 0, 0, 0, 0, 0, 0, 0, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 79, 66,
            73, 10, 1, 23, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45, 84,
            69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0, 77, 79, 66, 73, 211, 147, 90, 255, 64, 255,
            211, 211, 9, 0, 0, 0, 0, 0, 0, 0, 211, 10, 211, 211, 10, 1, 255, 0, 188, 255, 211, 1,
            23, 0, 0, 0, 0, 0, 0, 10, 20, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73,
            77, 79, 66, 73, 10, 1, 23, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 211, 0, 255,
            255, 255, 255, 255, 211, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0,
            248, 255, 255, 255, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 77, 79, 66, 73, 10, 1,
            255, 0, 188, 255, 211, 10, 211, 61, 45, 84, 69, 10, 211, 61, 45, 84, 69, 79, 75,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_read_out_of_bounds() {
        let bytes = [
            211, 147, 90, 255, 64, 255, 211, 211, 211, 10, 211, 211, 211, 255, 255, 255, 255, 255,
            211, 10, 211, 61, 45, 84, 69, 88, 84, 1, 0, 0, 0, 188, 128, 255, 42, 0, 211, 207, 147,
            90, 255, 64, 255, 211, 211, 211, 10, 211, 211, 108, 255, 255, 255, 255, 255, 211, 10,
            211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 77, 79, 66, 73, 10, 1, 23, 0, 0, 0, 0, 0,
            0, 0, 255, 255, 0, 0, 0, 159, 10, 211, 61, 211, 255, 69, 88, 84, 77, 79, 66, 73, 0, 0,
            0, 232, 10, 20, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 211, 147, 90, 255,
            64, 255, 211, 211, 211, 88, 84, 77, 79, 66, 73, 77, 79, 66, 73, 10, 1, 23, 0, 0, 0, 0,
            0, 0, 0, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73,
            10, 20, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 0, 0, 66, 73, 10, 1, 255, 0,
            211, 1, 23, 84, 69, 88, 84, 0, 0, 0, 0, 0, 0, 10, 20, 0, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 35, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 0,
            0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 248, 255, 255, 255, 211, 61, 45, 84, 69, 88, 84, 77,
            1, 0, 0, 0, 0, 0, 0, 1, 79, 66, 73, 77, 79, 66, 255, 255, 255, 255, 211, 0, 255, 255,
            255, 255, 0, 188, 73, 10, 1, 255, 0, 188, 255, 211, 1, 23, 0, 0, 0, 0, 0, 0, 0, 255,
            255, 255, 255, 255, 211, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0,
            248, 255, 255, 255, 211, 61, 255, 211, 10, 211, 61, 45, 84, 69, 10, 211, 45, 84, 69,
            88, 84, 77, 79, 66, 73, 77, 79, 66, 61, 45, 84, 73, 10, 1, 255, 0, 188, 255, 211, 10,
            69, 211, 61, 45, 84, 69, 10, 211, 61, 45, 84, 69, 79, 79, 75, 75,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_overflowing_record() {
        let bytes = [
            211, 147, 90, 255, 64, 255, 211, 211, 211, 88, 84, 77, 79, 66, 73, 77, 79, 66, 2, 0, 0,
            0, 73, 10, 1, 23, 0, 0, 0, 0, 0, 0, 77, 79, 66, 2, 0, 0, 0, 73, 10, 1, 23, 0, 0, 0,
            255, 255, 255, 22, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45, 84, 69, 88, 84, 77,
            79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10, 211, 61, 45, 84, 69, 88, 93, 77, 79, 66,
            73, 77, 79, 66, 73, 10, 1, 23, 0, 11, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 211, 10, 211,
            61, 45, 255, 211, 10, 211, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0, 0, 0,
            0, 0, 0, 10, 211, 61, 45, 84, 69, 88, 84, 93, 79, 66, 73, 77, 79, 66, 73, 84, 77, 79,
            66, 2, 0, 0, 0, 73, 10, 1, 0, 10, 20, 0, 255, 255, 255, 255, 45, 84, 69, 88, 84, 77,
            79, 66, 73, 0, 0, 0, 255, 255, 255, 22, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45,
            84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10, 211, 61, 45, 84, 69,
            88, 93, 77, 79, 66, 73, 77, 79, 66, 73, 10, 1, 23, 0, 11, 0, 0, 0, 0, 0, 0, 0, 255,
            255, 211, 61, 45, 255, 211, 10, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10,
            211, 61, 45, 84, 69, 88, 84, 93, 79, 66, 73, 77, 79, 66, 73, 84, 77, 79, 66, 2, 0, 0,
            0, 73, 10, 1, 0, 10, 20, 0, 255, 255, 255, 255, 45, 84, 69, 88, 84, 77, 79, 66, 73,
            179, 176, 189, 182, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 75, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 10, 1, 255, 0, 230, 230, 230, 230, 230, 230,
            230, 0, 0, 255, 255, 255, 255, 255, 211, 10, 211, 61, 45, 255, 211, 10, 211, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 45, 84, 69, 88, 84, 77,
            79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10, 211, 61, 45, 84, 69, 88, 84, 93, 79, 66,
            73, 77, 79, 66, 73, 84, 77, 79, 66, 2, 0, 0, 0, 73, 10, 1, 0, 10, 20, 0, 255, 255, 255,
            255, 45, 84, 69, 88, 84, 77, 79, 66, 73, 179, 176, 189, 182, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 75, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 179, 176, 189, 182, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 79, 66, 0, 0, 0, 0, 0, 0, 195, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 240, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 0, 0, 255, 255, 255, 255, 255, 211,
            10, 211, 61, 45, 255, 211, 10, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 61, 45, 84, 69, 88, 84, 77, 79, 66, 73, 10, 20, 0, 0, 0, 0, 0, 0, 0, 10,
            211, 61, 45, 84, 69, 88, 84, 93, 79, 66, 73, 77, 79, 66, 73, 84, 77, 79, 66, 2, 0, 0,
            0, 73, 10, 1, 0, 10, 20, 0, 255, 255, 255, 255, 45, 84, 69, 88, 84, 77, 79, 66, 73,
            179, 176, 189, 182, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 75, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 10, 1, 255, 0, 188, 255, 61, 45, 84, 69, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 39, 0, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230, 230,
            230, 230, 230, 230, 230, 230, 230, 230, 0, 0, 0, 0, 0, 79, 75,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_set_position_oom() {
        let bytes = [
            242, 242, 242, 242, 242, 242, 84, 80, 90, 55, 242, 242, 242, 242, 242, 242, 242, 242,
            242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 130, 62, 178, 126, 126, 126, 126,
            130, 9, 68, 82, 77, 73, 79, 78, 238, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
            126, 126, 126, 126, 126, 126, 84, 69, 88, 84, 82, 69, 65, 68, 126, 126, 126, 126, 242,
            136, 126, 1, 0, 8, 242, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
            15, 15, 15, 15, 15, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 84, 69,
            255, 255, 255, 255, 9, 0, 0, 0, 0, 0, 0, 0, 255, 126, 126, 126, 126, 126, 126, 126,
            126, 242, 0, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 84, 69, 88, 84, 82, 69,
            65, 68, 82, 175, 130, 129, 129, 77, 79, 66, 73, 0, 0, 0, 232, 122, 126, 255, 255, 255,
            255, 255, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
            15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
            15, 15, 1, 69, 88, 84, 15, 15, 15, 15, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
            15, 15, 15, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 48, 126, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163,
            163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163,
            163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 247,
            247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247,
            84, 69, 255, 255, 255, 255, 255, 255, 40, 255, 255, 255, 255, 255, 255, 126, 126, 126,
            126, 126, 93, 92, 92, 92, 92, 92, 92, 92, 163, 163, 163, 163, 163, 212, 163, 163, 163,
            163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163,
            163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_records_ascending() {
        let bytes = [
            242, 242, 242, 242, 242, 242, 84, 80, 90, 51, 242, 242, 242, 242, 242, 242, 242, 242,
            242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 130, 62, 178, 126, 126, 126, 126,
            130, 9, 68, 82, 77, 73, 79, 248, 2, 0, 0, 0, 0, 0, 0, 126, 126, 126, 126, 126, 126,
            126, 126, 126, 126, 84, 69, 88, 84, 82, 69, 65, 68, 126, 126, 250, 126, 242, 136, 126,
            1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247,
            247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 84, 69,
            255, 255, 255, 255, 255, 255, 255, 255, 127, 255, 255, 255, 255, 126, 126, 126, 126,
            126, 126, 126, 126, 242, 0, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 84, 69,
            88, 84, 82, 69, 65, 68, 82, 175, 130, 129, 129, 77, 79, 66, 73, 0, 0, 0, 232, 122, 0,
            0, 4, 228, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            96, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 146, 3,
            3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
            3, 3, 3, 3, 3, 3, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }

    #[test]
    fn test_backwards_name_offset() {
        let bytes = [
            242, 242, 242, 242, 242, 242, 84, 80, 90, 51, 242, 242, 242, 242, 242, 242, 242, 242,
            242, 242, 242, 242, 242, 250, 242, 118, 178, 242, 242, 242, 62, 131, 126, 126, 126,
            130, 15, 68, 82, 77, 73, 79, 76, 238, 126, 126, 126, 126, 126, 126, 126, 103, 126, 126,
            126, 126, 126, 126, 126, 126, 84, 69, 88, 84, 77, 79, 66, 73, 126, 147, 126, 127, 242,
            136, 41, 126, 0, 1, 0, 0, 0, 0, 0, 0, 4, 126, 84, 69, 88, 84, 82, 242, 242, 84, 80, 90,
            51, 242, 242, 242, 242, 242, 129, 129, 77, 79, 66, 73, 0, 0, 0, 242, 242, 242, 242,
            127, 126, 126, 58, 242, 242, 84, 80, 90, 50, 242, 242, 242, 242, 242, 242, 242, 242,
            242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 130, 62, 178, 126, 126, 126, 126,
            130, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 68, 82, 77, 73, 79,
            78, 238, 126, 126, 126, 126, 126, 126, 126, 127, 126, 126, 126, 126, 242, 242, 242,
            242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242,
            242, 242, 242, 242, 242, 242, 242, 242, 178, 130, 126, 126, 126, 126, 126, 126, 126,
            126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 242, 126, 126,
            126, 126, 126, 126, 126, 126, 84, 69, 88, 84, 82, 69, 65, 68, 82, 126, 126, 126, 242,
            242, 242, 84, 80, 90, 50, 242, 242, 242, 242, 130, 62, 178, 126, 126, 126, 126, 130,
            15, 68, 82, 77, 73, 79, 78, 238, 126, 109, 109, 109, 133, 133, 122, 126, 48, 234, 68,
            82, 77, 73, 79, 78, 238, 65, 68, 126, 126, 126, 126, 242, 136, 126, 1, 0, 1, 0, 242, 0,
            126, 126, 126, 126, 126, 126, 88, 88, 88, 88, 88, 88, 88, 88, 48, 88, 88, 88, 88, 88,
            88, 40, 39, 73, 68, 82, 175, 41, 130, 129, 129, 77, 79, 66, 73, 122, 126, 48, 126, 163,
            163, 44, 163, 163, 163, 163, 163, 40, 39, 40, 172,
        ];
        assert!(Mobi::new(bytes.to_vec()).is_err());
    }
}