]>
jfr.im git - irc/SurrealServices/srsv.git/blob - branches/0.5.0/SrSv/Unreal/Base64.pm
79410787eacf66fdb337e6553aeaec2b60089a58
1 # This file is part of SurrealServices.
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.
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.
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
17 package SrSv
::Unreal
::Base64
;
21 SrSv::Unreal::Base64 - Implementation of the UnrealIRCd Base64 encoding
30 import Math
::BigInt
try => 'GMP';
34 use Exporter
'import';
35 BEGIN { our @EXPORT_OK = qw(b64toi itob64); }
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',
42 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
44 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
46 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
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
69 *b64toi
= \
&base64_to_int
;
70 sub base64_to_int
($) {
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());
81 foreach my $ch (split(//, $base64)) {
83 $val += $base64_to_int6_map[ord($ch)];
88 *itob64
= \
&int_to_base64
;
89 sub int_to_base64
($) {
94 $base64 .= $int6_to_base64_map[$val & 63];
96 return scalar reverse($base64);
105 use SrSv::Unreal::Base64;
106 $integer = b64toi($base64);
107 $base64 = itob64($integer);
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.