line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIME::Base64::URLSafe; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
29761
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
48
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA @EXPORT $VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
86
|
|
5
|
1
|
|
|
1
|
|
1035
|
use MIME::Base64; |
|
1
|
|
|
|
|
964
|
|
|
1
|
|
|
|
|
306
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
@EXPORT = qw(urlsafe_b64encode urlsafe_b64decode); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.01'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub encode ($) { |
14
|
6
|
|
|
6
|
0
|
40
|
my $data = encode_base64($_[0], ''); |
15
|
6
|
|
|
|
|
12
|
$data =~ tr|+/=|\-_|d; |
16
|
6
|
|
|
|
|
41
|
$data; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub decode ($) { |
20
|
10
|
|
|
10
|
0
|
24
|
my $data = $_[0]; |
21
|
|
|
|
|
|
|
# +/ should not be handled, so convert them to invalid chars |
22
|
|
|
|
|
|
|
# also, remove spaces (\t..\r and SP) so as to calc padding len |
23
|
10
|
|
|
|
|
17
|
$data =~ tr|\-_\t-\x0d |+/|d; |
24
|
10
|
|
|
|
|
22
|
my $mod4 = length($data) % 4; |
25
|
10
|
100
|
|
|
|
27
|
if ($mod4) { |
26
|
9
|
|
|
|
|
21
|
$data .= substr('====', $mod4); |
27
|
|
|
|
|
|
|
} |
28
|
10
|
|
|
|
|
57
|
decode_base64($data); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
*urlsafe_b64encode = \&encode; |
32
|
|
|
|
|
|
|
*urlsafe_b64decode = \&decode; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
1; |
36
|
|
|
|
|
|
|
__END__ |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 NAME |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
MIME::Base64::URLSafe - Perl version of Python's URL-safe base64 codec |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 SYNOPSIS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use MIME::Base64::URLSafe; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$encoded = urlsafe_b64encode('Alladdin: open sesame'); |
47
|
|
|
|
|
|
|
$decoded = urlsafe_b64decode($encoded); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 DESCRIPTION |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This module is a perl version of python's URL-safe base64 encoder / decoder. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
When embedding binary data in URL, it is preferable to use base64 encoding. However, two characters ('+' and '/') used in the standard base64 encoding have special meanings in URLs, often leading to re-encoding with URL-encoding, or worse, interoperability problems. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
To overcome the problem, the module provides a variation of base64 codec compatible with python's urlsafe_b64encode / urlsafe_b64decode. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Modification rules from base64: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
use '-' and '_' instead of '+' and '/' |
60
|
|
|
|
|
|
|
no line feeds |
61
|
|
|
|
|
|
|
no trailing equals (=) |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
The following functions are provided: |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
urlsafe_b64encode($str) |
66
|
|
|
|
|
|
|
urlsafe_b64decode($str) |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
If you prefer not to import these routines to your namespace, you can call them as: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
use MIME::Base64::URLSafe (); |
71
|
|
|
|
|
|
|
$encoded = MIME::Base64::URLSafe::encode($decoded); |
72
|
|
|
|
|
|
|
$decoded = MIME::Base64::URLSafe::decode($encoded); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 SEE ALSO |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
L<MIME::Base64> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Fore more discussion on using base64 encoding in URL applications, see: http://en.wikipedia.org/wiki/Base64#URL_Applications |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 AUTHOR |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Kazuho Oku E<lt>kazuho ___at___ labs.cybozu.co.jpE<gt> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Copyright (C) 2006 Cybozu Labs, Inc. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
87
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.7 or, |
88
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |