| 1 | package Convert::Z85; | ||||||||||||
| 2 | 7 | $Convert::Z85::VERSION = '1.001001'; | |||||||||||
| 3 | 7 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use strict; use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 7 | ▶ | use parent 'Exporter::Tiny'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 7 | our @EXPORT = our @EXPORT_OK = qw/ | |||||||||||
| 8 | encode_z85 | ||||||||||||
| 9 | decode_z85 | ||||||||||||
| 10 | /; | ||||||||||||
| 11 | |||||||||||||
| 12 | 7 | require bytes; | |||||||||||
| 13 | |||||||||||||
| 14 | 7 | my @chrs = split '', | |||||||||||
| 15 | '0123456789abcdefghijklmnopqrstuvwxyz' | ||||||||||||
| 16 | .'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||||||||||||
| 17 | .'.-:+=^!/*?&<>()[]{}@%$#' | ||||||||||||
| 18 | ; | ||||||||||||
| 19 | |||||||||||||
| 20 | 7 | my %intforchr = map {; $chrs[$_] => $_ } 0 .. $#chrs; | |||||||||||
| 21 | |||||||||||||
| 22 | 7 | my @multiples = reverse map {; 85 ** $_ } 0 .. 4; | |||||||||||
| 23 | |||||||||||||
| 24 | 9 | ▶ | sub encode_z85 { | ||||||||||
|
Subroutine
encode_z85: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 25 | 9 | my ($bin, %param) = @_; | |||||||||||
| 26 | |||||||||||||
| 27 | 9 | ▶ | my $len = bytes::length($bin) || return ''; | ||||||||||
|
Condition50%1/2
&bytes::length($bin) || (return '')
MC/DC0%0/1
&bytes::length($bin) || (return '')
&bytes::length($bin)
Truth table50%1/2
A
A= &bytes::length($bin)
| |||||||||||||
| 28 | 9 | ▶ | if ($param{pad}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 4 | $bin .= "\0" x (-$len % 4); | |||||||||||
| 30 | 4 | $len = bytes::length($bin); | |||||||||||
| 31 | } | ||||||||||||
| 32 | 9 | ▶ | croak "Expected data padded to 4-byte chunks; got length $len" if $len % 4; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | |||||||||||||
| 34 | 8 | my $chunks = $len / 4; | |||||||||||
| 35 | 8 | my @values = unpack "(N)$chunks", $bin; | |||||||||||
| 36 | |||||||||||||
| 37 | 8 | my $str; | |||||||||||
| 38 | 8 | for my $val (@values) { | |||||||||||
| 39 | 645 | $str .= $chrs[ int($val / $_) % 85 ] for @multiples; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | $str | ||||||||||||
| 43 | 8 | } | |||||||||||
| 44 | |||||||||||||
| 45 | 8 | ▶ | sub decode_z85 { | ||||||||||
|
Subroutine
decode_z85: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 46 | 8 | my ($txt, %param) = @_; | |||||||||||
| 47 | 8 | ▶ | my $len = length $txt || return ''; | ||||||||||
|
Condition50%1/2
length $txt || (return '')
MC/DC0%0/1
length $txt || (return '')
length $txt
Truth table50%1/2
A
A= length $txt
| |||||||||||||
| 48 | |||||||||||||
| 49 | 8 | ▶ | croak "Expected Z85 text in 5-byte chunks; got length $len" if $len % 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | |||||||||||||
| 51 | 7 | my @values; | |||||||||||
| 52 | 7 | for my $idx (grep {; not($_ % 5) } 0 .. $len) { | |||||||||||
| 53 | 444 | my ($val, $cnt) = (0, 0); | |||||||||||
| 54 | |||||||||||||
| 55 | 444 | for my $mult (@multiples) { | |||||||||||
| 56 | 2192 | my $chr = substr $txt, ($idx + $cnt), 1; | |||||||||||
| 57 | 2192 | ▶ | last unless length $chr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | croak "Invalid Z85 input; '$chr' not recognized" | ||||||||||||
| 59 | 2185 | ▶ | unless exists $intforchr{$chr}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 2185 | $val += $intforchr{$chr} * $mult; | |||||||||||
| 61 | 2185 | ++$cnt; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 444 | push @values, $val; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 7 | my $chunks = $len / 5; | |||||||||||
| 68 | 7 | my $ret = pack "(N)$chunks", @values; | |||||||||||
| 69 | 7 | ▶ | $ret =~ s/\0{0,3}$// if $param{pad}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 7 | $ret | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | |||||||||||||
| 74 | 7 | 1; | |||||||||||
| 75 | |||||||||||||
| 76 | =pod | ||||||||||||
| 77 | |||||||||||||
| 78 | =head1 NAME | ||||||||||||
| 79 | |||||||||||||
| 80 | Convert::Z85 - Encode and decode Z85 strings | ||||||||||||
| 81 | |||||||||||||
| 82 | =head1 SYNOPSIS | ||||||||||||
| 83 | |||||||||||||
| 84 | use Convert::Z85; | ||||||||||||
| 85 | |||||||||||||
| 86 | my $encoded = encode_z85($binarydata); | ||||||||||||
| 87 | my $decoded = decode_z85($encoded); | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||||
| 90 | |||||||||||||
| 91 | An implementation of the I<Z85> encoding scheme (as described in | ||||||||||||
| 92 | L<ZeroMQ spec 32|http://rfc.zeromq.org/spec:32>) for encoding binary data as | ||||||||||||
| 93 | plain text. | ||||||||||||
| 94 | |||||||||||||
| 95 | Modelled on the L<PyZMQ|http://zeromq.github.io/pyzmq/> implementation. | ||||||||||||
| 96 | |||||||||||||
| 97 | This module uses L<Exporter::Tiny> to export two functions by default: | ||||||||||||
| 98 | L</encode_z85> and L</decode_z85>. | ||||||||||||
| 99 | |||||||||||||
| 100 | =head2 encode_z85 | ||||||||||||
| 101 | |||||||||||||
| 102 | my $z85 = encode_z85($data); | ||||||||||||
| 103 | |||||||||||||
| 104 | Takes binary data in 4-byte chunks and returns a Z85-encoded text string. | ||||||||||||
| 105 | |||||||||||||
| 106 | Per the spec, padding is not performed automatically; the B<pad> option can be | ||||||||||||
| 107 | specified to pad data with trailing zero bytes: | ||||||||||||
| 108 | |||||||||||||
| 109 | my $z85 = encode_z85($data, pad => 1); | ||||||||||||
| 110 | |||||||||||||
| 111 | =head2 decode_z85 | ||||||||||||
| 112 | |||||||||||||
| 113 | my $bin = decode_z85($encoded); | ||||||||||||
| 114 | |||||||||||||
| 115 | Takes a Z85 text string and returns the original binary data. | ||||||||||||
| 116 | |||||||||||||
| 117 | Dies (with a stack trace) if invalid data is encountered. | ||||||||||||
| 118 | |||||||||||||
| 119 | Padding (see L</encode_z85>) is not handled automatically; the B<pad> option | ||||||||||||
| 120 | can be specified to remove trailing zero bytes: | ||||||||||||
| 121 | |||||||||||||
| 122 | my $bin = decode_z85($encoded, pad => 1); | ||||||||||||
| 123 | |||||||||||||
| 124 | =head1 SEE ALSO | ||||||||||||
| 125 | |||||||||||||
| 126 | L<Convert::Ascii85> | ||||||||||||
| 127 | |||||||||||||
| 128 | L<POEx::ZMQ> | ||||||||||||
| 129 | |||||||||||||
| 130 | L<ZMQ::FFI> | ||||||||||||
| 131 | |||||||||||||
| 132 | =head1 AUTHOR | ||||||||||||
| 133 | |||||||||||||
| 134 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||