EBook::Tools::Mobipocket(3pm) | User Contributed Perl Documentation | EBook::Tools::Mobipocket(3pm) |
EBook::Tools::Mobipocket - Palm::PDB handler for manipulating the Mobipocket format.
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');
Instantiates a new Ebook::Tools::Mobipocket object.
Returns 1 if the "drmoffset" header value is neither 0 nor 0xffffffff. Returns undef if "drmoffset" is undefined. Returns 0 otherwise.
Returns the text of the file
Writes each image record to the disk.
Returns the number of images written.
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.
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.
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.
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.
Parses PDB records, updating the object attributes. This method is called automatically on every database record during "Load()".
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.
Parses a CDIC record. Takes as a sole argument a reference to the data of the record.
Record format
4 bytes, always 'CDIC'
4 bytes, big-endian long int, always = 16
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
4 bytes, big-endian long int, number of code bits
A number of big-endian short ints used as index points into the dictionary data
Dictionary result strings immediately following the indexes
Parses a HUFF record. Takes as a sole argument a reference to the data of the record.
Record format
4 bytes, always 'HUFF'
4 bytes, big-endian long int, always = 24
4 bytes, big-endian long int, always = 24
4 bytes, big-endian long int, always = 1048
4 bytes, big-endian long int, always = 1304
4 bytes, big-endian long int, always = 2328
1024 bytes, 256 big-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
256 bytes, 64 big-endian long ints
This is where the codeword is looked up if it isn't found in the cache table.
1024 bytes, 256 little-endian long ints.
This contains exactly the same data as in the cache table at offset 24, 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.
256 bytes, 64 little-endian long ints
This contains exactly the same data as in the base table at offset 1048, 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.
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.
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.
Takes raw Mobipocket text and replaces the custom tags and file position anchors
Arguments
The name of the output HTML file (used in generating hrefs). The procedure croaks if this is not supplied.
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.
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()".
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.
All procedures are exportable, but none are exported by default. All procedures can be exported by using the ":all" tag.
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.
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.
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
A numeric value indicating the type of EXTH data in the record. See package variable %exthtypes.
The length of the "data" value in bytes
The data of the record.
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):
The procedure carps a warning if an unexpected value is encountered.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
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.
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.
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.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
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.
Use with caution. This key may be renamed in the future if more information is found.
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.
Use with caution. This key may be renamed in the future if more information is found.
Use with caution. This key may be renamed in the future if more information is found.
Takes the integer values $languagecode and $regioncode unpacked from the Mobipocket header and returns a language string mostly (but not entirely) conformant to the IANA language subtag registry codes.
Croaks if $languagecode is not provided. If $regioncode is not provided or not recognized, it is disregarded and the base language string (with no region or script) is returned.
If $languagecode is not provided, the sub croaks. If it isn't recognized, a warning is carped and the sub returns undef. Note that 0,0 is a recognized code returning an empty string.
See %mobilanguagecodes 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).
Computes the Mobipocket PID checksum used as the final two bytes of the PID and appends them to $pid, returning the merged string.
Used by "pid_is_valid($pid)".
Returns 1 if the PID is a valid Mobipocket/Kindle PID and 0 otherwise.
This is determined by first ensuring that $pid is exactly ten bytes long, and then stripping the final two bytes normally used as a checksum and recomputing them, returning 1 only if they are recomputed correctly.
This is a COMPLETELY UNTESTED implementation of the Pukall Cipher 1 algorithm used for encryption and decryption in Mobipocket files. It is a 128-bit stream cipher. For more information and alternate implementations, see <http://membres.lycos.fr/pc1/>.
Use at your own risk. Bug reports appreciated.
Arguments
16-byte encryption key. This must be provided, and must be exactly 16 bytes, or the procedure will croak.
Input data to be either encrypted or decrypted. If this is not provided, the procedure croaks.
If set to true, the cipher will be used to encrypt the input data. If not set, or set to false, the cipher will be used to decrypt the input data.
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
A reference to the record data
16 bits worth of flags indicating which extra data fields are present.
Runs python on a copy of "MobiDeDrm.py" if it is available (not included with this distribution) to downconvert a Mobipocket file.
Returns the output filename on success, or undef otherwise.
Arguments
The input filename. If not specified or invalid, the procedure returns undef.
The output filename. If not specified, the program will use a name based on the input file, appending '-nodrm' to the basename and keeping the extension. In the special case of Mobipocket files ending in '-sm', the '-sm' portion of the basename is simply removed, and nothing else is appended.
The PID to use to decrypt the file. If not specified or invalid, the procedure returns undef.
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.
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.
Arguments
The input filename. If not specified or invalid, the procedure returns undef.
The output filename. The mobigen executable 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.
The directory in which to place the output file. The mobigen executable 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.
Compression level from 0-2, where 0 is no compression, 1 is PalmDoc compression, and 2 is HUFF/CDIC compression. If not specified, defaults to 1 (PalmDoc compression).
Uncompresses text compressed with the DictionaryHuffman compression scheme.
Arguments
A scalar containing the compressed data to uncompress.
A hashref pointing to the HUFF record data
An arrayref pointing to the CDIC record data
The current depth of the huffman tree, currently only used in debugging.
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.
In scalar context returns a language string mostly (but not entirely) conformant to the IANA language subtag registry codes.
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.
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>
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.
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.
Zed Pobre <zed@debian.org>
Copyright 2008 Zed Pobre
Licensed to the public under the terms of the GNU GPL, version 2
2019-08-08 | perl v5.28.1 |