EBook:...pocket(3pm)
π EBook::Tools::Mobipocket(3pm) π
okay bestie, welcome to your new fave User Contributed Perl Documentation page. This is EBook::Tools::Mobipocket, and it's literally a Palm::PDB handler for manipulating the Mobipocket format. no cap. π¦
β¨ NAME β¨
EBook::Tools::Mobipocket β Palm::PDB handler for manipulating the Mobipocket format. π
π SYNOPSIS π
use it like this, bestie:
use EBook::Tools::Mobipocket qw(:all);
my $mobi = EBook::Tools::Mobipocket->new();
$mobi->Load('filename.prc');
print "Title: ",$mobi->{title},"\n";
print "Author: ",$mobi->{header}{exth}{author},"\n";
print "Language: ",$mobi->{header}{mobi}{language},"\n";
my $mobigen = find_mobigen();
system_mobigen('myfile.opf');
π DEPENDENCIES π
You need these modules, periodt:
Bit::VectorCompress::ZlibHTML::TreeImage::SizeList::MoreUtilsP5-PalmString::CRC32
π CONSTRUCTOR π
new()
Instantiates a new Ebook::Tools::Mobipocket object. π«Ά
π ACCESSOR METHODS π
drm()
Returns 1 if the drmoffset header value is neither 0 nor 0xffffffff. Returns undef if drmoffset is undefined. Returns 0 otherwise. π₯
text()
Returns the text of the file. π
write_images()
Writes each image record to the disk. Returns the number of images written. πΈ
write_text($filename)
Writes the book text to disk with the given filename. This filename must match the filename given to fix_html() for the internal links to be consistent. Croaks if $filename is not specified. Returns 1 on success, or undef if there was no text to write. π
write_unknown_records()
Writes each unidentified record to disk with a filename in the format of 'raw-record-####', where #### is the record number (not the record ID). Returns the number of records written. π
π§ MODIFIER METHODS π§
These methods have two naming/capitalization schemes β methods directly related to the subclassing of Palm::PDB use its MethodName capitalization style. Any other methods are lowercase_with_underscores for consistency with the rest of EBook::Tools. π
Load($filename)
Sets $self->{filename} and then loads and parses the file specified by $filename, calling ParseRecord(%record) on every record found. If DictionaryHuffman compression is detected, text records will be left untouched during the ParseRecord pass, and uncompress_dictionaryhuffman_records() will be called after the initial parsing pass is complete. π₯
ParseRecord(%record)
Parses PDB records, updating the object attributes. This method is called automatically on every database record during Load(). π
ParseRecord0($data)
Parses the header record and places the parsed values into the hashref $self->{header}{palm}, the hashref $self->{header}{mobi}, and $self->{header}{exth} by calling parse_palmdoc_header(), parse_mobi_header(), and parse_mobi_exth() respectively. β¨
ParseRecordCDIC(\$data)
Parses a CDIC record. Takes as a sole argument a reference to the data of the record. π
Record format
- Offset 0: Record identifier β 4 bytes, always
'CDIC' - Offset 4: Header length β 4 bytes, big-endian long int, always =
16 - Offset 8: Index count β 4 bytes, big-endian long int, marks the number of big-endian short ints immediately following the header used as index points into the dictionary data
- Offset 12: Codelength β 4 bytes, big-endian long int, number of code bits
- Offset 16: Indexes β A number of big-endian short ints used as index points into the dictionary data
- Offset ??: Dictionary data β Dictionary result strings immediately following the indexes
ParseRecordHUFF(\$data)
Parses a HUFF record. Takes as a sole argument a reference to the data of the record. π¦
Record format
- Offset 0: Record identifier β 4 bytes, always
'HUFF' - Offset 4: Header length β 4 bytes, big-endian long int, always =
24 - Offset 8: Cache table (big-endian) offset β 4 bytes, big-endian long int, always =
24 - Offset 12: Base table (big-endian) offset β 4 bytes, big-endian long int, always =
1048 - Offset 16: Cache table (little-endian) offset β 4 bytes, big-endian long int, always =
1304 - Offset 20: Base table (little-endian) offset β 4 bytes, big-endian long int, always =
2328 - Offset 24: Cache table (big-endian) β
1024bytes,256big-endian long ints. This is a look up table for the length and decoding of short codewords. If the codeword represented by the 8 bits is unique, then bit 7 (0x80) will be set, and the low 5 bits are the length in bits of the code. The high three bytes partially represent the final symbol. If bit 7 is clear, then the code is looked up in the base table. - Offset 1048: Base table (big-endian) β
256bytes,64big-endian long ints. This is where the codeword is looked up if it isn't found in the cache table. - Offset 1304: Cache table (little-endian) β
1024bytes,256little-endian long ints. This contains exactly the same data as in the cache table at offset24, except that all of the values are stored in little-endian format instead of big-endian. Presumably this is for a speed advantage on slow little-endian processors. This module uses only the big-endian tables. - Offset 2328: Base table (little-endian) β
256bytes,64little-endian long ints. This contains exactly the same data as in the base table at offset1048, except that all of the values are stored in little-endian format instead of big-endian. Presumably this is for a speed advantage on slow little-endian processors. This module uses only the big-endian tables.
ParseRecordImage(\$dataref)
Parses image records, updating object attributes, most notably adding the image data to the hash $self->{imagedata}, adding the image filename to $self->{recindexlinks}, and incrementing $self->{recindex}. Takes as an argument a reference to the record data. Croaks if it isn't provided, or isn't a reference. This is called automatically by ParseRecord() and ParseResource() as needed. πΌοΈ
ParseRecordText(\$dataref)
Parses text records, updating object attributes, most notably appending text to $self->{text}. Takes as an argument a reference to the record data. This is called automatically by ParseRecord() and ParseResource() as needed. π
fix_html(%args)
Takes raw Mobipocket text and replaces the custom tags and file position anchors. π
Arguments
filenameβ The name of the output HTML file (used in generating hrefs). The procedure croaks if this is not supplied.nonewlines(optional) β If this is set to true, the procedure will not attempt to insert newlines for readability. This will leave the output in a single unreadable line, but has the advantage of reducing the processing time, especially useful if tidy is going to be run on the output anyway.
fix_html_filepos()
Takes the raw HTML text of the object and replaces the filepos anchors. This has to be called before any other action that modifies the text, or the filepos positions will not be valid. Returns 1 if successful, undef if there was no text to fix. This is called automatically by fix_html(). π
uncompress_dictionaryhuffman_records()
Uncompresses all text records using uncompress_dictionaryhuffman(). This destroys the existing contents of $self->{text} if any. This method is called automatically at the end of Load() if DictionaryHuffman encoding is detected. π
π PROCEDURES π
All procedures are exportable, but none are exported by default. All procedures can be exported by using the :all tag. β¨
find_mobidedrm()
Attempts to locate a copy of the MobiDeDrm script by searching PATH and looking in the EBook::Tools user configuration directory (see userconfigdir() in EBook::Tools. Returns the complete path to the script, or undef if nothing was found. This will use package variable $mobidedrm_cmd as its first guess, and set that variable to the return value as well. π΅οΈ
find_mobigen()
Attempts to locate the mobigen executable by making a test execution on predicted locations (including just checking PATH) and looking in the EBook::Tools user configuration directory (see userconfigdir() in EBook::Tools. Returns the system command used for a successful invocation, or undef if nothing worked. This will use package variable $mobigen_cmd as its first guess, and set that variable to the return value as well. π
parse_mobi_exth($headerdata)
Takes as an argument a scalar containing the variable-length Mobipocket EXTH data from the first record. Returns an array of hashes, each hash containing the data from one EXTH record with values from that data keyed to recognizable names. If $headerdata doesn't appear to be an EXTH header, carps a warning and returns an empty list. See:
http://wiki.mobileread.com/wiki/MOBI
Hash keys
typeβ A numeric value indicating the type of EXTH data in the record. See package variable%exthtypes.lengthβ The length of thedatavalue in bytes.dataβ The data of the record.
parse_mobi_header($headerdata)
Takes as an argument a scalar containing the variable-length Mobipocket-specific header data from the first record. Returns a hash containing values from that data keyed to recognizable names. See:
http://wiki.mobileread.com/wiki/MOBI
keys
The returned hash will have the following keys (documented in the order in which they are encountered in the header):
identifierβ This should always be the string'MOBI'. If it isn't, the procedure croaks.headerlengthβ This is the size of the complete header. If this value is different from the length of the argument, the procedure croaks.typeβ A numeric code indicating what category of Mobipocket file this is.encodingβ A numeric code representing the encoding. Expected values are'1252'(for Windows-1252) and'65001(for UTF-8). The procedure carps a warning if an unexpected value is encountered.uniqueidβ This is thought to be a unique ID for the book, but its actual use is unknown. Use with caution. This key may be renamed in the future if more information is found.versionβ This is thought to be the Mobipocket format version. A second version code shows up again later asversion2which is usually the same on unprotected books but different on DRMd books. Use with caution. This key may be renamed in the future if more information is found.reservedβ 40 bytes of reserved data. Use with caution. This key may be renamed in the future if more information is found."indxrecord"β thought to be the record offset to the very first'INDX'record β named that way bc those are literally its first four letters, iykyk π
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"titleoffset"β offset in record 0 (so not from the start of the file, bestie, pay attention π) of the full title of the book."titlelength"β length in bytes of the full title of the book."languageunknown"β 16 bits of unknown data thought to be related to the book language π¦
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"language"β a pseudo-IANA language code string for the main book language (i.e. the value of<dc:language>). See%mobilangcodesfor an exact map of raw values to this string, plus notes on non-compliant results."dilanguageunknown"β 16 bits of unknown data thought to be related to the dictionary input language.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"dilanguage"β a pseudo-IANA language code string for theDictionaryInLanguageelement. See%mobilangcodesfor an exact map of raw values to this string and notes on non-compliant results."dolanguageunknown"β 16 bits of unknown data thought to be related to the dictionary output language.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"dolanguage"β a pseudo-IANA language code string for theDictionaryOutLanguageelement. See%mobilangcodesfor an exact map of raw values to this string and notes on non-compliant results."version2"β another Mobipocket format version, this one about DRM. If there's no DRM present, it should be the same as"version".
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"firstimagerecord"β thought to be an index to the first record containing image data. If the book has no images, this value will be4294967295(0xffffffff) πΌοΈ
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"huffrecord"β thought to be the record offset to the'HUFF'record, the one used in HUFF/CDIC decompression.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"huffreccnt"β thought to be the number of HUFF and CDIC records, starting at"huffrecord".
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"datprecord"β thought to be the record offset to the first'DATP'record, so named for its first four letters (yes, same lil naming trick β¨).
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"datpreccnt"β thought to be the number of'DATP'records present.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"exthflags"β a 32-bit bitfield related to the Mobipocket EXTH data. If bit 6 (0x40) is set, then there's at least one EXTH record lurking in there."unknown116"β 36 bytes of unknown data at offset 116. This value will be undefined if the header data was not long enough to contain it π«
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"drmoffset"β a number thought to be the byte offset inside of the record 0 data where DRM data can be found. If present and no DRM is set, it contains either0xFFFFFFFF(normal books) or0x00000000(samples). This value will be undefined if the header data was not long enough to contain it π
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"drmcount"β a number thought to be related to DRM.
This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"drmsize"β a number thought to be the size of the data in bytes after"drmoffset"containing DRM keys.
This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"drmflags"β a number thought to be related to DRM.
This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown168"β 32 bits of unknown data at offset 168, usually zeroes. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown172"β 32 bits of unknown data at offset 172, usually zeroes. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown176"β 16 bits of unknown data at offset 176. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"lastimagerecord"β thought to be an index to the last record containing image data. If the book has no images, this value will be65535(0xffff) π
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown180"β 32 bits of unknown data at offset 180. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"fcisrecord"β thought to be an index to a'FCIS'record, so named because those are always the first four characters when the record data is decompressed usinguncompress_palmdoc().
This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown188"β 32 bits of unknown data at offset 188. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"flisrecord"β thought to be an index to a'FLIS'record, so named because those are always the first four characters when the record data is decompressed usinguncompress_palmdoc()(same energy asfcisrecord, fr fr π).
This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown196"β 32 bits of unknown data at offset 180. This value will be undefined if the header data was not long enough to contain it. (yes it says 180 again, and no, I'm not fixing it, that's what it says π)
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"unknown200"β unknown data of unknown length running all the way to the end of the header. This value will be undefined if the header data was not long enough to contain it.
β οΈ Use with caution. This key may be renamed in the future if more information is found.
"extradataflags"β two bytes sometimes found inside of"unknown200", used to figure out if extra data has been appended to each text record that should not be used in decompression.
π§ Functions, bestie π§
parse_mobi_language($languagecode, $regioncode)
Takes the integer values $languagecode and $regioncode unpacked from the Mobipocket header and returns a language string that's mostly (but not entirely, okay, don't come for me) conformant to the IANA language subtag registry codes π
It croaks if $languagecode isn't provided. If $regioncode isn't provided or isn't recognized, it gets disregarded and the base language string (no region, no script) is returned.
If $languagecode isn't provided, the sub croaks. If it isn't recognized, a warning is carped and the sub returns undef. And heads up: 0,0 is a recognized code returning an empty string π«Ά
See %mobilanguagecodes for an exact map of values. Also note that the bottom two bits of the region code appear to be unused β i.e. the values are all multiples of 4.
pid_append_checksum($pid)
Computes the Mobipocket PID checksum used as the final two bytes of the PID and appends them to $pid, returning the merged string.
It's used by "pid_is_valid($pid)".
pid_is_valid($pid)
Returns 1 if the PID is a valid Mobipocket/Kindle PID and 0 if it isn't.
How's that decided? First it makes sure $pid is exactly ten bytes long, then it strips the final two bytes normally used as a checksum, recomputes them, and returns 1 only if they get recomputed correctly π
pukall_cipher_1(%args)
This is a COMPLETELY UNTESTED implementation of the Pukall Cipher 1 algorithm, the one used for encryption and decryption in Mobipocket files. It's a 128-bit stream cipher. For more info and alternate implementations, see <http://membres.lycos.fr/pc1/>.
β οΈ Use at your own risk. Bug reports appreciated π
Arguments
"key"β 16-byte encryption key. This must be provided, and must be exactly 16 bytes, or the procedure will croak."input"β input data to be either encrypted or decrypted. If this isn't provided, the procedure croaks."encrypt"(optional) β if set to true, the cipher will be used to encrypt the input data. If it's not set, or set to false, the cipher will be used to decrypt the input data.
record_extradata_size(%args)
This checks the end of a text record for extra data that should not be made part of decompression and returns the total size of all data fields π
Arguments
"dataref"β a reference to the record data."extradataflags"β 16 bits worth of flags indicating which extra data fields are present.
system_mobidedrm(%args)
Runs python on a copy of "MobiDeDrm.py" if it's available (it's not included with this distribution) to downconvert a Mobipocket file π
Returns the output filename on success, or undef otherwise.
Arguments
"infile"β the input filename. If not specified or invalid, the procedure returnsundef."outfile"outfileβ the output filename, bestie. If you don't specify it, the program will use a name based on the input file, appending-nodrmto the basename and keeping the extension. In the special case of Mobipocket files ending in-sm, the-smportion of the basename is simply removed, and nothing else is appended. periodt.pidβ the PID to use to decrypt the file. If not specified or invalid, the procedure returnsundef.
β¨ system_mobigen(%args) β¨
Runs mobigen to convert OPF, HTML, or ePub input into a Mobipocket .prc/.mobi book. The procedure find_mobigen() is called to locate the executable. It's giving "where's the binary" energy.
Returns the return value from mobigen, or undef if no filename was specified or the file did not exist. Also returns undef if mobigen could not be found. No executable? No vibes.
Arguments
infileβ The input filename. If not specified or invalid, the procedure returnsundef. Simple as.outfileβ The output filename. Themobigenexecutable will choose its own filename for direct output, but if this argument is specified, the output file will be renamed to the specified filename instead. If not specified, the default output will be left in place. Your fave gets a glow-up rename.dirβ The directory in which to place the output file. Themobigenexecutable itself will always place its output into the current working directory, but if this argument is specified, the output file will be moved into the specified directory, creating that directory if necessary. It'll make the folder, no cap.compressionβ Compression level from0-2, where0is no compression,1is PalmDoc compression, and2is HUFF/CDIC compression. If not specified, defaults to1(PalmDoc compression). Default slay.
π¦ uncompress_dictionaryhuffman(%args) π¦
Uncompresses text compressed with the DictionaryHuffman compression scheme. Dictionary tea, unlocked.
Arguments
dataβ A scalar containing the compressed data to uncompress.huffβ A hashref pointing to the HUFF record datacdicsβ An arrayref pointing to the CDIC record datadepthβ The current depth of the huffman tree, currently only used in debugging. Debug brain.
πΈ unpack_mobi_language($data) πΈ
Takes as an argument 4 bytes of data. If less data is provided, the sub croaks. If more, a debug warning is provided, but the sub continues. So dramatic.
In scalar context returns a language string mostly (but not entirely) conformant to the IANA language subtag registry codes. Mostly iconic.
In list context, returns the language string, an unknown code integer, a region code integer, and a language code integer, with the last three being directly unpacked values.
See %mobilangcodes for an exact map of values. Note that the bottom two bits of the region code appear to be unused (i.e. the values are all multiples of 4). The unknown code integer appears to be unused, and is generally zero. Mystery bits, we don't know her.
The original implementation by Mobipocket may have been via Microsoft's .NET CultureInfo class. See: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(VS.71).aspx Nostalgia core.
π BUGS AND LIMITATIONS π
Okay bestie, here's the tea on what this thing can't do.
- Unpacking DRM-protected text isn't supported. Although infrastructure may be added later to make use of external helpers and plugins, direct DRM support will never be added to the main code for legal reasons. Legal said no.
- Repacking a
.prcwithout fully extracting to OPF and completely converting back isn't supported. This will have to be implemented before an interface to perform minor metadata alterations can be implemented. Future patch energy. - Mobipocket HUFF/CDIC decoding (used mostly on dictionaries) isn't well documented. Docs found dead.
- Not all Mobipocket data is understood, so a conversion from OPF to Mobipocket
.prcback to OPF will not result in all data being retained. Patches welcome. Patches welcome is the group chat invite. - Mobipocket INDX, DATP, FCIS, and FLIS records are not understood and are completely ignored Ignored, periodt.
- Mobipocket EXTH subjectcode records may not end up attached to the correct subject element if the number of subject records differs from the number of subjectcode records. This is because the Mobipocket format leaves the EXTH subjectcode records completely unlinked from the subject records, and there is no way to detect if a subject with no associated subjectcode comes before a subject with an associated subjectcode. It's giving unlinked.
Fortunately, this should rarely be a problem with real data, as Mobipocket Creator only allows a single subject to be set, and the only other way to have a subjectcode attached to a subject is to manually edit the OPF file and insert an additional dc:Subject element with a BASICCode attribute. Manual edit era.
Mobipocket has indicated that they may move data currently in their custom elements and attributes to the standard <meta> elements in a future release, so this problem may become moot then. Maybe moot soon.
AUTHOR π«Ά
Zed Pobre <zed@debian.org>
Icon, legend, the moment.
π LICENSE AND COPYRIGHT π
Copyright 2008 Zed Pobre
Licensed to the public under the terms of the GNU GPL, version 2
Respect the license, bestie.
And that's the whole vibe check π brought to you by perl v5.40.0 on 2025-01-10 π
π¦ where this page comes from
- debian trixie: libebook-tools-perl 0.5.4-2 Β· GPL-2.0-only
- ubuntu resolute: libebook-tools-perl 0.5.4-2 Β· GPL-2.0-only
- ubuntu stonking: libebook-tools-perl 0.5.4-2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms