| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::base64 Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::Base64; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | encoding => [ qw(utf8|ascii) ], | ||
| 20 | }, | ||
| 21 | attributes_default => { | ||
| 22 | encoding => 'ascii', | ||
| 23 | }, | ||
| 24 | commands => { | ||
| 25 | decode => [ qw(input output) ], | ||
| 26 | decode_from_string => [ qw(input_string output) ], | ||
| 27 | encode => [ qw(input output) ], | ||
| 28 | encode_from_string => [ qw(input_string output) ], | ||
| 29 | }, | ||
| 30 | require_modules => { | ||
| 31 | 'Metabrik::File::Raw' => [ ], | ||
| 32 | 'Metabrik::String::Base64' => [ ], | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub decode { | ||
| 38 | my $self = shift; | ||
| 39 | my ($input, $output) = @_; | ||
| 40 | |||
| 41 | $self->brik_help_run_undef_arg('decode', $input) or return; | ||
| 42 | $self->brik_help_run_file_not_found('decode', $input) or return; | ||
| 43 | $self->brik_help_run_undef_arg('decode', $output) or return; | ||
| 44 | |||
| 45 | my $fr_in = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 46 | $fr_in->encoding($self->encoding); | ||
| 47 | my $string = $fr_in->read($input) or return; | ||
| 48 | |||
| 49 | my $sb = Metabrik::String::Base64->new_from_brik_init($self) or return; | ||
| 50 | my $decoded = $sb->decode($string) or return; | ||
| 51 | |||
| 52 | my $fr_out = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 53 | $fr_out->encoding($self->encoding); | ||
| 54 | $fr_out->overwrite(1); | ||
| 55 | $fr_out->append(0); | ||
| 56 | $fr_out->write($decoded, $output) or return; | ||
| 57 | |||
| 58 | return $output; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub decode_from_string { | ||
| 62 | my $self = shift; | ||
| 63 | my ($input_string, $output) = @_; | ||
| 64 | |||
| 65 | $self->brik_help_run_undef_arg('decode_from_string', $input_string,) or return; | ||
| 66 | $self->brik_help_run_undef_arg('decode_from_string', $output) or return; | ||
| 67 | |||
| 68 | my $sb = Metabrik::String::Base64->new_from_brik_init($self) or return; | ||
| 69 | my $decoded = $sb->decode($input_string) or return; | ||
| 70 | |||
| 71 | my $fr = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 72 | $fr->encoding($self->encoding); | ||
| 73 | $fr->overwrite(1); | ||
| 74 | $fr->append(0); | ||
| 75 | $fr->write($decoded, $output) or return; | ||
| 76 | |||
| 77 | return $output; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub encode { | ||
| 81 | my $self = shift; | ||
| 82 | my ($input, $output) = @_; | ||
| 83 | |||
| 84 | $self->brik_help_run_undef_arg('encode', $input) or return; | ||
| 85 | $self->brik_help_run_file_not_found('encode', $input) or return; | ||
| 86 | $self->brik_help_run_undef_arg('encode', $output) or return; | ||
| 87 | |||
| 88 | my $fr_in = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 89 | $fr_in->encoding($self->encoding); | ||
| 90 | my $string = $fr_in->read($input) or return; | ||
| 91 | |||
| 92 | my $sb = Metabrik::String::Base64->new_from_brik_init($self) or return; | ||
| 93 | my $encoded = $sb->encode($string) or return; | ||
| 94 | |||
| 95 | my $fr_out = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 96 | $fr_out->encoding($self->encoding); | ||
| 97 | $fr_out->overwrite(1); | ||
| 98 | $fr_out->append(0); | ||
| 99 | $fr_out->write($encoded, $output) or return; | ||
| 100 | |||
| 101 | return $output; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub encode_from_string { | ||
| 105 | my $self = shift; | ||
| 106 | my ($input_string, $output) = @_; | ||
| 107 | |||
| 108 | $self->brik_help_run_undef_arg('encode_from_string', $input_string) or return; | ||
| 109 | $self->brik_help_run_file_not_found('encode_from_string', $input_string) or return; | ||
| 110 | $self->brik_help_run_undef_arg('encode_from_string', $output) or return; | ||
| 111 | |||
| 112 | my $sb = Metabrik::String::Base64->new_from_brik_init($self) or return; | ||
| 113 | my $encoded = $sb->encode($input_string) or return; | ||
| 114 | |||
| 115 | my $fr = Metabrik::File::Raw->new_from_brik_init($self) or return; | ||
| 116 | $fr->encoding($self->encoding); | ||
| 117 | $fr->overwrite(1); | ||
| 118 | $fr->append(0); | ||
| 119 | $fr->write($encoded, $output) or return; | ||
| 120 | |||
| 121 | return $output; | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ | ||