blib/lib/EBook/Ishmael/EBook/Mobi.pm

filter: Statementstmt 71.5% 403 / 563 Branchbran 46.9% 78 / 166 Conditioncond 39.0% 16 / 41 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 79.1% 38 / 48 Podpod 0.0% 0 / 10 totaltotal 63.0% 537 / 852 CC 122 SCAR 30.0
CC
122
Cov
65%
new
68.0
decode
42.0
_load_huff
30.0
CRAP 20.2
« blib/lib/EBook/Ishmael/Unzip.pm ↑ Summary blib/lib/EBook/Ishmael/ImageID.pm »
1 package EBook::Ishmael::EBook::Mobi;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Encode qw(from_to);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use EBook::Ishmael::Decode qw(palmdoc_decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use EBook::Ishmael::HTML qw(prepare_html);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use EBook::Ishmael::ImageID qw(image_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use EBook::Ishmael::PDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::Time qw(guess_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Many thanks to Tommy Persson, the original author of mobi2html, a script
18 # which much of this code is based off of.
19  
20 # TODO: Implement AZW4 support
21 # TODO: Add support for UTF16 MOBIs (65002)
22  
23 17 my $TYPE = 'BOOK';
24 17 my $CREATOR = 'MOBI';
25  
26 17 my $RECSIZE = 4096;
27  
28 17 my $NULL_INDEX = 0xffffffff;
29  
30 17 my $UNPACK_Q = !! eval { unpack "Q>", 1 };
31  
32 sub heuristic {
33  
34 70 ▶ my $class = shift;
Subroutine heuristic: called CC=5 SCAR=16.1
Pod undocumented
35 70 my $file = shift;
36 70 my $fh = shift;
37  
38 70 ▶ return 0 unless -s $file >= 68;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $file >= 68 0 70 70
39  
40 70 seek $fh, 32, 0;
41 70 read $fh, my ($null), 1;
42  
43 70 ▶ unless ($null eq "\0") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($null eq "\0") 35 35 70
44 35 return 0;
45 }
46  
47 35 seek $fh, 60, 0;
48 35 read $fh, my ($type), 4;
49 35 read $fh, my ($creator), 4;
50  
51 35 ▶ return 0 unless $type eq $TYPE && $creator eq $CREATOR;
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq $TYPE and $creator eq $CREATOR 24 11 35
Condition66%2/3
$type eq $TYPE and $creator eq $CREATOR
!ll&&!rl&&r
24011
MC/DC50%1/2
$type eq $TYPE and $creator eq $CREATOR
$type eq $TYPE $creator eq $CREATOR
Truth table66%2/3
A and B
A= $type eq $TYPEB= $creator eq $CREATOR
ABresult
0X0
100
111
52  
53 11 seek $fh, 78, 0;
54 11 read $fh, my ($off), 4;
55 11 $off = unpack "N", $off;
56 11 seek $fh, $off + 36, 0;
57 11 read $fh, my ($ver), 4;
58 11 $ver = unpack "N", $ver;
59  
60 11 return $ver != 8;
61  
62 }
63  
64  
65 # Many thanks to Calibre, much of the code in this module was based on their
66 # huffman decoder.
67  
68 17 package EBook::Ishmael::EBook::Mobi::MobiHuff {
69  
70 17 my $HUFF_HDR = pack "A4 N", 'HUFF', 24;
71 17 my $CDIC_HDR = pack "A4 N", 'CDIC', 16;
72  
73 sub _load_huff {
74  
75 0 ▶ my $self = shift;
Subroutine _load_huff: not called CC=5 SCAR=34.0
76 0 my $huff = shift;
77  
78 0 ▶ unless (substr($huff, 0, 8) eq $HUFF_HDR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($huff, 0, 8) eq $HUFF_HDR) 0 0 0
79 0 die "Invalid MOBI HUFF header\n";
80 }
81  
82 0 my @off = unpack "N N", substr $huff, 8, 8;
83  
84 0 @{ $self->{dict1} } = map {
85  
86 0 my $len = $_ & 0x1f;
87 0 my $term = $_ & 0x80;
88 0 my $max = $_ >> 8;
89  
90 0 ▶ if ($len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) 0 0 0
91 0 die "Invalid MOBI HUFF dictionary\n";
92 }
93  
94 0 ▶ if ($len <= 8 and !$term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 8 and not $term) 0 0 0
Condition0%0/3
$len <= 8 and not $term
!ll&&!rl&&r
000
MC/DC0%0/2
$len <= 8 and not $term
$len <= 8 not $term
Truth table0%0/3
A and not(B)
A= $len <= 8B= not $term
ABresult
0X0
100
111
95 0 die "Invalid MOBI HUFF dictionary\n";
96 }
97  
98 0 $max = (($max + 1) << (32 - $len)) - 1;
99  
100 0 [ $len, $term, $max ];
101  
102 } unpack "N256", substr $huff, $off[0], 4 * 256;
103  
104 0 my @dict2 = unpack "N64", substr $huff, $off[1], 4 * 64;
105  
106 0 my @mins = (0, map { $dict2[$_] } grep { $_ % 2 == 0 } (0 .. $#dict2));
107 0 my @maxs = (0, map { $dict2[$_] } grep { $_ % 2 != 0 } (0 .. $#dict2));
108  
109 0 $self->{mincode} = [ map { $mins[$_] << (32 - $_) } (0 .. $#mins) ];
110 0 $self->{maxcode} = [ map { (($maxs[$_] + 1) << (32 - $_)) - 1 } (0 .. $#maxs) ];
111  
112 0 return 1;
113  
114 }
115  
116 sub _load_cdic {
117  
118 0 ▶ my $self = shift;
Subroutine _load_cdic: not called CC=2 SCAR=17.9
119 0 my $cdic = shift;
120  
121 0 ▶ unless (substr($cdic, 0, 8) eq $CDIC_HDR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($cdic, 0, 8) eq $CDIC_HDR) 0 0 0
122 0 die "Invalid MOBI CDIC header\n";
123 }
124  
125 0 my ($phrases, $bits) = unpack "N N", substr $cdic, 8, 8;
126  
127 0 my $n = min(1 << $bits, $phrases - @{ $self->{dictionary} });
128  
129 0 push @{ $self->{dictionary} }, map {
130  
131 0 my $blen = unpack "n", substr $cdic, 16 + $_;
132  
133 [
134 0 substr($cdic, 18 + $_, $blen & 0x7fff),
135 $blen & 0x8000,
136 ];
137  
138 } unpack "n$n", substr $cdic, 16;
139  
140 0 return 1;
141  
142 }
143  
144 sub new {
145  
146 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
147 0 my $huff = shift;
148 0 my @cdic = @_;
149  
150 0 my $self = {
151 dict1 => [],
152 dictionary => [],
153 mincode => [],
154 maxcode => [],
155 };
156  
157 0 bless $self, $class;
158  
159 0 $self->_load_huff($huff);
160  
161 0 for my $c (@cdic) {
162 0 $self->_load_cdic($c);
163 }
164  
165 0 return $self;
166  
167 }
168  
169 sub decode {
170  
171 0 ▶ my $self = shift;
Subroutine decode: not called CC=6 SCAR=37.4
172 0 my $data = shift;
173  
174 0 my $left = length($data) * 8;
175 0 $data .= "\x00" x 8;
176 0 my $pos = 0;
177 0 my $x = unpack "Q>", $data;
178 0 my $n = 32;
179  
180 0 my $s = '';
181  
182 0 while (1) {
183  
184 0 ▶ if ($n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 0) 0 0 0
185 0 $pos += 4;
186 0 $x = unpack "Q>", substr $data, $pos, 8;
187 0 $n += 32;
188 }
189 0 my $code = ($x >> $n) & ((1 << 32) - 1);
190  
191 0 my ($len, $term, $max) = @{ $self->{dict1}[$code >> 24] };
192 0 ▶ unless ($term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term) 0 0 0
193 0 ▶ $len += 1 while $code < $self->{mincode}[$len];
Branch: 1 branch
BranchTrueFalseTotal
$code < $self->{"mincode"}[$len] and do { $len += 1 } 0 0 0
194 0 $max = $self->{maxcode}[$len];
195 }
196  
197 0 $n -= $len;
198 0 $left -= $len;
199 0 ▶ last if $left < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left < 0 0 0 0
200  
201 0 my $r = ($max - $code) >> (32 - $len);
202  
203 0 my ($slice, $flag) = @{ $self->{dictionary}[$r] };
204  
205 0 ▶ unless ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 0 0 0
206 0 $self->{dictionary}[$r] = [];
207 0 $slice = $self->decode($slice);
208 0 $self->{dictionary}[$r] = [ $slice, 1 ];
209 }
210  
211 0 $s .= $slice;
212  
213 }
214  
215 0 return $s;
216  
217 }
218  
219 }
220  
221 sub _clean_html {
222  
223 5 ▶ my $html = shift;
Subroutine _clean_html: called CC=1 SCAR=0.0
224  
225 5 $$html =~ s/<mbp:pagebreak\s*\//<br style=\"page-break-after:always\" \//g;
226 5 $$html =~ s/<mbp:pagebreak\s*/<br style=\"page-break-after:always\" \//g;
227 5 $$html =~ s/<\/mbp:pagebreak>//g;
228 5 $$html =~ s/<guide>.*?<\/guide>//g;
229 5 $$html =~ s/<\/?mbp:nu>//g;
230 5 $$html =~ s/<\/?mbp:section//g;
231 5 $$html =~ s/<\/?mbp:frameset>//g;
232 5 $$html =~ s/<\/?mbp:slave-frame>//g;
233  
234 5 return 1;
235  
236 }
237  
238 sub _trailing_entry_size {
239  
240 0 ▶ my $data = shift;
Subroutine _trailing_entry_size: not called CC=3 SCAR=24.8
241  
242 0 my $res = 0;
243  
244 0 my $trail = substr $data, -4;
245  
246 0 for my $c (unpack "C4", $trail) {
247 0 ▶ if ($c & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c & 128) 0 0 0
248 0 $res = 0;
249 }
250 0 $res = ($res << 7) | ($c & 0x7f);
251 }
252  
253 0 return $res;
254  
255 }
256  
257 sub _trailing_entries_size {
258  
259 138 ▶ my $self = shift;
Subroutine _trailing_entries_size: called CC=3 SCAR=12.0
260 138 my $data = shift;
261  
262 138 my $res = 0;
263  
264 138 for my $i (0 .. $self->{_trailers} - 1) {
265 0 my $n = _trailing_entry_size($data);
266 0 $res += $n;
267 0 substr $data, -$n, $n, '';
268 }
269  
270 138 ▶ if ($self->{_extra_data} & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_extra_data"} & 1) 138 0 138
271 138 $res += (ord(substr $data, -1) & 3) + 1;
272 }
273  
274 138 return $res;
275  
276 }
277  
278 # Index processing code was adapted from KindleUnpack
279  
280 sub _get_index_data {
281  
282 20 ▶ my $self = shift;
Subroutine _get_index_data: called CC=8 SCAR=20.9
283 20 my $idx = shift;
284  
285 20 ▶ return {} if $idx == $NULL_INDEX;
Branch: 1 branch
BranchTrueFalseTotal
if $idx == $NULL_INDEX 0 20 20
286  
287 20 my $outtbl = [];
288 20 my $ctoc = {};
289  
290 20 my $data;
291 20 $$data = $self->{_pdb}->record($idx)->data;
292  
293 20 my ($idxhdr, $hordt1, $hordt2) = $self->_parse_indx_header($data);
294 20 my $icount = $idxhdr->{count};
295 20 my $roff = 0;
296 20 my $off = $idx + $icount + 1;
297  
298 20 for my $i (0 .. $idxhdr->{nctoc} - 1) {
299 10 my $cdata = $self->{_pdb}->record($off + $i)->data;
300 10 my $ctocdict = $self->_read_ctoc(\$cdata);
301 10 for my $j (sort keys %$ctocdict) {
302 0 $ctoc->{ $j + $roff } = $ctocdict->{ $j };
303 }
304 10 $roff += 0x10000;
305 }
306  
307 20 my $tagstart = $idxhdr->{len};
308 20 my ($ctrlcount, $tagtbl) = _read_tag_section($tagstart, $data);
309  
310 20 for my $i ($idx + 1 .. $idx + 1 + $icount - 1) {
311 20 my $d = $self->{_pdb}->record($i)->data;
312 20 my ($hdrinfo, $ordt1, $ordt2) = $self->_parse_indx_header(\$d);
313 20 my $idxtpos = $hdrinfo->{start};
314 20 my $ecount = $hdrinfo->{count};
315 20 my $idxposits = [];
316 20 for my $j (0 .. $ecount - 1) {
317 100 my $pos = unpack "n", substr $d, $idxtpos + 4 + (2 * $j), 2;
318 100 push @$idxposits, $pos;
319 }
320 20 for my $j (0 .. $ecount - 1) {
321 100 my $spos = $idxposits->[$j];
322 100 my $epos = $idxposits->[$j + 1];
323 100 my $txtlen = ord(substr $d, $spos, 1);
324 100 my $txt = substr $d, $spos + 1, $txtlen;
325 100 ▶ if (@$hordt2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hordt2) 0 100 100
326 $txt = join '',
327 0 map { chr $hordt2->[ ord $_ ] }
328 split //, $txt;
329 }
330 100 my $tagmap = _get_tagmap(
331 $ctrlcount,
332 $tagtbl,
333 \$d,
334 $spos + 1 + $txtlen,
335 $epos
336 );
337 100 push @$outtbl, [ $txt, $tagmap ];
338 }
339 }
340  
341 20 return ( $outtbl, $ctoc );
342  
343 }
344  
345 sub _parse_indx_header {
346  
347 40 ▶ my $self = shift;
Subroutine _parse_indx_header: called CC=9 SCAR=30.7
348 40 my $data = shift;
349  
350 40 ▶ unless (substr($$data, 0, 4) eq 'INDX') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($$data, 0, 4) eq "INDX") 0 40 40
351 0 die "Index section is not INDX\n";
352 }
353  
354 40 my @words = qw(
355 len nul1 type gen start count code lng total ordt ligt nligt nctoc
356 );
357 40 my $num = scalar @words;
358 40 my @values = unpack "N$num", substr $$data, 4, 4 * $num;
359 40 my $header = {};
360  
361 40 for my $i (0 .. $#words) {
362 520 $header->{ $words[$i] } = $values[$i];
363 }
364  
365 40 my $ordt1 = [];
366 40 my $ordt2 = [];
367  
368 my (
369 40 $ocnt,
370 $oentries,
371 $op1,
372 $op2,
373 $otagx
374 ) = unpack "N N N N N", substr $$data, 0xa4, 4 * 5;
375  
376 40 ▶ if ($header->{code} == 0xfdea or $ocnt != 0 or $oentries > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->{"code"} == 65002 or $ocnt != 0 or $oentries > 0) 0 40 40
Condition33%2/6
$header->{"code"} == 65002 or $ocnt != 0 or $oentries > 0
l!l&&r!l&&!r
0040
$header->{"code"} == 65002 or $ocnt != 0
l!l&&r!l&&!r
0040
MC/DC0%0/3
$header->{"code"} == 65002 or $ocnt != 0 or $oentries > 0
$header->{"code"} == 65002 $ocnt != 0 $oentries > 0
Truth table25%1/4
A or B or C
A= $header->{"code"} == 65002B= $ocnt != 0C= $oentries > 0
ABCresult
1XX1
01X1
0011
0000
377  
378 0 ▶ unless ($ocnt == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ocnt == 1) 0 0 0
379 0 die "Corrupted INDX record\n";
380 }
381 0 ▶ unless (substr($$data, $op1, 4) eq 'ORDT') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($$data, $op1, 4) eq "ORDT") 0 0 0
382 0 die "Corrupted INDX record\n";
383 }
384 0 ▶ unless (substr($$data, $op2, 4) eq 'ORDT') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($$data, $op2, 4) eq "ORDT") 0 0 0
385 0 die "Corrupted INDX record\n";
386 }
387  
388 $ordt1 = [
389 0 unpack("C$oentries", substr $$data, $op1 + 4, $oentries)
390 ];
391 0 $ordt2 = [
392 unpack("n$oentries", substr $$data, $op2 + 4, $oentries * 2)
393 ];
394  
395 }
396  
397 40 return ( $header, $ordt1, $ordt2 );
398  
399 }
400  
401 sub _read_ctoc {
402  
403 10 ▶ my $self = shift;
Subroutine _read_ctoc: called CC=3 SCAR=11.0
404 10 my $data = shift;
405  
406 10 my $ctoc = {};
407  
408 10 my $off = 0;
409 10 my $len = length $$data;
410  
411 10 ▶ while ($off < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 40 0 40
412 40 ▶ if (substr($$data, $off, 1) eq "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$data, $off, 1) eq "\0") 10 30 40
413 10 last;
414 }
415  
416 30 my $idxoff = $off;
417  
418 30 my ($pos, $ilen) = _vwv($data, $off);
419 30 $off += $pos;
420  
421 30 my $name = substr $$data, $off, $ilen;
422 30 $off += $ilen;
423  
424 30 my $ctoc->{ $idxoff } = $name;
425  
426 }
427  
428 10 return $ctoc;
429  
430 }
431  
432 sub _vwv {
433  
434 560 ▶ my $data = shift;
Subroutine _vwv: called CC=3 SCAR=11.0
435 560 my $off = shift;
436  
437 560 my $value = 0;
438 560 my $consume = 0;
439 560 my $fin = 0;
440  
441 560 ▶ while (!$fin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fin) 810 560 1370
442 810 my $v = substr $$data, $off + $consume, 1;
443 810 $consume++;
444 810 ▶ if (ord($v) & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $v & 128) 560 250 810
445 560 $fin = 1;
446 }
447 810 $value = ($value << 7) | (ord($v) & 0x7f);
448 }
449  
450 560 return ( $consume, $value );
451  
452 }
453  
454 sub _read_tag_section {
455  
456 20 ▶ my $start = shift;
Subroutine _read_tag_section: called CC=3 SCAR=11.0
457 20 my $data = shift;
458  
459 20 my $ctrlcount = 0;
460  
461 20 my $tags = [];
462  
463 20 ▶ if (substr($$data, $start, 4) eq 'TAGX') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$data, $start, 4) eq "TAGX") 20 0 20
464 20 my $foff = unpack "N", substr $$data, $start + 4, 4;
465 20 $ctrlcount = unpack "N", substr $$data, $start + 8, 4;
466 20 ▶ for (my $i = 12; $i < $foff; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $foff) 80 20 100
467 80 my $pos = $start + $i;
468 80 push @$tags, [ unpack "C4", substr $$data, $pos, 4 ];
469 }
470 }
471  
472 20 return ( $ctrlcount, $tags );
473  
474 }
475  
476 sub _count_setbits {
477  
478 280 ▶ my $val = shift;
Subroutine _count_setbits: called CC=4 SCAR=13.9
479 280 ▶ my $bits = shift // 8;
Condition50%1/2
shift() // 8
l!l
0280
MC/DC0%0/1
shift() // 8
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
480  
481 280 my $count = 0;
482 280 for my $i (0 .. $bits - 1) {
483 2240 ▶ if (($val & 0x01) == 0x01) {
Branch: 1 branch
BranchTrueFalseTotal
if (($val & 1) == 1) 280 1960 2240
484 280 $count++;
485 }
486 2240 $val >>= 1;
487 }
488  
489 280 return $count;
490  
491 }
492  
493 sub _get_tagmap {
494  
495 100 ▶ my $ctrlcount = shift;
Subroutine _get_tagmap: called CC=12 SCAR=26.2
496 100 my $tagtbl = shift;
497 100 my $entry = shift;
498 100 my $spos = shift;
499 100 my $epos = shift;
500  
501 100 my $tags = [];
502 100 my $tagmap = {};
503 100 my $ctrli = 0;
504 100 my $start = $spos + $ctrlcount;
505  
506 100 for my $t (@$tagtbl) {
507 440 my ($tag, $values, $mask, $endflag) = @$t;
508 440 ▶ if ($endflag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endflag == 1) 100 340 440
509 100 $ctrli++;
510 100 next;
511 }
512 340 my $cbyte = ord(substr $$entry, $spos + $ctrli, 1);
513 340 my $val = $cbyte & $mask;
514 340 ▶ if ($val != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val != 0) 340 0 340
515 340 ▶ if ($val == $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val == $mask) { } 280 60 340
516 280 ▶ if (_count_setbits($mask) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_count_setbits($mask) > 1) { } 0 280 280
517 0 my ($consume, $val) = _vwv($entry, $start);
518 0 $start += $consume;
519 0 push @$tags, [ $tag, undef, $val, $values ];
520 } else {
521 280 push @$tags, [ $tag, 1, undef, $values ];
522 }
523 } else {
524 60 ▶ while (($mask & 0x01) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mask & 1) == 0) 60 60 120
525 60 $mask >>= 1;
526 60 $val >>= 1;
527 }
528 60 push @$tags, [ $tag, $val, undef, $values ];
529 }
530 }
531 }
532  
533 100 for my $t (@$tags) {
534 340 my ($tag, $count, $bytes, $per_entry) = @$t;
535 340 my $values = [];
536 340 ▶ if (defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count) { } 340 0 340
537 340 for my $i (1 .. $count) {
538 400 for my $j (1 .. $per_entry) {
539 530 my ($consume, $data) = _vwv($entry, $start);
540 530 $start += $consume;
541 530 push @$values, $data;
542 }
543 }
544 } else {
545 0 my $constotal = 0;
546 0 ▶ while ($constotal < $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constotal < $bytes) 0 0 0
547 0 my ($consume, $data) = _vwv($entry, $start);
548 0 $start += $consume;
549 0 push @$values, $data;
550 }
551 # Should we warn if $constotal does not match $bytes?
552 }
553 340 $tagmap->{ $tag } = $values;
554 }
555  
556 100 return $tagmap;
557  
558 }
559  
560 sub _kf8_init {
561  
562 10 ▶ my $self = shift;
Subroutine _kf8_init: called CC=11 SCAR=29.2
563  
564 10 ▶ if ($self->{_fdst} != $NULL_INDEX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fdst"} != $NULL_INDEX) 10 0 10
565 10 my $hdr = $self->{_pdb}->record($self->{_fdst})->data;
566 10 ▶ unless (substr($hdr, 0, 4) eq 'FDST') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($hdr, 0, 4) eq "FDST") 0 10 10
567 0 die "KF8 Mobi missing FDST info\n";
568 }
569 10 my $secnum = unpack "N", substr $hdr, 0x08, 4;
570 10 my $sc2 = $secnum * 2;
571 10 my @secs = unpack "N$sc2", substr $hdr, 12, 4 * $sc2;
572 $self->{_fdsttbl} = [
573 10 map({ $secs[$_] } grep { $_ % 2 == 0 } 0 .. $#secs)
574 ];
575 10 push @{ $self->{_fdsttbl} }, $self->{_textlen};
576 }
577  
578 10 ▶ if ($self->{_skelidx} != $NULL_INDEX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_skelidx"} != $NULL_INDEX) 10 0 10
579 10 my ($outtbl, $ctoc) = $self->_get_index_data($self->{_skelidx});
580 10 my $fptr = 0;
581 10 for my $o (@$outtbl) {
582 30 my ($txt, $tagmap) = @$o;
583 30 push @{ $self->{_skeltbl} }, [
584 30 $fptr, $txt, $tagmap->{1}[0], $tagmap->{6}[0], $tagmap->{6}[1]
585 ];
586 30 $fptr++;
587 }
588 }
589  
590 # TODO: The $cdat is usually undef. Not too important as we don't use it
591 # for anything at the moment.
592 10 ▶ if ($self->{_fragidx} != $NULL_INDEX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fragidx"} != $NULL_INDEX) 10 0 10
593 10 my ($outtbl, $ctoc) = $self->_get_index_data($self->{_fragidx});
594 10 for my $o (@$outtbl) {
595 70 my ($txt, $tagmap) = @$o;
596 70 my $coff = $tagmap->{2}[0];
597 70 my $cdat = $ctoc->{ $coff };
598 70 push @{ $self->{_fragtbl} }, [
599 int($txt), $cdat, $tagmap->{3}[0], $tagmap->{4}[0],
600 70 $tagmap->{6}[0], $tagmap->{6}[1]
601 ];
602 }
603 }
604  
605 10 ▶ if ($self->{_guideidx} != $NULL_INDEX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_guideidx"} != $NULL_INDEX) 0 10 10
606 0 my ($outtbl, $ctoc) = $self->_get_index_data($self->{_guideidx});
607 0 for my $o (@$outtbl) {
608 0 my ($txt, $tagmap) = @$o;
609 0 my $coff = $tagmap->{1}[0];
610 0 my $rtitle = $ctoc->{ $coff };
611 0 my $rtype = $txt;
612 0 my $fno;
613 0 ▶ if (exists $tagmap->{3}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tagmap->{3}) 0 0 0
614 0 $fno = $tagmap->{3}[0];
615 }
616 0 ▶ if (exists $tagmap->{6}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tagmap->{6}) 0 0 0
617 0 $fno = $tagmap->{6}[0];
618 }
619 0 push @{ $self->{_guidetbl} }, [ $rtype, $rtitle, $fno ];
620 }
621 }
622  
623 10 return 1;
624  
625 }
626  
627 sub _kf8_xhtml {
628  
629 5 ▶ my $self = shift;
Subroutine _kf8_xhtml: called CC=3 SCAR=11.0
630  
631 5 my @parts;
632  
633 5 my $rawml = $self->rawml;
634  
635 # xhtml is the first flow piece
636 my $source = substr(
637 $rawml,
638 $self->{_fdsttbl}[0],
639 5 $self->{_fdsttbl}[1] - $self->{_fdsttbl}[0]
640 );
641  
642 5 my $fragptr = 0;
643 5 my $baseptr = 0;
644  
645 5 for my $s (@{ $self->{_skeltbl} }) {
646 my (
647 15 $skelnum,
648 $skelnam,
649 $fragcnt,
650 $skelpos,
651 $skellen
652 ) = @$s;
653 15 my $baseptr = $skelpos + $skellen;
654 15 my $skeleton = substr $source, $skelpos, $skellen;
655 15 for my $i (0 .. $fragcnt - 1) {
656 my (
657 $inpos,
658 $idtxt,
659 $fnum,
660 $seqnum,
661 $spos,
662 $len
663 35 ) = @{ $self->{_fragtbl}[$fragptr] };
664 35 my $slice = substr $source, $baseptr, $len;
665 35 $inpos -= $skelpos;
666 35 my $head = substr $skeleton, 0, $inpos;
667 35 my $tail = substr $skeleton, $inpos;
668 35 $skeleton = $head . $slice . $tail;
669 35 $baseptr += $len;
670 35 $fragptr++;
671 }
672 15 push @parts, $skeleton;
673 }
674  
675 5 return @parts;
676  
677 }
678  
679 sub _decode_record {
680  
681 138 ▶ my $self = shift;
Subroutine _decode_record: called CC=4 SCAR=15.8
682 138 my $rec = shift;
683  
684 138 $rec++;
685  
686 138 my $encode = $self->{_pdb}->record($rec)->data;
687 138 my $trail = $self->_trailing_entries_size($encode);
688 138 substr $encode, -$trail, $trail, '';
689  
690 138 ▶ if ($self->{_compression} == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'_compression'} == 1) { } 0 138 138
elsif ($self->{'_compression'} == 2) { } 138 0 138
elsif ($self->{'_compression'} == 17480) { } 0 0 0
691 0 return $encode;
692 } elsif ($self->{_compression} == 2) {
693 138 return palmdoc_decode($encode);
694 } elsif ($self->{_compression} == 17480) {
695 0 return $self->{_huff}->decode($encode);
696 }
697  
698 }
699  
700 # TODO: Could probably optimize this.
701 sub _read_exth {
702  
703 20 ▶ my $self = shift;
Subroutine _read_exth: called CC=3 SCAR=11.2
704 20 my $exth = shift;
705  
706 my %exth_records = (
707 20 ▶ 100 => sub { $self->{Metadata}->add_author(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
708 0 ▶ 101 => sub { $self->{Metadata}->add_contributor(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
709 0 ▶ 103 => sub { $self->{Metadata}->set_description(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
710 0 ▶ 104 => sub { $self->{Metadata}->set_id(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
711 0 ▶ 105 => sub { $self->{Metadata}->add_genre(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
712 20 ▶ 106 => sub { $self->{Metadata}->set_created(eval { guess_time(shift) }) },
Subroutine __ANON__: called CC=1 SCAR=0.0
713 20 ▶ 108 => sub { $self->{Metadata}->add_contributor(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
714 0 ▶ 114 => sub { $self->{Metadata}->set_format('MOBI ' . shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
715 201 => sub {
716 20 ▶ if (defined $self->{_imgrec}) {
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_imgrec"}) 20 0 20
717 20 $self->{_coverrec} = $self->{_imgrec} + unpack "N", shift;
718 }
719 },
720 20 ▶ 524 => sub { $self->{Metadata}->add_language(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
721 20 );
722  
723 20 my ($doctype, $len, $items) = unpack "a4 N N", $exth;
724  
725 20 my $pos = 12;
726  
727 20 for my $i (1 .. $items) {
728  
729 390 my (undef, $size) = unpack "N N", substr $exth, $pos;
730 390 my $contlen = $size - 8;
731 390 my ($id, undef, $content) = unpack "N N a$contlen", substr $exth, $pos;
732  
733 390 ▶ if (exists $exth_records{ $id }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $exth_records{$id}) 100 290 390
734 100 $exth_records{ $id }->($content);
735 }
736  
737 390 $pos += $size;
738  
739 }
740  
741 20 return 1;
742  
743 }
744  
745 sub new {
746  
747 20 ▶ my $class = shift;
Subroutine new: called CC=29 SCAR=42.2
Pod undocumented
748 20 my $file = shift;
749 20 my $enc = shift;
750 20 ▶ my $net = shift // 1;
Condition50%1/2
shift() // 1
l!l
200
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
751  
752 20 my $self = {
753 Source => undef,
754 Metadata => EBook::Ishmael::EBook::Metadata->new,
755 Network => $net,
756 _pdb => undef,
757 _compression => undef,
758 _textlen => undef,
759 _recnum => undef,
760 _recsize => undef,
761 _encryption => undef,
762 _doctype => undef,
763 _length => undef,
764 _type => undef,
765 _codepage => undef,
766 _uid => undef,
767 _version => undef,
768 _exth_flag => undef,
769 _extra_data => undef,
770 _trailers => 0,
771 _huff => undef,
772 _imgrec => undef,
773 _coverrec => undef,
774 _lastcont => undef,
775 _images => [],
776 # kf8 stuff
777 _skelidx => undef,
778 _skeltbl => [],
779 _fragidx => undef,
780 _fragtbl => [],
781 _guideidx => undef,
782 _guidetbl => [],
783 _fdst => undef,
784 _fdsttbl => [ 0, $NULL_INDEX ],
785 };
786  
787 20 bless $self, $class;
788  
789 20 $self->{Source} = File::Spec->rel2abs($file);
790  
791 20 $self->{_pdb} = EBook::Ishmael::PDB->new($file);
792  
793 20 my $hdr = $self->{_pdb}->record(0)->data;
794  
795 (
796 $self->{_compression},
797 undef,
798 $self->{_textlen},
799 $self->{_recnum},
800 $self->{_recsize},
801 $self->{_encryption},
802 undef,
803 20 ) = unpack "n n N n n n n", $hdr;
804  
805 20 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_compression"} == 1 or $self->{"_compression"} == 2 or $self->{"_compression"} == 17480) 0 20 20
Condition33%2/6
$self->{"_compression"} == 1 or $self->{"_compression"} == 2 or $self->{"_compression"} == 17480
l!l&&r!l&&!r
2000
$self->{"_compression"} == 1 or $self->{"_compression"} == 2
l!l&&r!l&&!r
0200
MC/DC0%0/3
$self->{"_compression"} == 1 or $self->{"_compression"} == 2 or $self->{"_compression"} == 17480
$self->{"_compression"} == 1 $self->{"_compression"} == 2 $self->{"_compression"} == 17480
Truth table25%1/4
A or B or C
A= $self->{"_compression"} == 1B= $self->{"_compression"} == 2C= $self->{"_compression"} == 17480
ABCresult
1XX1
01X1
0011
0000
806 $self->{_compression} == 1 or
807 $self->{_compression} == 2 or
808 $self->{_compression} == 17480
809 ) {
810 0 die "Mobi $self->{Source} uses an unsupported compression level\n";
811 }
812  
813 20 ▶ if ($self->{_recsize} != 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_recsize"} != 4096) 0 20 20
814 0 die "$self->{Source} is not a Mobi file\n";
815 }
816  
817 20 ▶ unless ($self->{_encryption} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_encryption"} == 0) 0 20 20
818 0 die "Cannot read encrypted Mobi $self->{Source}\n";
819 }
820  
821 (
822 $self->{_doctype},
823 $self->{_length},
824 $self->{_type},
825 $self->{_codepage},
826 $self->{_uid},
827 $self->{_version},
828 20 ) = unpack "a4 N N N N N", substr $hdr, 16, 4 * 6;
829  
830 20 ▶ unless ($self->{_codepage} == 1252 or $self->{_codepage} == 65001) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_codepage"} == 1252 or $self->{"_codepage"} == 65001) 0 20 20
Condition33%1/3
$self->{"_codepage"} == 1252 or $self->{"_codepage"} == 65001
l!l&&r!l&&!r
0200
MC/DC0%0/2
$self->{"_codepage"} == 1252 or $self->{"_codepage"} == 65001
$self->{"_codepage"} == 1252 $self->{"_codepage"} == 65001
Truth table33%1/3
A or B
A= $self->{"_codepage"} == 1252B= $self->{"_codepage"} == 65001
ABresult
1X1
011
000
831 0 die "Mobi $self->{Source} uses an unsupported text encoding\n";
832 }
833  
834 # Read some parts of the Mobi header that we care about.
835 20 my ($toff, $tlen) = unpack "N N", substr $hdr, 0x54, 8;
836 20 $self->{_imgrec} = unpack "N", substr $hdr, 0x6c, 4;
837 20 my ($hoff, $hcount) = unpack "N N", substr $hdr, 0x70, 8;
838 20 $self->{_exth_flag} = unpack "N", substr $hdr, 0x80, 4;
839 20 $self->{_lastcont} = unpack "n", substr $hdr, 0xc2, 2;
840 20 $self->{_extra_data} = unpack "n", substr $hdr, 0xf2, 2;
841  
842 20 ▶ if ($self->{_compression} == 17480) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_compression"} == 17480) 0 20 20
843  
844 0 ▶ unless ($UNPACK_Q) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($UNPACK_Q) 0 0 0
845 0 die "Cannot read AZW $self->{Source}; perl does not support " .
846 "unpacking 64-bit integars\n";
847 }
848  
849 0 my @huffs = map { $self->{_pdb}->record($_)->data } ($hoff .. $hoff + $hcount - 1);
850 0 $self->{_huff} = EBook::Ishmael::EBook::Mobi::MobiHuff->new(@huffs);
851 }
852  
853 20 ▶ if ($self->{_length} >= 0xe3 and $self->{_version} >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_length"} >= 227 and $self->{"_version"} >= 5) 20 0 20
Condition33%1/3
$self->{"_length"} >= 227 and $self->{"_version"} >= 5
!ll&&!rl&&r
0020
MC/DC0%0/2
$self->{"_length"} >= 227 and $self->{"_version"} >= 5
$self->{"_length"} >= 227 $self->{"_version"} >= 5
Truth table33%1/3
A and B
A= $self->{"_length"} >= 227B= $self->{"_version"} >= 5
ABresult
0X0
100
111
854 20 my $flags = $self->{_extra_data};
855 20 ▶ while ($flags > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags > 1) 0 20 20
856 0 ▶ $self->{_trailers}++ if $flags & 2;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 0 0 0
857 0 $flags >>= 1;
858 }
859 }
860  
861 20 ▶ if ($self->{_version} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_version"} == 8) 10 10 20
862 10 $self->{_fdst} = unpack "N", substr $hdr, 0xc0, 4;
863 10 $self->{_fragidx} = unpack "N", substr $hdr, 0xf8, 4;
864 10 $self->{_skelidx} = unpack "N", substr $hdr, 0xfc, 4;
865 10 $self->{_guideidx} = unpack "N", substr $hdr, 0x104, 4;
866 10 $self->_kf8_init;
867 }
868  
869 20 ▶ if ($self->{_lastcont} > $self->{_pdb}->recnum - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_lastcont"} > $self->{"_pdb"}->recnum - 1) 0 20 20
870 0 $self->{_lastcont} = $self->{_pdb}->recnum - 1;
871 }
872  
873 20 ▶ if ($self->{_imgrec} >= $self->{_lastcont}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_imgrec"} >= $self->{"_lastcont"}) 0 20 20
874 0 undef $self->{_imgrec};
875 }
876  
877 20 ▶ if (defined $self->{_imgrec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_imgrec"}) 20 0 20
878 20 for my $i ($self->{_imgrec} .. $self->{_lastcont}) {
879 50 my $img = $self->{_pdb}->record($i)->data;
880 50 my $format = image_id($img);
881 50 ▶ next if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 10 40 50
882 40 push @{ $self->{_images} }, [ $i, $format ];
883 }
884 }
885  
886 20 ▶ if ($self->{_exth_flag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_exth_flag"}) 20 0 20
887 20 $self->_read_exth(substr $hdr, $self->{_length} + 16);
888 }
889  
890 20 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_coverrec"} and not grep({$self->{"_coverrec"} == $_->[0];} @{$$self{"_images"};})) 0 20 20
Condition33%1/3
defined $self->{"_coverrec"} and not grep({$self->{"_coverrec"} == $_->[0];} @{$$self{"_images"};})
!ll&&!rl&&r
0200
MC/DC0%0/2
defined $self->{"_coverrec"} and not grep({$self->{"_coverrec"} == $_->[0];} @{$$self{"_images"};})
defined $self->{"_coverrec"} not grep({$self->{"_coverrec"} == $_->[0];} @{$$self{"_images"};})
Truth table33%1/3
A and not(B)
A= defined $self->{"_coverrec"}B= not grep({$self->{"_coverrec"} == $_->[0];} @{$$self{"_images"};})
ABresult
0X0
100
111
891 defined $self->{_coverrec} and
892 40 not grep { $self->{_coverrec} == $_->[0] } @{ $self->{_images} }
893 ) {
894 0 undef $self->{_coverrec};
895 }
896  
897 20 $self->{Metadata}->set_title(substr $hdr, $toff, $tlen);
898  
899 20 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"Metadata"}->created or $self->{"_pdb"}->cdate > $self->{"Metadata"}->created) 20 0 20
Condition33%1/3
not defined $self->{"Metadata"}->created or $self->{"_pdb"}->cdate > $self->{"Metadata"}->created
l!l&&r!l&&!r
0200
MC/DC0%0/2
not defined $self->{"Metadata"}->created or $self->{"_pdb"}->cdate > $self->{"Metadata"}->created
not defined $self->{"Metadata"}->created $self->{"_pdb"}->cdate > $self->{"Metadata"}->created
Truth table33%1/3
not(A) or B
A= not defined $self->{"Metadata"}->createdB= $self->{"_pdb"}->cdate > $self->{"Metadata"}->created
ABresult
1X1
011
000
900 not defined $self->{Metadata}->created or
901 # If the PDB's created date is greater than the MOBI's EXTH one,
902 # probably means a corrupted EXTH date.
903 $self->{_pdb}->cdate > $self->{Metadata}->created
904 ) {
905 20 $self->{Metadata}->set_created($self->{_pdb}->cdate);
906 }
907  
908 20 ▶ if ($self->{_pdb}->mdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pdb"}->mdate) 20 0 20
909 20 $self->{Metadata}->set_modified($self->{_pdb}->mdate);
910 }
911  
912 20 ▶ if ($self->{_version} == 8) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_version'} == 8) { } 10 10 20
elsif (not defined $self->{'Metadata'}->format) { } 10 0 10
913 10 $self->{Metadata}->set_format('KF8');
914 } elsif (not defined $self->{Metadata}->format) {
915 10 $self->{Metadata}->set_format('MOBI');
916 }
917  
918 20 return $self;
919  
920 }
921  
922 sub rawml {
923  
924 10 ▶ my $self = shift;
Subroutine rawml: called CC=7 SCAR=21.3
Pod undocumented
925 10 my %param = @_;
926  
927 10 ▶ my $decode = $param{decode} // 0;
Condition50%1/2
$param{"decode"} // 0
l!l
010
MC/DC0%0/1
$param{"decode"} // 0
$param{"decode"}
Truth table50%1/2
A
A= $param{"decode"}
Aresult
11
00
928 10 ▶ my $clean = $param{clean} // 0;
Condition100%2/2
$param{"clean"} // 0
l!l
55
MC/DC100%1/1
$param{"clean"} // 0
$param{"clean"}
Truth table100%2/2
A
A= $param{"clean"}
Aresult
11
00
929  
930 my $cont =
931 join '',
932 115 map { $self->_decode_record($_) }
933 10 0 .. $self->{_recnum} - 1;
934  
935 10 ▶ _clean_html(\$cont) if $clean;
Branch: 1 branch
BranchTrueFalseTotal
if $clean 5 5 10
936  
937 10 ▶ if ($decode and $self->{_codepage} == 1252) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode and $self->{"_codepage"} == 1252) 0 10 10
Condition33%1/3
$decode and $self->{"_codepage"} == 1252
!ll&&!rl&&r
1000
MC/DC0%0/2
$decode and $self->{"_codepage"} == 1252
$decode $self->{"_codepage"} == 1252
Truth table33%1/3
A and B
A= $decodeB= $self->{"_codepage"} == 1252
ABresult
0X0
100
111
938 0 ▶ from_to($cont, "cp1252", "utf-8")
Branch: 1 branch
BranchTrueFalseTotal
&from_to($cont, "cp1252", "utf-8") or die qq[Failed to encode Mobi $self->{"Source"} text as utf-8\n] 0 0 0
939 or die "Failed to encode Mobi $self->{Source} text as utf-8\n";
940 }
941  
942 10 return $cont;
943  
944 }
945  
946 sub html {
947  
948 6 ▶ my $self = shift;
Subroutine html: called CC=7 SCAR=21.8
Pod undocumented
949 6 my $out = shift;
950  
951 6 my $html;
952  
953 6 ▶ if ($self->{_version} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_version'} == 8) { } 3 3 6
954  
955 3 for my $part ($self->_kf8_xhtml) {
956  
957 my $dom = XML::LibXML->load_html(
958 string => $part,
959 no_network => !$self->{Network},
960 9 recover => 2,
961 );
962  
963 9 ▶ my ($body) = $dom->findnodes('/html/body') or next;
Branch: 1 branch
BranchTrueFalseTotal
my($body) = $dom->findnodes("/html/body") or next 0 9 9
964 9 prepare_html($body);
965  
966 9 $html .= join '', map { $_->toString } $body->childNodes;
967  
968 }
969  
970 } else {
971  
972 3 my $rawml = $self->rawml(clean => 1);
973 3 ▶ my $enc = $self->{_codepage} == 1252 ? "cp1252" : "utf-8";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_codepage'} == 1252 ? : 0 3 3
974 my $dom = XML::LibXML->load_html(
975 string => $rawml,
976 no_network => !$self->{Network},
977 3 encoding => $enc,
978 recover => 2
979 );
980 3 prepare_html($dom);
981 3 $html = $dom->documentElement->toString;
982 }
983  
984 6 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 6 6
985 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
986 or die "Failed to open $out for writing: $!\n";
987 0 binmode $fh, ':utf8';
988 0 print { $fh } $html;
989 0 close $fh;
990 0 return $out;
991 } else {
992 6 return $html;
993 }
994  
995 }
996  
997 sub raw {
998  
999 4 ▶ my $self = shift;
Subroutine raw: called CC=7 SCAR=22.0
Pod undocumented
1000 4 my $out = shift;
1001  
1002 4 my $raw;
1003  
1004 4 ▶ if ($self->{_version} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_version'} == 8) { } 2 2 4
1005  
1006 2 for my $part ($self->_kf8_xhtml) {
1007 my $dom = XML::LibXML->load_html(
1008 string => $part,
1009 no_network => !$self->{Network},
1010 6 recover => 2,
1011 );
1012 6 ▶ my ($body) = $dom->findnodes('/html/body') or next;
Branch: 1 branch
BranchTrueFalseTotal
my($body) = $dom->findnodes("/html/body") or next 0 6 6
1013 6 prepare_html($body);
1014 6 $raw .= $body->textContent;
1015 }
1016  
1017 } else {
1018  
1019 2 my $rawml = $self->rawml(clean => 1);
1020 2 ▶ my $enc = $self->{_codepage} == 1252 ? "cp1252" : "utf-8";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_codepage'} == 1252 ? : 0 2 2
1021 my $dom = XML::LibXML->load_html(
1022 string => $rawml,
1023 no_network => !$self->{Network},
1024 2 encoding => $enc,
1025 recover => 2,
1026 );
1027 2 prepare_html($dom);
1028 2 $raw = $dom->documentElement->textContent;
1029  
1030 }
1031  
1032 4 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 4 4
1033 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
1034 or die "Failed to open $out for writing: $!\n";
1035 0 binmode $fh, ':utf8';
1036 0 print { $fh } $raw;
1037 0 close $fh;
1038 0 return $out;
1039 } else {
1040 4 return $raw;
1041 }
1042  
1043 }
1044  
1045 sub metadata {
1046  
1047 8 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
1048  
1049 8 return $self->{Metadata};
1050  
1051 }
1052  
1053 sub has_cover {
1054  
1055 8 ▶ my $self = shift;
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
1056  
1057 8 return defined $self->{_coverrec};
1058  
1059 }
1060  
1061 sub cover {
1062  
1063 4 ▶ my $self = shift;
Subroutine cover: called CC=3 SCAR=11.2
Pod undocumented
1064 4 my $out = shift;
1065  
1066 4 ▶ return (undef, undef) unless $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 4 4
1067  
1068 4 my $bin = $self->{_pdb}->record($self->{_coverrec})->data;
1069 4 my $format = image_id($bin);
1070 4 ▶ return (undef, undef) if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 4 4
1071  
1072 4 return ($bin, $format);
1073  
1074 }
1075  
1076 sub image_num {
1077  
1078 12 ▶ my $self = shift;
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
1079  
1080 12 return scalar @{ $self->{_images} };
1081  
1082 }
1083  
1084 sub image {
1085  
1086 8 ▶ my $self = shift;
Subroutine image: called CC=2 SCAR=7.2
Pod undocumented
1087 8 my $n = shift;
1088  
1089 8 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 8 8
1090 0 return (undef, undef);
1091 }
1092  
1093 8 my $img = $self->{_pdb}->record($self->{_images}->[$n][0])->data;
1094  
1095 8 return ($img, $self->{_images}[$n][1]);
1096  
1097 }
1098  
1099 17 1;
« blib/lib/EBook/Ishmael/Unzip.pm ↑ Summary blib/lib/EBook/Ishmael/ImageID.pm »