Rust

encoding

Module

Encodings: URL encoding, http_build_query, base64, hex, HTML entities and quoted-printable.

Ported from PHP 8.5.11 (ext/standard/url.c, http.c, base64.c, string.c, html.c, quot_print.c). Byte strings in, byte strings out; encoders whose output is always ASCII return String.

PHPRust
urlencode, rawurlencode, urldecode, rawurldecodeurlencode, rawurlencode, urldecode, rawurldecode
http_build_queryhttp_build_query with QueryEncoding
parse_strparse_str
base64_encode, base64_decodebase64_encode, base64_decode
bin2hex, hex2binbin2hex, hex2bin
htmlspecialchars, htmlentitieshtmlspecialchars, htmlentities with HtmlFlags
quoted_printable_encodequoted_printable_encode

The HTML functions support PHP's default charset, UTF-8. Their entity tables (encoding/html_tables.rs) are generated by data/html_entities.py from php-src's ext/standard/html_tables.h and PHP's own get_html_translation_table() (data/html_entities.php).

Contents

Functions
base64_decodePHP `base64_decode($string, $strict)`. Non-strict skips every byte that is not in the alphabet (and decodes data after `=` padding); strict skips only `\t \n \r` and spaces and fails (`None`, PHP's `false`) on anything else, on data after padding, on a lone final character or on bad padding.base64_encodePHP `base64_encode`: standard alphabet with `=` padding.bin2hexPHP `bin2hex`: lowercase hexadecimal.hex2binPHP `hex2bin`: `None` (PHP's `false`, with a warning) for an odd length or a non-hexadecimal character.htmlentitiesPHP `htmlentities($string, $flags, 'UTF-8', $double_encode)`: like `htmlspecialchars` but every character with a named entity in the doctype's table is converted (HTML 4.01 and XHTML: 253 entities, HTML5: 1511 including two-character ones such as `fj`; XML 1.0: the basic ones).htmlspecialcharsPHP `htmlspecialchars($string, $flags, 'UTF-8', $double_encode)`: escapes `& < >` and the quotes `flags` selects (`'` as `&#039;` in HTML 4.01, `&apos;` otherwise). With `double_encode` false, existing valid entities are kept. Invalid UTF-8 follows `ENT_IGNORE`/`ENT_SUBSTITUTE`, else the result is empty.http_build_queryPHP `http_build_query($data, $numeric_prefix, $arg_separator, $encoding_type)` for data decoded from JSON (objects are PHP arrays; keys that are canonical integers are integer keys, which get `numeric_prefix` at the top level). Nested arrays become `a%5Bb%5D=...`; `null` values are skipped, booleans are `1`/`0`, floats use the `precision` setting. `arg_separator` `None` is PHP's `arg_separator.output` default, `&`.is_integer_keyWhether a PHP array key string is an integer key (`ZEND_HANDLE_NUMERIC_STR`): `-?[1-9][0-9]*` or `0`, within `i64`.parse_str`parse_str($string, $result)`: `php_default_treat_data(PARSE_STRING)` with the default `arg_separator.input` (`&`), `max_input_vars` and `max_input_nesting_level`. Also how Swoole fills `$request->get` and, for `application/x-www-form-urlencoded` bodies, `$request->post`.quoted_printable_encodePHP `quoted_printable_encode` (RFC 2045, lines of at most 76 bytes with PHP's look-ahead for multi-byte UTF-8 lead bytes).rawurldecodePHP `rawurldecode`: `%XX` is a byte (`+` stays `+`).rawurldecode_into`rawurldecode`, appended to `out` instead of a new buffer.rawurlencodePHP `rawurlencode` (RFC 3986): alphanumerics and `-_.~` are kept, every other byte is `%XX`.register_variable`php_register_variable_ex($name, $value, $array)`: registers one request variable the way PHP fills `$_GET`, `$_POST` and `$_COOKIE`. Leading spaces are dropped, `.` and ` ` in the base name become `_`, `name[a][]` builds nested arrays (at most `MAX_INPUT_NESTING_LEVEL` levels; deeper names drop the whole variable), and an unterminated `[` is kept as `_`.urldecodePHP `urldecode`: `+` is a space and `%XX` a byte; a `%` not followed by two hex digits is kept.urldecode_into`urldecode`, appended to `out` instead of a new buffer.urlencodePHP `urlencode`: `application/x-www-form-urlencoded` (RFC 1738): alphanumerics and `-_.` are kept, a space is `+`, every other byte (including `~`) is `%XX`.
use utopia_php_std::encoding::HtmlFlags;
Rust
pub struct HtmlFlags {
    pub single_quotes: bool,
    pub double_quotes: bool,
    pub ignore: bool,
    pub substitute: bool,
    pub disallowed: bool,
    pub doctype: Doctype,
}

The $flags of the HTML functions, decoded from PHP's bit mask.

Also implements Default.
use utopia_php_std::encoding::Doctype;
Rust
pub enum Doctype {
    Html401,
    Xml1,
    Xhtml,
    Html5,
}

The document type of the ENT_HTML401/ENT_XML1/ENT_XHTML/ENT_HTML5 flags.

use utopia_php_std::encoding::QueryEncoding;
Rust
pub enum QueryEncoding {
    Rfc1738,
    Rfc3986,
}

http_build_query()'s $encoding_type.

use utopia_php_std::encoding::base64_decode;
Rust
pub fn base64_decode(s: &[u8], strict: bool) -> Option<Vec<u8>>

PHP base64_decode($string, $strict). Non-strict skips every byte that is not in the alphabet (and decodes data after = padding); strict skips only \t \n \r and spaces and fails (None, PHP's false) on anything else, on data after padding, on a lone final character or on bad padding.

use utopia_php_std::encoding::base64_encode;
Rust
pub fn base64_encode(s: &[u8]) -> String

PHP base64_encode: standard alphabet with = padding.

use utopia_php_std::encoding::bin2hex;
Rust
pub fn bin2hex(s: &[u8]) -> String

PHP bin2hex: lowercase hexadecimal.

use utopia_php_std::encoding::hex2bin;
Rust
pub fn hex2bin(s: &[u8]) -> Option<Vec<u8>>

PHP hex2bin: None (PHP's false, with a warning) for an odd length or a non-hexadecimal character.

use utopia_php_std::encoding::htmlentities;
Rust
pub fn htmlentities(s: &[u8], flags: HtmlFlags, double_encode: bool) -> Vec<u8>

PHP htmlentities($string, $flags, 'UTF-8', $double_encode): like htmlspecialchars but every character with a named entity in the doctype's table is converted (HTML 4.01 and XHTML: 253 entities, HTML5: 1511 including two-character ones such as fj; XML 1.0: the basic ones).

use utopia_php_std::encoding::htmlspecialchars;
Rust
pub fn htmlspecialchars(s: &[u8], flags: HtmlFlags, double_encode: bool) -> Vec<u8>

PHP htmlspecialchars($string, $flags, 'UTF-8', $double_encode): escapes & < > and the quotes flags selects (' as &#039; in HTML 4.01, &apos; otherwise). With double_encode false, existing valid entities are kept. Invalid UTF-8 follows ENT_IGNORE/ENT_SUBSTITUTE, else the result is empty.

use utopia_php_std::encoding::http_build_query;
Rust
pub fn http_build_query(
    data: &Value,
    numeric_prefix: &[u8],
    arg_separator: Option<&[u8]>,
    encoding: QueryEncoding,
) -> Vec<u8>

PHP http_build_query($data, $numeric_prefix, $arg_separator, $encoding_type) for data decoded from JSON (objects are PHP arrays; keys that are canonical integers are integer keys, which get numeric_prefix at the top level). Nested arrays become a%5Bb%5D=...; null values are skipped, booleans are 1/0, floats use the precision setting. arg_separator None is PHP's arg_separator.output default, &.

use utopia_php_std::encoding::is_integer_key;
Rust
pub fn is_integer_key(key: &str) -> bool

Whether a PHP array key string is an integer key (ZEND_HANDLE_NUMERIC_STR): -?[1-9][0-9]* or 0, within i64.

use utopia_php_std::encoding::parse_str;
Rust
pub fn parse_str(input: &[u8]) -> Array

parse_str($string, $result): php_default_treat_data(PARSE_STRING) with the default arg_separator.input (&), max_input_vars and max_input_nesting_level. Also how Swoole fills $request->get and, for application/x-www-form-urlencoded bodies, $request->post.

The input is a C string to the engine: it ends at the first NUL byte.

use utopia_php_std::encoding::quoted_printable_encode;
Rust
pub fn quoted_printable_encode(s: &[u8]) -> Vec<u8>

PHP quoted_printable_encode (RFC 2045, lines of at most 76 bytes with PHP's look-ahead for multi-byte UTF-8 lead bytes).

use utopia_php_std::encoding::rawurldecode;
Rust
pub fn rawurldecode(s: &[u8]) -> Vec<u8>

PHP rawurldecode: %XX is a byte (+ stays +).

use utopia_php_std::encoding::rawurldecode_into;
Rust
pub fn rawurldecode_into(out: &mut Vec<u8>, s: &[u8])

rawurldecode, appended to out instead of a new buffer.

use utopia_php_std::encoding::rawurlencode;
Rust
pub fn rawurlencode(s: &[u8]) -> String

PHP rawurlencode (RFC 3986): alphanumerics and -_.~ are kept, every other byte is %XX.

use utopia_php_std::encoding::register_variable;
Rust
pub fn register_variable(array: &mut Array, name: &[u8], value: Value, keep_existing: bool)

php_register_variable_ex($name, $value, $array): registers one request variable the way PHP fills $_GET, $_POST and $_COOKIE. Leading spaces are dropped, . and in the base name become _, name[a][] builds nested arrays (at most MAX_INPUT_NESTING_LEVEL levels; deeper names drop the whole variable), and an unterminated [ is kept as _.

keep_existing is the $_COOKIE rule: a top-level name that is already set is not overwritten.

use utopia_php_std::encoding::urldecode;
Rust
pub fn urldecode(s: &[u8]) -> Vec<u8>

PHP urldecode: + is a space and %XX a byte; a % not followed by two hex digits is kept.

use utopia_php_std::encoding::urldecode_into;
Rust
pub fn urldecode_into(out: &mut Vec<u8>, s: &[u8])

urldecode, appended to out instead of a new buffer.

use utopia_php_std::encoding::urlencode;
Rust
pub fn urlencode(s: &[u8]) -> String

PHP urlencode: application/x-www-form-urlencoded (RFC 1738): alphanumerics and -_. are kept, a space is +, every other byte (including ~) is %XX.

Rust
pub const MAX_INPUT_NESTING_LEVEL: usize = 64usize;

max_input_nesting_level: bracket levels in a variable name, PHP's default.

Rust
pub const MAX_INPUT_VARS: usize = 1_000usize;

max_input_vars: variables registered per input, PHP's default.