Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/carve/fmt/mobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,70 @@ i64 vMobi(ByteSource& s, i64 off, i64 max, const CarveSpec&) {
i64 table = off + 78;
if (table + 8 * (i64)num > off + max) return -1;
u32 prev = 78 + 8 * num; // record 0 must follow the table
u32 last_rec_off = 0;
for (u32 i = 0; i < num; i++) {
u32 o = s.be32(table + 8 * (i64)i);
if (o < prev) return -1;
prev = o;
if (i == num - 1) last_rec_off = o;
}
// Record 0 is the MOBI header; its first 8 bytes are the PalmDoc magic.
u32 r0 = s.be32(table);
auto sig = s.read(off + (i64)r0, 8);
if (sig.size() < 8 || std::memcmp(sig.data(), "TEXtREAd", 8) != 0) return -1;
// The last record has no stored size: report an unknown size.
return 0;
i64 last_off = off + (i64)last_rec_off;
if (last_off >= off + max) return -1;

i64 last_len = 0;
if (last_off + 4 <= off + max) {
u32 tag = s.be32(last_off);
if (tag == 0x464C4953) { // "FLIS"
u32 len = s.be32(last_off + 4);
last_len = (len > 0 && len <= 1048576) ? 8 + (i64)len : 36;
} else if (tag == 0x46434953) { // "FCIS"
u32 len = s.be32(last_off + 4);
last_len = (len > 0 && len <= 1048576) ? 8 + (i64)len : 44;
} else if (tag == 0x46445354) { // "FDST"
u32 len = s.be32(last_off + 4);
last_len = (len > 0 && len <= 1048576) ? 8 + (i64)len : 12;
} else if (tag == 0x494E4458) { // "INDX"
u32 len = s.be32(last_off + 4);
last_len = (len > 0 && len <= 1048576) ? (i64)len : 192;
} else if (tag == 0x53524353) { // "SRCS"
u32 len = s.be32(last_off + 4);
last_len = (len > 0 && len <= 1048576) ? 8 + (i64)len : 16;
} else if (tag == 0xE98E0D0A) { // MOBI EOF record marker
last_len = 4;
} else if (tag == 0x89504E47) { // PNG image record
for (i64 p = last_off + 8; p + 12 <= off + max; p++) {
if (s.be32(p + 4) == 0x49454E44) {
last_len = (p + 12) - last_off;
break;
}
}
} else if ((tag & 0xFFFF0000) == 0xFFD80000) { // JPEG image record
for (i64 p = last_off + 2; p + 2 <= off + max; p++) {
if (s.be16(p) == 0xFFD9) {
last_len = (p + 2) - last_off;
break;
}
}
}
}

if (last_len <= 0) {
if (num > 1) {
i64 prev_span = (i64)last_rec_off - (i64)r0;
i64 avg_size = prev_span / (i64)(num - 1);
last_len = std::min<i64>(std::max<i64>(avg_size, 16), 65536);
} else {
last_len = 240;
}
}

i64 total_size = (i64)last_rec_off + last_len;
if (total_size > max) return -1;
return total_size;
}void registerFmt_mobi(Registry& r) {
auto add = [&](CarveSpec c) { r.push_back(std::move(c)); };

Expand Down
3 changes: 2 additions & 1 deletion tests/carve_spec_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,8 @@ def make_mobi(_):
rec0[0:8] = b'TEXtREAd'
rec0[8:240] = junk(232)
out[512:752] = bytes(rec0)
return bytes(out) + nonzero(junk(400))
rec1 = b'FLIS' + u32be(392) + junk(392)
return bytes(out) + rec1


def make_chm(_):
Expand Down
Loading