s="lines-code">
+    if video_play:
139
+        video_path = args.video_path
140
+        if args.verbose:
141
+            print('Opening '+video_path+" .... ")
142
+        start_video(video_path)
143
+    if image:
144
+        image_path = args.image_path
145
+        if args.verbose:
146
+            print("Opening "+image_path+" .... ")
147
+        image_detect(image_path)
148
+
149
+
150
+    cv2.destroyAllWindows()

+ 182 - 0
yolov3-tiny.cfg

@@ -0,0 +1,182 @@
1
+[net]
2
+# Testing
3
+batch=1
4
+subdivisions=1
5
+# Training
6
+# batch=64
7
+# subdivisions=2
8
+width=416
9
+height=416
10
+channels=3
11
+momentum=0.9
12
+decay=0.0005
13
+angle=0
14
+saturation = 1.5
15
+exposure = 1.5
16
+hue=.1
17
+
18
+learning_rate=0.001
19
+burn_in=1000
20
+max_batches = 500200
21
+policy=steps
22
+steps=400000,450000
23
+scales=.1,.1
24
+
25
+[convolutional]
26
+batch_normalize=1
27
+filters=16
28
+size=3
29
+stride=1
30
+pad=1
31
+activation=leaky
32
+
33
+[maxpool]
34
+size=2
35
+stride=2
36
+
37
+[convolutional]
38
+batch_normalize=1
39
+filters=32
40
+size=3
41
+stride=1
42
+pad=1
43
+activation=leaky
44
+
45
+[maxpool]
46
+size=2
47
+stride=2
48
+
49
+[convolutional]
50
+batch_normalize=1
51
+filters=64
52
+size=3
53
+stride=1
54
+pad=1
55
+activation=leaky
56
+
57
+[maxpool]
58
+size=2
59
+stride=2
60
+
61
+[convolutional]
62
+batch_normalize=1
63
+filters=128
64
+size=3
65
+stride=1
66
+pad=1
67
+activation=leaky
68
+
69
+[maxpool]
70
+size=2
71
+stride=2
72
+
73
+[convolutional]
74
+batch_normalize=1
75
+filters=256
76
+size=3
77
+stride=1
78
+pad=1
79
+activation=leaky
80
+
81
+[maxpool]
82
+size=2
83
+stride=2
84
+
85
+[convolutional]
86
+batch_normalize=1
87
+filters=512
88
+size=3
89
+stride=1
90
+pad=1
91
+activation=leaky
92
+
93
+[maxpool]
94
+size=2
95
+stride=1
96
+
97
+[convolutional]
98
+batch_normalize=1
99
+filters=1024
100
+size=3
101
+stride=1
102
+pad=1
103
+activation=leaky
104
+
105
+###########
106
+
107
+[convolutional]
108
+batch_normalize=1
109
+filters=256
110
+size=1
111
+stride=1
112
+pad=1
113
+activation=leaky
114
+
115
+[convolutional]
116
+batch_normalize=1
117
+filters=512
118
+size=3
119
+stride=1
120
+pad=1
121
+activation=leaky
122
+
123
+[convolutional]
124
+size=1
125
+stride=1
126
+pad=1
127
+filters=255
128
+activation=linear
129
+
130
+
131
+
132
+[yolo]
133
+mask = 3,4,5
134
+anchors = 10,14,  23,27,  37,58,  81,82,  135,169,  344,319
135
+classes=80
136
+num=6
137
+jitter=.3
138
+ignore_thresh = .7
139
+truth_thresh = 1
140
+random=1
141
+
142
+[route]
143
+layers = -4
144
+
145
+[convolutional]
146
+batch_normalize=1
147
+filters=128
148
+size=1
149
+stride=1
150
+pad=1
151
+activation=leaky
152
+
153
+[upsample]
154
+stride=2
155
+
156
+[route]
157
+layers = -1, 8
158
+
159
+[convolutional]
160
+batch_normalize=1
161
+filters=256
162
+size=3
163
+stride=1
164
+pad=1
165
+activation=leaky
166
+
167
+[convolutional]
168
+size=1
169
+stride=1
170
+pad=1
171
+filters=255
172
+activation=linear
173
+
174
+[yolo]
175
+mask = 0,1,2
176
+anchors = 10,14,  23,27,  37,58,  81,82,  135,169,  344,319
177
+classes=80
178
+num=6
179
+jitter=.3
180
+ignore_thresh = .7
181
+truth_thresh = 1
182
+random=1

+ 788 - 0
yolov3.cfg

@@ -0,0 +1,788 @@
1
+[net]
2
+# Testing
3
+# batch=1
4
+# subdivisions=1
5
+# Training
6
+batch=64
7
+subdivisions=16
8
+width=608
9
+height=608
10
+channels=3
11
+momentum=0.9
12
+decay=0.0005
13
+angle=0
14
+saturation = 1.5
15
+exposure = 1.5
16
+hue=.1
17
+
18
+learning_rate=0.001
19
+burn_in=1000
20
+max_batches = 500200
21
+policy=steps
22
+steps=400000,450000
23
+scales=.1,.1
24
+
25
+[convolutional]
26
+batch_normalize=1
27
+filters=32
28
+size=3
29
+stride=1
30
+pad=1
31
+activation=leaky
32
+
33
+# Downsample
34
+
35
+[convolutional]
36
+batch_normalize=1
37
+filters=64
38
+size=3
39
+stride=2
40
+pad=1
41
+activation=leaky
42
+
43
+[convolutional]
44
+batch_normalize=1
45
+filters=32
46
+size=1
47
+stride=1
48
+pad=1
49
+activation=leaky
50
+
51
+[convolutional]
52
+batch_normalize=1
53
+filters=64
54
+size=3
55
+stride=1
56
+pad=1
57
+activation=leaky
58
+
59
+[shortcut]
60
+from=-3
61
+activation=linear
62
+
63
+# Downsample
64
+
65
+[convolutional]
66
+batch_normalize=1
67
+filters=128
68
+size=3
69
+stride=2
70
+pad=1
71
+activation=leaky
72
+
73
+[convolutional]
74
+batch_normalize=1
75
+filters=64
76
+size=1
77
+stride=1
78
+pad=1
79
+activation=leaky
80
+
81
+[convolutional]
82
+batch_normalize=1
83
+filters=128
84
+size=3
85
+stride=1
86
+pad=1
87
+activation=leaky
88
+
89
+[shortcut]
90
+from=-3
91
+activation=linear
92
+
93
+[convolutional]
94
+batch_normalize=1
95
+filters=64
96
+size=1
97
+stride=1
98
+pad=1
99
+activation=leaky
100
+
101
+[convolutional]
102
+batch_normalize=1
103
+filters=128
104
+size=3
105
+stride=1
106
+pad=1
107
+activation=leaky
108
+
109
+[shortcut]
110
+from=-3
111
+activation=linear
112
+
113
+# Downsample
114
+
115
+[convolutional]
116
+batch_normalize=1
117
+filters=256
118
+size=3
119
+stride=2
120
+pad=1
121
+activation=leaky
122
+
123
+[convolutional]
124
+batch_normalize=1
125
+filters=128
126
+size=1
127
+stride=1
128
+pad=1
129
+activation=leaky
130
+
131
+[convolutional]
132
+batch_normalize=1
133
+filters=256
134
+size=3
135
+stride=1
136
+pad=1
137
+activation=leaky
138
+
139
+[shortcut]
140
+from=-3
141
+activation=linear
142
+
143
+[convolutional]
144
+batch_normalize=1
145
+filters=128
146
+size=1
147
+stride=1
148
+pad=1
149
+activation=leaky
150
+
151
+[convolutional]
152
+batch_normalize=1
153
+filters=256
154
+size=3
155
+stride=1
156
+pad=1
157
+activation=leaky
158
+
159
+[shortcut]
160
+from=-3
161
+activation=linear
162
+
163
+[convolutional]
164
+batch_normalize=1
165
+filters=128
166
+size=1
167
+stride=1
168
+pad=1
169
+activation=leaky
170
+
171
+[convolutional]
172
+batch_normalize=1
173
+filters=256
174
+size=3
175
+stride=1
176
+pad=1
177
+activation=leaky
178
+
179
+[shortcut]
180
+from=-3
181
+activation=linear
182
+
183
+[convolutional]
184
+batch_normalize=1
185
+filters=128
186
+size=1
187
+stride=1
188
+pad=1
189
+activation=leaky
190
+
191
+[convolutional]
192
+batch_normalize=1
193
+filters=256
194
+size=3
195
+stride=1
196
+pad=1
197
+activation=leaky
198
+
199
+[shortcut]
200
+from=-3
201
+activation=linear
202
+
203
+
204
+[convolutional]
205
+batch_normalize=1
206
+filters=128
207
+size=1
208
+stride=1
209
+pad=1
210
+activation=leaky
211
+
212
+[convolutional]
213
+batch_normalize=1
214
+filters=256
215
+size=3
216
+stride=1
217
+pad=1
218
+activation=leaky
219
+
220
+[shortcut]
221
+from=-3
222
+activation=linear
223
+
224
+[convolutional]
225
+batch_normalize=1
226
+filters=128
227
+size=1
228
+stride=1
229
+pad=1
230
+activation=leaky
231
+
232
+[convolutional]
233
+batch_normalize=1
234
+filters=256
235
+size=3
236
+stride=1
237
+pad=1
238
+activation=leaky
239
+
240
+[shortcut]
241
+from=-3
242
+activation=linear
243
+
244
+[convolutional]
245
+batch_normalize=1
246
+filters=128
247
+size=1
248
+stride=1
249
+pad=1
250
+activation=leaky
251
+
252
+[convolutional]
253
+batch_normalize=1
254
+filters=256
255
+size=3
256
+stride=1
257
+pad=1
258
+activation=leaky
259
+
260
+[shortcut]
261
+from=-3
262
+activation=linear
263
+
264
+[convolutional]
265
+batch_normalize=1
266
+filters=128
267
+size=1
268
+stride=1
269
+pad=1
270
+activation=leaky
271
+
272
+[convolutional]
273
+batch_normalize=1
274
+filters=256
275
+size=3
276
+stride=1
277
+pad=1
278
+activation=leaky
279
+
280
+[shortcut]
281
+from=-3
282
+activation=linear
283
+
284
+# Downsample
285
+
286
+[convolutional]
287
+batch_normalize=1
288
+filters=512
289
+size=3
290
+stride=2
291
+pad=1
292
+activation=leaky
293
+
294
+[convolutional]
295
+batch_normalize=1
296
+filters=256
297
+size=1
298
+stride=1
299
+pad=1
300
+activation=leaky
301
+
302
+[convolutional]
303
+batch_normalize=1
304
+filters=512
305
+size=3
306
+stride=1
307
+pad=1
308
+activation=leaky
309
+
310
+[shortcut]
311
+from=-3
312
+activation=linear
313
+
314
+
315
+[convolutional]
316
+batch_normalize=1
317
+filters=256
318
+size=1
319
+stride=1
320
+pad=1
321
+activation=leaky
322
+
323
+[convolutional]
324
+batch_normalize=1
325
+filters=512
326
+size=3
327
+stride=1
328
+pad=1
329
+activation=leaky
330
+
331
+[shortcut]
332
+from=-3
333
+activation=linear
334
+
335
+
336
+[convolutional]
337
+batch_normalize=1
338
+filters=256
339
+size=1
340
+stride=1
341
+pad=1
342
+activation=leaky
343
+
344
+[convolutional]
345
+batch_normalize=1
346
+filters=512
347
+size=3
348
+stride=1
349
+pad=1
350
+activation=leaky
351
+
352
+[shortcut]
353
+from=-3
354
+activation=linear
355
+
356
+
357
+[convolutional]
358
+batch_normalize=1
359
+filters=256
360
+size=1
361
+stride=1
362
+pad=1
363
+activation=leaky
364
+
365
+[convolutional]
366
+batch_normalize=1
367
+filters=512
368
+size=3
369
+stride=1
370
+pad=1
371
+activation=leaky
372
+
373
+[shortcut]
374
+from=-3
375
+activation=linear
376
+
377
+[convolutional]
378
+batch_normalize=1
379
+filters=256
380
+size=1
381
+stride=1
382
+pad=1
383
+activation=leaky
384
+
385
+[convolutional]
386
+batch_normalize=1
387
+filters=512
388
+size=3
389
+stride=1
390
+pad=1
391
+activation=leaky
392
+
393
+[shortcut]
394
+from=-3
395
+activation=linear
396
+
397
+
398
+[convolutional]
399
+batch_normalize=1
400
+filters=256
401
+size=1
402
+stride=1
403
+pad=1
404
+activation=leaky
405
+
406
+[convolutional]
407
+batch_normalize=1
408
+filters=512
409
+size=3
410
+stride=1
411
+pad=1
412
+activation=leaky
413
+
414
+[shortcut]
415
+from=-3
416
+activation=linear
417
+
418
+
419
+[convolutional]
420
+batch_normalize=1
421
+filters=256
422
+size=1
423
+stride=1
424
+pad=1
425
+activation=leaky
426
+
427
+[convolutional]
428
+batch_normalize=1
429
+filters=512
430
+size=3
431
+stride=1
432
+pad=1
433
+activation=leaky
434
+
435
+[shortcut]
436
+from=-3
437
+activation=linear
438
+
439
+[convolutional]
440
+batch_normalize=1
441
+filters=256
442
+size=1
443
+stride=1
444
+pad=1
445
+activation=leaky
446
+
447
+[convolutional]
448
+batch_normalize=1
449
+filters=512
450
+size=3
451
+stride=1
452
+pad=1
453
+activation=leaky
454
+
455
+[shortcut]
456
+from=-3
457
+activation=linear
458
+
459
+# Downsample
460
+
461
+[convolutional]
462
+batch_normalize=1
463
+filters=1024
464
+size=3
465
+stride=2
466
+pad=1
467
+activation=leaky
468
+
469
+[convolutional]
470
+batch_normalize=1
471
+filters=512
472
+size=1
473
+stride=1
474
+pad=1
475
+activation=leaky
476
+
477
+[convolutional]
478
+batch_normalize=1
479
+filters=1024
480
+size=3
481
+stride=1
482
+pad=1
483
+activation=leaky
484
+
485
+[shortcut]
486
+from=-3
487
+activation=linear
488
+
489
+[convolutional]
490
+batch_normalize=1
491
+filters=512
492
+size=1
493
+stride=1
494
+pad=1
495
+activation=leaky
496
+
497
+[convolutional]
498
+batch_normalize=1
499
+filters=1024
500
+size=3
501
+stride=1
502
+pad=1
503
+activation=leaky
504
+
505
+[shortcut]
506
+from=-3
507
+activation=linear
508
+
509
+[convolutional]
510
+batch_normalize=1
511
+filters=512
512
+size=1
513
+stride=1
514
+pad=1
515
+activation=leaky
516
+
517
+[convolutional]
518
+batch_normalize=1
519
+filters=1024
520
+size=3
521
+stride=1
522
+pad=1
523
+activation=leaky
524
+
525
+[shortcut]
526
+from=-3
527
+activation=linear
528
+
529
+[convolutional]
530
+batch_normalize=1
531
+filters=512
532
+size=1
533
+stride=1
534
+pad=1
535
+activation=leaky
536
+
537
+[convolutional]
538
+batch_normalize=1
539
+filters=1024
540
+size=3
541
+stride=1
542
+pad=1
543
+activation=leaky
544
+
545
+[shortcut]
546
+from=-3
547
+activation=linear
548
+
549
+######################
550
+
551
+[convolutional]
552
+batch_normalize=1
553
+filters=512
554
+size=1
555
+stride=1
556
+pad=1
557
+activation=leaky
558
+
559
+[convolutional]
560
+batch_normalize=1
561
+size=3
562
+stride=1
563
+pad=1
564
+filters=1024
565
+activation=leaky
566
+
567
+[convolutional]
568
+batch_normalize=1
569
+filters=512
570
+size=1
571
+stride=1
572
+pad=1
573
+activation=leaky
574
+
575
+[convolutional]
576
+batch_normalize=1
577
+size=3
578
+stride=1
579
+pad=1
580
+filters=1024
581
+activation=leaky
582
+
583
+[convolutional]
584
+batch_normalize=1
585
+filters=512
586
+size=1
587
+stride=1
588
+pad=1
589
+activation=leaky
590
+
591
+[convolutional]
592
+batch_normalize=1
593
+size=3
594
+stride=1
595
+pad=1
596
+filters=1024
597
+activation=leaky
598
+
599
+[convolutional]
600
+size=1
601
+stride=1
602
+pad=1
603
+filters=255
604
+activation=linear
605
+
606
+
607
+[yolo]
608
+mask = 6,7,8
609
+anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
610
+classes=80
611
+num=9
612
+jitter=.3
613
+ignore_thresh = .7
614
+truth_thresh = 1
615
+random=1
616
+
617
+
618
+[route]
619
+layers = -4
620
+
621
+[convolutional]
622
+batch_normalize=1
623
+filters=256
624
+size=1
625
+stride=1
626
+pad=1
627
+activation=leaky
628
+
629
+[upsample]
630
+stride=2
631
+
632
+[route]
633
+layers = -1, 61
634
+
635
+
636
+
637
+[convolutional]
638
+batch_normalize=1
639
+filters=256
640
+size=1
641
+stride=1
642
+pad=1
643
+activation=leaky
644
+
645
+[convolutional]
646
+batch_normalize=1
647
+size=3
648
+stride=1
649
+pad=1
650
+filters=512
651
+activation=leaky
652
+
653
+[convolutional]
654
+batch_normalize=1
655
+filters=256
656
+size=1
657
+stride=1
658
+pad=1
659
+activation=leaky
660
+
661
+[convolutional]
662
+batch_normalize=1
663
+size=3
664
+stride=1
665
+pad=1
666
+filters=512
667
+activation=leaky
668
+
669
+[convolutional]
670
+batch_normalize=1
671
+filters=256
672
+size=1
673
+stride=1
674
+pad=1
675
+activation=leaky
676
+
677
+[convolutional]
678
+batch_normalize=1
679
+size=3
680
+stride=1
681
+pad=1
682
+filters=512
683
+activation=leaky
684
+
685
+[convolutional]
686
+size=1
687
+stride=1
688
+pad=1
689
+filters=255
690
+activation=linear
691
+
692
+
693
+[yolo]
694
+mask = 3,4,5
695
+anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
696
+classes=80
697
+num=9
698
+jitter=.3
699
+ignore_thresh = .7
700
+truth_thresh = 1
701
+random=1
702
+
703
+
704
+
705
+[route]
706
+layers = -4
707
+
708
+[convolutional]
709
+batch_normalize=1
710
+filters=128
711
+size=1
712
+stride=1
713
+pad=1
714
+activation=leaky
715
+
716
+[upsample]
717
+stride=2
718
+
719
+[route]
720
+layers = -1, 36
721
+
722
+
723
+
724
+[convolutional]
725
+batch_normalize=1
726
+filters=128
727
+size=1
728
+stride=1
729
+pad=1
730
+activation=leaky
731
+
732
+[convolutional]
733
+batch_normalize=1
734
+size=3
735
+stride=1
736
+pad=1
737
+filters=256
738
+activation=leaky
739
+
740
+[convolutional]
741
+batch_normalize=1
742
+filters=128
743
+size=1
744
+stride=1
745
+pad=1
746
+activation=leaky
747
+
748
+[convolutional]
749
+batch_normalize=1
750
+size=3
751
+stride=1
752
+pad=1
753
+filters=256
754
+activation=leaky
755
+
756
+[convolutional]
757
+batch_normalize=1
758
+filters=128
759
+size=1
760
+stride=1
761
+pad=1
762
+activation=leaky
763
+
764
+[convolutional]
765
+batch_normalize=1
766
+size=3
767
+stride=1
768
+pad=1
769
+filters=256
770
+activation=leaky
771
+
772
+[convolutional]
773
+size=1
774
+stride=1
775
+pad=1
776
+filters=255
777
+activation=linear
778
+
779
+
780
+[yolo]
781
+mask = 0,1,2
782
+anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
783
+classes=80
784
+num=9
785
+jitter=.3
786
+ignore_thresh = .7
787
+truth_thresh = 1
788
+random=1

tum/whitesports - Gogs: Simplico Git Service

暂无描述

IDNAEncoder.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * IDNA URL encoder
  4. *
  5. * Note: Not fully compliant, as nameprep does nothing yet.
  6. *
  7. * @package Requests
  8. * @subpackage Utilities
  9. * @see https://tools.ietf.org/html/rfc3490 IDNA specification
  10. * @see https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification
  11. */
  12. class Requests_IDNAEncoder {
  13. /**
  14. * ACE prefix used for IDNA
  15. *
  16. * @see https://tools.ietf.org/html/rfc3490#section-5
  17. * @var string
  18. */
  19. const ACE_PREFIX = 'xn--';
  20. /**#@+
  21. * Bootstrap constant for Punycode
  22. *
  23. * @see https://tools.ietf.org/html/rfc3492#section-5
  24. * @var int
  25. */
  26. const BOOTSTRAP_BASE = 36;
  27. const BOOTSTRAP_TMIN = 1;
  28. const BOOTSTRAP_TMAX = 26;
  29. const BOOTSTRAP_SKEW = 38;
  30. const BOOTSTRAP_DAMP = 700;
  31. const BOOTSTRAP_INITIAL_BIAS = 72;
  32. const BOOTSTRAP_INITIAL_N = 128;
  33. /**#@-*/
  34. /**
  35. * Encode a hostname using Punycode
  36. *
  37. * @param string $string Hostname
  38. * @return string Punycode-encoded hostname
  39. */
  40. public static function encode($string) {
  41. $parts = explode('.', $string);
  42. foreach ($parts as &$part) {
  43. $part = self::to_ascii($part);
  44. }
  45. return implode('.', $parts);
  46. }
  47. /**
  48. * Convert a UTF-8 string to an ASCII string using Punycode
  49. *
  50. * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`)
  51. * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`)
  52. * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`)
  53. * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`)
  54. *
  55. * @param string $string ASCII or UTF-8 string (max length 64 characters)
  56. * @return string ASCII string
  57. */
  58. public static function to_ascii($string) {
  59. // Step 1: Check if the string is already ASCII
  60. if (self::is_ascii($string)) {
  61. // Skip to step 7
  62. if (strlen($string) < 64) {
  63. return $string;
  64. }
  65. throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string);
  66. }
  67. // Step 2: nameprep
  68. $string = self::nameprep($string);
  69. // Step 3: UseSTD3ASCIIRules is false, continue
  70. // Step 4: Check if it's ASCII now
  71. if (self::is_ascii($string)) {
  72. // Skip to step 7
  73. if (strlen($string) < 64) {
  74. return $string;
  75. }
  76. throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string);
  77. }
  78. // Step 5: Check ACE prefix
  79. if (strpos($string, self::ACE_PREFIX) === 0) {
  80. throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string);
  81. }
  82. // Step 6: Encode with Punycode
  83. $string = self::punycode_encode($string);
  84. // Step 7: Prepend ACE prefix
  85. $string = self::ACE_PREFIX . $string;
  86. // Step 8: Check size
  87. if (strlen($string) < 64) {
  88. return $string;
  89. }
  90. throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string);
  91. }
  92. /**
  93. * Check whether a given string contains only ASCII characters
  94. *
  95. * @internal (Testing found regex was the fastest implementation)
  96. *
  97. * @param string $string
  98. * @return bool Is the string ASCII-only?
  99. */
  100. protected static function is_ascii($string) {
  101. return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1);
  102. }
  103. /**
  104. * Prepare a string for use as an IDNA name
  105. *
  106. * @todo Implement this based on RFC 3491 and the newer 5891
  107. * @param string $string
  108. * @return string Prepared string
  109. */
  110. protected static function nameprep($string) {
  111. return $string;
  112. }
  113. /**
  114. * Convert a UTF-8 string to a UCS-4 codepoint array
  115. *
  116. * Based on Requests_IRI::replace_invalid_with_pct_encoding()
  117. *
  118. * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`)
  119. * @param string $input
  120. * @return array Unicode code points
  121. */
  122. protected static function utf8_to_codepoints($input) {
  123. $codepoints = array();
  124. // Get number of bytes
  125. $strlen = strlen($input);
  126. // phpcs:ignore Generic.CodeAnalysis.JumbledIncrementer -- This is a deliberate choice.
  127. for ($position = 0; $position < $strlen; $position++) {
  128. $value = ord($input[$position]);
  129. // One byte sequence:
  130. if ((~$value & 0x80) === 0x80) {
  131. $character = $value;
  132. $length = 1;
  133. $remaining = 0;
  134. }
  135. // Two byte sequence:
  136. elseif (($value & 0xE0) === 0xC0) {
  137. $character = ($value & 0x1F) << 6;
  138. $length = 2;
  139. $remaining = 1;
  140. }
  141. // Three byte sequence:
  142. elseif (($value & 0xF0) === 0xE0) {
  143. $character = ($value & 0x0F) << 12;
  144. $length = 3;
  145. $remaining = 2;
  146. }
  147. // Four byte sequence:
  148. elseif (($value & 0xF8) === 0xF0) {
  149. $character = ($value & 0x07) << 18;
  150. $length = 4;
  151. $remaining = 3;
  152. }
  153. // Invalid byte:
  154. else {
  155. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
  156. }
  157. if ($remaining > 0) {
  158. if ($position + $length > $strlen) {
  159. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
  160. }
  161. for ($position++; $remaining > 0; $position++) {
  162. $value = ord($input[$position]);
  163. // If it is invalid, count the sequence as invalid and reprocess the current byte:
  164. if (($value & 0xC0) !== 0x80) {
  165. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
  166. }
  167. --$remaining;
  168. $character |= ($value & 0x3F) << ($remaining * 6);
  169. }
  170. $position--;
  171. }
  172. if (// Non-shortest form sequences are invalid
  173. $length > 1 && $character <= 0x7F
  174. || $length > 2 && $character <= 0x7FF
  175. || $length > 3 && $character <= 0xFFFF
  176. // Outside of range of ucschar codepoints
  177. // Noncharacters
  178. || ($character & 0xFFFE) === 0xFFFE
  179. || $character >= 0xFDD0 && $character <= 0xFDEF
  180. || (
  181. // Everything else not in ucschar
  182. $character > 0xD7FF && $character < 0xF900
  183. || $character < 0x20
  184. || $character > 0x7E && $character < 0xA0
  185. || $character > 0xEFFFD
  186. )
  187. ) {
  188. throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
  189. }
  190. $codepoints[] = $character;
  191. }
  192. return $codepoints;
  193. }
  194. /**
  195. * RFC3492-compliant encoder
  196. *
  197. * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code
  198. * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`)
  199. *
  200. * @param string $input UTF-8 encoded string to encode
  201. * @return string Punycode-encoded string
  202. */
  203. public static function punycode_encode($input) {
  204. $output = '';
  205. // let n = initial_n
  206. $n = self::BOOTSTRAP_INITIAL_N;
  207. // let delta = 0
  208. $delta = 0;
  209. // let bias = initial_bias
  210. $bias = self::BOOTSTRAP_INITIAL_BIAS;
  211. // let h = b = the number of basic code points in the input
  212. $h = 0;
  213. $b = 0; // see loop
  214. // copy them to the output in order
  215. $codepoints = self::utf8_to_codepoints($input);
  216. $extended = array();
  217. foreach ($codepoints as $char) {
  218. if ($char < 128) {
  219. // Character is valid ASCII
  220. // TODO: this should also check if it's valid for a URL
  221. $output .= chr($char);
  222. $h++;
  223. }
  224. // Check if the character is non-ASCII, but below initial n
  225. // This never occurs for Punycode, so ignore in coverage
  226. // @codeCoverageIgnoreStart
  227. elseif ($char < $n) {
  228. throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char);
  229. }
  230. // @codeCoverageIgnoreEnd
  231. else {
  232. $extended[$char] = true;
  233. }
  234. }
  235. $extended = array_keys($extended);
  236. sort($extended);
  237. $b = $h;
  238. // [copy them] followed by a delimiter if b > 0
  239. if (strlen($output) > 0) {
  240. $output .= '-';
  241. }
  242. // {if the input contains a non-basic code point < n then fail}
  243. // while h < length(input) do begin
  244. $codepointcount = count($codepoints);
  245. while ($h < $codepointcount) {
  246. // let m = the minimum code point >= n in the input
  247. $m = array_shift($extended);
  248. //printf('next code point to insert is %s' . PHP_EOL, dechex($m));
  249. // let delta = delta + (m - n) * (h + 1), fail on overflow
  250. $delta += ($m - $n) * ($h + 1);
  251. // let n = m
  252. $n = $m;
  253. // for each code point c in the input (in order) do begin
  254. for ($num = 0; $num < $codepointcount; $num++) {
  255. $c = $codepoints[$num];
  256. // if c < n then increment delta, fail on overflow
  257. if ($c < $n) {
  258. $delta++;
  259. }
  260. // if c == n then begin
  261. elseif ($c === $n) {
  262. // let q = delta
  263. $q = $delta;
  264. // for k = base to infinity in steps of base do begin
  265. for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) {
  266. // let t = tmin if k <= bias {+ tmin}, or
  267. // tmax if k >= bias + tmax, or k - bias otherwise
  268. if ($k <= ($bias + self::BOOTSTRAP_TMIN)) {
  269. $t = self::BOOTSTRAP_TMIN;
  270. }
  271. elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
  272. $t = self::BOOTSTRAP_TMAX;
  273. }
  274. else {
  275. $t = $k - $bias;
  276. }
  277. // if q < t then break
  278. if ($q < $t) {
  279. break;
  280. }
  281. // output the code point for digit t + ((q - t) mod (base - t))
  282. $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
  283. $output .= self::digit_to_char($digit);
  284. // let q = (q - t) div (base - t)
  285. $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t));
  286. } // end
  287. // output the code point for digit q
  288. $output .= self::digit_to_char($q);
  289. // let bias = adapt(delta, h + 1, test h equals b?)
  290. $bias = self::adapt($delta, $h + 1, $h === $b);
  291. // let delta = 0
  292. $delta = 0;
  293. // increment h
  294. $h++;
  295. } // end
  296. } // end
  297. // increment delta and n
  298. $delta++;
  299. $n++;
  300. } // end
  301. return $output;
  302. }
  303. /**
  304. * Convert a digit to its respective character
  305. *
  306. * @see https://tools.ietf.org/html/rfc3492#section-5
  307. * @throws Requests_Exception On invalid digit (`idna.invalid_digit`)
  308. *
  309. * @param int $digit Digit in the range 0-35
  310. * @return string Single character corresponding to digit
  311. */
  312. protected static function digit_to_char($digit) {
  313. // @codeCoverageIgnoreStart
  314. // As far as I know, this never happens, but still good to be sure.
  315. if ($digit < 0 || $digit > 35) {
  316. throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
  317. }
  318. // @codeCoverageIgnoreEnd
  319. $digits = 'abcdefghijklmnopqrstuvwxyz0123456789';
  320. return substr($digits, $digit, 1);
  321. }
  322. /**
  323. * Adapt the bias
  324. *
  325. * @see https://tools.ietf.org/html/rfc3492#section-6.1
  326. * @param int $delta
  327. * @param int $numpoints
  328. * @param bool $firsttime
  329. * @return int New bias
  330. *
  331. * function adapt(delta,numpoints,firsttime):
  332. */
  333. protected static function adapt($delta, $numpoints, $firsttime) {
  334. // if firsttime then let delta = delta div damp
  335. if ($firsttime) {
  336. $delta = floor($delta / self::BOOTSTRAP_DAMP);
  337. }
  338. // else let delta = delta div 2
  339. else {
  340. $delta = floor($delta / 2);
  341. }
  342. // let delta = delta + (delta div numpoints)
  343. $delta += floor($delta / $numpoints);
  344. // let k = 0
  345. $k = 0;
  346. // while delta > ((base - tmin) * tmax) div 2 do begin
  347. $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2);
  348. while ($delta > $max) {
  349. // let delta = delta div (base - tmin)
  350. $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN));
  351. // let k = k + base
  352. $k += self::BOOTSTRAP_BASE;
  353. } // end
  354. // return k + (((base - tmin + 1) * delta) div (delta + skew))
  355. return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW));
  356. }
  357. }