PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
← All changes | src/extra/binary-to-text-php/Base2n.php +48 -48 3.0.1 → 6.3.0 View file →
@@ -32,9 +32,9 @@
32 32 protected $_padFinalGroup;
33 33 protected $_padCharacter;
34 34 protected $_caseSensitive;
35 35 protected $_charmap;
36 -
36 +
37 37 /**
38 38 * Constructor
39 39 *
40 40 * @param integer $bitsPerCharacter Bits to use for each encoded character
@@ -55,59 +55,59 @@
55 55 // Ensure validity of $chars
56 56 if ( ! is_string( $chars ) || ( $charLength = strlen( $chars ) ) < 2 ) {
57 57 throw new InvalidArgumentException( '$chars must be a string of at least two characters' );
58 58 }
59 -
59 +
60 60 // Ensure validity of $padCharacter
61 61 if ( $padFinalGroup ) {
62 62 if ( ! is_string( $padCharacter ) || ! isset( $padCharacter[0] ) ) {
63 63 throw new InvalidArgumentException( '$padCharacter must be a string of one character' );
64 64 }
65 -
65 +
66 66 if ( $caseSensitive ) {
67 67 $padCharFound = strpos( $chars, $padCharacter[0] );
68 68 } else {
69 69 $padCharFound = stripos( $chars, $padCharacter[0] );
70 70 }
71 -
71 +
72 72 if ( $padCharFound !== false ) {
73 73 throw new InvalidArgumentException( '$padCharacter can not be a member of $chars' );
74 74 }
75 75 }
76 -
76 +
77 77 // Ensure validity of $bitsPerCharacter
78 78 if ( ! is_int( $bitsPerCharacter ) ) {
79 79 throw new InvalidArgumentException( '$bitsPerCharacter must be an integer' );
80 80 }
81 -
81 +
82 82 if ( $bitsPerCharacter < 1 ) {
83 83 // $bitsPerCharacter must be at least 1
84 84 throw new InvalidArgumentException( '$bitsPerCharacter can not be less than 1' );
85 -
85 +
86 86 } elseif ( $charLength < 1 << $bitsPerCharacter ) {
87 87 // Character length of $chars is too small for $bitsPerCharacter
88 88 // Find greatest acceptable value of $bitsPerCharacter
89 89 $bitsPerCharacter = 1;
90 90 $radix = 2;
91 -
91 +
92 92 while ( $charLength >= ( $radix <<= 1 ) && $bitsPerCharacter < 8 ) {
93 93 $bitsPerCharacter ++;
94 94 }
95 -
95 +
96 96 $radix >>= 1;
97 97 throw new InvalidArgumentException(
98 98 '$bitsPerCharacter can not be more than ' . $bitsPerCharacter
99 99 . ' given $chars length of ' . $charLength
100 100 . ' (max radix ' . $radix . ')' );
101 -
101 +
102 102 } elseif ( $bitsPerCharacter > 8 ) {
103 103 // $bitsPerCharacter must not be greater than 8
104 104 throw new InvalidArgumentException( '$bitsPerCharacter can not be greater than 8' );
105 -
105 +
106 106 } else {
107 107 $radix = 1 << $bitsPerCharacter;
108 108 }
109 -
109 +
110 110 $this->_chars = $chars;
111 111 $this->_bitsPerCharacter = $bitsPerCharacter;
112 112 $this->_radix = $radix;
113 113 $this->_rightPadFinalBits = $rightPadFinalBits;
@@ -114,9 +114,9 @@
114 114 $this->_padFinalGroup = $padFinalGroup;
115 115 $this->_padCharacter = $padCharacter[0];
116 116 $this->_caseSensitive = $caseSensitive;
117 117 }
118 -
118 +
119 119 /**
120 120 * Encode a string
121 121 *
122 122 * @param string $rawString Binary data to encode
@@ -126,26 +126,26 @@
126 126 public function encode( $rawString ) {
127 127 // Unpack string into an array of bytes
128 128 $bytes = unpack( 'C*', $rawString );
129 129 $byteCount = count( $bytes );
130 -
130 +
131 131 $encodedString = '';
132 132 $byte = array_shift( $bytes );
133 133 $bitsRead = 0;
134 134 $oldBits = 0;
135 -
135 +
136 136 $chars = $this->_chars;
137 137 $bitsPerCharacter = $this->_bitsPerCharacter;
138 138 $rightPadFinalBits = $this->_rightPadFinalBits;
139 139 $padFinalGroup = $this->_padFinalGroup;
140 140 $padCharacter = $this->_padCharacter;
141 -
141 +
142 142 $charsPerByte = 8 / $bitsPerCharacter;
143 143 $encodedLength = $byteCount * $charsPerByte;
144 -
144 +
145 145 // Generate encoded output; each loop produces one encoded character
146 146 for ( $c = 0; $c < $encodedLength; $c ++ ) {
147 -
147 +
148 148 // Get the bits needed for this encoded character
149 149 if ( $bitsRead + $bitsPerCharacter > 8 ) {
150 150 // Not enough bits remain in this byte for the current character
151 151 // Save the remaining bits before getting the next byte
@@ -151,9 +151,9 @@
151 151 // Save the remaining bits before getting the next byte
152 152 $oldBitCount = 8 - $bitsRead;
153 153 $oldBits = $byte ^ ( $byte >> $oldBitCount << $oldBitCount );
154 154 $newBitCount = $bitsPerCharacter - $oldBitCount;
155 -
155 +
156 156 if ( ! $bytes ) {
157 157 // Last bits; match final character and exit loop
158 158 if ( $rightPadFinalBits ) {
159 159 $oldBits <<= $newBitCount;
@@ -158,9 +158,9 @@
158 158 if ( $rightPadFinalBits ) {
159 159 $oldBits <<= $newBitCount;
160 160 }
161 161 $encodedString .= $chars[ $oldBits ];
162 -
162 +
163 163 if ( $padFinalGroup ) {
164 164 // Array of the lowest common multiples of $bitsPerCharacter and 8, divided by 8
165 165 $lcmMap = array( 1 => 1, 2 => 1, 3 => 3, 4 => 1, 5 => 5, 6 => 3, 7 => 7, 8 => 1 );
166 166 $bytesPerGroup = $lcmMap[ $bitsPerCharacter ];
@@ -166,37 +166,37 @@
166 166 $bytesPerGroup = $lcmMap[ $bitsPerCharacter ];
167 167 $pads = $bytesPerGroup * $charsPerByte - ceil( ( strlen( $rawString ) % $bytesPerGroup ) * $charsPerByte );
168 168 $encodedString .= str_repeat( $padCharacter, $pads );
169 169 }
170 -
170 +
171 171 break;
172 172 }
173 -
173 +
174 174 // Get next byte
175 175 $byte = array_shift( $bytes );
176 176 $bitsRead = 0;
177 -
177 +
178 178 } else {
179 179 $oldBitCount = 0;
180 180 $newBitCount = $bitsPerCharacter;
181 181 }
182 -
182 +
183 183 // Read only the needed bits from this byte
184 184 $bits = $byte >> 8 - ( $bitsRead + ( $newBitCount ) );
185 185 $bits ^= $bits >> $newBitCount << $newBitCount;
186 186 $bitsRead += $newBitCount;
187 -
187 +
188 188 if ( $oldBitCount ) {
189 189 // Bits come from seperate bytes, add $oldBits to $bits
190 190 $bits = ( $oldBits << $newBitCount ) | $bits;
191 191 }
192 -
192 +
193 193 $encodedString .= $chars[ $bits ];
194 194 }
195 -
195 +
196 196 return $encodedString;
197 197 }
198 -
198 +
199 199 /**
200 200 * Decode a string
201 201 *
202 202 * @param string $encodedString Data to decode
@@ -208,9 +208,9 @@
208 208 if ( ! $encodedString || ! is_string( $encodedString ) ) {
209 209 // Empty string, nothing to decode
210 210 return '';
211 211 }
212 -
212 +
213 213 $chars = $this->_chars;
214 214 $bitsPerCharacter = $this->_bitsPerCharacter;
215 215 $radix = $this->_radix;
216 216 $rightPadFinalBits = $this->_rightPadFinalBits;
@@ -216,26 +216,26 @@
216 216 $rightPadFinalBits = $this->_rightPadFinalBits;
217 217 $padFinalGroup = $this->_padFinalGroup;
218 218 $padCharacter = $this->_padCharacter;
219 219 $caseSensitive = $this->_caseSensitive;
220 -
220 +
221 221 // Get index of encoded characters
222 222 if ( $this->_charmap ) {
223 223 $charmap = $this->_charmap;
224 -
224 +
225 225 } else {
226 226 $charmap = array();
227 -
227 +
228 228 for ( $i = 0; $i < $radix; $i ++ ) {
229 229 $charmap[ $chars[ $i ] ] = $i;
230 230 }
231 -
231 +
232 232 $this->_charmap = $charmap;
233 233 }
234 -
234 +
235 235 // The last encoded character is $encodedString[$lastNotatedIndex]
236 236 $lastNotatedIndex = strlen( $encodedString ) - 1;
237 -
237 +
238 238 // Remove trailing padding characters
239 239 if ( $padFinalGroup ) {
240 240 while ( $encodedString[ $lastNotatedIndex ] === $padCharacter ) {
241 241 $encodedString = substr( $encodedString, 0, $lastNotatedIndex );
@@ -241,53 +241,53 @@
241 241 $encodedString = substr( $encodedString, 0, $lastNotatedIndex );
242 242 $lastNotatedIndex --;
243 243 }
244 244 }
245 -
245 +
246 246 $rawString = '';
247 247 $byte = 0;
248 248 $bitsWritten = 0;
249 -
249 +
250 250 // Convert each encoded character to a series of unencoded bits
251 251 for ( $c = 0; $c <= $lastNotatedIndex; $c ++ ) {
252 -
252 +
253 253 if ( ! $caseSensitive && ! isset( $charmap[ $encodedString[ $c ] ] ) ) {
254 254 // Encoded character was not found; try other case
255 255 if ( isset( $charmap[ $cUpper = strtoupper( $encodedString[ $c ] ) ] ) ) {
256 256 $charmap[ $encodedString[ $c ] ] = $charmap[ $cUpper ];
257 -
257 +
258 258 } elseif ( isset( $charmap[ $cLower = strtolower( $encodedString[ $c ] ) ] ) ) {
259 259 $charmap[ $encodedString[ $c ] ] = $charmap[ $cLower ];
260 260 }
261 261 }
262 -
262 +
263 263 if ( isset( $charmap[ $encodedString[ $c ] ] ) ) {
264 264 $bitsNeeded = 8 - $bitsWritten;
265 265 $unusedBitCount = $bitsPerCharacter - $bitsNeeded;
266 -
266 +
267 267 // Get the new bits ready
268 268 if ( $bitsNeeded > $bitsPerCharacter ) {
269 269 // New bits aren't enough to complete a byte; shift them left into position
270 270 $newBits = $charmap[ $encodedString[ $c ] ] << $bitsNeeded - $bitsPerCharacter;
271 271 $bitsWritten += $bitsPerCharacter;
272 -
272 +
273 273 } elseif ( $c !== $lastNotatedIndex || $rightPadFinalBits ) {
274 274 // Zero or more too many bits to complete a byte; shift right
275 275 $newBits = $charmap[ $encodedString[ $c ] ] >> $unusedBitCount;
276 276 $bitsWritten = 8; //$bitsWritten += $bitsNeeded;
277 -
277 +
278 278 } else {
279 279 // Final bits don't need to be shifted
280 280 $newBits = $charmap[ $encodedString[ $c ] ];
281 281 $bitsWritten = 8;
282 282 }
283 -
283 +
284 284 $byte |= $newBits;
285 -
285 +
286 286 if ( $bitsWritten === 8 || $c === $lastNotatedIndex ) {
287 287 // Byte is ready to be written
288 288 $rawString .= pack( 'C', $byte );
289 -
289 +
290 290 if ( $c !== $lastNotatedIndex ) {
291 291 // Start the next byte
292 292 $bitsWritten = $unusedBitCount;
293 293 $byte = ( $charmap[ $encodedString[ $c ] ] ^ ( $newBits << $unusedBitCount ) ) << 8 - $bitsWritten;
@@ -292,14 +292,14 @@
292 292 $bitsWritten = $unusedBitCount;
293 293 $byte = ( $charmap[ $encodedString[ $c ] ] ^ ( $newBits << $unusedBitCount ) ) << 8 - $bitsWritten;
294 294 }
295 295 }
296 -
296 +
297 297 } elseif ( $strict ) {
298 298 // Unable to decode character; abort
299 299 return null;
300 300 }
301 301 }
302 -
302 +
303 303 return $rawString;
304 304 }
305 305 }