Rust

encoding

Module

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

Ported from PHP 8.5.10 (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).

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations encoding.*).

Contents

1 submodule, and what the module defines itself.

use php_std::encoding::HtmlFlags;
Rust
pub struct HtmlFlags

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

Also implements Default.
use php_std::encoding::Doctype;
Rust
pub enum Doctype

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

use php_std::encoding::QueryEncoding;
Rust
pub enum QueryEncoding

http_build_query()'s $encoding_type.

use 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 php_std::encoding::base64_encode;
Rust
pub fn base64_encode(s: &[u8]) -> String

PHP base64_encode: standard alphabet with = padding.

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

PHP bin2hex: lowercase hexadecimal.

use 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 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 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 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 precision 14. arg_separator None is PHP's arg_separator.output default, &.

use 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 php_std::encoding::parse_str;
Rust
pub fn parse_str(input: &[u8]) -> crate::types::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 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 php_std::encoding::rawurldecode;
Rust
pub fn rawurldecode(s: &[u8]) -> Vec<u8>

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

use 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 php_std::encoding::register_variable;
Rust
pub fn register_variable(array: &mut crate::types::Array, name: &[u8], value: crate::types::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 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 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 = 64

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

Rust
pub const MAX_INPUT_VARS: usize = 1000

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

encoding::html_tables

Own page

HTML entity tables of PHP 8.5.10. Generated by data/html_entities.py; do not edit by hand.

Decoding tables (*_DECODE, sorted by name) come from php-src ext/standard/html_tables.h (tag php-8.5.10); encoding tables come from get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | <doctype>, 'UTF-8') of PHP 8.5.10 (data/html_entities.php), the tables htmlentities() uses.