Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base32 decode base32 encode #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Empty file added ext/standard/base32.c
Empty file.
10 changes: 10 additions & 0 deletions ext/standard/base32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#ifndef BASE32_H
#define BASE32_H

extern const char *PHP_BASE32_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
extern const char *PHP_BASE32_HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
nyamsprod marked this conversation as resolved.
Show resolved Hide resolved
PHP_FUNCTION(base32_decode);
PHP_FUNCTION(base32_encode);

#endif
14 changes: 14 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,20 @@ function array_combine(array $keys, array $values): array {}
/** @compile-time-eval */
function array_is_list(array $array): bool {}

/* base32.c */

/**
* @compile-time-eval
* @refcount 1
*/
function base32_encode(string $decoded, string $alphabet = PHP_BASE32_ASCII, string $padding = '='): string {}

/**
* @compile-time-eval
* @refcount 1
*/
function base32_decode(string $encoded, string $alphabet = PHP_BASE32_ASCII, string $padding = '=', bool $strict = false): string|false {}

/* base64.c */

/**
Expand Down
17 changes: 17 additions & 0 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions ext/standard/tests/url/base32_decode_invalid_parameters_relax_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
--TEST--
Test base32_decode() function : invalid parameters in relax mode
--FILE--
<?php
echo "*** Testing base32_decode() : invalid parameters in relax mode***\n\n";
$testData = [
'characters outside of base32 extended hex alphabet' => [
'sequence' => 'CPNMUOJ1E8Z======',
'message' => 'The encoded string contains characters outside of the base32 alphabet.',
'alphabet' => PHP_BASE32_HEX,
'padding' => '=',
],
'characters outside of base32 us ascii alphabet' => [
'sequence' => '90890808',
'message' => 'The encoded string contains characters outside of the base32 alphabet.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
],
'characters not upper-cased' => [
'sequence' => 'MzxQ====',
'message' => 'The encoded string contains lower-cased characters which is forbidden on strict mode.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
],
'padding character in the middle of the sequence' => [
'sequence' => 'Mzx==Q====',
'message' => 'A padding character is contained in the middle of the encoded string.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
],
'invalid padding length' => [
'sequence' => 'MzxQ=======',
'message' => 'The encoded string contains an invalid padding length.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
],
'invalid encoded string length' => [
'sequence' => 'A',
'message' => 'The encoded string length is not a multiple of 8.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => '=',
],
'invalid alphabet length' => [
'sequence' => 'A',
'message' => 'The alphabet must be a 32 bytes long string.',
'alphabet' => '1234567890asdfghjklzxcvbnm',
'padding' => '=',
],
'the padding character is contained within the alphabet' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => '*',
],
'the padding character is contained within the alphabet is case insensitive' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => 'a',
],
'the padding character is different than one byte' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => 'yo',
],
'the padding character can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\r",
],
'the padding character can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\n",
],
'the padding character can not contain "\t"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\t",
],
'the alphabet can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_ASCII, 0, -1)."\r",
'padding' => '=',
],
'the alphabet can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\n",
'padding' => '=',
],
'the alphabet can not contain "\t"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\t",
'padding' => '=',
],
];

foreach ($testData as $testTitle => $data) {
try {
echo "$testTitle\n";
var_dump(base32_decode($data['sequence'], $data['alphabet'], $data['padding'], false));
echo "===\n";
} catch (ValueError $exception) {
echo "error message: ", $exception->getMessage(), "\n===\n";
}
}
echo "\nDone\n";
?>
--EXPECT--
*** Testing base32_decode() : invalid parameters in relax mode***

characters outside of base32 extended hex alphabet
string(6) "foobar"
===
characters outside of base32 us ascii alphabet
string(0) ""
===
characters not upper-cased
string(2) "fo"
===
padding character in the middle of the sequence
string(2) "fo"
===
invalid padding length
string(2) "fo"
===
invalid encoded string length
string(0) ""
===
invalid alphabet length
error message: The alphabet must be a 32 bytes long string.
===
the padding character is contained within the alphabet
error message: The alphabet can not contain a reserved character.
===
the padding character is contained within the alphabet is case insensitive
string(0) ""
===
the padding character is different than one byte
error message: The padding character must be a non-reserved single byte character.
===
the padding character can not contain "\r"
error message: The padding character must be a non-reserved single byte character.
===
the padding character can not contain "\n"
error message: The padding character must be a non-reserved single byte character.
===
the padding character can not contain "\t"
error message: The padding character must be a non-reserved single byte character.
===
the alphabet can not contain "\r"
error message: The alphabet can not contain a reserved character.
===
the alphabet can not contain "\n"
error message: The alphabet can not contain a reserved character.
===
the alphabet can not contain "\t"
error message: The alphabet can not contain a reserved character.
===

Done
Loading