blib/lib/PlayStation/MemoryCard.pm

filter: Statementstmt 11.5% 17 / 147 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 25.0% 5 / 20 Podpod 0.0% 0 / 13 totaltotal 7.6% 22 / 286 CC 41 SCAR 43.4
CC
41
Cov
7%
load
240.0
SaveNameAndTitleMatch
72.0
parse_file_header
20.0
CRAP 76.4
↑ Summary
1 package PlayStation::MemoryCard;
2 # Copyright (c) 2021 Gavin Hayes and others, see LICENSE in the root of the project
3 1 ▶use version; our $VERSION = version->declare("v0.2.1");
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub parse_directory {
Subroutine parse_directory: not called CC=2 SCAR=17.9
Pod undocumented
10 0 my ($directory) = @_;
11 0 my $inuse = unpack('C', $directory);
12 0 my $datasize = unpack('V', substr($directory, 0x4, 0x4));
13 0 my $linkindex = unpack('v', substr($directory, 0x8));
14 0 my $codestr = unpack('Z*', substr($directory, 0xA));
15 0 my @toxor = unpack('C127', $directory);
16 0 my $storedxor = unpack('C', substr($directory, 0x7F));
17  
18 #my @blocktypes = ('INUSE', 'SRTLINK', 'MIDLINK', 'ENDLINK', 'EMPTY', 'UNUSABLE', 'UNKNOWN');
19 0 my $blockcount = int($datasize / 0x2000);
20  
21 0 my $calcxor = 0;
22 0 foreach my $char (@toxor) {
23 0 $calcxor ^= $char;
24 }
25  
26 return {
27 0 'inuse' => $inuse,
28 'datasize' => $datasize,
29 'linkindex' => $linkindex,
30 'codename' => $codestr,
31 'xor' => $storedxor,
32  
33 'calcxor' => $calcxor,
34 'calcblocks' => $blockcount
35 };
36 }
37  
38 0 ▶sub parse_file_header {
Subroutine parse_file_header: not called CC=4 SCAR=30.0
Pod undocumented
39 0 my ($file) = @_;
40 0 my $id = unpack('a2', $file);
41 0 my $displayflag = unpack('C', substr($file, 0x2));
42 0 my $blocknum = unpack('C', substr($file, 0x3));
43 0 my $shiftjisbuf = unpack('a64', substr($file, 0x4, 0x40));
44 0 my @clut = unpack('v16', substr($file, 0x60, 0x20));
45  
46  
47 0 my $iconfnt = 0;
48 0 ▶ if(($displayflag >= 0x11)|| ($displayflag <= 0x13)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($displayflag >= 17 or $displayflag <= 19) 0 0 0
Condition0%0/3
$displayflag >= 17 or $displayflag <= 19
l!l&&r!l&&!r
000
MC/DC0%0/2
$displayflag >= 17 or $displayflag <= 19
$displayflag >= 17 $displayflag <= 19
Truth table0%0/3
A or B
A= $displayflag >= 17B= $displayflag <= 19
ABresult
1X1
011
000
49 0 $iconfnt = $displayflag - 0x10;
50 }
51  
52 0 my $firstnul = index($shiftjisbuf, "\0");
53 0 ▶ if($firstnul != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstnul != -1) 0 0 0
54 0 $shiftjisbuf = substr($shiftjisbuf, 0, $firstnul);
55 }
56 0 my $shiftjis = decode('shiftjis', $shiftjisbuf);
57  
58 return {
59 0 'id' => $id,
60 'displayflag' => $displayflag,
61 'blocknum' => $blocknum,
62 'titlebuf' => $shiftjisbuf,
63 'clut' => \@clut,
64 'title' => $shiftjis,
65 'framecnt' => $iconfnt
66 };
67 }
68  
69 0 ▶sub is_mcd {
Subroutine is_mcd: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my ($res) = @_;
71  
72 0 ▶ (substr($res, 0, 2) eq 'MC') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($res, 0, 2) eq "MC" or return 0 0 0 0
73  
74  
75 # A PSX memory card is 1 Mebibyte/ 128 kibibyte/ 131072 bytes
76 # 1 header block of 8192 and 15 data blocks of 8192.
77 0 return (length($res) == 131072);
78 }
79  
80 0 ▶sub is_mcs {
Subroutine is_mcs: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my ($res) = @_;
82 # A PSX mcs save is 1 directory frame and X data frames
83 0 my $datasize = length($res) - 0x80;
84 0 return (($datasize % 0x2000) == 0);
85 }
86  
87 0 ▶sub xordirectory {
Subroutine xordirectory: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my ($directory) = @_;
89 0 my @toxor = unpack('C127', $directory);
90 0 my $xor = 0;
91 0 foreach my $char (@toxor) {
92 0 $xor ^= $char;
93 }
94 0 return $xor;
95 }
96  
97  
98 0 ▶sub load {
Subroutine load: not called CC=15 SCAR=54.8
Pod undocumented
99 0 my ($class, $filename, $overridefilename) = @_;
100
101 0 my $fh;
102 0 ▶ if($filename ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne '-') { } 0 0 0
103 0 ▶ open($fh, '<', $filename) or die("failed to open: $filename");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or die "failed to open: $filename" 0 0 0
104 }
105 else {
106 0 $fh = *STDIN;
107 0 $filename = 'STDIN';
108 }
109 0 my %self = ('filename' => $filename, 'contents' => '');
110 0 my $res = read($fh, $self{'contents'}, 131073);
111  
112 # a mcd file (full memory card dump) should be the largest file
113 0 ▶ (($res) && ($res <= 131072)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$res and $res <= 131072 or return undef 0 0 0
Condition0%0/3
$res and $res <= 131072
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res <= 131072
$res $res <= 131072
Truth table0%0/3
A and B
A= $resB= $res <= 131072
ABresult
0X0
100
111
114  
115 0 ▶ if(is_mcd($self{'contents'})) {
Branch: 4 branches
BranchTrueFalseTotal
if (is_mcd($self{'contents'})) { } 0 0 0
elsif (is_mcs($self{'contents'})) { } 0 0 0
elsif ($res <= 122880 and $res >= 8192 and $res % 8192 == 0) { } 0 0 0
elsif (substr($res, 0, 2) eq 'MC') { } 0 0 0
116 0 $self{'type'} = 'mcd';
117 }
118 elsif(is_mcs($self{'contents'})) {
119 0 $self{'type'} = 'mcs';
120 }
121 ▶ elsif(($res <= (15*0x2000)) && ($res >= 0x2000) && (($res % 0x2000) == 0)) {
Condition0%0/6
$res <= 122880 and $res >= 8192 and $res % 8192 == 0
!ll&&!rl&&r
000
$res <= 122880 and $res >= 8192
!ll&&!rl&&r
000
MC/DC0%0/3
$res <= 122880 and $res >= 8192 and $res % 8192 == 0
$res <= 122880 $res >= 8192 $res % 8192 == 0
Truth table0%0/4
A and B and C
A= $res <= 122880B= $res >= 8192C= $res % 8192 == 0
ABCresult
0XX0
10X0
1100
1111
122 0 $self{'type'} = 'rawsave';
123 0 ▶ my $filecodename = ($filename ne 'STDIN') ? basename($filename) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$filename ne 'STDIN' ? : 0 0 0
124 0 ▶ $self{'codename'} = $overridefilename ? $overridefilename : $filecodename;
Branch: 1 branch
BranchTrueFalseTotal
$overridefilename ? : 0 0 0
125 }
126 elsif(substr($res, 0, 2) eq 'MC') {
127 0 warn("File starts with MC, but filesize is $res. Assuming type is mcd");
128 0 $self{'type'} = 'mcd';
129 }
130 else {
131 0 return undef;
132 }
133  
134 0 ▶ if(($self{'type'} eq 'mcd') || ($self{'type'} eq 'mcs')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self{"type"} eq "mcd" or $self{"type"} eq "mcs") 0 0 0
Condition0%0/3
$self{"type"} eq "mcd" or $self{"type"} eq "mcs"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self{"type"} eq "mcd" or $self{"type"} eq "mcs"
$self{"type"} eq "mcd" $self{"type"} eq "mcs"
Truth table0%0/3
A or B
A= $self{"type"} eq "mcd"B= $self{"type"} eq "mcs"
ABresult
1X1
011
000
135 0 $self{'hasdir'} = 1;
136 }
137  
138 0 bless \%self, $class;
139 0 return \%self;
140 }
141  
142 # loop through the directory entries of a MCD file, calling callback for each one
143 # if it's a startblock read in the save as pass it to the callback
144 0 ▶sub foreachDirEntry {
Subroutine foreachDirEntry: not called CC=4 SCAR=30.0
Pod undocumented
145 0 my ($self, $callback) = @_;
146 0 ▶ ($self->{'type'} eq 'mcd') or die("Unhandled filetype");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"type"} eq "mcd" or die "Unhandled filetype" 0 0 0
147  
148 0 my $startindex = 1;
149 0 my $dataoffset = 0x2000;
150 0 my $maxcount = 15;
151
152 0 ▶ for(my $i = $startindex; $i < ($startindex+$maxcount); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $startindex + $maxcount) 0 0 0
153 0 my $entrydata = substr($self->{'contents'}, ($i * 0x80), 0x80);
154 0 my $entry = parse_directory($entrydata);
155 0 my $save;
156 0 ▶ if($entry->{'inuse'} == 0x51) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"inuse"} == 81) 0 0 0
157 $save = {
158 'filename' => $entry->{'codename'},
159 0 'data' => substr($self->{'contents'}, $dataoffset, $entry->{'datasize'}),
160 };
161 }
162 0 $callback->($entry, $save, $entrydata);
163 0 $dataoffset += 0x2000;
164 }
165 }
166  
167 0 ▶sub _readMCSSave {
Subroutine _readMCSSave: not called CC=1 SCAR=6.9
168 0 my ($self) = @_;
169 0 my $entrydata = substr($self->{'contents'}, 0, 0x80);
170 0 my $entry = parse_directory($entrydata);
171  
172 return {
173 'filename' => $entry->{'codename'},
174 0 'data' => substr($self->{'contents'}, 0x80, $entry->{'datasize'})
175 };
176 }
177  
178 0 ▶sub _readRawSave {
Subroutine _readRawSave: not called CC=2 SCAR=17.9
179 0 my ($self) = @_;
180 0 ▶ $self->{'codename'} or die("cannot read raw save without a filename");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"codename"} or die "cannot read raw save without a filename" 0 0 0
181 return {
182 'filename' => $self->{'codename'},
183 0 'data' => $self->{'contents'}
184 };
185 }
186  
187  
188 0 ▶sub readSave {
Subroutine readSave: not called CC=3 SCAR=24.8
Pod undocumented
189 0 my ($self) = @_;
190 0 ▶ if($self->{'type'} eq 'mcs') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'type'} eq 'mcs') { } 0 0 0
elsif ($self->{'type'} eq 'rawsave') { } 0 0 0
191 0 return _readMCSSave($self);
192 }
193 elsif($self->{'type'} eq 'rawsave') {
194 0 return _readRawSave($self);
195 }
196 else {
197 0 die("unimplemented type");
198 }
199 }
200  
201 0 ▶sub FormatSaveFirstDirEntry {
Subroutine FormatSaveFirstDirEntry: not called CC=4 SCAR=30.0
Pod undocumented
202 0 my ($save, $dirindex) = @_;
203 0 my $savelen = length($save->{'data'});
204 0 my $blockcount = length($save->{'data'}) / 0x2000;
205 0 ▶ (($blockcount % 1) == 0) or die("not integer blocksize");
Branch: 1 branch
BranchTrueFalseTotal
$blockcount % 1 == 0 or die "not integer blocksize" 0 0 0
206 0 ▶ ($blockcount >= 1) or die("must have at least one block");
Branch: 1 branch
BranchTrueFalseTotal
$blockcount >= 1 or die "must have at least one block" 0 0 0
207 0 ▶ my $blockptr = ($blockcount == 1) ? 0xFFFF : $dirindex;
Branch: 1 branch
BranchTrueFalseTotal
$blockcount == 1 ? : 0 0 0
208 0 my $directory = pack('VVvZ21x96', 0x51, $savelen, $blockptr, $save->{'filename'});
209 0 $directory .= pack('C', xordirectory($directory));
210  
211 0 return ($directory, $blockcount);
212 }
213  
214 0 ▶sub FormatSaveAsMCD {
Subroutine FormatSaveAsMCD: not called CC=3 SCAR=24.8
Pod undocumented
215 0 my ($dirstart, $save) = @_;
216  
217 0 my $dirindex = ($dirstart / 0x80);
218 0 my ($directory, $blockcount) = FormatSaveFirstDirEntry($save, $dirindex);
219
220 # format the possible mid and end link directories
221 0 ▶ if($blockcount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockcount > 1) 0 0 0
222 0 ▶ while ($blockcount > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockcount > 2) 0 0 0
223 0 $dirindex++;
224 0 my $newdir = pack('VVvx117', 0x52, 0x0, $dirindex);
225 0 $newdir .= pack('C', xordirectory($newdir));
226 0 $directory .= $newdir;
227 0 $blockcount--;
228 }
229 0 my $newdir = pack('VVvx117', 0x53, 0x0, 0xFFFF);
230 0 $newdir .= pack('C', xordirectory($newdir));
231 0 $directory .= $newdir;
232 }
233
234 return {
235 'dirdata' => $directory,
236 0 'savedata' => $save->{'data'}
237 };
238 }
239  
240 0 ▶sub FormatSaveAsMCS {
Subroutine FormatSaveAsMCS: not called CC=1 SCAR=6.9
Pod undocumented
241 0 my ($save) = @_;
242 0 my ($directory, $blockcount) = FormatSaveFirstDirEntry($save, 1);
243 0 return $directory .= $save->{'data'};
244 }
245  
246 0 ▶sub SaveNameAndTitleMatch {
Subroutine SaveNameAndTitleMatch: not called CC=8 SCAR=42.8
Pod undocumented
247 0 my ($save, $string) = @_;
248 0 ▶ $save->{'header'} //= PlayStation::MemoryCard::parse_file_header($save->{'data'});
Condition0%0/3
$save->{"header"} //= parse_file_header($save->{"data"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$save->{"header"} //= parse_file_header($save->{"data"})
$save->{"header"} parse_file_header($save->{"data"})
Truth table0%0/3
A //= B
A= $save->{"header"}B= parse_file_header($save->{"data"})
ABresult
1X1
011
000
249 0 my $title = $save->{'header'}{'title'};
250 0 my $asciititle = $title;
251 0 $asciititle =~ tr/\x{3000}\x{FF01}-\x{FF5E}/ -~/; # fullwidth to half-width if possible
252 #warn("title: $title");
253 #warn("searchfname: $string");
254 #warn("asciititle: $asciititle");
255 return (!$string ||
256 ($save->{'filename'} eq $string) || ($title eq $string) || ($asciititle eq $string) ||
257 0 ▶ ($save->{'filename'} =~ /\Q$string\E/i) || ($title =~ /\Q$string\E/i) || ($asciititle =~ /\Q$string\E/i));
Condition0%0/18
!$string || $save->{'filename'} eq $string || $title eq $string || $asciititle eq $string || $save->{'filename'} =~ /\Q$string\E/i || $title =~ /\Q$string\E/i || $asciititle =~ /\Q$string\E/i
l!l&&r!l&&!r
000
not $string or $save->{"filename"} eq $string or $title eq $string or $asciititle eq $string or $save->{"filename"} =~ /\Q$string\E/i or $title =~ /\Q$string\E/i
l!l&&r!l&&!r
000
not $string or $save->{"filename"} eq $string or $title eq $string or $asciititle eq $string or $save->{"filename"} =~ /\Q$string\E/i
l!l&&r!l&&!r
000
not $string or $save->{"filename"} eq $string or $title eq $string or $asciititle eq $string
l!l&&r!l&&!r
000
not $string or $save->{"filename"} eq $string or $title eq $string
l!l&&r!l&&!r
000
not $string or $save->{"filename"} eq $string
l!l&&r!l&&!r
000
MC/DC0%0/7
!$string || $save->{'filename'} eq $string || $title eq $string || $asciititle eq $string || $save->{'filename'} =~ /\Q$string\E/i || $title =~ /\Q$string\E/i || $asciititle =~ /\Q$string\E/i
not $string $save->{"filename"} eq $string $title eq $string $asciititle eq $string $save->{"filename"} =~ /\Q$string\E/i $title =~ /\Q$string\E/i $asciititle =~ /\Q$string\E/i
Truth table0%0/8
not(A) or B or C or D or E or F || G
A= not $stringB= $save->{"filename"} eq $stringC= $title eq $stringD= $asciititle eq $stringE= $save->{"filename"} =~ /\Q$string\E/iF= $title =~ /\Q$string\E/iG= $asciititle =~ /\Q$string\E/i
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
258 }
259  
260 0 ▶sub BlankMCD {
Subroutine BlankMCD: not called CC=3 SCAR=24.8
Pod undocumented
261 0 my $cardbuf = pack('x131072');
262  
263 # header frame
264 0 substr($cardbuf, 0, 2, 'MC');
265 0 substr($cardbuf, 0x7F, 1, pack('C', 0x0E));
266 # directory frames
267 0 ▶ for(my $i = 1; $i < 16; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 16) 0 0 0
268 0 my $frameoffset = $i*0x80;
269 0 substr($cardbuf, $frameoffset, 1, pack('C', 0xA0));
270 0 substr($cardbuf, $frameoffset+0x8, 2, pack('v', 0xFFFF));
271 0 substr($cardbuf, $frameoffset+0x7F, 1, pack('C', 0xA0));
272 }
273 # broken sector list
274 0 ▶ for(my $i = 16; $i < 36; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 36) 0 0 0
275 0 my $frameoffset = $i*0x80;
276 0 substr($cardbuf, $frameoffset, 4, pack('V', 0xFFFFFFFF));
277 0 substr($cardbuf, $frameoffset+0x8, 2, pack('v', 0xFFFF));
278 }
279 # broken sector replacement data 36-55
280 # unused frames 56-62
281 # write test frame 63
282 # file blocks
283  
284 0 return $cardbuf;
285 }
286  
287 1 1;
288  
289 __END__
↑ Summary