]> jfr.im git - irc/SurrealServices/srsv.git/blob - branches/0.5.0/SrSv/Unreal/Base64.pm
79410787eacf66fdb337e6553aeaec2b60089a58
[irc/SurrealServices/srsv.git] / branches / 0.5.0 / SrSv / Unreal / Base64.pm
1 # This file is part of SurrealServices.
2 #
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 package SrSv::Unreal::Base64;
18
19 =head1 NAME
20
21 SrSv::Unreal::Base64 - Implementation of the UnrealIRCd Base64 encoding
22
23 =cut
24
25 use strict;
26 use SrSv::64bit;
27 BEGIN {
28 if(!HAS_64BIT_INT) {
29 require Math::BigInt;
30 import Math::BigInt try => 'GMP';
31 }
32 }
33
34 use Exporter 'import';
35 BEGIN { our @EXPORT_OK = qw(b64toi itob64); }
36
37 # ':' and '#' and '&' and '+' and '@' must never be in this table. */
38 # these tables must NEVER CHANGE! >) */
39 our @int6_to_base64_map = (
40 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
41 'E', 'F',
42 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
43 'U', 'V',
44 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
45 'k', 'l',
46 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
47 '{', '}'
48 );
49
50 our @base64_to_int6_map = (
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
55 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
56 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
57 -1, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
58 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, 63, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
67 );
68
69 *b64toi = \&base64_to_int;
70 sub base64_to_int($) {
71 my ($base64) = @_;
72 my $val = 0;
73 #wKgIAw==
74 if(length($base64) > 8) {
75 warn "greater-than-32bit base64($base64) in base64_to_int";
76 $val = (HAS_64BIT_INT ? 0 : Math::BigInt->bzero());
77 } else {
78 $val = 0;
79 }
80
81 foreach my $ch (split(//, $base64)) {
82 $val <<= 6;
83 $val += $base64_to_int6_map[ord($ch)];
84 }
85 return $val;
86 }
87
88 *itob64 = \&int_to_base64;
89 sub int_to_base64($) {
90 my ($val) = @_;
91
92 my $base64 = '';
93 do {
94 $base64 .= $int6_to_base64_map[$val & 63];
95 } while ($val >>= 6);
96 return scalar reverse($base64);
97 }
98
99 1;
100
101 __END__
102
103 =head1 SYNOPSIS
104
105 use SrSv::Unreal::Base64;
106 $integer = b64toi($base64);
107 $base64 = itob64($integer);
108
109 =head1 NOTES
110
111 As far as I know, all usage of these functions will accept or return
112 a 32-bit integer. The only exception is for IPv6, but NICKIP uses the
113 standard table anyway.