MediaWiki REL1_32
base32.php
Go to the documentation of this file.
1<?php
24class Base32 {
25
26 private static $lut = array(
27 "A" => 0, "B" => 1,
28 "C" => 2, "D" => 3,
29 "E" => 4, "F" => 5,
30 "G" => 6, "H" => 7,
31 "I" => 8, "J" => 9,
32 "K" => 10, "L" => 11,
33 "M" => 12, "N" => 13,
34 "O" => 14, "P" => 15,
35 "Q" => 16, "R" => 17,
36 "S" => 18, "T" => 19,
37 "U" => 20, "V" => 21,
38 "W" => 22, "X" => 23,
39 "Y" => 24, "Z" => 25,
40 "2" => 26, "3" => 27,
41 "4" => 28, "5" => 29,
42 "6" => 30, "7" => 31
43 );
44
48 public static function decode($b32) {
49
50 $b32 = strtoupper($b32);
51
52 if (!preg_match('/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+$/', $b32, $match))
53 throw new Exception('Invalid characters in the base32 string.');
54
55 $l = strlen($b32);
56 $n = 0;
57 $j = 0;
58 $binary = "";
59
60 for ($i = 0; $i < $l; $i++) {
61 // Move buffer left by 5 to make room
62 $n = $n << 5;
63 // Add value into buffer
64 $n = $n + self::$lut[$b32[$i]];
65 // Keep track of number of bits in buffer
66 $j = $j + 5;
67
68 if ($j >= 8) {
69 $j = $j - 8;
70 $binary .= chr(($n & (0xFF << $j)) >> $j);
71 }
72 }
73
74 return $binary;
75 }
76
80 public static function encode($string) {
81
82 if (empty($string))
83 throw new Exception('Empty string.');
84
85 $b32 = "";
86 $binary = "";
87
88 $bytes = str_split($string);
89 $length = count( $bytes );
90 for ($i = 0; $i < $length; $i++) {
91 $bits = base_convert(ord($bytes[$i]), 10, 2);
92 $binary .= str_pad($bits, 8, '0', STR_PAD_LEFT);
93 }
94
95 $map = array_keys(self::$lut);
96 $fivebits = str_split($binary, 5);
97 $length = count( $fivebits );
98 for ($i = 0; $i < $length; $i++) {
99 $dec = base_convert(str_pad($fivebits[$i], 5, '0'), 2, 10);
100 $b32 .= $map[$dec];
101 }
102
103 return $b32;
104 }
105}
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition base32.php:24
static $lut
Definition base32.php:26
static decode($b32)
Decodes a base32 string into a binary string according to RFC 4648.
Definition base32.php:48
static encode($string)
Encodes a binary string into a base32 string according to RFC 4648 (no padding).
Definition base32.php:80
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))