| 1 | package Data::Serializer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 28 | require 5.004 ; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 28 | $VERSION = '0.65'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #Global cache of modules we've loaded | ||||||||||||||||||||||||||||
| 13 | 28 | my %_MODULES; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 28 | my %_fields = ( | |||||||||||||||||||||||||||
| 16 | serializer => 'Data::Dumper', | ||||||||||||||||||||||||||||
| 17 | digester => 'SHA-256', | ||||||||||||||||||||||||||||
| 18 | cipher => 'Blowfish', | ||||||||||||||||||||||||||||
| 19 | encoding => 'hex', | ||||||||||||||||||||||||||||
| 20 | compressor => 'Compress::Zlib', | ||||||||||||||||||||||||||||
| 21 | secret => undef, | ||||||||||||||||||||||||||||
| 22 | portable => '1', | ||||||||||||||||||||||||||||
| 23 | compress => '0', | ||||||||||||||||||||||||||||
| 24 | raw => '0', | ||||||||||||||||||||||||||||
| 25 | options => {}, | ||||||||||||||||||||||||||||
| 26 | serializer_token => '1', | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | 2650 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 2650 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 30 | 2650 | my $dataref = {%_fields}; | |||||||||||||||||||||||||||
| 31 | 2650 | foreach my $field (keys %_fields) { | |||||||||||||||||||||||||||
| 32 | 29150 | ▶ | $dataref->{$field} = $args{$field} if exists $args{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 2650 | my $self = $dataref; | |||||||||||||||||||||||||||
| 35 | 2650 | bless $self, $class; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #preintitialize serializer object | ||||||||||||||||||||||||||||
| 38 | 2650 | $self->_serializer_obj(); | |||||||||||||||||||||||||||
| 39 | 2650 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7954 | ▶ | sub _serializer_obj { | ||||||||||||||||||||||||||
|
Subroutine
_serializer_obj: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 43 | 7954 | my $self = (shift); | |||||||||||||||||||||||||||
| 44 | 7954 | my $method = (shift); | |||||||||||||||||||||||||||
| 45 | 7954 | my $reset = (shift); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 7954 | my $serializer = $self->{serializer}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | #remove cache if asked to | ||||||||||||||||||||||||||||
| 50 | 7954 | ▶ | if ($reset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 4 | delete $self->{serializer_obj}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | #If we're given the same method that we are already using, nothing to change | ||||||||||||||||||||||||||||
| 55 | 7954 | ▶ | if (defined $method && $method ne $serializer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $method and $method ne $serializer
MC/DC0%0/2
defined $method and $method ne $serializer
defined $method
$method ne $serializer
Truth table66%2/3
A and B
A= defined $methodB= $method ne $serializer
| |||||||||||||||||||||||||||||
| 56 | 0 | $serializer = $method; | |||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | #safe to return our cached object if we have it | ||||||||||||||||||||||||||||
| 59 | 7954 | ▶ | return $self->{serializer_obj} if (exists $self->{serializer_obj}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2654 | $self->_module_loader($serializer,"Data::Serializer"); #load in serializer module if necessary | |||||||||||||||||||||||||||
| 63 | 2654 | my $serializer_obj = {}; | |||||||||||||||||||||||||||
| 64 | 2654 | $serializer_obj->{options} = $self->{options}; | |||||||||||||||||||||||||||
| 65 | 2654 | bless $serializer_obj, "Data::Serializer::$serializer"; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #Cache it for later retrieval only if this is the default serializer for the object | ||||||||||||||||||||||||||||
| 68 | #ugly logic to support legacy token method that would allow the base to have a different serializer | ||||||||||||||||||||||||||||
| 69 | #than what it is reading | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2654 | ▶ | if ($serializer eq $self->{serializer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2654 | $self->{serializer_obj} = $serializer_obj; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2654 | return $serializer_obj; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1260 | ▶ | sub _persistent_obj { | ||||||||||||||||||||||||||
|
Subroutine
_persistent_obj: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 1260 | my $self = (shift); | |||||||||||||||||||||||||||
| 80 | 1260 | ▶ | return $self->{persistent_obj} if (exists $self->{persistent_obj}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 630 | $self->_module_loader('Data::Serializer::Persistent'); | |||||||||||||||||||||||||||
| 82 | 630 | my $persistent_obj = { parent => $self }; | |||||||||||||||||||||||||||
| 83 | 630 | bless $persistent_obj, "Data::Serializer::Persistent"; | |||||||||||||||||||||||||||
| 84 | 630 | $self->{persistent_obj} = $persistent_obj; | |||||||||||||||||||||||||||
| 85 | 630 | return $persistent_obj; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2776 | ▶ | sub serializer { | ||||||||||||||||||||||||||
|
Subroutine
serializer: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 2776 | my $self = (shift); | |||||||||||||||||||||||||||
| 93 | 2776 | my $return = $self->{serializer}; | |||||||||||||||||||||||||||
| 94 | 2776 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $self->{serializer} = (shift); | |||||||||||||||||||||||||||
| 96 | #Reinitialize object | ||||||||||||||||||||||||||||
| 97 | 0 | $self->_serializer_obj($self->{serializer}, 1); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 2776 | return $return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1512 | ▶ | sub digester { | ||||||||||||||||||||||||||
|
Subroutine
digester: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 1512 | my $self = (shift); | |||||||||||||||||||||||||||
| 104 | 1512 | my $return = $self->{digester}; | |||||||||||||||||||||||||||
| 105 | 1512 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 378 | my $value = (shift); | |||||||||||||||||||||||||||
| 107 | 378 | $self->{digester} = $value; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 1512 | return $return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1134 | ▶ | sub cipher { | ||||||||||||||||||||||||||
|
Subroutine
cipher: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 1134 | my $self = (shift); | |||||||||||||||||||||||||||
| 114 | 1134 | my $return = $self->{cipher}; | |||||||||||||||||||||||||||
| 115 | 1134 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $self->{cipher} = (shift); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 1134 | return $return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5292 | ▶ | sub compressor { | ||||||||||||||||||||||||||
|
Subroutine
compressor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 5292 | my $self = (shift); | |||||||||||||||||||||||||||
| 123 | 5292 | my $return = $self->{compressor}; | |||||||||||||||||||||||||||
| 124 | 5292 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 882 | $self->{compressor} = (shift); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 5292 | return $return; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 8450 | ▶ | sub secret { | ||||||||||||||||||||||||||
|
Subroutine
secret: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 8450 | my $self = (shift); | |||||||||||||||||||||||||||
| 132 | 8450 | my $return = $self->{secret}; | |||||||||||||||||||||||||||
| 133 | 8450 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1134 | $self->{secret} = (shift); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 8450 | return $return; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4544 | ▶ | sub encoding { | ||||||||||||||||||||||||||
|
Subroutine
encoding: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 4544 | my $self = (shift); | |||||||||||||||||||||||||||
| 141 | 4544 | my $return = $self->{encoding}; | |||||||||||||||||||||||||||
| 142 | 4544 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 504 | $self->{encoding} = (shift); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 4544 | return $return; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3028 | ▶ | sub portable { | ||||||||||||||||||||||||||
|
Subroutine
portable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 3028 | my $self = (shift); | |||||||||||||||||||||||||||
| 150 | 3028 | my $return = $self->{portable}; | |||||||||||||||||||||||||||
| 151 | 3028 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 504 | $self->{portable} = (shift); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 3028 | return $return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | ▶ | sub options { | ||||||||||||||||||||||||||
|
Subroutine
options: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 4 | my $self = (shift); | |||||||||||||||||||||||||||
| 159 | 4 | my $return = $self->{options}; | |||||||||||||||||||||||||||
| 160 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 4 | $self->{options} = (shift); | |||||||||||||||||||||||||||
| 162 | #Reinitialize object | ||||||||||||||||||||||||||||
| 163 | 4 | $self->_serializer_obj($self->{serializer}, 1); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 4 | return $return; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 5930 | ▶ | sub compress { | ||||||||||||||||||||||||||
|
Subroutine
compress: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 5930 | my $self = (shift); | |||||||||||||||||||||||||||
| 170 | 5930 | my $return = $self->{compress}; | |||||||||||||||||||||||||||
| 171 | 5930 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 882 | $self->{compress} = (shift); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 5930 | return $return; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 5048 | ▶ | sub raw { | ||||||||||||||||||||||||||
|
Subroutine
raw: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 5048 | my $self = (shift); | |||||||||||||||||||||||||||
| 179 | 5048 | my $return = $self->{raw}; | |||||||||||||||||||||||||||
| 180 | 5048 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $self->{raw} = (shift); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 5048 | return $return; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 2524 | ▶ | sub serializer_token { | ||||||||||||||||||||||||||
|
Subroutine
serializer_token: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 188 | 2524 | my $return = $self->{serializer_token}; | |||||||||||||||||||||||||||
| 189 | 2524 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | $self->{serializer_token} = (shift); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 2524 | return $return; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 15892 | ▶ | sub _module_loader { | ||||||||||||||||||||||||||
|
Subroutine
_module_loader: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 196 | 15892 | my $self = (shift); | |||||||||||||||||||||||||||
| 197 | 15892 | my $module_name = (shift); | |||||||||||||||||||||||||||
| 198 | 15892 | ▶ | unless (defined $module_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | confess "Something wrong - module not defined! $! $@\n"; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 15892 | ▶ | return if (exists $_MODULES{$module_name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 7970 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 7954 | $module_name = (shift) . "::$module_name"; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 7970 | my $package = $module_name; | |||||||||||||||||||||||||||
| 206 | 7970 | $package =~ s|::|/|g; | |||||||||||||||||||||||||||
| 207 | 7970 | $package .= ".pm"; | |||||||||||||||||||||||||||
| 208 | 7970 | eval { require $package }; | |||||||||||||||||||||||||||
| 209 | 7970 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | carp "Data::Serializer error: " . | |||||||||||||||||||||||||||
| 211 | "Please make sure $package is a properly installed package.\n"; | ||||||||||||||||||||||||||||
| 212 | 0 | return undef; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 7970 | $_MODULES{$module_name} = 1; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 2650 | ▶ | sub _serialize { | ||||||||||||||||||||||||||
|
Subroutine
_serialize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | 2650 | my $self = (shift); | |||||||||||||||||||||||||||
| 223 | 2650 | my @input = @{(shift)};#original @_ | |||||||||||||||||||||||||||
| 224 | 2650 | my $method = (shift); | |||||||||||||||||||||||||||
| 225 | 2650 | $self->_module_loader($method,"Data::Serializer"); #load in serializer module if necessary | |||||||||||||||||||||||||||
| 226 | 2650 | my $serializer_obj = $self->_serializer_obj($method); | |||||||||||||||||||||||||||
| 227 | 2650 | return $serializer_obj->serialize(@input); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 882 | ▶ | sub _compress { | ||||||||||||||||||||||||||
|
Subroutine
_compress: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 231 | 882 | my $self = (shift); | |||||||||||||||||||||||||||
| 232 | 882 | $self->_module_loader($self->compressor); | |||||||||||||||||||||||||||
| 233 | 882 | ▶ | if ($self->compressor eq 'Compress::Zlib') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 234 | 882 | return Compress::Zlib::compress((shift)); | |||||||||||||||||||||||||||
| 235 | } elsif ($self->compressor eq 'Compress::PPMd') { | ||||||||||||||||||||||||||||
| 236 | 0 | my $compressor = Compress::PPMd::Encoder->new(); | |||||||||||||||||||||||||||
| 237 | 0 | return $compressor->encode((shift)); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 882 | ▶ | sub _decompress { | ||||||||||||||||||||||||||
|
Subroutine
_decompress: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 241 | 882 | my $self = (shift); | |||||||||||||||||||||||||||
| 242 | 882 | $self->_module_loader($self->compressor); | |||||||||||||||||||||||||||
| 243 | 882 | ▶ | if ($self->compressor eq 'Compress::Zlib') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 244 | 882 | return Compress::Zlib::uncompress((shift)); | |||||||||||||||||||||||||||
| 245 | } elsif ($self->compressor eq 'Compress::PPMd') { | ||||||||||||||||||||||||||||
| 246 | 0 | my $compressor = Compress::PPMd::Decoder->new(); | |||||||||||||||||||||||||||
| 247 | 0 | return $compressor->decode((shift)); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 2524 | ▶ | sub _create_token { | ||||||||||||||||||||||||||
|
Subroutine
_create_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 252 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 253 | 2524 | return '^' . join('|', @_) . '^'; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 2524 | ▶ | sub _get_token { | ||||||||||||||||||||||||||
|
Subroutine
_get_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 256 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 257 | 2524 | my $line = (shift); | |||||||||||||||||||||||||||
| 258 | #Should be anchored to beginning | ||||||||||||||||||||||||||||
| 259 | #my ($token) = $line =~ /\^([^\^]+?)\^/; | ||||||||||||||||||||||||||||
| 260 | 2524 | my ($token) = $line =~ /^\^([^\^]{1,120}?)\^/; | |||||||||||||||||||||||||||
| 261 | 2524 | return $token; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 2524 | ▶ | sub _extract_token { | ||||||||||||||||||||||||||
|
Subroutine
_extract_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 264 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 265 | 2524 | my $token = (shift); | |||||||||||||||||||||||||||
| 266 | 2524 | return split('\|',$token); | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 2524 | ▶ | sub _remove_token { | ||||||||||||||||||||||||||
|
Subroutine
_remove_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 270 | 2524 | my $line = (shift); | |||||||||||||||||||||||||||
| 271 | 2524 | $line =~ s/^\^[^\^]{1,120}?\^//; | |||||||||||||||||||||||||||
| 272 | 2524 | return $line; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 2650 | ▶ | sub _deserialize { | ||||||||||||||||||||||||||
|
Subroutine
_deserialize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 275 | 2650 | my $self = (shift); | |||||||||||||||||||||||||||
| 276 | 2650 | my $input = (shift); | |||||||||||||||||||||||||||
| 277 | 2650 | my $method = (shift); | |||||||||||||||||||||||||||
| 278 | 2650 | $self->_module_loader($method,"Data::Serializer"); #load in serializer module if necessary | |||||||||||||||||||||||||||
| 279 | 2650 | my $serializer_obj = $self->_serializer_obj($method); | |||||||||||||||||||||||||||
| 280 | 2650 | $serializer_obj->deserialize($input); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 1134 | ▶ | sub _encrypt { | ||||||||||||||||||||||||||
|
Subroutine
_encrypt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 284 | 1134 | my $self = (shift); | |||||||||||||||||||||||||||
| 285 | 1134 | my $value = (shift); | |||||||||||||||||||||||||||
| 286 | 1134 | my $cipher = (shift); | |||||||||||||||||||||||||||
| 287 | 1134 | my $digester = (shift); | |||||||||||||||||||||||||||
| 288 | 1134 | my $secret = $self->secret; | |||||||||||||||||||||||||||
| 289 | 1134 | ▶ | croak "Cannot encrypt: No secret provided!" unless defined $secret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 1134 | $self->_module_loader('Crypt::CBC'); | |||||||||||||||||||||||||||
| 291 | 1134 | my $digest = $self->_endigest($value,$digester); | |||||||||||||||||||||||||||
| 292 | 1134 | my $cipher_obj = Crypt::CBC->new($secret,$cipher); | |||||||||||||||||||||||||||
| 293 | 1134 | return $cipher_obj->encrypt($digest); | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 1134 | ▶ | sub _decrypt { | ||||||||||||||||||||||||||
|
Subroutine
_decrypt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 296 | 1134 | my $self = (shift); | |||||||||||||||||||||||||||
| 297 | 1134 | my $input = (shift); | |||||||||||||||||||||||||||
| 298 | 1134 | my $cipher = (shift); | |||||||||||||||||||||||||||
| 299 | 1134 | my $digester = (shift); | |||||||||||||||||||||||||||
| 300 | 1134 | my $secret = $self->secret; | |||||||||||||||||||||||||||
| 301 | 1134 | ▶ | croak "Cannot encrypt: No secret provided!" unless defined $secret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 1134 | $self->_module_loader('Crypt::CBC'); | |||||||||||||||||||||||||||
| 303 | 1134 | my $cipher_obj = Crypt::CBC->new($secret,$cipher); | |||||||||||||||||||||||||||
| 304 | 1134 | my $digest = $cipher_obj->decrypt($input); | |||||||||||||||||||||||||||
| 305 | 1134 | return $self->_dedigest($digest,$digester); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | 1134 | ▶ | sub _endigest { | ||||||||||||||||||||||||||
|
Subroutine
_endigest: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 308 | 1134 | my $self = (shift); | |||||||||||||||||||||||||||
| 309 | 1134 | my $input = (shift); | |||||||||||||||||||||||||||
| 310 | 1134 | my $digester = (shift); | |||||||||||||||||||||||||||
| 311 | 1134 | $self->_module_loader('Digest'); | |||||||||||||||||||||||||||
| 312 | 1134 | my $digest = $self->_get_digest($input,$digester); | |||||||||||||||||||||||||||
| 313 | 1134 | return "$digest=$input"; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 1134 | ▶ | sub _dedigest { | ||||||||||||||||||||||||||
|
Subroutine
_dedigest: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 316 | 1134 | my $self = (shift); | |||||||||||||||||||||||||||
| 317 | 1134 | my $input = (shift); | |||||||||||||||||||||||||||
| 318 | 1134 | my $digester = (shift); | |||||||||||||||||||||||||||
| 319 | 1134 | $self->_module_loader('Digest'); | |||||||||||||||||||||||||||
| 320 | #my ($old_digest) = $input =~ /^([^=]+?)=/; | ||||||||||||||||||||||||||||
| 321 | 1134 | $input =~ s/^([^=]+?)=//; | |||||||||||||||||||||||||||
| 322 | 1134 | my $old_digest = $1; | |||||||||||||||||||||||||||
| 323 | 1134 | ▶ | return undef unless (defined $old_digest); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 1134 | my $new_digest = $self->_get_digest($input,$digester); | |||||||||||||||||||||||||||
| 325 | 1134 | ▶ | return undef unless ($new_digest eq $old_digest); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 1134 | return $input; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | 2268 | ▶ | sub _get_digest { | ||||||||||||||||||||||||||
|
Subroutine
_get_digest: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 329 | 2268 | my $self = (shift); | |||||||||||||||||||||||||||
| 330 | 2268 | my $input = (shift); | |||||||||||||||||||||||||||
| 331 | 2268 | my $digester = (shift); | |||||||||||||||||||||||||||
| 332 | 2268 | my $ctx = Digest->new($digester); | |||||||||||||||||||||||||||
| 333 | 2268 | $ctx->add($input); | |||||||||||||||||||||||||||
| 334 | 2268 | return $ctx->hexdigest; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 1516 | ▶ | sub _enhex { | ||||||||||||||||||||||||||
|
Subroutine
_enhex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 337 | 1516 | my $self = (shift); | |||||||||||||||||||||||||||
| 338 | 1516 | return join('',unpack 'H*',(shift)); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | 1516 | ▶ | sub _dehex { | ||||||||||||||||||||||||||
|
Subroutine
_dehex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 341 | 1516 | my $self = (shift); | |||||||||||||||||||||||||||
| 342 | 1516 | return (pack'H*',(shift)); | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 504 | ▶ | sub _enb64 { | ||||||||||||||||||||||||||
|
Subroutine
_enb64: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 346 | 504 | my $self = (shift); | |||||||||||||||||||||||||||
| 347 | 504 | $self->_module_loader('MIME::Base64'); | |||||||||||||||||||||||||||
| 348 | 504 | my $b64 = MIME::Base64::encode_base64( (shift), '' ); | |||||||||||||||||||||||||||
| 349 | 504 | return $b64; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 504 | ▶ | sub _deb64 { | ||||||||||||||||||||||||||
|
Subroutine
_deb64: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 354 | 504 | my $self = (shift); | |||||||||||||||||||||||||||
| 355 | 504 | $self->_module_loader('MIME::Base64'); | |||||||||||||||||||||||||||
| 356 | 504 | return MIME::Base64::decode_base64( (shift) ); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | # do all 3 stages | ||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub freeze { (shift)->serialize(@_); } | ||||||||||||||||||||||||||
|
Subroutine
freeze: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | 0 | ▶ | sub thaw { (shift)->deserialize(@_); } | ||||||||||||||||||||||||||
|
Subroutine
thaw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 2524 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 364 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 365 | 2524 | my ($serializer,$cipher,$digester,$encoding,$compressor) = ('','','','',''); | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 2524 | ▶ | if ($self->raw) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | return $self->raw_serialize(@_); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | #we always serialize no matter what. | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | #define serializer for token | ||||||||||||||||||||||||||||
| 374 | 2524 | $serializer = $self->serializer; | |||||||||||||||||||||||||||
| 375 | 2524 | my $value = $self->_serialize(\@_,$serializer); | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 2524 | ▶ | if ($self->compress) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 882 | $compressor = $self->compressor; | |||||||||||||||||||||||||||
| 379 | 882 | $value = $self->_compress($value); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 2524 | ▶ | if (defined $self->secret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | #define digester for token | ||||||||||||||||||||||||||||
| 384 | 1134 | $digester = $self->digester; | |||||||||||||||||||||||||||
| 385 | #define cipher for token | ||||||||||||||||||||||||||||
| 386 | 1134 | $cipher = $self->cipher; | |||||||||||||||||||||||||||
| 387 | 1134 | $value = $self->_encrypt($value,$cipher,$digester); | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | 2524 | ▶ | if ($self->portable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 2020 | $encoding = $self->encoding; | |||||||||||||||||||||||||||
| 391 | 2020 | $value = $self->_encode($value); | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | 2524 | ▶ | if ($self->serializer_token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 2524 | my $token = $self->_create_token($serializer,$cipher, $digester,$encoding,$compressor); | |||||||||||||||||||||||||||
| 395 | 2524 | $value = $token . $value; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | 2524 | return $value; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 630 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 401 | 630 | my $self = (shift); | |||||||||||||||||||||||||||
| 402 | 630 | my $persistent = $self->_persistent_obj(); | |||||||||||||||||||||||||||
| 403 | 630 | $persistent->_store(@_); | |||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 630 | ▶ | sub retrieve { | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 407 | 630 | my $self = (shift); | |||||||||||||||||||||||||||
| 408 | 630 | my $persistent = $self->_persistent_obj(); | |||||||||||||||||||||||||||
| 409 | 630 | $persistent->_retrieve(@_); | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 126 | ▶ | sub raw_serialize { | ||||||||||||||||||||||||||
|
Subroutine
raw_serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 413 | 126 | my $self = (shift); | |||||||||||||||||||||||||||
| 414 | 126 | my $serializer = $self->serializer; | |||||||||||||||||||||||||||
| 415 | 126 | return $self->_serialize(\@_,$serializer); | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 2020 | ▶ | sub _encode { | ||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 419 | 2020 | my $self = (shift); | |||||||||||||||||||||||||||
| 420 | 2020 | my $value = (shift); | |||||||||||||||||||||||||||
| 421 | 2020 | my $encoding = $self->encoding; | |||||||||||||||||||||||||||
| 422 | 2020 | ▶ | if ($encoding eq 'hex') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 423 | 1516 | return $self->_enhex($value); | |||||||||||||||||||||||||||
| 424 | } elsif ($encoding eq 'b64') { | ||||||||||||||||||||||||||||
| 425 | 504 | return $self->_enb64($value); | |||||||||||||||||||||||||||
| 426 | } else { | ||||||||||||||||||||||||||||
| 427 | 0 | croak "Unknown encoding method $encoding\n"; | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 2272 | ▶ | sub _decode { | ||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 432 | 2272 | my $self = (shift); | |||||||||||||||||||||||||||
| 433 | 2272 | my $value = (shift); | |||||||||||||||||||||||||||
| 434 | 2272 | my $encoding = (shift); | |||||||||||||||||||||||||||
| 435 | 2272 | ▶ | if ($encoding eq 'hex') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 436 | 1516 | return $self->_dehex($value); | |||||||||||||||||||||||||||
| 437 | } elsif ($encoding eq 'b64') { | ||||||||||||||||||||||||||||
| 438 | 504 | return $self->_deb64($value); | |||||||||||||||||||||||||||
| 439 | } elsif ($encoding !~ /\S/) { | ||||||||||||||||||||||||||||
| 440 | #quietly ignore empty encoding | ||||||||||||||||||||||||||||
| 441 | 252 | return $value; | |||||||||||||||||||||||||||
| 442 | } else { | ||||||||||||||||||||||||||||
| 443 | 0 | croak "Unknown encoding method $encoding\n"; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 126 | ▶ | sub raw_deserialize { | ||||||||||||||||||||||||||
|
Subroutine
raw_deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 448 | 126 | my $self = (shift); | |||||||||||||||||||||||||||
| 449 | 126 | my $serializer = $self->serializer; | |||||||||||||||||||||||||||
| 450 | 126 | return $self->_deserialize((shift),$serializer); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 2524 | ▶ | sub deserialize { | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=9 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 454 | 2524 | my $self = (shift); | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 2524 | ▶ | if ($self->raw) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | 0 | return $self->raw_deserialize(@_); | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 2524 | my $value = (shift); | |||||||||||||||||||||||||||
| 461 | 2524 | my $token = $self->_get_token($value); | |||||||||||||||||||||||||||
| 462 | 2524 | my ($serializer,$cipher, $digester,$encoding, $compressor); | |||||||||||||||||||||||||||
| 463 | 2524 | my $compress = $self->compress; | |||||||||||||||||||||||||||
| 464 | 2524 | ▶ | if (defined $token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | 2524 | ($serializer,$cipher, $digester,$encoding, $compressor) = $self->_extract_token($token); | |||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | #if compressor is defined and has a value then we must decompress it | ||||||||||||||||||||||||||||
| 468 | 2524 | ▶ | $compress = 1 if ($compressor); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | 2524 | $value = $self->_remove_token($value); | |||||||||||||||||||||||||||
| 470 | } else { | ||||||||||||||||||||||||||||
| 471 | 0 | $serializer = $self->serializer; | |||||||||||||||||||||||||||
| 472 | 0 | $cipher = $self->cipher; | |||||||||||||||||||||||||||
| 473 | 0 | $digester = $self->digester; | |||||||||||||||||||||||||||
| 474 | 0 | $compressor = $self->compressor; | |||||||||||||||||||||||||||
| 475 | 0 | ▶ | if ($self->portable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | 0 | $encoding = $self->encoding; | |||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | 2524 | ▶ | if (defined $encoding) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 480 | 2272 | $value = $self->_decode($value,$encoding); | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | 2524 | ▶ | if (defined $self->secret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 483 | 1134 | $value = $self->_decrypt($value,$cipher,$digester); | |||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||
| 485 | 2524 | ▶ | if ($compress) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 486 | 882 | $value = $self->_decompress($value); | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | #we always deserialize no matter what. | ||||||||||||||||||||||||||||
| 489 | 2524 | my @return = $self->_deserialize($value,$serializer); | |||||||||||||||||||||||||||
| 490 | 2524 | ▶ | return wantarray ? @return : $return[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 28 | 1; | |||||||||||||||||||||||||||
| 494 | __END__ | ||||||||||||||||||||||||||||