File: cd-read.pl

package info (click to toggle)
libdevice-cdio-perl 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 2,852 kB
  • sloc: ansic: 14,413; perl: 4,795; makefile: 19; sh: 3
file content (167 lines) | stat: -rwxr-xr-x 4,371 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl -w
#
#  Copyright (C) 2006, 2008, 2011 Rocky Bernstein <rocky@cpan.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Program to read CD blocks. See read-cd from the libcdio distribution
# for a more complete program.

use strict;

BEGIN {
    chdir 'example' if -d 'example';
    use lib '../lib';
    eval "use blib";  # if we fail keep going - maybe we have installed Cdio
}

use Device::Cdio;
use Device::Cdio::Device;

use vars qw($0 $program %opts $read_mode);

my %read_modes = (
    'audio'=> $perlcdio::READ_MODE_AUDIO,
    'm1f1' => $perlcdio::READ_MODE_M1F1,
    'm1f2' => $perlcdio::READ_MODE_M1F2,
    'm2f1' => $perlcdio::READ_MODE_M2F1,
    'm2f2' => $perlcdio::READ_MODE_M2F2,
    'data' => undef
		  );

sub init() {
  use File::Basename;
  $program     = basename($0); # Who am I today, anyway?
  $opts{start} = 1;
  $opts{number}= 1;
  $opts{mode}  = undef;
  $read_mode   = undef;
}

# Show the CVS version id string and quit.
sub show_version() {
    print "$program version $Device::Cdio::VERSION\n";
    exit 1;
}

sub usage {
  my($full_help) = @_;

  print "
usage:

   $program --mode [mode] [other options] [device]
    Read blocks of a CD or CD image.

options:
    --help          -- print this help and exit
    --mode *name*   -- CD Reading mode: audio, m1f1, m1f2, m2f1 or m2f2
    --start *n*     -- starting block number. Default 1.
    --number *n*    -- Number fo block. Default 1.
";
  exit 100;
}

# The bane of programming.
sub process_options() {
  use Getopt::Long;
  my(@opt_cf);
  $Getopt::Long::autoabbrev = 1;
  my($help, $long_help, $show_version);

  my $result = &GetOptions
    (
     'help'           => \$help,
     'mode=s'         => \$opts{mode},
     'start=n'        => \$opts{start},
     'number=n'       => \$opts{number},
    );
  show_version() if $show_version;

  usage(0) unless $result || @ARGV > 1;
  usage(1) if $help;

  if (!$opts{mode} ) {
      print "Read mode option must given.\n"; 
      exit 1
  } 
  $read_mode = $read_modes{$opts{mode}};
  if (!defined($read_mode)) {
      print "Read mode must be one of audio, m1f1, m1f2, m1f2 or m1f2.\n";
      exit 1;
  }
}

# Standard C library's isprint() in Ruby
sub isprint($) {
    return $_[0] =~ m{^[: -~]$} ;
}

# Print a hex dump and string interpretation of buffer.
# If just_hex is true, then show only the hex dump.
sub hexdump ($) {
    my $buffer = $_[0];
    foreach my $i (0 .. length($buffer) - 1) {
	printf "0x%04x: ", $i if $i % 16 == 0;
	printf "%02x", ord(substr($buffer, $i, 1));
	printf " " if $i % 2 == 1;
	if ($i % 16 == 15) {
	    printf "  ";
	    foreach my $j ($i-15 .. $i) {
		printf "%s", 
		isprint(substr($buffer, $j, 1)) ?
		    substr($buffer, $j, 1) : '.';
	    }
	    print "\n";
	}
    }
    print "\n";
}

init();
process_options();

my ($drive_name, $d);
if ($ARGV[0]) {
    $drive_name=$ARGV[0];
    $d = Device::Cdio::Device->new(-source=>$drive_name);
    if (!defined($drive_name)) {
	print "Problem opening CD-ROM: $drive_name\n";
	exit(1);
    }
} else {
    $d = Device::Cdio::Device->new(-driver_id=>$perlcdio::DRIVER_DEVICE);
    $drive_name = $d->get_device();
    if (!defined($drive_name)) {
        print "Problem finding a CD-ROM\n";
        exit(1);
    }
}

## All this setup just to issue this one of these commands.
my ($data, $size, $drc);
if (defined($read_mode)) {
    ($data, $size, $drc) =
	$d->read_sectors($opts{start}, $read_mode, $opts{number});
} else {
    ($data, $size, $drc) = 
	$d->read_data_blocks($opts{start}, $opts{number});
}
if ($perlcdio::DRIVER_OP_SUCCESS == $drc) {
    hexdump($data);
} else {
    print "Error reading block $opts{start} mode $opts{mode} on $drive_name\n";
    printf "Return code message: %s", Device::Cdio::driver_strerror($drc);
}