This file is indexed.

/usr/share/perl5/EBook/Tools/MSReader.pm is in libebook-tools-perl 0.4.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package EBook::Tools::MSReader;
use warnings; use strict; use utf8;
use English qw( -no_match_vars );
use version 0.74; our $VERSION = qv("0.4.8");

# Perl Critic overrides:
## no critic (Package variable)
# RequireBriefOpen seems to be way too brief to be useful
## no critic (RequireBriefOpen)
# Double-sigils are needed for lexical filehandles in clear print statements
## no critic (Double-sigil dereference)

=head1 NAME

EBook::Tools::MSReader - Helper code for working with Microsoft Reader (.lit) e-books.

=head1 SYNOPSIS

 use EBook::Tools::MSReader qw(find_convertlit find_convertlit_keys
                               system_convertlit);
 $EBook::Tools::MSReader::convertlit_cmd = '/opt/convertlit/clit';
 $EBook::Tools::MSReader::convertlit_keys = '/opt/convertlit/keys.txt';

 my $convertlit = find_convertlit();
 my $keyfile = find_convertlit_keys();
 system_convertlit(infile => 'myfile.lit',
                   dir => 'myfile-unpacked');

=cut

require Exporter;
use base qw(Exporter);

our @EXPORT_OK;
@EXPORT_OK = qw (
    &find_convertlit
    &find_convertlit_keys
    &system_convertlit
    );
our %EXPORT_TAGS = ('all' => [@EXPORT_OK]);

use Carp;
use EBook::Tools qw(debug userconfigdir);
use Encode;
use File::Basename qw(dirname fileparse);
use File::Path;     # Exports 'mkpath' and 'rmtree'
binmode(STDERR,":utf8");

my $drmsupport = 0;
eval
{
    require EBook::Tools::DRM;
    EBook::Tools::DRM->import();
}; # Trailing semicolon is required here
unless($@){ $drmsupport = 1; }


our $convertlit_cmd = '';
our $convertlit_keys = '';


################################
########## PROCEDURES ##########
################################

=head1 PROCEDURES

All procedures are exportable, but none are exported by default.


=head2 C<find_convertlit()>

Attempts to locate the convertlit executable by making a test
execution on predicted locations (including just checking PATH) and
looking in the EBook::Tools user configuration directory (see
L<EBook::Tools/userconfigdir()>.

Returns the system command used for a successful invocation, or undef
if nothing worked.

This will use package variable C<$convertlit_cmd> as its first guess,
and set that variable to the return value as well.

=cut

sub find_convertlit
{
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my @convertlit_guesses;
    my $retval;
    my $confdir = userconfigdir();

    if($OSNAME eq 'MSWin32')
    {
        @convertlit_guesses = (
            'clit',
            'C:\Program Files\ConvertLIT\clit',
            );
        if($confdir)
        {
            push(@convertlit_guesses,
                 $confdir . '\clit',
                 $confdir . '\convertlit');
        }
    }
    else
    {
        @convertlit_guesses = (
            'clit',
            'convertlit',
            );
        if($confdir)
        {
            push(@convertlit_guesses,
                 $confdir . "/clit",
                 $confdir . "/convertlit");
        }
        push(@convertlit_guesses,
             '/opt/convertlit/clit',
             '/opt/convertlit/convertlit',
             '/opt/clit/clit',
             '/opt/clit/convertlit'
            );
    }
    unshift(@convertlit_guesses,$convertlit_cmd)
        if($convertlit_cmd);
    undef($convertlit_cmd);

    foreach my $guess (@convertlit_guesses)
    {
        no warnings 'exec';
        `$guess`;
        # MS Windows may use 256 for a not-found code instead of -1
        if($? != -1 && $? != 256)
        {
            debug(2,'DEBUG: `',$guess,'` returned ',$?);
            $convertlit_cmd = $guess;
            last;
        }
    }

    if($convertlit_cmd)
    {
        debug(1,"DEBUG: Found convertlit as '",$convertlit_cmd,"'");
        return $convertlit_cmd;
    }
    else { return; }
}


=head2 C<find_convertlit_keys($filename)>

Attempts to locate the convertlit C<keys.txt> file by checking
predicted filenames, both in the current working directory and in the
EBook::Tools user configuration directory (see
L<EBook::Tools/userconfigdir()>.

If C<$filename> is provided, the file C<basename-keys.txt> will also
be checked in both locations.

Returns the name of the first file found, or undef if nothing was found.

This will use package variable C<$convertlit_keys> as its first guess,
and set that variable to the return value as well.

=cut

sub find_convertlit_keys
{
    my $filename = shift;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my $basename;
    $basename = fileparse($filename,'\.\w+$') if($filename);
    my $confdir = userconfigdir();
    my $keyfile;

    my @guesses = ( 'keys.txt' );
    push(@guesses, $basename . '-keys.txt') if($basename);
    push(@guesses, $confdir . '/keys.txt') if($confdir);
    push(@guesses, $confdir . '/' . $basename . '-keys.txt')
        if($basename && $confdir);
    unshift(@guesses,$convertlit_keys) if($convertlit_keys);

    foreach my $guess (@guesses)
    {
        if(-f $guess)
        {
            $keyfile = $guess;
            debug(1,"DEBUG: found convertlit keys in '",$keyfile,"'");
            last;
        }
    }
    if($keyfile)
    {
        $convertlit_keys = $keyfile;
        return $keyfile;
    }
    else { return; }
}


=head2 C<system_convertlit(%args)>

Runs C<convertlit> to extract or downconvert a MS Reader .lit file.
The procedures L<find_convertlit()> and L<find_convertlit_keys()> are
both called to locate necessary helper files.

Returns the return value from convertlit, or undef if convertlit or
the input file could not be found, or neither output file nor
directory is specified.

=head3 Arguments

=over

=item * C<infile>

The input filename.  If not specified or invalid, the procedure croaks.

=item * C<outfile>

The output filename.  If this is specified convertlit will perform a
downconversion.

=item * C<dir>

The output directory.  If this is specified, and C<outfile> is not,
convertlit will perform an extraction.  If both this and C<outfile>
are specified, convertlit will downconvert and place the downconverted
file into the specified directory.

=item * C<keyfile>

The location of the C<keys.txt> file containing the encryption keys,
if available.  This is only required if the C<.lit> file is
DRM-protected and package variable C<$convertlit_keys> does not point
to the correct file.

=back

=cut

sub system_convertlit
{
    my %args = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my %valid_args = (
        'infile' => 1,
        'outfile' => 1,
        'keyfile' => 1,
        'dir' => 1,
        );
    foreach my $arg (keys %args)
    {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    if(!$args{infile})
    {
        debug(1,$subname,"(): no input file specified!");
        return;
    }
    if(! -f $args{infile})
    {
        debug(1,$subname,"(): input file '",$args{infile},"' not found!");
        return;
    }

    find_convertlit();
    find_convertlit_keys();
    croak($subname,"(): convertlit command not specified!\n")
        unless($convertlit_cmd);

    my @convertlit = ($convertlit_cmd);
    my $retval;
    my $keyfile = $args{keyfile} || $convertlit_keys;
    my $outfile = $args{outfile};
    my $dir = $args{dir};

    if($keyfile)
    {
        push(@convertlit,"-k$keyfile");
    }

    push(@convertlit,$args{infile});

    if($outfile && $dir)
    {
        push(@convertlit,"$dir/$outfile");
    }
    elsif($outfile)
    {
        push(@convertlit,$outfile);
    }
    elsif($dir)
    {
        # Expansion into a directory requires a trailing slash
        unless($dir =~ m{(/ | \\) $}x)
        {
            $dir .= '/';
        }
        push(@convertlit,$dir);
    }
    else
    {
        debug(1,$subname,"(): neither output file nor directory specified!");
        return;
    }

    debug(2,"DEBUG: converting lit with '",join(' ',@convertlit),"'");
    $retval = system(@convertlit);
    return $retval;
}

########## END CODE ##########

=head1 BUGS AND LIMITATIONS

=over

=item * All handling happens through ConvertLIT as an external helper.
Native Perl code may eventually be written to handle non-DRMed
extraction.

=item * Unit tests are unwritten

=back

=head1 AUTHOR

Zed Pobre <zed@debian.org>

=head1 LICENSE AND COPYRIGHT

Copyright 2008 Zed Pobre

Licensed to the public under the terms of the GNU GPL, version 2.

ConvertLIT (not included) is copyright 2002, 2003 Dan A. Jackson, and
licensed under the terms of the GNU GPL, version 2 or later.

=cut

1;
__END__