mobiperl-0.0.43/0000755000175000017500000000000011230442002012425 5ustar tompetompemobiperl-0.0.43/Palm/0000755000175000017500000000000011230442002013316 5ustar tompetompemobiperl-0.0.43/Palm/Doc.pm0000644000175000017500000002750411230442002014371 0ustar tompetompe# Palm::Doc.pm # # Palm::PDB helper for handling Palm Doc databases # # Copyright (C) 2004 Christophe Beauregard # # $Id: Doc.pm,v 1.19 2005/05/12 01:36:49 cpb Exp $ use strict; package Palm::Doc; use Palm::PDB; use Palm::Raw(); use vars qw( $VERSION @ISA ); $VERSION = do { my @r = (q$Revision: 1.19 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; @ISA = qw( Palm::Raw ); use constant DOC_UNCOMPRESSED => scalar 1; use constant DOC_COMPRESSED => scalar 2; use constant DOC_RECSIZE => scalar 4096; =head1 NAME Palm::Doc - Handler for Palm Doc books =head1 SYNOPSIS use Palm::Doc; =head1 DESCRIPTION Helper for reading and writing Palm Doc books. The interface is based on L since it just makes sense. However, because of the nature of these databases, record-level processing is just a Bad Idea. Use the C and C calls rather than do direct access of the C<@records> array. =head1 EXAMPLES Convert a text file to a .pdb: use Palm::Doc; my $doc = new Palm::Doc; $doc->textfile( $ARGV[0] ); $doc->Write( $ARGV[0] . ".pdb" ); Convert an HTML file to a .prc: use HTML::TreeBuilder; use HTML::FormatText; use Palm::Doc; my $tree = HTML::TreeBuilder->new_from_file( $ARGV[0] ); my $formatter = HTML::FormatText->new( leftmargin => 0, rightmargin => 80 ); my $doc = new Palm::Doc; $doc->{attributes}{resource} = 1; $doc->text( $formatter->format( $tree ) ); $doc->Write( $ARGV[0] . ".prc" ); =cut #' sub import { &Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ "REAd", "TEXt" ], ); &Palm::PDB::RegisterPRCHandlers( __PACKAGE__, [ "REAd", "TEXt" ], ); &Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ "MOBI", "BOOK" ], ); &Palm::PDB::RegisterPRCHandlers( __PACKAGE__, [ "MOBI", "BOOK" ], ); } =head2 new $doc = new Palm::Doc; Create a new Doc object. By default, it's not a resource database. Setting C<$self->{attributes}{resource}> to C<1> before any manipulations will cause it to become a resource database. =cut sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->{'creator'} = 'REAd'; $self->{'type'} = 'TEXt'; $self->{attributes}{resource} = 0; $self->{appinfo} = undef; $self->{sort} = undef; $self->{records} = []; return $self; } # determine if the given (raw) record is a Doc header record and fill in the # record with appropriate fields if it is. sub _parse_headerrec($) { my $record = shift; return undef unless exists $record->{'data'}; # Doc header is minimum of 16 bytes return undef if length $record->{'data'} < 16; my ($version,$spare,$ulen, $records, $recsize, $position) = unpack( 'n n N n n N', $record->{'data'} ); my $h = sprintf ("%x", $version); print STDERR "Version: $version - $h - "; if ($version == DOC_COMPRESSED) { print STDERR " DOC_COMPRESSED\n"; } if ($version == DOC_UNCOMPRESSED) { print STDERR " DOC_UNCOMPRESSED\n"; } if ($version != DOC_UNCOMPRESSED and $version != DOC_COMPRESSED) { print STDERR " probably HUFFDIC_COMPRESSED - CANNOT BE DECOMPRESSED!!!\n"; } # the header is followed by a list of record sizes. We don't use # this since we can guess the sizes pretty easily by looking at # the actual records. # According to the spec, $version is either 1 (uncompressed) # or 2 (compress), while spare is always zero. AportisDoc supposedly sets # spare to something else, so screw AportisDoc. return undef if $version != DOC_UNCOMPRESSED and $version != DOC_COMPRESSED; return undef if $spare != 0; $record->{'version'} = $version; $record->{'length'} = $ulen; $record->{'records'} = $records; $record->{'recsize'} = $recsize; $record->{'position'} = $position; return $record; } sub _compress_record($$) { my ($version,$in) = @_; return $in if $version == DOC_UNCOMPRESSED; my $out = ''; my $lin = length $in; my $i = 0; while( $i < $lin ) { # See http://patb.dyndns.org/Programming/PilotDoc.htm for the code type # taxonomy. # Try type B compression first. # If the next 3 to 10 bytes are already in the compressed buffer, we can # encode them into a 2 byte sequence. Don't bother too close to the ends, # however... Makes the boundary conditions simpler. if( $i > 10 and $lin - $i > 10 ) { my $chunk = ''; my $match = -1; # the preamble is what'll be in the decoders output buffer. my $preamble = substr( $in, 0, $i ); for( my $j = 10; $j >= 3; $j -- ) { $chunk = substr( $in, $i, $j ); # grab next $j characters $match = rindex( $preamble, $chunk ); # in the output? # type B code has a 2047 byte sliding window, so matches have to be # within that range to be useful last if $match >= 0 and ($i - $match) <= 2047; $match = -1; } my $n = length $chunk; if( $match >= 0 and $n <= 10 and $n >= 3 ) { my $m = $i - $match; # first 2 bits are 10, next 11 are offset, next 3 are length-3 $out .= pack( "n", 0x8000 + (($m<<3)&0x3ff8) + ($n-3) ); $i += $n; next; } } my $ch = substr( $in, $i ++, 1 ); my $och = ord($ch); # Try type C compression. if( $i+1 < $lin and $ch eq ' ' ) { my $nch = substr( $in, $i, 1 ); my $onch = ord($nch); if( $onch >= 0x40 and $onch < 0x80 ) { # space plus ASCII character compression $out .= chr($onch ^ 0x80); $i ++; next; } } if( $och == 0 or ($och >= 9 and $och < 0x80) ) { # pass through $out .= $ch; } else { # type A code. This is essentially an 'escape' like '\\' in strings. # For efficiency, it's best to encode as long a sequence as # possible with one copy. This might seem like it would cause us to miss # out on a type B sequence, but in actuality keeping long binary strings # together improves the likelyhood of a later type B sequence than # interspersing them with x01's. my $next = substr($in,$i - 1); if( $next =~ /([\x01-\x08\x80-\xff]{1,8})/o ) { my $binseq = $1; $out .= chr(length $binseq); $out .= $binseq; $i += length( $binseq ) - 1; # first char, $ch, is already counted } } } return $out; } # algorithm taken from makedoc7.cpp with reference to # http://patb.dyndns.org/Programming/PilotDoc.htm and # http://www.pyrite.org/doc_format.html sub _decompress_record($$) { my ($version,$in) = @_; return $in if $version == DOC_UNCOMPRESSED; my $out = ''; my $lin = length $in; my $i = 0; while( $i < $lin ) { my $ch = substr( $in, $i ++, 1 ); my $och = ord($ch); if( $och >= 1 and $och <= 8 ) { # copy this many bytes... basically a way to 'escape' data $out .= substr( $in, $i, $och ); $i += $och; } elsif( $och < 0x80 ) { # pass through 0, 9-0x7f $out .= $ch; } elsif( $och >= 0xc0 ) { # 0xc0-0xff are 'space' plus ASCII char $out .= ' '; $out .= chr($och ^ 0x80); } else { # 0x80-0xbf is sequence from already decompressed buffer my $nch = substr( $in, $i ++, 1 ); $och = ($och << 8) + ord($nch); my $m = ($och & 0x3fff) >> 3; my $n = ($och & 0x7) + 3; # This isn't very perl-like, but a simple # substr($out,$lo-$m,$n) doesn't work. my $lo = length $out; for( my $j = 0; $j < $n; $j ++, $lo ++ ) { die "bad Doc compression" unless ($lo-$m) >= 0; $out .= substr( $out, $lo-$m, 1 ); } } } return $out; } sub Write { my $self = shift; my $prc = $self->{attributes}{resource}; my $recs = $prc ? $self->{'resources'} : $self->{'records'}; my $header = $recs->[0]; unless( defined _parse_headerrec($header) ) { die "@_: Doesn't appear to be a correct book..."; } $self->SUPER::Write(@_); } =head2 text $text = $doc->text; Return the contents of the Doc database. $text = $doc->text( @text ); Set the contents of the Doc book to the specified arguments. All the list arguments will simply be concatenated together. =cut sub text { my $self = shift; my $body = ''; my $prc = $self->{attributes}{resource}; if( @_ > 0 ) { $body = join( '', @_ ); my $version = DOC_COMPRESSED; $self->{'records'} = []; $self->{'resources'} = []; # first record is the header my $header = $prc ? $self->append_Resource() : $self->append_Record(); $header->{'version'} = $version; $header->{'length'} = 0; $header->{'records'} = 0; $header->{'recsize'} = DOC_RECSIZE; # break the document into record-sized chunks for( my $i = 0; $i < length($body); $i += DOC_RECSIZE ) { my $record = $prc ? $self->append_Resource : $self->append_Record; my $chunk = substr($body,$i,DOC_RECSIZE); $record->{'data'} = _compress_record( $version, $chunk ); $header->{'records'} ++; $header->{'length'} += length $body; } $header->{'recsize'} = $header->{'length'} if $header->{'length'} < DOC_RECSIZE; # pack up the header $header->{'data'} = pack( 'n xx N n n N', $header->{'version'}, $header->{'length'}, $header->{'records'}, $header->{'recsize'}, 0 ); } elsif( defined wantarray ) { my $recs = $prc ? $self->{'resources'} : $self->{'records'}; my $header = $recs->[0]; if( defined _parse_headerrec($header) ) { # a proper Doc file should be fine, but if it's not Doc # compression like some Mobi docs seem to be we want to # bail early. Otherwise we end up with a huge stream of # substr() errors and we _still_ don't get any content. eval { sub min { return ($_[0]<$_[1]) ? $_[0] : $_[1] } my $maxi = min($#$recs, $header->{'records'}); for( my $i = 1; $i <= $maxi; $i ++ ) { my $data = $recs->[$i]->{'data'}; my $len = length($data); my $overlap = ""; if ($self->{multibyteoverlap}) { my $c = chop $data; print STDERR "I:$i - $len - ", int($c), "\n"; my $n = $c & 7; foreach (0..$n-1) { $overlap = (chop $data) . $overlap; } } $body .= _decompress_record( $header->{'version'}, $data ); $body .= $overlap; } }; return undef if $@; } } return $body; } =head2 textfile $doc->textfile( "README.txt" ); Set the contents of the Doc to the contents of the file and sets the name of the PDB to the specified filename. =cut sub textfile($$) { my ($self, $filename) = @_; open IN, "< $filename" or return undef; binmode IN; $self->text( '', ); close IN; $self->{'name'} = $filename; } 1; __END__ =head1 BUGS Bookmarks are unsupported. I've never had any use for them. Output databases are always compressed and there's no option to disable compression. I consider this a feature, to be honest. =head2 Note On Character Sets L doesn't do anything with character sets. This might be a bug, depending on how you feel about this kind of thing, but the reality is that we're generally converting between text and Doc files, neither of which are real great at telling us what encoding we're supposed to use. My understanding of PalmOS character sets is that Doc books should be encoded in either Windows Code Page 1252 or, for Japanese, 932. Actually, the PalmOS encoding is a small variation on those. In practice, ISO 8859-1 works okay for western languages which is real nice because L doesn't know about the PalmOS stuff. This gist of all this is that when you're creating a L, you may need to do something along the lines of: use Encode 'from_to'; my $text = read_my_text(); from_to( $text, $charset, 'iso-8859-1' ) unless $charset =~ /8859-1$/; my $doc = new Palm::Doc(); $doc->text( $text ); And when you're reading a L and you care about the character set, you're pretty much going to have to guess the encoding and act appropriately: use Encode 'decode'; my $doc = new Palm::PDB; $doc->Load( $pdbname ); my $text = decode("iso-8859-1", $doc->text()); =head1 AUTHOR Christophe Beauregard Ecpb@cpan.orgE =head1 SEE ALSO L L makedoc L L L is another CPAN module for handling Doc databases, but doesn't use L and doesn't handle reading Docs. L for details on PalmOS text encoding =cut mobiperl-0.0.43/MobiPerl/0000755000175000017500000000000011230442002014136 5ustar tompetompemobiperl-0.0.43/MobiPerl/Config.pm0000644000175000017500000000616611230442002015712 0ustar tompetompepackage MobiPerl::Config; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/COnfig.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; use strict; sub new { my $this = shift; my $data = shift; my $class = ref($this) || $this; my $obj = bless { ADDCOVERLINK => 0, TOCFIRST => 0, COVERIMAGE => "", THUMBIMAGE => "", AUTHOR => "", TITLE => "", PREFIXTITLE => "", NOIMAGES => 0, FIXHTMLBR => 0, REMOVEJAVASCRIPT => 0, SCALEALLIMAGES => 1.0, KEEPBR => 0, IMAGEMAXBYTES => 61440, @_ }, $class; $obj->initialize_from_file ($data) if defined $data; return $obj; } sub add_cover_link { my $self = shift; my $val = shift; if (defined $val) { $self->{ADDCOVERLINK} = $val; } else { return $self->{ADDCOVERLINK}; } } sub toc_first { my $self = shift; my $val = shift; if (defined $val) { $self->{TOCFIRST} = $val; } else { return $self->{TOCFIRST}; } } sub cover_image { my $self = shift; my $val = shift; if (defined $val) { $self->{COVERIMAGE} = $val; } else { return $self->{COVERIMAGE}; } } sub thumb_image { my $self = shift; my $val = shift; if (defined $val) { $self->{THUMBIMAGE} = $val; } else { return $self->{THUMBIMAGE}; } } sub author { my $self = shift; my $val = shift; if (defined $val) { $self->{AUTHOR} = $val; } else { return $self->{AUTHOR}; } } sub title { my $self = shift; my $val = shift; if (defined $val) { $self->{TITLE} = $val; } else { return $self->{TITLE}; } } sub prefix_title { my $self = shift; my $val = shift; if (defined $val) { $self->{PREFIXTITLE} = $val; } else { return $self->{PREFIXTITLE}; } } sub no_images { my $self = shift; my $val = shift; if (defined $val) { $self->{NOIMAGES} = $val; } else { return $self->{NOIMAGES}; } } sub remove_java_script { my $self = shift; my $val = shift; if (defined $val) { $self->{REMOVEJAVASCRIPT} = $val; } else { return $self->{REMOVEJAVASCRIPT}; } } sub scale_all_images { my $self = shift; my $val = shift; if (defined $val) { $self->{SCALEALLIMAGES} = $val; } else { return $self->{SCALEALLIMAGES}; } } sub get_image_max_bytes { my $self = shift; return $self->{IMAGEMAXBYTES}; } sub set_image_max_bytes { my $self = shift; my $val = shift; $self->{IMAGEMAXBYTES} = $val; } return 1; mobiperl-0.0.43/MobiPerl/EXTH.pm0000644000175000017500000002273211230442002015252 0ustar tompetompepackage MobiPerl::EXTH; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/EXTH.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; use strict; # 400-499 application binary # 500-599 application string my %typename_to_type = ("drm_server_id" => 1, "drm_commerce_id" => 2, "drm_ebookbase_book_id" => 3, "author" => 100, "publisher" => 101, "imprint" => 102, "description" => 103, "isbn" => 104, "subject" => 105, "publishingdate" => 106, "review" => 107, "contributor" => 108, "rights" => 109, "subjectcode" => 110, "type" => 111, "source" => 112, "asin" => 113, "versionnumber" => 114, "sample" => 115, "startreading" => 116, "coveroffset" => 201, "thumboffset" => 202, "hasfakecover" => 203, "204" => 204, "205" => 205, "206" => 206, "207" => 207, "clippinglimit" => 401, # varies in size 1 or 4 seend "publisherlimit" => 402, "403" => 403, "ttsflag" => 404, "cdetype" => 501, "lastupdatetime" => 502, "updatedtitle" => 503, ); my %type_to_desc = (1 => "drm_server_id", 2 => "drm_commerce_id", 3 => "drm_ebookbase_book_id", 100 => "Author", 101 => "Publisher", 102 => "Imprint", 103 => "Description", 104 => "ISBN", 105 => "Subject", 106 => "PublishingDate", 107 => "Review", 108 => "Contributor", 109 => "Rights", 110 => "SubjectCode", 111 => "Type", 112 => "Source", 113 => "ASIN", 114 => "VersionNumber", 115 => "Sample", 116 => "StartReading", 201 => "CoverOffset", 202 => "ThumbOffset", 203 => "hasFakeCover", 401 => "ClippingLimit", 402 => "PublisherLimit", 404 => "TTSFlag", 501 => "CDEContentType", 502 => "LastUpdateTime", 503 => "UpdatedTitle", 504 => "cDEContentKey", ); my %binary_data = (114 => 1, 115 => 1, 201 => 1, 202 => 1, 203 => 1, 204 => 1, 205 => 1, 206 => 1, 207 => 1, 300 => 1, 401 => 1, 403 => 1, 404 => 1, ); my %format = (114 => 4, 201 => 4, 202 => 4, 203 => 4, 204 => 4, 205 => 4, 206 => 4, 207 => 4, 403 => 1); sub new { my $this = shift; my $data = shift; my $class = ref($this) || $this; my $obj = bless { TYPE => [], DATA => [], @_ }, $class; $obj->initialize_from_data ($data) if defined $data; return $obj; } sub get_string { my $self = shift; my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; my $res = ""; foreach my $i (0..$#type) { my $type = $type[$i]; my $data = $data[$i]; my $typedesc = $type; if (defined $type_to_desc{$type}) { $typedesc = $type_to_desc{$type}; if (defined $binary_data{$type}) { $res .= $typedesc . " - " . "not printable" . "\n"; } else { $res .= $typedesc . " - " . $data . "\n"; } } } return $res; } sub add { my $self = shift; my $typename = shift; my $data = shift; my $type = $self->get_type ($typename); if (is_binary_data ($type)) { my $hex = MobiPerl::Util::iso2hex ($data); print STDERR "EXTH add: $typename - $type - ", int($data), " - $hex\n"; } else { print STDERR "EXTH add: $typename - $type - $data\n"; } if ($type) { push @{$self->{TYPE}}, $type; push @{$self->{DATA}}, $data; } else { print STDERR "WARNING: $typename is not defined as an EXTH type\n"; } return $type; } sub delete { my $self = shift; my $typename = shift; my $delexthindex = shift; my $type = $self->get_type ($typename); print STDERR "EXTH delete: $typename - $type - $delexthindex\n"; if ($type) { my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; @{$self->{TYPE}} = (); @{$self->{DATA}} = (); my $index = 0; foreach my $i (0..$#type) { ## print STDERR "TYPE: $type[$i]\n"; if ($type[$i] == $type) { $index++; ## print STDERR "INDEX: $index\n"; } if ($type[$i] == $type and ($delexthindex == 0 or $delexthindex == $index)) { if (is_binary_data ($type[$i])) { my $hex = MobiPerl::Util::iso2hex ($data[$i]); print STDERR "DELETING $type[$i]: ", int($data[$i]), " - $hex\n"; } else { print STDERR "DELETING $type[$i]: $data[$i]\n"; } } else { push @{$self->{TYPE}}, $type[$i]; push @{$self->{DATA}}, $data[$i]; } } } else { print STDERR "WARNING: $typename is not defined as an EXTH type\n"; } } sub get_type { my $self = shift; my $typename = shift; my $res = 0; ### print STDERR "EXTH: GETTYPE: $typename\n"; if (defined $typename_to_type{$typename}) { $res = $typename_to_type{$typename}; } else { if ($typename =~ /^\d+$/) { $res = $typename; } } return $res; } sub set { my $self = shift; my $typename = shift; my $data = shift; my $type = $self->get_type ($typename); my $hex = MobiPerl::Util::iso2hex ($data); print STDERR "EXTH setting data: $typename - $type - $data - $hex\n"; if ($type) { my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; my $found = 0; foreach my $i (0..$#type) { if ($type[$i] == $type) { print STDERR "EXTH replacing data: $type - $data - $hex\n"; $self->{TYPE}->[$i] = $type; $self->{DATA}->[$i] = $data; $found = 1; last; } } if (not $found) { $self->add ($typename, $data); } } return $type; } sub initialize_from_data { my $self = shift; my $data = shift; my ($doctype, $len, $n_items) = unpack ("a4NN", $data); ## print "EXTH doctype: $doctype\n"; ## print "EXTH length: $len\n"; ## print "EXTH n_items: $n_items\n"; my $pos = 12; foreach (1..$n_items) { my ($type, $size) = unpack ("NN", substr ($data, $pos)); $pos += 8; my $contlen = $size-8; my ($content) = unpack ("a$contlen", substr ($data, $pos)); if (defined $format{$type}) { my $len = $format{$type}; ## print STDERR "TYPE:$type:$len\n"; if ($len == 4) { ($content) = unpack ("N", substr ($data, $pos)); ## print STDERR "CONT:$content\n"; } if ($len == 1) { ($content) = unpack ("C", substr ($data, $pos)); ## print STDERR "CONT:$content\n"; } } push @{$self->{TYPE}}, $type; push @{$self->{DATA}}, $content; $pos += $contlen; } if ($self->get_data () ne substr ($data, 0, $len)) { print STDERR "ERROR: generated EXTH does not match original\n"; my $s1 = $self->get_data (); my $s0 = substr ($data, 0, $len); foreach my $i (0..length ($s0)-1) { if (substr ($s0, $i, 1) ne substr ($s1, $i, 1)) { my $c0 = substr ($s0, $i, 1); my $c1 = substr ($s1, $i, 1); $c0 = MobiPerl::Util::iso2hex ($c0); $c1 = MobiPerl::Util::iso2hex ($c1); print STDERR "MISMATCH POS:$i:$c0:$c1\n"; } } } # open EXTH0, ">exth0"; # print EXTH0 substr ($data, 0, $len); # open EXTH1, ">exth1"; # print EXTH1 $self->get_data (); } sub get_data { my $self = shift; my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; my $exth = pack ("a*", "EXTH"); my $content = ""; my $n_items = 0; foreach my $i (0..$#type) { my $type = $type[$i]; my $data = $data[$i]; next unless defined $data; # remove type... if (defined $format{$type}) { my $len = $format{$type}; if ($len == 4) { $content .= pack ("NNN", $type, $len+8, $data); } if ($len == 1) { $content .= pack ("NNC", $type, $len+8, $data); } } else { $content .= pack ("NNa*", $type, length ($data)+8, $data); } $n_items++; } # # Maybe fill up to even 4... # my $comp = length ($content) % 4; if ($comp) { foreach ($comp .. 3) { $content .= pack ("C", 0); } } $exth .= pack ("NN", length ($content)+12, $n_items); $exth .= $content; return $exth; } sub get_cover_offset { my $self = shift; my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; # pdurrant: 0 is a valid cover offset, so return -1 if not found my $res = -1; # my $res = 0; foreach my $i (0..$#type) { if ($type[$i] == 201) { ## print STDERR "TYPE: $type[$i] - $data[$i]\n"; ## ($res) = unpack ("N", $data[$i]); $res = $data[$i]; ## print STDERR "RES: $res\n"; } } return $res; } sub get_thumb_offset { my $self = shift; my @type = @{$self->{TYPE}}; my @data = @{$self->{DATA}}; # pdurrant: 0 is a valid cover offset, so return -1 if not found my $res = -1; # my $res = 0; foreach my $i (0..$#type) { if ($type[$i] == 202) { $res = $data[$i]; } } return $res; } # # Non object methods # sub get_description { my $type = shift; my $res = $type; if (defined $type_to_desc{$type}) { $res = $type_to_desc{$type}; } return $res; } sub is_binary_data { my $type = shift; return $binary_data{$type}; } return 1; mobiperl-0.0.43/MobiPerl/LinksInfo.pm0000644000175000017500000000660311230442002016375 0ustar tompetompepackage MobiPerl::LinksInfo; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/LinksInfo.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; use strict; ###use MobiPerl::Util; ##use Data::Dumper; sub new { my $this = shift; my $class = ref($this) || $this; my $obj = bless { LINKEXISTS => {}, RECORDINDEX => 0, RECORDTOIMAGEFILE => {}, COVEROFFSET => -1, THUMBOFFSET => -1, @_ }, $class; return $obj; } sub link_exists { my $self = shift; return $self->{LINKEXISTS}; } sub add_image_link { my $self = shift; my $image = shift; ## print STDERR "ADD_IMAGE_LINK: $image\n"; $self->{RECORDINDEX}++; $self->{RECORDTOIMAGEFILE}->{$self->get_record_index ()} = $image; } sub add_cover_image { my $self = shift; my $image = shift; $self->add_image_link ($image); $self->{COVEROFFSET} = $self->get_record_index () - 1; } sub add_thumb_image { my $self = shift; my $image = shift; $self->add_image_link ($image); $self->{THUMBOFFSET} = $self->get_record_index () - 1; } sub get_cover_offset { my $self = shift; return $self->{COVEROFFSET}; } sub get_thumb_offset { my $self = shift; return $self->{THUMBOFFSET}; } sub get_record_index { my $self = shift; return $self->{RECORDINDEX}; } sub get_image_file { my $self = shift; my $val = shift; return $self->{RECORDTOIMAGEFILE}->{$val}; } sub get_n_images { my $self = shift; my $res = keys %{$self->{RECORDTOIMAGEFILE}}; return $res; } sub check_for_links { my $self = shift; my $html = shift; for (@{$html->extract_links('a', 'img')}) { my($link, $element, $attr, $tag) = @$_; next if ($link =~ /http/); next if ($link =~ /mailto/); next if ($link =~ /www/); # print STDERR "LINK: $tag $link $attr at ", $element->address(), " "; if ($tag eq "a") { my $filename = $element->as_trimmed_text (); ## print STDERR "LINKEXISTS $filename -> $link - "; # # Remove possible prefix file name in link # $link =~ s/^.*\#//; ## print STDERR "$link\n"; $element->attr("href", "\#$link"); $self->{LINKEXISTS}->{$link} = 1; next; } if ($tag eq "img") { my $src = $element->attr("src"); if (-e "$src") { # # Onlys save link if image exists. # $element->attr("src", undef); $self->{RECORDINDEX}++; $element->attr("recindex", sprintf ("%05d", $self->{RECORDINDEX})); $self->{RECORDTOIMAGEFILE}->{$self->{RECORDINDEX}} = $src; } else { print STDERR "Warning: Image file do not exists: $src\n"; } next; } print STDERR "LINK: $tag $link $attr at ", $element->address(), " "; # print STDERR $element->as_HTML; } } return 1; mobiperl-0.0.43/MobiPerl/MobiFile.pm0000644000175000017500000001670011230442002016166 0ustar tompetompepackage MobiPerl::MobiFile; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/MobiFile.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use Palm::PDB; use Palm::Doc; use MobiPerl::MobiHeader; use MobiPerl::Util; use constant DOC_UNCOMPRESSED => scalar 1; use constant DOC_COMPRESSED => scalar 2; use constant DOC_RECSIZE => scalar 4096; use strict; sub save_mobi_file { my $html = shift; my $filename = shift; my $linksinfo = shift; my $config = shift; my $rescale = shift; my $imrescale = $MobiPerl::Util::rescale_large_images; $imrescale = $rescale if defined $rescale; my $author = $config->author (); my $title = $config->title (); print STDERR "Saving mobi file (version 4): $filename\n"; my $mobi = new Palm::Doc; $mobi->{attributes}{"resource"} = 0; $mobi->{attributes}{"ResDB"} = 0; $mobi->{"name"} = $title; $mobi->{"type"} = "BOOK"; $mobi->{"creator"} = "MOBI"; $mobi->{"version"} = 0; $mobi->{"uniqueIDseed"} = 28; # $mobi->{"attributes"}{"resource"} = $data; # my $header = Palm::PDB->new_Record(); # $header->{"categori"} = 0; # $header->{"attributes"}{"Dirty"} = 1; # $header->{"id"} = 0; # $header->{"data"} = $data; # $mobi->append_Record ($header); ## $mobi->text ([$data, $html->as_HTML ()]); ## $mobi->text ($html->as_HTML ()); # # From Doc.pm and modified # my $version = DOC_COMPRESSED; $mobi->{'records'} = []; $mobi->{'resources'} = []; my $header = $mobi->append_Record(); $header->{'version'} = $version; $header->{'length'} = 0; $header->{'records'} = 0; $header->{'recsize'} = DOC_RECSIZE; my $body = $html->as_HTML (); $body =~ s/&\;nbsp\;/ \;/g; #fix   that fix_pre_tags have added # print STDERR "HTMLSIZE: " . length ($body) . "\n"; my $current_record_index = 1; # break the document into record-sized chunks for( my $i = 0; $i < length($body); $i += DOC_RECSIZE ) { my $record = $mobi->append_Record; my $chunk = substr($body,$i,DOC_RECSIZE); $record->{'data'} = Palm::Doc::_compress_record( $version, $chunk ); $record->{'id'} = $current_record_index++; $header->{'records'} ++; } $header->{'length'} += length $body; $header->{'recsize'} = $header->{'length'} if $header->{'length'} < DOC_RECSIZE; # # pack the Palm Doc header # $header->{'data'} = pack( 'n xx N n n N', $header->{'version'}, $header->{'length'}, $header->{'records'}, $header->{'recsize'}, 0 ); # # Add MOBI header # my $mh = new MobiPerl::MobiHeader; $mh->set_title ($title); $mh->set_author ($author); $mh->set_image_record_index ($current_record_index); ## $mh->set_codepage (65001); # $mh->set_cover_offset (0); # It crashes on Kindle if no cover is # is available and offset is set to 0 my $cover_offset = $linksinfo->get_cover_offset (); print STDERR "COVEROFFSET: $cover_offset\n"; $mh->set_cover_offset ($cover_offset); # Set to -1 if no cover image # if ($cover_offset >= 0) { # $mh->set_cover_offset ($cover_offset); # } my $thumb_offset = $linksinfo->get_thumb_offset (); print STDERR "THUMBOFFSET: $thumb_offset\n"; if ($thumb_offset >= 0) { $mh->set_thumb_offset ($thumb_offset); } ## my $codepage = 65001; # utf-8 # my $codepage = 1252; # westerner # my $ver = 3; # my $type = 2; # book # my $mobiheadersize = 0x74; # my $unique_id = 17; # if ($ver == 4) { # $mobiheadersize = 0xE4; # } # # my $extended_title_offset = $mobiheadersize + 16; # my $extended_title_length = length ($title); # # my $use_extended_header = 1; # my $extended_header_flag = 0x00; # if ($use_extended_header) { # $extended_header_flag = 0x50; # At MOBI+0x70 # } # # my $exth = ""; # if ($use_extended_header) { # $exth = pack ("a*", "EXTH"); # my $content = ""; # my $n_items = 1; # $content .= pack ("NNa*", 100, length ($author)+8, $author); # $exth .= pack ("NN", length ($content), $n_items); # $exth .= $content; # $extended_title_offset += length ($exth); # } # # # # NNNN Number of char, N1 N2 N3 # # N3 = Pointer to start of Title # # Not true in Alice Case... # # # # my $vie1 = 0; # 0x11 Alice 0x0D Rosenbaum # # print STDERR "MOBIHDR: imgrecpointer: $current_record_index\n"; # # $header->{'data'} .= pack ("a*NNNNN", "MOBI", # $mobiheadersize, $type, # $codepage, $unique_id, $ver); # # $header->{'data'} .= pack ("NN", 0xFFFFFFFF, 0xFFFFFFFF); # $header->{'data'} .= pack ("NNNN", 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); # $header->{'data'} .= pack ("NNNN", 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); # $header->{'data'} .= pack ("NNNN", $vie1, $extended_title_offset, $extended_title_length, 0x0409); # $header->{'data'} .= pack ("NNNN", 0, 0, 0x04, $current_record_index); # $header->{'data'} .= pack ("NNNN", 0, 0, 0, 0); # $header->{'data'} .= pack ("N", $extended_header_flag); ## print STDERR "MOBIHEADERSIZE: $mobiheadersize " . length ($header->{'data'}). "\n"; # while (length ($header->{'data'}) < ($mobiheadersize+16)) { # print STDERR "LEN: " . length ($header->{'data'}). " - $mobiheadersize #\n"; # $header->{'data'} .= pack ("N", 0); # } # $header->{'data'} .= $exth; # $header->{'data'} .= pack ("a*", $title); # for (1..48) { # $header->{'data'} .= pack ("N", 0); # } # $header->{'data'} .= $mh->get_data (); # # End from Doc.pm # if (not $config->no_images ()) { for my $i (1..$linksinfo->get_record_index ()) { my $filename = $linksinfo->get_image_file ($i); ## print STDERR "New record for image $current_record_index: $filename\n"; # # Is it really correct to assign id and categori? # my $img = Palm::PDB->new_Record(); $img->{"categori"} = 0; $img->{"attributes"}{"Dirty"} = 1; $img->{"id"} = $current_record_index++; my $data = MobiPerl::Util::get_image_data ($filename, $imrescale, $config); $img->{"data"} = $data; $mobi->append_Record ($img); } my $coverimage = $config->cover_image (); # # This will not work since EXTH information not set # # # Adding thumb for Cybook does not seem to be neccessary. # To automatically add the first image seems wrong... # So functionality disabled for now. # if ($coverimage and 0) { print STDERR "New record for library image $current_record_index: $coverimage\n"; my $img = Palm::PDB->new_Record(); $img->{"categori"} = 0; $img->{"attributes"}{"Dirty"} = 1; $img->{"id"} = $current_record_index++; my $data = MobiPerl::Util::get_thumb_cover_image_data ($coverimage); $img->{"data"} = $data; $mobi->append_Record ($img); } } $mobi->Write ($filename); } return 1; mobiperl-0.0.43/MobiPerl/MobiHeader.pm0000644000175000017500000005035011230442002016476 0ustar tompetompeuse strict; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/MobiHeader.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # This is a patch of a function in Palm::Doc to be able to handle # DRM:ed files. # package Palm::Doc; sub _parse_headerrec($) { my $record = shift; return undef unless exists $record->{'data'}; # Doc header is minimum of 16 bytes return undef if length $record->{'data'} < 16; my ($version,$spare,$ulen, $records, $recsize, $position) = unpack( 'n n N n n N', $record->{'data'} ); # the header is followed by a list of record sizes. We don't use # this since we can guess the sizes pretty easily by looking at # the actual records. # According to the spec, $version is either 1 (uncompressed) # or 2 (compress), while spare is always zero. AportisDoc supposedly sets # spare to something else, so screw AportisDoc. # # $version is 17480 for DRM:ed MobiPocket books # # So comment away the check ### return undef if $version != DOC_UNCOMPRESSED and $version != DOC_COMPRESSED; return undef if $spare != 0; $record->{'version'} = $version; $record->{'length'} = $ulen; $record->{'records'} = $records; $record->{'recsize'} = $recsize; $record->{'position'} = $position; return $record; } package MobiPerl::MobiHeader; use FindBin qw($RealBin); use lib "$RealBin"; use MobiPerl::EXTH; use strict; # # TYPE: 2=book # # VERSION: Should be 3 or 4 # # CODEPAGE: utf-8: 65001; westerner: 1252 # # IMAGERECORDINDEX: the index of the first record with image in it # # Language seems to be stored in 4E: en-us 0409 # sv 041d # fi 000b # en 0009 # # 0x50 and 0x54 might also be some kind of language specification # # # 0000: MOBI header-size type codepage # 0010: unique-id version FFFFFFFF FFFFFFFF # # header-size = E4 if version = 4 # type = 2 - book # codepage = 1252 - westerner # unique-id = seems to be random # version = 3 or 4 # # 0040: data4 exttitleoffset exttitlelength language # 0050: data1 data2 data3 nonbookrecordpointer # 0060: data5 # # data1 and data2 id 09 in Oxford dictionary. The same as languange... # nonbookrecordpointer in Oxford is 0x7167. data5 is 0x7157 # data3 is 05 in Oxford so maybe this is the version? # #pdurrant: # # 0040: nonbookrecordpointer exttitleoffset exttitlelength language # 0050: data1 data2 data3 firstimagerecordpointer # 0060: data5 # my %langmap = ( "es" => 0x000a, "sv" => 0x001d, "sv-se" => 0x041d, "sv-fi" => 0x081d, "fi" => 0x000b, "en" => 0x0009, "en-au" => 0x0C09, "en-bz" => 0x2809, "en-ca" => 0x1009, "en-cb" => 0x2409, "en-ie" => 0x1809, "en-jm" => 0x2009, "en-nz" => 0x1409, "en-ph" => 0x3409, "en-za" => 0x1c09, "en-tt" => 0x2c09, "en-us" => 0x0409, "en-gb" => 0x0809, "en-zw" => 0x3009, "da" => 0x0006, "da-dk" => 0x0406, "da" => 0x0006, "da" => 0x0006, "nl" => 0x0013, "nl-be" => 0x0813, "nl-nl" => 0x0413, "fi" => 0x000b, "fi-fi" => 0x040b, "fr" => 0x000c, "fr-fr" => 0x040c, "de" => 0x0007, "de-at" => 0x0c07, "de-de" => 0x0407, "de-lu" => 0x1007, "de-ch" => 0x0807, "no" => 0x0014, "nb-no" => 0x0414, "nn-no" => 0x0814, ); my %mainlanguage = ( 0 => "NEUTRAL", 54 => "AFRIKAANS", 28 => "ALBANIAN", 1 => "ARABIC", 43 => "ARMENIAN", 77 => "ASSAMESE", 44 => "AZERI", 45 => "BASQUE", 35 => "BELARUSIAN", 69 => "BENGALI", 2 => "BULGARIAN", 3 => "CATALAN", 4 => "CHINESE", 26 => "CROATIAN", 5 => "CZECH", 6 => "DANISH", 19 => "DUTCH", 9 => "ENGLISH", 37 => "ESTONIAN", 56 => "FAEROESE", 41 => "FARSI", 11 => "FINNISH", 12 => "FRENCH", 55 => "GEORGIAN", 7 => "GERMAN", 8 => "GREEK", 71 => "GUJARATI", 13 => "HEBREW", 57 => "HINDI", 14 => "HUNGARIAN", 15 => "ICELANDIC", 33 => "INDONESIAN", 16 => "ITALIAN", 17 => "JAPANESE", 75 => "KANNADA", 63 => "KAZAK", 87 => "KONKANI", 18 => "KOREAN", 38 => "LATVIAN", 39 => "LITHUANIAN", 47 => "MACEDONIAN", 62 => "MALAY", 76 => "MALAYALAM", 58 => "MALTESE", 78 => "MARATHI", 97 => "NEPALI", 20 => "NORWEGIAN", 72 => "ORIYA", 21 => "POLISH", 22 => "PORTUGUESE", 70 => "PUNJABI", 23 => "RHAETOROMANIC", 24 => "ROMANIAN", 25 => "RUSSIAN", 59 => "SAMI", 79 => "SANSKRIT", 26 => "SERBIAN", 27 => "SLOVAK", 36 => "SLOVENIAN", 46 => "SORBIAN", 10 => "SPANISH", 48 => "SUTU", 65 => "SWAHILI", 29 => "SWEDISH", 73 => "TAMIL", 68 => "TATAR", 74 => "TELUGU", 30 => "THAI", 49 => "TSONGA", 50 => "TSWANA", 31 => "TURKISH", 34 => "UKRAINIAN", 32 => "URDU", 67 => "UZBEK", 42 => "VIETNAMESE", 52 => "XHOSA", 53 => "ZULU", ); my $langmap = {}; $langmap->{"ENGLISH"} = { 1 => "ENGLISH_US", 2 => "ENGLISH_UK", 3 => "ENGLISH_AUS", 4 => "ENGLISH_CAN", 5 => "ENGLISH_NZ", 6 => "ENGLISH_EIRE", 7 => "ENGLISH_SOUTH_AFRICA", 8 => "ENGLISH_JAMAICA", 10 => "ENGLISH_BELIZE", 11 => "ENGLISH_TRINIDAD", 12 => "ENGLISH_ZIMBABWE", 13 => "ENGLISH_PHILIPPINES", }; my %sublanguage = ( 0 => "NEUTRAL", 1 => "ARABIC_SAUDI_ARABIA", 2 => "ARABIC_IRAQ", 3 => "ARABIC_EGYPT", 4 => "ARABIC_LIBYA", 5 => "ARABIC_ALGERIA", 6 => "ARABIC_MOROCCO", 7 => "ARABIC_TUNISIA", 8 => "ARABIC_OMAN", 9 => "ARABIC_YEMEN", 10 => "ARABIC_SYRIA", 11 => "ARABIC_JORDAN", 12 => "ARABIC_LEBANON", 13 => "ARABIC_KUWAIT", 14 => "ARABIC_UAE", 15 => "ARABIC_BAHRAIN", 16 => "ARABIC_QATAR", 1 => "AZERI_LATIN", 2 => "AZERI_CYRILLIC", 1 => "CHINESE_TRADITIONAL", 2 => "CHINESE_SIMPLIFIED", 3 => "CHINESE_HONGKONG", 4 => "CHINESE_SINGAPORE", 1 => "DUTCH", 2 => "DUTCH_BELGIAN", 1 => "FRENCH", 2 => "FRENCH_BELGIAN", 3 => "FRENCH_CANADIAN", 4 => "FRENCH_SWISS", 5 => "FRENCH_LUXEMBOURG", 6 => "FRENCH_MONACO", 1 => "GERMAN", 2 => "GERMAN_SWISS", 3 => "GERMAN_AUSTRIAN", 4 => "GERMAN_LUXEMBOURG", 5 => "GERMAN_LIECHTENSTEIN", 1 => "ITALIAN", 2 => "ITALIAN_SWISS", 1 => "KOREAN", 1 => "LITHUANIAN", 1 => "MALAY_MALAYSIA", 2 => "MALAY_BRUNEI_DARUSSALAM", 1 => "NORWEGIAN_BOKMAL", 2 => "NORWEGIAN_NYNORSK", 2 => "PORTUGUESE", 1 => "PORTUGUESE_BRAZILIAN", 2 => "SERBIAN_LATIN", 3 => "SERBIAN_CYRILLIC", 1 => "SPANISH", 2 => "SPANISH_MEXICAN", 4 => "SPANISH_GUATEMALA", 5 => "SPANISH_COSTA_RICA", 6 => "SPANISH_PANAMA", 7 => "SPANISH_DOMINICAN_REPUBLIC", 8 => "SPANISH_VENEZUELA", 9 => "SPANISH_COLOMBIA", 10 => "SPANISH_PERU", 11 => "SPANISH_ARGENTINA", 12 => "SPANISH_ECUADOR", 13 => "SPANISH_CHILE", 14 => "SPANISH_URUGUAY", 15 => "SPANISH_PARAGUAY", 16 => "SPANISH_BOLIVIA", 17 => "SPANISH_EL_SALVADOR", 18 => "SPANISH_HONDURAS", 19 => "SPANISH_NICARAGUA", 20 => "SPANISH_PUERTO_RICO", 1 => "SWEDISH", 2 => "SWEDISH_FINLAND", 1 => "UZBEK_LATIN", 2 => "UZBEK_CYRILLIC", ); my %booktypedesc = (2 => "BOOK", 3 => "PALMDOC", 4 => "AUDIO", 257 => "NEWS", 258 => "NEWS_FEED", 259 => "NEWS_MAGAZINE", 513 => "PICS", 514 => "WORD", 515 => "XLS", 516 => "PPT", 517 => "TEXT", 518 => "HTML", ); sub new { my $this = shift; my $class = ref($this) || $this; bless { TYPE => 2, VERSION => 4, CODEPAGE => 1252, TITLE => "Unspecified Title", AUTHOR => "Unspecified Author", PUBLISHER => "", DESCRIPTION => "", SUBJECT => "", IMAGERECORDINDEX => 0, LANGUAGE => "en", COVEROFFSET => -1, THUMBOFFSET => -1, @_ }, $class; } sub set_author { my $self = shift; my $val = shift; $self->{AUTHOR} = $val; } sub get_author { my $self = shift; return $self->{AUTHOR}; } sub set_cover_offset { my $self = shift; my $val = shift; $self->{COVEROFFSET} = $val; } sub get_cover_offset { my $self = shift; return $self->{COVEROFFSET}; } sub set_thumb_offset { my $self = shift; my $val = shift; $self->{THUMBOFFSET} = $val; } sub get_thumb_offset { my $self = shift; return $self->{THUMBOFFSET}; } sub set_publisher { my $self = shift; my $val = shift; $self->{PUBLISHER} = $val; } sub get_publisher { my $self = shift; return $self->{PUBLISHER}; } sub set_description { my $self = shift; my $val = shift; $self->{DESCRIPTION} = $val; } sub get_description { my $self = shift; return $self->{DESCRIPTION}; } sub set_subject { my $self = shift; my $val = shift; $self->{SUBJECT} = $val; } sub get_subject { my $self = shift; return $self->{SUBJECT}; } sub set_language { my $self = shift; my $val = shift; $self->{LANGUAGE} = $val; } sub get_language { my $self = shift; return $self->{LANGUAGE}; } sub set_title { my $self = shift; my $val = shift; $self->{TITLE} = $val; } sub get_title { my $self = shift; return $self->{TITLE}; } sub set_image_record_index { my $self = shift; my $val = shift; $self->{IMAGERECORDINDEX} = $val; } sub get_image_record_index { my $self = shift; return $self->{IMAGERECORDINDEX}; } sub get_type { my $self = shift; return $self->{TYPE}; } sub get_codepage { my $self = shift; return $self->{CODEPAGE}; } sub set_codepage { my $self = shift; my $value = shift; $self->{CODEPAGE} = $value; } sub set_version { my $self = shift; my $val = shift; $self->{VERSION} = $val; } sub get_version { my $self = shift; return $self->{VERSION}; } sub get_unique_id { my $self = shift; my $r1 = int (rand (256)); my $r2 = int (rand (256)); my $r3 = int (rand (256)); my $r4 = int (rand (256)); my $res = $r1+$r2*256+$r3*256*256+$r4*256*256*256; return $res; } sub get_header_size { my $self = shift; my $res = 0x74; if ($self->get_version () == 4) { $res = 0xE4; } return $res; } sub get_extended_header_data { my $self = shift; my $author = $self->get_author (); my $eh = new MobiPerl::EXTH; $eh->set ("author", $author); my $pub = $self->get_publisher (); $eh->set ("publisher", $pub) if $pub; my $desc = $self->get_description (); $eh->set ("description", $desc) if $desc; my $subj = $self->get_subject (); $eh->set ("subject", $subj) if $subj; my $coffset = $self->get_cover_offset (); if ($coffset >= 0) { ## my $data = pack ("N", $coffset); ## print STDERR "COFFSET:$coffset:$data:\n"; $eh->set ("coveroffset", $coffset); } my $toffset = $self->get_thumb_offset (); if ($toffset >= 0) { ## my $data = pack ("N", $toffset); ## my $hex = MobiPerl::Util::iso2hex ($data); ## print STDERR "TOFFSET:$toffset:$hex\n"; $eh->set ("thumboffset", $toffset); } ## $eh->set ("hasfakecover", pack ("N", 0)); return $eh->get_data (); } sub get_data { my $self = shift; my $res = ""; my $vie1 = 0; # 0x11 Alice 0x0D Rosenbaum 0xFFFFFFFF, Around the world $vie1 = 0xFFFFFFFF; my $vie2 = 0x04; # had this, around the world have 0x01 my $use_extended_header = 1; my $extended_header_flag = 0x00; if ($use_extended_header) { $extended_header_flag = 0x50; # At MOBI+0x70 } my $extended_title_offset = $self->get_header_size () + 16 + length ($self->get_extended_header_data ()); my $extended_title_length = length ($self->get_title ()); print STDERR "MOBIHDR: imgrecpointer: ", $self->get_image_record_index (), "\n"; $res .= pack ("a*NNNNN", "MOBI", $self->get_header_size (), $self->get_type (), $self->get_codepage (), $self->get_unique_id (), $self->get_version ()); $res .= pack ("NN", 0xFFFFFFFF, 0xFFFFFFFF); $res .= pack ("NNNN", 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); $res .= pack ("NNNN", 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); my $langnumber = $self->get_language (); if (defined $langmap{$langnumber}) { $langnumber = $langmap{$langnumber}; } $res .= pack ("NNNN", $vie1, $extended_title_offset, $extended_title_length, $langnumber); $res .= pack ("NNNN", 0xFFFFFFFF, 0xFFFFFFFF, $vie2, $self->get_image_record_index ()); $res .= pack ("NNNN", 0xFFFFFFFF, 0, 0xFFFFFFFF, 0); $res .= pack ("N", $extended_header_flag); # print STDERR "MOBIHEADERSIZE: $mobiheadersize " . length ($header->{'data'}). "\n"; while (length ($res) < $self->get_header_size ()) { ### print STDERR "LEN: " . length ($res) . " - " . $self->get_header_size () . "\n"; $res .= pack ("N", 0); } substr ($res, 0x94, 4, pack ("N", 0xFFFFFFFF)); substr ($res, 0x98, 4, pack ("N", 0xFFFFFFFF)); substr ($res, 0xb0, 4, pack ("N", 0xFFFFFFFF)); # maybe pointer to last image or to thumbnail image record substr ($res, 0xb8, 4, pack ("N", 0xFFFFFFFF)); # record pointer substr ($res, 0xc0, 4, pack ("N", 0xFFFFFFFF)); # record pointer substr ($res, 0xc8, 4, pack ("N", 0xFFFFFFFF)); # record pointer # # unknown # substr ($res, 0xd0, 4, pack ("N", 0xFFFFFFFF)); substr ($res, 0xd8, 4, pack ("N", 0xFFFFFFFF)); substr ($res, 0xdc, 4, pack ("N", 0xFFFFFFFF)); $res .= $self->get_extended_header_data (); $res .= pack ("a*", $self->get_title ()); # # Why? # for (1..48) { $res .= pack ("N", 0); } return $res; } # # Help function that is not dependent on object state # sub get_extended_title { my $h = shift; my $len = length ($h); my ($exttitleoffset) = unpack ("N", substr ($h, 0x44)); my ($exttitlelength) = unpack ("N", substr ($h, 0x48)); my ($title) = unpack ("a$exttitlelength", substr ($h, $exttitleoffset-16)); return $title; } sub set_extended_title { my $mh = shift; my $len = length ($mh); my $title = shift; my $titlelen = length ($title); my ($exttitleoffset) = unpack ("N", substr ($mh, 0x44)); my ($exttitlelength) = unpack ("N", substr ($mh, 0x48)); my ($version) = unpack ("N", substr ($mh, 0x14)); my $res = substr ($mh, 0, $exttitleoffset-16); my $aftertitle = substr ($mh, $exttitleoffset-16+$exttitlelength); $res .= $title; my $diff = $titlelen - $exttitlelength; if ($diff <= 0) { foreach ($diff .. -1) { $res .= pack ("C", 0); $diff++; } } else { my $comp = $diff % 4; if ($comp) { foreach ($comp .. 3) { $res .= pack ("C", 0); $diff++; } } } $res = fix_pointers ($res, $exttitleoffset, $diff); $res .= $aftertitle; substr ($res, 0x48, 4, pack ("N", $titlelen)); return $res; } sub get_mh_language_code { my $h = shift; my $len = length ($h); my ($lang) = unpack ("N", substr ($h, 0x4C)); return $lang; } sub get_language_desc { my $code = shift; my $lid = $code & 0xFF; my $lang = $mainlanguage{$lid}; my $sublid = ($code >> 10) & 0xFF; my $sublang = $langmap->{$lang}->{$sublid}; my $res = ""; $res .= "$lang"; $res .= " - $sublang"; return $res; } sub set_booktype { my $mh = shift; my $len = length ($mh); my $type = shift; substr ($mh, 0x08, 4, pack ("N", $type)); return $mh; } sub set_language_in_header { my $mh = shift; my $len = length ($mh); my $lan = shift; my $langnumber = $lan; if (defined $langmap{$langnumber}) { $langnumber = $langmap{$langnumber}; } substr ($mh, 0x4C, 4, pack ("N", $langnumber)); return $mh; } sub add_exth_data { my $h = shift; my $type = shift; my $data = shift; return set_exth_data ($h, $type, $data, 1); } sub set_exth_data { my $h = shift; my $len = length ($h); my $type = shift; my $data = shift; my $addflag = shift; my $delexthindex = shift; my $res = $h; if (defined $data) { print STDERR "Setting extended header data: $type - $data\n"; } else { print STDERR "Deleting extended header data of type: $type - $delexthindex\n"; } my ($doctype, $length, $htype, $codepage, $uniqueid, $ver) = unpack ("a4NNNNN", $h); my ($exthflg) = unpack ("N", substr ($h, 0x70)); my $exth = substr ($h, $length); my $prefix = substr ($h, 0, $length); my $suffix; my $mobidiff = 0; my $eh; my $exthlen = 0; if ($exthflg & 0x40) { my ($doctype, $exthlen1, $n_items) = unpack ("a4NN", $exth); $exthlen = $exthlen1; $suffix = substr ($exth, $exthlen); $eh = new MobiPerl::EXTH ($exth); } else { $eh = new MobiPerl::EXTH (); $suffix = $exth; substr ($prefix, 0x70, 4, pack ("N", $exthflg | 0x40)); # pdurrant: as well as setting the exthflg, we need make sure the version >= 4 if ($ver < 4) { substr($prefix, 0x14, 4, pack("N",4)); } # pdurrant: and if the mobi header is short, we need to increase its size if ($length < 0xE8) { if ($length < 0x9C) { #get rid of any old bad data inappropriate for new header $prefix = substr($prefix, 0, 0x74); } $prefix .= substr(pack("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF), length($prefix)-0xE8); $mobidiff = 0xE8-$length; substr ($prefix, 4, 4, pack ("N", 0xE8)); } } if ($addflag) { $eh->add ($type, $data); } else { if (defined $data) { $eh->set ($type, $data); } else { $eh->delete ($type, $delexthindex); } } print STDERR "GETSTRING: ", $eh->get_string (); # # Fix DRM and TITLE info pointers... # my $exthdata = $eh->get_data (); my $exthdiff = length ($exthdata)-$exthlen; if ($exthdiff <= 0) { foreach ($exthdiff .. -1) { $exthdata .= pack ("C", 0); $exthdiff++; } } $res = $prefix . $exthdata . $suffix; $res = fix_pointers ($res, $length, $mobidiff+$exthdiff); return $res; } sub fix_pointers { my $mh = shift; my $startblock = shift; my $offset = shift; # # Fix pointers to long title and to DRM record # my ($exttitleoffset) = unpack ("N", substr ($mh, 0x44)); if ($exttitleoffset > $startblock and $offset > 0) { substr ($mh, 0x44, 4, pack ("N", $exttitleoffset+$offset)); } # pdurrant my ($ehlen) = unpack ("N", substr ($mh,0x04)); if ($ehlen > 0x98 ) { #pdurrant my ($drmoffset) = unpack ("N", substr ($mh, 0x98)); if ($drmoffset != 0xFFFFFFFF and $drmoffset > $startblock and $offset > 0) { substr ($mh, 0x98, 4, pack ("N", $drmoffset+$offset)); } } return $mh; } sub get_booktype_desc { my $type = shift; my $res = $type; if (defined $booktypedesc{$type}) { $res = $booktypedesc{$type}; } return $res; } return 1; mobiperl-0.0.43/MobiPerl/Opf.pm0000644000175000017500000002522111230442002015222 0ustar tompetompepackage MobiPerl::Opf; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/Opf.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use Encode; use FindBin qw($RealBin); use lib "$RealBin"; use strict; use XML::Parser::Lite::Tree; #use File::Spec; use URI::Escape; use MobiPerl::Util; ##use Data::Dumper; sub new { my $this = shift; my $data = shift; my $class = ref($this) || $this; my $obj = bless { OPF => 0, TITLE => "", AUTHOR => "Unspecified Author", IDTOHREF => {}, IDTOMEDIATYPE => {}, MANIFESTIDS => [], COVERIMAGE => "", SPINEIDS => [], TOCHREF => "", FILENAME => $data, @_ }, $class; $obj->initialize_from_file ($data) if defined $data; return $obj; } sub get_href { my $self = shift; my $val = shift; return $self->{IDTOHREF}->{$val}; } sub get_media_type { my $self = shift; my $val = shift; return $self->{IDTOMEDIATYPE}->{$val}; } sub get_spine_ids { my $self = shift; return @{$self->{SPINEIDS}}; } sub get_manifest_ids { my $self = shift; return @{$self->{MANIFESTIDS}}; } sub set_opf { my $self = shift; my $val = shift; $self->{OPF} = $val; } sub get_opf { my $self = shift; return $self->{OPF}; } sub set_title { my $self = shift; my $val = shift; $self->{TITLE} = $val; } sub get_title { my $self = shift; return $self->{TITLE}; } sub set_author { my $self = shift; my $val = shift; $self->{AUTHOR} = $val; } sub get_author { my $self = shift; return $self->{AUTHOR}; } sub set_toc_href { my $self = shift; my $val = shift; $self->{TOCHREF} = $val; } sub get_toc_href { my $self = shift; return $self->{TOCHREF}; } sub set_cover_image { my $self = shift; my $val = shift; $self->{COVERIMAGE} = $val; } sub get_cover_image { my $self = shift; return $self->{COVERIMAGE}; } sub initialize_from_file { my $self = shift; my $filename = shift; print STDERR "Opf: Initialize from file: $filename\n"; open OPF, "<$filename" or die "Could not open opf file: $filename\n"; local $/; my $content = ; $content =~ s/^\xef\xbb\xbf//; $content =~ s/>\n \n<\/guide/>\n<\/guide/g; $content =~ s/>\n<\/metadata/>\n<\/metadata/g; $content =~ s/>\n<\/dc-metadata/>\n<\/dc-metadata/g; $content =~ s/>\n<\/x-metadata/>\n<\/x-metadata/g; $content =~ s/>\n<\/manifest/>\n<\/manifest/g; $content =~ s/>\n<\/package/>\n<\/package/g; $content =~ s/>\n<\/dc:Title/>\n<\/dc:Title/g; $content =~ s/>\n<\/dc:Language/>\n<\/dc:Language/g; $content =~ s/>\n<\/dc:Identifier/>\n<\/dc:Identifier/g; $content =~ s/>\n<\/dc:Creator/>\n<\/dc:Creator/g; $content =~ s/>\n<\/dc:Subject/>\n<\/dc:Subject/g; $content =~ s/>\n<\/output/>\n<\/output/g; $content =~ s/>\n<\/item/>\n<\/item/g; $content =~ s/>\n<\/EmbeddedCover/>\n<\/EmbeddedCover/g; $content =~ s/>\n<\/spine/>\n<\/spine/g; $content =~ s/>\n<\/tours/>\n<\/tours/g; print STDERR "CONTENT: $content\n"; my $tree_parser = XML::Parser::Lite::Tree::instance(); my $opf = $tree_parser->parse(encode_utf8 $content); $self->set_opf ($opf); ## print STDERR Dumper($opf); my $title = opf_get_title ($opf); # global variable $title, bad... print STDERR "OPF: TITLE: $title\n"; $self->set_title ($title); my $creator = opf_get_tag ($opf, "dc:Creator"); # global variable $title, bad... print STDERR "OPF: CREATOR: $creator\n"; $self->set_author ($creator); $self->parse_manifest ($opf); $self->parse_spine ($opf); $self->parse_guide ($opf); } sub parse_manifest { my $self = shift; my $opf = shift; # my ($vol,$dir,$basefile) = File::Spec->splitpath ($self->{FILENAME}); # print STDERR "OPFFILE: $vol - $dir - $basefile\n"; my $type = $opf->{"type"}; # print STDERR "TYPE: $type - "; if ($type eq "tag" or $type eq "element") { my $name = $opf->{"name"}; # print STDERR "$name\n"; if ($name eq "manifest") { print STDERR "Init from manifest\n"; my $children = $opf->{"children"}; foreach my $c (@{$children}) { if ($c->{name} eq "item") { my $id = $c->{"attributes"}->{"id"}; my $href= $c->{"attributes"}->{"href"}; my $mediatype = $c->{"attributes"}->{"media-type"}; print STDERR "$id - $href - $mediatype\n"; $self->{IDTOHREF}->{$id} = $href; $self->{IDTOMEDIATYPE}->{$id} = $mediatype; push @{$self->{MANIFESTIDS}}, $id; # $opf_id_to_href{$id} = $href; # $opf_id_to_mediatype{$id} = $mediatype; # push @opf_manifest_ids, $id; # # Check if image is coverimage file # if ($mediatype =~ /image/) { print STDERR "CHECK IF IMAGE: $href\n"; if (MobiPerl::Util::is_cover_image ($href)) { $self->set_cover_image ($href); ## $coverimage = $href; } } } } return; } } if ($type eq "data") { return ""; } if ($type eq "tag" or $type eq "root" or $type eq "element") { my $children = $opf->{"children"}; foreach my $c (@{$children}) { $self->parse_manifest ($c); } } } sub parse_spine { my $self = shift; my $opf = shift; my $type = $opf->{"type"}; # print STDERR "TYPE: $type - "; if ($type eq "tag" or $type eq "element") { my $name = $opf->{"name"}; # print STDERR "$name\n"; if ($name eq "spine") { # print STDERR "Init from spine\n"; my $children = $opf->{"children"}; my %idcheck = (); foreach my $c (@{$children}) { if ($c->{name} eq "itemref") { my $idref = $c->{"attributes"}->{"idref"}; if ($idcheck{$idref}) { print STDERR "WARNING: Spine, duplice idref: $idref\n"; } else { push @{$self->{SPINEIDS}}, $idref; ## push @opf_spine_ids, $idref; $idcheck{$idref} = 1; } } } foreach my $id (@{$self->{MANIFESTIDS}}) { ## print STDERR "CHECK FOR ADDING to spine from manifest - $id\n"; if (not $idcheck{$id}) { print STDERR "Warning, $id missing from spine, adding\n"; push @{$self->{SPINEIDS}}, $id; ## push @opf_spine_ids, $id; } } return; } } if ($type eq "data") { return ""; } if ($type eq "tag" or $type eq "root" or $type eq "element") { my $children = $opf->{"children"}; foreach my $c (@{$children}) { $self->parse_spine ($c); } } } sub parse_guide { my $self = shift; my $opf = shift; my $type = $opf->{"type"}; # print STDERR "TYPE: $type - "; if ($type eq "tag" or $type eq "element") { my $name = $opf->{"name"}; # print STDERR "$name\n"; if ($name eq "guide") { print STDERR "Init from guide\n"; my $children = $opf->{"children"}; foreach my $c (@{$children}) { if ($c->{name} eq "reference") { my $type = $c->{"attributes"}->{"type"}; # print STDERR "TYPE: $type\n"; if ($type eq "toc") { my $escref = $c->{"attributes"}->{"href"}; ## Remove file part... Should this really be done?? $escref = uri_unescape($escref); $escref =~ s/^.*?\#/\#/; $self->set_toc_href ($escref); print STDERR "TOCHREF: ", $self->get_toc_href (), "\n"; } if ($type eq "other.ms-coverimage") { my $href = $c->{"attributes"}->{"href"}; print STDERR "COVERIMAGEHREF: $href\n"; $self->set_cover_image ($href); } if ($type eq "other.ms-coverimage-standard") { my $href = $c->{"attributes"}->{"href"}; print STDERR "COVERIMAGEHREF: $href\n"; $self->set_cover_image ($href); } } } return; } } if ($type eq "data") { return ""; } if ($type eq "tag" or $type eq "root" or $type eq "element") { my $children = $opf->{"children"}; foreach my $c (@{$children}) { $self->parse_guide ($c); } } } # # Non object methods # sub opf_get_title { my $opf = shift; # print STDERR "SELF:$self\n"; my $type = $opf->{"type"}; # print STDERR "TYPE: $type - "; if ($type eq "tag" or $type eq "element") { my $name = $opf->{"name"}; print STDERR "$name\n"; if ($name eq "dc:Title") { my $children = $opf->{"children"}; return @{$children}[0]->{"content"}; } } if ($type eq "data") { return ""; my $content = $opf->{"content"}; chomp $content; chomp $content; print STDERR "$content\n"; } if ($type eq "tag" or $type eq "root" or $type eq "element") { my $children = $opf->{"children"}; foreach my $c (@{$children}) { my $res = opf_get_title ($c); if ($res) { return $res; } } } return ""; } sub opf_get_tag { my $opf = shift; my $tag = shift; ## print STDERR "opf_get_tag: $tag\n"; # print STDERR "SELF:$self\n"; my $type = $opf->{"type"}; # print STDERR "TYPE: $type - "; if ($type eq "tag" or $type eq "element") { my $name = $opf->{"name"}; ## print STDERR "$name - $tag\n"; if ($name eq $tag) { my $children = $opf->{"children"}; return @{$children}[0]->{"content"}; } } if ($type eq "data") { return ""; my $content = $opf->{"content"}; chomp $content; chomp $content; print STDERR "$content\n"; } if ($type eq "tag" or $type eq "root" or $type eq "element") { my $children = $opf->{"children"}; foreach my $c (@{$children}) { my $res = opf_get_tag ($c, $tag); if ($res) { return $res; } } } return ""; } return 1; mobiperl-0.0.43/MobiPerl/Util.pm0000644000175000017500000004536511230442002015426 0ustar tompetompepackage MobiPerl::Util; # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # MobiPerl/Util.pm, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use strict; use GD; use Image::BMP; use Image::Size; use File::Copy; use File::Spec; use HTML::TreeBuilder; my $rescale_large_images = 0; sub is_cover_image { my $file = shift; my $res = 0; if (not -e "$file") { die "ERROR: File does not exist: $file"; } my $p = new GD::Image ($file); if (not defined $p) { print STDERR "Could not read image file: $file\n"; return $res; } my ($x, $y) = $p->getBounds(); # my $x = $p->width; # my $y = $p->height; if ($x == 510 and $y == 680) { print STDERR "GUESSING COVERIMAGE: $file\n"; $res = 1; } if ($x == 600 and $y == 800) { print STDERR "GUESSING COVERIMAGE: $file\n"; $res = 1; } return $res; } # # OPF related functions # sub get_tree_from_opf { my $file = shift; my $config = shift; my $linksinfo = shift; # my ($vol,$dir,$basefile) = File::Spec->splitpath ($file); # print STDERR "OPFFILE: $vol - $dir - $basefile\n"; my $opf = new MobiPerl::Opf ($file); my $tochref = $opf->get_toc_href (); my @opf_spine_ids = $opf->get_spine_ids (); my @opf_manifest_ids = $opf->get_manifest_ids (); my $title = $opf->get_title (); print STDERR "OPFTITLE: $title\n"; if ($config->title ()) { $title = $config->title (); } $title = $config->prefix_title () . $title; $config->title ($title); my $author = $opf->get_author (); print STDERR "OPFAUTHOR: $author\n"; if (not $config->author ()) { $config->author ($author); } # # If cover image not assigned search all files in current dir # and see if some file is a coverimage # my $coverimage = $opf->get_cover_image (); if ($coverimage eq "") { opendir DIR, "."; my @files = readdir (DIR); foreach my $f (@files) { if ($f =~ /\.jpg/ or $f =~ /\.JPG/ or $f =~ /\.gif/) { # print STDERR "Checking if file is coverimage: $f\n"; if (MobiPerl::Util::is_cover_image ($f)) { $coverimage = $f; } } } } print STDERR "Coverimage: $coverimage\n"; my $html = HTML::Element->new('html'); my $head = HTML::Element->new('head'); # # Generate guide tag, specific for Mobipocket and is # not understood by HTML::TreeBuilder... # my $guide = HTML::Element->new('guide'); if ($tochref) { print STDERR "Util.pm: GENERATE GUIDE SECTION: $tochref\n"; my $tocref = HTML::Element->new('reference', title=>"Table of Contents", type=>"toc", href=>"\#$tochref"); $guide->push_content ($tocref); } if ($config->add_cover_link ()) { print STDERR "Util.pm: GENERATE GUIDE SECTION ADDCOVVERLINK\n"; my $coverref = HTML::Element->new('reference', title=>"Cover", type=>"cover", href=>"\#addedcoverlink"); $guide->push_content ($coverref); } $head->push_content ($guide); my $titleel = HTML::Element->new('title'); $titleel->push_content ($title); $head->push_content ($titleel); # # Generate body # my $body = HTML::Element->new('body'); # topmargin => "0", # leftmargin => "0", # bottommargin => "0", # rightmargin => "0"); my $coverp = HTML::Element->new('p', id=>"addedcoverlink", align=>"center"); my $coverimageel = HTML::Element->new('a', onclick => "document.goto_page_relative(1)"); $coverp->push_content ($coverimageel); if ($config->add_cover_link ()) { $body->push_content ($coverp); $body->push_content (HTML::Element->new('mbp:pagebreak')); } #

# # Add TOC first also if --tocfirst # if ($tochref and $config->toc_first ()) { print STDERR "ADDING TOC FIRST ALSO: $tochref\n"; my $tree = new HTML::TreeBuilder (); $tree->ignore_unknown (0); $tree->parse_file ($tochref) || die "1-Could not find file: $tochref\n"; ### check_for_links ($tree); $linksinfo->check_for_links ($tree); my $b = $tree->find ("body"); $body->push_content ($b->content_list()); $body->push_content (HTML::Element->new('mbp:pagebreak')); } # # All files in manifest # foreach my $id (@opf_spine_ids) { my $filename = $opf->get_href ($id); my $mediatype = $opf->get_media_type ($id); print STDERR "SPINE: adding $id - $filename - $mediatype\n"; next unless ($mediatype =~ /text/); # only include text content my $tree = new HTML::TreeBuilder (); $tree->ignore_unknown (0); open FILE, "<$filename" or die "2-Could not find file: $filename\n"; { local $/; my $content = ; $content =~ s/&\#226;&\#8364;&\#166;/&\#8230;/g; # fixes bug in coding $tree->parse ($content); $tree->eof(); } if ($config->{FIXHTMLBR}) { fix_html_br ($tree, $config); } $linksinfo->check_for_links ($tree); print STDERR "Adding: $filename - $id\n"; # print STDERR "FILETOLINKCHECK:$filename:\n"; if ($linksinfo->link_exists ($filename)) { # print STDERR "FILETOLINKCHECK:$filename: SUCCESS\n"; my $a = HTML::Element->new('a', name => $filename); $body->push_content ($a); } print STDERR "+"; my $b = $tree->find ("body"); print STDERR "+"; my @content = $b->content_list(); print STDERR "+"; foreach my $c (@content) { $body->push_content ($c); # print STDERR $c; print STDERR "."; } print STDERR "+"; } print STDERR "All spine elements have been added\n"; if ($config->cover_image ()) { $coverimage = $config->cover_image (); } if ($coverimage) { copy ("../$coverimage", $coverimage); # copy if specified --coverimage $linksinfo->add_cover_image ($coverimage); if ($config->add_cover_link ()) { my $el = HTML::Element->new ('img', src => "$coverimage"); $coverimageel->push_content ($el); $linksinfo->check_for_links ($coverimageel); } } if ($config->thumb_image ()) { $linksinfo->add_thumb_image ($config->thumb_image ()); } else { if ($coverimage) { $linksinfo->add_thumb_image ($coverimage); } } # # Fix anchor to positions given by id="III"... # # filepos="0000057579" # my @refs = $body->look_down ("href", qr/^\#/); push @refs, $head->look_down ("href", qr/^\#/); my @hrefs = (); my @refels = (); my %href_to_ref = (); foreach my $r (@refs) { $r->attr ("filepos", "0000000000"); my $key = $r->attr ("href"); $key =~ s/\#//g; push @hrefs, $key; push @refels, $r; # $r->attr ("href", undef); } $html->push_content ($head); $html->push_content ($body); my $data = $html->as_HTML (); foreach my $i (0..$#hrefs) { my $h = $hrefs[$i]; my $r = $refels[$i]; my $searchfor1 = "id=\"$h\""; my $searchfor2 = "= 0) { # # search backwards for < # while (substr ($data, $pos, 1) ne "<") { $pos--; } ## $pos -=4; # back 4 positions to get to

attr ("filepos", "$form"); } else { ### print STDERR "SEARCHFOR2: $searchfor2\n"; $pos = index ($data, $searchfor2); if ($pos >= 0) { my $form = "0" x (10-length($pos)) . "$pos"; ### print STDERR "POSITION: $pos - $searchfor2 - $form\n"; $r->attr ("filepos", "$form"); } else { } } } # my @anchors = $body->look_down ("id", qr/./); # foreach my $a (@anchors) { # my $name = $a->attr("id"); # my $tag = $a->tag (); # my $text = $a->as_trimmed_text (); # if ($link_exists{$name}) { # $a->delete_content (); # my $ael = HTML::Element->new('a', name => $name); # $ael->push_content ($text); # $a->push_content ($ael); # } # print STDERR "ANCHORS: $tag - $name - $text\n"; # } # $html->push_content ($head); # $html->push_content ($body); return $html; } # # lit file functons # sub unpack_lit_file { my $litfile = shift; my $unpackdir = shift; print STDERR "Unpack file $litfile in dir $unpackdir\n"; mkdir $unpackdir; opendir DIR, $unpackdir; my @files = readdir (DIR); foreach my $f (@files) { if ($f =~ /^\./) { next; } if ($f =~ /^\.\./) { next; } # print STDERR "FILE: $f\n"; unlink "$unpackdir/$f"; } system ("clit \"$litfile\" $unpackdir") == 0 or die "system (clit $litfile $unpackdir) failed: $?"; } sub get_thumb_cover_image_data { my $filename = shift; ## print STDERR "COVERIMAGE: $filename\n"; my $data = ""; if (not -e $filename) { print STDERR "Image file does not exist: $filename\n"; return $data; } my $p = new GD::Image ("$filename"); my ($x, $y) = $p->getBounds(); # my $x = $p->width; # my $y = $p->height; ## add_text_to_image ($p, $opt_covertext); # pdurrant # Make thumb 320 high and proportional width # latest Mobipocket Creator makes Thumbnails 320 high my $scaled = scale_gd_image ($p, 320/$y); print STDERR "Resizing image $x x $y -> $x*320/$y x 320 -> scaled.jpg\n"; # my $scaled = scale_gd_image ($p, 180, 240); # print STDERR "Resizing image $x x $y -> 180 x 240 -> scaled.jpg\n"; return $scaled->jpeg (); } sub scale_gd_image { my $im = shift; my $x = shift; my $y = shift; my ($w0, $h0) = $im->getBounds(); # my $w0 = $im->width; # my $h0 = $im->height; my $w1 = $w0*$x; my $h1 = $h0*$x; print STDERR "SCALE GD: $w0 $h0 -> $w1 $h1\n"; if (defined $y) { $w1 = $x; $h1 = $y; } my $res = new GD::Image ($w1, $h1); $res->copyResized ($im, 0, 0, 0, 0, $w1, $h1, $w0, $h0); return $res; } sub get_text_image { my $width = shift; my $height = shift; my $text = shift; # my $image = Image::Magick->new; # $image->Set(size=>"$width x $height"); # $image->ReadImage('xc:white'); # $image->Draw (pen => "red", # primitive => "text", # x => 200, # y => 200, # font => "Bookman-DemiItalic", # text => "QQQQ$text, 200, 200", # fill => "black", # pointsize => 40); # $image->Draw(pen => 'red', fill => 'red', primitive => 'rectangle', # points => '20,20 100,100'); # $image->Write (filename => "draw2.jpg"); } sub get_gd_image_data { my $im = shift; my $filename = shift; my $quality = shift; $quality = -1 if not defined $quality; # # For some strange reason it does not work if using # the gif file with size 600x800 # ## if ($filename =~ /\.gif/ or $filename =~ /\.GIF/) { ## return $im->gif (); ## } if ($quality <= 0) { return $im->jpeg (); } else { return $im->jpeg ($quality); } } sub add_text_to_image { my $im = shift; my $text = shift; my $x = $im->Get ("width"); my $y = $im->Get ("height"); if (defined $text and $text) { print STDERR "DRAW TEXT: $text\n"; my $textim = get_text_image ($x, $y, $text); $im->Draw (primitive => "text", text => $text, points => "50,50", fill => "red", pointsize => 72); } $im->Write (filename => "draw.jpg"); } sub get_image_data { my $filename = shift; my $rescale = shift; my $config = shift; $rescale_large_images = $rescale if defined $rescale; my $scale_factor; $scale_factor = $config->scale_all_images() if defined $config; # pdurrant # make maxsize exactly 60KiB my $maxsize = 61440; $maxsize = $config->get_image_max_bytes () if defined $config; print STDERR "GET IMAGE DATA (file - maxsize): $filename - $maxsize\n"; # my $maxsize = 61000; my $maxwidth = 480; my $maxheight = 640; my $data = ""; if (not -e $filename) { print STDERR "Image file does not exist: $filename\n"; return $data; } my $filesize = -s $filename; my ($x, $y, $type) = imgsize ($filename); print STDERR "Reading data from file: $filename - $x x $y - $type\n"; # if ($filesize < $maxsize and $x < $maxwidth and $y<$maxheight # and $type ne "PNG") { # pdurrant # do not resize large images if the filesize is OK, # even if pixel dimensions are large if ($filesize < $maxsize and ((not $rescale_large_images) || ($x <= $maxwidth and $y <= $maxheight)) and $type ne "PNG" and (not defined $scale_factor or $scale_factor == 1.0)) { # No transformation has to be done, keep data as is print STDERR "No transformation: $filename - $x x $y\n"; open(IMG, $filename) or die "can't open $filename: $!"; binmode(IMG); # now DOS won't mangle binary input from GIF my $buff; while (read(IMG, $buff, 8 * 2**10)) { $data .= $buff; } return $data; } my $p = new GD::Image ("$filename"); if (not defined $p) { my $im = new Image::BMP (file => "$filename"); if (defined $im) { my $w = $im->{Width}; my $h = $im->{Height}; print STDERR "BMP IMAGE $filename: $w x $h\n"; $p = new GD::Image ($w, $h); foreach my $x (0..$w-1) { foreach my $y (0..$h-1) { my ($r,$g,$b) = $im->xy_rgb ($x, $y); my $index = $p->colorExact ($r, $g, $b); if ($index == -1) { $index = $p->colorAllocate ($r, $g, $b); } $p->setPixel ($x, $y, $index); } } } ## open IMAGE, ">dummy-$filename.jpg"; ## print IMAGE $p->jpeg (); ## close IMAGE; } ($x, $y) = $p->getBounds(); # reuse of $x and $y... # my $x = $p->width; # my $y = $p->height; # # If I do not resize 600x800 images it does not work on Gen3 # # check this one more time, 600x800 gif and jpeg with size # less than 64K does not work on Gen3 # # pdurrant # as of July 2008, # 600x800 with size less than 61440 does work on Gen3 # so must use the --imagerescale argument to get 600x800. if (defined $scale_factor and $scale_factor != 1.0) { print STDERR "SCALE IMAGE: $scale_factor\n"; $p = MobiPerl::Util::scale_gd_image ($p, $scale_factor); } if ($rescale_large_images) { my $xdiff = $x-$maxwidth; my $ydiff = $y-$maxheight; if ($ydiff > $xdiff) { if ($y > $maxheight) { my $scale = $maxheight*1.0/$y; $p = MobiPerl::Util::scale_gd_image ($p, $scale); } } else { if ($x > $maxwidth) { my $scale = $maxwidth*1.0/$x; $p = MobiPerl::Util::scale_gd_image ($p, $scale); } } } # # Scale if scale option given # or does it work just setting width? # ## $filename =~ s/\....$/\.gif/; ## print STDERR "UTIL FILENAME: $filename\n"; my $quality = -1; my $size = length (MobiPerl::Util::get_gd_image_data ($p, $filename)); if ($size > $maxsize) { $quality = 100; while (length (MobiPerl::Util::get_gd_image_data ($p, $filename, $quality)) > $maxsize and $quality >= 0) { $quality -= 10; } if ($quality < 0) { die "Could not shrink image file size for $filename"; } } ## if ($y < 640 and $x < 480 and defined $opt_scale) { ## my $scale = $opt_scale; ## $p = MobiPerl::Util::scale_gd_image ($p, $scale); ## print STDERR "Rescaling $$scale\n"; ## } $data = MobiPerl::Util::get_gd_image_data ($p, $filename, $quality); return $data; } sub iso2hex($) { my $hex = ''; for (my $i = 0; $i < length($_[0]); $i++) { my $ordno = ord substr($_[0], $i, 1); $hex .= sprintf("%lx", $ordno); } $hex =~ s/ $//;; $hex = "0x$hex"; return $hex; } sub fix_html { my $tree = shift; print STDERR "FIX HTML\n"; # # Fix strange HTML code # my @paras = $tree->find ("p"); my $inside_para = 0; my $newp; foreach my $p (@paras) { if (not $inside_para) { $newp = HTML::Element->new("p"); $inside_para = 1; } my $html = $p->as_HTML (); ## print STDERR "$html\n"; if ($html =~ /\ \;/) { ## print STDERR $newp->as_HTML (); my $h = $newp->as_HTML (); ## if ($h =~ /All three Stewards/) { ## last; ## } $p->replace_with ($newp); $inside_para = 0; print STDERR "P"; } else { my @span = $p->find ("span"); foreach my $span (@span) { $span->replace_with ($span->content_list ()); } $p->normalize_content (); $newp->push_content ($p->content_list ()); $newp->push_content (" "); $p->delete (); print STDERR "+"; } } } sub fix_html_br { my $tree = shift; my $config = shift; print STDERR "FIX HTML BR\n"; # # Fix strange HTML code with

instead if

# my $b = $tree->find ("body"); print STDERR "+"; my @content = $b->content_list(); print STDERR "+"; my @paras = (); my $p = HTML::Element->new("p"); push @paras, $p; my $i = 0; while ($i <= $#content) { # print STDERR "-"; my $c = $content[$i]; if ($c and ref($c) eq "HTML::Element") { my $tag = $c->tag; if ($tag eq "br" and ref($c) eq "HTML::Element" and defined $content[$i+1] and ref ($content[$i+1]) and $content[$i+1]->tag eq "br") { $p = HTML::Element->new("p"); push @paras, $p; if ($config->{KEEPBR}) { # $p->push_content (HTML::Element->new("br")); $p->push_content (HTML::Element->new("br")); } $i++; if ($i % 10 == 0) { print STDERR "P"; } } else { # print STDERR $c->as_HTML; $p->push_content ($c); } ## print STDERR "TAG:$tag:\n"; } else { if (ref($c)) { # print STDERR $c->as_HTML; } else { # print STDERR $c; } $p->push_content ($c); } $i++; } $b->delete_content (); $b->push_content (@paras); } sub fix_pre_tags { my $tree = shift; print STDERR "FIX PRE TAGS\n"; my @pres = $tree->find ("pre"); foreach my $pre (@pres) { print STDERR "FIX PRE TAGS: $pre\n"; my $p = HTML::Element->new("p", align => "left"); my @content = $pre->content_list (); my $text = $content[0]; my @lines = split ("\n", $text); foreach my $line (@lines) { my $br = HTML::Element->new("br"); $line =~ s/\s/ \;/g; ## print STDERR $line; $p->push_content ($line); $p->push_content ($br); $p->push_content ("\n"); } $pre->replace_with ($p); } } sub remove_java_script { my $tree = shift; print STDERR "REMOVE SCRIPT CODE\n"; my @scripts = $tree->find ("script"); foreach my $script (@scripts) { print STDERR "REMOVING SCRIPT NODE: $script\n"; $script->detach (); } } return 1; mobiperl-0.0.43/mobi2html0000755000175000017500000002567011230442002014262 0ustar tompetompe#!/usr/bin/env perl # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # mobi2html, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use Encode; use FindBin qw($RealBin); use lib "$RealBin"; use MobiPerl::Util; use HTML::TreeBuilder; use Palm::PDB; use Palm::Doc; use Image::Size; use Date::Parse; use Date::Format; use Getopt::Mixed; use MobiPerl::EXTH; use strict; use vars qw ($opt_rawhtml $opt_record0 $opt_saveallrecords $opt_mobihtml); Getopt::Mixed::getOptions ("rawhtml record0 saveallrecords mobihtml"); my $globalcodepage = 0; my $fullpathfilename = shift; if (not $fullpathfilename) { print "Usage: mobi2html [options] filename [unpackdir]\n"; print "Options: --rawhtml\n"; print " --record0\n"; print " --saveallrecords\n"; print " --mobihtml\n"; exit 0; } my $explodedir = shift; my $filename = $fullpathfilename; $filename =~ s!^.*/!!; $filename =~ s!^.*\\!!; $explodedir = "unpacked" unless $explodedir; print STDERR "FULLFILENAME: $fullpathfilename\n"; print STDERR "FILENAME: $filename\n"; print STDERR "UNPACK DIRECTORY: $explodedir\n"; die "A directory to exlode the mobi file must be specified as second argument" unless defined $explodedir; die "File does not exist: $fullpathfilename" unless -e $fullpathfilename; mkdir $explodedir; my $pdb = new Palm::PDB; $pdb->Load($fullpathfilename); my $name = $pdb->{"name"}; my $version = $pdb->{"version"}; my $type = $pdb->{"type"}; my $creator = $pdb->{"creator"}; my $seed = $pdb->{"uniqueIDseed"}; my $ctime = $pdb->{"ctime"}; my $mtime = $pdb->{"mtime"}; my $sctime = ctime ($ctime); my $smtime = ctime ($mtime); print STDERR "Name: $name\n"; print STDERR "Version: $version\n"; print STDERR "Type: $type\n"; print STDERR "Creator: $creator\n"; ##print STDERR "Seed: $seed\n"; ##print STDERR "Resdb: " . $pdb->{"attributes"}{"ResDB"} . "\n"; ##print STDERR "AppInfoDirty: " . $pdb->{"attributes"}{"AppInfoDirty"} . "\n"; ##print STDERR "ctime: $ctime - $sctime\n"; ##print STDERR "mtime: $mtime - $smtime\n"; print STDERR "baktime: " . $pdb->{"baktime"} . "\n"; my @records = @{$pdb->{"records"}}; print STDERR "Number of record: " . $#records . "\n"; my $image_index = 0; my %image_index_to_filename = (); foreach my $r (@records) { my $id = $r->{"id"}; my $cat = $r->{"category"}; my $offset = $r->{"offset"}; my $data = $r->{"data"}; my $size = length ($data); my $filename = "record-$id"; my ($x, $y, $type) = imgsize(\$data); if (defined $x) { ## print STDERR "Record $id - $cat - $offset - $size - $x x $ y\n"; $image_index++; $image_index_to_filename{$image_index} = "$filename.$type"; open DATA, ">$explodedir/$filename.$type"; binmode (DATA); print DATA $data; close DATA; # print STDERR "SIZE: $x $y\n"; } else { if (defined $opt_record0 or defined $opt_saveallrecords) { open DATA, ">$explodedir/$filename"; print DATA $data; close DATA; } } if (defined $opt_record0) { exit (0); } if ($id == 0) { parse_record_0 ($data); } } #my @resources = @{$pdb->{"resources"}}; #print STDERR "Number of resources: " . $#resources . "\n"; my $text = $pdb->text; # # One example file contained null character. Removing them solved the problem # #$text =~ s///g; #$text =~ s///g; # # Test of hack utf-8 # #$text =~ s/’/'/g; #$text =~ s/ / /g; if (defined $opt_rawhtml) { binmode (STDOUT); print $text; } my %fileposmap; print STDERR "Looking for filepos\n"; my $cp = 0; my $len = length ($text); while ($cp < $len) { my $s = substr ($text, $cp, 50); if (substr ($s, 0, 7) eq "filepos") { if ($s =~ /^filepos=(\d+)/) { # print STDERR "FILEPOS: $cp - $1\n"; $fileposmap{$1} = 1; } if ($s =~ /^filepos=\"(\d+)\"/) { # print STDERR "FILEPOS: $cp - $1\n"; $fileposmap{$1} = 1; } } $cp++; } print STDERR "Found all filepos\n"; my $offset = 0; print STDERR "Adding name attributes\n"; foreach my $pos (sort keys %fileposmap) { # print STDERR "NAMEPOS: $pos\n"; my $a = substr ($text, $pos+$offset, 2); if ($a eq " # Put an empty acnhor before mbp:pagebreak that are doubled... substr ($text, $pos+$offset, 2, "<"); $offset += (15 + length ($pos)); next; } print STDERR "WARNING: $pos - Not an anchor: $a\n"; } my $tree = new HTML::TreeBuilder (); $tree->ignore_unknown (0); ##if ($text =~ / / or $text =~ /’/) { if ($globalcodepage == 1252 or $globalcodepage == 0) { $tree->parse ($text); # seems to generate the corrrect entities... } if ($globalcodepage == 65001) { print STDERR "UTF-8 detected: convert before TreBuilder parse\n"; $tree->parse (decode_utf8 $text); # seems to generate the corrrect entities... } $tree->eof (); #my $tree = HTML::TreeBuilder->new_from_content ($text); fix_filepos_attributes ($tree); fix_image_tags ($tree); my $htmlfile = $filename; $htmlfile =~ s/\.mobi/.html/; $htmlfile =~ s/\.prc/.html/; $htmlfile =~ s/\.pdb/.html/; $htmlfile =~ s/\.azw/.html/; open HTML, ">$explodedir/$htmlfile" or die "Could not open file $explodedir/$htmlfile"; my $html = $tree->as_HTML; if (not defined $opt_mobihtml) { $html =~ s//
/g; # $html =~ s///g; $html =~ s/<\/mbp:pagebreak>//g; $html =~ s/.*?<\/guide>//g; $html =~ s///g; $html =~ s/<\/mbp:nu>//g; $html =~ s///g; $html =~ s/<\/mbp:section>//g; $html =~ s///g; $html =~ s/<\/mbp:frameset>//g; $html =~ s///g; $html =~ s/<\/mbp:slave-frame>//g; $html =~ s/\/div>/\/div>\n/g; } if ($globalcodepage == 1252) { $html =~ s///; } if ($globalcodepage == 65001) { $html =~ s///; } print HTML $html; close HTML; sub fix_image_tags { my $tree = shift; my @imgel = $tree->find ("img"); foreach my $img (@imgel) { my $recindex = $img->attr ("recindex"); my $ind = int ($recindex); my $filename = $image_index_to_filename{$ind}; ## print STDERR "FIX IMAGE TAGS: $recindex - $ind - $filename\n"; $img->attr ("recindex", undef); $img->attr ("src", $filename); } } sub fix_filepos_attributes { my $tree = shift; my @ael = $tree->find ("a"); print STDERR "Fixing filpos attribute\n"; foreach my $a (@ael) { my $filepos = $a->attr ("filepos"); if ($filepos) { $a->attr ("href", "\#$filepos"); $a->attr ("filepos", undef); ## print STDERR "FIX FILEPOS ATTR: $filepos\n"; } } } sub parse_record_0 { my $rec = shift; my $palmdocheader = substr ($rec, 0, 16); parse_palmdoc_header ($palmdocheader); if ($type eq "BOOK" and $creator eq "MOBI") { my $mobiheader = substr ($rec, 16); parse_mobi_header ($mobiheader); } } sub parse_palmdoc_header { my $data = shift; my ($version, $length, $nrecords, $recsize, $unknown) = unpack ("nxxNnnN", $data); print STDERR "PDHEADER Version: $version\n"; print STDERR "PDHEADER Length: $length\n"; print STDERR "PDHEADER NRecords: $nrecords\n"; print STDERR "PDHEADER Recsize: $recsize\n"; print STDERR "PDHEADER Unknown: $unknown\n"; } sub parse_mobi_header { my $data = shift; my ($doctype, $length, $type, $codepage, $uniqueid, $ver) = unpack ("a4NNNNN", $data); my ($exthflg) = unpack ("N", substr ($data, 0x70)); my $extradataflag = unpack ("n", substr ($data, 242-16)); print STDERR "MOBIHEADER doctype: $doctype\n"; print STDERR "MOBIHEADER length: $length\n"; print STDERR "MOBIHEADER type: $type\n"; print STDERR "MOBIHEADER codep: $codepage\n"; print STDERR "MOBIHEADER uniqid: $uniqueid\n"; print STDERR "MOBIHEADER ver: $ver\n"; print STDERR "MOBIHEADER exthflg: $exthflg\n"; print "MOBIHEADER xtradata: $extradataflag\n"; $pdb->{multibyteoverlap} = $extradataflag & 1; $globalcodepage = $codepage; if ($exthflg & 0x40) { my $exth = substr ($data, $length); parse_mobi_exth ($exth); } } sub parse_mobi_exth { my $data = shift; my ($doctype, $len, $n_items) = unpack ("a4NN", $data); print STDERR "EXTH doctype: $doctype\n"; print STDERR "EXTH length: $len\n"; print STDERR "EXTH n_items: $n_items\n"; my $pos = 12; foreach (1..$n_items) { my ($id, $size) = unpack ("NN", substr ($data, $pos)); my $contlen = $size-8; my ($id, $size, $content) = unpack ("NNa$contlen", substr ($data, $pos)); my $hid = sprintf ("%x", $id); my $hsize = sprintf ("%x", $size); if (MobiPerl::EXTH::is_binary_data ($id)) { $content = MobiPerl::Util::iso2hex ($content); } print STDERR "ITEM: $hid $hsize - $id $size - $content\n"; $pos += $size; } } =pod =head1 NAME mobi2html - A script to explode a DRM-free MobiPocket file to html =head1 SYNOPSIS mobi2html file.mobi [unpackdir] =head1 DESCRIPTION A script to explode a DRM-free MobiPocket file to html. If no unpack directory is specified the directory unpacked in the current directory will be used. =head1 OPTIONS =over 4 =item B<--mobihtml> Do not remove MobiPocket specific HTML code. Should be used if you want to unpack a book and fix some problem and then pack it to a new MobiPocket file. =item B<--rawhtml> Output the unmodified HTML code on STDOUT. Mostly useful for debugging. =back =head1 EXAMPLES mobi2html "Bleak House.prc" unpack mobi2html "Bleak House.prc" mobi2html "Bleak House.prc" unpack --rawhtml > t.html =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/html2mobi0000755000175000017500000003171211230442002014254 0ustar tompetompe#!/usr/bin/env perl # html2mobi, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; use MobiPerl::MobiFile; use MobiPerl::Opf; use MobiPerl::Config; use MobiPerl::LinksInfo; use HTML::TreeBuilder; use Getopt::Mixed; use strict; use vars qw ($opt_title $opt_author $opt_htmlfile $opt_mobifile $opt_gentoc $opt_coverimage $opt_addthumbnail $opt_noimages $opt_addcoverlink $opt_prefixtitle $opt_fixhtml $opt_fixhtmlbr $opt_keepbr $opt_imagerescale $opt_removejavascript $opt_scaleallimages $opt_imagemaxbytes); Getopt::Mixed::getOptions ("title=s author=s htmlfile=s mobifile=s gentoc coverimage=s addthumbnail=s noimages addcoverlink prefixtitle=s fixhtml fixhtmlbr keepbr removejavascript scaleallimages=s imagerescale=s imagemaxbytes=s"); # # expand html document with links... # Generate TOC automatically, guide thingy # Getting images to work... # Small image in library, 8 record, 180x240 jpeg # make testhtmlsgentoc, links in non generatec toc is not working # # # Debian: libpalm-perl # libimage-size-perl # # 8 DWord dwType //pub type: 2=book,3=palmdoc,4=audio,news=257,feed=258,magazin e=259 etc # C DWord dwCodepage //1252=western, 65001 = UTF8. Better not use anything else my @filenames = @ARGV; if ($#filenames < 0) { print "Usage: html2mobi [options] filename [filename]*\n"; print "Options: --title TITLE\n"; print " --author AUTHOR\n"; print " --htmlfile FILENAME\n"; print " --mobifile FILENAME\n"; print " --coverimage FILENAME\n"; print " --addthumbnail FILENAME\n"; print " --prefixtitle PREFIX\n"; print " --gentoc\n"; print " --noimages\n"; print " --addcoverlink\n"; print " --fixhtml\n"; print " --keepbr\n"; print " --removejavascript\n"; print " --scaleallimages factor\n"; print " --imagerescale 0|1\n"; print " --imagemaxbytes n\n"; exit 0; } my $tree = 0; my %file_to_tree = (); my %file_to_title = (); my $namerefindex = 0; my %file_to_nameref = (); my $linksinfo = new MobiPerl::LinksInfo; my $mobifile = "t.mobi"; my $config = new MobiPerl::Config; $config->add_cover_link (1) if defined $opt_addcoverlink; $config->no_images (1) if defined $opt_noimages; $config->cover_image ($opt_coverimage) if defined $opt_coverimage; $config->thumb_image ($opt_addthumbnail) if defined $opt_addthumbnail; $config->author ("Unspecified Author"); $config->author ($opt_author) if defined $opt_author; $config->title ($opt_title) if defined $opt_title; $config->prefix_title ($opt_prefixtitle); $config->remove_java_script (1) if defined $opt_removejavascript; $config->{KEEPBR} = 1 if defined $opt_keepbr; $config->{SCALEALLIMAGES} = $opt_scaleallimages if defined $opt_scaleallimages; $config->set_image_max_bytes ($opt_imagemaxbytes) if defined $opt_imagemaxbytes; my $filename = $filenames[0]; $mobifile = $filename; $mobifile =~ s/\.html/\.mobi/; $mobifile =~ s/\.htm/\.mobi/; if ($#filenames == 0) { $tree = one_html_file ($filename); } else { if (not defined $opt_title) { $config->title ("dummycollectiontitle") } $tree = get_collection ($config, @filenames); } convert_tree ($tree, $linksinfo); if (defined $opt_htmlfile) { open HTML, ">$opt_htmlfile" or die "Could not open html file $opt_htmlfile: $!\n"; my $text = $tree->as_HTML; $text =~ s/&\;nbsp\;/ \;/g; print HTML $text; close HTML; } if (defined $opt_mobifile) { $mobifile = $opt_mobifile; } if ($mobifile eq $filename) { $mobifile .= ".mobi"; } my $rescaleimages = $MobiPerl::Util::rescale_large_images; $rescaleimages = $opt_imagerescale if defined $opt_imagerescale; MobiPerl::MobiFile::save_mobi_file ($tree, $mobifile, $linksinfo, $config, $rescaleimages); # # HTML manipulation functions # sub one_html_file { my $filename = shift; print STDERR "ONEHTMLFILE: $filename\n"; my $tree = new HTML::TreeBuilder (); $tree->ignore_unknown (0); $tree->parse_file ($filename) || die "Could not find file: $filename\n"; return $tree; } sub convert_tree { my $tree = shift; my $linksinfo = shift; $linksinfo->check_for_links ($tree); my $titleelement = $tree->find ("title"); if ($titleelement and not $config->title ()) { $config->title ($titleelement->as_trimmed_text ()); } ## print STDERR "T:", $config->title (), ":\n"; if (not $config->title ()) { my $title = $filename; $title =~ s/\.html//; $title =~ s/\.htm//; print STDERR "Setting title from filename: $title\n"; $config->title ($title); } my $title = $config->prefix_title () . $config->title (); $config->title ($title); my $coverimage = ""; if ($config->cover_image ()) { $coverimage = $config->cover_image (); } if ($coverimage) { $linksinfo->add_cover_image ($coverimage); if ($config->add_cover_link ()) { my $coverp = HTML::Element->new('p', id=>"addedcoverlink", align=>"center"); my $coverimageel = HTML::Element->new('a', onclick => "document.goto_page_relative(1)"); $coverp->push_content ($coverimageel); my $el = HTML::Element->new ('img', src => "$coverimage"); $coverimageel->push_content ($el); my $body = $tree->find ("body"); if ($body) { print STDERR "Adding cover link: $coverimage\n"; $body->unshift_content ($coverp); } $linksinfo->check_for_links ($tree); } } if ($config->thumb_image ()) { $linksinfo->add_thumb_image ($config->thumb_image ()); } else { if ($coverimage) { $linksinfo->add_thumb_image ($coverimage); } } MobiPerl::Util::fix_pre_tags ($tree); if ($config->remove_java_script ()) { MobiPerl::Util::remove_java_script ($tree); } if (defined $opt_fixhtml) { MobiPerl::Util::fix_html ($tree); } if (defined $opt_fixhtmlbr) { MobiPerl::Util::fix_html_br ($tree, $config); } # Fix links, convert them to filepos my @refs = $tree->look_down ("href", qr/^\#/); my @hrefs = (); my @refels = (); my %href_to_ref = (); foreach my $r (@refs) { $r->attr ("filepos", "0000000000"); my $key = $r->attr ("href"); $key =~ s/\#//g; push @hrefs, $key; push @refels, $r; # $r->attr ("href", undef); } my $data = $tree->as_HTML (); foreach my $i (0..$#hrefs) { my $h = $hrefs[$i]; my $r = $refels[$i]; my $searchfor1 = "id=\"$h\""; my $searchfor2 = "= 0) { # # search backwards for < # while (substr ($data, $pos, 1) ne "<") { $pos--; } ## $pos -=4; # back 4 positions to get to

attr ("filepos", "$form"); } else { ### print STDERR "SEARCHFOR2: $searchfor2\n"; $pos = index ($data, $searchfor2); if ($pos >= 0) { my $form = "0" x (10-length($pos)) . "$pos"; ### print STDERR "POSITION: $pos - $searchfor2 - $form\n"; $r->attr ("filepos", "$form"); } else { } } } return $tree; } sub get_trees { my @files = @_; my @res = (); foreach my $filename (@files) { my $tree = new HTML::TreeBuilder (); $tree->ignore_unknown (0); $tree->parse_file ($filename) || die "Could not find file: $filename\n"; push @res, $tree; } return @res; } sub get_title { my $t = shift; my $res = ""; my $titleelement = $t->find ("title"); if ($titleelement) { $res = $titleelement->as_trimmed_text (); } return $res; } sub get_titles { my @trees = @_; my @res = (); foreach my $t (@trees) { my $title = get_title ($t); push @res, $title; } return @res; } sub get_toc_tree { my @files = @_; my $res = HTML::Element->new('ul'); my @trees = get_trees (@filenames); my @titles = get_titles (@trees); foreach my $i (0..@titles-1) { my $title = $titles[$i]; my $file = $files[$i]; my $name = "tocname-" . $namerefindex++; $file_to_tree{$file} = $trees[$i]; $file_to_title{$file} = $title; $file_to_nameref{$file} = $name; if (not $title) { # Not title tag in html file $title = $file; $title =~ s/\.html//i; $title =~ s/\.htm//i; } print STDERR "GETTOCTREETITLE: $title - $file - $name\n"; my $li = HTML::Element->new('li'); my $a = HTML::Element->new('a', href => "\#$name"); $a->push_content ($title); $li->push_content ($a); $res->push_content ($li); } return $res; } sub get_collection { my $config = shift; my @files = @_; my $toctree = get_toc_tree (@filenames); # print $toctree->as_HTML; my $title = $config->title (); my $html = HTML::Element->new('html'); my $head = HTML::Element->new('head'); my $titleel = HTML::Element->new('title'); $titleel->push_content ($title); $head->push_content ($titleel); my $body = HTML::Element->new('body', topmargin => "0", leftmargin => "0", bottommargin => "0", rightmargin => "0"); # topmargin="1em" leftmargin="2em" bottommargin="0" rightmargin="0" # # Title # my $h1 = HTML::Element->new('h1'); $h1->push_content ($title); $body->push_content ($h1); # # Table of content # if (defined $opt_gentoc) { my $h2 = HTML::Element->new('h2'); $h2->push_content ("TABLE OF CONTENTS"); $body->push_content ($h2); $body->push_content ($toctree); } # # All files # foreach my $file (@files) { print STDERR "ADDING TO COLLECTION: $file\n"; my $tree = $file_to_tree{$file}; my $title = $file_to_title{$file}; my $nameref = $file_to_nameref{$file}; my $h2 = HTML::Element->new('h2'); my $a = HTML::Element->new('a', name => "$nameref"); $a->push_content ("$title"); $h2->push_content ($a); $body->push_content ($h2); my $b = $tree->find ("body"); $body->push_content ($b->content_list()); } $html->push_content ($head); $html->push_content ($body); return $html; } =pod =head1 NAME html2mobi - A script to convert html files or a collection of html files to a MobiPocket file =head1 SYNOPSIS html2mobi file.html html2mobi file1.html file2.html ... (not tested so much...) =head1 DESCRIPTION A script to convert html files or a collection of html files to a MobiPocket file =head1 OPTIONS =over 4 =item B<--title TITLE> Specify the title for the book. This overrides the value given in the html file. =item B<--prefixtitle PREFIX> Add a prefix to the title of the book. Useful for specifying number for books in series. =item B<--author AUTHOR> Specify the author of the book. =item B<--mobifile MOBIFILE> Name of the output file. This overrides the default value. =item B<--htmlfile HTMLFILE> Saves the html that is packed into mobi format. This html code contains Mobipocket specific things that are added automatically. This is mostly useful for debugging. =item B<--coverimage IMAGE> The image to be used as cover image. =item B<--addthumbnail IMAGE> The image to be used as tumb nail. If this flag is not used the specified cover image is used. =item B<--addcoverlink> Add link to cover image first in main HTML document. This requires the --coverimage flag. =item B<--fixhtmlbr> Tries to fix html files where two
in a row has been used instead of

. =item B<--keepbr> Used together with --fixhtmlbr and causes to
to be kept so the result is a book with space between paragraphs. =item B<--gentoc> For a collection of html files generate the table of contents automatically. =item B<--removejavascript> Remove Java script code from the html code before saving as a book. =item B<--scaleallimages factor> Scale factor to be applied to all images. =item B<--imagerescale 0|1> Default is rescaling images for them to work on Cybook Gen3. To disable this specify --imagerescale 0. =back =head1 EXAMPLES html2mobi Alice_In_Wonderland.html =head1 TODO - Specify margins with flags - Follow local links when given a root html file - Get meta information from somewhere... - Include wget - News argument (bbc, ....) =head1 BUGS =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/lit2mobi0000755000175000017500000001311111230442002014071 0ustar tompetompe#!/usr/bin/env perl # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # lit2mobi, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use strict; use FindBin qw($RealBin); use lib "$RealBin"; use MobiPerl::MobiFile; use MobiPerl::Opf; use MobiPerl::Config; use MobiPerl::LinksInfo; use Getopt::Mixed; use File::Copy; use vars qw ($opt_title $opt_author $opt_htmlfile $opt_mobifile $opt_coverimage $opt_addthumbnail $opt_noimages $opt_tocfirst $opt_addcoverlink $opt_prefixtitle $opt_fixhtml $opt_fixhtmlbr $opt_imagerescale); Getopt::Mixed::getOptions ("title=s author=s htmlfile=s mobifile=s coverimage=s addthumbnail=s noimages tocfirst addcoverlink prefixtitle=s fixhtml fixhtmlbr imagerescale=s"); #my @args = map { s/\s/\\ /g; $_ } @ARGV; #my $command = "html2mobi " . join " ", @args; #print STDERR "Command: $command\n"; #system ($command) == 0 or die "system ($command) failed: $!\n"; my $filename = shift; if (not $filename) { print "Usage: lit2mobi [options] filename\n"; print "Options: --title TITLE\n"; print " --author AUTHOR\n"; print " --htmlfile FILENAME\n"; print " --mobifile FILENAME\n"; print " --coverimage FILENAME\n"; print " --addthumbnail FILENAME\n"; print " --prefixtitle PREFIX\n"; print " --noimages\n"; print " --tocfirst\n"; print " --addcoverlink\n"; print " --fixhtml\n"; print " --keepbr\n"; print " --imagerescale 0|1\n"; exit 0; } if (not $filename =~ /\.lit$/) { die "File $filename has wrong extension\n"; } my $config = new MobiPerl::Config; $config->add_cover_link (1) if defined $opt_addcoverlink; $config->toc_first (1) if defined $opt_tocfirst; $config->no_images (1) if defined $opt_noimages; $config->cover_image ($opt_coverimage); $config->author ($opt_author); $config->title ($opt_title); $config->prefix_title ($opt_prefixtitle); $config->{FIXHTMLBR} = 1 if defined $opt_fixhtmlbr; MobiPerl::Util::unpack_lit_file ($filename, "ctmp"); my $opffile = $filename; my $mobifile = $filename; $mobifile =~ s/\.lit/\.mobi/; $mobifile = $opt_mobifile if defined $opt_mobifile; if ($mobifile eq $filename) { $mobifile .= ".mobi"; } my $rescaleimages = $MobiPerl::Util::rescale_large_images; $rescaleimages = $opt_imagerescale if defined $opt_imagerescale; $opffile =~ s/\.lit/.opf/; chdir "ctmp"; my $linksinfo = new MobiPerl::LinksInfo; print STDERR "Read in HTML tree from opf\n"; my $tree = MobiPerl::Util::get_tree_from_opf ($opffile, $config, $linksinfo); print STDERR "Have Read in HTML tree from opf\n"; MobiPerl::Util::fix_pre_tags ($tree); if (defined $opt_fixhtml) { MobiPerl::Util::fix_html ($tree); } if (defined $opt_htmlfile) { print STDERR "Saving HTML file: $opt_htmlfile\n"; open HTML, ">$opt_htmlfile" or die "Could not open html file $opt_htmlfile: $!\n"; print HTML $tree->as_HTML; close HTML; move ($opt_htmlfile, "../"); } MobiPerl::MobiFile::save_mobi_file ($tree, $mobifile, $linksinfo, $config, $rescaleimages); move ($mobifile, "../"); =pod =head1 NAME lit2mobi - A script to convert a lit file to a MobiPocket file =head1 SYNOPSIS html2mobi file.lit =head1 DESCRIPTION A script to convert a lit file to a MobiPocket file This requires clit (ConvertLit) to be installed and in the path. =head1 OPTIONS =over 4 =item B<--title TITLE> Specify the title for the book. This overrides the value given in the opf file. =item B<--prefixtitle PREFIX> Add a prefix to the title of the book. Useful for specifying number for books in series. =item B<--author AUTHOR> Specify the author of the book. This overrides the value given in the opf file. This value is stored in the EXTH part of record 0. =item B<--mobifile MOBIFILE> Name of the output file. This overrides the default value. =item B<--coverimage IMAGE> Use IMAGE as cover image instead of possible image found in opf directory. =item B<--addthumbnail IMAGE> The image to be used as tumb nail. If this flag is used the cover image is used instead. =item B<--addcoverlink> Add link to cover image first in main HTML document. Also add entry "Cover" in guide that jumps to the cover image in the HTML document. =item B<--tocfirst> Make a copy of the toc and place it first. =item B<--htmlfile HTMLFILE> Saves the html that is packed into mobi format. This html code contains Mobipocket specific things that are added automatically. This is mostly useful for debugging. =item B<--imagerescale 0|1> Default is rescaling images for them to work on Cybook Gen3. To disable this specify --imagerescale 0. =back =head1 EXAMPLES lit2mobi Alice_In_Wonderland.lit lit2mobi --tocfirst --addcoverlink The_Railway_Children.lit =head1 TODO - Extract language information from opf file =head1 BUGS =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/mobi2mobi0000755000175000017500000006566111230442002014250 0ustar tompetompe#!/usr/bin/env perl # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # mobi2mobi, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; # # TODO: # # - add extended tags to existing tags, test with I, Robot # - perldoc, document which packages are needed # - add arbitrary meta information... # # # These packages need to be installed via CPAN or via distribution # specific packages. # use Palm::PDB; use Palm::Doc; use Date::Format; # for ctime call use Getopt::Mixed; use Image::Size; # Used to recognize the first record that is an image # # The ActivePerl ppm packaged corresponding to the above is: # # p5-Palm # TimeDate # Getopt-Mixed # Image-Size # use MobiPerl::MobiHeader; # The MobiHeader package file is in the distribution use MobiPerl::Util; use MobiPerl::Config; use strict; use vars qw ($opt_outfile $opt_title $opt_updatedtitle $opt_prefixtitle $opt_author $opt_addauthor $opt_publisher $opt_description $opt_language $opt_subject $opt_databasename $opt_fiximagesizes $opt_gen3imagefix $opt_addthumbnail $opt_coverimage $opt_patch $opt_coveroffset $opt_exthtype $opt_addexthtype $opt_exthdata $opt_booktype $opt_delexthtype $opt_delexthindex $opt_mbpfile $opt_savecover $opt_savethumb $opt_imagerescale $opt_allowtts $opt_deleteclippinglimit $opt_imagemaxbytes); Getopt::Mixed::getOptions ("outfile=s title=s updatedtitle=s prefixtitle=s author=s addauthor=s description=s language=s subject=s publisher=s databasename=s patch fiximagesizes gen3imagefix exthtype=s addexthtype=s exthdata=s delexthtype=s delexthindex=s booktype=s addthumbnail=s coverimage=s coveroffset=s mbpfile=s savecover=s savethumb=s imagerescale=s allowtts deleteclippinglimit imagemaxbytes=s "); if (not defined $opt_outfile) { if (defined $opt_title or defined $opt_updatedtitle or defined $opt_prefixtitle or defined $opt_author or defined $opt_addauthor or defined $opt_description or defined $opt_language or defined $opt_subject or defined $opt_publisher or defined $opt_databasename or defined $opt_patch or defined $opt_fiximagesizes or defined $opt_gen3imagefix or defined $opt_exthtype or defined $opt_addexthtype or defined $opt_exthdata or defined $opt_delexthtype or defined $opt_delexthindex or defined $opt_booktype or defined $opt_addthumbnail or defined $opt_coverimage or defined $opt_coveroffset or defined $opt_mbpfile or defined $opt_imagerescale) { print STDERR "ERROR: --outfile must be specified\n"; exit 0; } } my $author = ""; my $addauthor = ""; my $publisher = ""; my $description = ""; my $subject = ""; my $title = ""; my $updatedtitle = ""; my $delexthindex = 0; $delexthindex = $opt_delexthindex if defined $opt_delexthindex; # # Read info from mbp file # if (defined $opt_mbpfile) { open MBP, "<$opt_mbpfile" or die "Could not open $opt_mbpfile for reading: $!"; binmode MBP; my $mbp = ""; { undef $/; $mbp = ; print STDERR "LENGTH: ", length($mbp), "\n"; } $author = get_mbp_data ("AUTH", $mbp); my $cover = get_mbp_data ("COVE", $mbp); my $abstract = get_mbp_data ("ABST", $mbp); my $genre = get_mbp_data ("GENR", $mbp); $publisher = get_mbp_data ("PUBL", $mbp); $title = get_mbp_data ("TITL", $mbp); print STDERR "Author: $author\n"; print STDERR "Title: $title\n"; print STDERR "Cover: $cover\n"; print STDERR "Abstract: $abstract\n"; print STDERR "Genre: $genre\n"; print STDERR "Publiser: $publisher\n"; } $author = $opt_author if defined $opt_author; $addauthor = $opt_addauthor if defined $opt_addauthor; $title = $opt_title if defined $opt_title; $updatedtitle = $opt_updatedtitle if defined $opt_updatedtitle; $publisher = $opt_publisher if defined $opt_publisher; $description = $opt_description if defined $opt_description; $subject = $opt_subject if defined $opt_subject; # # Read mobi file # my $filename = shift; if (not $filename) { print "Usage: mobi2mobi [options] filename\n"; print "Options: --outfile FILENAME\n"; print " --title TITLE\n"; print " --updatedtitle TITLE\n"; print " --prefixtitle PREFIX\n"; print " --author AUTHOR\n"; print " --addauthor AUTHOR\n"; print " --description DESCRIPTION\n"; print " --subject SUBJECT\n"; print " --language LANGUAGE\n"; print " --publisher PUBLISHER\n"; print " --databasename NAME\n"; print " --patch\n"; print " --fiximagesize\n"; print " --gen3imagefix\n"; print " --exthtype TYPE\n"; print " --addexthtype TYPE\n"; print " --exthdata DATA\n"; print " --delexthtype TYPE\n"; print " --delexthindex INDEX\n"; print " --booktype TYPE\n"; print " --addthumbnail FILENAME\n"; print " --coverimage FILENAME\n"; print " --coveroffset OFFSET\n"; print " --mbpfile FILENAME\n"; print " --savecover FILENAME\n"; print " --savethumb FILENAME\n"; print " --imagerescale 0|1\n"; print " --imagemaxbytes n\n"; exit 0; } my $rescaleimages = $MobiPerl::Util::rescale_large_images; $rescaleimages = $opt_imagerescale if defined $opt_imagerescale; my $config = new MobiPerl::Config; $config->set_image_max_bytes ($opt_imagemaxbytes) if defined $opt_imagemaxbytes; my $pdb = new Palm::PDB; $pdb->Load($filename); my $name = $pdb->{"name"}; my $version = $pdb->{"version"}; my $type = $pdb->{"type"}; my $creator = $pdb->{"creator"}; my $seed = $pdb->{"uniqueIDseed"}; my $ctime = $pdb->{"ctime"}; my $mtime = $pdb->{"mtime"}; my $btime = $pdb->{"baktime"}; my $sctime = ctime ($ctime); my $smtime = ctime ($mtime); my $sbtime = ctime ($btime); print "Database Name: $name\n"; print " Version: $version\n"; print " Type: $type\n"; print " Creator: $creator\n"; print " Seed: $seed\n"; print " Resdb: " . $pdb->{"attributes"}{"ResDB"} . "\n"; print " AppInfoDirty: " . $pdb->{"attributes"}{"AppInfoDirty"} . "\n"; print " ctime: $ctime - $sctime"; print " mtime: $mtime - $smtime"; print " baktime: $btime - $sbtime"; print "---------------------------------------------------\n"; # # # $pdb->{"name"} = $opt_databasename if defined $opt_databasename; my @records = @{$pdb->{"records"}}; # # Check image sizes and warn if they are to big # my $firstimageid = 0; foreach my $r (@records) { my $data = $r->{"data"}; my $id = $r->{"id"}; ## print "CECKING RECORD WITH ID: $id\n"; my ($x, $y) = imgsize (\$data); if (defined $x) { my $len = length ($data); if (not $firstimageid) { print "FIRST IMG Record Id: $id\n"; $firstimageid = $id; } if ($len > 65535) { print "ERROR: Record $id - Image data size definitely too large: $len\n"; } else { if ($len > 61000) { print "WARNING: Record $id - Image data size might be too large: $len\n"; } } # if ($x > 480) { # print "WARNING: Record $id - Image size $x x $y might be too large due to bug in Gen3\n"; # } } } print "---------------------------------------------------\n"; # # Find the first record with an image # my $imgindex = 0; my $coverimageindex = 0; my $thumbimageindex = 0; my $firstimageindex = 0; foreach my $r (@records) { my $data = $r->{"data"}; my ($x, $y) = imgsize (\$data); if (defined $x) { print "Image record index: $imgindex ($x x $y)\n"; $firstimageindex = $imgindex; ### $coverimageindex = $imgindex; ### Do not replace image since you cannot know that the image is a coverimage. last; } $imgindex++; } print "START IMAGE INDEX: $imgindex\n"; print "COVER IMAGE INDEX: $firstimageindex\n"; my $r0 = $records[0]; my $ismobi = parse_record_0 ($r0->{"data"}); my $palmdocheader = substr ($r0->{"data"}, 0, 16); # pdurrant: these two bytes are sometimes non-zero in Aportis DOC files # since we're converting to Mobipocket, force to zero substr($palmdocheader,2,2, pack("n",0)); my $lastid = $records[$#records]->{"id"}; print "LASTID: $lastid\n"; print "-----------------\n"; if (defined $opt_outfile) { my $coveroffset = -1; my $thumboffset = -1; $coveroffset = $opt_coveroffset if defined $opt_coveroffset; if (defined $opt_fiximagesizes) { foreach my $r (@records) { my $data = $r->{"data"}; my ($x, $y) = imgsize (\$data); if (defined $x) { open IMG, ">tmpimage"; binmode IMG; print IMG $data; close IMG; sleep (1); # Why is this sleep needed? # Without it the wrong file is usesd. # I there another way to ensure that the data is available? $r->{"data"} = MobiPerl::Util::get_image_data ("tmpimage"); } } } if (defined $opt_gen3imagefix) { foreach my $r (@records) { my $data = $r->{"data"}; my ($x, $y, $type) = imgsize (\$data); # if (defined $x and $type eq "JPG") { if (defined $x) { ## print STDERR "GEN3 image fix $type - $x x $y, shrinking image (maybe converting to gif is better...)\n"; open IMG, ">tmpimage"; binmode IMG; print IMG $data; close IMG; sleep (1); $r->{"data"} = MobiPerl::Util::get_image_data ("tmpimage", 1); } } } if (defined $opt_coverimage) { # if ($coverimageindex != 0) { # pdurrant: Sometimes the coverimageindex in the file is -1 for no # cover image perl might interpret that as a large positive number, so # make sure we skip it. if ($coverimageindex > 0 and $coverimageindex < 65535) { print STDERR "Setting record $coverimageindex to $opt_coverimage\n"; my $data = ""; # # Alaway rescale since the bug seems to be in Gen 3 # $data = MobiPerl::Util::get_image_data ($opt_coverimage, $rescaleimages, $config); # if ($ismobi) { # only rescale if not a mobi file # $data = MobiPerl::Util::get_image_data ($opt_coverimage, 0); # } else { # $data = MobiPerl::Util::get_image_data ($opt_coverimage, 1); # } $pdb->{"records"}->[$coverimageindex]->{"data"} = $data; $coveroffset = $coverimageindex-$imgindex; } else { my $img = Palm::PDB->new_Record(); $img->{"categori"} = 0; $img->{"attributes"}{"Dirty"} = 1; $lastid++; print "CREATING COVER IMAGE WITH ID: $lastid\n"; $img->{"id"} = $lastid; my $data = MobiPerl::Util::get_image_data ($opt_coverimage, $rescaleimages, $config); $img->{"data"} = $data; $pdb->append_Record ($img); my @records = @{$pdb->{"records"}}; my $coverindex = $#records; $coveroffset = $coverindex-$imgindex; # pdurrant: if there are no images in the file yet, note that the cover image is first if ($firstimageindex == 0) { $firstimageindex = $coverindex; } print "CREATING COVER IMAGE WITH COVEROFFSET: $coveroffset\n"; } } if (defined $opt_addthumbnail) { # if ($thumbimageindex != 0) { # pdurrant: Sometimes the thumbimageindex in the file is -1 for no # thumb image perl might interpret that as a large positive number, so # make sure we skip it. if ($thumbimageindex >0 && $thumbimageindex < 65535) { print STDERR "Setting record $thumbimageindex to $opt_addthumbnail\n"; my $data = MobiPerl::Util::get_thumb_cover_image_data ($opt_addthumbnail); $pdb->{"records"}->[$thumbimageindex]->{"data"} = $data; $thumboffset = $thumbimageindex-$imgindex; } else { my $img = Palm::PDB->new_Record(); $img->{"categori"} = 0; $img->{"attributes"}{"Dirty"} = 1; $lastid++; $img->{"id"} = $lastid; my $data = MobiPerl::Util::get_thumb_cover_image_data ($opt_addthumbnail); $img->{"data"} = $data; $pdb->append_Record ($img); my @records = @{$pdb->{"records"}}; my $thumbindex = $#records; $thumboffset = $thumbindex-$imgindex; # if there are no images in the file yet, note that the thumb image is first if ($firstimageindex == 0) { $firstimageindex = $thumbindex; } print "CREATING THUMBNAIL IMAGE WITH THUMBNAILOFFSET: $thumboffset\n"; } } if ($ismobi) { my $mh = substr ($r0->{"data"}, 16); if ($author) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "author", $author); } if ($addauthor) { $mh = MobiPerl::MobiHeader::add_exth_data ($mh, "author", $addauthor); } if ($publisher) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "publisher", $publisher); } if ($description) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "description", $description); } if ($subject) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "subject", $subject); } if ($title) { print STDERR "Setting new title: $title\n"; $mh = MobiPerl::MobiHeader::set_extended_title ($mh, $title); } if ($updatedtitle) { print STDERR "Setting new updated title: $updatedtitle\n"; $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "updatedtitle", $updatedtitle); } if (defined $opt_booktype) { print STDERR "Setting new booktype: $opt_booktype\n"; $mh = MobiPerl::MobiHeader::set_booktype ($mh, $opt_booktype); } if (defined $opt_language) { print STDERR "Setting new language: $opt_language\n"; $mh = MobiPerl::MobiHeader::set_language_in_header ($mh, $opt_language); } if (defined $opt_prefixtitle) { my $t = MobiPerl::MobiHeader::get_extended_title ($mh); $t = $opt_prefixtitle . $t; print STDERR "Setting new title: $t\n"; $mh = MobiPerl::MobiHeader::set_extended_title ($mh, $t); } if (defined $opt_patch) { my ($imgflg, $imgindex) = unpack ("nn", substr ($mh, 0xb0)); my $newimgindex = $imgindex; print STDERR "PATCHING IMGINDEX $imgindex -> $newimgindex\n"; substr ($mh, 0xb0, 4, pack ("nn", 0, $newimgindex)); } $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "thumboffset", $thumboffset) if ($thumboffset >= 0); $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "coveroffset", $coveroffset) if ($coveroffset >= 0); if (defined $opt_exthtype and defined $opt_exthdata) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "$opt_exthtype", $opt_exthdata); } if (defined $opt_addexthtype and defined $opt_exthdata) { $mh = MobiPerl::MobiHeader::add_exth_data ($mh, "$opt_addexthtype", $opt_exthdata); } if (defined $opt_delexthtype) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "$opt_delexthtype", undef, 0, $delexthindex); } if (defined $opt_allowtts) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "ttsflag", undef, 0, 0); } if (defined $opt_deleteclippinglimit) { $mh = MobiPerl::MobiHeader::set_exth_data ($mh, "clippinglimit", undef, 0, 0); } # pdurrant: if we know the first image index, note it in the mobi header # The CyBook Gen3 uses this as the base for offsets if ($firstimageindex > 0) { substr ($mh, 0x5c, 4, pack ("N", $firstimageindex)); } $r0->{"data"} = $palmdocheader . $mh; } else { my $mh = new MobiPerl::MobiHeader; my $t = $filename; $t =~ s/\.prc$//; $t =~ s/\.pdb$//; $t =~ s/\.mobi$//; # remove directory paths from name $t =~ s!^.*/!!; $t = $title if $title; $t = $opt_prefixtitle . $t if defined $opt_prefixtitle; print STDERR "WARNING: Add author do not work for books without EXTH headers, convert to a proper MobiPocket file first\n" if $addauthor; $mh->set_title ($t); $mh->set_author ($author) if $author; $mh->set_publisher ($publisher) if $publisher; $mh->set_description ($description) if $description; $mh->set_subject ($subject) if $subject; $mh->set_language ($opt_language) if $opt_language; # $mh->set_image_record_index ($imgindex); # pdurrant: if we know the first image index, note it in the mobi header # The CyBook Gen3 uses this as the base for offsets if ($firstimageindex > 0) { $mh->set_image_record_index ($firstimageindex); } $mh->set_thumb_offset ($thumboffset) if ($thumboffset >= 0); $mh->set_cover_offset ($coveroffset) if ($coveroffset >= 0); $pdb->{"type"} = "BOOK"; $pdb->{"creator"} = "MOBI"; $r0->{"data"} = $palmdocheader . $mh->get_data (); } $pdb->Write ($opt_outfile); } sub parse_record_0 { my $rec = shift; my $res = 0; my $palmdocheader = substr ($rec, 0, 16); parse_palmdoc_header ($palmdocheader); if ($type eq "BOOK" and $creator eq "MOBI") { my $mobiheader = substr ($rec, 16); parse_mobi_header ($mobiheader); $res = 1; } return $res; } sub parse_palmdoc_header { my $data = shift; my ($version, $length, $nrecords, $recsize, $unknown) = unpack ("nxxNnnN", $data); print "PDHEADER Version: $version\n"; print "PDHEADER Length: $length\n"; print "PDHEADER NRecords: $nrecords\n"; print "PDHEADER Recsize: $recsize\n"; print "PDHEADER Unknown: $unknown\n"; } sub parse_mobi_header { my $data = shift; my ($doctype, $length, $type, $codepage, $uniqueid, $ver) = unpack ("a4NNNNN", $data); my ($exthflg) = unpack ("N", substr ($data, 0x70)); if ($ver > 3) { my ($ciflg, $ciptr) = unpack ("nn", substr ($data, 0xb0)); print "MOBIHEADER ciflg: $ciflg\n"; print "MOBIHEADER ciptr: $ciptr\n"; } my $extradataflag = unpack ("n", substr ($data, 242-16)); my $langcode = MobiPerl::MobiHeader::get_mh_language_code ($data); my $lid = $langcode & 0xFF; my $sublid = ($langcode >> 10) & 0xFF; my $typedesc = MobiPerl::MobiHeader::get_booktype_desc ($type); my $langdesc = MobiPerl::MobiHeader::get_language_desc ($langcode); print "MOBIHEADER doctype: $doctype\n"; print "MOBIHEADER length: $length\n"; print "MOBIHEADER booktype: $type - $typedesc\n"; print "MOBIHEADER codep: $codepage\n"; print "MOBIHEADER uniqid: $uniqueid\n"; print "MOBIHEADER ver: $ver\n"; print "MOBIHEADER exthflg: $exthflg\n"; print "MOBIHEADER language: $langcode - $lid - $sublid - $langdesc\n"; print "MOBIHEADER xtradata: $extradataflag ", MobiPerl::Util::iso2hex ($extradataflag), "\n"; if ($exthflg & 0x40) { my $exth = substr ($data, $length); my $eh = new MobiPerl::EXTH ($exth); my $cover_offset = $eh->get_cover_offset (); print "COVEROFFSET: $cover_offset\n"; my $thumb_offset = $eh->get_thumb_offset (); print "THUMBOFFSET: $thumb_offset\n"; # if ($cover_offset) { # pdurrant: 0 is a valid cover_offset if ($cover_offset >= 0) { $coverimageindex = $imgindex + $cover_offset; } # if ($thumb_offset) { # pdurrant: 0 is a valid thumb_offset if ($thumb_offset >= 0) { $thumbimageindex = $imgindex + $thumb_offset; } if (defined $opt_savecover) { if ($cover_offset != 0) { my $filename = $opt_savecover; my $r = $records[$coverimageindex]; my $data = $r->{"data"}; my ($x, $y, $type) = imgsize(\$data); if (not $filename =~ /\./) { $filename .= "." . lc ($type); } if (defined $x) { print "Saving cover image $x x $y: $filename\n"; if (open DATA, ">$filename") { binmode (DATA); print DATA $data; close DATA; } else { print STDERR "Could not save cover: $!"; } } else { print STDERR "ERROR: Data at cover offset is not an image\n"; } } else { print STDERR "ERROR: No cover image in book\n"; } } if (defined $opt_savethumb) { if ($thumb_offset != 0) { my $filename = $opt_savethumb; my $r = $records[$thumbimageindex]; my $data = $r->{"data"}; my ($x, $y, $type) = imgsize(\$data); if (not $filename =~ /\./) { $filename .= "." . lc ($type); } if (defined $x) { print "Saving cover image $x x $y: $filename\n"; if (open DATA, ">$filename") { binmode (DATA); print DATA $data; close DATA; } else { print STDERR "Could not save thumb: $!"; } } else { print STDERR "ERROR: Data at thumb offset is not an image\n"; } } else { print STDERR "ERROR: No thumb image in book\n"; } } parse_mobi_exth ($exth); } my $title = MobiPerl::MobiHeader::get_extended_title ($data); print "LONGTITLE: $title\n"; } sub parse_mobi_exth { my $data = shift; my ($doctype, $len, $n_items) = unpack ("a4NN", $data); print "EXTH doctype: $doctype\n"; print "EXTH length: $len\n"; print "EXTH n_items: $n_items\n"; my $pos = 12; foreach (1..$n_items) { my ($id, $size) = unpack ("NN", substr ($data, $pos)); $pos += 8; my $contlen = $size-8; my ($content) = unpack ("a$contlen", substr ($data, $pos)); my $hid = sprintf ("%x", $id); my $hsize = sprintf ("%x", $size); my $desc = MobiPerl::EXTH::get_description ($id); if (MobiPerl::EXTH::is_binary_data ($id)) { $content = MobiPerl::Util::iso2hex ($content); } print "EXTH item: $id - $desc - $contlen - $content\n"; $pos += $contlen; } } sub get_mbp_data { my $key = shift; my $data = shift; my $res = ""; my $d = (split /$key/, $data)[-1]; if ($d and $d ne $data) { my ($len) = unpack ("N", $d); ($len, $res) = unpack ("Na$len", $d); # Looks like it. Longman mbp abstract has 2013, which is en dash in UTF-16. $res =~ s/\0//g; # coding is probably UTF-16 } return $res; } =pod =head1 NAME mobi2mobi - A program to manipulate mobi files =head1 SYNOPSIS mobi2mobi file.mobi mobi2mobi file.prc mobi2mobi --outfile out.mobi --prefixtitle "01-" file.mobi =head1 DESCRIPTION A program to manipulate MobiPocket files. Author and title can be set and a cover image (also thumb nail image for Gen3) can be added. There are two kind of prc files used for electronic books. One is a PalmDOC file which does not have a MOBI header but can contain HTML code marked up with MobiPocket specific markup and it can be read by a MobiPocket reader. For this format you cannot store meta information in the header. The other format is MobiPocket and it has a MOBI header and some additional data where you can store meta information and an extended title. This program can change the extended title for a MobiPocket file. It can also automatically convert a PalmDOC file to a MobiPocket file and set the title. It can also add author information to a PalmDOC file by converting it to a MobiPocket file and set the author meta information. It can also change or set the author information for a MobiPocket file. You can also add a prefix to a title in a MobiPocket file. This does not work for PalmDOC files. It is possible to add an image to the file. If there are no other images in the file then the added image will be used as cover image and thumb nail image for Cybook Gen3. You can also replace the cover image if it already exists. Just running the program on a mobifile without any flags will print some information about the file. Since there is no specification available for the MOBI header this program might generate books that are not entirely correct. So keep the original files... =head1 OPTIONS =over 4 =item B<--databasename NAME> Change the internal database name of the file (the first 31 characters in the file). This may be useful in conjunction with the oeb:redirect tag when creating mobi-format ebooks. =item B<--mbpfile FILE> Use the author and publisher info in the mbp file and set these in the outfile. --author and --publisher overrides this information. =item B<--title TITLE> Change the the title of the book. =item B<--prefixtitle prefix> Add a prefix to the title of the book. =item B<--author AUTHOR> Set the author of the book. =item B<--addauthor AUTHOR> Add another author of the book. =item B<--publisher PUBLISHER> Set the publisher of the book. =item B<--description DESCRIPTION> Set the description of the book. =item B<--subject SUBJECT> Set the subject of the book. Can currently only set one subject. =item B<--language LANGUAGE> Set the language of the book. LANGUAGE should be a string like "en-gb" or a number like 2057. =item B<--addthumbnail IMAGEFILE> Add an image that will be used as thumbnail image. If there already is a thumb nail image it will be replaced. The only way for it to be a thumb nail image is if the thumb offset is specified in the EXTH data. =item B<--coverimage IMAGEFILE> Set the cover image of a book. If there already is a cover image it will be replaced. The only way for it to be a cover image is if the cover offset is specified in the EXTH data. =item B<--savecover FILENAME> Save the cover image if it exists. If filename is given without extension the correct extension will be added. =item B<--savethumb FILENAME> Save the thumb nail image if it exists. If filename is given without extension the correct extension will be added. =item B<--fiximagesizes> Fix image sizes so that they are less then 61000 bytes and rescale so they work on the Gen3. =item B<--gen3imagefix> Fix image sizes so that they are less then 61000 bytes and rescale so they work on the Gen3. =item B<--coveroffset OFFSET> Change the offset of the cover image so it points to another image. =item B<--exthtype TYPE> The type of the EXTH item you want to change. This is the name of the type and the names can be found in MobiPerl/EXTH.pm. =item B<--addexthtype TYPE> Add another EXTH entry. TYPE is the name of the type and the names can be found in MobiPerl/EXTH.pm. =item B<--delexthtype TYPE> Delete all entries in EXTH with type TYPE. TYPE is the name of the type and the names can be found in MobiPerl/EXTH.pm. =item B<--delexthindex INDEX> Specify that --delexthtype should not delete all items of the type TYPE but just delete the first one if INDEX is 1 and the second one if INDEX is 2 and so on. =item B<--exthdata TYPE> The data of the EXTH item you want to change. =item B<--outfile FILENAME> Specifies an output file. Only when this flag is given is any conversion done. =item B<--imagerescale 0|1> Default is rescaling images for them to work on Cybook Gen3. To disable this specify --imagerescale 0. =item B<--allowtts> Allow text to speech to be used (for Kindle books). =item B<--deleteclippinglimit> Remove the clipping limit (for Kindle books). =back =head1 EXAMPLES mobi2mobi file.mobi mobi2mobi --outfile out.mobi --prefixtitle "01-" file.mobi mobi2mobi --outfile out.mobi --title "Correction of title" file.mobi mobi2mobi --addthumbnail cover.jpg --outfile out.mobi file.mobi =head1 TODO - Functionality to change or add other meta information then author. =head1 BUGS - according to the Mobilread thread there is some problem with DRM:ed files... =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/opf2mobi0000755000175000017500000001220711230442002014072 0ustar tompetompe#!/usr/bin/env perl # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # opf2mobi, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use strict; use FindBin qw($RealBin); use lib "$RealBin"; use MobiPerl::MobiFile; use MobiPerl::Opf; use MobiPerl::Config; use MobiPerl::LinksInfo; use Getopt::Mixed; use File::Copy; use File::Spec; use Cwd; use vars qw ($opt_title $opt_author $opt_htmlfile $opt_mobifile $opt_coverimage $opt_noimages $opt_tocfirst $opt_addcoverlink $opt_prefixtitle $opt_imagerescale $opt_imagemaxbytes); Getopt::Mixed::getOptions ("title=s author=s htmlfile=s mobifile=s coverimage=s noimages tocfirst addcoverlink prefixtitle=s imagerescale=s imagemaxbytes=s"); #my @args = map { s/\s/\\ /g; $_ } @ARGV; #my $command = "html2mobi " . join " ", @args; #print STDERR "Command: $command\n"; #system ($command) == 0 or die "system ($command) failed: $!\n"; my $filename = shift; if (not $filename) { print "Usage: opf2mobi [options] filename\n"; print "Options: --title TITLE\n"; print " --author AUTHOR\n"; print " --htmlfile FILENAME\n"; print " --mobifile FILENAME\n"; print " --coverimage FILENAME\n"; print " --prefixtitle PREFIX\n"; print " --noimages\n"; print " --tocfirst\n"; print " --addcoverlink\n"; print " --imagerescale 0|1\n"; print " --imagemaxbytes n\n"; exit 0; } if (not $filename =~ /\.opf$/) { die "File $filename has wrong extension\n"; } my $config = new MobiPerl::Config; $config->add_cover_link (1) if defined $opt_addcoverlink; $config->toc_first (1) if defined $opt_tocfirst; $config->no_images (1) if defined $opt_noimages; $config->cover_image ($opt_coverimage); $config->author ($opt_author); $config->title ($opt_title); $config->prefix_title ($opt_prefixtitle); $config->set_image_max_bytes ($opt_imagemaxbytes) if defined $opt_imagemaxbytes; my ($vol,$dir,$basefile) = File::Spec->splitpath ($filename); print STDERR "OPFFILE: $vol - $dir - $basefile\n"; my $cwd = getcwd; print STDERR "CURRENTDIR:$cwd\n"; if ("$vol$dir" ne "") { chdir "$vol$dir"; } my $mobifile = $basefile; $mobifile =~ s/\.opf/\.mobi/; $mobifile = $opt_mobifile if defined $opt_mobifile; if ($mobifile eq $basefile) { $mobifile .= ".mobi"; } my $rescaleimages = $MobiPerl::Util::rescale_large_images; $rescaleimages = $opt_imagerescale if defined $opt_imagerescale; my $linksinfo = new MobiPerl::LinksInfo; my $tree = MobiPerl::Util::get_tree_from_opf ($basefile, $config, $linksinfo); if (defined $opt_htmlfile) { open HTML, ">$opt_htmlfile" or die "Could not open html file $opt_htmlfile: $!\n"; print HTML $tree->as_HTML; close HTML; } MobiPerl::MobiFile::save_mobi_file ($tree, $mobifile, $linksinfo, $config, $rescaleimages); move("$mobifile", "$cwd"); =pod =head1 NAME opf2mobi - A script to convert an opf file to a MobiPocket file =head1 SYNOPSIS opf2mobi file.opf =head1 DESCRIPTION A script to convert an opf file to a MobiPocket file. =head1 OPTIONS =over 4 =item B<--title TITLE> Specify the title for the book. This overrides the value given in the opf file. =item B<--prefixtitle PREFIX> Add a prefix to the title of the book. Useful for specifying number for books in series. =item B<--author AUTHOR> Specify the author of the book. This overrides the value given in the opf file. This value is stored in the EXTH part of record 0. =item B<--mobifile MOBIFILE> Name of the output file. This overrides the default value. =item B<--addcoverlink> Add link to cover image first in main document. =item B<--tocfirst> Make a copy of the toc and place it first. =item B<--htmlfile HTMLFILE> Saves the html that is packed into mobi format. This html code contains Mobipocket specific things that are added automatically. This is mostly useful for debugging. =item B<--imagerescale 0|1> Default is rescaling images for them to work on Cybook Gen3. To disable this specify --imagerescale 0. =item B<--imagemaxbytes n> Set the max number if bytes for the image data for one image that is included in the MobiPocket file. For the file to work on old devices fo not set this value higher then 61440. =back =head1 EXAMPLES opt2mobi Alice_In_Wonderland.opf opf2mobi --tocfirst --addcoverlink The_Railway_Children.opf =head1 TODO - Extract language information from opf file =head1 BUGS =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/mobils0000755000175000017500000001563011230442002013645 0ustar tompetompe#!/usr/bin/env perl # Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # mobi2ls, Copyright (C) 2007 Tommy Persson, tpe@ida.liu.se # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use FindBin qw($RealBin); use lib "$RealBin"; use Palm::PDB; use Palm::Doc; use Date::Format; # for ctime call use Getopt::Mixed; use MobiPerl::MobiHeader; # The MobiHeader package file is in the distribution use MobiPerl::Util; use strict; use vars qw ($opt_R); Getopt::Mixed::getOptions ("R"); my $author = ""; my $publisher = ""; my $longtitle = ""; my $type = ""; # Type of the book my $creator = ""; # Creator of the book my $debug = 0; if ($#ARGV < 0) { # No arguments, ls all files in directory lsdir ("."); } else { foreach my $file (@ARGV) { lsfile ($file); } } sub lsdir { my $dirname = shift; opendir DIR, "$dirname" || die "Could not open dir: $dirname - $!"; my @files = readdir(DIR); my @dirs = (); close DIR; foreach my $file (@files) { my $name = "$dirname/$file"; next if ($file eq "."); next if ($file =~ /^\./); next if ($file eq ".."); if (-d $name) { if (defined $opt_R) { # print STDERR "RECURSIVE: $name\n"; push @dirs, $name; # lsdir ("$name"); } } if (-f $name) { if ($name =~ /\.mobi$/i or $name =~ /\.prc$/i) { lsfile ($name); } } } foreach my $dir (@dirs) { print "\n$dir:\n"; lsdir ($dir); } } sub lsfile { my $filename = shift; # print STDERR "lsfile: $filename\n"; if (not -f $filename or not ($filename =~ /\.mobi$/i or $filename =~ /\.prc$/i)) { return; } $author = "NOAUTHOR"; $longtitle = "NOTITLE"; $type = ""; $creator = ""; check_file ($filename); print "$author: $longtitle ($filename)\n"; } sub check_file { my $filename = shift; my $pdb = new Palm::PDB; $pdb->Load($filename); my $name = $pdb->{"name"}; my $version = $pdb->{"version"}; $type = $pdb->{"type"}; $creator = $pdb->{"creator"}; my $seed = $pdb->{"uniqueIDseed"}; my $ctime = $pdb->{"ctime"}; my $mtime = $pdb->{"mtime"}; my $btime = $pdb->{"baktime"}; my $sctime = ctime ($ctime); my $smtime = ctime ($mtime); my $sbtime = ctime ($btime); if ($debug) { print "Database Name: $name\n"; print " Version: $version\n"; print " Type: $type\n"; print " Creator: $creator\n"; print " Seed: $seed\n"; print " Resdb: " . $pdb->{"attributes"}{"ResDB"} . "\n"; print " AppInfoDirty: " . $pdb->{"attributes"}{"AppInfoDirty"} . "\n"; print " ctime: $ctime - $sctime"; print " mtime: $mtime - $smtime"; print " baktime: $btime - $sbtime"; print "---------------------------------------------------\n"; } my @records = @{$pdb->{"records"}}; my $r0 = $records[0]; my $ismobi = parse_record_0 ($r0->{"data"}); my $palmdocheader = substr ($r0->{"data"}, 0, 16); } sub parse_record_0 { my $rec = shift; my $res = 0; my $palmdocheader = substr ($rec, 0, 16); parse_palmdoc_header ($palmdocheader); if ($type eq "BOOK" and $creator eq "MOBI") { my $mobiheader = substr ($rec, 16); parse_mobi_header ($mobiheader); $res = 1; } return $res; } sub parse_palmdoc_header { my $data = shift; my ($version, $length, $nrecords, $recsize, $unknown) = unpack ("nxxNnnN", $data); if ($debug) { print "PDHEADER Version: $version\n"; print "PDHEADER Length: $length\n"; print "PDHEADER NRecords: $nrecords\n"; print "PDHEADER Recsize: $recsize\n"; print "PDHEADER Unknown: $unknown\n"; } } sub parse_mobi_header { my $data = shift; my ($doctype, $length, $type, $codepage, $uniqueid, $ver) = unpack ("a4NNNNN", $data); my ($exthflg) = unpack ("N", substr ($data, 0x70)); if ($ver > 3) { my ($ciflg, $ciptr) = unpack ("nn", substr ($data, 0xb0)); if ($debug) { print "MOBIHEADER ciflg: $ciflg\n"; print "MOBIHEADER ciptr: $ciptr\n"; } } my $langcode = MobiPerl::MobiHeader::get_mh_language_code ($data); my $lid = $langcode & 0xFF; my $sublid = ($langcode >> 10) & 0xFF; my $typedesc = MobiPerl::MobiHeader::get_booktype_desc ($type); my $langdesc = MobiPerl::MobiHeader::get_language_desc ($langcode); if ($debug) { print "MOBIHEADER doctype: $doctype\n"; print "MOBIHEADER length: $length\n"; print "MOBIHEADER booktype: $type - $typedesc\n"; print "MOBIHEADER codep: $codepage\n"; print "MOBIHEADER uniqid: $uniqueid\n"; print "MOBIHEADER ver: $ver\n"; print "MOBIHEADER exthflg: $exthflg\n"; print "MOBIHEADER language: $langcode - $lid - $sublid - $langdesc\n"; } if ($exthflg & 0x40) { my $exth = substr ($data, $length); my $eh = new MobiPerl::EXTH ($exth); my $cover_offset = $eh->get_cover_offset (); if ($debug) { print "COVEROFFSET: $cover_offset\n"; } parse_mobi_exth ($exth); } my $title = MobiPerl::MobiHeader::get_extended_title ($data); $longtitle = $title; # print "LONGTITLE: $title\n"; } sub parse_mobi_exth { my $data = shift; my ($doctype, $len, $n_items) = unpack ("a4NN", $data); if ($debug) { print "EXTH doctype: $doctype\n"; print "EXTH length: $len\n"; print "EXTH n_items: $n_items\n"; } my $pos = 12; foreach (1..$n_items) { my ($id, $size) = unpack ("NN", substr ($data, $pos)); my $contlen = $size-8; my ($id, $size, $content) = unpack ("NNa$contlen", substr ($data, $pos)); my $hid = sprintf ("%x", $id); my $hsize = sprintf ("%x", $size); my $desc = MobiPerl::EXTH::get_description ($id); if (MobiPerl::EXTH::is_binary_data ($id)) { $content = MobiPerl::Util::iso2hex ($content); } if ($desc eq "Author") { $author = $content; } if ($debug) { print "EXTH item: $id - $desc - $contlen - $content\n"; } $pos += $size; } } =pod =head1 NAME mobils - A "ls" program for MobiPocket files =head1 SYNOPSIS mobils mobils -R mobils file.mobi mobils file.prc find . -name "*.prc" -exec mobils \{\} \; =head1 DESCRIPTION A "ls" program for MobiPocket files. Currently lists author, title and filename. =head1 OPTIONS =over 4 =item B<-R> Like the -R flag to ls. Recursicely list MobiPocket files. =back =head1 EXAMPLES mobils mobils -R mobils file.mobi mobils file.prc find . -name "*.prc" -exec mobils \{\} \; =head1 BUGS =head1 AUTHOR Tommy Persson (tpe@ida.liu.se) =cut mobiperl-0.0.43/gpl-3.0.txt0000644000175000017500000010451311230442002014252 0ustar tompetompe GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . mobiperl-0.0.43/Makefile0000644000175000017500000000721211230442002014067 0ustar tompetompe FILEPREFIX = mobiperl-0.0.43 TARFILE =$(FILEPREFIX).tar RARFILE =$(FILEPREFIX)-win.rar ZIPFILE =$(FILEPREFIX)-win.zip PALMFILES = Palm/Doc.pm MOBIPERLFILES = \ MobiPerl/Config.pm \ MobiPerl/EXTH.pm \ MobiPerl/LinksInfo.pm \ MobiPerl/MobiFile.pm \ MobiPerl/MobiHeader.pm \ MobiPerl/Opf.pm \ MobiPerl/Util.pm \ FILES = mobi2html html2mobi lit2mobi \ mobi2mobi opf2mobi mobils \ gpl-3.0.txt \ Makefile README dist: -rm -rf $(FILEPREFIX) -mkdir $(FILEPREFIX) -mkdir $(FILEPREFIX)/Palm -mkdir $(FILEPREFIX)/MobiPerl cp $(FILES) $(FILEPREFIX)/ cp $(PALMFILES) $(FILEPREFIX)/Palm/ cp $(MOBIPERLFILES) $(FILEPREFIX)/MobiPerl/ tar cvf $(TARFILE) $(FILEPREFIX)/ pod2html mobi2mobi > html/mobi2mobi.html pod2html mobils > html/mobils.html pod2html mobi2html > html/mobi2html.html pod2html lit2mobi > html/lit2mobi.html pod2html opf2mobi > html/opf2mobi.html pod2html html2mobi > html/html2mobi.html copy: mobiperlupload $(TARFILE) mobiperlupload "html/*" # scp $(TARFILE) remote.ida.liu.se:www-pub/mobiperl/downloads/ # scp html/*.html index.html remote.ida.liu.se:www-pub/mobiperl/ copyhtml: mobiperlupload "html/*" # scp index.html html/*.html remote.ida.liu.se:www-pub/mobiperl/ hm: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -M XML::Parser::Lite::Tree -M Data::Dumper -M GD -M HTML::TreeBuilder -o html2mobi.exe html2mobi om: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -M XML::Parser::Lite::Tree -M Data::Dumper -M GD -M HTML::TreeBuilder -o opf2mobi.exe opf2mobi lm: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -M XML::Parser::Lite::Tree -M Data::Dumper -M GD -M HTML::TreeBuilder -o lit2mobi.exe lit2mobi mm: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -o mobi2mobi.exe mobi2mobi ml: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -o mobils.exe mobils mh: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Date::Parse -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -M Encode -o mobi2html.exe mobi2html mi: pp -M FindBin -M Palm::PDB -M Palm::Doc -M Date::Format -M Date::Parse -M Getopt::Mixed -M Image::Size -M Image::BMP -M MobiPerl::MobiHeader -M MobiPerl::MobiFile -M MobiPerl::Opf -M MobiPerl::Config -M MobiPerl::LinksInfo -M Win32::OLE -o mobi2html.exe mobi2html all: hm om lm mm mh ml wininstall: copy html2mobi.exe c:\Perlb820\bin copy opf2mobi.exe c:\Perlb820\bin copy lit2mobi.exe c:\Perlb820\bin copy mobi2mobi.exe c:\Perlb820\bin copy mobi2html.exe c:\Perlb820\bin copy mobi2imp.exe c:\Perlb820\bin copy mobils.exe c:\Perlb820\bin pack: "c:\Program Files\7-ZIP\7z" -tzip a $(ZIPFILE) html2mobi.exe opf2mobi.exe lit2mobi.exe mobi2mobi.exe mobi2html.exe mobils.exe $(TARFILE) oldpack: "c:\Program Files\WinRAR\rar" a $(RARFILE) html2mobi.exe opf2mobi.exe lit2mobi.exe mobi2mobi.exe mobi2html.exe $(TARFILE) mobiperl-0.0.43/README0000644000175000017500000002000611230442002013303 0ustar tompetompeChanges in 0.0.43 - Added flag "--imagemaxbytes n" to specify the maximum size of image data included in the mobi file to mobi2mobi. Changes in 0.0.42 - Added flag --allowtts to mobi2mobi - Added flag --deleteclippinglimit to mobi2mobi - Enhancement and bug fixes in Opf.pm (goto toc now works and it recognize more cover image tags) - Opf.pm: Removed BOM (byte order mark) from opf file data - --imagerescale default is now 0 (disabled rescaling) - Added flag "--imagemaxbytes n" to specify the maximum size of image data included in the mobi file. Changes in 0.0.41 - Flag --addauthor added. - Flag --addexthtype added. - --delexthtype now works to delete for example multiple authors: Usage: "--delexthtype author" or "--delexthtype 100". - Added flag for updated title: --updatedtitle - Added flag --delexthindex which can be used to specify that just on EXTH item should be deleted and 1 is the first and 2 the second and so on. - Changed the licence from GPL3 or later to GPL2 or later. Changes in 0.0.40 - --removejavascript flag added to html2mobi - XML::Parser::Lite::Tree have changed tag field to element. Fix for this. - opf2mobi: Now works with filename containing directory path. - Fixed bug in html2mobi and collections. Now links should be generated and --gentoc should work. Changes in 0.0.39 - Bug fixes from pdurrant integrated - pdurrant: Make thumb 320 high and proportional width latest Mobipocket Creator makes Thumbnails 320 high - pdurrant: make maxsize 61440 - The list of pdurrant fixes is not complete. Changes in 0.0.38 - Added flag --description to mobi2mobi. - Added flag --language to mobi2mobi. - Read title from mbp file (not tested...) in mobi2mobi. - Unpack directory argument is now optional for mobi2html. - Added print of usage when programs are started without argument. - --imagerescale flag added to a mobi2mobi and *2mobi programs. Default is rescaling. To not rescale use "--imagerescale 0". - %langmap in MobiHeader.pm extended but it is still not compleat. - Added flag --subject to mobi2mobi. Currently only one subject can be set. Changes in 0.0.37: - The --publisher flag to mobi2mobi now works correctly. - Added flag "--savecover FILENAME" to mobi2mobi. - Added flag "--savethumb FILENAME" to mobi2mobi. - Changed so that "--coverimage FILENAME" now only replaces an image if cover offset is specified in EXTH. If cover offset is not specified a new image is created and used as cover image. - Changed so that "--addthumbnail FILENAME" works in the same way as the --coverimage flag. Changes in 0.0.36: - added mobils to MobiPerl. Changes in 0.0.35: - Fixed a bug with mobi2html and files where no codepage was specified. - Fixed a bug in "html2mobi --fixhtmlbr file.html". It now seems possible to use this for eReader books that have been converted to html and were

has been used instead of

. - Added a flag --keepbr to used together with --fixhtmlbr if you want to have space between paragraphs (as it was originally with

. Changes in 0.0.34: - png files did not work for html2mobi. Changed so that png files are converted to jpg before it is packed into the mobi file. Changes in 0.0.33: - The fix for UTF-8 was wrong. It was applied if the codepage was 1252 also. Changes in 0.0.32: - html2mobi now works if images that are refered to in the html file does not exist. - Added flag --fixhtmlbr to html2mobi. This flag will try to fix html files where two
have been uses instead of

. - Removed mobi2imp from distribution since it is distributed seperately in MobileRead forum. Duplications are bad and confusing. - Fixed so that mobi2html works with utf-8 encoded files. - Added meta tag with charset in the html generated by mobi2html. - Added instructions of how to install Perl and modules on Mac on the web page. Changes in 0.0.31: - mobi2html changed so that < is recognized as an anchor position as a last resort. Changes in 0.0.30: - mobi2html changed so that in mobi2html - Fixed bug in MobiFile pointed out by Gary Tsang. The cover offset was set to 0 when no cover was available and that made the Kindle reader crash. - Fixed html2mobi so that file name is used as title in generated toc if no title tag is available. Changes in 0.0.27: - mobi2html now works with the mobi file specified with a full path - MobiPocket specific html removed in mobi2html. --mobihtml to keep it. Changes in 0.0.26: - --prefixtitle now works for mobi2mobi and when input is a PalmDOC file. - Fixed bug with mobi2mobi and --coverimage. It did not work if the file did not contain any images. - Fixed rescaling bug. Now also images whose height is greater than 640 are rescaled. This is done because of a bug in the Gen3. - Fixed bug in mobi2mobi --fiximagesizes. In Linux the wrong tmp file was read. - Introduced a flag --gen3imagefix to mobi2mobi that can be used if you have a book that hang the Gen3. These kind of hangings are often caused by a large image in the book. This is a firmware bug in the Gen3. - Added call to fix_pre_tags in lit2mobi - Now it is possible to add author information to a Mobipocket file that does not have an EXTH. Changes in 0.0.25: - Added all language codes and added info about language in mobi2mobi Changes in 0.0.24: - Added flag --boktype to mobi2mobi to change the booktype value. 2 = BOOK (default), 257 = NEWS (Wall Street Journal as this value) - Added flag --delexthtype that can be used to remove type mobi2mobi file.awz --outfile t.mobi --delexthtype cdetype - Fixed so that lit2mobi does not crach when failing to open an image file. Changes in 0.0.23: - Item 401 in EXTH can be 1 or 4 butes according to examples (awz files). Removed assumption that it was 4 bytes to get mobi2mobi to work for an example file. Changes in 0.0.22: - Fixed so that flags --coverimage, --addthumbnail, --addcoverlink works for html2mobi and lit2mobi. Changes in 0.0.21: - Added detection of images that are to large in mobi2mobi - Added flag --fiximagesizes to mobi2mobi so it will fix the incorrect image sizes. - --htmlfile flag for lit2mobi placed file in wrong dir, fixed now. - Added flag --coveroffset to mobi2mobi and fixed a bug relating to this. Now it is possible to change which image that is cover image by just specifying another offset. - The distribution is now a packed directory. - Added flags --exthtype and --exthdata to mobi2mobi to be able to set any type. Values can be set for unknown types 204, 205, 206, 207, 401, 403. This can help to figure out what these items mean. Changes in 0.0.20: -
-tags is now replaced with someting that displays better
  in a MobiPocket reader.

- Changed max file size for images to 61000 to avoid conversion of 
  a BMP image.

- Images are now not converted to jpg if they fit the file size and image size
  requirements.



Changes in 0.0.19:

- Fixed resizing of image data and rescaling of image for
  mobi2mobi. The rescaling is neccessary because of the Gen3
  not working on files with images with width larger than 480
  or something around that size. This seems to be a bug in the
  Gen3.

- Added Image::BMP to Util.pm. Used to convert a file
  from BMP to GD if needed.

- Fixed TOC bug caused by filepos attribute not removed in 
  mobit2html.

- Changed max file size for images to 60000.
  From MobiPocket forum: "- the size of each JPEG image is limited to
  about 63K for a number of technical reasons "

- Fixed filepos bug in html2mobi so now TOC in the beginning works
  for Twain example.

- Fixed mobi2html so it works for the Alice example were the filepos
  point to "