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
pub(crate) mod book;
pub(crate) mod exth;
pub(crate) mod header;
pub(crate) mod lz77;
pub(crate) mod mobih;
pub(crate) mod palmdoch;
pub(crate) mod record;
use byteorder::{BigEndian, ReadBytesExt};
#[cfg(feature = "time")]
use chrono::prelude::*;
use exth::BookInfo;
pub use exth::ExtHeader;
pub use header::Header;
pub use mobih::MobiHeader;
use palmdoch::Compression;
pub use palmdoch::PalmDocHeader;
pub use record::Record;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io::Cursor;
use std::io::Read;
use std::path::Path;
#[derive(Debug, Default)]
pub struct Mobi {
pub contents: Vec<u8>,
pub header: Header,
pub palmdoc: PalmDocHeader,
pub mobi: MobiHeader,
pub exth: ExtHeader,
pub records: Vec<Record>,
}
impl Mobi {
pub fn new<P: AsRef<Path>>(file_path: P) -> Result<Mobi, std::io::Error> {
let contents = fs::read(file_path)?;
let header = Header::parse(&contents)?;
let palmdoc = PalmDocHeader::parse(&contents, header.num_of_records)?;
let mobi = MobiHeader::parse(&contents, header.num_of_records)?;
let exth = {
if mobi.has_exth_header {
ExtHeader::parse(&contents, header.num_of_records)?
} else {
ExtHeader::default()
}
};
let records = Record::parse_records(
&contents,
header.num_of_records,
mobi.extra_bytes,
palmdoc.compression_en(),
)?;
Ok(Mobi {
contents,
header,
palmdoc,
mobi,
exth,
records,
})
}
pub fn from_reader<R: Read>(reader: R) -> Result<Mobi, std::io::Error> {
let mut contents = vec![];
for byte in reader.bytes() {
contents.push(byte?);
}
let header = Header::parse(&contents)?;
let palmdoc = PalmDocHeader::parse(&contents, header.num_of_records)?;
let mobi = MobiHeader::parse(&contents, header.num_of_records)?;
let exth = {
if mobi.has_exth_header {
ExtHeader::parse(&contents, header.num_of_records)?
} else {
ExtHeader::default()
}
};
let records = Record::parse_records(
&contents,
header.num_of_records,
mobi.extra_bytes,
palmdoc.compression_en(),
)?;
Ok(Mobi {
contents,
header,
palmdoc,
mobi,
exth,
records,
})
}
pub fn author(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Author)
}
pub fn publisher(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Publisher)
}
pub fn description(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Description)
}
pub fn isbn(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Isbn)
}
pub fn publish_date(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::PublishDate)
}
pub fn contributor(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Contributor)
}
pub fn title(&self) -> Option<&String> {
self.exth.get_book_info(BookInfo::Title)
}
pub fn text_encoding(&self) -> Option<String> {
self.mobi.text_encoding()
}
pub fn mobi_type(&self) -> Option<String> {
self.mobi.mobi_type()
}
pub fn language(&self) -> Option<String> {
self.mobi.language()
}
#[cfg(feature = "time")]
pub fn created_datetime(&self) -> NaiveDateTime {
self.header.created_datetime()
}
#[cfg(feature = "time")]
pub fn mod_datetime(&self) -> NaiveDateTime {
self.header.mod_datetime()
}
pub fn compression(&self) -> Option<String> {
self.palmdoc.compression()
}
pub fn encryption(&self) -> Option<String> {
self.palmdoc.encryption()
}
pub fn content_as_string(&self) -> String {
(1..self.palmdoc.record_count - 1)
.map(|i| self.records[i as usize].to_string())
.collect()
}
pub fn content_slice(&self, b: usize, e: usize) -> Option<String> {
if (b >= 1) && (b <= e) && (e < (self.palmdoc.record_count - 1) as usize) {
Some(
(b..e)
.map(|i| self.records[i as usize].to_string())
.collect(),
)
} else {
None
}
}
}
#[cfg(feature = "fmt")]
impl fmt::Display for Mobi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let empty_str = String::from("");
write!(
f,
"
------------------------------------------------------------------------------------
Title: {}
Author: {}
Publisher: {}
Description: {}
ISBN: {}
Publish Date: {}
Contributor: {}
------------------------------------------------------------------------------------
{}
------------------------------------------------------------------------------------
{}
------------------------------------------------------------------------------------
{}
------------------------------------------------------------------------------------
{}
------------------------------------------------------------------------------------",
self.title().unwrap_or(&empty_str),
self.author().unwrap_or(&empty_str),
self.publisher().unwrap_or(&empty_str),
self.description().unwrap_or(&empty_str),
self.isbn().unwrap_or(&empty_str),
self.publish_date().unwrap_or(&empty_str),
self.contributor().unwrap_or(&empty_str),
self.header,
self.palmdoc,
self.mobi,
self.exth,
)
}
}
pub(crate) trait FieldHeaderEnum {}
pub(crate) trait HeaderField<T: FieldHeaderEnum> {
fn position(self) -> u16;
}
pub(crate) struct Reader<'r> {
cursor: Cursor<&'r [u8]>,
num_of_records: u16,
}
impl<'r> Reader<'r> {
pub(crate) fn new(content: &[u8], num_of_records: u16) -> Reader {
Reader {
cursor: Cursor::new(content),
num_of_records,
}
}
pub(crate) fn read_i16_header<T: FieldHeaderEnum, F: HeaderField<T>>(
&mut self,
field: F,
) -> Result<i16, std::io::Error> {
self.cursor
.set_position(field.position() as u64 + u64::from(self.num_of_records * 8));
self.cursor.read_i16::<BigEndian>()
}
pub(crate) fn read_u16_header<T: FieldHeaderEnum, F: HeaderField<T>>(
&mut self,
field: F,
) -> Result<u16, std::io::Error> {
self.cursor
.set_position(field.position() as u64 + u64::from(self.num_of_records * 8));
self.cursor.read_u16::<BigEndian>()
}
pub(crate) fn read_u32_header<T: FieldHeaderEnum, F: HeaderField<T>>(
&mut self,
field: F,
) -> Result<u32, std::io::Error> {
self.cursor
.set_position(field.position() as u64 + u64::from(self.num_of_records * 8));
self.cursor.read_u32::<BigEndian>()
}
pub(crate) fn read_string_header<T: FieldHeaderEnum, F: HeaderField<T>>(
&mut self,
field: F,
len: u64,
) -> String {
let position = field.position();
String::from_utf8_lossy(
&self.cursor.get_ref()[position as usize..(position as u64 + len) as usize],
)
.to_owned()
.to_string()
}
}