Technical Architecture: Decompressing PalmDOC & Synthesizing EPUB 3
The conversion from Mobipocket (MOBI) to Electronic Publication (EPUB 3) represents the reverse engineering of one of the digital publishing industry's earliest mobile binary container formats. While modern publishing relies on modular, semantic XHTML containers, MOBI originated in the year 2000 as an extension of the Palm OS Palm Database (PDB) format.
To transform a binary MOBI file into an interoperable EPUB that renders seamlessly on Kobo, Apple Books, and Android readers, 2ClickKit executes a multi-tiered decoding and compilation pipeline entirely in the client browser.
Phase 1: Palm Database Container Parsing
The engine first deserializes the 78-byte Palm Database header and the subsequent record offset table:
- Header Verification: Validates database type (
'BOOK') and creator FourCC code ('MOBI'). - Record Offset Mapping: Extracts offsets for all records stored in the file to establish memory boundaries for text chunks and embedded media.
- Record 0 Inspection: Reads the PalmDOC compression mode (Type 1 for uncompressed, Type 2 for PalmDOC LZ77) and extracts the MOBI header length, text encoding (UTF-8 vs CP1252), and EXTH metadata records.
Phase 2: PalmDOC LZ77 Decompression Mechanics
When a MOBI file utilizes PalmDOC LZ77 compression, text data is stored in dense 4,096-byte blocks. The in-browser decompressor iterates through each byte according to the following mathematical rules:
| Byte Range (Hex) | Classification | Decompression Operation |
|---|---|---|
0x00 | Literal Null | Outputs literal null byte 0x00. |
0x01 - 0x08 | Literal Sequence | Reads the following N bytes directly into the output stream without modification. |
0x09 - 0x7F | Single ASCII Character | Outputs the byte value directly as a character token. |
0x80 - 0xBF | Distance / Length Pointer | Reads next byte b1. Distance = ((b0 & 0x03) << 8) | b1. Length = ((b0 >> 2) & 0x07) + 3. Copies length bytes from distance positions backwards in the buffer. |
0xC0 - 0xFF | Space + Character Pair | Outputs an ASCII space (0x20) followed by the character code b0 ^ 0x80. |
Phase 3: Chapter Detection & XHTML Spine Assembly
Once all text records are decompressed and joined into a unified HTML document, the engine analyzes the structure to divide the single monolithic stream into modular chapters:
- Kindle Page Breaks: Scans for
<mbp:pagebreak/>tags and CSS page-break properties. - Heading Hierarchy: Detects
<h1>,<h2>, and<title>elements to assign meaningful chapter titles. - XML Well-Formedness: Wraps each section into a valid, standalone XHTML document adhering to XML namespace requirements (
xmlns="http://www.w3.org/1999/xhtml").
Phase 4: EPUB 3 Packaging & Dual Navigation
An interoperable EPUB requires strict conformance to IDPF and W3C specifications:
| Package File | Standard Specification | Function in Reading Systems |
|---|---|---|
mimetype | Stored (Zero Compression) | Identifies the file as application/epub+zip for operating system file type detection. |
META-INF/container.xml | Open Container Format (OCF) | Points reading applications to the primary package manifest at OEBPS/content.opf. |
OEBPS/nav.xhtml | EPUB 3 Navigation Document | Semantic HTML5 <nav epub:type="toc"> list for modern e-readers. |
OEBPS/toc.ncx | EPUB 2 NCX Specification | Hierarchical XML navigation tree for older hardware e-readers and legacy software. |
OEBPS/content.opf | Package Document | Catalogs manifest items, metadata Dublin Core tags, and linear reading spine order. |
Why Client-Side In-Browser Conversion Is Superior
Privacy is the defining feature of 2ClickKit. Traditional conversion sites force users to transmit unreleased manuscripts, sensitive corporate training books, or personal digital diaries across public networks to cloud servers.
- Zero Server Storage: Your eBook never touches an external disk, eliminating data leaks and intellectual property theft.
- No Cloud Bandwidth Lag: Processing occurs locally at gigabyte-per-second memory speeds.
- Full Offline Support: Once the webpage assets are cached in your browser, the tool operates seamlessly without an active internet connection.
Developer Code Example: Decompressing PalmDOC in JavaScript
// Client-side implementation of PalmDOC LZ77 decompressor
function decompressPalmDocBlock(data) {
const output = [];
let i = 0;
const len = data.length;
while (i < len) {
const b0 = data[i++];
if (b0 === 0x00) {
output.push(0x00);
} else if (b0 >= 0x01 && b0 <= 0x08) {
for (let j = 0; j < b0 && i < len; j++) output.push(data[i++]);
} else if (b0 >= 0x09 && b0 <= 0x7f) {
output.push(b0);
} else if (b0 >= 0x80 && b0 <= 0xbf) {
if (i >= len) break;
const b1 = data[i++];
const distance = ((b0 & 0x03) << 8) | b1;
const length = ((b0 >> 2) & 0x07) + 3;
const start = output.length - distance;
for (let j = 0; j < length; j++) {
output.push(output[start + j] || 0x20);
}
} else if (b0 >= 0xc0) {
output.push(0x20);
output.push(b0 ^ 0x80);
}
}
return new Uint8Array(output);
} Tips for Optimal Reading Quality
- Check Table of Contents: Open the EPUB in Apple Books or Calibre and click the Table of Contents button to verify all chapter breaks are correctly indexed.
- Cross-Platform Transfer: To read your newly converted EPUB on Kobo, connect your e-reader via USB and drag the file directly into the main storage folder.