| 1 | package Archive::Tyd; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Crypt::CipherSaber; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 11 | 0 | ▶ | my $class = ref($proto) || $proto || 'Archive::Tyd'; | ||||||||||||||||||||||||||
|
Condition0%0/5
ref $proto || $proto || 'Archive::Tyd'
ref $proto or $proto
MC/DC0%0/2
ref $proto || $proto || 'Archive::Tyd'
ref $proto
$proto
Truth table0%0/3
A or B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | my $self = { | |||||||||||||||||||||||||||
| 14 | password => 'default', # Archive Password | ||||||||||||||||||||||||||||
| 15 | contents => [], # Contents Array | ||||||||||||||||||||||||||||
| 16 | files => {}, # File Data | ||||||||||||||||||||||||||||
| 17 | cipher => undef, # Cipher object | ||||||||||||||||||||||||||||
| 18 | debug => 0, | ||||||||||||||||||||||||||||
| 19 | @_, | ||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | bless ($self,$class); | |||||||||||||||||||||||||||
| 23 | 0 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub cipher { | ||||||||||||||||||||||||||
|
Subroutine
cipher: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $force = shift || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (not defined $self->{cipher} || $force) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{'cipher'} || $force
MC/DC0%0/2
defined $self->{'cipher'} || $force
defined $self->{'cipher'}
$force
Truth table0%0/3
A || B
A= defined $self->{'cipher'}B= $force
| |||||||||||||||||||||||||||||
| 31 | 0 | $self->{cipher} = Crypt::CipherSaber->new ($self->{password}); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub password { | ||||||||||||||||||||||||||
|
Subroutine
password: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($self,$pass) = shift; | |||||||||||||||||||||||||||
| 37 | 0 | $self->{password} = $pass; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | $self->cipher (1); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub openArchive { | ||||||||||||||||||||||||||
|
Subroutine
openArchive: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $self->cipher; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Read the file. | ||||||||||||||||||||||||||||
| 48 | 0 | open (FILE, "$file"); | |||||||||||||||||||||||||||
| 49 | 0 | binmode FILE; | |||||||||||||||||||||||||||
| 50 | 0 | my @data = <FILE>; | |||||||||||||||||||||||||||
| 51 | 0 | close (FILE); | |||||||||||||||||||||||||||
| 52 | 0 | chomp @data; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | my $contents = join ("\n",@data); | |||||||||||||||||||||||||||
| 55 | 0 | $contents = $self->{cipher}->decrypt ($contents); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my @lines = split(/\n/, $contents); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 60 | 0 | my ($name,$bin) = split(/::/, $line, 2); | |||||||||||||||||||||||||||
| 61 | 0 | $bin =~ s/<<ln>>/\n/g; | |||||||||||||||||||||||||||
| 62 | 0 | $bin =~ s/<<lr>>/\r/g; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | $self->{files}->{$name} = $bin; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | print "Opened archive $file - contents " . scalar(@lines) . " files\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub writeArchive { | ||||||||||||||||||||||||||
|
Subroutine
writeArchive: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | $self->cipher; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Write to the file. | ||||||||||||||||||||||||||||
| 76 | 0 | my @write = (); | |||||||||||||||||||||||||||
| 77 | 0 | foreach my $item (keys %{$self->{files}}) { | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | print "\tAdding $item to output\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Make sure the line breaks are taken care of. | ||||||||||||||||||||||||||||
| 81 | 0 | $self->{files}->{$item} =~ s/\n/<<ln>>/g; | |||||||||||||||||||||||||||
| 82 | 0 | $self->{files}->{$item} =~ s/\r/<<lr>>/g; | |||||||||||||||||||||||||||
| 83 | 0 | push (@write, "$item" . '::' . "$self->{files}->{$item}"); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $bin = join ("\n",@write); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $enc = $self->{cipher}->encrypt ($bin); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | open (OUT, ">$file"); | |||||||||||||||||||||||||||
| 91 | 0 | binmode OUT; | |||||||||||||||||||||||||||
| 92 | 0 | print OUT $enc; | |||||||||||||||||||||||||||
| 93 | 0 | close (OUT); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | print "Wrote " . scalar(keys %{$self->{files}}) . " files to $file\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub contents { | ||||||||||||||||||||||||||
|
Subroutine
contents: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my @files = keys %{$self->{files}}; | |||||||||||||||||||||||||||
| 102 | 0 | return @files; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub addFile { | ||||||||||||||||||||||||||
|
Subroutine
addFile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $self->cipher; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | open (FILE, $file); | |||||||||||||||||||||||||||
| 111 | 0 | binmode FILE; | |||||||||||||||||||||||||||
| 112 | 0 | my @data = <FILE>; | |||||||||||||||||||||||||||
| 113 | 0 | close (FILE); | |||||||||||||||||||||||||||
| 114 | 0 | chomp @data; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $filename = $self->filename ($file); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $content = join ("<<linebreak>>",@data); | |||||||||||||||||||||||||||
| 119 | 0 | $content =~ s/\n/<<ln>>/g; | |||||||||||||||||||||||||||
| 120 | 0 | $content =~ s/\r/<<lr>>/g; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | print "Added $filename to archive\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Add this to the index. | ||||||||||||||||||||||||||||
| 125 | 0 | $self->{files}->{$filename} = $content; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub deleteFile { | ||||||||||||||||||||||||||
|
Subroutine
deleteFile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my ($self,$filename) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | $self->cipher; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (exists $self->{files}->{$filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | print "Deleted $filename\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | delete $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 0 | warn "File $filename doesn't exist in this archive"; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub readFile { | ||||||||||||||||||||||||||
|
Subroutine
readFile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self,$filename) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | $self->cipher; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (exists $self->{files}->{$filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | my $bin = $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 149 | 0 | $bin =~ s/<<ln>>/\n/g; | |||||||||||||||||||||||||||
| 150 | 0 | $bin =~ s/<<lr>>/\r/g; | |||||||||||||||||||||||||||
| 151 | 0 | $bin =~ s/<<linebreak>>/\n/g; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | print "Read $filename\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return $bin; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 0 | warn "File $filename doesn't exist in this archive"; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub filename { | ||||||||||||||||||||||||||
|
Subroutine
filename: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | my ($self,$path) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my @parts = split(/(\/|\\)/, $path); | |||||||||||||||||||||||||||
| 166 | 0 | my $name = pop(@parts); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | return $name; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | 1; | |||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||