Browse Source

* Subtitle::SSA::File : don't decode uue by default, store as-is

master
Alex 'AdUser' Z 6 years ago
parent
commit
8edb4b49ba
  1. 2
      lib/Subtitle/SSA.pm
  2. 64
      lib/Subtitle/SSA/File.pm
  3. 6
      t/format-ssa-file.t

2
lib/Subtitle/SSA.pm

@ -82,7 +82,7 @@ sub parse {
} }
my $curr = $self->{fonts}->[-1] my $curr = $self->{fonts}->[-1]
or next; # missing 'fontname' line? or next; # missing 'fontname' line?
$curr->parse_uue_line($line); $curr->add_uue_line($line);
next; next;
} }
# TODO: graphics section # TODO: graphics section

64
lib/Subtitle/SSA/File.pm

@ -21,7 +21,7 @@ sub new {
sub size { sub size {
my ($self) = @_; my ($self) = @_;
return length $self->{data}; return _length_bytes($self->{data});
} }
## accessors / setters ## accessors / setters
@ -67,51 +67,51 @@ sub name {
## import functions ## import functions
sub load { sub from_binary {
my ($self, $path) = @_; my ($self, $bin) = @_;
open my $FH, '<', $path or return unless defined $bin and length($bin) > 0;
return $self->error("can't open file: $path -- $!"); $self->{data} = _encode_uue($bin);
local $/ = undef;
$self->{data} = <$FH>;
close $FH;
return 1; return 1;
} }
sub parse_uue_line { sub add_uue_line {
my ($self, $line) = @_; my ($self, $line) = @_;
my $len = length($line);
# 1-char uue chunk with 6 significant bits can't hold 1 byte with 8 bits
return $self->error("bad uuencoded data (odd length)\n") if ($len == 80) {
if length($line) % 4 == 1; # optimization
$self->{data} .= $line;
# decode uue to binary form return 60;
my $bytes = _decode_uue($line); } elsif ($len % 4 == 1) {
$self->{data} .= $bytes; # 1-char uue chunk with 6 significant bits can't hold 1 byte with 8 bits
return $self->error("bad uuencoded data (odd length)\n");
return length $bytes; } elsif ($len == 0) {
return 0;
} # else...
chomp $line;
$self->{data} .= $line;
return _length_bytes($line);
} }
## export functions ## export functions
sub binary { sub to_binary {
my ($self, $bin) = @_; my ($self) = @_;
if (defined $bin) { return _decode_uue($self->{data});
$self->{data} = $bin;
}
return $self->{data};
} }
sub uue_line { sub uue_string {
my ($self) = @_; my ($self) = @_;
return _encode_uue($self->{data}); return $self->{data};
} }
sub uue_block { sub uue_block {
my ($self) = @_; my ($self) = @_;
my $uue = $self->uue_line; my $uue = $self->uue_string;
my $cap = ($self->{type} eq 'font') ? 'fontname' : 'filename'; my $cap = ($self->{type} eq 'font') ? 'fontname' : 'filename';
my $out = sprintf '%s: %s%s', $cap, $self->{name}, $self->{eol}; my $out = sprintf '%s: %s%s', $cap, $self->{name}, $self->{eol};
while (my $line = substr($uue, 0, 80, '')) { while (my $line = substr($uue, 0, 80, '')) {
@ -124,6 +124,14 @@ sub uue_block {
## private UUE functions ## private UUE functions
sub _length_bytes {
my ($uue) = @_;
my $len = length $uue;
my $tail = $len % 4;
my $size = int($len / 4) * 3;
return $size + int(($tail * 6) / 8);
}
sub _decode_uue { sub _decode_uue {
my ($uue) = @_; my ($uue) = @_;
my $bytes = ''; my $bytes = '';

6
t/format-ssa-file.t

@ -17,15 +17,15 @@ sub random_data {
my $file = Subtitle::SSA::File->new; my $file = Subtitle::SSA::File->new;
is(ref $file, 'Subtitle::SSA::File'); is(ref $file, 'Subtitle::SSA::File');
can_ok($file, qw(new error size type name parse_uue_line binary uue_line uue_block)); can_ok($file, qw(new error size type name from_binary add_uue_line to_binary uue_string uue_block));
undef $file; undef $file;
for (my $i = 0; $i < 15; $i++) { for (my $i = 0; $i < 15; $i++) {
my $bin = random_data(); my $bin = random_data();
$file = Subtitle::SSA::File->new; $file = Subtitle::SSA::File->new;
$file->binary($bin); $file->from_binary($bin);
is($file->size => length $bin); is($file->size => length $bin);
my $uue = $file->uue_line; my $uue = $file->uue_string;
isnt($uue, ''); isnt($uue, '');
ok(length($uue) % 4 != 1); ok(length($uue) % 4 != 1);
my $invalid = 0; my $invalid = 0;

Loading…
Cancel
Save