| 1 | package EBook::Ishmael::EBook::zTXT; | ||||||||||||
| 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(decode); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 17 | ▶ | use EBook::Ishmael::CharDet; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 17 | ▶ | use EBook::Ishmael::EBook::Metadata; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 17 | ▶ | use EBook::Ishmael::PDB; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 17 | ▶ | use EBook::Ishmael::HTML qw(text2html); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 17 | my $TYPE = 'zTXT'; | |||||||||||
| 15 | |||||||||||||
| 16 | # Minimum version we support is 1.40. Pre-1.40 is de-standardized. | ||||||||||||
| 17 | 17 | my $MINVER = 0x0128; | |||||||||||
| 18 | |||||||||||||
| 19 | 17 | my $RANDOM_ACCESS = 0x1; | |||||||||||
| 20 | |||||||||||||
| 21 | # Checks to see if the PDB type is 'zTXT'. The creator is ignored, as that can | ||||||||||||
| 22 | # differ between zTXT creators. | ||||||||||||
| 23 | sub heuristic { | ||||||||||||
| 24 | |||||||||||||
| 25 | 24 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
heuristic: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 26 | 24 | my $file = shift; | |||||||||||
| 27 | 24 | my $fh = shift; | |||||||||||
| 28 | |||||||||||||
| 29 | 24 | ▶ | return 0 unless -s $file >= 68; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | 24 | seek $fh, 32, 0; | |||||||||||
| 32 | 24 | read $fh, my ($null), 1; | |||||||||||
| 33 | |||||||||||||
| 34 | # Last byte in title must be null | ||||||||||||
| 35 | 24 | ▶ | unless ($null eq "\0") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 12 | return 0; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 12 | seek $fh, 60, 0; | |||||||||||
| 40 | 12 | read $fh, my ($type), 4; | |||||||||||
| 41 | |||||||||||||
| 42 | 12 | return $type eq $TYPE; | |||||||||||
| 43 | |||||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | # Should be called with record #1 first. | ||||||||||||
| 47 | sub _decode_record { | ||||||||||||
| 48 | |||||||||||||
| 49 | 21 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_decode_record: called CC=2 SCAR=7.1
| |||||||||||||
| 50 | 21 | my $rec = shift; | |||||||||||
| 51 | |||||||||||||
| 52 | 21 | $rec++; | |||||||||||
| 53 | |||||||||||||
| 54 | 21 | my $out = $self->{_inflate}->inflate($self->{_pdb}->record($rec)->data); | |||||||||||
| 55 | |||||||||||||
| 56 | 21 | ▶ | unless (defined $out) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | die "Error decompressing zTXT stream in $self->{Source}\n"; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 21 | return $out; | |||||||||||
| 61 | |||||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | sub _text { | ||||||||||||
| 65 | |||||||||||||
| 66 | 7 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_text: called CC=2 SCAR=7.0
| |||||||||||||
| 67 | |||||||||||||
| 68 | 7 | require Compress::Zlib; | |||||||||||
| 69 | |||||||||||||
| 70 | 7 | ▶ | $self->{_inflate} = Compress::Zlib::inflateInit(-WindowBits => 15) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | or die "Failed to initialize zlib inflator\n"; | ||||||||||||
| 72 | |||||||||||||
| 73 | 7 | my $text = join('', map { $self->_decode_record($_) } 0 .. $self->{_recnum} - 1); | |||||||||||
| 74 | |||||||||||||
| 75 | 7 | $self->{_inflate} = undef; | |||||||||||
| 76 | |||||||||||||
| 77 | 7 | return $text; | |||||||||||
| 78 | |||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | sub new { | ||||||||||||
| 82 | |||||||||||||
| 83 | 11 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||
| 84 | 11 | my $file = shift; | |||||||||||
| 85 | 11 | my $enc = shift; | |||||||||||
| 86 | |||||||||||||
| 87 | 11 | my $self = { | |||||||||||
| 88 | Source => undef, | ||||||||||||
| 89 | Metadata => EBook::Ishmael::EBook::Metadata->new, | ||||||||||||
| 90 | Encoding => $enc, | ||||||||||||
| 91 | _pdb => undef, | ||||||||||||
| 92 | _version => undef, | ||||||||||||
| 93 | _recnum => undef, | ||||||||||||
| 94 | _size => undef, | ||||||||||||
| 95 | _recsize => undef, | ||||||||||||
| 96 | _bookmarknum => undef, | ||||||||||||
| 97 | _bookmarkrec => undef, | ||||||||||||
| 98 | _annotnum => undef, | ||||||||||||
| 99 | _annotrec => undef, | ||||||||||||
| 100 | _flags => undef, | ||||||||||||
| 101 | _reserved => undef, | ||||||||||||
| 102 | _crc32 => undef, | ||||||||||||
| 103 | }; | ||||||||||||
| 104 | |||||||||||||
| 105 | 11 | bless $self, $class; | |||||||||||
| 106 | |||||||||||||
| 107 | 11 | $self->{Source} = File::Spec->rel2abs($file); | |||||||||||
| 108 | |||||||||||||
| 109 | 11 | $self->{_pdb} = EBook::Ishmael::PDB->new($file); | |||||||||||
| 110 | |||||||||||||
| 111 | 11 | my $hdr = $self->{_pdb}->record(0)->data; | |||||||||||
| 112 | |||||||||||||
| 113 | ( | ||||||||||||
| 114 | $self->{_version}, | ||||||||||||
| 115 | $self->{_recnum}, | ||||||||||||
| 116 | $self->{_size}, | ||||||||||||
| 117 | $self->{_recsize}, | ||||||||||||
| 118 | $self->{_bookmarknum}, | ||||||||||||
| 119 | $self->{_bookmarkrec}, | ||||||||||||
| 120 | $self->{_annotnum}, | ||||||||||||
| 121 | $self->{_annotrec}, | ||||||||||||
| 122 | $self->{_flags}, | ||||||||||||
| 123 | $self->{_reserved}, | ||||||||||||
| 124 | $self->{_crc32}, | ||||||||||||
| 125 | undef | ||||||||||||
| 126 | 11 | ) = unpack "n n N n n n n n C C N C4", $hdr; | |||||||||||
| 127 | |||||||||||||
| 128 | 11 | ▶ | if ($self->{_version} < $MINVER) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | die sprintf | ||||||||||||
| 130 | "%s zTXT is of an unsupported version, %d.%d (%d.%d and above are supported).\n", | ||||||||||||
| 131 | $self->{Source}, | ||||||||||||
| 132 | 0 | ($self->{_version} >> 8) & 0xff, $self->{_version} & 0xff, | |||||||||||
| 133 | ($MINVER >> 8) & 0xff, $MINVER & 0xff; | ||||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 11 | ▶ | unless ($self->{_flags} & $RANDOM_ACCESS) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 0 | die "$self->{Source} zTXT uses unsupported compression method\n"; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | 11 | $self->{Metadata}->set_title($self->{_pdb}->name); | |||||||||||
| 141 | |||||||||||||
| 142 | 11 | ▶ | if ($self->{_pdb}->cdate) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 11 | $self->{Metadata}->set_created($self->{_pdb}->cdate); | |||||||||||
| 144 | } | ||||||||||||
| 145 | 11 | ▶ | if ($self->{_pdb}->mdate) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 11 | $self->{Metadata}->set_modified($self->{_pdb}->mdate); | |||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | $self->{Metadata}->set_format( | ||||||||||||
| 150 | sprintf | ||||||||||||
| 151 | "zTXT %s.%s", | ||||||||||||
| 152 | ($self->{_version} >> 8) & 0xff, | ||||||||||||
| 153 | 11 | $self->{_version} & 0xff | |||||||||||
| 154 | ); | ||||||||||||
| 155 | |||||||||||||
| 156 | 11 | return $self; | |||||||||||
| 157 | |||||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | sub html { | ||||||||||||
| 161 | |||||||||||||
| 162 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
html: called CC=3 SCAR=16.0
Pod
undocumented
| |||||||||||||
| 163 | 3 | my $out = shift; | |||||||||||
| 164 | |||||||||||||
| 165 | 3 | my $html = text2html($self->raw); | |||||||||||
| 166 | |||||||||||||
| 167 | 3 | ▶ | if (defined $out) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 0 | ▶ | open my $fh, '>', $out | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | or die "Failed to open $out for writing: $!\n"; | ||||||||||||
| 170 | 0 | binmode $fh, ':utf8'; | |||||||||||
| 171 | 0 | print { $fh } $html; | |||||||||||
| 172 | 0 | close $fh; | |||||||||||
| 173 | 0 | return $out; | |||||||||||
| 174 | } else { | ||||||||||||
| 175 | 3 | return $html; | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | sub raw { | ||||||||||||
| 181 | |||||||||||||
| 182 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
raw: called CC=5 SCAR=19.9
Pod
undocumented
| |||||||||||||
| 183 | 6 | my $out = shift; | |||||||||||
| 184 | |||||||||||||
| 185 | 6 | my $raw = $self->_text; | |||||||||||
| 186 | 6 | ▶ | if (not defined $self->{Encoding}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 187 | 5 | ▶ | $self->{Encoding} = chardet($raw) // 'ASCII'; | ||||||||||
|
Condition50%1/2
chardet($raw) // "ASCII"
MC/DC0%0/1
chardet($raw) // "ASCII"
chardet($raw)
Truth table50%1/2
A
A= chardet($raw)
| |||||||||||||
| 188 | } | ||||||||||||
| 189 | 6 | $raw = decode($self->{Encoding}, $raw); | |||||||||||
| 190 | |||||||||||||
| 191 | 6 | ▶ | if (defined $out) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 192 | 0 | ▶ | open my $fh, '>', $out | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 193 | or die "Failed to open $out for writing: $!\n"; | ||||||||||||
| 194 | 0 | binmode $fh, ':utf8'; | |||||||||||
| 195 | 0 | print { $fh } $raw; | |||||||||||
| 196 | 0 | close $fh; | |||||||||||
| 197 | 0 | return $out; | |||||||||||
| 198 | } else { | ||||||||||||
| 199 | 6 | return $raw; | |||||||||||
| 200 | } | ||||||||||||
| 201 | |||||||||||||
| 202 | } | ||||||||||||
| 203 | |||||||||||||
| 204 | sub metadata { | ||||||||||||
| 205 | |||||||||||||
| 206 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
metadata: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 207 | |||||||||||||
| 208 | 4 | return $self->{Metadata}; | |||||||||||
| 209 | |||||||||||||
| 210 | } | ||||||||||||
| 211 | |||||||||||||
| 212 | 2 | ▶ | sub has_cover { 0 } | ||||||||||
|
Subroutine
has_cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 213 | |||||||||||||
| 214 | 1 | ▶ | sub cover { (undef, undef) } | ||||||||||
|
Subroutine
cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 215 | |||||||||||||
| 216 | 2 | ▶ | sub image_num { 0 } | ||||||||||
|
Subroutine
image_num: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 217 | |||||||||||||
| 218 | 1 | ▶ | sub image { (undef, undef) } | ||||||||||
|
Subroutine
image: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 219 | |||||||||||||
| 220 | 17 | 1; | |||||||||||