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

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