/var/www/html_sp/wp-content/plugins/woocommerce/vendor/opis/uri/src/Punycode.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/* ============================================================================
 * Copyright 2021 Zindex Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================================ */

namespace Opis\Uri;

use 
Opis\String\UnicodeString;

final class 
Punycode
{
    private const 
BASE 36;
    private const 
TMIN 1;
    private const 
TMAX 26;
    private const 
SKEW 38;
    private const 
DAMP 700;
    private const 
INITIAL_BIAS 72;
    private const 
INITIAL_N 0x80;
    private const 
PREFIX 'xn--';
    private const 
PREFIX_LEN 4;
    private const 
DELIMITER 0x2D;
    private const 
MAX_INT 0x7FFFFFFF;
    private const 
NON_ASCII '#[^\0-\x7E]#';

    public static function 
encode(string $input): string
    
{
        return 
implode('.'array_map([self::class, 'encodePart'], explode('.'$input)));
    }

    public static function 
decode(string $input): string
    
{
        return 
implode('.'array_map([self::class, 'decodePart'], explode('.'$input)));
    }

    public static function 
normalize(string $input): string
    
{
        return 
implode('.'array_map([self::class, 'normalizePart'], explode('.'$input)));
    }

    public static function 
encodePart(string $input): string
    
{
        if (!
preg_match(self::NON_ASCII$input)) {
            return 
$input;
        }

        
$input UnicodeString::getCodePointsFromString($inputUnicodeString::LOWER_CASE);
        
$input_len count($input);

        
$output array_filter($input, static function (int $code): bool {
            return 
$code 0x80;
        });

        if (
$output) {
            
$output array_values($output);
        }

        
$delta 0;
        
$n self::INITIAL_N;
        
$bias self::INITIAL_BIAS;

        
$handled $basic_length count($output);

        if (
$basic_length) {
            
$output[] = self::DELIMITER;
        }

        while (
$handled $input_len) {
            
$m self::MAX_INT;

            for (
$i 0;  $i $input_len;  $i++) {
                if (
$input[$i] >= $n && $input[$i] < $m) {
                    
$m $input[$i];
                }
            }

            if ((
$m $n) > intdiv(self::MAX_INT $delta$handled 1)) {
                throw new 
PunycodeException("Punycode overflow");
            }

            
$delta += ($m $n) * ($handled 1);

            
$n $m;

            for (
$i 0;  $i $input_len;  $i++) {
                if (
$input[$i] < $n && (++$delta === 0)) {
                    throw new 
PunycodeException("Punycode overflow");
                }

                if (
$input[$i] === $n) {
                    
$q $delta;
                    for (
$k self::BASE; ; $k += self::BASE) {
                        
$t self::threshold($k$bias);
                        if (
$q $t) {
                            break;
                        }

                        
$base_minus_t self::BASE $t;

                        
$q -= $t;

                        
$output[] = self::encodeDigit($t + ($q $base_minus_t));

                        
$q intdiv($q$base_minus_t);
                    }

                    
$output[] = self::encodeDigit($q);

                    
$bias self::adapt($delta$handled 1$handled === $basic_length);
                    
$delta 0;
                    
$handled++;
                }
            }

            
$delta++; $n++;
        }

        return 
self::PREFIX UnicodeString::getStringFromCodePoints($output);
    }

    public static function 
decodePart(string $input): string
    
{
        if (
stripos($inputself::PREFIX) !== 0) {
            return 
$input;
        }

        
$input UnicodeString::getCodePointsFromString(substr($inputself::PREFIX_LEN), UnicodeString::LOWER_CASE);
        
$input_len count($input);

        
$pos array_keys($inputself::DELIMITERtrue);
        if (
$pos) {
            
$pos end($pos);
        } else {
            
$pos = -1;
        }

        
/** @var int $pos */

        
if ($pos === -1) {
            
$output = [];
            
$pos $output_len 0;
        } else {
            
$output array_slice($input0, ++$pos);
            
$output_len $pos;
            for (
$i 0$i $pos$i++) {
                if (
$output[$i] >= 0x80) {
                    throw new 
PunycodeException("Non-basic code point is not allowed: {$output[$i]}");
                }
            }
        }

        
$i 0;
        
$n self::INITIAL_N;
        
$bias self::INITIAL_BIAS;

        while (
$pos $input_len) {
            
$old_i $i;

            for (
$w 1$k self::BASE; ; $k += self::BASE) {
                if (
$pos >= $input_len) {
                    throw new 
PunycodeException("Punycode bad input");
                }

                
$digit self::decodeDigit($input[$pos++]);

                if (
$digit >= self::BASE || $digit intdiv(self::MAX_INT $i$w)) {
                    throw new 
PunycodeException("Punycode overflow");
                }

                
$i += $digit $w;

                
$t self::threshold($k$bias);
                if (
$digit $t) {
                    break;
                }

                
$t self::BASE $t;

                if (
$w intdiv(self::MAX_INT$t)) {
                    throw new 
PunycodeException("Punycode overflow");
                }

                
$w *= $t;
            }

            
$output_len++;

            if (
intdiv($i$output_len) > self::MAX_INT $n) {
                throw new 
PunycodeException("Punycode overflow");
            }

            
$n += intdiv($i$output_len);

            
$bias self::adapt($i $old_i$output_len$old_i === 0);

            
$i %= $output_len;

            
array_splice($output$i0$n);

            
$i++;
        }

        return 
UnicodeString::getStringFromCodePoints($output);
    }

    public static function 
normalizePart(string $input): string
    
{
        
$input strtolower($input);

        if (
strpos($inputself::DELIMITER) === 0) {
            
self::decodePart($input); // just validate
            
return $input;
        }

        return 
self::encodePart($input);
    }

    private static function 
encodeDigit(int $digit): int
    
{
        return 
$digit 0x16 + ($digit 0x1A 0x4B0x00);
    }

    private static function 
decodeDigit(int $code): int
    
{
        if (
$code 0x3A) {
            return 
$code 0x16;
        }
        if (
$code 0x5B) {
            return 
$code 0x41;
        }
        if (
$code 0x7B) {
            return 
$code 0x61;
        }

        return 
self::BASE;
    }

    private static function 
threshold(int $kint $bias): int
    
{
        
$d $k $bias;

        if (
$d <= self::TMIN) {
            return 
self::TMIN;
        }

        if (
$d >= self::TMAX) {
            return 
self::TMAX;
        }

        return 
$d;
    }

    private static function 
adapt(int $deltaint $num_pointsbool $first_time false): int
    
{
        
$delta intdiv($delta$first_time self::DAMP 2);
        
$delta += intdiv($delta$num_points);

        
$k 0;
        
$base_tmin_diff self::BASE self::TMIN;
        
$lim $base_tmin_diff self::TMAX 2;

        while (
$delta $lim) {
            
$delta intdiv($delta$base_tmin_diff);
            
$k += self::BASE;
        }

        
$k += intdiv(($base_tmin_diff 1) * $delta$delta self::SKEW);

        return 
$k;
    }
}