[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
[0456]
[0457]
[0458]
[0459]
[0460]
[0461]
[0462]
[0463]
[0464]
[0465]
[0466]
[0467]
[0468]
[0469]
[0470]
[0471]
[0472]
[0473]
[0474]
[0475]
[0476]
[0477]
[0478]
[0479]
[0480]
[0481]
[0482]
[0483]
[0484]
[0485]
[0486]
[0487]
[0488]
[0489]
[0490]
[0491]
[0492]
[0493]
[0494]
[0495]
[0496]
[0497]
[0498]
[0499]
[0500]
[0501]
[0502]
[0503]
[0504]
[0505]
[0506]
[0507]
[0508]
[0509]
[0510]
[0511]
[0512]
[0513]
[0514]
[0515]
[0516]
[0517]
[0518]
[0519]
[0520]
[0521]
[0522]
[0523]
[0524]
[0525]
[0526]
[0527]
[0528]
[0529]
[0530]
[0531]
[0532]
[0533]
[0534]
[0535]
[0536]
[0537]
[0538]
[0539]
[0540]
[0541]
[0542]
[0543]
[0544]
[0545]
[0546]
[0547]
[0548]
[0549]
[0550]
[0551]
[0552]
[0553]
[0554]
[0555]
[0556]
[0557]
[0558]
[0559]
[0560]
[0561]
[0562]
[0563]
[0564]
[0565]
[0566]
[0567]
[0568]
[0569]
[0570]
[0571]
[0572]
[0573]
[0574]
[0575]
[0576]
[0577]
[0578]
[0579]
[0580]
[0581]
[0582]
[0583]
[0584]
[0585]
[0586]
[0587]
[0588]
[0589]
[0590]
[0591]
[0592]
[0593]
[0594]
[0595]
[0596]
[0597]
[0598]
[0599]
[0600]
[0601]
[0602]
[0603]
[0604]
[0605]
[0606]
[0607]
[0608]
[0609]
[0610]
[0611]
[0612]
[0613]
[0614]
[0615]
[0616]
[0617]
[0618]
[0619]
[0620]
[0621]
[0622]
[0623]
[0624]
[0625]
[0626]
[0627]
[0628]
[0629]
[0630]
[0631]
[0632]
[0633]
[0634]
[0635]
[0636]
[0637]
[0638]
[0639]
[0640]
[0641]
[0642]
[0643]
[0644]
[0645]
[0646]
[0647]
[0648]
[0649]
[0650]
[0651]
[0652]
[0653]
[0654]
[0655]
[0656]
[0657]
[0658]
[0659]
[0660]
[0661]
[0662]
[0663]
[0664]
[0665]
[0666]
[0667]
[0668]
[0669]
[0670]
[0671]
[0672]
[0673]
[0674]
[0675]
[0676]
[0677]
[0678]
[0679]
[0680]
[0681]
[0682]
[0683]
[0684]
[0685]
[0686]
[0687]
[0688]
[0689]
[0690]
[0691]
[0692]
[0693]
[0694]
[0695]
[0696]
[0697]
[0698]
[0699]
[0700]
[0701]
[0702]
[0703]
[0704]
[0705]
[0706]
[0707]
[0708]
[0709]
[0710]
[0711]
[0712]
[0713]
[0714]
[0715]
[0716]
[0717]
[0718]
[0719]
[0720]
[0721]
[0722]
[0723]
[0724]
[0725]
[0726]
[0727]
[0728]
[0729]
[0730]
[0731]
[0732]
[0733]
[0734]
[0735]
[0736]
[0737]
[0738]
[0739]
[0740]
[0741]
[0742]
[0743]
[0744]
[0745]
[0746]
[0747]
[0748]
[0749]
[0750]
[0751]
[0752]
[0753]
[0754]
[0755]
[0756]
[0757]
[0758]
[0759]
[0760]
[0761]
[0762]
[0763]
[0764]
[0765]
[0766]
[0767]
[0768]
[0769]
[0770]
[0771]
[0772]
[0773]
[0774]
[0775]
[0776]
[0777]
[0778]
[0779]
[0780]
[0781]
[0782]
[0783]
[0784]
[0785]
[0786]
[0787]
[0788]
[0789]
[0790]
[0791]
[0792]
[0793]
[0794]
[0795]
[0796]
[0797]
[0798]
[0799]
[0800]
[0801]
[0802]
[0803]
[0804]
[0805]
[0806]
[0807]
[0808]
[0809]
[0810]
[0811]
[0812]
[0813]
[0814]
[0815]
[0816]
[0817]
[0818]
[0819]
[0820]
[0821]
[0822]
[0823]
[0824]
[0825]
[0826]
[0827]
[0828]
[0829]
[0830]
[0831]
[0832]
[0833]
[0834]
[0835]
[0836]
[0837]
[0838]
[0839]
[0840]
[0841]
[0842]
[0843]
[0844]
[0845]
[0846]
[0847]
[0848]
[0849]
[0850]
[0851]
[0852]
[0853]
[0854]
[0855]
[0856]
[0857]
[0858]
[0859]
[0860]
[0861]
[0862]
[0863]
[0864]
[0865]
[0866]
[0867]
[0868]
[0869]
[0870]
[0871]
[0872]
[0873]
[0874]
[0875]
[0876]
[0877]
[0878]
[0879]
[0880]
[0881]
[0882]
[0883]
[0884]
[0885]
[0886]
[0887]
[0888]
[0889]
[0890]
[0891]
[0892]
[0893]
[0894]
[0895]
[0896]
[0897]
[0898]
[0899]
[0900]
[0901]
[0902]
[0903]
[0904]
[0905]
[0906]
[0907]
[0908]
[0909]
[0910]
[0911]
[0912]
[0913]
[0914]
[0915]
[0916]
[0917]
[0918]
[0919]
[0920]
[0921]
[0922]
[0923]
[0924]
[0925]
[0926]
[0927]
[0928]
[0929]
[0930]
[0931]
[0932]
[0933]
[0934]
[0935]
[0936]
[0937]
[0938]
[0939]
[0940]
[0941]
[0942]
[0943]
[0944]
[0945]
[0946]
[0947]
[0948]
[0949]
[0950]
[0951]
[0952]
[0953]
[0954]
[0955]
[0956]
[0957]
[0958]
[0959]
[0960]
[0961]
[0962]
[0963]
[0964]
[0965]
[0966]
[0967]
[0968]
[0969]
[0970]
[0971]
[0972]
[0973]
[0974]
[0975]
[0976]
[0977]
[0978]
[0979]
[0980]
[0981]
[0982]
[0983]
[0984]
[0985]
[0986]
[0987]
[0988]
[0989]
[0990]
[0991]
[0992]
[0993]
[0994]
[0995]
[0996]
[0997]
[0998]
[0999]
[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]
/*****************************************************************************
/*
                                 Support.c

Miscellaneous support functions for HTTPd.


VERSION HISTORY
---------------
12-JUL-2021  MGD  rework UTC/GMT processing
                  GenerateUniqueId() algorithm changed
                  bugfix; HttpIsoTime() timezone parsing
06-MAR-2021  MGD  HttpGetTime()
14-NOV-2020  MGD  migrate from ulong[2] to int64 for 64 bit quantities
26-JUL-2018  MGD  SysTrnLnm() moved from dyanamic to static return
23-DEC-2017  MGD  HttpIsoTimeString()
30-OCT-2017  MGD  ThisLongAgo()
04-MAY-2017  MGD  HtmlMetaInfo() MS Edge <meta name="format-detection..> :-{
26-JAN-2016  MGD  ListAdd..() enforce entry data type
13-NOV-2015  MGD  QuadLtQuad() for 32 bit platform
08-JUL-2014  MGD  ConvertUtf8ToEscape() for UTF-8 encoded file name
24-JAN-2010  MGD  8 bit ASCII ConvertToUtf8() and ConvertFromUtf8()
06-JUL-2009  MGD  v10orPrev10()
02-JUN-2009  MGD  UserAtClient()
                  VmsToHttpStatus() RMS$_FLK from 409 to 423
09-JAN-2008  MGD  CompareVmsTime64() accomodate (VAX?) LIB$ RTL issue
                  with pre-V7.3(?) lib$sub_times() returning SS$_IVTIME
31-MAY-2007  MGD  ErrorVmsToHttpStatus()
                  incorporate WebDAV (RFC 2518) response codes
04-JUL-2006  MGD  PercentOf32() calculating percentages of unsigned longs
                  PercentOf64() calculating percentages of unsigned quads
31-JUL-2005  MGD  ClientHostString() for use in event logging
10-JUL-2005  MGD  ServerSignature() modifies signature if /SOFTWAREid= set
10-JUN-2005  MGD  refine SysDclAst() EXQUOTA reporting
20-JUL-2004  MGD  HTTP/1.1 compliance,
                  refine HttpIfModifiedSince(),
                  add HttpIfUnModifiedSince()
09-APR-2003  MGD  allow ParseNetMask() to accept IP address without a mask
10-JAN-2003  MGD  ServerSignature() use 'HttpdName' and 'HttpdVersion'
13-OCT-2001  MGD  move string functions to STRNG.C module
04-AUG-2001  MGD  support module WATCHing
17-MAY-2001  MGD  move ...Fao() functions to FAO.C
30-APR-2001  MGD  use permanent global section for monitor data
05-APR-2001  MGD  bugfix; ParseNetMask() VLSM mask octet ordering
21-FEB-2001  MGD  modify ParseNetMask() to allow VLSM (variable-length subnet
                  masking, e.g. '131.185.250.0/24')
02-FEB-2001  MGD  add !%I to FaolSAK()
17-JAN-2000  MGD  bugfix; HttpHeaderChallenge()
23-DEC-2000  MGD  GenerateUniqueId() substitute '@' for '_',
                  remove non-DECC strftime() kludge (VAXC no longer supported!)
01-OCT-2000  MGD  move privilege functions here
27-AUG-2000  MGD  add !SL to FaolSAK()
09-AUG-2000  MGD  bugfix; ParseQueryField() string length check
11-JUN-2000  MGD  add ParseNetMask()
08-APR-2000  MGD  add GenerateUniqueId() based on Apache implementation
04-MAR-2000  MGD  add FaolSAK() and it's siblings
                  FaoToBuffer(), FaolToBuffer(), FaoToNet(),
                  remove CopyTohtml(), UrlEncodeString(),
                  improved SearchTextString()
01-JAN-2000  MGD  support ODS-2 and ODS-5,
                  add formatted OPCOM messages
15-OCT-1999  MGD  use common 'HttpdProcess.Pid'
28-AUG-1999  MGD  add strzcpy(), GetSysInfo.VersionInteger()
25-MAY-1999  MGD  reverse the order of the digest and basic challenges
                  (MS IE5 will not authenticate if digest comes first!!)
31-MAR-1999  MGD  bugfix; HttpHeaderChallenge() 'AuthRealmPtr' NULL check
18-JAN-1999  MGD  FaoPrint() and FaoToStdout() for sys$fao()
                  variable-argument string printing,
                  provide proxy accounting logical and zeroing
01-OCT-1998  MGD  HttpHeader() "Content-Type: text/...; charset=...",
                  support SET mapping rules for charset and content-type,
                  UrlEncodeString(),
                  bugfix; NameOfDirectoryFile()
10-AUG-1998  MGD  refined CopyToHtml() slightly,
                  bugfix; TimeSetTimezone() unsigned to signed longs
10-JUL-1998  MGD  a little Y2K insurance
21-JUN-1998  MGD  changed format of request logical
02-APR-1998  MGD  after email about non-conforming date formats back to RFC1123
12-MAR-1998  MGD  added FormatProtection()
08-JAN-1997  MGD  TimeSetGmt() now can use SYS$TIMEZONE_DIFFERENTIAL
05-OCT-1997  MGD  additional list processing functions
28-SEP-1997  MGD  accounting structure has grown beyond 255 bytes
09-AUG-1997  MGD  message database, added SearchTextString() and
                  NameOfDirectoryFile() functions
12-JUL-1997  MGD  EnableSysPrv(), DisableSysPrv()
07-JUL-1997  MGD  attempt to reduce dynamic memory fragmentation within C RTL
                  using HEAP_MIN_CHUNK_SIZE functionality,
                   prevent request logical redefinition if keep-alive timeout 
16-JUN-1997  MGD  generic linked list functions
12-MAR-1997  MGD  HTTP header generation
01-FEB-1997  MGD  HTTPd version 4
01-OCT-1996  MGD  report menu
18-JUN-1996  MGD  bugfix; HTTPD$GMT conversion to VMS delta time TimeSetGmt()
06-APR-1996  MGD  persistent connections ("keep-alive")
01-DEC-1995  MGD  HTTPd version 3.0
27-SEP-1995  MGD  provide GMT functions
20-DEC-1994  MGD  initial development
*/
/*****************************************************************************/

#ifdef WASD_VMS_V7
#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000
#endif

/* standard C header files */
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* VMS related header files */
#include <descrip.h>
#include <jpidef.h>
#include <libdef.h>
#include <libicb.h>
#include <libdtdef.h>
#include <libclidef.h>
#include <lnmdef.h>
#include <opcdef.h>
#include <prvdef.h>
#include <psldef.h>
#include <rms.h>
#include <ssdef.h>
#include <stsdef.h>

/* lib$cvts_from_internal_time() does not exist with V7.3-2 or earlier */
#include <cvtdef.h>
#include <cvt$routines.h>
#define CVT$K_VAX_F 0
#define CVT$K_IEEE_S 4

/* application related header files */
#include "wasd.h"

#define WASD_MODULE "SUPPORT"

/******************/
/* global storage */
/******************/

#define RFC_1123_DATE yup

const int64  DeltaOneDay = DELTA64_ONE_DAY,
             DeltaOneHour = DELTA64_ONE_HR,
             Delta01Sec = DELTA64_ONE_SEC,
             Delta02Sec = DELTA64_TWO_SEC,
             Delta05Sec = DELTA64_FIVE_SEC,
             Delta10Sec = DELTA64_TEN_SEC,
             Delta30Sec = DELTA64_THIRTY_SEC,
             Delta60Sec = DELTA64_ONE_MIN,
             Delta001mSec = DELTA64_ONE_MSEC,
             Delta010mSec = DELTA64_TEN_MSEC,
             Delta100mSec = DELTA64_HUN_MSEC,
             Delta001uSec = DELTA64_ONE_USEC,
             Delta010uSec = DELTA64_TEN_USEC,
             Delta100uSec = DELTA64_HUN_USEC;

int64  TimeUtcDelta64;

BOOL  TimeAheadOfGmt;

char  TimeGmtString [16],
      TimeZoneString [16];

char  *DayName [] =
   { "", "Monday", "Tuesday", "Wednesday",
     "Thursday", "Friday", "Saturday", "Sunday" };

char  *MonthName [] =
   { "", "January", "February", "March", "April", "May", "June",
         "July", "August", "September", "October", "November", "December" };

/* these arrays are used with the TOUP() and TOLO() macros defined in WASD.H */

int  ToUpperCase [] =
{  0,  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, 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,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
};  

int  ToLowerCase [] =
{  0,  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, 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, 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
};  

/********************/
/* external storage */
/********************/

extern BOOL  AccountingZeroOnStartup,
             CliSoftwareID,
             MonitorEnabled;

extern int  CharsetCount,
            EfnWait,
            ServerPort;

extern ulong  SysPrvMask[];

extern char  ErrorSanityCheck[],
             HttpdName[],
             HttpdVersion[],
             ServerHostName[],
             SoftwareID[];

extern ACCOUNTING_STRUCT  *AccountingPtr;
extern CONFIG_STRUCT  Config;
extern HTTPD_GBLSEC  *HttpdGblSecPtr;
extern HTTPD_PROCESS  HttpdProcess;
extern MSG_STRUCT  Msgs;
extern WATCH_STRUCT  Watch;

/*****************************************************************************/
/*
Declare an AST, exit if there is any problem ... must have our ASTs!
*/ 

void SysDclAst
(
void *Address,
ulong Parameter
)
{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (0 && WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "SysDclAst() !&A(!&X)", Address, Parameter);

   status = sys$dclast (Address, Parameter, 0);
   if (VMSok (status)) return;

   if (status == SS$_EXQUOTA)
   {
      /* no ASTs means not much of anything else can happen so just exit! */
      sys$canexh(0);
      /* make the message a little more meaningful */
      sys$exit (SS$_EXASTLM);
   }
   else
      ErrorExitVmsStatus (status, "sys$dclast()", FI_LI);
}

/*****************************************************************************/
/*
Generic list handling function.  Add entry to head of list.
*/ 

LIST_ENTRY* ListAddHead
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr,
ulong DataType
)
{
   /*********/
   /* begin */
   /*********/

   if (!lptr->HeadPtr)
   {
      /* empty list */
      lptr->HeadPtr = lptr->TailPtr = eptr;
      eptr->PrevPtr = eptr->NextPtr = NULL;
      if (DataType) eptr->DataType = DataType;
      lptr->EntryCount++;
      return (eptr);
   }
   else
   {
      /* non-empty list */
      eptr->PrevPtr = NULL;
      eptr->NextPtr = lptr->HeadPtr;
      eptr->NextPtr->PrevPtr = lptr->HeadPtr = eptr;
      if (DataType) eptr->DataType = DataType;
      lptr->EntryCount++;
      return (eptr);
   }
}

/*****************************************************************************/
/*
Generic list handling function.  Add entry to tail of list.
*/ 

LIST_ENTRY* ListAddTail
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr,
ulong DataType
)
{
   /*********/
   /* begin */
   /*********/

   if (!lptr->HeadPtr)
   {
      /* empty list */
      lptr->HeadPtr = lptr->TailPtr = eptr;
      eptr->PrevPtr = eptr->NextPtr = NULL;
      if (DataType) eptr->DataType = DataType;
      lptr->EntryCount++;
      return (eptr);
   }
   else
   {
      /* non-empty list */
      eptr->NextPtr = NULL;
      eptr->PrevPtr = lptr->TailPtr;
      eptr->PrevPtr->NextPtr = lptr->TailPtr = eptr;
      if (DataType) eptr->DataType = DataType;
      lptr->EntryCount++;
      return (eptr);
   }
}

/*****************************************************************************/
/*
Generic list handling function.  Add entry 2 "in front of" entry 1.
*/ 

LIST_ENTRY* ListAddBefore
(
LIST_HEAD *lptr,
LIST_ENTRY *e1ptr,
LIST_ENTRY *e2ptr,
ulong DataType
)
{
   /*********/
   /* begin */
   /*********/

   if (lptr->HeadPtr == e1ptr)
   {
      /* at head of list */
      e1ptr->PrevPtr = e2ptr;
      e2ptr->PrevPtr = NULL;
      e2ptr->NextPtr = e1ptr;
      lptr->HeadPtr = e2ptr;
      if (DataType) e2ptr->DataType = DataType;
      lptr->EntryCount++;
      return (e2ptr);
   }
   else
   {
      /* not at head of list */
      if (e1ptr->PrevPtr)
      {
         e2ptr->PrevPtr = e1ptr->PrevPtr;
         e2ptr->PrevPtr->NextPtr = e2ptr;
      }
      e1ptr->PrevPtr = e2ptr;
      e2ptr->NextPtr = e1ptr;
      if (DataType) e2ptr->DataType = DataType;
      lptr->EntryCount++;
      return (e2ptr);
   }
}

/*****************************************************************************/
/*
Generic list handling function.  Remove entry from list.  Check first that
entry looks legitimate, return NULL if not!
*/ 

LIST_ENTRY* ListRemove
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr
)
{
   /*********/
   /* begin */
   /*********/

   if (!eptr->PrevPtr)
   {                                                                        
      /* at head of list */
      if (!eptr->NextPtr)
      {
         /* only entry in list */
         if (lptr->HeadPtr != eptr || lptr->TailPtr != eptr) return (NULL);
         lptr->HeadPtr = lptr->TailPtr = NULL;
         if (lptr->EntryCount) lptr->EntryCount--;
         return (eptr);
      }
      else
      {
         /* remove from head of list */
         if (lptr->HeadPtr != eptr) return (NULL);
         eptr->NextPtr->PrevPtr = NULL;
         lptr->HeadPtr = eptr->NextPtr;
         if (lptr->EntryCount) lptr->EntryCount--;
         return (eptr);
      }
   }
   else
   {
      /* not at head of list */
      if (!eptr->NextPtr)
      {
         /* at tail of list */
         if (lptr->TailPtr != eptr) return (NULL);
         eptr->PrevPtr->NextPtr = NULL;
         lptr->TailPtr = eptr->PrevPtr;
         if (lptr->EntryCount) lptr->EntryCount--;
         return (eptr);
      }
      else
      {
         /* somewhere in the middle! */
         if (eptr->PrevPtr->NextPtr != eptr ||
             eptr->NextPtr->PrevPtr != eptr) return (NULL);
         eptr->PrevPtr->NextPtr = eptr->NextPtr;
         eptr->NextPtr->PrevPtr = eptr->PrevPtr;
         if (lptr->EntryCount) lptr->EntryCount--;
         return (eptr);
      }
   }
}

/*****************************************************************************/
/*
Generic list handling function.  If not already there move it to the head.
*/ 

#ifdef __DECC
#pragma inline(ListMoveHead)
#endif

LIST_ENTRY* ListMoveHead
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr
)
{
   /*********/
   /* begin */
   /*********/

   /* move entry to the head of the cache list */
   if (lptr->HeadPtr != eptr)
   {
      ListRemove (lptr, eptr);
      ListAddHead (lptr, eptr, 0);
   }

   return (eptr);
}

/*****************************************************************************/
/*
Generic list handling function.  If not already there move it to the tail.
*/ 

#ifdef __DECC
#pragma inline(ListMoveTail)
#endif

LIST_ENTRY* ListMoveTail
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr
)
{
   /*********/
   /* begin */
   /*********/

   /* move entry to the head of the cache list */
   if (lptr->TailPtr != eptr)
   {
      ListRemove (lptr, eptr);
      ListAddTail (lptr, eptr, 0);
   }

   return (eptr);
}

/*****************************************************************************/
/*
Generic list handling function.  Remove the head entry.
*/ 

#ifdef __DECC
#pragma inline(ListRemoveHead)
#endif

LIST_ENTRY* ListRemoveHead (LIST_HEAD *lptr)

{
   LIST_ENTRY  *eptr;

   /*********/
   /* begin */
   /*********/

   if (eptr = lptr->HeadPtr) ListRemove (lptr, eptr);

   return (eptr);
}

/*****************************************************************************/
/*
Generic list handling function.  Remove the tail entry.
*/ 

#ifdef __DECC
#pragma inline(ListRemoveTail)
#endif

LIST_ENTRY* ListRemoveTail (LIST_HEAD *lptr)

{
   LIST_ENTRY  *eptr;

   /*********/
   /* begin */
   /*********/

   if (eptr = lptr->TailPtr) ListRemove (lptr, eptr);

   return (eptr);
}

/*****************************************************************************/
/*
Generic list handling function.  Check if a specific entry is in specific list.
*/ 

LIST_ENTRY* ListCheck
(
LIST_HEAD *lptr,
LIST_ENTRY *eptr
)
{
   LIST_ENTRY  *teptr;

   /*********/
   /* begin */
   /*********/

   fprintf (stdout, "ListCheck() %d %d %d %d\n",
            lptr, lptr->HeadPtr, lptr->TailPtr, lptr->EntryCount);

   for (teptr = lptr->HeadPtr; teptr; teptr = teptr->NextPtr)
      if (teptr == eptr) return (eptr);
   return (NULL);
}

/*****************************************************************************/
/*
list the entries in the list.
*/ 

ListDebug (LIST_HEAD* lptr)

{
   LIST_ENTRY  *eptr;

   /*********/
   /* begin */
   /*********/

   for (eptr = lptr->HeadPtr; eptr; eptr = eptr->NextPtr)
      fprintf (stdout, "%d <- %d -> %d\n", eptr->PrevPtr, eptr, eptr->NextPtr);
}

/*****************************************************************************/
/*
Generate the appropriate server signature returned the supplied buffer.
*/

char* ServerSignature
(
REQUEST_STRUCT *rqptr,
char *BufferPtr,
int BufferSize
)
{
   int  status,
        ServerPortNumber;
   char  *cptr, *sptr, *zptr,
         *ServerHostNamePtr;
   char  MsgBuffer [256],
         EmailBuffer [256];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ServerSignature()");

   if (!Config.cfServer.Signature)
   {
      BufferPtr[0] = '\0';
      return (BufferPtr);
   }

   if (rqptr)
   {
      ServerHostNamePtr = rqptr->ServicePtr->ServerHostName;
      ServerPortNumber = rqptr->ServicePtr->ServerPort;
   }
   else
   {
      ServerHostNamePtr = ServerHostName;
      ServerPortNumber = ServerPort;
   }

   if (Config.cfServer.Signature == CONFIG_SERVER_SIGNATURE_EMAIL &&
       Config.cfServer.AdminEmail[0])
   {
      status = FaoToBuffer (EmailBuffer, sizeof(EmailBuffer), NULL,
                            "<a href=\"mailto:!AZ\">!AZ</a>",
                            Config.cfServer.AdminEmail, ServerHostNamePtr);
      if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);
   }
   else
      EmailBuffer[0] = '\0';

   if (CliSoftwareID)
   {
      cptr = MsgFor(rqptr, MSG_STATUS_SIGNATURE);
      zptr = (sptr = MsgBuffer) + sizeof(MsgBuffer)-1;
      while (*cptr && *cptr != '!' && sptr < zptr) *sptr++ = *cptr++;
      if (strsame (cptr, "!AZ/", 4))
      {
         cptr += 4;
         while (*cptr && sptr < zptr) *sptr++ = *cptr++;
         *sptr = '\0';
         status = FaoToBuffer (BufferPtr, BufferSize, NULL, MsgBuffer,
                               SoftwareID,
                               EmailBuffer[0] ? EmailBuffer : ServerHostNamePtr,
                               ServerPortNumber);
         if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);
         return (BufferPtr);
      }
      ErrorNoticed (rqptr, SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
   }

   status = FaoToBuffer (BufferPtr, BufferSize, NULL,
                         MsgFor(rqptr, MSG_STATUS_SIGNATURE),
                         HttpdName, HttpdVersion,
                         EmailBuffer[0] ? EmailBuffer : ServerHostNamePtr,
                         ServerPortNumber);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);

   return (BufferPtr);
}
   
/*****************************************************************************/
/*
Create an HTTP format local time string in the storage pointed at by
'TimeString', e.g. "Fri, 25 Aug 1995 17:32:40" (RFC1123). If 'Time64Ptr' is
null the time string represents the current time. If it points to a quadword,
VMS time value the string represents that time. 'TimeString' must point to
storage large enough for 31 characters.
*/

int HttpLocalTimeString
(
char *TimeString,
int64 *Time64Ptr
)
{
   /* alternate formats for time string */
#ifdef RFC_1123_DATE
   /* day, dd mmm yyyy hh:mm */
   static $DESCRIPTOR (HttpTimeFaoDsc, "!3AZ, !2ZW !3AZ !4ZW !2ZW:!2ZW:!2ZW");
#else
   /* weekday, dd-mmm-yyyy hh:mm */
   static $DESCRIPTOR (HttpTimeFaoDsc, "!3AZ, !2ZW-!3AZ-!4ZW !2ZW:!2ZW:!2ZW");
#endif

   static $DESCRIPTOR (TimeStringDsc, "");

   int  status;
   int64  time64;
   ushort  Length;
   ushort  time7 [7];
   ulong  DayOfWeek;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "HttpLocalTimeString()");

   if (Time64Ptr)
      time64 = *(UINT64PTR)Time64Ptr;
   else
      sys$gettim (&time64);

   status = sys$numtim (&time7, &time64);

   if (VMSnok (status = lib$day_of_week (&time64, &DayOfWeek)))
      return (status);

   /* set the descriptor address and size of the resultant time string */
   TimeStringDsc.dsc$w_length = 25;
   TimeStringDsc.dsc$a_pointer = TimeString;

   status = sys$fao (&HttpTimeFaoDsc, &Length, &TimeStringDsc,
                     DayName[DayOfWeek], time7[2], MonthName[time7[1]],
                     time7[0], time7[3], time7[4], time7[5]);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      TimeString[0] = '\0';
   else
      TimeString[Length] = '\0';

   return (status);
}

/*****************************************************************************/
/*
Create an HTTP format Greenwich Mean Time (UTC) time string in the storage 
pointed at by 'TimeString', e.g. "Fri, 25 Aug 1995 17:32:40 GMT" (RFC1123). 
This must be at least 30 characters capacity.  If 'Time64Ptr' is NULL the 
time string represents the current time.  If it points to a quadword, VMS time 
value the string represents that time.
*/

int HttpGmTimeString
(
char *TimeString,
int64 *Time64Ptr
)
{
   /* alternate formats for time string */
#ifdef RFC_1123_DATE
   /* day, dd mmm yyyy hh:mm */
   static $DESCRIPTOR (HttpTimeFaoDsc,
          "!3AZ, !2ZW !3AZ !4ZW !2ZW:!2ZW:!2ZW GMT");
#else
   /* weekday, dd-mmm-yyyy hh:mm */
   static $DESCRIPTOR (HttpTimeFaoDsc,
          "!3AZ, !2ZW-!3AZ-!4ZW !2ZW:!2ZW:!2ZW GMT");
#endif

   static $DESCRIPTOR (TimeStringDsc, "");

   int  status;
   int64  time64;
   ushort  Length;
   ushort  time7 [7];
   ulong  DayOfWeek;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "HttpGmTimeString()");

   if (Time64Ptr)
      time64 = *(UINT64PTR)Time64Ptr;
   else
      sys$gettim (&time64);

   TimeAdjustToGMT (&time64);

   status = sys$numtim (&time7, &time64);

   if (VMSnok (status = lib$day_of_week (&time64, &DayOfWeek)))
      return (status);

   /* set the descriptor address and size of the resultant time string */
   TimeStringDsc.dsc$w_length = 29;
   TimeStringDsc.dsc$a_pointer = TimeString;

   status = sys$fao (&HttpTimeFaoDsc, &Length, &TimeStringDsc,
                     DayName[DayOfWeek], time7[2], MonthName[time7[1]],
                     time7[0], time7[3], time7[4], time7[5]);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      TimeString[0] = '\0';
   else
      TimeString[Length] = '\0';

   return (status);
}

/*****************************************************************************/
/*
Create an ISO 8601 format UTC (GMT) basic time string in the storage pointed at
by 'TimeString', e.g. "19950825T173240.34Z+0000", or separated local time
string, e.g. "1995-08-25T17:32:40.34Z+0930".  This must be at least 32 
characters capacity.  If 'Time64Ptr' is NULL the time string represents the
current time.  If it points to a quadword, VMS time value the string represents
that time.
*/

int HttpIsoTimeString
(
BOOL LocalTimeZone,
char *TimeString,
int64 *Time64Ptr
)
{
   static $DESCRIPTOR (UtcTimeFaoDsc,
          "!4ZL!2ZL!2ZLT!2ZL!2ZL!2ZL.!2ZL+0000");
   static $DESCRIPTOR (LocalTimeFaoDsc,
          "!4ZL-!2ZL-!2ZLT!2ZL:!2ZL:!2ZL.!2ZL!AZ");
   static $DESCRIPTOR (TimeStringDsc, "");

   int  status;
   int64  time64;
   ushort  Length;
   ushort  time7 [7];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "HttpIsoTimeString()");

   if (Time64Ptr)
      time64 = *(UINT64PTR)Time64Ptr;
   else
      sys$gettim (&time64);

   if (!LocalTimeZone) TimeAdjustToGMT (&time64);

   status = sys$numtim (&time7, &time64);

   /* set the descriptor address and size of the resultant time string */
   TimeStringDsc.dsc$w_length = 31;
   TimeStringDsc.dsc$a_pointer = TimeString;

   if (LocalTimeZone)
      status = sys$fao (&LocalTimeFaoDsc, &Length, &TimeStringDsc,
                        time7[0], time7[1], time7[2],
                        time7[3], time7[4], time7[5], time7[6],
                        TimeZoneString);
   else
      status = sys$fao (&UtcTimeFaoDsc, &Length, &TimeStringDsc,
                        time7[0], time7[1], time7[2],
                        time7[3], time7[4], time7[5], time7[6]);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      TimeString[0] = '\0';
   else
      TimeString[Length] = '\0';

   return (status);
}

/*****************************************************************************/
/*
Given a string such as "Fri, 25 Aug 1995 17:32:40 GMT" (RFC1123),
or "Friday, 25-Aug-1995 17:32:40 GMT" (RFC 1036),
or "1995-08-25T17:32:40.34Z+0000"" (ISO 8601),
create an internal, local, binary time.

For purposes such a if-modified-since.  The RFC specifies RFC1123 but some
recently use ISO 8601.  This function tries the 1123 format first and then the
8601.
*/ 

int HttpGetTime
(
char *TimeString,
int64 *Time64Ptr
)
{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (VMSnok (status = HttpGmTime (TimeString, Time64Ptr)))
      status = HttpIsoTime (TimeString, Time64Ptr);
   return (status);
}

/*****************************************************************************/
/*
Given a string such as "Fri, 25 Aug 1995 17:32:40 GMT" (RFC1123), or "Friday,
25-Aug-1995 17:32:40 GMT" (RFC 1036), create an internal, local, binary time
with the current GMT offset.  See complementary function HttpGmTimeString().
*/ 

int HttpGmTime
(
char *TimeString,
int64 *Time64Ptr
)
{
   static char  *MonthName [] =
      { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

   int  status;
   ushort  Length;
   ushort  time7 [7] = { 0,0,0,0,0,0,0 };
   ulong  DayOfWeek;
   char  *tptr;

   /*********/
   /* begin */
   /*********/

   tptr = TimeString;
   /* hunt straight for the comma after the weekday name! */
   while (*tptr && *tptr != ',') tptr++;
   if (*tptr) tptr++;
   /* span white space between weekday name and date */
   while (*tptr && ISLWS(*tptr)) tptr++;
   if (!*tptr) return (SS$_IVTIME);

   /* get the date and then skip to month name */
   if (isdigit(*tptr)) time7[2] = atoi (tptr);
   while (*tptr && isdigit(*tptr)) tptr++;
   while (*tptr && (*tptr == '-' || ISLWS(*tptr))) tptr++;
   if (!*tptr) return (SS$_IVTIME);

   /* get the month number from the name and skip to the year */
   for (time7[1] = 1; time7[1] <= 12; time7[1]++)
      if (strsame (tptr, MonthName[time7[1]], 3)) break;
   if (time7[1] > 12) return (SS$_IVTIME);
   while (isalpha(*tptr)) tptr++;
   while (*tptr && (*tptr == '-' || ISLWS(*tptr))) tptr++;
   if (!*tptr) return (SS$_IVTIME);

   /* get the year and then skip to the hour */
   if (isdigit(*tptr))
   {
      time7[0] = atoi (tptr);
      if (time7[0] <= 99)
      {
         /* for older browsers supplying 2 digit years, some Y2K insurance! */
         if (time7[0] >= 70)
            time7[0] += 1900;
         else
            time7[0] += 2000;
      }
   }
   while (*tptr && isdigit(*tptr)) tptr++;
   while (*tptr && ISLWS(*tptr)) tptr++;
   if (!*tptr) return (SS$_IVTIME);

   /* get the hour, minute and second */
   if (isdigit(*tptr)) time7[3] = atoi (tptr);
   while (*tptr && isdigit(*tptr)) tptr++;
   if (*tptr == ':') tptr++;
   if (isdigit(*tptr)) time7[4] = atoi (tptr);
   while (*tptr && isdigit(*tptr)) tptr++;
   if (*tptr == ':') tptr++;
   if (isdigit(*tptr)) time7[5] = atoi (tptr);
   while (*tptr && isdigit(*tptr)) tptr++;
   if (*tptr == ':') tptr++;
   if (!*tptr) return (SS$_IVTIME);

   /* the only thing remaining should be the "GMT" */
   while (*tptr && ISLWS(*tptr)) tptr++;
   if (!MATCH3 (tptr, "GMT")) return (SS$_IVTIME);

   /*******************************************/
   /* convert what looks like legitimate GMT! */
   /*******************************************/

   status = lib$cvt_vectim (&time7, Time64Ptr);
   if (VMSnok (status)) return (status);

   TimeAdjustFromGMT (Time64Ptr);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
Given an ISO 8601 format time string (e.g. "1995-08-25T17:32:40.34Z+0000")
create an internal, local, binary time.  See complementary function
HttpIsoTimeString().
*/ 

int HttpIsoTime
(
char *TimeString,
int64 *Time64Ptr
)
{
   int64  time64;
   int  year, month, day, hour, min, sec, frac, ohour, omin, status, zoffset;
   ushort  time7 [7];
   char  offset;
   char  *tptr;

   /*********/
   /* begin */
   /*********/

   *Time64Ptr = 0;
   frac = ohour = omin = offset = zoffset = 0;

   tptr = TimeString;

   /* yyyy mm dd */
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%4d", &year) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;
   if (*tptr == '-') tptr++;
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%2d", &month) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;
   if (*tptr == '-') tptr++;
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%2d", &day) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;

   /* hh mm ss */
   if (*tptr == 'T') tptr++;
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%2d", &hour) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;
   if (*tptr == ':') tptr++;
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%2d", &min) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;
   if (*tptr == ':') tptr++;
   if (!isdigit(*tptr)) return (SS$_IVTIME);
   if (sscanf (tptr, "%2d", &sec) != 1) return (SS$_IVTIME);
   while (isdigit(*tptr)) tptr++;

   /* fraction */
   if (*tptr == '.')
   {
      tptr++;
      if (!isdigit(*tptr)) return (SS$_IVTIME);
      if (sscanf (tptr, "%d", &frac) != 1) return (SS$_IVTIME);
      while (isdigit(*tptr)) tptr++;
   }

   /* zulu offset */
   if (*tptr == 'Z') tptr++;
   if (*tptr == '+')
      offset = *tptr++;
   else
   if (*tptr == '-')
      offset = *tptr++;
   if (isdigit(*tptr))
   {
      if (sscanf (tptr, "%2d", &ohour) != 1) return (SS$_IVTIME);
      if (isdigit(*tptr)) tptr++;
      if (isdigit(*tptr)) tptr++;
      if (*tptr == ':') tptr++;
      if (sscanf (tptr, "%2d", &omin) != 1) return (SS$_IVTIME);
      zoffset = (((ohour * 3600) + (omin * 60)) * Delta01Sec);
   }

   time7[0] = year;
   time7[1] = month;
   time7[2] = day;
   time7[3] = hour;
   time7[4] = min;
   time7[5] = sec;
   time7[6] = frac;
   status = lib$cvt_vectim (&time7, &time64);
   if (VMSnok (status)) return (status);

   if (offset == '+')
      time64 -= zoffset;
   else
   if (offset == '-')
      time64 += zoffset;

   *Time64Ptr = time64;

   return (status);
}

/*****************************************************************************/
/*
Translate the logical WASD_UTC, WASD_GMT or HTTPD$GMT (defined to be something
like "+10:30" or "-01:15") and convert it into a delta time and store in 
'TimeUtcDelta64'.  If these are not present use the UTC time structure.  And if
this doesn't make sense (for whatever reason) use SYS$TIMEZONE_DIFFERENTIAL.
*/

void TimeSetHttpdUTC ()

{
   int  hh, mm, offset, status;
   uint64  time128 [2];
   ushort  time13 [13];
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (!(cptr = SysTrnLnm (WASD_UTC)))
      if (!(cptr = SysTrnLnm (WASD_GMT)))
         cptr = SysTrnLnm (HTTPD$GMT);

   if (cptr)
   {
      sptr = cptr;
      if (*cptr == '+' || *cptr == '-') cptr++;
      hh = atoi(cptr);
      while (isdigit(*cptr)) cptr++;
      while (!isdigit(*cptr)) cptr++;
      mm = atoi(cptr);
      if (hh >= 0 && hh <= 23 &&  mm >= 0 && hh <= 59)
      {
         offset = hh * 60 + mm;
         if (offset >= 0 && offset <= 1440)
         {
            if (*sptr == '-') offset = -offset;
            TimeUtcDelta64 = (int64)offset * TIME64_ONE_MIN;
         }
         else
            cptr = NULL;
         if (!(*sptr == '+' || *sptr == '-')) sptr = "+";
      }
      else
         cptr = NULL;
   }

   if (!cptr)
   {
      /* use system UTC time */
      sys$getutc (&time128);
      sys$numutc (&time13, &time128);
      /* the timezone offset in minutes */
      offset = time13[12];
      TimeUtcDelta64 = (int64)offset * TIME64_ONE_MIN;
      if (!TimeUtcDelta64) TimeUtcDelta64 = DELTA64_TEN_MSEC;
      if (offset >= 0)
         sptr = "+";
      else
      {
         sptr = "-";
         offset = -offset;
      }
      hh = offset / 60;
      mm = offset % 60;
   }

   if (offset > 1440)
   {
      /* if doesn't make sense fall back to this hoary old approach */
      if (cptr = SysTrnLnm ("SYS$TIMEZONE_DIFFERENTIAL"))
      {
         offset = atoi(cptr) / 60;
         if (offset <= 1440 || offset >= -1440)
         {
            TimeUtcDelta64 = (int64)offset * TIME64_ONE_MIN;
            if (!TimeUtcDelta64) TimeUtcDelta64 = DELTA64_TEN_MSEC;
            if (offset >= 0)
               sptr = "+";
            else
            {
               sptr = "-";
               offset = -offset;
            }
            hh = offset / 60;
            mm = offset % 60;
         }
      }
   }

   sprintf (TimeGmtString, "%c%02.02d:%02.02d", *sptr, hh, mm);
   sprintf (TimeZoneString, "%c%02.02d%02.02d", *sptr, hh, mm);
}

/*****************************************************************************/
/*
Adjust pointed-to time to GMT from local.  /TimeUtcDelta64/ will either be
positive 64 bits (i.e. VMS binary time) if ahead of GMT or negative 64 bits if
behind (or zero if on GMT).  So to move to GMT the delta to GMT is subtracted
from the supplied time.
*/ 

void TimeAdjustToGMT (int64 *Time64Ptr)

{
   /*********/
   /* begin */
   /*********/

   *Time64Ptr = *Time64Ptr - TimeUtcDelta64;
}

/*****************************************************************************/
/*
Adjust pointed-to time from GMT to local.  Same as above only /TimeUtcDelta64/
is added to the supplied time. 
*/ 

void TimeAdjustFromGMT (int64 *Time64Ptr)

{
   /*********/
   /* begin */
   /*********/

   *Time64Ptr = *Time64Ptr + TimeUtcDelta64;
}

/*****************************************************************************/
/*
*/

char* DigitDayTime (int64 *Time64Ptr)

{
   static char  TimeString [16];
   static $DESCRIPTOR (TimeFaoDsc, "!2ZL !2ZL:!2ZL:!2ZL\0");
   static $DESCRIPTOR (TimeStringDsc, TimeString);

   int  status;
   int64  time64;
   ushort  time7 [7];

   /*********/
   /* begin */
   /*********/

   if (!Time64Ptr) sys$gettim (Time64Ptr = &time64);
   if (VMSnok (status = sys$numtim (&time7, Time64Ptr)))
      return ("*ERROR*");
   if (VMSnok (sys$fao (&TimeFaoDsc, 0, &TimeStringDsc,
               time7[2], time7[3], time7[4], time7[5])))
      return ("*ERROR*");
   return (TimeString);
}

/*****************************************************************************/
/*
Return a pointer to the name of the day of the week for the supplied binary
time.  If a NULL pointer to the binary time then return current day of week.
*/

char* DayOfWeekName (int64 *Time64Ptr)

{
   int  status;
   ulong  DayOfWeek;
   int64  time64;

   /*********/
   /* begin */
   /*********/

   if (!Time64Ptr) sys$gettim (Time64Ptr = &time64);
   if (VMSnok (status = lib$day_of_week (Time64Ptr, &DayOfWeek)))
      DayOfWeek = 0;
   return (DayName[DayOfWeek]);
}

/*****************************************************************************/
/*
HTTP/1.0 and HTTP/1.1 requests.  If the content length has changed (HTTP/1.0 de
facto standard), or if it has been modified since the specified date and time
then return /true/ indicating that the object has been modified.  If not
modified then create a 304 HTTP header and return /false/ to indicate the file
should not be sent.  With VMS' fractions of a second, add one second to the
specified 'since' time to ensure a reliable comparison.

Implements defacto HTTP/1.0 persistent connections.  Currently we only provide
"keep-alive" response if we're sending a file in binary mode and know its
precise length.  An accurate "Content-Length:" field is vital for the client's
correct reception of the transmitted data.  The only other time a "keep-alive"
response can be provided is HERE, where a 304 header is returned (it has a
content length of precisely zero!)
*/ 
 
BOOL HttpIfModifiedSince
(
REQUEST_STRUCT *rqptr,
int64 *RdtTime64Ptr,
int LastContentLength
)
{
   int64  time64;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "HttpIfModifiedSince() !UL !UL !%D",
                 LastContentLength, rqptr->rqHeader.IfModifiedSinceLength,
                 &rqptr->rqTime.IfModifiedSinceTime64);

   if (!rqptr->rqTime.IfModifiedSinceTime64) return (true);

   if (rqptr->rqHeader.HttpVersion == HTTP_VERSION_1_0 &&
       rqptr->rqHeader.IfModifiedSinceLength >= 0 &&
       LastContentLength >= 0 &&
       rqptr->rqHeader.IfModifiedSinceLength != LastContentLength)
      return (true);
 
   /* slide a second to help ensure a reliable result */
   time64 = *(INT64PTR)RdtTime64Ptr - TIME64_ONE_SEC;
   if (time64 < rqptr->rqTime.IfModifiedSinceTime64)
   {
      /* not modified (note the zero content length!) */
      ResponseHeader (rqptr, 304, NULL, 0, NULL, NULL);
      return (false);
   }

   return (true);
}

/*****************************************************************************/
/*
HTTP/1.1 if-not-modified-since request.  If it has NOT been modified since the
specified date and time then return /true/ indicating that the object has not
been modified.  If modified then create a 412 HTTP header (precondition failed)
and return /false/ to indicate the request should not be processed.  With VMS'
fractions of a second, add one second to the specified 'since' time to ensure a
reliable comparison.
*/ 
 
BOOL HttpIfUnModifiedSince
(
REQUEST_STRUCT *rqptr,
int64 *RdtTime64Ptr
)
{
   int64  time64;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "HttpIfUnModifiedSince() !%D",
                 &rqptr->rqTime.IfModifiedSinceTime64);

   if (!rqptr->rqTime.IfUnModifiedSinceTime64) return (true);

   /* slide a second to help ensure a reliable result */
   time64 = *(INT64PTR)RdtTime64Ptr + TIME64_ONE_SEC;
   if (time64 > rqptr->rqTime.IfUnModifiedSinceTime64)
   {
      /* modified, precondition failed (note the zero content length!) */
      ResponseHeader (rqptr, 412, NULL, 0, NULL, NULL);
      return (true);
   }

   /* if not modified! */
   return (false);
}

/*****************************************************************************/
/*
HTTP/1.1 if-range not modified since.
*/ 
 
void HttpIfRange
(
REQUEST_STRUCT *rqptr,
int64 *RdtTime64Ptr
)
{
   int64  time64;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "HttpIfRange() !%D", &rqptr->rqTime.IfRangeBeginTime64);

   if (!rqptr->rqTime.IfRangeBeginTime64) return;

   /* slide a second to help ensure a reliable result */
   time64 = *(INT64PTR)RdtTime64Ptr + TIME64_ONE_SEC;
   if (time64 > rqptr->rqTime.IfModifiedSinceTime64)
   {
      /* modified, cancel any range data */
      if (rqptr->rqHeader.RangeBytePtr) rqptr->rqHeader.RangeBytePtr->Total = 0;
   }
}

/*****************************************************************************/
/*
Return a reasonable HTTP status from the supplied VMS status.
*/
 
int VmsToHttpStatus (int VmsStatus) 

{
   int  HttpStatus;

   /*********/
   /* begin */
   /*********/

   if (VmsStatus == RMS$_DNF ||
       VmsStatus == RMS$_DEV ||
       VmsStatus == RMS$_FNF ||
       VmsStatus == RMS$_NMF ||
       VmsStatus == SS$_NOSUCHDEV ||
       VmsStatus == SS$_NOSUCHFILE)
      HttpStatus = 404;
   else
   if (VmsStatus == RMS$_SYN ||
       VmsStatus == RMS$_FNM ||
       VmsStatus == RMS$_TYP ||
       VmsStatus == RMS$_VER ||
       VmsStatus == RMS$_DIR ||
       VmsStatus == SS$_BADFILENAME ||
       VmsStatus == SS$_BADFILEVER ||
       VmsStatus == SS$_BADIRECTORY)
      HttpStatus = 403;
   else
   if (VmsStatus == RMS$_FLK)
      HttpStatus = 423;
   else
   if (VmsStatus == SS$_DIRNOTEMPTY ||
       VmsStatus == SS$_EXDISKQUOTA)
      HttpStatus = 409;
   else
   if (VmsStatus == RMS$_PRV ||
       VmsStatus == SS$_NOPRIV)
      HttpStatus = 403;
   else
   if (VmsStatus == SS$_CREATED)
      HttpStatus = 201;
   else
   if (VmsStatus & 1)
      HttpStatus = 200;
   else
      HttpStatus = 500;

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "VmsToHttpStatus() !&S !UL",
                 VmsStatus, HttpStatus);

   return (HttpStatus);
}
 
/*****************************************************************************/
/*
Return the a pointer to abbreviated meaning of the supplied HTTP status code.
These are typical of those included on the response header status line.
*/

char *HttpStatusCodeText (int StatusCode)

{
   /*********/
   /* begin */
   /*********/

   switch (StatusCode)
   {
      case 100 : return ("Continue"); /* (HTTP/1.1) */
      case 101 : return ("Switching Protocols"); /* (HTTP/1.1) */
      case 102 : return ("Processing"); /* (WebDAV - RFC2581) */
      case 200 : return ("OK");
      case 201 : return ("Created");
      case 202 : return ("Accepted");
      case 203 : return ("Non-authoritative"); /* (HTTP/1.1) */
      case 204 : return ("No Content");
      case 205 : return ("Reset Content"); /* (HTTP/1.1) */
      case 206 : return ("Partial Content"); /* (HTTP/1.1) */
      case 207 : return ("Multistatus"); /* (WebDAV - RFC2581) */
      case 300 : return ("Multiple Choices"); /* (HTTP/1.1) */
      case 301 : return ("Moved Permanently");
      case 302 : return ("Found (Moved Temporarily)");
      case 303 : return ("See Other"); /* (HTTP/1.1) */
      case 304 : return ("Not Modified");
      case 305 : return ("Use Proxy"); /* (HTTP/1.1) */
      case 306 : return ("[reserved]");
      case 307 : return ("Temporary Redirect"); /* (HTTP/1.1) */
      case 400 : return ("Bad Request");
      case 401 : return ("Authorization Required");
      case 402 : return ("Payment Required"); /* (HTTP/1.1) */
      case 403 : return ("Forbidden");
      case 404 : return ("Not Found");
      case 405 : return ("Method Not Allowed"); /* (HTTP/1.1) */
      case 406 : return ("Not Acceptable"); /* (HTTP/1.1) */
      case 407 : return ("Proxy Authentication Required"); /* (HTTP/1.1) */
      case 408 : return ("Request Timeout"); /* (HTTP/1.1) */
      case 409 : return ("Conflict"); /* (HTTP/1.1) */
      case 410 : return ("Gone"); /* (HTTP/1.1) */
      case 411 : return ("Length Required"); /* (HTTP/1.1) */
      case 412 : return ("Precondition Failed"); /* (HTTP/1.1) */
      case 413 : return ("Request Entity Too Large"); /* (HTTP/1.1) */
      case 414 : return ("Request URI Too Long"); /* (HTTP/1.1) */
      case 415 : return ("Unsupported Media Type"); /* (HTTP/1.1) */
      case 416 : return ("Requested Range Not Satisfiable"); /* (HTTP/1.1) */
      case 417 : return ("Expectation Failed"); /* (HTTP/1.1) */
      case 418 : return ("I'm a teapot"); /* (RFC2324) */
      case 421 : return ("Misdirected Request"); /* (HTTP/2 - RFC7540) */
      case 422 : return ("Unprocessable Entity"); /* (WebDAV - RFC2581) */
      case 423 : return ("Locked"); /* (WebDAV - RFC2581) */
      case 424 : return ("Failed Dependency"); /* (WebDAV - RFC2581) */
      case 426 : return ("Upgrade Required"); /* (RFC2847) */
      case 500 : return ("Internal Error");
      case 501 : return ("Not Implemented");
      case 502 : return ("Bad Gateway");
      case 503 : return ("Service Unavailable");
      case 504 : return ("Gateway Timeout"); /* (HTTP/1.1) */
      case 505 : return ("HTTP Version Not Supported"); /* (HTTP/1.1) */
      case 507 : return ("Insufficient Storage"); /* (WebDAV - RFC2581) */
      default :  return ("Unknown Code!");
   }
}

/*****************************************************************************/
/*
Return the a pointer an explanation of the supplied HTTP status code.  These
are supplied from the message database can be are used in error messages, etc.
*/

char *HttpStatusCodeExplanation
(
REQUEST_STRUCT *rqptr,
int StatusCode
)
{
   /*********/
   /* begin */
   /*********/

   switch (StatusCode)
   {
      /* continue (HTTP/1.1) */
      case 100 : return (MsgFor(rqptr,MSG_HTTP_100));
      /* switching protocols (HTTP/1.1) */
      case 101 : return (MsgFor(rqptr,MSG_HTTP_101));
      /* processing (WedDAV - RFC2518) */ 
      case 102 : return ("processing");
      /* success */
      case 200 : return (MsgFor(rqptr,MSG_HTTP_200));
      /* created */
      case 201 : return (MsgFor(rqptr,MSG_HTTP_201));
      /* accepted */
      case 202 : return (MsgFor(rqptr,MSG_HTTP_202));
      /* non-authoritative (HTTP/1.1) */
      case 203 : return (MsgFor(rqptr,MSG_HTTP_203));
      /* no content */
      case 204 : return (MsgFor(rqptr,MSG_HTTP_204));
      /* reset content (HTTP/1.1) */
      case 205 : return (MsgFor(rqptr,MSG_HTTP_205));
      /* partial content (HTTP/1.1) */
      case 206 : return (MsgFor(rqptr,MSG_HTTP_206));
      /* multi-status (WedDAV - RFC2518) */ 
      case 207 : return ("multi-status");
      /* multiple choices (HTTP/1.1) */
      case 300 : return (MsgFor(rqptr,MSG_HTTP_300));
      /* moved permanently */
      case 301 : return (MsgFor(rqptr,MSG_HTTP_301));
      /* moved temporarily */
      case 302 : return (MsgFor(rqptr,MSG_HTTP_302));
      /* see other (HTTP/1.1)  */
      case 303 : return (MsgFor(rqptr,MSG_HTTP_303));
      /* not modified */
      case 304 : return (MsgFor(rqptr,MSG_HTTP_304));
      /* use proxy (HTTP/1.1)  */
      case 305 : return (MsgFor(rqptr,MSG_HTTP_305));
      /* reserved (WedDAV - RFC2518) */ 
      case 306 : return ("reserved");
      /* temporary redirect (WedDAV - RFC2518) */ 
      case 307 : return ("temporary redirect");
      /* bad request */
      case 400 : return (MsgFor(rqptr,MSG_HTTP_400));
      /* authorization required */
      case 401 : return (MsgFor(rqptr,MSG_HTTP_401));
      /* payment required */
      case 402 : return (MsgFor(rqptr,MSG_HTTP_402));
      /* forbidden */
      case 403 : return (MsgFor(rqptr,MSG_HTTP_403));
      /* not found */
      case 404 : return (MsgFor(rqptr,MSG_HTTP_404));
      /* method not allowed (HTTP/1.1) */
      case 405 : return (MsgFor(rqptr,MSG_HTTP_405));
      /* not acceptable (HTTP/1.1) */
      case 406 : return (MsgFor(rqptr,MSG_HTTP_406));
      /* proxy authentication required (HTTP/1.1) */
      case 407 : return (MsgFor(rqptr,MSG_HTTP_407));
      /* request timeout (HTTP/1.1) */
      case 408 : return (MsgFor(rqptr,MSG_HTTP_408));
      /* resource conflict (HTTP/1.1) */
      case 409 : return (MsgFor(rqptr,MSG_HTTP_409));
      /* gone (HTTP/1.1) */
      case 410 : return (MsgFor(rqptr,MSG_HTTP_410));
      /* length required (HTTP/1.1) */
      case 411 : return (MsgFor(rqptr,MSG_HTTP_411));
      /* precondition failed (HTTP/1.1) */
      case 412 : return (MsgFor(rqptr,MSG_HTTP_412));
      /* request entity too large (HTTP/1.1) */
      case 413 : return (MsgFor(rqptr,MSG_HTTP_413));
      /* request-URI too long (HTTP/1.1) */
      case 414 : return (MsgFor(rqptr,MSG_HTTP_414));
      /* unsupported media type (HTTP/1.1) */
      case 415 : return (MsgFor(rqptr,MSG_HTTP_415));
      /*  requested range not satisfiable (HTTP/1.1) */
      case 416 : return (MsgFor(rqptr,MSG_HTTP_416));
      /*  expectation failed (HTTP/1.1) */
      case 417 : return (MsgFor(rqptr,MSG_HTTP_417));
      /* misdirected request (HTTP/2 - RFC7540) */
      case 421 : return ("misdirected request");
      /* unprocessable entity (WedDAV - RFC2518) */ 
      case 422 : return ("unprocessable entity");
      /* locked (WedDAV - RFC2518) */ 
      case 423 : return ("locked");
      /* internal error */
      case 500 : return (MsgFor(rqptr,MSG_HTTP_500));
      /* not implemented */
      case 501 : return (MsgFor(rqptr,MSG_HTTP_501));
      /* bad gateway */
      case 502 : return (MsgFor(rqptr,MSG_HTTP_502));
      /* service unavailable */
      case 503 : return (MsgFor(rqptr,MSG_HTTP_503));
      /* gateway timeout (HTTP/1.1) */
      case 504 : return (MsgFor(rqptr,MSG_HTTP_504));
      /* HTTP version not supported (HTTP/1.1) */
      case 505 : return (MsgFor(rqptr,MSG_HTTP_505));
      /* unknown code! */
      default :
         return ("The server is reporting an UNKNOWN status code!");
   }
}

/*****************************************************************************/
/*
Returns a pointer to a VmGetHeap()ed string of <meta ...> tags for an
internally generated HTML document.
*/

char* HtmlMetaInfo
(
REQUEST_STRUCT *rqptr,
char *VmsInfoPtr
)
{
   int  status;
   ulong  FaoVector [16];
   ulong  *vecptr;
   ushort  Length;
   char  *MetaPtr;
   char  Buffer [2048];

   /*********/
   /* begin */
   /*********/

   vecptr = &FaoVector;
   *vecptr++ = SoftwareID;
   *vecptr++ = rqptr->rqTime.GmDateTime;

   if (rqptr->RemoteUser[0])
   {
      *vecptr++ = "<meta name=\"author\" content=\"!&;AZ.\'!&;AZ\'@!AZ\">\n";
      *vecptr++ = rqptr->RemoteUser;
      *vecptr++ = rqptr->rqAuth.RealmDescrPtr;
      *vecptr++ = rqptr->ServicePtr->ServerHostPort;
   }
   else
   {
      *vecptr++ = "<meta name=\"host\" content=\"!AZ\">\n";
      *vecptr++ = rqptr->ServicePtr->ServerHostPort;
   }

   if (VmsInfoPtr && VmsInfoPtr[0])
   {
      if (Config.cfReport.MetaInfoEnabled)
      {
         *vecptr++ = "<meta name=\"VMS\" content=\"!&;AZ\">\n!&@";
         *vecptr++ = VmsInfoPtr;
         if (rqptr->PathOds)
         {
            *vecptr++ = "<meta name=\"ODS\" content=\"!AZ\">\n";
            switch (rqptr->PathOds)
            {
               case MAPURL_PATH_ODS_2 : *vecptr++ = "2"; break;
               case MAPURL_PATH_ODS_5 : *vecptr++ = "5"; break;
               case MAPURL_PATH_ODS_ADS : *vecptr++ = "ADS"; break;
               case MAPURL_PATH_ODS_PWK : *vecptr++ = "PWK"; break;
               case MAPURL_PATH_ODS_SMB : *vecptr++ = "SMB"; break;
               case MAPURL_PATH_ODS_SRI : *vecptr++ = "SRI"; break;
               default : *vecptr++ = "?";
            }
         }
         else
            *vecptr++ = "";
      }
      else
         *vecptr++ = "";
   }
   else
      *vecptr++ = "";

   status = FaolToBuffer (Buffer, sizeof(Buffer), &Length,
"<meta name=\"generator\" content=\"!AZ\">\n\
<meta name=\"date\" content=\"!AZ\">\n\
!&@\
!&@\
<meta name=\"format-detection\" content=\"telephone=no\">\n",
      &FaoVector);

   if (VMSnok (status) || status == SS$_BUFFEROVF)
   {
      ErrorNoticed (rqptr, status, NULL, FI_LI);
      return ("<!-- META overflow ERROR -->\n");
   }

   MetaPtr = VmGetHeap (rqptr, Length+1);
   memcpy (MetaPtr, Buffer, Length+1);

   return (MetaPtr);
}

/*****************************************************************************/
/*
Return a string containing a duration string.
Durations are in standard (VMS) 100nS units.
If less than one minute provide as 'seconds.micro[milli]seconds'.
If a minute or greater as '[ddd-]hh.mm.ss'.
*/

char* DurationString
(
REQUEST_STRUCT *rqptr,
int64 *Time64Ptr
)
{
   static int  bidx;
   static buf [4][32];
   static $DESCRIPTOR (DateFaoDsc, "!13%D\0");
   static $DESCRIPTOR (TimeFaoDsc, "!8%T\0");
   static $DESCRIPTOR (TimeDsc, "");

   int64  time64;
   double  dsecs;
   char  *sptr;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "DurationString() !&SQ !%T",
                 Time64Ptr, Time64Ptr);

   if (rqptr)
      sptr = VmGetHeap (rqptr, 32);
   else
   {
      sptr = buf[bidx++];
      if (bidx >= 4) bidx = 0;
   }

   time64 = *Time64Ptr;
   if (!time64)
      sptr = "0.0s";
   else
   if (time64 >= DELTA64_ONE_SEC)
   {
      /* less than one second, give in milliseconds */
      dsecs = (double)time64;
      if (time64 > DELTA64_TEN_MSEC)
         sprintf (sptr, "%.3fms", dsecs / (double)DELTA64_ONE_MSEC);
      else
      if (time64 > DELTA64_HUN_MSEC)
         sprintf (sptr, "%.1fms", dsecs / (double)DELTA64_ONE_MSEC);
      else
         sprintf (sptr, "%.0fms", dsecs / (double)DELTA64_ONE_MSEC);
   }
   else
   if (time64 >= DELTA64_ONE_MIN)
   {
      /* less than one minute, give in seconds and fractions */
      dsecs = (double)time64;
      if (time64 > DELTA64_TEN_SEC)
         sprintf (sptr, "%.3fs", dsecs / (double)DELTA64_ONE_SEC);
      else
         sprintf (sptr, "%.2fs", dsecs / (double)DELTA64_ONE_SEC);
   }
   else
   if (time64 >= DELTA64_ONE_DAY)
   {
      /* less than twenty-four hours */
      TimeDsc.dsc$a_pointer = sptr;
      TimeDsc.dsc$w_length = 32;
      sys$fao (&TimeFaoDsc, 0, &TimeDsc, Time64Ptr);
   }
   else
   {
      /* greater than twenty-four hours */
      TimeDsc.dsc$a_pointer = sptr;
      TimeDsc.dsc$w_length = 32;
      sys$fao (&DateFaoDsc, 0, &TimeDsc, Time64Ptr);
      sptr[4] = 160;  /* ISO8859-1 non-breaking space */
      while (*sptr == ' ') sptr++;
   }

   return (sptr);
}

/*****************************************************************************/
/*
Return an allocated string containing the average duration.
*/

char* AverageDurationString
(
REQUEST_STRUCT *rqptr,
int64 *Time64Ptr,
ulong Count
)
{
   int  status;
   int64  time64;
   float  fcount;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "AverageDurationString() !&X !&X !%T",
                 Time64Ptr[0], Time64Ptr[1], Time64Ptr);

   time64 = 0;
   if (Count)
   {
      time64 = *(INT64PTR)Time64Ptr;
      fcount = (float)Count;
      fcount = 1.0 / fcount;

      /* could use lib$mults_delta_time() only it doesn't exist on V7.3 :-( */
      status = cvt$convert_float (&fcount, CVT$K_IEEE_S,
                                  &fcount, CVT$K_VAX_F, 0);
      if (VMSnok (status))
      {
         ErrorNoticed (rqptr, status, NULL, FI_LI);
         time64 = 0;
      }

      status = lib$multf_delta_time (&fcount, &time64);
      if (VMSnok (status))
      {
         ErrorNoticed (rqptr, status, NULL, FI_LI);
         time64 = 0;
      }
   }
   return (DurationString (rqptr, &time64));
}

/*****************************************************************************/
/*
Format a string containing how many seconds, minutes, hours, days ago from the
current time.  For example; "15s", "10m", "17h", "152d".  Return VMS status.
|AgoBuf| should be at least 16 chars.
*/

void ThisLongAgo
(
int64 *Time64Ptr,
char *AgoBuf
)
{
   static ulong  LibDeltaSecs = LIB$K_DELTA_SECONDS;
   static $DESCRIPTOR (AgoFaoDsc, "!UL!AZ\0");
   static $DESCRIPTOR (AgoBufDsc, "");

   int  status;
   ulong  SecsAgo;
   int64  NowTime64;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ThisLongAgo()");

   *(USHORTPTR)AgoBuf = '0\0';

   if (!*Time64Ptr) return;

   sys$gettim (&NowTime64);
   SecsAgo = (NowTime64 - *Time64Ptr) / TIME64_ONE_SEC;

   AgoBufDsc.dsc$a_pointer = AgoBuf;
   AgoBufDsc.dsc$w_length = 16;
 
   status = SS$_NORMAL;

   if (SecsAgo >= (60*60*24))
      status = sys$fao (&AgoFaoDsc, 0, &AgoBufDsc, SecsAgo / (60*60*24), "d");
   else
   if (SecsAgo >= (60*60))
      status = sys$fao (&AgoFaoDsc, 0, &AgoBufDsc,  SecsAgo / (60*60), "h");
   else
   if (SecsAgo >= 60)
      status = sys$fao (&AgoFaoDsc, 0, &AgoBufDsc, SecsAgo / 60, "m");
   else
   if (SecsAgo > 1)
      status = sys$fao (&AgoFaoDsc, 0, &AgoBufDsc, SecsAgo, "s");
   else
      strcpy (AgoBuf, "now");
}

/*****************************************************************************/
/*
Put into the supplied buffer the time with the year removed (e.g. 31-OCT
23:01:15").  The buffer should have at least 16 bytes capacity.
*/

void TimeSansYear
(
ulong *Time64Ptr,
char *Buffer
)
{
   static $DESCRIPTOR (TimeFaoDsc, "!20%D\0");
   static char  TimeBuf [32];
   static $DESCRIPTOR (TimeBufDsc, TimeBuf);

   int  status;
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ThisLongAgo() !8XL", Time64Ptr);

   if (!*(UINT64PTR)Time64Ptr)
   {
      strcpy (Buffer, "(none)");
      return;
   }

   Buffer[0] = '\0';
   status = sys$fao (&TimeFaoDsc, 0, &TimeBufDsc, Time64Ptr);
   if (VMSnok (status)) return;
   sptr = Buffer;
   for (cptr = TimeBuf; *cptr && *cptr != '-'; *sptr++ = *cptr++);
   for (*sptr++ = *cptr++; *cptr && *cptr != '-'; *sptr++ = *cptr++);
   while (*cptr && *cptr != ' ') cptr++;
   while (*cptr) *sptr++ = *cptr++;
   *sptr = '\0';
}

/*****************************************************************************/
/*
Convert a VMS quadword, binary time to a Unix-style time component structure.
*/

BOOL TimeVmsToUnix
(
int64 *Time64Ptr,
struct tm *TmPtr
)
{
   static ulong  LibDayOfWeek = LIB$K_DAY_OF_WEEK;
   static ulong  LibDayOfYear = LIB$K_DAY_OF_YEAR;

   int  status;
   ulong  DayOfWeek,
          DayOfYear;
   ushort  time7 [7];
   int64  time64;

   /*********/
   /* begin */
   /*********/

   if (!Time64Ptr) sys$gettim (Time64Ptr = &time64);

   if (VMSnok (status = sys$numtim (&time7, Time64Ptr)))
      return (status);

   lib$cvt_from_internal_time (&LibDayOfWeek, &DayOfWeek, Time64Ptr);
   lib$cvt_from_internal_time (&LibDayOfYear, &DayOfYear, Time64Ptr);

   TmPtr->tm_sec = time7[5];
   TmPtr->tm_min = time7[4];
   TmPtr->tm_hour = time7[3];
   TmPtr->tm_mday = time7[2];
   TmPtr->tm_mon = time7[1] - 1;
   TmPtr->tm_year = time7[0] - 1900;
   TmPtr->tm_wday = DayOfWeek;
   TmPtr->tm_yday = DayOfYear;
   TmPtr->tm_isdst = 0;

   return (SS$_NORMAL);
}

/****************************************************************************/
/*
generate a standard VMS protection string from the mask. 'String' must be at
least 28 bytes (better use 32 :^)  Returns the length of the string.
*/ 
 
int FormatProtection
(
ushort pmask,
char *String
)
{
   char  *sptr;

   /*********/
   /* begin */
   /*********/

   sptr = String;

   if (!(pmask & 0x0001)) *sptr++ = 'R';
   if (!(pmask & 0x0002)) *sptr++ = 'W';
   if (!(pmask & 0x0004)) *sptr++ = 'E';
   if (!(pmask & 0x0008)) *sptr++ = 'D';
   *sptr++ = ',';
   if (!(pmask & 0x0010)) *sptr++ = 'R';
   if (!(pmask & 0x0020)) *sptr++ = 'W';
   if (!(pmask & 0x0040)) *sptr++ = 'E';
   if (!(pmask & 0x0080)) *sptr++ = 'D';
   *sptr++ = ',';
   if (!(pmask & 0x0100)) *sptr++ = 'R';
   if (!(pmask & 0x0200)) *sptr++ = 'W';
   if (!(pmask & 0x0400)) *sptr++ = 'E';
   if (!(pmask & 0x0800)) *sptr++ = 'D';
   *sptr++ = ',';
   if (!(pmask & 0x1000)) *sptr++ = 'R';
   if (!(pmask & 0x2000)) *sptr++ = 'W';
   if (!(pmask & 0x4000)) *sptr++ = 'E';
   if (!(pmask & 0x8000)) *sptr++ = 'D';

   *sptr = '\0';
   return (sptr - String);
}
 
/*****************************************************************************/
/*
Just return the process' current BYTLM quota.
*/ 

GetJpiBytLm ()

{
   static ulong  Pid = 0;

   static ulong  JpiBytLm;
   static VMS_ITEM_LIST3  JpiItems [] =
   {
      { sizeof(JpiBytLm), JPI$_BYTLM, &JpiBytLm, 0 },
      {0,0,0,0}
   };

   int  status;
   IO_SB  IOsb;

   /*********/
   /* begin */
   /*********/

   status = sys$getjpiw (EfnWait, &Pid, 0, &JpiItems, &IOsb, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSok (status)) return (JpiBytLm);
   ErrorExitVmsStatus (status, NULL, FI_LI);
}

/*****************************************************************************/
/*
Returns a pointer to a dynamic string containing the client host details as
"name (address)" or just "address".
*/

char* ClientHostString (REQUEST_STRUCT *rqptr)

{
   static $DESCRIPTOR (AddressFaoDsc, "!AZ\0");
   static $DESCRIPTOR (NameAddressFaoDsc, "!AZ (!AZ)\0");

   ushort  Length;
   char  *cptr;
   char  Buffer [256];
   $DESCRIPTOR (BufferDsc, Buffer);
   
   /*********/
   /* begin */
   /*********/

   if (!strcmp (rqptr->ClientPtr->Lookup.HostName,
                &rqptr->ClientPtr->IpAddressString))
      sys$fao (&AddressFaoDsc, &Length, &BufferDsc, 
               &rqptr->ClientPtr->IpAddressString);
   else
      sys$fao (&NameAddressFaoDsc, &Length, &BufferDsc, 
               rqptr->ClientPtr->Lookup.HostName,
               &rqptr->ClientPtr->IpAddressString);

   cptr = VmGetHeap (rqptr, Length);
   strcpy (cptr, Buffer);
   return (cptr);
}

/*****************************************************************************/
/*
*/

char* UserAtClient (REQUEST_STRUCT *rqptr)

{
   char  *cptr, *sptr, *zptr;
   char  StringBuffer [256];

   /*********/
   /* begin */
   /*********/

   if (!rqptr->RemoteUser[0]) return (rqptr->ClientPtr->Lookup.HostName);

   zptr = (sptr = StringBuffer) + sizeof(StringBuffer)-1;
   for (cptr = rqptr->RemoteUser; *cptr; cptr++)
      if (!isalnum(*cptr) && *cptr != '-' && *cptr != '_') break;
   if (*cptr) *sptr++ = '\"';
   for (cptr = rqptr->RemoteUser;
        *cptr && sptr < zptr;
        *sptr++ = *cptr++);
   if (StringBuffer[0] == '\"' && sptr < zptr) *sptr++ = '\"';
   if (rqptr->rqAuth.RealmPtr)
   {
      if (sptr < zptr) *sptr++ = '.';
      for (cptr = rqptr->rqAuth.RealmPtr;
           *cptr && sptr < zptr;
           *sptr++ = *cptr++);
   }
   if (sptr < zptr) *sptr++ = '@';
   for (cptr = rqptr->ClientPtr->Lookup.HostName;
        *cptr && sptr < zptr;
        *sptr++ = *cptr++);
   *sptr = '\0';

   cptr = VmGetHeap (rqptr, sptr-StringBuffer);
   strcpy (cptr, StringBuffer);
   return (cptr);
}

/****************************************************************************/
/*
Given a buffer of UTF-8 convert in-situ to 8 bit ASCII.  Ignore non- 8 bit
ASCII characters.  End-of-string is indicated by text-length not a
null-character, however the resultant string is nulled. If supplied the 8 bit
character 'SubsChar' is substituted for any non 8 bit code in the string.  
Return the number of converted characters.  Return -1 if there is an error. 
The string is mangled if an error.
*/

int ConvertFromUtf8
(
char *UtfPtr,
int UtfCount,
char SubsChar
)
{
   uchar  ch;
   uchar  *cptr, *sptr, *zptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "ConvertFromUtf8() !&C !#H", SubsChar, UtfCount, UtfPtr);

   if (UtfCount == -1) UtfCount = strlen(UtfPtr);

   if (UtfCount < 0) return (-1);

    /* is there a potentially UTF-8 bit pattern here? */
    for (zptr = (cptr = (uchar*)UtfPtr) + UtfCount; cptr < zptr; cptr++)
      if ((*cptr & 0xc0) == 0xc0) break;

   /* return if no UTF-8 conversion necessary (i.e. all 7 bit characters) */
   if (cptr >= zptr) return (UtfCount);
   if (*cptr == 0xff) return (cptr - (uchar*)UtfPtr);

   sptr = cptr;
   while (cptr < zptr)
   {
      if ((*cptr & 0xf8) == 0xf0)
      {
         /* four byte sequence */
         if (++cptr >= zptr) goto utf8_nbg;
         if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
         if (++cptr >= zptr) goto utf8_nbg;
         if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
         if (++cptr >= zptr) goto utf8_nbg;
         if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
         if (SubsChar) *sptr++ = SubsChar;
      }
      else
      if ((*cptr & 0xf0) == 0xe0)
      {
         /* three byte sequence */
         if (++cptr >= zptr) goto utf8_nbg;
         if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
         if (++cptr >= zptr) goto utf8_nbg;
         if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
         if (SubsChar) *sptr++ = SubsChar;
      }
      else
      if ((*cptr & 0xe0) == 0xc0)
      {
         /* two byte sequence */
         if (*cptr & 0x1c)
         {
            /* out-of-range character */
            if (++cptr >= zptr) goto utf8_nbg;
            if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
            if (++cptr >= zptr) goto utf8_nbg;
            if (SubsChar) *sptr++ = SubsChar;
         }
         else
         {
            /* 8 bit ASCII 128 to 255 */
            ch = (*cptr & 0x03) << 6;
            if (++cptr >= zptr) goto utf8_nbg;
            if ((*cptr & 0xc0) != 0x80) goto utf8_nbg;
            ch |= *cptr & 0x3f;
            *sptr++ = ch;
            cptr++;
         }
      }
      else
      {
         /* 8 bit ASCII 0 to 127 */
         *sptr++ = *cptr++;
      }
   }
   *sptr = '\0';

   return (sptr - (uchar*)UtfPtr);

   utf8_nbg:
      return (-1);
}

/****************************************************************************/
/*
Given a buffer of 8 bit ASCII text convert it in-situ to UTF-8.  Worst-case the
buffer space needs to be two times in size (i.e. every character has the hi bit
set requiring a leading UTF-8 byte).  End-of-string is indicated by text-length
not a null-character, however the resultant string is nulled.  Return the
length of the converted string.  Return -1 if the buffer space will be too
small.
*/

int ConvertToUtf8
(
char *TextPtr,
int TextLength,
int SizeOfText
)
{
   int  HiBitCount = 0;
   char  *cptr, *sptr, *zptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "ConvertFromUtf8() !!UL !#H", SizeOfText, TextLength, TextPtr);

   if (TextLength == -1) TextLength = strlen(TextPtr);

   for (zptr = (cptr = TextPtr) + TextLength; cptr < zptr; cptr++)
      if (*cptr & 0x80) HiBitCount++;

   if (!HiBitCount) return (TextLength);

   if (TextLength + HiBitCount >= SizeOfText - 1) return (-1);

   sptr = (cptr = (zptr = TextPtr) + TextLength - 1) + HiBitCount + 1;
   *sptr-- = '\0';
   while (cptr >= zptr)
   {
      if (*cptr & 0x80)
      {
         *sptr-- = (*cptr & 0x3f) | 0x80;
         *sptr-- = ((*cptr-- & 0xc0) >> 6) | 0xc0;
      }
      else
         *sptr-- = *cptr--;
   }

   return (TextLength + HiBitCount);
}

/****************************************************************************/
/*
For UTF-8 encoded file names hex-escape characters that cause issues.  This is
intended to feed into RMS services such as $PARSE.  Return zero if the original
string can be used.  Return minus one if the buffer overflowed.  Return the
number of characters in the escaped string when the buffer should be used.
*/ 

int ConvertUtf8ToEscape
(
char *FileSpec,
char *BufferPtr,
int BufferSize
)
{
   static char  HexDigit [] = "0123456789ABCDEF";
   static char  ScratchBuffer [ODS_MAX_FILE_NAME_LENGTH+1];

   char  *cptr, *sptr, *zptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER,
                 "ConvertUtf8ToEscape() !&Z", FileSpec);

   for (cptr = FileSpec; *cptr; cptr++)
      if (*(uchar*)cptr >= 0x80 && *(uchar*)cptr <= 0x9f)
         break;

   if (!*cptr) return (0);

   if (BufferPtr == FileSpec)
   {
      /* reusing the file spec buffer so put it into a scratch buffer */
      zptr = (sptr = ScratchBuffer) + sizeof(ScratchBuffer)-1;
      for (cptr = FileSpec; *cptr && sptr < zptr; *sptr++ = *cptr++);
      if (sptr >= zptr)
      {
         BufferPtr[0] = '\0';
         return (-1);
      }
      *sptr = '\0';
      cptr = ScratchBuffer;
   }
   else
      cptr = FileSpec;

   zptr = (sptr = BufferPtr) + BufferSize - 1;
   while (*cptr && sptr < zptr)
   {
      if (*(uchar*)cptr >= 0x80 && *(uchar*)cptr <= 0x9f)
      {
         *sptr++ = '^';
         if (sptr < zptr) *sptr++ = HexDigit[((uchar)*cptr & 0xf0) >> 4];
         if (sptr < zptr) *sptr++ = HexDigit[(uchar)*cptr & 0x0f];
         cptr++;
      }
      else
         *sptr++ = *cptr++;
   }

   if (sptr >= zptr)
   {
      BufferPtr[0] = '\0';
      return (-1);
   }

   *sptr = '\0';
   if (WATCH_MODULE(WATCH_MOD_ODS))
      WatchThis (WATCHALL, WATCH_MOD_ODS, "!&Z", BufferPtr);

   return (sptr - BufferPtr);
}

/*****************************************************************************/
/*
Generate a unique request identifier.  Based on the description and code of the
Apache Group's "mod_unique" module.  One change has been made to allow the ID
to be used as part of VMS file names, the substitution of '@' for '_'.
*/

char* GenerateUniqueId (REQUEST_STRUCT *rqptr)

{
   static uint64  UniqueId [3];
   static char  UniqueIdString [32];

   /*********/
   /* begin */
   /*********/

   sys$gettim (UniqueId[0]);
   UniqueId[1] = HttpdProcess.Pid;
   UniqueId[2] = UniqueId[2] * 69069 + 1;

   Md5HexString (UniqueId, sizeof(UniqueId), UniqueIdString);
   UniqueIdString[UNIQUE_ID_SIZE] = '\0';

   return (UniqueIdString);
}

/*****************************************************************************/
/*
Support version 10 and pre-version-10 logical and file naming conventions. 
Look for one of multiple (usually just two but can be more) possible logical
names.  When one is successfully translated return a pointer to it's null
terminated name.  Otherwise test for a subsequent and return a pointer to it if
found.  If none is found then return NoneFound if zero or positive, return the
LogicalName if minus one.  The constants should be #defined in the manner of
"?WASD_CONFIG_GLOBAL\0HTTPD$CONFIG\0".
*/

char* v10orPrev10
(
char *LogicalName,
int NoneFound
)
{
   static ushort  Length;
   static char  ValueString [128];
   static $DESCRIPTOR (LogicalNameDsc, "");
   static $DESCRIPTOR (LnmFileDevDsc, "LNM$FILE_DEV");
   static VMS_ITEM_LIST3  LnmItems [] =
   {
      { sizeof(ValueString)-1, LNM$_STRING, ValueString, &Length },
      { 0,0,0,0 }
   };

   int  count, status;
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (!LogicalName || LogicalName[0] != '?') return (LogicalName);

   count = 0;
   /* stop at any logical name delimiting colon (perhaps of a file name) */
   for (sptr = cptr = LogicalName+1; *sptr && *sptr != ':'; sptr++);
   while (*cptr)
   {
      LogicalNameDsc.dsc$a_pointer = cptr;
      LogicalNameDsc.dsc$w_length = sptr - cptr;
      status = sys$trnlnm (0, &LnmFileDevDsc, &LogicalNameDsc, 0, &LnmItems);
      if (VMSok (status))
      {
         if (count)
            FaoToStdout ("%HTTPD-W-DEPRECATED, change !AZ to !AZ (soon!!)\n",
                         cptr, LogicalName+1);
         return (cptr);
      }
      count++;
      /* scan past any logical-delimiting colon */
      while (*sptr) sptr++;
      /* stop at any logical name delimiting colon (perhaps of a file name) */
      for (cptr = (sptr += 1); *sptr && *sptr != ':'; sptr++);
   }

   if (NoneFound != -1) return (NoneFound);
   return (LogicalName+1);
}

/*****************************************************************************/
/*
Translate a logical name using LNM$FILE_DEV.  Returns a pointer to the value
string as a pointer to a static buffer which must be copied if to be retained,
or NULL if the name does not exist.
*/

char* SysTrnLnm (char *LogName)

{
   static ushort  ValueLength;
   static ulong  LnmAttributes;
   static char  LogValue [256];
   static $DESCRIPTOR (LogNameDsc, "");
   static $DESCRIPTOR (LnmTableDsc, "LNM$FILE_DEV");
   static struct {
      short int  buf_len;
      short int  item;
      void  *buf_addr;
      ushort  *ret_len;
   } LnmItems [] =
   {
      { sizeof(LnmAttributes), LNM$_ATTRIBUTES, &LnmAttributes, 0 },
      { sizeof(LogValue)-1, LNM$_STRING, LogValue, &ValueLength },
      { 0,0,0,0 }
   };

   int  status;
   char  *cptr;

   /*********/
   /* begin */
   /*********/

   for (cptr = LogName; *cptr; cptr++);
   LogNameDsc.dsc$w_length = cptr - LogName;
   LogNameDsc.dsc$a_pointer = LogName;

   memset (LogValue, 0, sizeof(LogValue));

   status = sys$trnlnm (0, &LnmTableDsc, &LogNameDsc, 0, &LnmItems);
   if (!(status & 1) || !(LnmAttributes & LNM$M_EXISTS)) return (NULL);

   return (LogValue);
}

/****************************************************************************/
/*
Assign a global symbol.
*/ 

int SetGlobalSymbol
(
char *Name,
char *String
)
{
   static int  GlobalSymbol = LIB$K_CLI_GLOBAL_SYM;
   static $DESCRIPTOR (NameDsc, "");
   static $DESCRIPTOR (ValueDsc, "");

   int  status;

   /*********/
   /* begin */
   /*********/

   NameDsc.dsc$a_pointer = Name;
   NameDsc.dsc$w_length = strlen(Name);
   ValueDsc.dsc$a_pointer = String;
   ValueDsc.dsc$w_length = strlen(String);

   status = lib$set_symbol (&NameDsc, &ValueDsc, &GlobalSymbol);
   return (status);
}

/*****************************************************************************/
/*
Return a float number of seconds.
*/

float FloatDeltaTime (ulong *DeltaTimePtr)

{
   static int  LibDeltaSecs = LIB$K_DELTA_SECONDS_F;

   int  status;
   float  fsecs;

   /*********/
   /* begin */
   /*********/

   if (*DeltaTimePtr == DELTA64_ZERO) return (0.0);

#ifdef __ia64
   status = lib$cvts_from_internal_time (&LibDeltaSecs, &fsecs, DeltaTimePtr);
#else
   status = lib$cvtf_from_internal_time (&LibDeltaSecs, &fsecs, DeltaTimePtr);
#  ifdef __ALPHA
   if (VMSok(status))
      status = cvt$convert_float (&fsecs, CVT$K_VAX_F,
                                  &fsecs, CVT$K_IEEE_S, 0);
#  endif
#endif
   if (VMSnok(status))
   {
      ErrorNoticed (NULL, status, "lib$cvts_from_internal_time()", FI_LI);
      return (0.0);
   }

   return (fsecs);
}

/*****************************************************************************/
/*
Return a longword bytes/second.
*/

int BytesPerSecond
(
ulong *BytesRxPtr,
ulong *BytesTxPtr,
ulong *DeltaTimePtr
)
{
   static int  LibDeltaSecs = LIB$K_DELTA_SECONDS_F;

   int  status;
   uint  bps;
   float  fbytes = 0.0,
          fsecs;

   /*********/
   /* begin */
   /*********/

   /* if zero time then only an artifact will be generated! */
   if (!*DeltaTimePtr || *DeltaTimePtr == DELTA64_ZERO) return (0);

   if (BytesRxPtr)
      fbytes += (float)BytesRxPtr[0] +
                ((float)BytesRxPtr[1] * 4294967296.0);
   if (BytesTxPtr)
      fbytes += (float)BytesTxPtr[0] +
                ((float)BytesTxPtr[1] * 4294967296.0);

#ifdef __ALPHA
   status = lib$cvtf_from_internal_time (&LibDeltaSecs, &fsecs, DeltaTimePtr);
   if (VMSok(status))
      status = cvt$convert_float (&fsecs, CVT$K_VAX_F,
                                  &fsecs, CVT$K_IEEE_S, 0);
#else
   status = lib$cvts_from_internal_time (&LibDeltaSecs, &fsecs, DeltaTimePtr);
#endif
   if (VMSnok(status))
   {
      if (status != LIB$_DELTIMREQ)
         ErrorNoticed (NULL, status, "lib$cvts_from_internal_time()", FI_LI);
      return (0);
   }

   /* if greater than 2000000000 (2GB/S) it's likely to be an artifact! */
   if ((bps = (uint)(fbytes / fsecs)) < 2000000000)
      return ((int)bps);
   else
      return (0);
}

/*****************************************************************************/
/*
Calculate percentages of unsigned longs using floats to avoid integer overflow
and allowing more accurate rounding.
*/

int PercentOf32
(
ulong arg1,
ulong arg2
)
{
   int  iperc;
   float  farg1, farg2, fperc;

   /*********/
   /* begin */
   /*********/

   if (arg2)
   {
      farg1 = (float)arg1;
      farg2 = (float)arg2;
      fperc = farg1 * 100.0 / farg2;
      iperc = (int)fperc;
      if (fperc - (float)iperc >= 0.5) iperc++;
      return (iperc);
   }
   return (0);
}

/*****************************************************************************/
/*
Calculate percentages of unsigned 64bits using floats to avoid overflow and
allowing more accurate rounding.
*/

int PercentOf64
(
uint64 *arg1,
uint64 *arg2
)
{
   int  iqperc;
   float  farg1, farg2, fqperc;

   /*********/
   /* begin */
   /*********/

   if (arg2)
   {
      farg1 = (float)(*(UINT64PTR)arg1);
      farg2 = (float)(*(UINT64PTR)arg2);
      if (farg2 == 0.0) return (0);
      fqperc = farg1 * 100.0 / farg2;
      iqperc = (int)fqperc;
      if (fqperc - (float)iqperc >= 0.5) iqperc++;
      return (iqperc);
   }
   return (0);
}

/*****************************************************************************/
/*
Integer power-of.
*/

int ipow
(
int base,
int exp
)
{
   int result = 1;

   /*********/
   /* begin */
   /*********/

   while (exp)
   {
      if (exp & 1) result *= base;
      exp >>= 1;
      base *= base;
   }

   return (result);
}

/*****************************************************************************/
/*
132 columns!
*/ 

#if WATCH_MOD

void TimeTest ()

{
   int  status;
   int64  time64;
   uint64  time128 [2];
   ushort  time7 [7],
           time13 [13];
   char  *cptr;
   char  buf [256];

   /*********/
   /* begin */
   /*********/

   sys$getutc (&time128);
   status = sys$numutc (&time13, &time128);
   printf ("sys$numutc() %%X%08.08X %d %d %d %d %d %d %d %d %d %d %d %d %d\n", 
           status,
           time13[0], time13[1], time13[2], time13[3], time13[4],
           time13[5], time13[6], time13[7], time13[8], time13[9],
           time13[10], time13[11], time13[12]);

   TimeSetHttpdUTC ();
   printf ("TimeUtcDelta64: %lld sec:%lld min:%lld |%s|\n",
           TimeUtcDelta64, TimeUtcDelta64 / TIME64_ONE_SEC,
           TimeUtcDelta64 / TIME64_ONE_MIN, TimeGmtString);

   HttpGetTime (cptr = "Fri, 25 Aug 1995 17:32:40 GMT", &time64); 
   sys$numtim (&time7, &time64);
   HttpGmTimeString (buf, &time64);
   printf ("%32.32s  %d %d %d %d %d %d %d |%s|\n",
           cptr, time7[0], time7[1], time7[2], time7[3], time7[4],
                 time7[5], time7[6], buf); 

   HttpGetTime (cptr = "Friday, 25-Aug-1995 17:32:40 GMT", &time64);
   sys$numtim (&time7, &time64);
   HttpGmTimeString (buf, &time64);
   printf ("%32.32s  %d %d %d %d %d %d %d |%s|\n",
           cptr, time7[0], time7[1], time7[2], time7[3], time7[4],
                 time7[5], time7[6], buf); 

   HttpGetTime (cptr = "1995-08-25T17:32:40.34Z+0000", &time64);
   sys$numtim (&time7, &time64);
   HttpGmTimeString (buf, &time64);
   printf ("%32.32s  %d %d %d %d %d %d %d  |%s|\n",
           cptr, time7[0], time7[1], time7[2], time7[3], time7[4],
                 time7[5], time7[6], buf); 
}

#endif /* WATCH_MOD */

/*****************************************************************************/
/*
Set any 64 bit time using the value of the specificed logical name.
Only for development purposes.
*/

#if WATCH_MOD

void SetAnyTime (char *lname, ulong *tbptr)

{
   int  status;
   char  *cptr;
   if (cptr = SysTrnLnm (lname))
   {
      $DESCRIPTOR (dsc, "");
      dsc.dsc$a_pointer = cptr;
      dsc.dsc$w_length = strlen(cptr);
      status = sys$bintim (&dsc, tbptr);
      if (VMSnok (status)) ErrorExitVmsStatus (status, "SetAnyTime()", FI_LI);
   }
}

#endif /* WATCH_MOD */

/*****************************************************************************/
/*
$ HTTPD /TEST MATCH for the relative performance of MATCH1..8() and memcmp().
*/

#if WATCH_MOD

MatchPerf ()

#pragma optimize save
#pragma optimize level=0

{
   int  cnt, result;
   char  MatchTest1 [] = "01234567890",
         MatchTest2 [] = "012345678_0";
   int  MatchTest3 = '0123';

   /*********/
   /* begin */
   /*********/

   lib$init_timer (0);
   for (cnt = 100000000; cnt; cnt--)
   {
      result = MATCH2 (MatchTest1, MatchTest2);
      result = MATCH3 (MatchTest1, MatchTest2);
      result = MATCH4 (MatchTest1, MatchTest2);
      result = MATCH5 (MatchTest1, MatchTest2);
      result = MATCH6 (MatchTest1, MatchTest2);
      result = MATCH7 (MatchTest1, MatchTest2);
      result = MATCH8 (MatchTest1, MatchTest2);
   }
   lib$show_timer (0,0,0,0);
   lib$init_timer (0);
   for (cnt = 100000000; cnt; cnt--)
   {
      result = !memcmp (MatchTest1, MatchTest2, 2);
      result = !memcmp (MatchTest1, MatchTest2, 3);
      result = !memcmp (MatchTest1, MatchTest2, 4);
      result = !memcmp (MatchTest1, MatchTest2, 5);
      result = !memcmp (MatchTest1, MatchTest2, 6);
      result = !memcmp (MatchTest1, MatchTest2, 7);
      result = !memcmp (MatchTest1, MatchTest2, 8);
   }
   lib$show_timer (0,0,0,0);
   lib$init_timer (0);
   for (cnt = 100000000; cnt; cnt--)
   {
      result = MATCH2 (MatchTest1, MatchTest2);
      result = MATCH3 (MatchTest1, MatchTest2);
      result = MATCH4 (MatchTest1, MatchTest2);
      result = MATCH2 (MatchTest1, MatchTest2);
      result = MATCH3 (MatchTest1, MatchTest2);
      result = MATCH4 (MatchTest1, MatchTest2);
   }
   lib$show_timer (0,0,0,0);
   lib$init_timer (0);
   for (cnt = 100000000; cnt; cnt--)
   {
      result = SAME2 (MatchTest1, MatchTest3);
      result = SAME3 (MatchTest1, MatchTest3);
      result = SAME4 (MatchTest1, MatchTest3);
      result = SAME2 (MatchTest1, MatchTest3);
      result = SAME3 (MatchTest1, MatchTest3);
      result = SAME4 (MatchTest1, MatchTest3);
   }
   lib$show_timer (0,0,0,0);
}

#pragma optimize restore

#endif /* WATCH_MOD */

/****************************************************************************/