MCPcopy Create free account
hub / github.com/NativeScript/android / verify_punycode

Function verify_punycode

test-app/runtime/src/main/cpp/ada/ada.cpp:8088–8147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8086}
8087
8088bool verify_punycode(std::string_view input) {
8089 if (input.starts_with("xn--")) {
8090 return false;
8091 }
8092 size_t written_out{0};
8093 uint32_t n = initial_n;
8094 int32_t i = 0;
8095 int32_t bias = initial_bias;
8096 // grab ascii content
8097 size_t end_of_ascii = input.find_last_of('-');
8098 if (end_of_ascii != std::string_view::npos) {
8099 for (uint8_t c : input.substr(0, end_of_ascii)) {
8100 if (c >= 0x80) {
8101 return false;
8102 }
8103 written_out++;
8104 }
8105 input.remove_prefix(end_of_ascii + 1);
8106 }
8107 while (!input.empty()) {
8108 int32_t oldi = i;
8109 int32_t w = 1;
8110 for (int32_t k = base;; k += base) {
8111 if (input.empty()) {
8112 return false;
8113 }
8114 uint8_t code_point = input.front();
8115 input.remove_prefix(1);
8116 int32_t digit = char_to_digit_value(code_point);
8117 if (digit < 0) {
8118 return false;
8119 }
8120 if (digit > (0x7fffffff - i) / w) {
8121 return false;
8122 }
8123 i = i + digit * w;
8124 int32_t t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
8125 if (digit < t) {
8126 break;
8127 }
8128 if (w > 0x7fffffff / (base - t)) {
8129 return false;
8130 }
8131 w = w * (base - t);
8132 }
8133 bias = adapt(i - oldi, int32_t(written_out + 1), oldi == 0);
8134 if (i / (written_out + 1) > 0x7fffffff - n) {
8135 return false;
8136 }
8137 n = n + i / int32_t(written_out + 1);
8138 i = i % int32_t(written_out + 1);
8139 if (n < 0x80) {
8140 return false;
8141 }
8142 written_out++;
8143 ++i;
8144 }
8145

Callers 1

to_unicodeFunction · 0.85

Calls 4

char_to_digit_valueFunction · 0.85
adaptFunction · 0.85
frontMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected