File Coverage

script/tsd-extract
Criterion Covered Total %
statement 60 72 83.3
branch 20 42 47.6
condition 1 3 33.3
subroutine 7 7 100.0
pod n/a
total 88 124 70.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1     1   3612 use strict;
  1         1  
  1         31  
4 1     1   3 use warnings;
  1         1  
  1         39  
5 1     1   596 use Getopt::Long;
  1         11650  
  1         5  
6 1     1   512 use Pod::Usage;
  1         74260  
  1         105  
7 1     1   7 use File::Spec;
  1         3  
  1         30  
8 1     1   3 use File::Path qw(mkpath);
  1         1  
  1         49  
9              
10 1     1   328 use Crypt::TSD;
  1         4  
  1         2246  
11              
12             # Command line options
13 1         169657 my $help = 0;
14 1         1 my $version = 0;
15 1         2 my $output_file;
16 1         4 my $extract_timestamps = 0;
17 1         3 my $timestamp_dir;
18 1         2 my $verbose = 0;
19              
20             # Parse command line options
21 1 50       9 GetOptions(
22             'help|h' => \$help,
23             'version' => \$version,
24             'output|o=s' => \$output_file,
25             'timestamps|t' => \$extract_timestamps,
26             'timestamp-dir|d=s' => \$timestamp_dir,
27             'verbose|v' => \$verbose,
28             ) or pod2usage(2);
29              
30             # Show help
31 1 50       1419 if ($help) {
32 0         0 pod2usage(-verbose => 2);
33 0         0 exit 0;
34             }
35              
36             # Show version
37 1 50       5 if ($version) {
38 0         0 print "tsd-extract version $Crypt::TSD::VERSION\n";
39 0         0 exit 0;
40             }
41              
42             # Get input file from command line
43 1         2 my $input_file = shift @ARGV;
44 1 50       4 if (!$input_file) {
45 0         0 print "Error: Input TSD file required\n";
46 0         0 pod2usage(1);
47             }
48              
49             # Check if input file exists
50 1 50       35 unless (-f $input_file) {
51 0         0 die "Error: Input file '$input_file' not found\n";
52             }
53              
54             # Validate timestamp extraction parameters
55 1 50 33     9 if ($extract_timestamps && !$timestamp_dir) {
56 0         0 die "Error: Timestamp directory (-d) is required when extracting timestamps (-t)\n";
57             }
58              
59 1 50       3 print "Extracting from TSD file...\n" if $verbose;
60 1 50       2 print "Input file: $input_file\n" if $verbose;
61              
62             # Read TSD file
63 1         2 my $tsd_data;
64 1         3 eval {
65 1         15 $tsd_data = Crypt::TSD->read_file($input_file);
66             };
67 1 50       4 if ($@) {
68 0         0 die "Error reading TSD file: $@\n";
69             }
70              
71 1 50       3 print "TSD file read successfully\n" if $verbose;
72 1 50       3 print "Version: " . $tsd_data->{version} . "\n" if $verbose;
73              
74             # Extract content
75 1         5 my $content = Crypt::TSD->extract_content_der($tsd_data);
76 1 50       4 if (defined $content) {
77             # Generate output filename if not provided
78 1 50       24 unless ($output_file) {
79 1         2 $output_file = $input_file;
80 1         8 $output_file =~ s/\.tsd$/.extracted/;
81             }
82            
83 1 50       3 print "Extracting content to: $output_file\n" if $verbose;
84            
85             # Write content to file
86 1 50       239 open my $fh, '>:raw', $output_file or die "Cannot create output file '$output_file': $!\n";
87 1         16 print $fh $content;
88 1         73 close $fh;
89            
90 1         7 print "Content extracted successfully: $output_file\n";
91 1 50       9 print "Content size: " . length($content) . " bytes\n" if $verbose;
92             } else {
93 0         0 print "No embedded content found in TSD file\n";
94             }
95              
96             # Extract timestamps if requested
97 1 50       4 if ($extract_timestamps) {
98 1 50       3 print "Extracting timestamps...\n" if $verbose;
99            
100             # Create timestamp directory
101 1 50       24 unless (-d $timestamp_dir) {
102 0 0       0 mkdir $timestamp_dir or die "Cannot create timestamp directory '$timestamp_dir': $!\n";
103             }
104            
105 1         11 my $token_count = Crypt::TSD->write_tst_files($tsd_data, $timestamp_dir);
106 1 50       4 if ($token_count > 0) {
107 1         5 print "Extracted $token_count timestamp(s) to directory: $timestamp_dir\n";
108             } else {
109 0         0 print "No timestamps found in TSD file\n";
110             }
111             }
112              
113 1         0 print "Done!\n";
114              
115             __END__