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
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
831
  
832
  
833
  
834
  
835
  
836
  
837
  
838
  
839
  
840
  
841
  
842
  
843
  
844
  
845
  
846
  
847
  
848
  
849
  
850
  
851
  
852
  
853
  
854
  
855
  
856
  
857
  
858
  
859
  
860
  
861
  
862
  
863
  
864
  
865
  
866
  
867
  
868
  
869
  
870
  
871
  
872
  
873
  
874
  
875
  
876
  
877
  
878
  
879
  
880
  
881
  
882
  
883
  
884
  
885
  
886
  
887
  
888
  
889
  
890
  
891
  
892
  
893
  
894
  
895
  
896
  
897
  
898
  
899
  
900
  
901
  
902
  
903
  
904
  
905
  
906
  
907
  
908
  
909
  
910
  
911
  
912
  
913
  
914
  
915
  
916
  
917
  
918
  
919
  
920
  
921
  
922
  
923
  
924
  
925
  
926
  
927
  
928
  
929
  
930
  
931
  
932
  
933
  
934
  
935
  
936
  
937
  
938
  
939
  
940
  
941
  
942
  
943
  
944
  
945
  
946
  
947
  
948
  
949
  
950
  
951
  
952
  
953
  
954
  
955
  
956
  
957
  
958
  
959
  
960
  
961
  
962
  
963
  
964
  
965
  
966
  
967
  
968
  
969
  
970
  
971
  
972
  
973
  
974
  
975
  
976
  
977
  
978
  
979
  
980
  
981
  
982
  
983
  
984
  
985
  
986
  
987
  
988
  
989
  
990
  
991
  
992
  
993
  
994
  
995
  
996
  
997
  
998
  
999
  
1000
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
3127
  
3128
  
3129
  
3130
  
3131
  
3132
  
3133
  
3134
  
3135
  
3136
  
3137
  
3138
  
3139
  
3140
  
3141
  
3142
  
3143
  
3144
  
3145
  
3146
  
3147
  
3148
  
3149
  
3150
  
3151
  
3152
  
3153
  
3154
  
3155
  
3156
  
3157
  
3158
  
3159
  
3160
  
3161
  
3162
  
3163
  
3164
  
3165
  
3166
  
3167
  
3168
  
3169
  
3170
  
3171
  
3172
  
3173
  
3174
  
3175
  
3176
  
3177
  
3178
  
3179
  
3180
  
3181
  
3182
  
3183
  
3184
  
3185
  
3186
  
3187
  
3188
  
3189
  
3190
  
3191
  
3192
  
3193
  
3194
  
3195
  
3196
  
3197
  
3198
  
3199
  
3200
  
3201
  
3202
  
3203
  
3204
  
3205
  
3206
  
3207
  
3208
  
3209
  
3210
  
3211
  
3212
  
3213
  
3214
  
3215
  
3216
  
3217
  
3218
  
3219
  
3220
  
3221
  
3222
  
3223
  
3224
  
3225
  
3226
  
3227
  
3228
  
3229
  
3230
  
3231
  
3232
  
3233
  
3234
  
3235
  
3236
  
3237
  
3238
  
3239
  
3240
  
3241
  
3242
  
3243
  
3244
  
3245
  
3246
  
3247
  
3248
  
3249
  
3250
  
3251
  
3252
  
3253
  
3254
  
3255
  
3256
  
3257
  
3258
  
3259
  
3260
  
3261
  
3262
  
3263
  
3264
  
3265
  
3266
  
3267
  
3268
  
3269
  
3270
  
3271
  
3272
  
3273
  
3274
  
3275
  
3276
  
3277
  
3278
  
3279
  
3280
  
3281
  
3282
  
3283
  
3284
  
3285
  
3286
  
3287
  
3288
  
3289
  
3290
  
3291
  
3292
  
3293
  
3294
  
3295
  
3296
  
3297
  
3298
  
3299
  
3300
  
3301
  
3302
  
3303
  
3304
  
3305
  
3306
  
3307
  
3308
  
3309
  
3310
  
3311
  
3312
  
3313
  
3314
  
3315
  
3316
  
3317
  
3318
  
3319
  
3320
  
3321
  
3322
  
3323
  
3324
  
3325
  
3326
  
3327
  
3328
  
3329
  
3330
  
3331
  
3332
  
3333
  
3334
  
3335
  
3336
  
3337
  
3338
  
3339
  
3340
  
3341
  
3342
  
3343
  
3344
  
3345
  
3346
  
3347
  
3348
  
3349
  
3350
  
3351
  
3352
  
3353
  
3354
  
3355
  
3356
  
3357
  
3358
  
3359
  
3360
  
3361
  
3362
  
3363
  
3364
  
3365
  
3366
  
3367
  
3368
  
3369
  
3370
  
3371
  
3372
  
3373
  
3374
  
3375
  
3376
  
3377
  
3378
  
3379
  
3380
  
3381
  
3382
  
3383
  
3384
  
3385
  
3386
  
3387
  
3388
  
3389
  
3390
  
3391
  
3392
  
3393
  
3394
  
3395
  
3396
  
3397
  
3398
  
3399
  
3400
  
3401
  
3402
  
3403
  
3404
  
3405
  
3406
  
3407
  
3408
  
3409
  
3410
  
3411
  
3412
  
3413
  
3414
  
3415
  
3416
  
3417
  
3418
  
3419
  
3420
  
3421
  
3422
  
3423
  
3424
  
3425
  
3426
  
3427
  
3428
  
3429
  
3430
  
3431
  
3432
  
3433
  
3434
  
3435
  
3436
  
3437
  
3438
  
3439
  
3440
  
3441
  
3442
  
3443
  
3444
  
3445
  
3446
  
3447
  
3448
  
3449
  
3450
  
3451
  
3452
  
3453
  
3454
  
3455
  
3456
  
3457
  
3458
  
3459
  
3460
  
3461
  
3462
  
3463
  
3464
  
3465
  
3466
  
3467
  
3468
  
3469
  
3470
  
3471
  
3472
  
3473
  
3474
  
3475
  
3476
  
3477
  
3478
  
3479
  
3480
  
3481
  
3482
  
3483
  
3484
  
3485
  
3486
  
3487
  
3488
  
3489
  
3490
  
3491
  
3492
  
3493
  
3494
  
3495
  
3496
  
3497
  
3498
  
3499
  
3500
  
3501
  
3502
  
3503
  
3504
  
3505
  
3506
  
3507
  
3508
  
3509
  
3510
  
3511
  
3512
  
3513
  
3514
  
3515
  
3516
  
3517
  
3518
  
3519
  
3520
  
3521
  
3522
  
3523
  
3524
  
3525
  
3526
  
3527
  
3528
  
3529
  
3530
  
3531
  
3532
  
3533
  
3534
  
3535
  
3536
  
3537
  
3538
  
3539
  
3540
  
3541
  
3542
  
3543
  
3544
  
3545
  
3546
  
3547
  
3548
  
3549
  
3550
  
3551
  
3552
  
3553
  
3554
  
3555
  
3556
  
3557
  
3558
  
3559
  
3560
  
3561
  
3562
  
3563
  
3564
  
3565
  
3566
  
3567
  
3568
  
3569
  
3570
  
3571
  
3572
  
3573
  
3574
  
3575
  
3576
  
3577
  
3578
  
3579
  
3580
  
3581
  
3582
  
3583
  
3584
  
3585
  
3586
  
3587
  
3588
  
3589
  
3590
  
3591
  
3592
  
3593
  
3594
  
3595
  
3596
  
3597
  
3598
  
3599
  
3600
  
3601
  
3602
  
3603
  
3604
  
3605
  
3606
  
3607
  
3608
  
3609
  
3610
  
3611
  
3612
  
3613
  
3614
  
3615
  
3616
  
3617
  
3618
  
3619
  
3620
  
3621
  
3622
  
3623
  
3624
  
3625
  
3626
  
3627
  
3628
  
3629
  
3630
  
3631
  
3632
  
3633
  
3634
  
3635
  
3636
  
3637
  
3638
  
3639
  
3640
  
3641
  
3642
  
3643
  
3644
  
3645
  
3646
  
3647
  
3648
  
3649
  
3650
  
3651
  
3652
  
3653
  
3654
  
3655
  
3656
  
3657
  
3658
  
3659
  
3660
  
3661
  
3662
  
3663
  
3664
  
3665
  
3666
  
3667
  
3668
  
3669
  
3670
  
3671
  
3672
  
3673
  
3674
  
3675
  
3676
  
3677
  
3678
  
3679
  
3680
  
3681
  
3682
  
3683
  
3684
  
3685
  
3686
  
3687
  
3688
  
3689
  
3690
  
3691
  
3692
  
3693
  
3694
  
3695
  
3696
  
3697
  
3698
  
3699
  
3700
  
3701
  
3702
  
3703
  
3704
  
3705
  
3706
  
3707
  
3708
  
3709
  
3710
  
3711
  
3712
  
3713
  
3714
  
3715
  
3716
  
3717
  
3718
  
3719
  
3720
  
3721
  
3722
  
3723
  
3724
  
3725
  
3726
  
3727
  
3728
  
3729
  
3730
  
3731
  
3732
  
3733
  
3734
  
3735
  
3736
  
3737
  
3738
  
3739
  
3740
  
3741
  
3742
  
3743
  
3744
  
3745
  
3746
  
3747
  
3748
  
3749
  
3750
  
3751
  
3752
  
3753
  
3754
  
3755
  
3756
  
3757
  
3758
  
3759
  
3760
  
3761
  
3762
  
3763
  
3764
  
3765
  
3766
  
3767
  
3768
  
3769
  
3770
  
3771
  
3772
  
3773
  
3774
  
3775
  
3776
  
3777
  
3778
  
3779
  
3780
  
3781
  
3782
  
3783
  
3784
  
3785
  
3786
  
3787
  
3788
  
3789
  
3790
  
3791
  
3792
  
3793
  
3794
  
3795
  
3796
  
3797
  
3798
  
3799
  
3800
  
3801
  
3802
  
3803
  
3804
  
3805
  
3806
  
3807
  
3808
  
3809
  
3810
  
3811
  
3812
  
3813
  
3814
  
3815
  
3816
  
3817
  
3818
  
3819
  
3820
  
3821
  
3822
  
3823
  
3824
  
3825
  
3826
  
3827
  
3828
  
3829
  
3830
  
3831
  
3832
  
3833
  
3834
  
3835
  
3836
  
3837
  
3838
  
3839
  
3840
  
3841
  
3842
  
3843
  
3844
  
3845
  
3846
  
3847
  
3848
  
3849
  
3850
  
3851
  
3852
  
3853
  
3854
  
3855
  
3856
  
3857
  
3858
  
3859
  
3860
  
3861
  
3862
  
3863
  
3864
  
3865
  
3866
  
3867
  
3868
  
3869
  
3870
  
3871
  
3872
  
3873
  
3874
  
3875
  
3876
  
3877
  
3878
  
3879
  
3880
  
3881
  
3882
  
3883
  
3884
  
3885
  
3886
  
3887
  
3888
  
3889
  
3890
  
3891
  
3892
  
3893
  
3894
  
3895
  
3896
  
3897
  
3898
  
3899
  
3900
  
3901
  
3902
  
3903
  
3904
  
3905
  
3906
  
3907
  
3908
  
3909
  
3910
  
3911
  
3912
  
3913
  
3914
  
3915
  
3916
  
3917
  
3918
  
3919
  
3920
  
3921
  
3922
  
3923
  
3924
  
3925
  
3926
  
3927
  
3928
  
3929
  
3930
  
3931
  
3932
  
3933
  
3934
  
3935
  
3936
  
3937
  
3938
  
3939
  
3940
  
3941
  
3942
  
3943
  
3944
  
3945
  
3946
  
3947
  
3948
  
3949
  
3950
  
3951
  
3952
  
3953
  
3954
  
3955
  
3956
  
3957
  
3958
  
3959
  
3960
  
3961
  
3962
  
3963
  
3964
  
3965
  
3966
  
3967
  
3968
  
3969
  
3970
  
3971
  
3972
  
3973
  
3974
  
3975
  
3976
  
3977
  
3978
  
3979
  
3980
  
3981
  
3982
  
3983
  
3984
  
3985
  
3986
  
3987
  
3988
  
3989
  
3990
  
3991
  
3992
  
3993
  
3994
  
3995
  
3996
  
3997
  
3998
  
3999
  
4000
  
4001
  
4002
  
4003
  
4004
  
4005
  
4006
  
4007
  
4008
  
4009
  
4010
  
4011
  
4012
  
4013
  
4014
  
4015
  
4016
  
4017
  
4018
  
4019
  
4020
  
4021
  
4022
  
4023
  
4024
  
4025
  
4026
  
4027
  
4028
  
4029
  
4030
  
4031
  
4032
  
4033
  
4034
  
4035
  
4036
  
4037
  
4038
  
4039
  
4040
  
4041
  
4042
  
4043
  
4044
  
4045
  
4046
  
4047
  
4048
  
4049
  
4050
  
4051
  
4052
  
4053
  
4054
  
4055
  
4056
  
4057
  
4058
  
4059
  
4060
  
4061
  
4062
  
4063
  
4064
  
4065
  
4066
  
4067
  
4068
  
4069
  
4070
  
4071
  
4072
  
4073
  
4074
  
4075
  
4076
  
4077
  
4078
  
4079
  
4080
  
4081
  
4082
  
4083
  
4084
  
4085
  
4086
  
4087
  
4088
  
4089
  
4090
  
4091
  
4092
  
4093
  
4094
  
4095
  
4096
  
4097
  
4098
  
4099
  
4100
  
4101
  
4102
  
4103
  
4104
  
4105
  
4106
  
4107
  
4108
  
4109
  
4110
  
4111
  
4112
  
4113
  
4114
  
4115
  
4116
  
4117
  
4118
  
4119
  
4120
  
4121
  
4122
  
4123
  
4124
  
4125
  
4126
  
4127
  
4128
  
4129
  
4130
  
4131
  
4132
  
4133
  
4134
  
4135
  
4136
  
4137
  
4138
  
4139
  
4140
  
4141
  
4142
  
4143
  
4144
  
4145
  
4146
  
4147
  
4148
  
4149
  
4150
  
4151
  
4152
  
4153
  
4154
  
4155
  
4156
  
4157
  
4158
  
4159
  
4160
  
4161
  
4162
  
4163
  
4164
  
4165
  
4166
  
4167
  
4168
  
4169
  
4170
  
4171
  
4172
  
4173
  
4174
  
4175
  
4176
  
4177
  
4178
  
4179
  
4180
  
4181
  
4182
  
4183
  
4184
  
4185
  
4186
  
4187
  
4188
  
4189
  
4190
  
4191
  
4192
  
4193
  
4194
  
4195
  
4196
  
4197
  
4198
  
4199
  
4200
  
4201
  
4202
  
4203
  
4204
  
4205
  
4206
  
4207
  
4208
  
4209
  
4210
  
4211
  
4212
  
4213
  
4214
  
4215
  
4216
  
4217
  
4218
  
4219
  
4220
  
4221
  
4222
  
4223
  
4224
  
4225
  
4226
  
4227
  
4228
  
4229
  
4230
  
4231
  
4232
  
4233
  
4234
  
4235
  
4236
  
4237
  
4238
  
4239
  
4240
  
4241
  
4242
  
4243
  
4244
  
4245
  
4246
  
4247
  
4248
  
4249
  
4250
  
4251
  
4252
  
4253
  
4254
  
4255
  
4256
  
4257
  
4258
  
4259
  
4260
  
4261
  
4262
  
4263
  
4264
  
4265
  
4266
  
4267
  
4268
  
4269
  
4270
  
4271
  
4272
  
4273
  
4274
  
4275
  
4276
  
4277
  
4278
  
4279
  
4280
  
4281
  
4282
  
4283
  
4284
  
4285
  
4286
  
4287
  
4288
  
4289
  
4290
  
4291
  
4292
  
4293
  
4294
  
4295
  
4296
  
4297
  
4298
  
4299
  
4300
  
4301
  
4302
  
4303
  
4304
  
4305
  
4306
  
4307
  
4308
  
4309
  
4310
  
4311
  
4312
  
4313
  
4314
  
4315
  
4316
  
4317
  
4318
  
4319
  
4320
  
4321
  
4322
  
4323
  
4324
  
4325
  
4326
  
4327
  
4328
  
4329
  
4330
  
4331
  
4332
  
4333
  
4334
  
4335
  
4336
  
4337
  
4338
  
4339
  
4340
  
4341
  
4342
  
4343
  
4344
  
4345
  
4346
  
4347
  
4348
  
4349
  
4350
  
4351
  
4352
  
4353
  
4354
  
4355
  
4356
  
4357
  
4358
  
4359
  
4360
  
4361
  
4362
  
4363
  
4364
  
4365
  
4366
  
4367
  
4368
  
4369
  
4370
  
4371
  
4372
  
4373
  
4374
  
4375
  
4376
  
4377
  
4378
  
4379
  
4380
  
4381
  
4382
  
4383
  
4384
  
4385
  
4386
  
4387
  
4388
  
4389
  
4390
  
4391
  
4392
  
4393
  
4394
  
4395
  
4396
  
4397
  
4398
  
4399
  
4400
  
4401
  
4402
  
4403
  
4404
  
4405
  
4406
  
4407
  
4408
  
4409
  
4410
  
4411
  
4412
  
4413
  
4414
  
4415
  
4416
  
4417
  
4418
  
4419
  
4420
  
4421
  
4422
  
4423
  
4424
  
4425
  
4426
  
4427
  
4428
  
4429
  
4430
  
4431
  
4432
  
4433
  
4434
  
4435
  
4436
  
4437
  
4438
  
4439
  
4440
  
4441
  
4442
  
4443
  
4444
  
4445
  
4446
  
4447
  
4448
  
4449
  
4450
  
4451
  
4452
  
4453
  
4454
  
4455
  
4456
  
4457
  
4458
  
4459
  
4460
  
4461
  
4462
  
4463
  
4464
  
4465
  
4466
  
4467
  
4468
  
4469
  
4470
  
4471
  
4472
  
4473
  
4474
  
4475
  
4476
  
4477
  
4478
  
4479
  
4480
  
4481
  
4482
  
4483
  
4484
  
4485
  
4486
  
4487
  
4488
  
4489
  
4490
  
4491
  
4492
  
4493
  
4494
  
4495
  
4496
  
4497
  
4498
  
4499
  
4500
  
4501
  
4502
  
4503
  
4504
  
4505
  
4506
  
4507
  
4508
  
4509
  
4510
  
4511
  
4512
  
4513
  
4514
  
4515
  
4516
  
4517
  
4518
  
4519
  
4520
  
4521
  
4522
  
4523
  
4524
  
4525
  
4526
  
4527
  
4528
  
4529
  
4530
  
4531
  
4532
  
4533
  
4534
  
4535
  
4536
  
4537
  
4538
  
4539
  
4540
  
4541
  
4542
  
4543
  
4544
  
4545
  
4546
  
4547
  
4548
  
4549
  
4550
  
4551
  
4552
  
4553
  
4554
  
4555
  
4556
  
4557
  
4558
  
4559
  
4560
  
4561
  
4562
  
4563
  
4564
  
4565
  
4566
  
4567
  
4568
  
4569
  
4570
  
4571
  
4572
  
4573
  
4574
  
4575
  
4576
  
4577
  
4578
  
4579
  
4580
  
4581
  
4582
  
4583
  
4584
  
4585
  
4586
  
4587
  
4588
  
4589
  
4590
  
4591
  
4592
  
4593
  
4594
  
4595
  
4596
  
4597
  
4598
  
4599
  
4600
  
4601
  
4602
  
4603
  
4604
  
4605
  
4606
  
4607
  
4608
  
4609
  
4610
  
4611
  
4612
  
4613
  
4614
  
4615
  
4616
  
4617
  
4618
  
4619
  
4620
  
4621
  
4622
  
4623
  
4624
  
4625
  
4626
  
4627
  
4628
  
4629
  
4630
  
4631
  
4632
  
4633
  
4634
  
4635
  
4636
  
4637
  
4638
  
4639
  
4640
  
4641
  
4642
  
4643
  
4644
  
4645
  
4646
  
4647
  
4648
  
4649
  
4650
  
4651
  
4652
  
4653
  
4654
  
4655
  
4656
  
4657
  
4658
  
4659
  
4660
  
4661
  
4662
  
4663
  
4664
  
4665
  
4666
  
4667
  
4668
  
4669
  
4670
  
4671
  
4672
  
4673
  
4674
  
4675
  
4676
  
4677
  
4678
  
4679
  
4680
  
4681
  
4682
  
4683
  
4684
  
4685
  
4686
  
4687
  
4688
  
4689
  
4690
  
4691
  
4692
  
4693
  
4694
  
4695
  
4696
  
4697
  
4698
  
4699
  
4700
  
4701
  
4702
  
4703
  
4704
  
4705
  
4706
  
4707
  
4708
  
4709
  
4710
  
4711
  
4712
  
4713
  
4714
  
4715
  
4716
  
4717
  
4718
  
4719
  
4720
  
4721
  
4722
  
4723
  
4724
  
4725
  
4726
  
4727
  
4728
  
4729
  
4730
  
4731
  
4732
  
4733
  
4734
  
4735
  
4736
  
4737
  
4738
  
4739
  
4740
  
4741
  
4742
  
4743
  
4744
  
4745
  
4746
  
4747
  
4748
  
4749
  
4750
  
4751
  
4752
  
4753
  
4754
  
4755
  
4756
  
4757
  
4758
  
4759
  
4760
  
4761
  
4762
  
4763
  
4764
  
4765
  
4766
  
4767
  
4768
  
4769
  
4770
  
4771
  
4772
  
4773
  
4774
  
4775
  
4776
  
4777
  
4778
  
4779
  
4780
  
4781
  
4782
  
4783
  
4784
  
4785
  
4786
  
4787
  
4788
  
4789
  
4790
  
4791
  
4792
  
4793
  
4794
  
4795
  
4796
  
4797
  
4798
  
4799
  
4800
  
4801
  
4802
  
4803
  
4804
  
4805
  
4806
  
4807
  
4808
  
4809
  
4810
  
4811
  
4812
  
4813
  
4814
  
4815
  
4816
  
4817
  
4818
  
4819
  
4820
  
4821
  
4822
  
4823
  
4824
  
4825
  
4826
  
4827
  
4828
  
4829
  
4830
  
4831
  
4832
  
4833
  
4834
  
4835
  
4836
  
4837
  
4838
  
4839
  
4840
  
4841
  
4842
  
4843
  
4844
  
4845
  
4846
  
4847
  
4848
  
4849
  
4850
  
4851
  
4852
  
4853
  
4854
  
4855
  
4856
  
4857
  
4858
  
4859
  
4860
  
4861
  
4862
  
4863
  
4864
  
4865
  
4866
  
4867
  
4868
  
4869
  
4870
  
4871
  
4872
  
4873
  
4874
  
4875
  
4876
  
4877
  
4878
  
4879
  
4880
  
4881
  
4882
  
4883
  
4884
  
4885
  
4886
  
4887
  
4888
  
4889
  
4890
  
4891
  
4892
  
4893
  
4894
  
4895
  
4896
  
4897
  
4898
  
4899
  
4900
  
4901
  
4902
  
4903
  
4904
  
4905
  
4906
  
4907
  
4908
  
4909
  
4910
  
4911
  
4912
  
4913
  
4914
  
4915
  
4916
  
4917
  
4918
  
4919
  
4920
  
4921
  
4922
  
4923
  
4924
  
4925
  
4926
  
4927
  
4928
  
4929
  
4930
  
4931
  
4932
  
4933
  
4934
  
4935
  
4936
  
4937
  
4938
  
4939
  
4940
  
4941
  
4942
  
4943
  
4944
  
4945
  
4946
  
4947
  
4948
  
4949
  
4950
  
4951
  
4952
  
4953
  
4954
  
4955
  
4956
  
4957
  
4958
  
4959
  
4960
  
4961
  
4962
  
4963
  
4964
  
4965
  
4966
  
4967
  
4968
  
4969
  
4970
  
4971
  
4972
  
4973
  
4974
  
4975
  
4976
  
4977
  
4978
  
4979
  
4980
  
4981
  
4982
  
4983
  
4984
  
4985
  
4986
  
4987
  
4988
  
4989
  
4990
  
4991
  
4992
  
4993
  
4994
  
4995
  
4996
  
4997
  
4998
  
4999
  
5000
  
5001
  
5002
  
5003
  
5004
  
5005
  
5006
  
5007
  
5008
  
5009
  
5010
  
5011
  
5012
  
5013
  
5014
  
5015
  
5016
  
5017
  
5018
  
5019
  
5020
  
5021
  
5022
  
5023
  
5024
  
5025
  
5026
  
5027
  
5028
  
5029
  
5030
  
5031
  
5032
  
5033
  
5034
  
5035
  
5036
  
5037
  
5038
  
5039
  
5040
  
5041
  
5042
  
5043
  
5044
  
5045
  
5046
  
5047
  
5048
  
5049
  
5050
  
5051
  
5052
  
5053
  
5054
  
5055
  
5056
  
5057
  
5058
  
5059
  
5060
  
5061
  
5062
  
5063
  
5064
  
5065
  
5066
  
5067
  
5068
  
5069
  
5070
  
5071
  
5072
  
5073
  
5074
  
5075
  
5076
  
5077
  
5078
  
5079
  
5080
  
5081
  
5082
  
5083
  
5084
  
5085
  
5086
  
5087
  
5088
  
5089
  
5090
  
5091
  
5092
  
5093
  
5094
  
5095
  
5096
  
5097
  
5098
  
5099
  
5100
  
5101
  
5102
  
5103
  
5104
  
5105
  
5106
  
5107
  
5108
  
5109
  
5110
  
5111
  
5112
  
5113
  
5114
  
5115
  
5116
  
5117
  
5118
  
5119
  
5120
  
5121
  
5122
  
5123
  
5124
  
5125
  
5126
  
5127
  
5128
  
5129
  
5130
  
5131
  
5132
  
5133
  
5134
  
5135
  
5136
  
5137
  
5138
  
5139
  
5140
  
5141
  
5142
  
5143
  
5144
  
5145
  
5146
  
5147
  
5148
  
5149
  
5150
  
5151
  
5152
  
5153
  
5154
  
5155
  
5156
  
5157
  
5158
  
5159
  
5160
  
5161
  
5162
  
5163
  
5164
  
5165
  
5166
  
5167
  
5168
  
5169
  
5170
  
5171
  
5172
  
5173
  
5174
  
5175
  
5176
  
5177
  
5178
  
5179
  
5180
  
5181
  
5182
  
5183
  
5184
  
5185
  
5186
  
5187
  
5188
  
5189
  
5190
  
5191
  
5192
  
5193
  
5194
  
5195
  
5196
  
5197
  
5198
  
5199
  
5200
  
5201
  
5202
  
5203
  
5204
  
5205
  
5206
  
5207
  
5208
  
5209
  
5210
  
5211
  
5212
  
5213
  
5214
  
5215
  
5216
  
5217
  
5218
  
5219
  
5220
  
5221
  
5222
  
5223
  
5224
  
5225
  
5226
  
5227
  
5228
  
5229
  
5230
  
5231
  
5232
  
5233
  
5234
  
5235
  
5236
  
5237
  
5238
  
5239
  
5240
  
5241
  
5242
  
5243
  
5244
  
5245
  
5246
  
5247
  
5248
  
5249
  
5250
  
5251
  
5252
  
5253
  
5254
  
5255
  
5256
  
5257
  
5258
  
5259
  
5260
  
5261
  
5262
  
5263
  
5264
  
5265
  
5266
  
5267
  
5268
  
5269
  
5270
  
5271
  
5272
  
5273
  
5274
  
5275
  
5276
  
5277
  
5278
  
5279
  
5280
  
5281
  
5282
  
5283
  
5284
  
5285
  
5286
  
5287
  
5288
  
5289
  
5290
  
5291
  
5292
  
5293
  
5294
  
5295
  
5296
  
5297
  
5298
  
5299
  
5300
  
5301
  
5302
  
5303
  
5304
  
5305
  
5306
  
5307
  
5308
  
5309
  
5310
  
5311
  
5312
  
5313
  
5314
  
5315
  
5316
  
5317
  
5318
  
5319
  
5320
  
5321
  
5322
  
5323
  
5324
  
5325
  
5326
  
5327
  
5328
  
5329
  
5330
  
5331
  
5332
  
5333
  
5334
  
5335
  
5336
  
5337
  
5338
  
5339
  
5340
  
5341
  
5342
  
5343
  
5344
  
5345
  
5346
  
5347
  
5348
  
5349
  
5350
  
5351
  
5352
  
5353
  
5354
  
5355
  
5356
  
5357
  
5358
  
5359
  
5360
  
5361
  
5362
  
5363
  
5364
  
5365
  
5366
  
5367
  
5368
  
5369
  
5370
  
5371
  
5372
  
5373
  
5374
  
5375
  
5376
  
5377
  
5378
  
5379
  
5380
  
5381
  
5382
  
5383
  
5384
  
5385
  
5386
  
5387
  
5388
  
5389
  
5390
  
5391
  
5392
  
5393
  
5394
  
5395
  
5396
  
5397
  
5398
  
5399
  
5400
  
5401
  
5402
  
5403
  
5404
  
5405
  
5406
  
5407
  
5408
  
5409
  
5410
  
5411
  
5412
  
5413
  
5414
  
5415
  
5416
  
5417
  
5418
  
5419
  
5420
  
5421
  
5422
  
5423
  
5424
  
5425
  
5426
  
5427
  
5428
  
5429
  
5430
  
5431
  
5432
  
5433
  
5434
  
5435
  
5436
  
5437
  
5438
  
5439
  
5440
  
5441
  
5442
  
5443
  
5444
  
5445
  
5446
  
5447
  
5448
  
5449
  
5450
  
5451
  
5452
  
5453
  
5454
  
5455
  
5456
  
5457
  
5458
  
5459
  
5460
  
5461
  
5462
  
5463
  
5464
  
5465
  
5466
  
5467
  
5468
  
5469
  
5470
  
5471
  
5472
  
5473
  
5474
  
5475
  
5476
  
5477
  
5478
  
5479
  
5480
  
5481
  
5482
  
5483
  
5484
  
5485
  
5486
  
5487
  
5488
  
5489
  
5490
  
5491
  
5492
  
5493
  
5494
  
5495
  
5496
  
5497
  
5498
  
5499
  
5500
  
5501
  
5502
  
5503
  
5504
  
5505
  
5506
  
5507
  
5508
  
5509
  
5510
  
5511
  
5512
  
5513
  
5514
  
5515
  
5516
  
5517
  
5518
  
5519
  
5520
  
5521
  
5522
  
5523
  
5524
  
5525
  
5526
  
5527
  
5528
  
5529
  
5530
  
5531
  
5532
  
5533
  
5534
  
5535
  
5536
  
5537
  
5538
  
5539
  
5540
  
5541
  
5542
  
5543
  
5544
  
5545
  
5546
  
5547
  
5548
  
5549
  
5550
  
5551
  
5552
  
5553
  
5554
  
5555
  
5556
  
5557
  
5558
  
5559
  
5560
  
5561
  
5562
  
5563
  
5564
  
5565
  
5566
  
5567
  
5568
  
5569
  
5570
  
5571
  
5572
  
5573
  
5574
  
5575
  
5576
  
5577
  
5578
  
5579
  
5580
  
5581
  
5582
  
5583
  
5584
  
5585
  
5586
  
5587
  
5588
  
5589
  
5590
  
5591
  
5592
  
5593
  
5594
  
5595
  
5596
  
5597
  
5598
  
5599
  
5600
  
5601
  
5602
  
5603
  
5604
  
5605
  
5606
  
5607
  
5608
  
5609
  
5610
  
5611
  
5612
  
5613
  
5614
  
5615
  
5616
  
5617
  
5618
  
5619
  
5620
  
5621
  
5622
  
5623
  
5624
  
5625
  
5626
  
5627
  
5628
  
5629
  
5630
  
5631
  
5632
  
5633
  
5634
  
5635
  
5636
  
5637
  
5638
  
5639
  
5640
  
5641
  
5642
  
5643
  
5644
  
5645
  
5646
  
5647
  
5648
  
5649
  
5650
  
5651
  
5652
  
5653
  
5654
  
5655
  
5656
  
5657
  
5658
  
5659
  
5660
  
5661
  
5662
  
5663
  
5664
  
5665
  
5666
  
5667
  
5668
  
5669
  
5670
  
5671
  
5672
  
5673
  
5674
  
5675
  
5676
  
5677
  
5678
  
5679
  
5680
  
5681
  
5682
  
5683
  
5684
  
5685
  
5686
  
5687
  
5688
  
5689
  
5690
  
5691
  
5692
  
5693
  
5694
  
5695
  
5696
  
5697
  
5698
  
5699
  
5700
  
5701
  
5702
  
5703
  
5704
  
5705
  
5706
  
5707
  
5708
  
5709
  
5710
  
5711
  
5712
  
5713
  
5714
  
5715
  
5716
  
5717
  
5718
  
5719
  
5720
  
5721
  
5722
  
5723
  
5724
  
5725
  
5726
  
5727
  
5728
  
5729
  
5730
  
5731
  
5732
  
5733
  
5734
  
5735
  
5736
  
5737
  
5738
  
5739
  
5740
  
5741
  
5742
  
5743
  
5744
  
5745
  
5746
  
5747
  
5748
  
5749
  
5750
  
5751
  
5752
  
5753
  
5754
  
5755
  
5756
  
5757
  
5758
  
5759
  
5760
  
5761
  
5762
  
5763
  
5764
  
5765
  
5766
  
5767
  
5768
  
5769
  
5770
  
5771
  
5772
  
5773
  
5774
  
5775
  
5776
  
5777
  
5778
  
5779
  
5780
  
5781
  
5782
  
5783
  
5784
  
5785
  
5786
  
5787
  
5788
  
5789
  
5790
  
5791
  
5792
  
5793
  
5794
  
5795
  
5796
  
5797
  
5798
  
5799
  
5800
  
5801
  
5802
  
5803
  
5804
  
5805
  
5806
  
5807
  
5808
  
5809
  
5810
  
5811
  
5812
  
5813
  
5814
  
5815
  
5816
  
5817
  
5818
  
5819
  
5820
  
5821
  
5822
  
5823
  
5824
  
5825
  
5826
  
5827
  
5828
  
5829
  
5830
  
5831
  
5832
  
5833
  
5834
  
5835
  
5836
  
5837
  
5838
  
5839
  
5840
  
5841
  
5842
  
5843
  
5844
  
5845
  
5846
  
5847
  
5848
  
5849
  
5850
  
5851
  
5852
  
5853
  
5854
  
5855
  
5856
  
5857
  
5858
  
5859
  
5860
  
5861
  
5862
  
5863
  
5864
  
5865
  
5866
  
5867
  
5868
  
5869
  
5870
  
5871
  
5872
  
5873
  
5874
  
5875
  
5876
  
5877
  
5878
  
5879
  
5880
  
5881
  
5882
  
5883
  
5884
  
5885
  
5886
  
5887
  
5888
  
5889
  
5890
  
5891
  
5892
  
5893
  
5894
  
5895
  
5896
  
5897
  
5898
  
5899
  
5900
  
5901
  
5902
  
5903
  
5904
  
5905
  
5906
  
5907
  
5908
  
5909
  
5910
  
5911
  
5912
  
5913
  
5914
  
5915
  
5916
  
5917
  
5918
  
5919
  
5920
  
5921
  
5922
  
5923
  
5924
  
5925
  
5926
  
5927
  
5928
  
5929
  
5930
  
5931
  
5932
  
5933
  
5934
  
5935
  
5936
  
5937
  
5938
  
5939
  
5940
  
5941
  
5942
  
5943
  
5944
  
5945
  
5946
  
5947
  
5948
  
5949
  
5950
  
5951
  
5952
  
5953
  
5954
  
5955
  
5956
  
5957
  
5958
  
5959
  
5960
  
5961
  
5962
  
5963
  
5964
  
5965
  
5966
  
5967
  
5968
  
5969
  
5970
  
5971
  
5972
  
5973
  
5974
  
5975
  
5976
  
5977
  
5978
  
5979
  
5980
  
5981
  
5982
  
5983
  
5984
  
5985
  
5986
  
5987
  
5988
  
5989
  
5990
  
5991
  
5992
  
5993
  
5994
  
5995
  
5996
  
5997
  
5998
  
5999
  
6000
  
6001
  
6002
  
6003
  
6004
  
6005
  
6006
  
6007
  
6008
  
6009
  
6010
  
6011
  
6012
  
6013
  
6014
  
6015
  
6016
  
6017
  
6018
  
6019
  
6020
  
6021
  
6022
  
6023
  
6024
  
6025
  
6026
  
6027
  
6028
  
6029
  
6030
  
6031
  
6032
  
6033
  
6034
  
6035
  
6036
  
6037
  
6038
  
6039
  
6040
  
6041
  
6042
  
6043
  
6044
  
6045
  
6046
  
6047
  
6048
  
6049
  
6050
  
6051
  
6052
  
6053
  
6054
  
6055
  
6056
  
6057
  
6058
  
6059
  
6060
  
6061
  
6062
  
6063
  
6064
  
6065
  
6066
  
6067
  
6068
  
6069
  
6070
  
6071
  
6072
  
6073
  
6074
  
6075
  
6076
  
6077
  
6078
  
6079
  
6080
  
6081
  
6082
  
6083
  
6084
  
6085
  
6086
  
6087
  
6088
  
6089
  
6090
  
6091
  
6092
  
6093
  
6094
  
6095
  
6096
  
6097
  
6098
  
6099
  
6100
  
6101
  
6102
  
6103
  
6104
  
6105
  
6106
  
6107
  
6108
  
6109
  
6110
  
6111
  
6112
  
6113
  
6114
  
6115
  
6116
  
6117
  
6118
  
6119
  
6120
  
6121
  
6122
  
6123
  
6124
  
6125
  
6126
  
6127
  
6128
  
6129
  
6130
  
6131
  
6132
  
6133
  
6134
  
6135
  
6136
  
6137
  
6138
  
6139
  
6140
  
6141
  
6142
  
6143
  
6144
  
6145
  
6146
  
6147
  
6148
  
6149
  
6150
  
6151
  
6152
  
6153
  
6154
  
6155
  
6156
  
6157
  
6158
  
6159
  
6160
  
6161
  
6162
  
6163
  
6164
  
6165
  
6166
  
6167
  
6168
  
6169
  
6170
  
6171
  
6172
  
6173
  
6174
  
6175
  
6176
  
6177
  
6178
  
6179
  
6180
  
6181
  
6182
  
6183
  
6184
  
6185
  
6186
  
6187
  
6188
  
6189
  
6190
  
6191
  
6192
  
6193
  
6194
  
6195
  
6196
  
6197
  
6198
  
6199
  
6200
  
6201
  
6202
  
6203
  
6204
  
6205
  
6206
  
6207
  
6208
  
6209
  
6210
  
6211
  
6212
  
6213
  
6214
  
6215
  
6216
  
6217
  
6218
  
6219
  
6220
  
6221
  
6222
  
6223
  
6224
  
6225
  
6226
  
6227
  
6228
  
6229
  
6230
  
6231
  
6232
  
6233
  
6234
  
6235
  
6236
  
6237
  
6238
  
6239
  
6240
  
6241
  
6242
  
6243
  
6244
  
6245
  
6246
  
6247
  
6248
  
6249
  
6250
  
6251
  
6252
  
6253
  
6254
  
6255
  
6256
  
6257
  
6258
  
6259
  
6260
  
6261
  
6262
  
6263
  
6264
  
6265
  
6266
  
6267
  
6268
  
6269
  
6270
  
6271
  
6272
  
6273
  
6274
  
6275
  
6276
  
6277
  
6278
  
6279
  
6280
  
6281
  
6282
  
6283
  
6284
  
6285
  
6286
  
6287
  
6288
  
6289
  
6290
  
6291
  
6292
  
6293
  
6294
  
6295
  
6296
  
6297
  
6298
  
6299
  
6300
  
6301
  
6302
  
6303
  
6304
  
6305
  
6306
  
6307
  
6308
  
6309
  
6310
  
6311
  
6312
  
6313
  
6314
  
6315
  
6316
  
6317
  
6318
  
6319
  
6320
  
6321
  
6322
  
6323
  
6324
  
6325
  
6326
  
6327
  
6328
  
6329
  
6330
  
6331
  
6332
  
6333
  
6334
  
6335
  
6336
  
6337
  
6338
  
6339
  
6340
  
6341
  
6342
  
6343
  
// This is a roxen pike module. Copyright © 1999 - 2009, Roxen IS. 
// 
// $Id$ 
 
#include <roxen.h> 
#include <config.h> 
#include <version.h> 
#include <module.h> 
#include <stat.h> 
#define roxen roxenp() 
 
#ifdef HTTP_DEBUG 
# define HTTP_WERR(X) report_debug("HTTP: "+X+"\n"); 
#else 
# define HTTP_WERR(X) 
#endif 
 
// Tell Pike.count_memory this is global. 
constant pike_cycle_depth = 0; 
 
// Error handling tools 
 
enum OnError { 
  THROW_INTERNAL = 0,   //! Throw a generic error. 
  THROW_RXML,           //! Throw an RXML run error. 
  LOG_ERROR,            //! Log the error and return @expr{0@} (zero). 
  RETURN_ZERO,          //! Return @expr{0@} (zero). 
}; 
//! Flags to control the error handling in various functions taking an 
//! argument of this type. 
//! 
//! Typical use is as an argument to a function that in turn 
//! calls @[raise_err()] in order to handle an error. 
//! 
//! @note 
//!   This covers only specific types of errors that the function might 
//!   generate. Other errors might throw internal exceptions or return 
//!   zero. See the function docs. 
//! 
//! @seealso 
//!   @[raise_err()] 
 
int(0..0) raise_err (OnError on_error, sprintf_format msg, 
                     sprintf_args... args) 
//! Trig an error according to @[on_error]. 
//! 
//! Typical use is as an expression in a @expr{return@} statement. 
//! 
//! @param on_error 
//!   Method to signal the error: 
//!   @int 
//!     @value THROW_INTERNAL 
//!       Throw a generic exception (@expr{"internal server error"@}). 
//!       Use this for error conditions that never should 
//!       happen if the code is correct. This is the default. 
//!    
//!     @value THROW_RXML 
//!       Throw the error as a RXML run error. 
//!       Convenient in rxml tag implementations. 
//!    
//!     @value LOG_ERROR 
//!       Print a message using @[report_error] and 
//!       return @expr{0@} (zero). 
//!    
//!     @value RETURN_ZERO 
//!       Just return @expr{0@} (zero). 
//!   @endint 
//! 
//! @param msg 
//!   Error message. 
//! 
//! @param args 
//!   @[sprintf()] parameters for @[msg] (if any). 
//! 
//! @returns 
//!   If the function returns, it will always be the 
//!   value @expr{0@} (zero). 
//! 
//! @seealso 
//!   @[OnError] 
{ 
  switch(on_error) { 
    case LOG_ERROR: report_error(msg, @args); break; 
    case RETURN_ZERO: break; 
    case THROW_RXML: RXML.run_error(msg, @args); 
    default: error(msg, @args); 
  } 
  return 0; 
} 
 
 
// Thunks to be able to access the cache from here, since this module 
// is compiled and instantiated before cache.pike. 
function cache_lookup = 
  lambda (mixed... args) { 
    return (cache_lookup = all_constants()["cache_lookup"]) (@args); 
  }; 
function cache_set = 
  lambda (mixed... args) { 
    return (cache_set = all_constants()["cache_set"]) (@args); 
  }; 
function cache_remove = 
  lambda (mixed... args) { 
    return (cache_remove = all_constants()["cache_remove"]) (@args); 
  }; 
 
object|array(object) parse_xml_tmpl( string ttag, string itag, 
                                     string xml_file, 
                                     string|void ident ) 
{ 
  string tmpl; 
  array(mapping) data = ({}); 
 
  Parser.HTML p = Parser.HTML(); 
 
  object apply_template( mapping data ) 
  { 
    Parser.HTML p = Parser.HTML(); 
    p->ignore_tags( 1 ); 
    p->_set_entity_callback( lambda( Parser.HTML p, string ent ) 
                             { 
                               string enc = "none"; 
                               sscanf( ent, "&%s;", ent ); 
                               sscanf( ent, "%s:%s", ent, enc ); 
                               sscanf( ent, "_.%s", ent ); 
                               switch( enc ) 
                               { 
                                 case "none": 
                                   return data[ ent ]; 
                                 case "int": 
                                   return (string)(int)data[ ent ]; 
                                 case "float": 
                                   return (string)(float)data[ ent ]; 
                                 case "string": 
                                 default: 
                                   return sprintf("%O", data[ent] ); 
                               } 
                             } ); 
    string code = p->feed( tmpl )->finish()->read(); 
    p = 0;                      // To avoid trampoline garbage. 
    return compile_string( code, xml_file )(); 
  }; 
 
   
  p->xml_tag_syntax( 2 ); 
  p->add_quote_tag ("!--", "", "--"); 
  p->add_container( ttag, 
                    lambda( Parser.HTML p, mapping m, string c ) 
                    { 
                      tmpl = c; 
                    } ); 
  p->add_container( itag, 
                    lambda( Parser.HTML p, mapping m, string c ) 
                    { 
                      string current_tag; 
                      mapping row = m; 
                      void got_tag( Parser.HTML p, string c ) 
                      { 
                        sscanf( c, "<%s>", c ); 
                        if( c[0] == '/' ) 
                          current_tag = 0; 
                        else 
                          current_tag = c; 
                      }; 
 
                      void got_data( Parser.HTML p, string c ) 
                      { 
                        if( current_tag ) 
                          if( row[current_tag] ) 
                            row[current_tag] += html_decode_string(c); 
                          else 
                            row[current_tag] = html_decode_string(c); 
                      }; 
                        
                      p = Parser.HTML( ); 
                      p->xml_tag_syntax( 2 ); 
                      p->add_quote_tag ("!--", "", "--") 
                        ->_set_tag_callback( got_tag ) 
                        ->_set_data_callback( got_data ) 
                        ->feed( c ) 
                        ->finish(); 
                      data += ({ row }); 
                      p = 0;    // To avoid trampoline garbage. 
                    } ) 
    ->feed( Stdio.read_file( xml_file ) ) 
    ->finish(); 
 
  p = 0;                        // To avoid trampoline garbage. 
 
  if( ident ) 
  { 
    foreach( data, mapping m ) 
      if( m->ident == ident ) 
        return apply_template( m ); 
    return 0; 
  } 
  return map( data, apply_template ); 
} 
 
object|array(object) parse_box_xml( string xml_file, string|void ident ) 
{ 
  return parse_xml_tmpl( "template", "box", xml_file, ident ); 
} 
 
int ip_to_int(string ip) 
{ 
  int res; 
  foreach(((ip/".") + ({ "0", "0", "0" }))[..3], string num) 
    res = (res<<8) | (int)num; 
  return res; 
} 
 
string http_roxen_config_cookie(string from) 
{ 
  return "RoxenConfig="+http_encode_cookie(from) 
    +"; expires=" + http_date (3600*24*365*2 + time (1)) + "; path=/"; 
} 
 
string http_roxen_id_cookie(void|string unique_id) 
{ 
  return "RoxenUserID=" + (unique_id || roxen->create_unique_id()) + "; expires=" + 
    http_date (3600*24*365*2 + time (1)) + "; path=/"; 
} 
 
protected mapping(string:function(string, RequestID:string)) cookie_callbacks = 
  ([]); 
protected class CookieChecker(string cookie) 
{ 
  string `()(string path, RequestID id) 
  { 
    if (!id->real_cookies) { 
      id->init_cookies(); 
    } 
    // Note: Access the real_cookies directly to avoid registering callbacks. 
    return id->real_cookies[cookie]; 
  } 
  string _sprintf(int c) 
  { 
    return c == 'O' && sprintf("CookieChecker(%O)", cookie); 
  } 
} 
function(string, RequestID:string) get_cookie_callback(string cookie) 
{ 
  function(string, RequestID:string) cb = cookie_callbacks[cookie]; 
  if (cb) return cb; 
  cb = CookieChecker(cookie); 
  return cookie_callbacks[cookie] = cb; 
} 
 
protected mapping(string:function(string, RequestID:string)) lang_callbacks = ([ ]); 
 
protected class LangChecker(multiset(string) known_langs, string header, 
                            string extra) 
{ 
  string `()(string path, RequestID id) 
  { 
    string proto_key = ""; 
 
    switch (header) { 
    case "accept-language": 
      //  Make sure the Accept-Language header has been parsed for this request 
      PrefLanguages pl = id->misc->pref_languages; 
      if (!pl) { 
        id->init_pref_languages(); 
        pl = id->misc->pref_languages; 
      } 
      proto_key = filter(pl->get_languages(), known_langs) * ","; 
      break; 
 
    case "cookie": 
      if (!id->real_cookies) 
        id->init_cookies(); 
       
      //  Avoid cookie jar tracking 
      if (string cookie_val = id->real_cookies[extra]) { 
        if (known_langs[cookie_val]) 
          proto_key = cookie_val; 
      } 
      break; 
    } 
     
    return proto_key; 
  } 
   
  string _sprintf(int c) 
  { 
    return (c == 'O') && sprintf("LangChecker(%O,%O,%O)", 
                                 indices(known_langs) * "+", header, extra); 
  } 
} 
 
function(string, RequestID:string) get_lang_vary_cb(multiset(string) known_langs, 
                                                    string header, string extra) 
{ 
  string key = sort(indices(known_langs)) * "+" + "|" + header + "|" + extra; 
  return 
    lang_callbacks[key] || 
    (lang_callbacks[key] = LangChecker(known_langs, header, extra)); 
} 
 
//! Return id->remoteaddr. 
//! 
//! Useful to use with @[RequestID()->register_vary_callback()]. 
string get_remoteaddr(string ignored, RequestID id) 
{ 
  return id->remoteaddr; 
} 
 
// These five functions are questionable, but rather widely used. 
string msectos(int t) 
{ 
  if(t<1000) // One sec. 
  { 
    return sprintf("0.%02d sec", t/10); 
  } else if(t<6000) {  // One minute 
    return sprintf("%d.%02d sec", t/1000, (t%1000 + 5) / 10); 
  } else if(t<3600000) { // One hour 
    return sprintf("%d:%02d m:s", t/60000,  (t%60000)/1000); 
  } 
  return sprintf("%d:%02d h:m", t/3600000, (t%3600000)/60000); 
} 
 
string decode_mode(int m) 
{ 
  string s; 
  s=""; 
 
  if(S_ISLNK(m))  s += "Symbolic link"; 
  else if(S_ISREG(m))  s += "File"; 
  else if(S_ISDIR(m))  s += "Dir"; 
  else if(S_ISSOCK(m)) s += "Socket"; 
  else if(S_ISFIFO(m)) s += "FIFO"; 
  else if((m&0xf000)==0xd000) s+="Door"; 
  else if(S_ISBLK(m))  s += "Device"; 
  else if(S_ISCHR(m))  s += "Special"; 
  else s+= "Unknown"; 
 
  s+=", "; 
 
  if(S_ISREG(m) || S_ISDIR(m)) 
  { 
    s+="<tt>"; 
    if(m&S_IRUSR) s+="r"; else s+="-"; 
    if(m&S_IWUSR) s+="w"; else s+="-"; 
    if(m&S_IXUSR) s+="x"; else s+="-"; 
 
    if(m&S_IRGRP) s+="r"; else s+="-"; 
    if(m&S_IWGRP) s+="w"; else s+="-"; 
    if(m&S_IXGRP) s+="x"; else s+="-"; 
 
    if(m&S_IROTH) s+="r"; else s+="-"; 
    if(m&S_IWOTH) s+="w"; else s+="-"; 
    if(m&S_IXOTH) s+="x"; else s+="-"; 
    s+="</tt>"; 
  } else { 
    s+="--"; 
  } 
  return s; 
} 
 
mapping(string:mixed) add_http_header(mapping(string:mixed) to, 
                                      string name, string value) 
//! Adds a header @[name] with value @[value] to the header style 
//! mapping @[to] (which commonly is @tt{id->defines[" _extra_heads"]@}) 
//! if no header with that value already exist. 
//! 
//! @note 
//! This function doesn't notify the RXML p-code cache, which makes it 
//! inappropriate to use for updating @tt{id->defines[" _extra_heads"]@} 
//! in RXML tags (which has been its primary use). Use 
//! @[RequestID.add_response_header] instead. 
{ 
  if(to[name]) { 
    if(arrayp(to[name])) { 
      if (search(to[name], value) == -1) 
        to[name] += ({ value }); 
    } else { 
      if (to[name] != value) 
        to[name] = ({ to[name], value }); 
    } 
  } 
  else 
    to[name] = value; 
  return to; 
} 
 
mapping(string:mixed) merge_http_headers (mapping(string:mixed) a, 
                                          mapping(string:mixed) b) 
//! Merges two response header mappings as if @[add_http_header] was 
//! called for @[a] with every header in @[b], except that it isn't 
//! destructive on @[a]. 
{ 
  mapping(string:mixed) res = a ^ b; 
  foreach (a & b; string name;) { 
    string|array(string) a_val = a[name], b_val = b[name]; 
    if (a_val == b_val) 
      // Shortcut for the string case (usually). This also ensures 
      // that same-string values don't become arrays with a single 
      // element. 
      res[name] = a_val; 
    else { 
      if (!arrayp (a_val)) a_val = ({a_val}); 
      if (!arrayp (b_val)) b_val = ({b_val}); 
      res[name] = a_val | b_val; 
    } 
  } 
  return res; 
} 
 
int is_mysql_keyword( string name ) 
//! Return true if the argument is a mysql keyword. 
//! Not in DBManager due to recursive module dependencies. 
{ 
  return (< 
      "action", "add", "aggregate", "all", "alter", "after", "and", "as", 
      "asc", "avg", "avg_row_length", "auto_increment", "between", "bigint", 
      "bit", "binary", "blob", "bool", "both", "by", "cascade", "case", 
      "char", "character", "change", "check", "checksum", "column", 
      "columns", "comment", "constraint", "create", "cross", "current_date", 
      "current_time", "current_timestamp", "data", "database", "databases", 
      "date", "datetime", "day", "day_hour", "day_minute", "day_second", 
      "dayofmonth", "dayofweek", "dayofyear", "dec", "decimal", "default", 
      "delayed", "delay_key_write", "delete", "desc", "describe", "distinct", 
      "distinctrow", "double", "drop", "end", "else", "escape", "escaped", 
      "enclosed", "enum", "explain", "exists", "fields", "file", "first", 
      "float", "float4", "float8", "flush", "foreign", "from", "for", "full", 
      "function", "global", "grant", "grants", "group", "having", "heap", 
      "high_priority", "hour", "hour_minute", "hour_second", "hosts", 
      "identified", "ignore", "in", "index", "infile", "inner", "insert", 
      "insert_id", "int", "integer", "interval", "int1", "int2", "int3", 
      "int4", "int8", "into", "if", "is", "isam", "join", "key", "keys", 
      "kill", "last_insert_id", "leading", "left", "length", "like", 
      "lines", "limit", "load", "local", "lock", "logs", "long", "longblob", 
      "longtext", "low_priority", "max", "max_rows", "match", "mediumblob", 
      "mediumtext", "mediumint", "middleint", "min_rows", "minute", 
      "minute_second", "modify", "month", "monthname", "myisam", "natural", 
      "numeric", "no", "not", "null", "on", "optimize", "option", 
      "optionally", "or", "order", "outer", "outfile", "pack_keys", 
      "partial", "password", "precision", "primary", "procedure", "process", 
      "processlist", "privileges", "read", "real", "references", "reload", 
      "regexp", "rename", "replace", "restrict", "returns", "revoke", 
      "rlike", "row", "rows", "second", "select", "set", "show", "shutdown", 
      "smallint", "soname", "sql_big_tables", "sql_big_selects", 
      "sql_low_priority_updates", "sql_log_off", "sql_log_update", 
      "sql_select_limit", "sql_small_result", "sql_big_result", 
      "sql_warnings", "straight_join", "starting", "status", "string", 
      "table", "tables", "temporary", "terminated", "text", "then", "time", 
      "timestamp", "tinyblob", "tinytext", "tinyint", "trailing", "to", 
      "type", "use", "using", "unique", "unlock", "unsigned", "update", 
      "usage", "values", "varchar", "variables", "varying", "varbinary", 
      "with", "write", "when", "where", "year", "year_month", "zerofill",       
  >)[ name ]; 
} 
 
string short_name(string|Configuration long_name) 
//! Given either a long name or a Configuration object, return a short 
//! (no longer than 20 characters) identifier. 
//! 
//! This function also does Unicode normalization and removes all 
//! 'non-character' characters from the name. The string is then 
//! utf8-encoded. 
{ 
  string id; 
  if( objectp( long_name ) ) 
  { 
    if( !long_name->name ) 
      error("Illegal first argument to short_name.\n" 
            "Expected Configuration object or string\n"); 
    long_name = long_name->name; 
  } 
 
  id = Unicode.split_words_and_normalize( lower_case(long_name) )*"_"; 
   
  if( strlen( id ) > 20 ) 
    id = (id[..16]+"_"+hash(id)->digits(36))[..19]; 
 
  if( !strlen( id ) ) 
    id = hash(long_name)->digits(36); 
 
  if( is_mysql_keyword( id ) ) 
    return "x"+id[..19]; 
 
  while( strlen(string_to_utf8( id )) > 20 ) 
    id = id[..strlen(id)-2]; 
 
  return string_to_utf8( id ); 
} 
 
int _match(string w, array (string) a) 
{ 
  if(!stringp(w)) // Internal request.. 
    return -1; 
  foreach(a, string q) 
    if(stringp(q) && strlen(q) && glob(q, w)) 
      return 1; 
} 
 
 
string canonicalize_http_header (string header) 
//! Returns the given http header on the canonical capitalization form 
//! as given in RFC 2616. E.g. @expr{"content-type"@} or 
//! @expr{"CONTENT-TYPE"@} is returned as @expr{"Content-Type"@}. 
//! Returns zero if the given string isn't a known http header. 
//! 
//! @seealso 
//! @[RequestID.add_response_header] 
{ 
  return ([ 
    // RFC 2616 section 4.5: General Header Fields 
    "cache-control":                "Cache-Control", 
    "connection":           "Connection", 
    "date":                 "Date", 
    "pragma":                       "Pragma", 
    "trailer":                      "Trailer", 
    "transfer-encoding":    "Transfer-Encoding", 
    "upgrade":                      "Upgrade", 
    "via":                  "Via", 
    "warning":                      "Warning", 
    // RFC 2616 section 5.3: Request Header Fields 
    "accept":                       "Accept", 
    "accept-charset":               "Accept-Charset", 
    "accept-encoding":              "Accept-Encoding", 
    "accept-language":              "Accept-Language", 
    "authorization":                "Authorization", 
    "expect":                       "Expect", 
    "from":                 "From", 
    "host":                 "Host", 
    "if-match":                     "If-Match", 
    "if-modified-since":    "If-Modified-Since", 
    "if-none-match":                "If-None-Match", 
    "if-range":                     "If-Range", 
    "if-unmodified-since":  "If-Unmodified-Since", 
    "max-forwards":         "Max-Forwards", 
    "proxy-authorization":  "Proxy-Authorization", 
    "range":                        "Range", 
    "referer":                      "Referer", 
    "te":                   "TE", 
    "user-agent":           "User-Agent", 
    // RFC 2616 section 6.2: Response Header Fields 
    "accept-ranges":                "Accept-Ranges", 
    "age":                  "Age", 
    "etag":                 "ETag", 
    "location":                     "Location", 
    "proxy-authenticate":   "Proxy-Authenticate", 
    "retry-after":          "Retry-After", 
    "server":                       "Server", 
    "vary":                 "Vary", 
    "www-authenticate":             "WWW-Authenticate", 
    // RFC 2616 section 7.1: Entity Header Fields 
    "allow":                        "Allow", 
    "content-encoding":             "Content-Encoding", 
    "content-language":             "Content-Language", 
    "content-length":               "Content-Length", 
    "content-location":             "Content-Location", 
    "content-md5":          "Content-MD5", 
    "content-range":                "Content-Range", 
    "content-type":         "Content-Type", 
    "expires":                      "Expires", 
    "last-modified":                "Last-Modified", 
    // The obsolete RFC 2068 defined this header for compatibility (19.7.1.1). 
    "keep-alive":           "Keep-Alive", 
    // RFC 2965 
    "cookie":                       "Cookie", 
    "cookie2":                      "Cookie2", 
    "set-cookie2":          "Set-Cookie2", 
  ])[lower_case (header)]; 
} 
 
mapping(string:mixed) http_low_answer( int status_code, string data ) 
//! Return a result mapping with the specified HTTP status code and 
//! data. @[data] is sent as the content of the response and is 
//! tagged as text/html. 
//! 
//! @note 
//! The constants in @[Protocols.HTTP] can be used for status codes. 
{ 
  if(!data) data=""; 
  HTTP_WERR("Return code "+status_code+" ("+data+")"); 
  return 
    ([ 
      "error" : status_code, 
      "data"  : data, 
      "len"   : strlen( data ), 
      "type"  : "text/html", 
      ]); 
} 
 
mapping(string:mixed) http_status (int status_code, 
                                   void|string message, mixed... args) 
//! Return a response mapping with the specified HTTP status code and 
//! optional message. As opposed to @[http_low_answer], the message is 
//! raw text which can be included in more types of responses, e.g. 
//! inside multistatus responses in WebDAV. The message may contain 
//! line feeds ('\n') and ISO-8859-1 characters in the ranges 32..126 
//! and 128..255. Line feeds are converted to spaces if the response 
//! format doesn't allow them. 
//! 
//! If @[args] is given, @[message] is taken as an @[sprintf] style 
//! format which is applied to them. 
{ 
  if (message) { 
    if (sizeof (args)) message = sprintf (message, @args); 
    HTTP_WERR ("Return status " + status_code + " " + message); 
    return (["error": status_code, "rettext": message]); 
  } 
  else { 
    HTTP_WERR ("Return status " + status_code); 
    return (["error": status_code]); 
  } 
} 
 
mapping(string:mixed) http_method_not_allowed ( 
  string allowed_methods, void|string message, mixed... args) 
//! Make a HTTP 405 method not allowed response with the required 
//! Allow header containing @[allowed_methods], which is a comma 
//! separated list of HTTP methods, e.g. @expr{"GET, HEAD"@}. 
{ 
  mapping(string:mixed) response = 
    http_status (Protocols.HTTP.HTTP_METHOD_INVALID, message, @args); 
  response->extra_heads = (["Allow": allowed_methods]); 
  return response; 
} 
 
//! Returns a response mapping indicating that the module or script 
//! will take over the rest of the handling of the request. This 
//! aborts the request chain and leaves you in control for as long as 
//! you wish. 
//! 
//! Typically, you'll want to return the control again by sending a 
//! new result mapping to @[RequestID.send_result()], but should you 
//! want to glue together request headers and close the socket on your 
//! own, you are free to do so. The method @[RequestID.connection()] 
//! gives you the Stdio.File object for the current client connection. 
  mapping(string:mixed) http_pipe_in_progress() 
{ 
  HTTP_WERR("Pipe in progress"); 
  return ([ "file":-1, "pipe":1, ]); 
} 
 
mapping(string:mixed) http_rxml_answer( string rxml, RequestID id, 
                                        void|Stdio.File file, 
                                        void|string type ) 
//! Convenience functions to use in Roxen modules. When you just want 
//! to return a string of data, with an optional type, this is the 
//! easiest way to do it if you don't want to worry about the internal 
//! roxen structures. 
{ 
  rxml =  
       ([function(string,RequestID,Stdio.File:string)]id->conf->parse_rxml) 
       (rxml, id, file); 
  HTTP_WERR("RXML answer ("+(type||"text/html")+")"); 
  return (["data":rxml, 
           "type":(type||"text/html"), 
           "stat":id->misc->defines[" _stat"], 
           "error":id->misc->defines[" _error"], 
           "rettext":id->misc->defines[" _rettext"], 
           "extra_heads":id->misc->defines[" _extra_heads"], 
           ]); 
} 
 
 
mapping(string:mixed) http_try_again( float delay ) 
//! Causes the request to be retried in @[delay] seconds. 
{ 
  return ([ "try_again_later":delay ]); 
} 
 
protected class Delayer 
{ 
  RequestID id; 
  int resumed; 
 
  void resume( ) 
  { 
    if( resumed ) 
      return; 
    remove_call_out( resume ); 
    resumed = 1; 
    if( !id ) 
      error("Cannot resume request -- connection close\n"); 
    roxenp()->handle( id->handle_request ); 
    id = 0; // free the reference. 
  } 
 
  void create( RequestID _id, float max_delay ) 
  { 
    id = _id; 
    if( max_delay && max_delay > 0.0 ) 
      call_out( resume, max_delay ); 
  } 
} 
 
array(object|mapping) http_try_resume( RequestID id, float|void max_delay ) 
//! Returns an object and a return mapping. 
//! Call 'retry' in the object to resume the request. 
//! Please note that this will cause your callback to be called again. 
//! An optional maximum delay time can be specified. 
//! 
//! @example 
//! void first_try( RequestID id ) 
//! { 
//!   if( !id->misc->has_logged_in ) 
//!   { 
//!     [object key, mapping result] = Roxen.http_try_resume( id, 10.0 ); 
//!     void do_the_work( ) 
//!     { 
//!        id->misc->have_logged_in = "no"; 
//!        if( connect_to_slow_id_host_and_get_login( id ) ) 
//!          id->misc->have_logged_in = "yes"; 
//!        key->resume(); 
//!     }; 
//!     thread_create( do_the_work, key ); 
//!     return result; 
//!   } 
//! } 
{ 
  Delayer delay = Delayer( id, max_delay ); 
  return ({delay, ([ "try_again_later":delay ]) }); 
} 
 
mapping(string:mixed) http_string_answer(string text, string|void type) 
//! Generates a result mapping with the given text as the request body 
//! with a content type of `type' (or "text/html" if none was given). 
{ 
  HTTP_WERR("String answer ("+(type||"text/html")+")"); 
  return ([ "data":text, "type":(type||"text/html") ]); 
} 
 
mapping(string:mixed) http_file_answer(Stdio.File text, 
                                       string|void type, void|int len) 
//! Generate a result mapping with the given (open) file object as the 
//! request body, the content type defaults to text/html if none is 
//! given, and the length to the length of the file object. 
{ 
  HTTP_WERR("file answer ("+(type||"text/html")+")"); 
  return ([ "file":text, "type":(type||"text/html"), "len":len ]); 
} 
 
protected constant months = ({ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }); 
protected constant days = ({ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }); 
 
string log_date(int t) { 
  mapping(string:int) lt = localtime(t); 
  return(sprintf("%04d-%02d-%02d", 
                 1900+lt->year,lt->mon+1, lt->mday)); 
} 
  
string log_time(int t) { 
  mapping(string:int) lt = localtime(t); 
  return(sprintf("%02d:%02d:%02d", 
                 lt->hour, lt->min, lt->sec)); 
} 
 
// CERN date formatter. Note similar code in LogFormat in roxen.pike. 
 
protected int chd_lt; 
protected string chd_lf; 
 
string cern_http_date(int t) 
//! Return a date, formated to be used in the common log format 
{ 
  if( t == chd_lt ) 
    // Interpreter lock assumed here. 
    return chd_lf; 
 
  string c; 
  mapping(string:int) lt = localtime(t); 
  int tzh = lt->timezone/3600; 
  if(tzh > 0) 
    c="-"; 
  else { 
    tzh = -tzh; 
    c="+"; 
  } 
 
  c = sprintf("%02d/%s/%04d:%02d:%02d:%02d %s%02d00", 
              lt->mday, months[lt->mon], 1900+lt->year, 
              lt->hour, lt->min, lt->sec, c, tzh); 
 
  chd_lt = t; 
  // Interpreter lock assumed here. 
  chd_lf = c; 
 
  return c; 
} 
 
constant http_status_messages = ([ 
  100:"Continue", 
  101:"Switching Protocols", 
  102:"Processing", 
 
  200:"OK", 
  201:"Created",            // URI follows 
  202:"Accepted", 
  203:"Non-Authoritative Information",      // Provisional Information 
  204:"No Content", 
  205:"Reset Content", 
  206:"Partial Content",    // Byte Ranges 
  207:"Multi-Status", 
  226:"IM Used",            // RFC 3229 
 
  300:"Multiple Choices",   // Moved 
  301:"Moved Permanently",  // Permanent Relocation 
  302:"Found", 
  303:"See Other", 
  304:"Not Modified", 
  305:"Use Proxy", 
  // RFC 2616 10.3.7: 306 not used but reserved. 
  307:"Temporary Redirect", 
 
  400:"Bad Request", 
  401:"Unauthorized",               // Access denied 
  402:"Payment Required", 
  403:"Forbidden", 
  404:"Not Found",          // No such file or directory 
  405:"Method Not Allowed", 
  406:"Not Acceptable", 
  407:"Proxy Authentication Required", // Proxy authorization needed 
  408:"Request Timeout", 
  409:"Conflict", 
  410:"Gone",                       // This document is no more. It has gone to meet its creator. It is gone. It will not be back. Give up. I promise. There is no such file or directory.", 
  411:"Length Required", 
  412:"Precondition Failed", 
  413:"Request Entity Too Large", 
  414:"Request-URI Too Long", 
  415:"Unsupported Media Type", 
  416:"Requested Range Not Satisfiable", 
  417:"Expectation Failed", 
  418:"I'm a teapot", 
  // FIXME: What is 419? 
  420:"Server temporarily unavailable", 
  421:"Server shutting down at operator request", 
  422:"Unprocessable Entity", 
  423:"Locked", 
  424:"Failed Dependency", 
 
  500:"Internal Server Error.", 
  501:"Not Implemented", 
  502:"Bad Gateway",                // Gateway Timeout 
  503:"Service Unavailable", 
  504:"Gateway Timeout", 
  505:"HTTP Version Not Supported", 
  506:"Variant Also Negotiates", 
  507:"Insufficient Storage", 
]); 
 
string http_status_message (int status_code) 
//! Returns the standard message that corresponds to the given HTTP 
//! status code. 
{ 
  return http_status_messages[status_code]; 
} 
 
string http_date( mixed t ) 
//! Returns a http_date, as specified by the HTTP-protocol standard. 
//! This is used for logging as well as the Last-Modified and Time 
//! headers in the reply. 
{ 
  mapping(string:int) l = gmtime( (int)t ); 
  return(sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", 
                 days[l->wday], l->mday, months[l->mon], 1900+l->year, 
                 l->hour, l->min, l->sec)); 
} 
 
string parse_http_response (string response, 
                            void|mapping(string:mixed) response_map, 
                            void|mapping(string:string) headers, 
                            void|int|string on_error) 
//! Parses a raw http response and converts it to a response mapping 
//! suitable to return from @[RoxenModule.find_file] etc. 
//! 
//! The charset, if any is found, is used to decode the body. If a 
//! charset isn't found in the Content-Type header, some heuristics is 
//! used on the body to try to find one. 
//! 
//! @param response 
//!   The raw http response message, starting with formatted headers 
//!   that are terminated by an empty line. 
//! 
//! @param response_map 
//!   If this is set, it's filled in as a response mapping. The body 
//!   of the response is included in @expr{@[response_map]->data@}. 
//! 
//! @param headers 
//!   If this is set, it's filled in with all the http headers from 
//!   the response. The indices are lowercased, but otherwise the 
//!   headers aren't processed much (see also @[_Roxen.HeaderParser]). 
//! 
//! @param on_error 
//!   What to do if a parse error occurs. Throws a normal error if 
//!   zero, throws an RXML run error if 1, or ignores it and tries to 
//!   recover if -1. If it's a string then it's logged in the debug 
//!   log with the string inserted to explain the context. 
//! 
//! @returns 
//! Returns the body of the response message, with charset decoded if 
//! applicable. 
{ 
  array parsed = Roxen.HeaderParser()->feed (response); 
  if (!parsed) { 
    string err_msg = "Could not find http headers.\n"; 
    if (stringp (on_error)) 
      werror ("Error parsing http response%s: %s", 
              on_error != "" ? " " + on_error : "", err_msg); 
    else if (on_error == 0) 
      error (err_msg); 
    else if (on_error == 1) 
      RXML.run_error (err_msg); 
    return response; 
  } 
 
  mapping(string:string) hdr = parsed[2]; 
  if (headers) 
    foreach (hdr; string name; string val) 
      headers[name] = val; 
 
  return low_parse_http_response (hdr, parsed[0], response_map, on_error); 
} 
 
string low_parse_http_response (mapping(string:string) headers, 
                                string body, 
                                void|mapping(string:mixed) response_map, 
                                void|int|string on_error, 
                                void|int(0..1) ignore_unknown_ce) 
//! Similar to @[parse_http_response], but takes a http response 
//! message that has been split into headers in @[headers] and the 
//! message body in @[body]. 
//! 
//! The indices in @[headers] are assumed to be in lower case. 
//! 
//! @param ignore_unknown_ce 
//!   If set, unknown Content-Encoding headers will be ignored and 
//!   parsing will continue on the verbatim body data. 
{ 
  string err_msg; 
 
proc: { 
    if (response_map) { 
      if (string lm = headers["last-modified"]) 
        // Let's just ignore parse errors in the date. 
        response_map->last_modified = parse_since (lm)[0]; 
    } 
 
    string type, subtype, charset; 
 
    if (string ct = headers["content-type"]) { 
      // Use the MIME module to parse the Content-Type header. It 
      // doesn't need the data. 
      MIME.Message m = MIME.Message ("", (["content-type": ct]), 0, 1); 
      type = m->type; 
      subtype = m->subtype; 
      charset = m->charset; 
      if (charset == "us-ascii" && !has_value (lower_case (ct), "us-ascii")) 
        // MIME.Message is a bit too "convenient" and defaults to 
        // "us-ascii" if no charset is specified. 
        charset = 0; 
      if (response_map) 
        response_map->type = type + "/" + subtype; 
    } 
 
    if (string ce = headers["content-encoding"]) { 
      switch(lower_case(ce)) { 
      case "gzip": 
        { 
          Stdio.FakeFile f = Stdio.FakeFile(body, "rb"); 
          Gz.File gz = Gz.File(f, "rb"); 
          body = gz->read(); 
        } 
        break; 
      case "deflate": 
        body = Gz.inflate(-15)->inflate(body); 
        break; 
      default: 
        if (!ignore_unknown_ce) { 
          err_msg = sprintf("Content-Encoding %O not supported.\n", ce); 
          break proc; 
        } 
      } 
    } 
 
    if (!charset) { 
      // Guess the charset from the content. Adapted from insert#href, 
      // insert#cached-href and SiteBuilder.pmod. 
      if (type == "text" || 
          (type == "application" && 
           (subtype == "xml" || has_prefix (subtype || "", "xml-")))) { 
 
        if (subtype == "html") { 
          Parser.HTML parser = Parser.HTML(); 
          parser->case_insensitive_tag(1); 
          parser->lazy_entity_end(1); 
          parser->ignore_unknown(1); 
          parser->match_tag(0); 
          parser->add_quote_tag ("!--", "", "--"); 
          parser->add_tag ( 
            "meta", 
            lambda (Parser.HTML p, mapping m) 
            { 
              string val = m->content; 
              if(val && m["http-equiv"] && 
                 lower_case(m["http-equiv"]) == "content-type") { 
                MIME.Message m = 
                  MIME.Message ("", (["content-type": val]), 0, 1); 
                charset = m->charset; 
                if (charset == "us-ascii" && 
                    !has_value (lower_case (val), "us-ascii")) 
                  charset = 0; 
                throw (0);      // Done. 
              } 
            }); 
          if (mixed err = catch (parser->finish (body))) { 
            err_msg = describe_error (err); 
            break proc; 
          } 
        } 
 
        else if (subtype == "xml" || has_prefix (subtype || "", "xml-")) { 
          // Look for BOM, then an xml header. The BOM is stripped off 
          // since we use it to decode the data here. 
          if (sscanf (body, "\xef\xbb\xbf%s", body)) 
            charset = "utf-8"; 
          else if (sscanf (body, "\xfe\xff%s", body)) 
            charset = "utf-16"; 
          else if (sscanf (body, "\xff\xfe\x00\x00%s", body)) 
            charset = "utf-32le"; 
          else if (sscanf (body, "\xff\xfe%s", body)) 
            charset = "utf-16le"; 
          else if (sscanf (body, "\x00\x00\xfe\xff%s", body)) 
            charset = "utf-32"; 
 
          else if (sizeof(body) > 6 && 
                   has_prefix(body, "<?xml") && 
                   Parser.XML.isspace(body[5]) && 
                   sscanf(body, "<?%s?>", string hdr)) { 
            hdr += "?"; 
            if (sscanf(lower_case(hdr), "%*sencoding=%s%*[\n\r\t ?]", 
                       string xml_enc) == 3) 
              charset = xml_enc - "'" - "\""; 
          } 
        } 
      } 
    } 
 
    // FIXME: Parse away BOM in xml documents also when the charset 
    // already is known. 
 
    if (charset) { 
      Charset.Decoder decoder; 
      if (mixed err = catch (decoder = Charset.decoder (charset))) { 
        err_msg = sprintf ("Unrecognized charset %q.\n", charset); 
        break proc; 
      } 
      if (mixed err = catch (body = decoder->feed (body)->drain())) { 
        if (objectp (err) && err->is_charset_decode_error) { 
          err_msg = describe_error (err); 
          break proc; 
        } 
        throw (err); 
      } 
    } 
 
    if (response_map) 
      response_map->data = body; 
 
    return body; 
  } 
 
  // Get here on error. 
  if (stringp (on_error)) 
    werror ("Error parsing http response%s: %s", 
            on_error != "" ? " " + on_error : "", err_msg); 
  else if (on_error == 0) 
    error (err_msg); 
  else if (on_error == 1) 
    RXML.run_error ("Error parsing http response: " + err_msg); 
 
  return body; 
} 
 
//! Returns a timestamp formatted according to ISO 8601 Date and Time 
//! RFC 2518 23.2. No fraction, UTC only. 
string iso8601_date_time(int ts, int|void ns) 
{ 
  mapping(string:int) gmt = gmtime(ts); 
  if (zero_type(ns)) { 
    return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", 
                   1900 + gmt->year, gmt->mon+1, gmt->mday, 
                   gmt->hour, gmt->min, gmt->sec); 
  } 
  return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%09dZ", 
                 1900 + gmt->year, gmt->mon+1, gmt->mday, 
                 gmt->hour, gmt->min, gmt->sec, ns); 
} 
 
#if !defined (MODULE_DEBUG) ||                                                \ 
  defined (ENABLE_INHERENTLY_BROKEN_HTTP_ENCODE_STRING_FUNCTION) 
// Since http_encode_string is broken by design we don't define it in 
// module debug mode, so that modules still using it can be detected 
// easily during compilation. If you for some reason choose to 
// disregard the STRONG deprecation of this function, then you can use 
// the other define above to always enable it. 
string http_encode_string(string f) 
//! Encode dangerous characters in a string so that it can be used as 
//! a URL. Specifically, nul, space, tab, newline, linefeed, %, ' and 
//! " are quoted. 
//! 
//! @note 
//! This function is STRONGLY deprecated since using it almost 
//! invariably leads to incorrect encoding: It doesn't encode URI 
//! special chars like "/", ":", "?" etc, presumably with the 
//! intention to be used on an entire URI string. Still, since it 
//! encodes "%", that URI string can't contain any prior encoded chars 
//! from the URI component strings. Thus, the result is that "%" 
//! easily gets incorrectly double-encoded with this function. 
//! 
//! Either use @[http_encode_url] to encode the URI component strings 
//! before they are pasted together to the complete URI, or use 
//! @[http_encode_invalids] on the complete URI to only encode any 
//! chars that can't occur raw in the HTTP protocol. 
{ 
  return replace(f, ({ "\000", " ", "\t", "\n", "\r", "%", "'", "\"" }), 
                 ({"%00", "%20", "%09", "%0A", "%0D", "%25", "%27", "%22"})); 
} 
#endif 
 
string http_encode_invalids (string f) 
//! Encode dangerous chars to be included as a URL in an HTTP message 
//! or header field. This includes control chars, space and the quote 
//! chars @expr{'@} and @expr{"@}. Note that chars allowed in a quoted 
//! string (RFC 2616 section 2.2) are not encoded. This function may 
//! be used on a complete URI since it doesn't encode any URI special 
//! chars, including the escape char @expr{%@}. 
//! 
//! @note 
//! Eight bit chars and wider are encoded using UTF-8 followed by http 
//! escaping, as mandated by RFC 3987, section 3.1 and appendix B.2 in 
//! the HTML 4.01 standard 
//! (http://www.w3.org/TR/html4/appendix/notes.html#non-ascii-chars). 
//! (It should work regardless of the charset used in the XML document 
//! the URL might be inserted into.) 
//! 
//! @seealso 
//! @[http_encode_url] 
{ 
  return replace ( 
    string_to_utf8 (f), ({ 
      // Encode all chars outside the set of reserved characters 
      // (RFC 3986, section 2.2) and unreserved chars (section 2.3). 
      // 
      // Control chars 
      "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007", 
      "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017", 
      "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027", 
      "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037", 
      "\177", 
      // Others 
      " ", "\"", 
      // Encoded by legacy (presumably since it's used to delimit 
      // attributes in xml). The single quote is valid but may be 
      // escaped without changing its meaning in URI's according to 
      // RFC 2396 section 2.3. FIXME: In the successor RFC 3986 it is 
      // however part of the reserved set and ought therefore not be 
      // encoded. 
      "'", 
      // FIXME: The following chars are invalid according to RFC 3986, 
      // but can we add them without compatibility woes? 
      //"<", ">", "\\", "^", "`", "{", "|", "}", 
      // All eight bit chars (this is fast with the current replace() 
      // implementation). 
      "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207", 
      "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217", 
      "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227", 
      "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237", 
      "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247", 
      "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257", 
      "\260", "\261", "\262", "\263", "\264", "\265", "\266", "\267", 
      "\270", "\271", "\272", "\273", "\274", "\275", "\276", "\277", 
      "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307", 
      "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317", 
      "\320", "\321", "\322", "\323", "\324", "\325", "\326", "\327", 
      "\330", "\331", "\332", "\333", "\334", "\335", "\336", "\337", 
      "\340", "\341", "\342", "\343", "\344", "\345", "\346", "\347", 
      "\350", "\351", "\352", "\353", "\354", "\355", "\356", "\357", 
      "\360", "\361", "\362", "\363", "\364", "\365", "\366", "\367", 
      "\370", "\371", "\372", "\373", "\374", "\375", "\376", "\377", 
    }), 
    ({ 
      "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", 
      "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F", 
      "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", 
      "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F", 
      "%7F", 
      "%20", "%22", 
      "%27", 
      "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", 
      "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F", 
      "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", 
      "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F", 
      "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7", 
      "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF", 
      "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7", 
      "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF", 
      "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", 
      "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", 
      "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7", 
      "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", 
      "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", 
      "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", 
      "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", 
      "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF", 
    })); 
} 
 
string http_encode_cookie(string f) 
//! Encode dangerous characters in a string so that it can be used as 
//! the value string or name string in a cookie. 
//! 
//! @note 
//! This encodes with the same kind of %-escapes as 
//! @[http_encode_url], and that isn't an encoding specified by the 
//! cookie RFC 2965. It works because there is a nonstandard decoding 
//! of %-escapes in the Roxen HTTP protocol module. 
{ 
  // FIXME: There are numerous invalid chars that this doesn't encode, 
  // e.g. 8 bit and wide chars. 
  return replace( 
    f, ({ 
      "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007", 
      "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017", 
      "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027", 
      "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037", 
      "\177", 
      "=", ",", ";", "%", 
    }), ({ 
      "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", 
      "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F", 
      "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", 
      "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F", 
      "%7F", 
      "%3D", "%2C", "%3B", "%25", 
    })); 
} 
 
string http_encode_url (string f) 
//! Encode any string to be used as a component part in a URI. This 
//! means that all URI reserved and excluded characters are escaped, 
//! i.e. everything except @expr{A-Z@}, @expr{a-z@}, @expr{0-9@}, 
//! @expr{-@}, @expr{.@}, @expr{_@}, and @expr{~@} (see RFC 3986 
//! section 2.3). 
//! 
//! @note 
//! Eight bit chars and wider are encoded using UTF-8 followed by http 
//! escaping, as mandated by RFC 3987, section 3.1 and appendix B.2 in 
//! the HTML 4.01 standard 
//! (http://www.w3.org/TR/html4/appendix/notes.html#non-ascii-chars). 
//! (It should work regardless of the charset used in the XML document 
//! the URL might be inserted into.) 
//! 
//! @seealso 
//! @[http_encode_invalids] 
{ 
  return replace ( 
    string_to_utf8 (f), ({ 
      // Control chars 
      "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007", 
      "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017", 
      "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027", 
      "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037", 
      "\177", 
      // RFC 3986, section 2.2, gen-delims 
      ":", "/", "?", "#", "[", "]", "@", 
      // RFC 3986, section 2.2, sub-delims 
      "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "=", 
      // Others outside the unreserved chars (RFC 3986, section 2.2) 
      " ", "\"", "%", "<", ">", "\\", "^", "`", "{", "|", "}", 
      // Compat note: "!", "(", ")" and "*" were not encoded in 4.5 
      // and earlier since they were part of the unreserved set in the 
      // superseded URI RFC 2396. 
      // All eight bit chars (this is fast with the current replace() 
      // implementation). 
      "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207", 
      "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217", 
      "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227", 
      "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237", 
      "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247", 
      "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257", 
      "\260", "\261", "\262", "\263", "\264", "\265", "\266", "\267", 
      "\270", "\271", "\272", "\273", "\274", "\275", "\276", "\277", 
      "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307", 
      "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317", 
      "\320", "\321", "\322", "\323", "\324", "\325", "\326", "\327", 
      "\330", "\331", "\332", "\333", "\334", "\335", "\336", "\337", 
      "\340", "\341", "\342", "\343", "\344", "\345", "\346", "\347", 
      "\350", "\351", "\352", "\353", "\354", "\355", "\356", "\357", 
      "\360", "\361", "\362", "\363", "\364", "\365", "\366", "\367", 
      "\370", "\371", "\372", "\373", "\374", "\375", "\376", "\377", 
    }), 
    ({ 
      "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", 
      "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F", 
      "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", 
      "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F", 
      "%7F", 
      "%3A", "%2F", "%3F", "%23", "%5B", "%5D", "%40", 
      "%21","%24","%26","%27","%28","%29","%2A","%2B","%2C","%3B","%3D", 
      "%20","%22","%25","%3C","%3E","%5C","%5E","%60","%7B","%7C","%7D", 
      "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", 
      "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F", 
      "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", 
      "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F", 
      "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7", 
      "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF", 
      "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7", 
      "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF", 
      "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", 
      "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", 
      "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7", 
      "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", 
      "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", 
      "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", 
      "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", 
      "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF", 
    })); 
} 
 
//! Compatibility alias for @[http_encode_url]. 
string correctly_http_encode_url(string f) { 
  return http_encode_url (f); 
} 
 
string add_pre_state( string url, multiset state ) 
//! Adds the provided states as prestates to the provided url. 
{ 
#ifdef MODULE_DEBUG 
  if(!url) 
    error("URL needed for add_pre_state()\n"); 
#endif 
  if(!state || !sizeof(state)) 
    return url; 
  string base; 
  if (sscanf (url, "%s://%[^/]%s", base, string host, url) == 3) 
    base += "://" + host; 
  else 
    base = ""; 
  if(strlen(url)>5 && (url[1] == '(' || url[1] == '<')) 
    return base + url; 
  return base + "/(" + sort(indices(state)) * "," + ")" + url ; 
} 
 
string make_absolute_url (string url, RequestID|void id, 
                          multiset|void prestates, mapping|void variables) 
//! Returns an absolute URL built from the components: If @[url] is a 
//! virtual (possibly relative) path, the current @[RequestID] object 
//! must be supplied in @[id] to resolve the absolute URL. 
//! 
//! If no @[prestates] are provided, the current prestates in @[id] 
//! are added to the URL, provided @[url] is a local absolute or 
//! relative URL. 
//! 
//! If @[variables] is given it's a mapping containing variables that 
//! should be appended to the URL. Each index is a variable name and 
//! the value can be a string or an array, in which case a separate 
//! variable binding is added for each string in the array. That means 
//! that e.g. @[RequestID.real_variables] can be used as @[variables]. 
//! 
//! @[url] is encoded using @[http_encode_invalids] so it may contain 
//! eight bit chars and wider. All variable names and values in 
//! @[variables] are thoroughly encoded using @[http_encode_url] so 
//! they should not be encoded in any way to begin with. 
{ 
  // If the URL is a local relative URL we make it absolute. 
  url = fix_relative(url, id); 
   
  // Add protocol and host to local absolute URLs. 
  if (has_prefix (url, "/")) { 
    if(id) { 
      Standards.URI uri = Standards.URI(id->url_base()); 
 
      // Handle proxies 
      string xf_proto = id->request_headers["x-forwarded-proto"]; 
      string xf_host = id->request_headers["x-forwarded-host"]; 
 
      if (xf_proto && xf_host) { 
        uri = Standards.URI(xf_proto + "://" + xf_host + uri->path); 
      } 
      else if (xf_host) { 
        uri = Standards.URI(uri->scheme + "://" + xf_host + uri->path); 
      } 
      else if (xf_proto) { 
        uri = Standards.URI(xf_proto + "://" + uri->host + ":" + uri->port + uri->path); 
      } 
 
      url = (string)uri + url[1..]; 
      if (!prestates) prestates = id->prestate; 
    } 
    else { 
      // Ok, no domain present in the URL and no ID object given. 
      // Perhaps one should dare throw an error here, but since most 
      // UA can handle the redirect it is nicer no to. 
    } 
  } 
 
  if(prestates && sizeof(prestates)) 
    url = add_pre_state (url, prestates); 
 
  if( String.width( url )>8 && !has_value( url, "?" ) ) 
    url += "?magic_roxen_automatic_charset_variable="+ 
      magic_charset_variable_value; 
 
  url = http_encode_invalids (url); 
  if (variables) { 
    string concat_char = has_value (url, "?") ? "&" : "?"; 
    foreach (indices (variables), string var) { 
      var = http_encode_url (var); 
      mixed val = variables[var]; 
      if (stringp (val)) { 
        url += concat_char + var + "=" + http_encode_url (val); 
        concat_char = "&"; 
      } 
      else if (arrayp (val)) 
        foreach (val, mixed part) 
          if (stringp (part)) { 
            url += concat_char + var + "=" + http_encode_url (part); 
            concat_char = "&"; 
          } 
    } 
  } 
 
  return url; 
} 
 
mapping http_redirect( string url, RequestID|void id, multiset|void prestates, 
                       mapping|void variables, void|int http_code) 
//! Returns a http-redirect message to the specified URL. The absolute 
//! URL that is required for the @expr{Location@} header is built from 
//! the given components using @[make_absolute_url]. See that function 
//! for details. 
//! 
//! If @[http_code] is nonzero, it specifies the http status code to 
//! use in the response. It's @[Protocols.HTTP.HTTP_FOUND] (i.e. 302) 
//! by default. 
{ 
  // If we don't get any URL we don't know what to do. 
  // But we do!  /per 
  if(!url) 
    url = ""; 
 
  url = make_absolute_url (url, id, prestates, variables); 
 
  HTTP_WERR("Redirect -> "+url); 
 
  return http_status( http_code || Protocols.HTTP.HTTP_FOUND, 
                      "Redirect to " + html_encode_string(url)) 
    + ([ "extra_heads":([ "Location":url ]) ]); 
} 
 
mapping http_stream(Stdio.File from) 
//! Returns a result mapping where the data returned to the client 
//! will be streamed raw from the given Stdio.File object, instead of 
//! being packaged by roxen. In other words, it's entirely up to you 
//! to make sure what you send is HTTP data. 
{ 
  return ([ "raw":1, "file":from, "len":-1, ]); 
} 
 
mapping(string:mixed) http_digest_required(mapping(string:string) challenge, 
                                           string|void message) 
//! Generates a result mapping that instructs the browser to 
//! authenticate the user using Digest authentication (see RFC 2617 
//! section 3). 
//! 
//! The optional message is the message body that the client typically 
//! shows the user if he or she decides to abort the authentication 
//! request. 
{ 
  if(!message) 
    message = "<h1>Authentication failed.\n</h1>"; 
  HTTP_WERR(sprintf("Auth required (%O)", challenge)); 
  string digest_challenge = ""; 
  foreach(challenge; string key; string val) { 
    // FIXME: This doesn't work with all Digest directives. E.g. the 
    // algorithm gets incorrectly quoted. 
    digest_challenge += sprintf(" %s=%O", key, val); 
  } 
  return http_low_answer(401, message) 
    + ([ "extra_heads":([ "WWW-Authenticate":"Digest"+digest_challenge,]),]); 
} 
 
mapping(string:mixed) http_auth_required(string realm, string|void message, 
                                         void|RequestID id) 
//! Generates a result mapping that instructs the browser to 
//! authenticate the user using Basic authentication (see RFC 2617 
//! section 2). @[realm] is the name of the realm on the server, which 
//! will typically end up in the browser's prompt for a name and 
//! password (e.g. "Enter username for @i{realm@} at @i{hostname@}:"). 
//! 
//! The optional message is the message body that the client typically 
//! shows the user if he or she decides to abort the authentication 
//! request. 
{ 
  HTTP_WERR("Auth required ("+realm+")"); 
  if (id) { 
    return id->conf->auth_failed_file( id, message ) 
      + ([ "extra_heads":([ "WWW-Authenticate": 
                            sprintf ("Basic realm=%O", realm)])]); 
  } 
  if(!message) 
    message = "<h1>Authentication failed.</h1>"; 
  return http_low_answer(401, message) 
    + ([ "extra_heads":([ "WWW-Authenticate": 
                          sprintf ("Basic realm=%O", realm)])]); 
} 
 
mapping(string:mixed) http_proxy_auth_required(string realm, 
                                               void|string message) 
//! Similar to @[http_auth_required], but returns a 407 
//! Proxy-Authenticate header (see RFC 2616 section 14.33). 
{ 
  if(!message) 
    message = "<h1>Proxy authentication failed.</h1>"; 
  return http_low_answer(407, message) 
    + ([ "extra_heads":([ "Proxy-Authenticate": 
                          sprintf ("Basic realm=%O", realm)])]); 
} 
 
 
// --- From the old 'roxenlib' file ------------------------------- 
 
string extract_query(string from) 
{ 
  if(!from) return ""; 
  if(sscanf(from, "%*s?%s%*[ \t\n]", from)) 
    return (from/"\r")[0]; 
  return ""; 
} 
 
protected string mk_env_var_name(string name) 
{ 
  name = replace(name, " ", "_"); 
  string res = ""; 
  do { 
    string ok_part=""; 
    sscanf(name, "%[A-Za-z0-9_]%s", ok_part, name); 
    res += ok_part; 
    if (sizeof(name)) { 
      res += "_"; 
      name = name[1..]; 
    } 
  } while (sizeof(name)); 
  return res; 
} 
 
mapping build_env_vars(string f, RequestID id, string path_info) 
//! Generate a mapping with environment variables suitable for use 
//! with CGI-scripts or SSI scripts etc. 
//! 
//! @mapping 
//!   @member string REQUEST_URI 
//!     URI requested by the user. 
//!   @member string REDIRECT_URL 
//!     Target of the first internal redirect. 
//!   @member string INDEX 
//!   @member string SCRIPT_NAME 
//!   @member string PATH_INFO 
//!   @member string PATH_TRANSLATED 
//!   @member string DOCUMENT_NAME 
//!   @member string DOCUMENT_URI 
//!   @member string LAST_MODIFIED 
//!   @member string SCRIPT_FILENAME 
//!   @member string DOCUMENT_ROOT 
//!   @member string HTTP_HOST 
//!   @member string HTTP_PROXY_CONNECTION 
//!   @member string HTTP_ACCEPT 
//!   @member string HTTP_COOKIE 
//!   @member string HTTP_PRAGMA 
//!   @member string HTTP_CONNECTION 
//!   @member string HTTP_USER_AGENT 
//!   @member string HTTP_REFERER 
//!   @member string REMOTE_ADDR 
//!   @member string REMOTE_HOST 
//!   @member string REMOTE_PORT 
//!   @member string QUERY_STRING 
//!   @member string REMOTE_USER 
//!   @member string ROXEN_AUTHENTICATED 
//!   @member string CONTENT_TYPE 
//!   @member string CONTENT_LENGTH 
//!   @member string REQUEST_METHOD 
//!   @member string SERVER_PORT 
//! @endmapping 
{ 
  string addr=id->remoteaddr || "Internal"; 
  mapping(string:string) new = ([]); 
 
  if(id->query && strlen(id->query)) 
    new->INDEX=id->query; 
 
  if(path_info && strlen(path_info)) 
  { 
    if(path_info[0] != '/') 
      path_info = "/" + path_info; 
 
    // Kludge 
    if ( ([mapping(string:mixed)]id->misc)->path_info == path_info ) { 
      // Already extracted 
      new["SCRIPT_NAME"]=id->not_query; 
    } else { 
      new["SCRIPT_NAME"]= 
        id->not_query[0..strlen([string]id->not_query)-strlen(path_info)-1]; 
    } 
    new["PATH_INFO"]=path_info; 
 
 
    // FIXME: Consider looping over the splitted path. 
    string trailer = ""; 
    while(1) 
    { 
      // Fix PATH_TRANSLATED correctly. 
      string translated_base = id->conf->real_file(path_info, id); 
      if (translated_base) 
      { 
        new["PATH_TRANSLATED"] = combine_path_unix(translated_base, trailer); 
        break; 
      } 
      array(string) tmp = path_info/"/" - ({""}); 
      if(!sizeof(tmp)) 
        break; 
      path_info = "/" + (tmp[..sizeof(tmp)-2]) * "/"; 
      trailer = tmp[-1] + "/" + trailer; 
    } 
  } else 
    new["SCRIPT_NAME"]=id->not_query; 
 
  // Find the original request. 
  RequestID tmpid = id; 
  RequestID previd; 
  while(tmpid->misc->orig) { 
    // internal get 
    tmpid = (previd = tmpid)->misc->orig; 
  } 
 
  // The original URL. 
  new["REQUEST_URI"] = 
    tmpid->misc->redirected_raw_url || tmpid->raw_url; 
 
  if(tmpid->misc->is_redirected || previd) { 
    // Destination of the first internal redirect. 
    if (tmpid->misc->redirected_to) { 
      new["REDIRECT_URL"] = 
        Roxen.http_encode_invalids(tmpid->misc->redirected_to); 
    } else if (previd) { 
      new["REDIRECT_URL"] = previd->raw_url; 
    } 
    new["REDIRECT_STATUS"] = "200"; 
  } 
 
  // Begin "SSI" vars. 
  array(string) tmps; 
  if(sizeof(tmps = tmpid->not_query/"/" - ({""}))) 
    new["DOCUMENT_NAME"]=tmps[-1]; 
 
  new["DOCUMENT_URI"]= tmpid->not_query; 
 
  Stat tmpi; 
  string real_file=tmpid->conf->real_file(tmpid->not_query||"", tmpid); 
  if (real_file) { 
    if(stringp(real_file)) { 
      if ((tmpi = file_stat(real_file)) && 
          sizeof(tmpi)) { 
        new["LAST_MODIFIED"]=http_date(tmpi[3]); 
      } 
    } else { 
      // Extra paranoia. 
      report_error(sprintf("real_file(%O, %O) returned %O\n", 
                           tmpid->not_query||"", tmpid, real_file)); 
    } 
  } 
 
  // End SSI vars. 
 
 
  if(string tmp = id->conf->real_file(new["SCRIPT_NAME"], id)) 
    new["SCRIPT_FILENAME"] = tmp; 
 
  if(string tmp = id->conf->real_file("/", id)) 
    new["DOCUMENT_ROOT"] = tmp; 
 
  if(!new["PATH_TRANSLATED"]) 
    m_delete(new, "PATH_TRANSLATED"); 
  else if(new["PATH_INFO"][-1] != '/' && new["PATH_TRANSLATED"][-1] == '/') 
    new["PATH_TRANSLATED"] = 
      new["PATH_TRANSLATED"][0..strlen(new["PATH_TRANSLATED"])-2]; 
 
  // HTTP_ style variables: 
 
  mapping hdrs; 
 
  if ((hdrs = id->request_headers)) { 
    foreach(indices(hdrs) - ({ "authorization", "proxy-authorization", 
                               "security-scheme", }), string h) { 
      string hh = "HTTP_" + replace(upper_case(h), 
                                    ({ " ", "-", "\0", "=" }), 
                                    ({ "_", "_", "", "_" })); 
 
      new[mk_env_var_name(hh)] = replace(hdrs[h], ({ "\0" }), ({ "" })); 
    } 
    if (!new["HTTP_HOST"]) { 
      if(objectp(id->my_fd) && id->my_fd->query_address(1)) 
        new["HTTP_HOST"] = replace(id->my_fd->query_address(1)," ",":"); 
    } 
  } else { 
    if(id->misc->host) 
      new["HTTP_HOST"]=id->misc->host; 
    else if(objectp(id->my_fd) && id->my_fd->query_address(1)) 
      new["HTTP_HOST"]=replace(id->my_fd->query_address(1)," ",":"); 
    if(id->misc["proxy-connection"]) 
      new["HTTP_PROXY_CONNECTION"]=id->misc["proxy-connection"]; 
    if(id->misc->accept) { 
      if (arrayp(id->misc->accept)) { 
        new["HTTP_ACCEPT"]=id->misc->accept*", "; 
      } else { 
        new["HTTP_ACCEPT"]=(string)id->misc->accept; 
      } 
    } 
 
    if(id->misc->cookies) 
      new["HTTP_COOKIE"] = id->misc->cookies; 
 
    if(sizeof(id->pragma)) 
      new["HTTP_PRAGMA"]=indices(id->pragma)*", "; 
 
    if(stringp(id->misc->connection)) 
      new["HTTP_CONNECTION"]=id->misc->connection; 
 
    new["HTTP_USER_AGENT"] = id->client*" "; 
 
    if(id->referer && sizeof(id->referer)) 
      new["HTTP_REFERER"] = id->referer*""; 
  } 
 
  new["REMOTE_ADDR"]=addr; 
 
  if(roxen->quick_ip_to_host(addr) != addr) 
    new["REMOTE_HOST"]=roxen->quick_ip_to_host(addr); 
 
  catch { 
    if(id->my_fd) 
      new["REMOTE_PORT"] = (id->my_fd->query_address()/" ")[1]; 
  }; 
 
  if (id->query && sizeof(id->query)) { 
    new["QUERY_STRING"] = id->query; 
  } 
 
  if(id->realauth) 
    new["REMOTE_USER"] = (id->realauth / ":")[0]; 
  if( User u = id->conf->authenticate( id ) ) 
    new["ROXEN_AUTHENTICATED"] = u->name(); 
  // User is valid with the Roxen userdb. 
 
  if(id->data && strlen(id->data)) 
  { 
    if(id->misc["content-type"]) 
      new["CONTENT_TYPE"]=id->misc["content-type"]; 
    else 
      new["CONTENT_TYPE"]="application/x-www-form-urlencoded"; 
    new["CONTENT_LENGTH"]=(string)strlen(id->data); 
  } 
 
  if(id->query && strlen(id->query)) 
    new["INDEX"]=id->query; 
 
  new["REQUEST_METHOD"]=id->method||"GET"; 
  new["SERVER_PORT"] = id->my_fd? 
    ((id->my_fd->query_address(1)||"foo unknown")/" ")[1]: "Internal"; 
 
  // Protect against execution of arbitrary code in broken bash. 
  foreach(new; string e; string v) { 
    if (has_prefix(v, "() {")) { 
      report_warning("ENV: Function definition in environment variable:\n" 
                     "ENV: %O=%O\n", 
                     e, v); 
      new[e] = " " + v; 
    } 
  } 
 
  return new; 
} 
 
mapping build_roxen_env_vars(RequestID id) 
//! Generate a mapping with additional environment variables suitable 
//! for use with CGI-scripts or SSI scripts etc. These variables are 
//! roxen extensions and not defined in any standard document. 
//! Specifically: 
//! @pre{ 
//! For each cookie:          COOKIE_cookiename=cookievalue 
//! For each variable:        VAR_variablename=variablevalue 
//!                           (Where the null character is encoded as "#") 
//! For each variable:        QUERY_variablename=variablevalue 
//!                           (Where the null character is encoded as " ") 
//! For each 'prestate':      PRESTATE_x=true 
//! For each 'config':        CONFIG_x=true 
//! For each 'supports' flag: SUPPORTS_x=true 
//! @} 
//! 
//! @mapping 
//!   @member string ROXEN_USER_ID 
//!     The unique ID for that client, if available. 
//!   @member string COOKIES 
//!     A space delimitered list of all the cookies names. 
//!   @member string CONFIGS 
//!     A space delimitered list of all config flags. 
//!   @member string VARIABLES 
//!     A space delimitered list of all variable names. 
//!   @member string PRESTATES 
//!     A space delimitered list of all prestates. 
//!   @member string SUPPORTS 
//!     A space delimitered list of all support flags. 
//! @endmapping 
{ 
  mapping(string:string) new = ([]); 
  string tmp; 
 
  if(id->cookies->RoxenUserID) 
    new["ROXEN_USER_ID"]=id->cookies->RoxenUserID; 
 
  new["COOKIES"] = ""; 
  foreach(indices(id->cookies), tmp) 
    { 
      new["COOKIE_"+mk_env_var_name(tmp)] = id->cookies[tmp]; 
      new["COOKIES"]+= mk_env_var_name(tmp)+" "; 
    } 
 
  foreach(indices(id->config), tmp) 
    { 
      tmp = mk_env_var_name(tmp); 
      new["CONFIG_"+tmp]="true"; 
      if(new["CONFIGS"]) 
        new["CONFIGS"] += " " + tmp; 
      else 
        new["CONFIGS"] = tmp; 
    } 
 
  foreach(indices(id->variables), tmp) 
  { 
    string name = mk_env_var_name(tmp); 
    if (mixed value = id->variables[tmp]) 
      if (!catch (value = (string) value) && (sizeof(value) < 8192)) { 
        // Some shells/OS's don't like LARGE environment variables 
        new["QUERY_"+name] = replace(value,"\000"," "); 
        new["VAR_"+name] = replace(value,"\000","#"); 
      } 
    // Is it correct to record the names for variables with no values here? /mast 
    if(new["VARIABLES"]) 
      new["VARIABLES"]+= " " + name; 
    else 
      new["VARIABLES"]= name; 
  } 
 
  foreach(indices(id->prestate), tmp) 
  { 
    tmp = mk_env_var_name(tmp); 
    new["PRESTATE_"+tmp]="true"; 
    if(new["PRESTATES"]) 
      new["PRESTATES"] += " " + tmp; 
    else 
      new["PRESTATES"] = tmp; 
  } 
 
  foreach(indices(id->supports), tmp) 
  { 
    tmp = mk_env_var_name(tmp-","); 
    new["SUPPORTS_"+tmp]="true"; 
    if (new["SUPPORTS"]) 
      new["SUPPORTS"] += " " + tmp; 
    else 
      new["SUPPORTS"] = tmp; 
  } 
 
  // Protect against execution of arbitrary code in broken bash. 
  foreach(new; string e; string v) { 
    if (has_prefix(v, "() {")) { 
      report_warning("ENV: Function definition in environment variable:\n" 
                     "ENV: %O=%O\n", 
                     e, v); 
      new[e] = " " + v; 
    } 
  } 
 
  return new; 
} 
 
string strip_config(string from) 
//! Remove all 'config' data from the given (local) URL. 
{ 
  sscanf(from, "/<%*s>%s", from); 
  return from; 
} 
 
string strip_prestate(string from) 
//! Remove all 'prestate' data from the given (local) URL. 
{ 
  sscanf(from, "/(%*s)%s", from); 
  return from; 
} 
 
string parse_rxml(string what, RequestID id ) 
//! Parse the given string as RXML and return the result. This 
//! function inherits the current RXML evaluation context if there is 
//! any, otherwise a new context is created. 
//! 
//! @note 
//! Try to avoid using this function to parse recursively; the RXML 
//! module provides several ways to accomplish that. If there's code 
//! that recurses directly then several RXML features, like p-code 
//! compilation, streaming operation and continuations, won't work in 
//! that part of the RXML code. 
{ 
  if(!objectp(id)) error("No id passed to parse_rxml\n"); 
  return id->conf->parse_rxml( what, id ); 
} 
 
array(string|RXML.PCode) compile_rxml (string what, RequestID id) 
//! Evaluates and compiles the given string as RXML. Returns an array 
//! where the first element is the result of the evaluation and the 
//! second is the p-code object that contains the compiled RXML tree. 
//! It can be re-evaluated by e.g. @[Roxen.eval_p_code]. This function 
//! initiates a new context for the evaluation, so it won't recurse in 
//! the currently ongoing RXML evaluation, if any. 
{ 
  RXML.Parser parser = get_rxml_parser (id, 0, 1); 
  parser->write_end (what); 
  array(string|RXML.PCode) res = ({parser->eval(), parser->p_code}); 
  res[1]->finish(); 
  //parser->type->give_back (parser); // RXML.PXml is not resettable anyway. 
  return res; 
} 
 
mixed eval_p_code (RXML.PCode p_code, RequestID id) 
//! Evaluates the given p-code object and returns the result. This 
//! function initiates a new context for the evaluation, so it won't 
//! recurse in the currently ongoing RXML evaluation, if any. 
//! 
//! @note 
//! The caller should first check with @[p_code->is_stale] that the 
//! p-code isn't stale, i.e. that none of the tag sets used in it have 
//! changed since it was created. If that's the case it isn't safe to 
//! evaluate the p-code, so it should be discarded and perhaps 
//! replaced with a new one retrieved by @[RXML.string_to_p_code] or 
//! generated from source. See also @[RXML.RenewablePCode], which 
//! never can become stale. 
{ 
  return p_code->eval (p_code->new_context (id)); 
} 
 
RXML.Parser get_rxml_parser (RequestID id, void|RXML.Type type, void|int make_p_code) 
//! Returns a parser object for parsing and evaluating a string as 
//! RXML in a new context. @[type] may be used to set the top level 
//! type to parse. It defaults to the standard type and parser for 
//! RXML code. 
//! 
//! If @[make_p_code] is nonzero, the parser is initialized with an 
//! @[RXML.PCode] object to collect p-code during the evaluation. When 
//! the parser is finished, the p-code is available in the variable 
//! @[RXML.Parser.p_code]. The p-code itself is not finished, though; 
//! @[RXML.PCode.finished] should be called in it before use to 
//! compact it, although that isn't mandatory. 
{ 
  RXML.Parser parser = id->conf->rxml_tag_set->get_parser ( 
    type || id->conf->default_content_type, id, make_p_code); 
  parser->recover_errors = 1; 
  if (make_p_code) parser->p_code->recover_errors = 1; 
  return parser; 
} 
 
protected int(0..0) return_zero() {return 0;} 
 
protected Parser.HTML xml_parser = 
  lambda() { 
    Parser.HTML p = Parser.HTML(); 
    p->lazy_entity_end (1); 
    p->match_tag (0); 
    p->xml_tag_syntax (3); 
    p->add_quote_tag ("!--", return_zero, "--"); 
    p->add_quote_tag ("![CDATA[", return_zero, "]]"); 
    p->add_quote_tag ("?", return_zero, "?"); 
    return p; 
  }(); 
 
Parser.HTML get_xml_parser() 
//! Returns a @[Parser.HTML] initialized for parsing XML. It has all 
//! the flags set properly for XML syntax and have callbacks to ignore 
//! comments, CDATA blocks and unknown PI tags, but it has no 
//! registered tags and doesn't decode any entities. 
{ 
  return xml_parser->clone(); 
} 
 
constant iso88591 
=([ "&nbsp;":   " ", 
    "&iexcl;":  "¡", 
    "&cent;":   "¢", 
    "&pound;":  "£", 
    "&curren;": "¤", 
    "&yen;":    "¥", 
    "&brvbar;": "¦", 
    "&sect;":   "§", 
    "&uml;":    "¨", 
    "&copy;":   "©", 
    "&ordf;":   "ª", 
    "&laquo;":  "«", 
    "&not;":    "¬", 
    "&shy;":    "­", 
    "&reg;":    "®", 
    "&macr;":   "¯", 
    "&deg;":    "°", 
    "&plusmn;": "±", 
    "&sup2;":   "²", 
    "&sup3;":   "³", 
    "&acute;":  "´", 
    "&micro;":  "µ", 
    "&para;":   "¶", 
    "&middot;": "·", 
    "&cedil;":  "¸", 
    "&sup1;":   "¹", 
    "&ordm;":   "º", 
    "&raquo;":  "»", 
    "&frac14;": "¼", 
    "&frac12;": "½", 
    "&frac34;": "¾", 
    "&iquest;": "¿", 
    "&Agrave;": "À", 
    "&Aacute;": "Á", 
    "&Acirc;":  "Â", 
    "&Atilde;": "Ã", 
    "&Auml;":   "Ä", 
    "&Aring;":  "Å", 
    "&AElig;":  "Æ", 
    "&Ccedil;": "Ç", 
    "&Egrave;": "È", 
    "&Eacute;": "É", 
    "&Ecirc;":  "Ê", 
    "&Euml;":   "Ë", 
    "&Igrave;": "Ì", 
    "&Iacute;": "Í", 
    "&Icirc;":  "Î", 
    "&Iuml;":   "Ï", 
    "&ETH;":    "Ð", 
    "&Ntilde;": "Ñ", 
    "&Ograve;": "Ò", 
    "&Oacute;": "Ó", 
    "&Ocirc;":  "Ô", 
    "&Otilde;": "Õ", 
    "&Ouml;":   "Ö", 
    "&times;":  "×", 
    "&Oslash;": "Ø", 
    "&Ugrave;": "Ù", 
    "&Uacute;": "Ú", 
    "&Ucirc;":  "Û", 
    "&Uuml;":   "Ü", 
    "&Yacute;": "Ý", 
    "&THORN;":  "Þ", 
    "&szlig;":  "ß", 
    "&agrave;": "à", 
    "&aacute;": "á", 
    "&acirc;":  "â", 
    "&atilde;": "ã", 
    "&auml;":   "ä", 
    "&aring;":  "å", 
    "&aelig;":  "æ", 
    "&ccedil;": "ç", 
    "&egrave;": "è", 
    "&eacute;": "é", 
    "&ecirc;":  "ê", 
    "&euml;":   "ë", 
    "&igrave;": "ì", 
    "&iacute;": "í", 
    "&icirc;":  "î", 
    "&iuml;":   "ï", 
    "&eth;":    "ð", 
    "&ntilde;": "ñ", 
    "&ograve;": "ò", 
    "&oacute;": "ó", 
    "&ocirc;":  "ô", 
    "&otilde;": "õ", 
    "&ouml;":   "ö", 
    "&divide;": "÷", 
    "&oslash;": "ø", 
    "&ugrave;": "ù", 
    "&uacute;": "ú", 
    "&ucirc;":  "û", 
    "&uuml;":   "ü", 
    "&yacute;": "ý", 
    "&thorn;":  "þ", 
    "&yuml;":   "ÿ", 
]); 
 
constant international 
=([ "&OElig;":  "\x0152", 
    "&oelig;":  "\x0153", 
    "&Scaron;": "\x0160", 
    "&scaron;": "\x0161", 
    "&Yuml;":   "\x0178", 
    "&circ;":   "\x02C6", 
    "&tilde;":  "\x02DC", 
    "&ensp;":   "\x2002", 
    "&emsp;":   "\x2003", 
    "&thinsp;": "\x2009", 
    "&zwnj;":   "\x200C", 
    "&zwj;":    "\x200D", 
    "&lrm;":    "\x200E", 
    "&rlm;":    "\x200F", 
    "&ndash;":  "\x2013", 
    "&mdash;":  "\x2014", 
    "&lsquo;":  "\x2018", 
    "&rsquo;":  "\x2019", 
    "&sbquo;":  "\x201A", 
    "&ldquo;":  "\x201C", 
    "&rdquo;":  "\x201D", 
    "&bdquo;":  "\x201E", 
    "&dagger;": "\x2020", 
    "&Dagger;": "\x2021", 
    "&permil;": "\x2030", 
    "&lsaquo;": "\x2039", 
    "&rsaquo;": "\x203A", 
    "&euro;":   "\x20AC", 
]); 
 
constant symbols 
=([ "&fnof;":     "\x0192", 
    "&thetasym;": "\x03D1", 
    "&upsih;":    "\x03D2", 
    "&piv;":      "\x03D6", 
    "&bull;":     "\x2022", 
    "&hellip;":   "\x2026", 
    "&prime;":    "\x2032", 
    "&Prime;":    "\x2033", 
    "&oline;":    "\x203E", 
    "&frasl;":    "\x2044", 
    "&weierp;":   "\x2118", 
    "&image;":    "\x2111", 
    "&real;":     "\x211C", 
    "&trade;":    "\x2122", 
    "&alefsym;":  "\x2135", 
    "&larr;":     "\x2190", 
    "&uarr;":     "\x2191", 
    "&rarr;":     "\x2192", 
    "&darr;":     "\x2193", 
    "&harr;":     "\x2194", 
    "&crarr;":    "\x21B5", 
    "&lArr;":     "\x21D0", 
    "&uArr;":     "\x21D1", 
    "&rArr;":     "\x21D2", 
    "&dArr;":     "\x21D3", 
    "&hArr;":     "\x21D4", 
    "&forall;":   "\x2200", 
    "&part;":     "\x2202", 
    "&exist;":    "\x2203", 
    "&empty;":    "\x2205", 
    "&nabla;":    "\x2207", 
    "&isin;":     "\x2208", 
    "&notin;":    "\x2209", 
    "&ni;":       "\x220B", 
    "&prod;":     "\x220F", 
    "&sum;":      "\x2211", 
    "&minus;":    "\x2212", 
    "&lowast;":   "\x2217", 
    "&radic;":    "\x221A", 
    "&prop;":     "\x221D", 
    "&infin;":    "\x221E", 
    "&ang;":      "\x2220", 
    "&and;":      "\x2227", 
    "&or;":       "\x2228", 
    "&cap;":      "\x2229", 
    "&cup;":      "\x222A", 
    "&int;":      "\x222B", 
    "&there4;":   "\x2234", 
    "&sim;":      "\x223C", 
    "&cong;":     "\x2245", 
    "&asymp;":    "\x2248", 
    "&ne;":       "\x2260", 
    "&equiv;":    "\x2261", 
    "&le;":       "\x2264", 
    "&ge;":       "\x2265", 
    "&sub;":      "\x2282", 
    "&sup;":      "\x2283", 
    "&nsub;":     "\x2284", 
    "&sube;":     "\x2286", 
    "&supe;":     "\x2287", 
    "&oplus;":    "\x2295", 
    "&otimes;":   "\x2297", 
    "&perp;":     "\x22A5", 
    "&sdot;":     "\x22C5", 
    "&lceil;":    "\x2308", 
    "&rceil;":    "\x2309", 
    "&lfloor;":   "\x230A", 
    "&rfloor;":   "\x230B", 
    "&lang;":     "\x2329", 
    "&rang;":     "\x232A", 
    "&loz;":      "\x25CA", 
    "&spades;":   "\x2660", 
    "&clubs;":    "\x2663", 
    "&hearts;":   "\x2665", 
    "&diams;":    "\x2666", 
]); 
 
constant greek 
= ([ "&Alpha;":   "\x391", 
     "&Beta;":    "\x392", 
     "&Gamma;":   "\x393", 
     "&Delta;":   "\x394", 
     "&Epsilon;": "\x395", 
     "&Zeta;":    "\x396", 
     "&Eta;":     "\x397", 
     "&Theta;":   "\x398", 
     "&Iota;":    "\x399", 
     "&Kappa;":   "\x39A", 
     "&Lambda;":  "\x39B", 
     "&Mu;":      "\x39C", 
     "&Nu;":      "\x39D", 
     "&Xi;":      "\x39E", 
     "&Omicron;": "\x39F", 
     "&Pi;":      "\x3A0", 
     "&Rho;":     "\x3A1", 
     "&Sigma;":   "\x3A3", 
     "&Tau;":     "\x3A4", 
     "&Upsilon;": "\x3A5", 
     "&Phi;":     "\x3A6", 
     "&Chi;":     "\x3A7", 
     "&Psi;":     "\x3A8", 
     "&Omega;":   "\x3A9", 
     "&alpha;":   "\x3B1", 
     "&beta;":    "\x3B2", 
     "&gamma;":   "\x3B3", 
     "&delta;":   "\x3B4", 
     "&epsilon;": "\x3B5", 
     "&zeta;":    "\x3B6", 
     "&eta;":     "\x3B7", 
     "&theta;":   "\x3B8", 
     "&iota;":    "\x3B9", 
     "&kappa;":   "\x3BA", 
     "&lambda;":  "\x3BB", 
     "&mu;":      "\x3BC", 
     "&nu;":      "\x3BD", 
     "&xi;":      "\x3BE", 
     "&omicron;": "\x3BF", 
     "&pi;":      "\x3C0", 
     "&rho;":     "\x3C1", 
     "&sigmaf;":  "\x3C2", 
     "&sigma;":   "\x3C3", 
     "&tau;":     "\x3C4", 
     "&upsilon;": "\x3C5", 
     "&phi;":     "\x3C6", 
     "&chi;":     "\x3C7", 
     "&psi;":     "\x3C8", 
     "&omega;":   "\x3C9", 
]); 
 
constant replace_entities = indices( iso88591 ) + 
  indices( international ) + 
  indices( symbols ) + 
  indices( greek ) + 
  ({"&lt;","&gt;","&amp;","&quot;","&apos;","&#x22;","&#34;","&#39;","&#0;"}); 
 
constant replace_values = values( iso88591 ) + 
  values( international ) + 
  values( symbols ) + 
  values( greek ) + 
  ({"<",">","&","\"","\'","\"","\"","\'","\000"}); 
 
constant safe_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"/""; 
constant empty_strings = ({""})*sizeof(safe_characters); 
 
//! Returns 1 if @[in] is nonempty and only contains alphanumerical 
//! characters (a-z, A-Z and 0-9). Otherwise returns 0. 
int(0..1) is_safe_string(string in) 
{ 
  return strlen(in) && !strlen(replace(in, safe_characters, empty_strings)); 
} 
 
string make_entity( string q ) 
{ 
  return "&"+q+";"; 
} 
 
string make_tag_attributes(mapping(string:string) in, 
                           void|int preserve_roxen_entities) 
{ 
  if (!in || !sizeof(in)) 
    return ""; 
 
  //  Special quoting which leaves Roxen entities (e.g. &page.path;) 
  //  unescaped. 
  string quote_fn(string text) 
  { 
    string out = ""; 
    int pos = 0; 
    while ((pos = search(text, "&")) >= 0) { 
      if ((sscanf(text[pos..], "&%[^ <>;&];", string entity) == 1) && 
          search(entity, ".") >= 0) { 
        out += html_encode_string(text[..pos - 1]) + "&" + entity + ";"; 
        text = text[pos + strlen(entity) + 2..]; 
      } else { 
        out += html_encode_string(text[..pos]); 
        text = text[pos + 1..]; 
      } 
    } 
    return out + html_encode_string(text); 
  }; 
   
  string res = ""; 
  array(string) sorted_attrs = sort(indices(in)); 
  if (preserve_roxen_entities) { 
    foreach(sorted_attrs, string a) 
      res += " " + a + "=\"" + quote_fn((string) in[a]) + "\""; 
  } else { 
    foreach(sorted_attrs, string a) 
      res += " " + a + "=\"" + html_encode_string((string) in[a]) + "\""; 
  } 
  return res; 
} 
 
string make_tag(string name, mapping(string:string) args, void|int xml, 
                void|int preserve_roxen_entities) 
//! Returns an empty element tag @[name], with the tag arguments dictated 
//! by the mapping @[args]. If the flag @[xml] is set, slash character will 
//! be added in the end of the tag. Use RXML.t_xml->format_tag(name, args) 
//! instead. 
{ 
  string attrs = make_tag_attributes(args, preserve_roxen_entities); 
  return "<" + name + attrs + (xml ? " /" : "" ) + ">"; 
} 
 
string make_container(string name, mapping(string:string) args, string content, 
                      void|int preserve_roxen_entities) 
//! Returns a container tag @[name] encasing the string @[content], with 
//! the tag arguments dictated by the mapping @[args]. Use 
//! RXML.t_xml->format_tag(name, args, content) instead. 
{ 
  if(args["/"]=="/") m_delete(args, "/"); 
  return make_tag(name, args, 0, 
                  preserve_roxen_entities) + content + "</" + name + ">"; 
} 
 
string add_config( string url, array config, multiset prestate ) 
{ 
  if(!sizeof(config)) 
    return url; 
  if(strlen(url)>5 && (url[1] == '(' || url[1] == '<')) 
    return url; 
  return "/<" + config * "," + ">" + add_pre_state(url, prestate); 
} 
 
string extension( string f, RequestID|void id) 
{ 
  string ext, key; 
  if(!f || !strlen(f)) return ""; 
  if(!id || !(ext = [string]id->misc[key="_ext_"+f])) { 
    sscanf(reverse(f), "%s.%*s", ext); 
    if(!ext) ext = ""; 
    else { 
      ext = lower_case(reverse(ext)); 
      if(sizeof (ext) && (ext[-1] == '~' || ext[-1] == '#')) 
        ext = ext[..strlen(ext)-2]; 
    } 
    if(id) id->misc[key]=ext; 
  } 
  return ext; 
} 
 
int(0..1) backup_extension( string f ) 
  //! Determines if the provided filename indicates 
  //! that the file is a backup file. 
{ 
  if(!strlen(f)) 
    return 1; 
  return (f[-1] == '#' || f[-1] == '~' || f[0..1]==".#" 
          || (f[-1] == 'd' && sscanf(f, "%*s.old")) 
          || (f[-1] == 'k' && sscanf(f, "%*s.bak"))); 
} 
 
array(string) win_drive_prefix(string path) 
//! Splits path into ({ prefix, path }) array. Prefix is "" for paths on 
//! non-Windows systems or when no proper drive prefix is found. 
{ 
#ifdef __NT__ 
  string prefix; 
  if (sscanf(path, "\\\\%s%*[\\/]%s", prefix, string path_end) == 3) { 
    return ({ "\\\\" + prefix, "/" + path_end }); 
  } else if (sscanf(path, "%1s:%s", prefix, path) == 2) { 
    return ({ prefix + ":", path }); 
  } 
#endif 
  return ({ "", path }); 
} 
 
string simplify_path(string file) 
//! This one will remove .././ etc. in the path. The returned value 
//! will be a canonic representation of the given path. 
{ 
  // Faster for most cases since "//", "./" or "../" rarely exists. 
  if(!strlen(file) || (!has_value(file, "./") && (file[-1] != '.') && 
                       !has_value (file, "//"))) 
    return file; 
 
  int relative, got_slashdot_suffix; 
 
  [string prefix, file] = win_drive_prefix(file); 
 
  if (!has_prefix (file, "/")) 
    relative = 1; 
 
  // The following used to test for "//" at the end (thus replacing 
  // that too with "/."). That must be some kind of old confusion 
  // (dates back to at least roxenlib.pike 1.1 from 11 Nov 1996). 
  // /mast 
  if (has_suffix (file, "/.")) 
    got_slashdot_suffix = 1; 
 
  file=combine_path("/", file); 
 
  if(got_slashdot_suffix) file += "/."; 
  if(relative) return prefix + file[1..]; 
 
  return prefix + file; 
} 
 
string short_date(int timestamp) 
//! Returns a short date string from a time-int 
{ 
  int date = time(1); 
 
  if(ctime(date)[20..23] != ctime(timestamp)[20..23]) 
    return ctime(timestamp)[4..9] +" "+ ctime(timestamp)[20..23]; 
 
  return ctime(timestamp)[4..9] +" "+ ctime(timestamp)[11..15]; 
} 
 
string int2roman(int m) 
  //! Converts the provided integer to a roman integer (i.e. a string). 
{ 
  string res=""; 
  if (m>10000000||m<0) return "que"; 
  while (m>999) { res+="M"; m-=1000; } 
  if (m>899) { res+="CM"; m-=900; } 
  else if (m>499) { res+="D"; m-=500; } 
  else if (m>399) { res+="CD"; m-=400; } 
  while (m>99) { res+="C"; m-=100; } 
  if (m>89) { res+="XC"; m-=90; } 
  else if (m>49) { res+="L"; m-=50; } 
  else if (m>39) { res+="XL"; m-=40; } 
  while (m>9) { res+="X"; m-=10; } 
  if (m>8) return res+"IX"; 
  else if (m>4) { res+="V"; m-=5; } 
  else if (m>3) return res+"IV"; 
  while (m) { res+="I"; m--; } 
  return res; 
} 
 
string number2string(int n, mapping m, array|function names) 
{ 
  string s; 
  switch (m->type) 
  { 
  case "string": 
     if (functionp(names)) { 
       s=([function(int:string)]names)(n); 
       break; 
     } 
     if (n<0 || n>=sizeof(names)) 
       s=""; 
     else 
       s=([array(string)]names)[n]; 
     break; 
  case "roman": 
    s=int2roman(n); 
    break; 
  default: 
    return (string)n; 
  } 
 
  switch(m["case"]) { 
    case "lower": return lower_case(s); 
    case "upper": return upper_case(s); 
    case "capitalize": return capitalize(s); 
  } 
 
#ifdef old_rxml_compat 
  if (m->lower) return lower_case(s); 
  if (m->upper) return upper_case(s); 
  if (m->cap||m->capitalize) return capitalize(s); 
#endif 
 
  return s; 
} 
 
string image_from_type( string t ) 
  //! Returns an internal-gopher icon link that corresponds to the 
  //! provided MIME-type, e.g. "internal-gopher-image" for "image/gif". 
{ 
  if(t) 
  { 
    sscanf(t, "%s/", t); 
    switch(t) 
    { 
     case "audio": 
     case "sound": 
      return "internal-gopher-sound"; 
     case "image": 
      return "internal-gopher-image"; 
     case "application": 
      return "internal-gopher-binary"; 
     case "text": 
      return "internal-gopher-text"; 
    } 
  } 
  return "internal-gopher-unknown"; 
} 
 
protected constant size_suffix = 
  ({ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }); 
 
string sizetostring( int|float size ) 
  //! Returns the size as a memory size string with suffix, 
  //! e.g. 43210 is converted into "42.2 kB". To be correct 
  //! to the latest standards it should really read "42.2 KiB", 
  //! but we have chosen to keep the old notation for a while. 
  //! The function knows about the quantifiers kilo, mega, giga, 
  //! tera, peta, exa, zetta and yotta. 
{ 
  int neg = size < 0; 
  int|float abs_size = abs (size); 
 
  if (abs_size < 1024) { 
    if (intp (size)) 
      return size + " " + size_suffix[0]; 
    return size < 10.0 ? 
      sprintf ("%.2f %s", size, size_suffix[0]) : 
      sprintf ("%.0f %s", size, size_suffix[0]); 
  } 
 
  float s = (float) abs_size; 
  size=0; 
  while( s >= 1024.0 ) 
  { 
    s /= 1024.0; 
    if (++size == sizeof (size_suffix) - 1) break; 
  } 
  if (neg) s = -s; 
  return sprintf("%.1f %s", s, size_suffix[ size ]); 
} 
 
string format_hrtime (int hrtime, void|int pad) 
//! Returns a nicely formatted string for a time lapse value expressed 
//! in microseconds. If @[pad] is nonzero then the value is formatted 
//! right justified in a fixed-length string. 
{ 
  if (hrtime < 1000000) 
    return sprintf (pad ? "%7.3f ms" : "%.3f ms", hrtime / 1e3); 
  else if (hrtime < 60 * 1000000) 
    return sprintf (pad ? "%8.3f s" : "%.3f s", hrtime / 1e6); 
  else if (hrtime < 60 * 60 * 1000000) 
    return sprintf (pad ? "%3d:%02d min" : "%d:%02d min", 
                    hrtime / (60 * 1000000), (hrtime / 1000000) % 60); 
  else 
    return sprintf (pad ? "%4d:%02d:%02d" : "%d:%02d:%02d", 
                    hrtime / (60 * 60 * 1000000), 
                    (hrtime / (60 * 1000000)) % 60, 
                    (hrtime / 1000000) % 60); 
} 
 
string html_decode_string(LocaleString str) 
//! Decodes `str', opposite to @[html_encode_string()]. 
{ 
  return replace((string)str, replace_entities, replace_values); 
} 
 
string html_encode_tag_value(LocaleString str) 
//! Encodes `str' for use as a value in an html tag. 
{ 
  // '<' is not allowed in attribute values in XML 1.0. 
  return "\"" + replace((string)str, ({"&", "\"", "<"}), ({"&amp;", "&quot;", "&lt;"})) + "\""; 
} 
 
protected string my_sprintf(int prefix, string f, int arg) 
//! Filter prefix option in format string if prefix = 0. 
{ 
  if(!prefix && sscanf(f, "%%%*d%s", string format) == 2) 
    f = "%" + format; 
  return sprintf(f, arg); 
} 
 
string strftime(string fmt, int t, 
                void|string lang, void|function language, void|RequestID id) 
//! Encodes the time `t' according to the format string `fmt'. 
{ 
  if(!sizeof(fmt)) return ""; 
  mapping lt = localtime(t); 
  fmt=replace(fmt, "%%", "\0"); 
  array(string) a = fmt/"%"; 
  string res = a[0]; 
  mapping(string:string) m = (["type":"string"]); 
   
  foreach(a[1..], string key) { 
    m_delete (m, "case"); 
    int(0..1) prefix = 1; 
    int(0..1) alternative_numbers = 0; 
    int(0..1) alternative_form = 0; 
    while (sizeof(key)) { 
      switch(key[0]) { 
        // Flags. 
      case '!':     // Inhibit numerical padding (Roxen specific). 
      case '-':     // Inhibit numerical padding (glibc-style). 
        prefix = 0; 
        key = key[1..]; 
        continue; 
      case '^':     // Upper-case (glibc-style). 
        m->case = "upper"; 
        key = key[1..]; 
        continue; 
      case '~':     // Capitalize (Roxen specific). 
        m->case = "capitalize"; 
        key = key[1..]; 
        continue; 
      case 'E':     // Locale-dependent alternative form. 
        alternative_form = 1; 
        key = key[1..]; 
        continue; 
      case 'O':     // Locale-dependent alternative numeric representation. 
        alternative_numbers = 1; 
        key = key[1..]; 
        continue; 
 
        // Formats. 
      case 'a':     // Abbreviated weekday name 
        if (language) 
          res += number2string(lt->wday+1,m,language(lang,"short_day",id)); 
        else 
          res += ({ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" })[lt->wday]; 
        break; 
      case 'A':     // Weekday name 
        if (language) 
          res += number2string(lt->wday+1,m,language(lang,"day",id)); 
        else 
          res += ({ "Sunday", "Monday", "Tuesday", "Wednesday", 
                    "Thursday", "Friday", "Saturday" })[lt->wday]; 
        break; 
      case 'b':     // Abbreviated month name 
      case 'h':     // Abbreviated month name 
        if (language) 
          res += number2string(lt->mon+1,m,language(lang,"short_month",id)); 
        else 
          res += ({ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" })[lt->mon]; 
        break; 
      case 'B':     // Month name 
        if (language) { 
          if (alternative_form) { 
            res += number2string(lt->mon+1,m,language(lang,"numbered_month",id)); 
          } else { 
            res += number2string(lt->mon+1,m,language(lang,"month",id)); 
          } 
        } else 
          res += ({ "January", "February", "March", "April", "May", "June", 
                    "July", "August", "September", "October", "November", "December" })[lt->mon]; 
        break; 
      case 'c':     // Date and time 
        // FIXME: Should be preferred date and time for the locale. 
        res += strftime(sprintf("%%a %%b %02d  %02d:%02d:%02d %04d", 
                                lt->mday, lt->hour, lt->min, lt->sec, 1900 + lt->year), t); 
        break; 
      case 'C':     // Century number; 0-prefix 
        res += my_sprintf(prefix, "%02d", 19 + lt->year/100); 
        break; 
      case 'd':     // Day of month [1,31]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->mday); 
        break; 
      case 'D':     // Date as %m/%d/%y 
        res += strftime("%m/%d/%y", t); 
        break; 
      case 'e':     // Day of month [1,31]; space-prefix 
        res += my_sprintf(prefix, "%2d", lt->mday); 
        break; 
      case 'F':     // ISO 8601 date %Y-%m-%d 
        res += sprintf("%04d-%02d-%02d", 
                       1900 + lt->year, lt->mon + 1, lt->mday); 
        break; 
      case 'G':     // Year for the ISO 8601 week containing the day. 
        { 
          int wday = (lt->wday + 1)%7;      // ISO 8601 weekday number. 
          if ((wday - lt->yday) >= 4) { 
            // The day belongs to the last week of the previous year. 
            res += my_sprintf(prefix, "%04d", 1899 + lt->year); 
          } else if ((lt->mon == 11) && ((lt->mday - wday) >= 29)) { 
            // The day belongs to the first week of the next year. 
            res += my_sprintf(prefix, "%04d", 1901 + lt->year); 
          } else { 
            res += my_sprintf(prefix, "%04d", 1900 + lt->year); 
          } 
        } 
        break; 
      case 'g':     // Short year for the ISO 8601 week containing the day. 
        { 
          int wday = (lt->wday + 1)%7;      // ISO 8601 weekday number. 
          if ((wday - lt->yday) >= 4) { 
            // The day belongs to the last week of the previous year. 
            res += my_sprintf(prefix, "%02d", (99 + lt->year) % 100); 
          } else if ((lt->mon == 11) && ((lt->mday - wday) >= 29)) { 
            // The day belongs to the first week of the next year. 
            res += my_sprintf(prefix, "%02d", (1 + lt->year) % 100); 
          } else { 
            res += my_sprintf(prefix, "%02d", (lt->year) % 100); 
          } 
        } 
        break; 
      case 'H':     // Hour (24-hour clock) [0,23]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->hour); 
        break; 
      case 'I':     // Hour (12-hour clock) [1,12]; 0-prefix 
        res += my_sprintf(prefix, "%02d", 1 + (lt->hour + 11)%12); 
        break; 
      case 'j':     // Day number of year [1,366]; 0-prefix 
        res += my_sprintf(prefix, "%03d", lt->yday); 
        break; 
      case 'k':     // Hour (24-hour clock) [0,23]; space-prefix 
        res += my_sprintf(prefix, "%2d", lt->hour); 
        break; 
      case 'l':     // Hour (12-hour clock) [1,12]; space-prefix 
        res += my_sprintf(prefix, "%2d", 1 + (lt->hour + 11)%12); 
        break; 
      case 'm':     // Month number [1,12]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->mon + 1); 
        break; 
      case 'M':     // Minute [00,59]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->min); 
        break; 
      case 'n':     // Newline 
        res += "\n"; 
        break; 
      case 'p':     // a.m. or p.m. 
        res += lt->hour<12 ? "a.m." : "p.m."; 
        break; 
      case 'P':     // am or pm 
        res += lt->hour<12 ? "am" : "pm"; 
        break; 
      case 'r':     // Time in 12-hour clock format with %p 
        res += strftime("%I:%M:%S %p", t); 
        break; 
      case 'R':     // Time as %H:%M 
        res += sprintf("%02d:%02d", lt->hour, lt->min); 
        break; 
      case 's':     // Seconds since epoch. 
        res += my_sprintf(prefix, "%d", t); 
        break; 
      case 'S':     // Seconds [00,61]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->sec); 
        break; 
      case 't':     // Tab 
        res += "\t"; 
        break; 
      case 'T':     // Time as %H:%M:%S 
      case 'X':     // FIXME: Time in locale preferred format. 
        res += sprintf("%02d:%02d:%02d", lt->hour, lt->min, lt->sec); 
        break; 
      case 'u':     // Weekday as a decimal number [1,7], Monday == 1 
        res += my_sprintf(prefix, "%d", 1 + ((lt->wday + 6) % 7)); 
        break; 
      case 'U':     // Week number of current year [00,53]; 0-prefix 
                // Sunday is first day of week. 
        res += my_sprintf(prefix, "%02d", 1 + (lt->yday - lt->wday)/ 7); 
        break; 
      case 'V':     // ISO week number of the year as a decimal number [01,53]; 0-prefix 
        res += my_sprintf(prefix, "%02d", Calendar.ISO.Second(t)->week_no()); 
        break; 
      case 'w':     // Weekday as a decimal number [0,6], Sunday == 0 
        res += my_sprintf(prefix, "%d", lt->wday); 
        break; 
      case 'W':     // Week number of year as a decimal number [00,53], 
                // with Monday as the first day of week 1; 0-prefix 
        res += my_sprintf(prefix, "%02d", ((lt->yday+(5+lt->wday)%7)/7)); 
        break; 
      case 'x':     // Date 
                // FIXME: Locale preferred date format. 
        res += strftime("%a %b %d %Y", t); 
        break; 
      case 'y':     // Year [00,99]; 0-prefix 
        res += my_sprintf(prefix, "%02d", lt->year % 100); 
        break; 
      case 'Y':     // Year [0000.9999]; 0-prefix 
        res += my_sprintf(prefix, "%04d", 1900 + lt->year); 
        break; 
      case 'z':     // Time zone as hour offset from UTC. 
                // Needed for RFC822 dates. 
        { 
          int minutes = lt->timezone/60; 
          int hours = minutes/60; 
          minutes -= hours * 60; 
          res += my_sprintf(prefix, "%+05d%", hours*100 + minutes); 
        } 
        break; 
      case 'Z':     // FIXME: Time zone name or abbreviation, or no bytes if 
                // no time zone information exists 
        break; 
      } 
      res+=key[1..]; 
      break; 
    } 
  } 
  return replace(res, "\0", "%"); 
} 
 
RoxenModule get_module (string modname) 
//! Resolves a string as returned by get_modname to a module object if 
//! one exists. 
{ 
  string cname, mname; 
  int mid = 0; 
 
  if (sscanf (modname, "%s/%s", cname, mname) != 2 || 
      !sizeof (cname) || !sizeof(mname)) return 0; 
  sscanf (mname, "%s#%d", mname, mid); 
 
  if (Configuration conf = roxen->get_configuration (cname)) 
    if (mapping moddata = conf->modules[mname]) 
      return moddata->copies[mid]; 
 
  return 0; 
} 
 
string get_modname (RoxenModule module) 
//! Returns a string uniquely identifying the given module on the form 
//! `<config name>/<module short name>#<copy>'. 
{ 
  return module && module->module_identifier(); 
} 
 
string get_modfullname (RoxenModule module) 
//! This determines the full module (human-readable) name in 
//! approximately the same way as the config UI. Note that the 
//! returned string is text/html. 
{ 
  if (module) { 
    string|mapping(string:string)|Locale.DeferredLocale name = 0; 
    if (module->query) 
      catch { 
        mixed res = module->query ("_name"); 
        if (res) name = (string) res; 
      }; 
    if (!(name && sizeof (name)) && module->query_name) 
      name = module->query_name(); 
    if (!(name && sizeof (name))) { 
      name = [string]module->register_module()[1]; 
      sscanf (module->module_local_id(), "%*s#%d", int mod_copy); 
      if (mod_copy) name += " # " + mod_copy; 
    } 
    if (mappingp (name)) 
      name = name->standard; 
    return (string) name; 
  } 
  else return 0; 
} 
 
protected constant xml_invalid_mappings = ([ 
  "\0":"\22000",  "\1":"\22001", 
  "\2":"\22002",  "\3":"\22003", 
  "\4":"\22004",  "\5":"\22005", 
  "\6":"\22006",  "\7":"\22007", 
  "\b":"\22010",  "\13":"\22013", 
  "\14":"\22014", "\16":"\22016", 
  "\17":"\22017", "\20":"\22020", 
  "\21":"\22021", "\22":"\22022", 
  "\23":"\22023", "\24":"\22024", 
  "\25":"\22025", "\26":"\22026", 
  "\27":"\22027", "\30":"\22030", 
  "\31":"\22031", "\32":"\22032", 
  "\33":"\22033", "\34":"\22034", 
  "\35":"\22035", "\36":"\22036", 
  "\37":"\22037", "\177":"\22041", 
  "\xFFFE":"", "\xFFFF":"" // Invalid unicode chars in XML! 
]); 
 
string encode_xml_invalids(string s) 
//! Remap control characters not valid in XML-documents to their 
//! corresponding printable code points (@tt{U2400 - U2421@}). 
{ 
  return replace(s, xml_invalid_mappings); 
} 
 
//! Encode a single segment of @[roxen_encode()]. 
//! 
//! See @[roxen_encode()] for details. 
protected string low_roxen_encode(string val, string encoding) 
{ 
  switch (encoding) { 
   case "": 
   case "none": 
     return val; 
 
   case "utf8": 
   case "utf-8": 
     return string_to_utf8(val); 
 
   case "-utf8": 
   case "-utf-8": 
    if( catch { 
        return utf8_to_string(val); 
      }) 
      RXML.run_error("Cannot decode utf-8 string. Bad data.\n"); 
 
   case "utf16": 
   case "utf16be": 
     return Charset.encoder("utf16be")->feed(val)->drain(); 
 
   case "utf16le": 
     return Charset.encoder("utf16le")->feed(val)->drain(); 
 
  case "hex": 
    if(String.width(val) > 8) 
      RXML.run_error(  "Cannot hex encode wide characters.\n" ); 
    return String.string2hex(val); 
 
  case "-hex": 
    if( catch { 
        return String.hex2string(val); 
      }) 
      RXML.run_error("Cannot decode hex string. Bad data.\n"); 
 
  case "base64": 
  case "base-64": 
  case "b64": 
    return MIME.encode_base64(val); 
 
  case "-base64": 
  case "-base-64": 
  case "-b64": 
    if( catch { 
        return MIME.decode_base64(val); 
      }) 
      RXML.run_error("Cannot decode base64 string. Bad data.\n"); 
 
  case "base64url": 
  case "base-64-url": 
  case "b64url": 
#if constant (MIME.encode_base64url) 
    return MIME.encode_base64url(val); 
#else 
    return replace (MIME.encode_base64 (val, 1), 
                    ([ "=": "", 
                       "+": "-", 
                       "/": "_" ])); 
#endif 
  case "-base64url": 
  case "-base-64-url": 
  case "-b64url": 
    if( catch { 
#if constant (MIME.decode_base64url) 
        return MIME.decode_base64url(val); 
#else 
        string data = replace (val, ([ "-": "+", 
                                       "_": "/", 
                                       "=": "" ])); 
        data = (data + ("=" * (4-sizeof (data) % 4))); // Add padding. 
        return MIME.decode_base64 (data); 
#endif 
      }) 
      RXML.run_error("Cannot decode base64 string. Bad data.\n"); 
 
  case "md5": 
  case "sha1": 
  case "sha256": 
    if (String.width(val) > 8) 
      RXML.run_error("Cannot hash wide characters.\n"); 
    return Crypto[upper_case(encoding)]->hash(val); 
     
   case "quotedprintable": 
   case "quoted-printable": 
   case "qp": 
     return MIME.encode_qp(val); 
 
   case "http": 
     return http_encode_invalids (val); 
 
   case "cookie": 
     return http_encode_cookie (val); 
 
   case "url": 
     return http_encode_url (val); 
 
   case "wml-url": 
     // Note: In 4.0 and earlier, this encoding was ambiguous since 8 
     // bit strings were %-encoded according to the ISO 8859-1 charset 
     // while wider strings first were UTF-8 encoded and then 
     // %-encoded. Although unlikely, it might be possible that the 
     // old ambiguous encoding is the one mandated by the WAP/WML 
     // standard - I haven't been able to verify it. /mast 
     return http_encode_url(val); 
 
   case "html": 
     return html_encode_string (val); 
   case "-html": 
     //  Can't use html_decode_string() which doesn't understand numerical 
     //  entities. 
     return RXML.TXml()->decode_charrefs(val); 
 
   case "invalids": 
   case "xmlinvalids": 
   case "xml-invalids": 
     return encode_xml_invalids(val); 
 
   case "wml": 
     return replace(html_encode_string(val), "$", "$$"); 
 
   case "dtag": 
     // This is left for compatibility... 
     return replace (val, "\"", "\"'\"'\""); 
 
   case "stag": 
     // This is left for compatibility 
     return replace(val, "'", "'\"'\"'"); 
 
   case "pike": 
     return replace (val, 
                    ({ "\"", "\\", "\n" }), 
                    ({ "\\\"", "\\\\", "\\n" })); 
 
   case "json": 
#if constant (Standards.JSON.escape_string) 
     return Standards.JSON.escape_string (val); 
#else 
     // Simpler variant for compat with older pikes. 
     return replace(val, 
                   ({ "\"",   "\\",   "/",   "\b", 
                      "\f",   "\n",   "\r",  "\t", 
                      "\u2028",       "\u2029", }), 
                   ({ "\\\"", "\\\\", "\\/", "\\b", 
                      "\\f",  "\\n",  "\\r", "\\t", 
                      "\\u2028",      "\\u2029", })); 
#endif 
 
   case "js": 
   case "javascript": 
     return replace (val, 
                    ({ "\b", "\014", "\n", "\r", "\t", "\\", 
                       "'", "\"", 
                       "\u2028", "\u2029", 
                       "</", "<!--"}), 
                    ({ "\\b", "\\f", "\\n", "\\r", "\\t", "\\\\", 
                       "\\'", "\\\"", 
                       "\\u2028", "\\u2029", 
                       "<\\/", "<\\!--" })); 
 
   case "mysql": 
     // Note: Quotes the single-quote (') in traditional sql-style, 
     //       for maximum compatibility with other sql databases. 
     return replace (val, 
                    ({ "\"", "'", "\\" }), 
                    ({ "\\\"" , "''", "\\\\" }) ); 
 
   case "sql": 
   case "oracle": 
     return replace (val, "'", "''"); 
 
  case "bytea": 
    return replace (val, 
                    ({ "'", "\\", "\0", "&" }), 
                    ({ "\\'", "\\\\\\\\", "\\\\000", "\\\\046" }) ); 
 
   case "csv": 
     if (sizeof(val) && 
         ((<' ', '\t'>)[val[0]] || (<' ', '\t'>)[val[-1]] || 
          has_value(val, ",") || has_value(val, ";") || 
          has_value(val, "\"") || has_value(val, "\n"))) { 
       return "\"" + replace(val, "\"", "\"\"") + "\""; 
     } 
     return val; 
 
   case "mysql-dtag": 
     // This is left for compatibility 
     return replace (val, 
                    ({ "\"", "'", "\\" }), 
                    ({ "\\\"'\"'\"", "\\'", "\\\\" })); 
 
   case "mysql-pike": 
     return replace (val, 
                    ({ "\"", "'", "\\", "\n" }), 
                    ({ "\\\\\\\"", "\\\\'", 
                       "\\\\\\\\", "\\n" }) ); 
 
   case "sql-dtag": 
   case "oracle-dtag": 
     // This is left for compatibility 
     return replace (val, 
                    ({ "'", "\"" }), 
                    ({ "''", "\"'\"'\"" }) ); 
 
   default: 
     // Unknown encoding. Let the caller decide what to do with it. 
     return 0; 
  } 
} 
 
//! Quote strings in a multitude of ways. Used primarily by entity quoting. 
//! 
//! The @[encoding] string is split on @expr{"."@}, and encoded in order. 
//! 
//! The segments in the split @[encoding] string can be any of 
//! the following: 
//! @string 
//!   @value "" 
//!   @value "none" 
//!     No encoding. 
//! 
//!   @value "utf8" 
//!   @value "utf-8" 
//!     UTF-8 encoding. C.f. @[string_to_utf8]. 
//! 
//!   @value "-utf8" 
//!   @value "-utf-8" 
//!     UTF-8 decoding. C.f. @[utf8_to_string]. 
//! 
//!   @value "utf16" 
//!   @value "utf16be" 
//!     (Big endian) UTF-16 encoding. C.f. @[Charset], encoder 
//!     @expr{"utf16be"@}. 
//! 
//!   @value "utf16le" 
//!     Little endian UTF-16 encoding. C.f. @[Charset], encoder 
//!     @expr{"utf16le"@}. 
//! 
//!   @value "hex" 
//!     Hexadecimal encoding, e.g. @expr{"foo"@} is encoded to 
//!     @expr{"666f6f"@}. Requires octet (i.e. non-wide) strings. 
//!     C.f. @[String.string2hex]. 
//! 
//!   @value "-hex" 
//!     Hexadecimal decoding, e.g. @expr{"666f6f"@} is decoded to 
//!     @expr{"foo"@}. 
//!     C.f. @[String.hex2string]. 
//! 
//!   @value "base64" 
//!   @value "base-64" 
//!   @value "b64" 
//!     Base-64 MIME encoding. Requires octet (i.e. non-wide) strings. 
//!     C.f. @[MIME.encode_base64]. 
//! 
//!   @value "-base64" 
//!   @value "-base-64" 
//!   @value "-b64" 
//!     Base-64 MIME decoding. 
//!     C.f. @[MIME.decode_base64]. 
//! 
//!   @value "md5" 
//!   @value "sha1" 
//!   @value "sha256" 
//!     Message digest using supplied hash algorithm. Requires octet 
//!     (i.e. non-wide) strings. Note that the result is a binary string 
//!     so apply e.g. hex encoding afterward to get a printable value. 
//!     C.f. @[Crypto.MD5.hash], @[Crypto.SHA1.hash] and 
//!     @[Crypto.SHA256.hash]. 
//! 
//!   @value "quotedprintable" 
//!   @value "quoted-printable" 
//!   @value "qp" 
//!     Quoted-Printable MIME encoding. Requires octet (i.e. non-wide) 
//!     strings. C.f. @[MIME.encode_qp]. 
//! 
//!   @value "http" 
//!     HTTP encoding (i.e. using @expr{%xx@} style escapes) of 
//!     characters that never can occur verbatim in URLs. Other 
//!     URL-special chars, including @expr{%@}, are not encoded. 8-bit 
//!     and wider chars are encoded according to the IRI standard (RFC 
//!     3987). C.f. @[Roxen.http_encode_invalids]. 
//! 
//!   @value "url" 
//!     Similar to the @expr{"http"@} encoding, but encodes all URI 
//!     reserved and excluded chars, that otherwise could have special 
//!     meaning; see RFC 3986. This includes @expr{:@}, @expr{/@}, 
//!     @expr{%@}, and quote chars. C.f. @[Roxen.http_encode_url]. 
//! 
//!   @value "cookie" 
//!     Nonstandard HTTP-style encoding for cookie values. The Roxen 
//!     HTTP protocol module automatically decodes incoming cookies 
//!     using this encoding, so by using this for @expr{Set-Cookie@} 
//!     headers etc you will get back the original value in the 
//!     @expr{cookie@} scope. Note that @[Roxen.set_cookie] and the 
//!     RXML @expr{<set-cookie>@} tag already does this encoding for 
//!     you. C.f. @[Roxen.http_encode_cookie]. 
//! 
//!   @value "html" 
//!     HTML encoding, for generic text in html documents. This means 
//!     encoding chars like @expr{<@}, @expr{&@}, and quotes using 
//!     character reference entities. 
//! 
//!   @value "-html" 
//!     HTML decoding of entities (literals and decimal/hexadecimal 
//!     representations). 
//! 
//!   @value "wml" 
//!     HTML encoding, and doubling of any @tt{$@}'s. 
//! 
//!   @value "csv" 
//!     CSV (Comma Separated Values) encoding. Properly quotes all 
//!     separator characters in CSV records (comma, semicolon, double-quotes 
//!     leading spaces and newlines). 
//! 
//!   @value "pike" 
//!     Pike string quoting, for use in e.g. the @tt{<pike></pike>@} 
//!     tag. This means backslash escapes for chars that cannot occur 
//!     verbatim in Pike string literals. 
//! 
//!   @value "json" 
//!     JSON string quoting. Similar to the @expr{"js"@} quoting, 
//!     but keeps strictly to RFC 4627. 
//! 
//!   @value "js" 
//!   @value "javascript" 
//!     Javascript string quoting, i.e. using backslash escapes for 
//!     @expr{"@}, @expr{\@}, and more. 
//! 
//!     For safe use inside @tt{<script>@} elements, it quotes some 
//!     additional character sequences: 
//! 
//!     @ul 
//!     @item 
//!       @tt{</@} is quoted as @tt{<\/@} according to appendix B.3.2 
//!       in the HTML 4.01 spec. 
//!     @item 
//!       @tt{<!--@} is quoted as @tt{<\!--@} according to 4.3.1.2 in 
//!       the HTML 5 spec. 
//!     @endul 
//! 
//!     Both are harmless in Javascript string literals in other 
//!     contexts. 
//! 
//!   @value "mysql" 
//!     MySQL quoting. This also means backslash escapes, except the 
//!     @expr{'@} character which is quoted in SQL style as 
//!     @expr{''@}. 
//! 
//!   @value "sql" 
//!   @value "oracle" 
//!     SQL/Oracle quoting, i.e. @expr{'@} is encoded as @expr{''@}. 
//! 
//!     NOTE: Do NOT use this quoting method when creating 
//!           sql-queries intended for MySQL! 
//! 
//!   @value "bytea" 
//!     PostgreSQL quoting for BYTEA (binary) values. 
//! 
//!   @value "mysql-pike" 
//!     Compat. MySQL quoting followed by Pike string quoting. 
//!     Equvivalent to using @expr{"mysql.pike"@}. 
//! 
//!   @value "wml-url" 
//!     Compat alias for @expr{"url"@}. 
//! 
//!   @value "dtag" 
//!   @value "stag" 
//!     Compat. @expr{"dtag"@} encodes @expr{"@} as @expr{"'"'"@}, and 
//!     @expr{"stag"@} encodes @expr{'@} as @expr{'"'"'@}. They were 
//!     used frequently before rxml 2.0 to quote rxml attributes, but 
//!     are no longer necessary. 
//! 
//!   @value "mysql-dtag" 
//!   @value "sql-dtag" 
//!   @value "oracle-dtag" 
//!     Compat. Same as @expr{"mysql.dtag"@}, @expr{"sql.dtag"@}, and 
//!     @expr{"oracle.dtag@}, respectively. 
//! @endstring 
//! 
//! Returns zero if the encoding isn't recognized. 
//! 
//! @example 
//!   UTF8-encode a string for use in a Mysql query in an HTML page: 
//!   @expr{roxen_encode(val, "utf8.mysql.html")@}. 
string roxen_encode(string val, string encoding) 
{ 
  foreach(encoding/".", string enc) { 
    if (!(val = low_roxen_encode(val, enc))) 
      return 0; 
  } 
  return val; 
} 
 
string fix_relative( string file, RequestID|void id ) 
//! Using @expr{@[id]->not_query@}, turns a relative (or already 
//! absolute) virtual path into an absolute virtual path, i.e. one 
//! rooted at the virtual server's root directory. The returned path 
//! is simplified to not contain any @expr{"."@} or @expr{".."@} 
//! segments. 
{ 
  Standards.URI uri = Standards.URI(":///"); 
  if (id) { 
    uri = Standards.URI(id->not_query, uri); 
  } 
  uri = Standards.URI(file, uri); 
  if (uri->path != "") { 
    // NB: Without the above test "" will expand to "/". 
    //     cf [bug 7566]. 
    uri->path = (uri->combine_uri_path("", uri->path)/"/" - ({ ".." })) * "/"; 
  } 
  string res = sprintf("%s", uri); 
  // +(id->misc->path_info?id->misc->path_info:"");