Repository navigation
Expand file tree
/
Copy pathbase64_lib.php
More file actions
executable file
·126 lines (120 loc) · 3.52 KB
/
Copy pathbase64_lib.php
File metadata and controls
executable file
·126 lines (120 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @file
* @author John Mannix
* @date 2011-11-04
*
* base64_lib.php
*
* A function library used to convert a binary string to a base64-encoded
* string, as well as the reverse.
*
* When converting from binary to base64 (and the reverse), we first
* convert to an intermediate int value. When doing this, the following
* conventions are used:
* -'0' to '9' correspond to 0 to 9
* -'a' to 'z' correspond to 10 to 35
* -'A' to 'Z' correspond to 36 to 61
* -'-' is 62
* -'_' is 63
*
* WARNING: Any call to these MUST have a legal input. I.e., any binary call
* must be a legal binary call, and any base64 call must be a legal base64
* call.
*
*/
/**
* int2char - Converts a given int to the proper base64 char.
*
* This function compliments char2int.
*
* @param $int - the given int
* @return - the resulting base64 char
*/
function int2char($int){
if(($int > -1) && ($int < 10)) //0-9
return $int;
else if (($int > 9) && ($int < 36)) //10-35, a-z
return chr($int + 87); //87 is the lowercase ASCII offset for our numbers
else if (($int > 35) && ($int < 62)) //36-61, A-Z
return chr($int + 29); //29 is the uppercase ASCII offset for our numbers
else if ($int == 62) //62, -
return '-';
else if ($int == 63) //63, _
return '_';
else
return ' '; //Unknown value, return a space!
}
/**
* char2int - Converts a given base64 char to the proper int.
*
* This function compliments int2char. It uses ord() to great effect.
*
* @param $char - the given base64 char
* @return - the resulting int
*/
function char2int($char){
$int = ord($char); //convert to ASCII number.
if(($int > 47) && ($int < 58))
return $int - 48; //47 is the ASCII number offset.
if(($int > 96) && ($int < 123)) //a-z
return $int - 87; //87 is the ASCII offset.
else if (($int > 64) && ($int < 91)) //A-Z
return $int - 29; //29 is the ASCII offset
else if ($int == 45) // -
return 62;
else if ($int == 95) // _
return 63;
else
return -1; //Unknown char, return a -1.
}
/**
* bin2base64 - Convert a binary string to base64
*
* This fucntion converts a binary string of any size to base 64.
* If the given binary string is not divisible evenly by 6, the
* converter pads the last segment with '0's from the end of the
* segment and then converts that segment.
*
* @param $bin - the binary string to convert
* @return $output - the converted base64 string.
*/
function bin2base64($bin){
$output = "";
$length = floor(strlen($bin) / 6);
$overflow = strlen($bin) % 6;
for($i = 0; $i < $length; $i++){
$buffer = substr($bin,($i * 6),6);
$output .= int2char(bindec($buffer));
}
if($overflow > 0){
$buffer = substr($bin, (($length * 6)));
$buffer = str_pad($buffer, 6, '0');
$output .= int2char(bindec($buffer));
}
return $output;
}
/**
* base642bin - Converts a given base64 string to its binary representation.
*
* WARNING: This function is NOT an exact compliment of bin2base64. This
* function does not remove any padded 0s from the end of the base64 string
* given by bin2base64. It is up to the user of this function to know where
* the binary string is supposed to end.
*
* This function takes each char and converts it to a 6 bit binary string,
* adding 0s to the left if needed.
*
* @param $base - The given base64 string.
* @return $output - The resulting binary string.
*/
function base642bin($base){
$output = "";
for($i = 0; $i < strlen($base); $i++){
$buffer = decbin(char2int($base{$i}));
$buffer = str_pad($buffer, 6, "0", STR_PAD_LEFT);
$output .= $buffer;
}
return $output;
}
?>