| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Net::FCP::Util - utility functions. | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use Net::FCP::Util; | ||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||
| 11 | |||||||||||
| 12 | =over 4 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | package Net::FCP::Util; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Digest::SHA1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use MIME::Base64 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | no warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | use base Exporter::; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | @EXPORT_OK = qw(tolc touc xeh); | |||||||||
| 27 | |||||||||||
| 28 | =item touc $string | ||||||||||
| 29 | |||||||||||
| 30 | Returns the uppercased version of the given identifier. Used internally to | ||||||||||
| 31 | map from e.g. URIError/KeyCollision to the form used in this module, i.e. | ||||||||||
| 32 | uri_error/key_collision etc. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub touc($) { | ||||||||
|
Subroutine
touc: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 37 | 0 | local $_ = shift; | |||||||||
| 38 | 0 | ▶ | 1 while s/((?:^|_)(?:svk|chk|uri)(?:_|$))/\U$1/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | s/(?:^|_)(.)/\U$1/g; | |||||||||
| 40 | 0 | $_; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | =item tolc $string | ||||||||||
| 44 | |||||||||||
| 45 | Returns the lowercased version of the given identifier. See C<touc>. | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub tolc($) { | ||||||||
|
Subroutine
tolc: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 50 | 0 | local $_ = shift; | |||||||||
| 51 | 0 | ▶ | 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | ▶ | 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | s/(?<=[a-z])(?=[A-Z])/_/g; | |||||||||
| 54 | 0 | lc $_; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | =item xeh $number | ||||||||||
| 58 | |||||||||||
| 59 | The opposite of C<hex>, i.e. returns the the number as hex string. Mainly | ||||||||||
| 60 | useful due to it's prototype of C<$>. | ||||||||||
| 61 | |||||||||||
| 62 | =cut | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub xeh($) { | ||||||||
|
Subroutine
xeh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 65 | 0 | sprintf "%x", $_[0]; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | =item log2 $num[, $minlog] | ||||||||||
| 69 | |||||||||||
| 70 | Calculate the (integer) log2 of a number, rounded up. If C<$minlog> is | ||||||||||
| 71 | given it will be the minimum value returned. | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub log2($;$) { # n, minlog | ||||||||
|
Subroutine
log2: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 76 | 0 | ▶ | $_[0] && length sprintf "%$_[1]b", $_[0] - 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | # the above line is much faster than the equivalent | ||||||||||
| 79 | # below, which illustrates a fine point of perl... | ||||||||||
| 80 | # my ($n, $b) = @_; | ||||||||||
| 81 | # $b++ while 1 << $b < $n; | ||||||||||
| 82 | # $b; | ||||||||||
| 83 | |||||||||||
| 84 | =item encode_mpi $num | ||||||||||
| 85 | |||||||||||
| 86 | Encode the given number as a multiple-precision number (2 byte bitlength + bytes) | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | sub encode_mpi($) { | ||||||||
|
Subroutine
encode_mpi: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 91 | 0 | my $num = pack "N", $_[0]; | |||||||||
| 92 | 0 | my $len = log2 $_[0], 1; | |||||||||
| 93 | 0 | $num =~ s/^\x00+//; | |||||||||
| 94 | 0 | pack "n a*", $len, $num; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | =item decode_base64 $string | ||||||||||
| 98 | |||||||||||
| 99 | Decode freenet's perverted version of base64. | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | sub decode_base64($) { | ||||||||
|
Subroutine
decode_base64: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 104 | 0 | my $s = shift; | |||||||||
| 105 | |||||||||||
| 106 | 0 | $s =~ y%~\-%+/%; | |||||||||
| 107 | 0 | MIME::Base64::decode_base64 "$s======"; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =item encode_base64 $data | ||||||||||
| 111 | |||||||||||
| 112 | Encode into freenet's perverted version of base64. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub encode_base64($) { | ||||||||
|
Subroutine
encode_base64: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 117 | 0 | my $s = MIME::Base64::encode_base64 shift, ""; | |||||||||
| 118 | 0 | $s =~ s/=+$//; | |||||||||
| 119 | 0 | $s =~ y%+/%~\-%; | |||||||||
| 120 | 0 | $s; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | =item generate_chk_hash $metadata, $data | ||||||||||
| 124 | |||||||||||
| 125 | Generate and return they hash portion (the part after the comma, the | ||||||||||
| 126 | crypto key) that would be used in the CHK (as binary). This can be used to | ||||||||||
| 127 | verify contents of a CHK, since this key is a hash over the data. | ||||||||||
| 128 | |||||||||||
| 129 | (This function assumes a 128 bit key, which seems standard in freenet). | ||||||||||
| 130 | |||||||||||
| 131 | =cut | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub generate_chk_hash($$) { | ||||||||
|
Subroutine
generate_chk_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 134 | 0 | my $d = new Digest::SHA1; | |||||||||
| 135 | |||||||||||
| 136 | 0 | $d->add ($_[0]); | |||||||||
| 137 | 0 | $d->add ($_[1]); | |||||||||
| 138 | 0 | $d = $d->digest; | |||||||||
| 139 | |||||||||||
| 140 | 0 | my $k = new Digest::SHA1; | |||||||||
| 141 | 0 | $k->add ("\x00" x 1); # only one iteration | |||||||||
| 142 | 0 | $k->add ($d); | |||||||||
| 143 | |||||||||||
| 144 | 0 | substr $k->digest, 0, 16; # extract leading 128 bit | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | =item extract_chk_hash $uri | ||||||||||
| 148 | |||||||||||
| 149 | Extract the hash portion (the part after the comma, the crypto key) of a | ||||||||||
| 150 | CHK (in binary). Useful to compare against the output of generate_chk_key. | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub extract_chk_hash($) { | ||||||||
|
Subroutine
extract_chk_hash: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 155 | 0 | ▶ | $_[0] =~ /CHK\@[a-zA-Z0-9~\-]{31},([a-zA-Z0-9~\-]{22})/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | or Carp::croak "unable to parse CHK key from '$_[0]'"; | ||||||||||
| 157 | |||||||||||
| 158 | 0 | decode_base64 $1; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | =back | ||||||||||
| 162 | |||||||||||
| 163 | =head1 SEE ALSO | ||||||||||
| 164 | |||||||||||
| 165 | L<Net::FCP>. | ||||||||||
| 166 | |||||||||||
| 167 | =head1 BUGS | ||||||||||
| 168 | |||||||||||
| 169 | Not heavily tested. | ||||||||||
| 170 | |||||||||||
| 171 | =head1 AUTHOR | ||||||||||
| 172 | |||||||||||
| 173 | Marc Lehmann <schmorp@schmorp.de> | ||||||||||
| 174 | http://home.schmorp.de/ | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | |||||||||||
| 178 | 1 | 1; | |||||||||
| 179 | |||||||||||