use super::js::{self, RangeError};
pub const VERSION: &str = "2.3.1";
const MAX_INT: i64 = 2147483647;
const BASE: i64 = 36;
const T_MIN: i64 = 1;
const T_MAX: i64 = 26;
const SKEW: i64 = 38;
const DAMP: i64 = 700;
const INITIAL_BIAS: i64 = 72;
const INITIAL_N: i64 = 128; const DELIMITER: u16 = 0x2D;
const BASE_MINUS_T_MIN: i64 = BASE - T_MIN;
fn floor_div(a: i64, b: i64) -> i64 {
a.div_euclid(b)
}
fn error(kind: &str) -> RangeError {
let message = match kind {
"overflow" => "Overflow: input needs wider integers to process",
"not-basic" => "Illegal input >= 0x80 (not a basic code point)",
"invalid-input" => "Invalid input",
_ => unreachable!("punycode error type {kind}"),
};
RangeError(message.to_owned())
}
fn map(
array: &[&[u16]],
callback: fn(&[u16]) -> Result<Vec<u16>, RangeError>,
) -> Result<Vec<Vec<u16>>, RangeError> {
let mut result: Vec<Vec<u16>> = vec![Vec::new(); array.len()];
let mut length = array.len();
while length > 0 {
length -= 1;
result[length] = callback(array[length])?;
}
Ok(result)
}
fn is_separator(c: u16) -> bool {
matches!(c, 0x2E | 0x3002 | 0xFF0E | 0xFF61)
}
fn map_domain(
domain: &[u16],
callback: fn(&[u16]) -> Result<Vec<u16>, RangeError>,
) -> Result<Vec<u16>, RangeError> {
let parts = js::split(domain, &[u16::from(b'@')]);
let mut result: Vec<u16> = Vec::new();
let mut domain = domain;
if parts.len() > 1 {
result.extend_from_slice(parts[0]);
result.push(u16::from(b'@'));
domain = parts[1];
}
let domain: Vec<u16> = domain
.iter()
.map(|&c| if is_separator(c) { 0x2E } else { c })
.collect();
let labels = js::split(&domain, &[0x2E]);
let encoded = map(&labels, callback)?;
for (i, label) in encoded.iter().enumerate() {
if i > 0 {
result.push(0x2E);
}
result.extend_from_slice(label);
}
Ok(result)
}
pub fn ucs2_decode(string: &[u16]) -> Vec<u32> {
let mut output: Vec<u32> = Vec::new();
let mut counter = 0;
let length = string.len();
while counter < length {
let value = u32::from(string[counter]);
counter += 1;
if value >= 0xD800 && value <= 0xDBFF && counter < length {
let extra = u32::from(string[counter]);
counter += 1;
if (extra & 0xFC00) == 0xDC00 {
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
} else {
output.push(value);
counter -= 1;
}
} else {
output.push(value);
}
}
output
}
pub fn ucs2_encode(code_points: &[u32]) -> Result<Vec<u16>, RangeError> {
js::from_code_point(code_points)
}
fn basic_to_digit(code_point: i64) -> i64 {
if code_point >= 0x30 && code_point < 0x3A {
return 26 + (code_point - 0x30);
}
if code_point >= 0x41 && code_point < 0x5B {
return code_point - 0x41;
}
if code_point >= 0x61 && code_point < 0x7B {
return code_point - 0x61;
}
BASE
}
fn digit_to_basic(digit: i64, flag: i64) -> i64 {
digit + 22 + 75 * i64::from(digit < 26) - (i64::from(flag != 0) << 5)
}
fn adapt(delta: i64, num_points: i64, first_time: bool) -> i64 {
let mut k = 0;
let mut delta = if first_time {
floor_div(delta, DAMP)
} else {
delta >> 1
};
delta += floor_div(delta, num_points);
while delta > (BASE_MINUS_T_MIN * T_MAX) >> 1 {
delta = floor_div(delta, BASE_MINUS_T_MIN);
k += BASE;
}
(k as f64 + ((BASE_MINUS_T_MIN + 1) * delta) as f64 / (delta + SKEW) as f64).floor() as i64
}
fn threshold(k: i64, bias: i64) -> i64 {
if k <= bias {
T_MIN
} else if k >= bias + T_MAX {
T_MAX
} else {
k - bias
}
}
pub fn decode(input: &[u16]) -> Result<Vec<u16>, RangeError> {
let mut output: Vec<u32> = Vec::new();
let input_length = input.len() as i64;
let mut i: i64 = 0;
let mut n: i64 = INITIAL_N;
let mut bias: i64 = INITIAL_BIAS;
let mut basic: i64 = js::last_index_of(input, &[DELIMITER], None).map_or(-1, |b| b as i64);
if basic < 0 {
basic = 0;
}
for j in 0..basic as usize {
if input[j] >= 0x80 {
return Err(error("not-basic"));
}
output.push(u32::from(input[j]));
}
let mut index: i64 = if basic > 0 { basic + 1 } else { 0 };
while index < input_length {
let oldi = i;
let mut w: i64 = 1;
let mut k: i64 = BASE;
loop {
if index >= input_length {
return Err(error("invalid-input"));
}
let digit = basic_to_digit(i64::from(input[index as usize]));
index += 1;
if digit >= BASE {
return Err(error("invalid-input"));
}
if digit > floor_div(MAX_INT - i, w) {
return Err(error("overflow"));
}
i += digit * w;
let t = threshold(k, bias);
if digit < t {
break;
}
let base_minus_t = BASE - t;
if w > floor_div(MAX_INT, base_minus_t) {
return Err(error("overflow"));
}
w *= base_minus_t;
k += BASE;
}
let out = output.len() as i64 + 1;
bias = adapt(i - oldi, out, oldi == 0);
if floor_div(i, out) > MAX_INT - n {
return Err(error("overflow"));
}
n += floor_div(i, out);
i %= out;
output.insert(i as usize, n as u32);
i += 1;
}
js::from_code_point(&output)
}
pub fn encode(input: &[u16]) -> Result<Vec<u16>, RangeError> {
let mut output: Vec<u16> = Vec::new();
let input: Vec<i64> = ucs2_decode(input).into_iter().map(i64::from).collect();
let input_length = input.len() as i64;
let mut n: i64 = INITIAL_N;
let mut delta: i64 = 0;
let mut bias: i64 = INITIAL_BIAS;
for ¤t_value in &input {
if current_value < 0x80 {
output.push(current_value as u16);
}
}
let basic_length = output.len() as i64;
let mut handled_cp_count = basic_length;
if basic_length != 0 {
output.push(DELIMITER);
}
while handled_cp_count < input_length {
let mut m = MAX_INT;
for ¤t_value in &input {
if current_value >= n && current_value < m {
m = current_value;
}
}
let handled_cp_count_plus_one = handled_cp_count + 1;
if m - n > floor_div(MAX_INT - delta, handled_cp_count_plus_one) {
return Err(error("overflow"));
}
delta += (m - n) * handled_cp_count_plus_one;
n = m;
for ¤t_value in &input {
if current_value < n && {
delta += 1;
delta > MAX_INT
} {
return Err(error("overflow"));
}
if current_value == n {
let mut q = delta;
let mut k = BASE;
loop {
let t = threshold(k, bias);
if q < t {
break;
}
let q_minus_t = q - t;
let base_minus_t = BASE - t;
output.push(digit_to_basic(t + q_minus_t % base_minus_t, 0) as u16);
q = floor_div(q_minus_t, base_minus_t);
k += BASE;
}
output.push(digit_to_basic(q, 0) as u16);
bias = adapt(
delta,
handled_cp_count_plus_one,
handled_cp_count == basic_length,
);
delta = 0;
handled_cp_count += 1;
}
}
delta += 1;
n += 1;
}
Ok(output)
}
fn regex_punycode(string: &[u16]) -> bool {
string.starts_with(&js::to_utf16("xn--"))
}
fn regex_non_ascii(string: &[u16]) -> bool {
string.iter().any(|&c| c > 0x7F)
}
pub fn to_unicode(input: &[u16]) -> Result<Vec<u16>, RangeError> {
map_domain(input, |string| {
if regex_punycode(string) {
decode(&js::to_lower_case(js::slice(string, 4, None)))
} else {
Ok(string.to_vec())
}
})
}
pub fn to_ascii(input: &[u16]) -> Result<Vec<u16>, RangeError> {
map_domain(input, |string| {
if regex_non_ascii(string) {
let mut out = js::to_utf16("xn--");
out.extend(encode(string)?);
Ok(out)
} else {
Ok(string.to_vec())
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::markdown_it::mdurl::gen_cases::{Function, Syms, check};
#[test]
fn generated_urls_match_node() {
let functions: [Function; 7] = [
("puny_to_ascii", |o, s| {
o.result(to_ascii(s));
}),
("puny_to_unicode", |o, s| {
o.result(to_unicode(s));
}),
("puny_encode", |o, s| {
o.result(encode(s));
}),
("puny_decode", |o, s| {
o.result(decode(s));
}),
("puny_roundtrip", |o, s| {
o.result(encode(s).and_then(|e| decode(&e)));
}),
("puny_ucs2", |o: &mut Syms, s| {
let cps = ucs2_decode(s);
o.0.push(cps.len() as u32);
o.0.extend(&cps);
o.result(ucs2_encode(&cps));
}),
("puny_host", |o, s| {
let Some(h) = crate::markdown_it::mdurl::parse(s, true).hostname else {
o.0.push(crate::markdown_it::mdurl::gen_cases::NULL);
return;
};
let a = o.result(to_ascii(&h));
o.result(to_unicode(&h));
if let Some(a) = a {
o.result(to_unicode(&a));
}
}),
];
check(&functions);
}
fn u(s: &str) -> Vec<u16> {
js::to_utf16(s)
}
fn r(x: Result<Vec<u16>, RangeError>) -> String {
match x {
Ok(v) => js::to_string(&v),
Err(e) => e.to_string(),
}
}
#[test]
fn examples_and_errors() {
assert_eq!(r(to_ascii(&u("münchen.de"))), "xn--mnchen-3ya.de");
assert_eq!(r(to_ascii(&u("user@ü.\u{3002}x"))), "user@xn--tda..x");
assert_eq!(r(to_ascii(&u("a@b@ä"))), "a@b");
assert_eq!(r(to_unicode(&u("xn--mnchen-3ya.de"))), "münchen.de");
assert_eq!(
r(to_unicode(&u("XN--mnchen-3ya.xn--MNCHEN-3YA"))),
"XN--mnchen-3ya.münchen"
);
assert_eq!(
r(decode(&u("99999999999"))),
"RangeError: Overflow: input needs wider integers to process"
);
assert_eq!(
r(decode(&u("ä-a"))),
"RangeError: Illegal input >= 0x80 (not a basic code point)"
);
assert_eq!(r(decode(&u("a-"))), "a");
assert_eq!(r(decode(&u("!"))), "RangeError: Invalid input");
assert_eq!(r(encode(&[0xD83D, 0xDE00, 0xD800])), "ib9bt343c");
assert_eq!(r(decode(&u("ls8h"))), "\u{1F4A9}");
assert_eq!(
r(to_unicode(&u("xn--!.xn--99999999999"))),
"RangeError: Overflow: input needs wider integers to process"
);
assert_eq!(r(decode(&u("hzzzzz"))), "RangeError: Invalid input");
assert_eq!(
r(ucs2_encode(&[0x41, 0x110000])),
"RangeError: Invalid code point 1114112"
);
assert_eq!(
ucs2_decode(&[0x61, 0xD83D, 0xDE00, 0xD800, 0xDBFF, 0xDC00]),
[0x61, 0x1F600, 0xD800, 0x10FC00]
);
}
}