Lite.pm

filter: Statementstmt 81.7% 215 / 263 Branchbran 35.4% 17 / 48 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 89.2% 50 / 56 Podpod 44.4% 12 / 27 totaltotal 73.0% 295 / 404 CC 29 SCAR 15.2
CC
29
Cov
74%
add_image_entry
28.5
add_identifier
6.0
copy_xhtml
6.0
CRAP 4.6
↑ Summary Lite/Container/Zip.pm »
1 # Copyright (c) 2009, 2010 Oleksandr Tymoshenko <gonzo@bluezbox.com>
2 # All rights reserved.
3  
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12  
13 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 # SUCH DAMAGE.
24  
25 package EBook::EPUB::Lite;
26  
27 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 our $VERSION = 0.71;
31  
32 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use Types::Standard qw/ArrayRef HashRef Object Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use EBook::EPUB::Lite::Metadata; # done
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use EBook::EPUB::Lite::Manifest; # done
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use EBook::EPUB::Lite::Guide; # done
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use EBook::EPUB::Lite::Spine; # done
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use EBook::EPUB::Lite::NCX; # done
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 4 ▶use EBook::EPUB::Lite::Container::Zip; # not moose
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 4 ▶use UUID::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 has metadata => (
51 isa => Object,
52 is => 'ro',
53 2 ▶ default => sub { EBook::EPUB::Lite::Metadata->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 4 handles => [ qw/add_contributor
55 add_creator
56 add_coverage
57 add_date
58 add_meta_dcitem
59 add_description
60 add_format
61 add_meta_item
62 add_language
63 add_publisher
64 add_relation
65 add_rights
66 add_source
67 add_subject
68 add_translator
69 add_type
70 /],
71  
72 );
73  
74 has manifest => (
75 isa => Object,
76 is => 'ro',
77 2 ▶ default => sub { EBook::EPUB::Lite::Manifest->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 4 );
79  
80 has spine => (
81 isa => Object,
82 is => 'ro',
83 2 ▶ default => sub { EBook::EPUB::Lite::Spine->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 4 );
85  
86 has guide => (
87 isa => Object,
88 is => 'ro',
89 2 ▶ default => sub { EBook::EPUB::Lite::Guide->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 4 );
91  
92 has ncx => (
93 isa => Object,
94 is => 'ro',
95 2 ▶ default => sub { EBook::EPUB::Lite::NCX->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 4 handles => [ qw/add_navpoint/ ],
97 );
98  
99 4 has _uuid => (
100 isa => Str,
101 is => 'rw',
102 );
103  
104 4 has _encryption_key => (
105 isa => Str,
106 is => 'rw',
107 );
108  
109 # Array of filenames that should be encrypted
110 has _encrypted_filerefs => (
111 is => 'ro',
112 isa => ArrayRef[Object],
113 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 4 );
115  
116 1 ▶sub add_encrypted_fileref {
Subroutine add_encrypted_fileref: called CC=1 SCAR=0.0
Pod undocumented
117 1 my ($self, @args) = @_;
118 1 push @{ shift->_encrypted_filerefs }, @args;
119 }
120  
121 2 ▶sub encrypted_filerefs {
Subroutine encrypted_filerefs: called CC=1 SCAR=0.0
Pod undocumented
122 2 return @{ shift->_encrypted_filerefs };
123 }
124  
125 2 ▶has id_counters => ( isa => HashRef, is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
126  
127 has _temporary_dir_handle => (isa => Object,
128 is => 'ro',
129 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 # defaults to CLEANUP => 1 as per doc
131 2 return File::Temp->newdir;
132 4 });
133  
134 18 ▶sub tmpdir {
Subroutine tmpdir: called CC=1 SCAR=0.0
Pod undocumented
135 # return the path, not the object.
136 18 return shift->_temporary_dir_handle->dirname;
137 }
138  
139 sub BUILD
140 2 ▶{
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
141 2 my ($self) = @_;
142 2 $self->manifest->add_item(
143 id => 'ncx',
144 href => 'toc.ncx',
145 media_type => 'application/x-dtbncx+xml'
146 );
147  
148 2 $self->spine->toc('ncx');
149 2 ▶ mkdir ($self->tmpdir . "/OPS") or die "Can't make OPS dir in " . $self->tmpdir;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->tmpdir . '/OPS' or die q[Can't make OPS dir in ] . $self->tmpdir 2 0 2
150 # Implicitly generate UUID for book
151 2 $self->_set_uuid(UUID::Tiny::uuid_to_string(UUID::Tiny::create_uuid()));
152 }
153  
154 sub to_xml
155 2 ▶{
Subroutine to_xml: called CC=1 SCAR=0.0
Pod undocumented
156 2 my ($self) = @_;
157 2 my $xml;
158  
159 2 my $writer = XML::Writer->new(
160 OUTPUT => \$xml,
161 DATA_MODE => 1,
162 DATA_INDENT => 2,
163 );
164  
165 2 $writer->xmlDecl("utf-8");
166 2 $writer->startTag('package',
167 xmlns => 'http://www.idpf.org/2007/opf',
168 version => '2.0',
169 'unique-identifier' => 'BookId',
170 );
171 2 $self->metadata->encode($writer);
172 2 $self->manifest->encode($writer);
173 2 $self->spine->encode($writer);
174 2 $self->guide->encode($writer);
175 2 $writer->endTag('package');
176 2 $writer->end();
177  
178 2 return $xml;
179 }
180  
181 sub add_author
182 1 ▶{
Subroutine add_author: called CC=1 SCAR=0.0
Pod documented
183 1 my ($self, $author, $formal) = @_;
184 1 $self->metadata->add_author($author, $formal);
185 1 $self->ncx->add_author($author);
186 }
187  
188 sub add_title
189 1 ▶{
Subroutine add_title: called CC=2 SCAR=7.0
Pod documented
190 1 my ($self, $title) = @_;
191 1 $self->metadata->add_title($title);
192 1 my $ncx_title = $self->ncx->title;
193 # Collect all titles in a row for NCX
194 1 ▶ $title = "$ncx_title $title" if (defined($ncx_title));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ncx_title 0 1 1
195 1 $self->ncx->title($title);
196 }
197  
198 sub _set_uuid
199 2 ▶{
Subroutine _set_uuid: called CC=3 SCAR=11.9
200 2 my ($self, $uuid) = @_;
201  
202 # Just some naive check for key to be UUID
203 2 ▶ if ($uuid !~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uuid =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i) 0 2 2
204 0 carp "$uuid - is not valid UUID";
205 0 return;
206 }
207 2 my $key = $uuid;
208  
209 2 $key =~ s/-//g;
210 2 $key =~ s/([a-f0-9]{2})/chr(hex($1))/egi;
211 2 $self->_encryption_key($key);
212 2 ▶ if (defined($self->_uuid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_uuid) 0 2 2
213 0 warn "Overriding existing uuid " . $self->_uuid;
214 0 $self->_uuid($uuid);
215 }
216  
217 2 $self->ncx->uid("urn:uuid:$uuid");
218 2 $self->metadata->set_book_id("urn:uuid:$uuid");
219 2 $self->_uuid($uuid);
220 }
221  
222 sub add_identifier
223 0 ▶{
Subroutine add_identifier: not called CC=2 SCAR=17.9
Pod documented
224 0 my ($self, $ident, $scheme) = @_;
225 0 ▶ if ($ident =~ /^urn:uuid:(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /^urn:uuid:(.*)/i) { } 0 0 0
226 0 my $uuid = $1;
227 0 $self->_set_uuid($uuid);
228 }
229 else {
230 0 $self->metadata->add_identifier($ident, $scheme);
231 }
232 }
233  
234 sub add_xhtml_entry
235 4 ▶{
Subroutine add_xhtml_entry: called CC=3 SCAR=11.4
Pod undocumented
236 4 my ($self, $filename, %opts) = @_;
237 4 my $linear = 1;
238  
239 $linear = 0 if (defined ($opts{'linear'}) &&
240 4 ▶ $opts{'linear'} eq 'no');
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"linear"} and $opts{"linear"} eq "no" 0 4 4
Condition33%1/3
defined $opts{"linear"} and $opts{"linear"} eq "no"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $opts{"linear"} and $opts{"linear"} eq "no"
defined $opts{"linear"} $opts{"linear"} eq "no"
Truth table33%1/3
A and B
A= defined $opts{"linear"}B= $opts{"linear"} eq "no"
ABresult
0X0
100
111
241  
242  
243 4 my $id = $self->nextid('ch');
244 4 $self->manifest->add_item(
245 id => $id,
246 href => $filename,
247 media_type => 'application/xhtml+xml',
248 );
249  
250 4 $self->spine->add_itemref(
251 idref => $id,
252 linear => $linear,
253 );
254  
255 4 return $id;
256 }
257  
258 sub add_stylesheet_entry
259 2 ▶{
Subroutine add_stylesheet_entry: called CC=1 SCAR=0.0
Pod undocumented
260 2 my ($self, $filename) = @_;
261 2 my $id = $self->nextid('css');
262 2 $self->manifest->add_item(
263 id => $id,
264 href => $filename,
265 media_type => 'text/css',
266 );
267  
268 2 return $id;
269 }
270  
271 sub add_image_entry
272 1 ▶{
Subroutine add_image_entry: called CC=7 SCAR=33.5
Pod undocumented
273 1 my ($self, $filename, $type) = @_;
274 # trying to guess
275 1 ▶ if (!defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 1 1
276 0 ▶ if (($filename =~ /\.jpg$/i) || ($filename =~ /\.jpeg$/i)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i) { } 0 0 0
elsif ($filename =~ /\.gif$/i) { } 0 0 0
elsif ($filename =~ /\.png$/i) { } 0 0 0
elsif ($filename =~ /\.svg$/i) { } 0 0 0
Condition0%0/3
$filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i
$filename =~ /\.jpg$/i $filename =~ /\.jpeg$/i
Truth table0%0/3
A or B
A= $filename =~ /\.jpg$/iB= $filename =~ /\.jpeg$/i
ABresult
1X1
011
000
277 0 $type = 'image/jpeg';
278 }
279 elsif ($filename =~ /\.gif$/i) {
280 0 $type = 'image/gif';
281 }
282 elsif ($filename =~ /\.png$/i) {
283 0 $type = 'image/png';
284 }
285 elsif ($filename =~ /\.svg$/i) {
286 0 $type = 'image/svg+xml';
287 }
288 else {
289 0 croak ("Unknown image type for file $filename");
290 0 return;
291 }
292 }
293  
294 1 my $id = $self->nextid('img');
295 1 $self->manifest->add_item(
296 id => $id,
297 href => $filename,
298 media_type => $type,
299 );
300  
301 1 return $id;
302 }
303  
304 sub add_entry
305 1 ▶{
Subroutine add_entry: called CC=1 SCAR=0.0
Pod undocumented
306 1 my ($self, $filename, $type) = @_;
307 1 my $id = $self->nextid('item');
308 1 $self->manifest->add_item(
309 id => $id,
310 href => $filename,
311 media_type => $type,
312 );
313  
314 1 return $id;
315 }
316  
317 4 ▶sub add_xhtml {
Subroutine add_xhtml: called CC=1 SCAR=0.0
Pod documented
318 4 my ($self, $filename, $data, %opts) = @_;
319 4 $self->_write_text([OPS => $filename], $data);
320 4 return $self->add_xhtml_entry($filename, %opts);
321 }
322  
323 2 ▶sub add_stylesheet {
Subroutine add_stylesheet: called CC=1 SCAR=0.0
Pod documented
324 2 my ($self, $filename, $data) = @_;
325 2 $self->_write_text([OPS => $filename], $data);
326 2 return $self->add_stylesheet_entry($filename);
327 }
328  
329 sub add_image
330 1 ▶{
Subroutine add_image: called CC=1 SCAR=0.0
Pod documented
331 1 my ($self, $filename, $data, $type) = @_;
332 1 $self->_write_data([OPS => $filename], $data);
333 1 return $self->add_image_entry($filename, $type);
334 }
335  
336 sub add_data
337 1 ▶{
Subroutine add_data: called CC=1 SCAR=0.0
Pod undocumented
338 1 my ($self, $filename, $data, $type) = @_;
339 1 $self->_write_data([OPS => $filename], $data);
340 1 return $self->add_entry($filename, $type);
341 }
342  
343 sub copy_xhtml
344 0 ▶{
Subroutine copy_xhtml: not called CC=2 SCAR=17.9
Pod documented
345 0 my ($self, $src_filename, $filename, %opts) = @_;
346 0 my $tmpdir = $self->tmpdir;
347 0 ▶ if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) { } 0 0 0
348 0 return $self->add_xhtml_entry($filename, %opts);
349 }
350 else {
351 0 carp ("Failed to copy $src_filename to $tmpdir/OPS/$filename");
352 }
353  
354 0 return;
355 }
356  
357 sub copy_stylesheet
358 0 ▶{
Subroutine copy_stylesheet: not called CC=2 SCAR=17.9
Pod documented
359 0 my ($self, $src_filename, $filename) = @_;
360 0 my $tmpdir = $self->tmpdir;
361 0 ▶ if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) { } 0 0 0
362 0 return $self->add_stylesheet_entry("$filename");
363 }
364 else {
365 0 carp ("Failed to copy $src_filename to $tmpdir/OPS/$filename");
366 }
367  
368 0 return;
369 }
370  
371 sub copy_image
372 0 ▶{
Subroutine copy_image: not called CC=2 SCAR=17.9
Pod documented
373 0 my ($self, $src_filename, $filename, $type) = @_;
374 0 my $tmpdir = $self->tmpdir;
375 0 ▶ if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) { } 0 0 0
376 0 return $self->add_image_entry("$filename");
377 }
378 else {
379 0 carp ("Failed to copy $src_filename to $tmpdir/OPS/$filename");
380 }
381  
382 0 return;
383 }
384  
385 sub copy_file
386 0 ▶{
Subroutine copy_file: not called CC=2 SCAR=17.9
Pod documented
387 0 my ($self, $src_filename, $filename, $type) = @_;
388 0 my $tmpdir = $self->tmpdir;
389 0 ▶ if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir_and_copy($src_filename, "$tmpdir/OPS/$filename")) { } 0 0 0
390 0 my $id = $self->nextid('id');
391 0 $self->manifest->add_item(
392 id => $id,
393 href => "$filename",
394 media_type => $type,
395 );
396 0 return $id;
397 }
398 else {
399 0 carp ("Failed to copy $src_filename to $tmpdir/OPS/$filename");
400 }
401  
402 0 return;
403 }
404  
405 sub encrypt_file
406 1 ▶{
Subroutine encrypt_file: called CC=3 SCAR=11.9
Pod documented
407 1 my ($self, $src_filename, $filename, $type) = @_;
408 1 my $tmpdir = $self->tmpdir;
409 1 ▶ if (!defined($self->_encryption_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_encryption_key) 0 1 1
410 0 croak "Can't encrypt without a key: no urn:uuid: indetifier has been provided";
411 }
412  
413 1 my $key = $self->_encryption_key;
414 1 ▶ if (adobe_encrypt($src_filename, "$tmpdir/OPS/$filename", $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (adobe_encrypt($src_filename, "$tmpdir/OPS/$filename", $key)) { } 1 0 1
415 1 my $id = $self->nextid('id');
416 1 $self->manifest->add_item(
417 id => $id,
418 href => "$filename",
419 media_type => $type,
420 );
421 1 $self->add_encrypted_fileref("OPS/$filename");
422 1 return $id;
423 }
424 else {
425 0 carp ("Failed to copy $src_filename to $tmpdir/OPS/$filename");
426 }
427  
428 0 return;
429 }
430  
431  
432 sub nextid
433 9 ▶{
Subroutine nextid: called CC=3 SCAR=11.0
Pod undocumented
434 9 my ($self, $prefix) = @_;
435 9 my $id;
436  
437 9 ▶ $prefix = 'id' unless(defined($prefix));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 9 9
438 9 ▶ if (defined(${$self->id_counters}{$prefix})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined do { $self->id_counters }->{$prefix}) { } 2 7 9
439 2 $id = "$prefix" . ${$self->id_counters}{$prefix};
440 2 ${$self->id_counters}{$prefix}++;
441 }
442 else
443 {
444 # First usage of prefix
445 7 $id = "${prefix}1";
446 7 ${$self->id_counters}{$prefix} = 2;
447 }
448  
449 9 return $id;
450 }
451  
452 sub pack_zip
453 2 ▶{
Subroutine pack_zip: called CC=2 SCAR=6.9
Pod documented
454 2 my ($self, $filename) = @_;
455 2 my $tmpdir = $self->tmpdir;
456 2 $self->write_ncx;
457 2 $self->write_opf;
458 2 my $container = EBook::EPUB::Lite::Container::Zip->new($filename);
459 2 $container->add_path($tmpdir . "/OPS", "OPS/");
460 2 $container->add_root_file("OPS/content.opf", "application/oebps-package+xml");
461 2 foreach my $fref ($self->encrypted_filerefs) {
462 1 $container->add_encrypted_path($fref);
463 }
464 2 return $container->write();
465 }
466  
467 2 ▶sub write_opf {
Subroutine write_opf: called CC=1 SCAR=0.0
Pod undocumented
468 2 my ($self) = @_;
469 2 $self->_write_text([OPS => 'content.opf' ], $self->to_xml);
470 }
471  
472 2 ▶sub write_ncx {
Subroutine write_ncx: called CC=1 SCAR=0.0
Pod undocumented
473 2 my ($self) = @_;
474 2 $self->_write_text([OPS => 'toc.ncx'], $self->ncx->to_xml)
475 }
476  
477 # helper function that performs Adobe content protection "encryption"
478 sub adobe_encrypt
479 1 ▶{
Subroutine adobe_encrypt: called CC=5 SCAR=16.1
Pod undocumented
480 1 my ($src, $dst, $key) = @_;
481 1 my @key_bytes = unpack "C*", $key;
482  
483 # open source/destination files for read/write
484 1 ▶ open (my $in, '<', $src) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $src or return 1 0 1
485 1 ▶ open (my $out, '>', $dst) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $dst or return 1 0 1
486 1 binmode $in;
487 1 binmode $out;
488  
489 # XOR first 1024 bytes of file by provided key
490 1 my $data;
491 1 read($in, $data, 1024);
492 1 my @bytes = unpack ("C*", $data);
493 1 my $key_ptr = 0;
494 1 foreach my $d (@bytes) {
495 1024 $d = $d ^ $key_bytes[$key_ptr];
496 1024 $key_ptr += 1;
497 1024 $key_ptr = $key_ptr % @key_bytes;
498 }
499  
500 1 my $crypted_data = pack "C*", @bytes;
501 1 print $out $crypted_data;
502  
503 # Copy th erest of the file, 1M buffer seems to be reasonable default
504 1 ▶ while (read($in, $data, 1024*1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in, $data, 1048576) 1 1 2
505 1 print $out $data;
506 }
507  
508 1 close $in;
509 1 close $out;
510 }
511  
512 10 ▶sub _write_text {
Subroutine _write_text: called CC=2 SCAR=7.0
513 10 my ($self, $path, $data) = @_;
514 10 my $filename = File::Spec->catfile($self->tmpdir, @$path);
515 # print "Writing $filename\n";
516 10 ▶ open (my $fh, '>:encoding(UTF-8)', $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $filename or die "Failed to open $filename $!" 10 0 10
517 or die "Failed to open $filename $!";
518 10 print $fh $data;
519 10 close $fh;
520 }
521  
522 2 ▶sub _write_data {
Subroutine _write_data: called CC=2 SCAR=7.0
523 2 my ($self, $path, $data) = @_;
524 2 my $filename = File::Spec->catfile($self->tmpdir, @$path);
525 2 ▶ open (my $fh, '>', $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Failed to open $filename $!" 2 0 2
526 or die "Failed to open $filename $!";
527 2 binmode $fh;
528 2 print $fh $data;
529 2 close $fh;
530 }
531  
532 0 ▶sub mkdir_and_copy {
Subroutine mkdir_and_copy: not called CC=1 SCAR=6.9
Pod undocumented
533 0 my ($from, $to) = @_;
534 0 mkpath(dirname($to));
535 0 return copy($from, $to);
536 }
537  
538 4 1;
539  
540 __END__
↑ Summary Lite/Container/Zip.pm »