[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]
[2929]
[2930]
[2931]
[2932]
[2933]
[2934]
[2935]
[2936]
[2937]
[2938]
[2939]
[2940]
[2941]
[2942]
[2943]
[2944]
[2945]
[2946]
[2947]
[2948]
[2949]
[2950]
[2951]
[2952]
[2953]
[2954]
[2955]
[2956]
[2957]
[2958]
[2959]
[2960]
[2961]
[2962]
[2963]
[2964]
[2965]
[2966]
[2967]
[2968]
[2969]
[2970]
[2971]
[2972]
[2973]
[2974]
[2975]
[2976]
[2977]
[2978]
[2979]
[2980]
[2981]
[2982]
[2983]
[2984]
[2985]
[2986]
[2987]
[2988]
[2989]
[2990]
[2991]
[2992]
[2993]
[2994]
[2995]
[2996]
[2997]
[2998]
[2999]
[3000]
[3001]
[3002]
[3003]
[3004]
[3005]
[3006]
[3007]
[3008]
[3009]
[3010]
[3011]
[3012]
[3013]
[3014]
[3015]
[3016]
[3017]
[3018]
[3019]
[3020]
[3021]
[3022]
[3023]
[3024]
[3025]
[3026]
[3027]
[3028]
[3029]
[3030]
[3031]
[3032]
[3033]
[3034]
[3035]
[3036]
[3037]
[3038]
[3039]
[3040]
[3041]
[3042]
[3043]
[3044]
[3045]
[3046]
[3047]
[3048]
[3049]
[3050]
[3051]
[3052]
[3053]
[3054]
[3055]
[3056]
[3057]
[3058]
[3059]
[3060]
[3061]
[3062]
[3063]
[3064]
[3065]
[3066]
[3067]
[3068]
[3069]
[3070]
[3071]
[3072]
[3073]
[3074]
[3075]
[3076]
[3077]
[3078]
[3079]
[3080]
[3081]
[3082]
[3083]
[3084]
[3085]
[3086]
[3087]
[3088]
[3089]
[3090]
[3091]
[3092]
[3093]
[3094]
[3095]
[3096]
[3097]
[3098]
[3099]
[3100]
[3101]
[3102]
[3103]
[3104]
[3105]
[3106]
[3107]
[3108]
[3109]
[3110]
[3111]
[3112]
[3113]
[3114]
[3115]
[3116]
[3117]
[3118]
[3119]
[3120]
[3121]
[3122]
[3123]
[3124]
[3125]
[3126]
[3127]
[3128]
[3129]
[3130]
[3131]
[3132]
[3133]
[3134]
[3135]
[3136]
[3137]
[3138]
[3139]
[3140]
[3141]
[3142]
[3143]
[3144]
[3145]
[3146]
[3147]
[3148]
[3149]
[3150]
[3151]
[3152]
[3153]
[3154]
[3155]
[3156]
[3157]
[3158]
[3159]
[3160]
[3161]
[3162]
[3163]
[3164]
[3165]
[3166]
[3167]
[3168]
[3169]
[3170]
[3171]
[3172]
[3173]
[3174]
[3175]
[3176]
[3177]
[3178]
[3179]
[3180]
[3181]
[3182]
[3183]
[3184]
[3185]
[3186]
[3187]
[3188]
[3189]
[3190]
[3191]
[3192]
[3193]
[3194]
[3195]
[3196]
[3197]
[3198]
[3199]
[3200]
[3201]
[3202]
[3203]
[3204]
[3205]
[3206]
[3207]
[3208]
[3209]
[3210]
[3211]
[3212]
[3213]
[3214]
[3215]
[3216]
[3217]
[3218]
[3219]
[3220]
[3221]
[3222]
[3223]
[3224]
[3225]
[3226]
[3227]
[3228]
[3229]
[3230]
[3231]
[3232]
[3233]
[3234]
[3235]
[3236]
[3237]
[3238]
[3239]
[3240]
[3241]
[3242]
[3243]
[3244]
[3245]
[3246]
[3247]
[3248]
[3249]
[3250]
[3251]
[3252]
[3253]
[3254]
[3255]
[3256]
[3257]
[3258]
[3259]
[3260]
[3261]
[3262]
[3263]
[3264]
[3265]
[3266]
[3267]
[3268]
[3269]
[3270]
[3271]
[3272]
[3273]
[3274]
[3275]
[3276]
[3277]
[3278]
[3279]
[3280]
[3281]
[3282]
[3283]
[3284]
[3285]
[3286]
[3287]
[3288]
[3289]
[3290]
[3291]
[3292]
[3293]
[3294]
[3295]
[3296]
[3297]
[3298]
[3299]
[3300]
[3301]
[3302]
[3303]
[3304]
[3305]
[3306]
[3307]
[3308]
[3309]
[3310]
[3311]
[3312]
[3313]
[3314]
[3315]
[3316]
[3317]
[3318]
[3319]
[3320]
[3321]
[3322]
[3323]
[3324]
[3325]
[3326]
[3327]
[3328]
[3329]
[3330]
[3331]
[3332]
[3333]
[3334]
[3335]
[3336]
[3337]
[3338]
[3339]
[3340]
[3341]
[3342]
[3343]
[3344]
[3345]
[3346]
[3347]
[3348]
[3349]
[3350]
[3351]
[3352]
[3353]
[3354]
[3355]
[3356]
[3357]
[3358]
[3359]
[3360]
[3361]
[3362]
[3363]
[3364]
[3365]
[3366]
[3367]
[3368]
[3369]
[3370]
[3371]
[3372]
[3373]
[3374]
[3375]
[3376]
[3377]
[3378]
[3379]
[3380]
[3381]
[3382]
[3383]
[3384]
[3385]
[3386]
[3387]
[3388]
[3389]
[3390]
[3391]
[3392]
[3393]
[3394]
[3395]
[3396]
[3397]
[3398]
[3399]
[3400]
[3401]
[3402]
[3403]
[3404]
[3405]
[3406]
[3407]
[3408]
[3409]
[3410]
[3411]
[3412]
[3413]
[3414]
[3415]
[3416]
[3417]
[3418]
[3419]
[3420]
[3421]
[3422]
[3423]
[3424]
[3425]
[3426]
[3427]
[3428]
[3429]
[3430]
[3431]
[3432]
[3433]
[3434]
[3435]
[3436]
[3437]
[3438]
[3439]
[3440]
[3441]
[3442]
[3443]
[3444]
[3445]
[3446]
[3447]
[3448]
[3449]
[3450]
[3451]
[3452]
[3453]
[3454]
[3455]
[3456]
[3457]
[3458]
[3459]
[3460]
[3461]
[3462]
[3463]
[3464]
[3465]
[3466]
[3467]
[3468]
[3469]
[3470]
[3471]
[3472]
[3473]
[3474]
[3475]
[3476]
[3477]
[3478]
[3479]
[3480]
[3481]
[3482]
[3483]
[3484]
[3485]
[3486]
[3487]
[3488]
[3489]
[3490]
[3491]
[3492]
[3493]
[3494]
[3495]
[3496]
[3497]
[3498]
[3499]
[3500]
[3501]
[3502]
[3503]
[3504]
[3505]
[3506]
[3507]
[3508]
[3509]
[3510]
[3511]
[3512]
[3513]
[3514]
[3515]
[3516]
[3517]
[3518]
[3519]
[3520]
[3521]
[3522]
[3523]
[3524]
[3525]
[3526]
[3527]
[3528]
[3529]
[3530]
[3531]
[3532]
[3533]
[3534]
[3535]
[3536]
[3537]
[3538]
[3539]
[3540]
[3541]
[3542]
[3543]
[3544]
[3545]
[3546]
[3547]
[3548]
[3549]
[3550]
[3551]
[3552]
[3553]
[3554]
[3555]
[3556]
[3557]
[3558]
[3559]
[3560]
[3561]
[3562]
[3563]
[3564]
[3565]
[3566]
[3567]
[3568]
[3569]
[3570]
[3571]
[3572]
[3573]
[3574]
[3575]
[3576]
[3577]
[3578]
[3579]
[3580]
[3581]
[3582]
[3583]
[3584]
[3585]
[3586]
[3587]
[3588]
[3589]
[3590]
[3591]
[3592]
[3593]
[3594]
[3595]
[3596]
[3597]
[3598]
[3599]
[3600]
[3601]
[3602]
[3603]
[3604]
[3605]
[3606]
[3607]
[3608]
[3609]
[3610]
[3611]
[3612]
[3613]
[3614]
[3615]
[3616]
[3617]
[3618]
[3619]
[3620]
[3621]
[3622]
[3623]
[3624]
[3625]
[3626]
[3627]
[3628]
[3629]
[3630]
[3631]
[3632]
[3633]
[3634]
[3635]
[3636]
[3637]
[3638]
[3639]
[3640]
[3641]
[3642]
[3643]
[3644]
[3645]
[3646]
[3647]
[3648]
[3649]
[3650]
[3651]
[3652]
[3653]
[3654]
[3655]
[3656]
[3657]
[3658]
[3659]
[3660]
[3661]
[3662]
[3663]
[3664]
[3665]
[3666]
[3667]
[3668]
[3669]
[3670]
[3671]
[3672]
[3673]
[3674]
[3675]
[3676]
[3677]
[3678]
[3679]
[3680]
[3681]
[3682]
[3683]
[3684]
[3685]
[3686]
[3687]
[3688]
[3689]
[3690]
[3691]
[3692]
[3693]
[3694]
[3695]
[3696]
[3697]
[3698]
[3699]
[3700]
[3701]
[3702]
[3703]
[3704]
[3705]
[3706]
[3707]
[3708]
[3709]
[3710]
[3711]
[3712]
[3713]
[3714]
[3715]
[3716]
[3717]
[3718]
[3719]
[3720]
[3721]
[3722]
[3723]
[3724]
[3725]
[3726]
[3727]
[3728]
[3729]
[3730]
[3731]
[3732]
[3733]
[3734]
[3735]
[3736]
[3737]
[3738]
[3739]
[3740]
[3741]
[3742]
[3743]
[3744]
[3745]
[3746]
[3747]
[3748]
[3749]
[3750]
[3751]
[3752]
[3753]
[3754]
[3755]
[3756]
[3757]
[3758]
[3759]
[3760]
[3761]
[3762]
[3763]
[3764]
[3765]
[3766]
[3767]
[3768]
[3769]
[3770]
[3771]
[3772]
[3773]
[3774]
[3775]
[3776]
[3777]
[3778]
[3779]
[3780]
[3781]
[3782]
[3783]
[3784]
[3785]
[3786]
[3787]
[3788]
[3789]
[3790]
[3791]
[3792]
[3793]
[3794]
[3795]
[3796]
[3797]
[3798]
[3799]
[3800]
[3801]
[3802]
[3803]
[3804]
[3805]
[3806]
[3807]
[3808]
[3809]
[3810]
[3811]
[3812]
[3813]
[3814]
[3815]
[3816]
[3817]
[3818]
[3819]
[3820]
[3821]
[3822]
[3823]
[3824]
[3825]
[3826]
[3827]
[3828]
[3829]
[3830]
[3831]
[3832]
[3833]
[3834]
[3835]
[3836]
[3837]
[3838]
[3839]
[3840]
[3841]
[3842]
[3843]
[3844]
[3845]
[3846]
[3847]
[3848]
[3849]
[3850]
[3851]
[3852]
[3853]
[3854]
[3855]
[3856]
[3857]
[3858]
[3859]
[3860]
[3861]
[3862]
[3863]
[3864]
[3865]
[3866]
[3867]
[3868]
[3869]
[3870]
[3871]
[3872]
[3873]
[3874]
[3875]
[3876]
[3877]
[3878]
[3879]
[3880]
[3881]
[3882]
[3883]
[3884]
[3885]
[3886]
[3887]
[3888]
[3889]
[3890]
[3891]
[3892]
[3893]
[3894]
[3895]
[3896]
[3897]
[3898]
[3899]
[3900]
[3901]
[3902]
[3903]
[3904]
[3905]
[3906]
[3907]
[3908]
[3909]
[3910]
[3911]
[3912]
[3913]
[3914]
[3915]
[3916]
[3917]
[3918]
[3919]
[3920]
[3921]
[3922]
[3923]
[3924]
[3925]
[3926]
[3927]
[3928]
[3929]
[3930]
[3931]
[3932]
[3933]
[3934]
[3935]
[3936]
[3937]
[3938]
[3939]
[3940]
[3941]
[3942]
[3943]
[3944]
[3945]
[3946]
[3947]
[3948]
[3949]
[3950]
[3951]
[3952]
[3953]
[3954]
[3955]
[3956]
[3957]
[3958]
[3959]
[3960]
[3961]
[3962]
[3963]
[3964]
[3965]
[3966]
[3967]
[3968]
[3969]
[3970]
[3971]
[3972]
[3973]
[3974]
[3975]
[3976]
[3977]
[3978]
[3979]
[3980]
[3981]
[3982]
[3983]
[3984]
[3985]
[3986]
[3987]
[3988]
[3989]
[3990]
[3991]
[3992]
[3993]
[3994]
[3995]
[3996]
[3997]
[3998]
[3999]
[4000]
[4001]
[4002]
[4003]
[4004]
[4005]
[4006]
[4007]
[4008]
[4009]
[4010]
[4011]
[4012]
[4013]
[4014]
[4015]
[4016]
[4017]
[4018]
[4019]
[4020]
[4021]
[4022]
[4023]
[4024]
[4025]
[4026]
[4027]
[4028]
[4029]
[4030]
[4031]
[4032]
[4033]
[4034]
[4035]
[4036]
[4037]
[4038]
[4039]
[4040]
[4041]
[4042]
[4043]
[4044]
[4045]
[4046]
[4047]
[4048]
[4049]
[4050]
[4051]
[4052]
[4053]
[4054]
[4055]
[4056]
[4057]
[4058]
[4059]
[4060]
[4061]
[4062]
[4063]
[4064]
[4065]
[4066]
[4067]
[4068]
[4069]
[4070]
[4071]
[4072]
[4073]
[4074]
[4075]
[4076]
[4077]
[4078]
[4079]
[4080]
[4081]
[4082]
[4083]
[4084]
[4085]
[4086]
[4087]
[4088]
[4089]
[4090]
[4091]
[4092]
[4093]
[4094]
[4095]
[4096]
[4097]
[4098]
[4099]
[4100]
[4101]
[4102]
[4103]
[4104]
[4105]
[4106]
[4107]
[4108]
[4109]
[4110]
[4111]
[4112]
[4113]
[4114]
[4115]
[4116]
[4117]
[4118]
[4119]
[4120]
[4121]
[4122]
[4123]
[4124]
[4125]
[4126]
[4127]
[4128]
[4129]
[4130]
[4131]
[4132]
[4133]
[4134]
[4135]
[4136]
[4137]
[4138]
[4139]
[4140]
[4141]
[4142]
[4143]
[4144]
[4145]
[4146]
[4147]
[4148]
[4149]
[4150]
[4151]
[4152]
[4153]
[4154]
[4155]
[4156]
[4157]
[4158]
[4159]
[4160]
[4161]
[4162]
[4163]
[4164]
[4165]
[4166]
[4167]
[4168]
[4169]
[4170]
[4171]
[4172]
[4173]
[4174]
[4175]
[4176]
[4177]
[4178]
[4179]
[4180]
[4181]
[4182]
[4183]
[4184]
[4185]
[4186]
[4187]
[4188]
[4189]
[4190]
[4191]
[4192]
[4193]
[4194]
[4195]
[4196]
[4197]
[4198]
[4199]
[4200]
[4201]
[4202]
[4203]
[4204]
[4205]
[4206]
[4207]
[4208]
[4209]
[4210]
[4211]
[4212]
[4213]
[4214]
[4215]
[4216]
[4217]
[4218]
[4219]
[4220]
[4221]
[4222]
[4223]
[4224]
[4225]
[4226]
[4227]
[4228]
[4229]
[4230]
[4231]
[4232]
[4233]
[4234]
[4235]
[4236]
[4237]
[4238]
[4239]
[4240]
[4241]
[4242]
[4243]
[4244]
[4245]
[4246]
[4247]
[4248]
[4249]
[4250]
[4251]
[4252]
[4253]
[4254]
[4255]
[4256]
[4257]
[4258]
[4259]
[4260]
[4261]
[4262]
[4263]
[4264]
[4265]
[4266]
[4267]
[4268]
[4269]
[4270]
[4271]
[4272]
[4273]
[4274]
[4275]
[4276]
[4277]
[4278]
[4279]
[4280]
[4281]
[4282]
[4283]
[4284]
[4285]
[4286]
[4287]
[4288]
[4289]
[4290]
[4291]
[4292]
[4293]
[4294]
[4295]
[4296]
[4297]
[4298]
[4299]
[4300]
[4301]
[4302]
[4303]
[4304]
[4305]
[4306]
[4307]
[4308]
[4309]
[4310]
[4311]
[4312]
[4313]
[4314]
[4315]
[4316]
[4317]
[4318]
[4319]
[4320]
[4321]
[4322]
[4323]
[4324]
[4325]
[4326]
[4327]
[4328]
[4329]
[4330]
[4331]
[4332]
[4333]
[4334]
[4335]
[4336]
[4337]
[4338]
[4339]
[4340]
[4341]
[4342]
[4343]
[4344]
[4345]
[4346]
[4347]
[4348]
[4349]
[4350]
[4351]
[4352]
[4353]
[4354]
[4355]
[4356]
[4357]
[4358]
[4359]
[4360]
[4361]
[4362]
[4363]
[4364]
[4365]
[4366]
[4367]
[4368]
[4369]
[4370]
[4371]
[4372]
[4373]
[4374]
[4375]
[4376]
[4377]
[4378]
[4379]
[4380]
[4381]
[4382]
[4383]
[4384]
[4385]
[4386]
[4387]
[4388]
[4389]
[4390]
[4391]
[4392]
[4393]
[4394]
[4395]
[4396]
[4397]
[4398]
[4399]
[4400]
[4401]
[4402]
[4403]
[4404]
[4405]
[4406]
[4407]
[4408]
[4409]
[4410]
[4411]
[4412]
[4413]
[4414]
[4415]
[4416]
[4417]
[4418]
[4419]
[4420]
[4421]
[4422]
[4423]
[4424]
[4425]
[4426]
[4427]
[4428]
[4429]
[4430]
[4431]
[4432]
[4433]
[4434]
[4435]
[4436]
[4437]
[4438]
[4439]
[4440]
[4441]
[4442]
[4443]
[4444]
[4445]
[4446]
[4447]
[4448]
[4449]
[4450]
[4451]
[4452]
[4453]
[4454]
[4455]
[4456]
[4457]
[4458]
[4459]
[4460]
[4461]
[4462]
[4463]
[4464]
[4465]
[4466]
[4467]
[4468]
[4469]
[4470]
[4471]
[4472]
[4473]
[4474]
[4475]
[4476]
[4477]
[4478]
[4479]
[4480]
[4481]
[4482]
[4483]
[4484]
[4485]
[4486]
[4487]
[4488]
[4489]
[4490]
[4491]
[4492]
[4493]
[4494]
[4495]
[4496]
[4497]
[4498]
[4499]
[4500]
[4501]
[4502]
[4503]
[4504]
[4505]
[4506]
[4507]
[4508]
[4509]
[4510]
[4511]
[4512]
[4513]
[4514]
[4515]
[4516]
[4517]
[4518]
[4519]
[4520]
[4521]
[4522]
[4523]
[4524]
[4525]
[4526]
[4527]
[4528]
[4529]
[4530]
[4531]
[4532]
[4533]
[4534]
[4535]
[4536]
[4537]
[4538]
[4539]
[4540]
[4541]
[4542]
[4543]
[4544]
[4545]
[4546]
[4547]
[4548]
[4549]
[4550]
[4551]
[4552]
[4553]
[4554]
[4555]
[4556]
[4557]
[4558]
[4559]
[4560]
[4561]
[4562]
[4563]
[4564]
[4565]
[4566]
[4567]
[4568]
[4569]
[4570]
[4571]
[4572]
[4573]
[4574]
[4575]
[4576]
[4577]
[4578]
[4579]
[4580]
[4581]
[4582]
[4583]
[4584]
[4585]
[4586]
[4587]
[4588]
[4589]
[4590]
[4591]
[4592]
[4593]
[4594]
[4595]
[4596]
[4597]
[4598]
[4599]
[4600]
[4601]
[4602]
[4603]
[4604]
[4605]
[4606]
[4607]
[4608]
[4609]
[4610]
[4611]
[4612]
[4613]
[4614]
[4615]
[4616]
[4617]
[4618]
[4619]
[4620]
[4621]
[4622]
[4623]
[4624]
[4625]
[4626]
[4627]
[4628]
[4629]
[4630]
[4631]
[4632]
[4633]
[4634]
[4635]
[4636]
[4637]
[4638]
[4639]
[4640]
[4641]
[4642]
[4643]
[4644]
[4645]
[4646]
[4647]
[4648]
[4649]
[4650]
[4651]
[4652]
[4653]
[4654]
[4655]
[4656]
[4657]
[4658]
[4659]
[4660]
[4661]
[4662]
[4663]
[4664]
[4665]
[4666]
[4667]
[4668]
[4669]
[4670]
[4671]
[4672]
[4673]
[4674]
[4675]
[4676]
[4677]
[4678]
[4679]
[4680]
[4681]
[4682]
[4683]
[4684]
[4685]
[4686]
[4687]
[4688]
[4689]
[4690]
[4691]
[4692]
[4693]
[4694]
[4695]
[4696]
[4697]
[4698]
[4699]
[4700]
[4701]
[4702]
[4703]
[4704]
[4705]
[4706]
[4707]
[4708]
[4709]
[4710]
[4711]
[4712]
[4713]
[4714]
[4715]
[4716]
[4717]
[4718]
[4719]
[4720]
[4721]
[4722]
[4723]
[4724]
[4725]
[4726]
[4727]
[4728]
[4729]
[4730]
[4731]
[4732]
[4733]
[4734]
[4735]
[4736]
[4737]
[4738]
[4739]
[4740]
[4741]
[4742]
[4743]
[4744]
[4745]
[4746]
[4747]
[4748]
[4749]
[4750]
[4751]
[4752]
[4753]
[4754]
[4755]
[4756]
[4757]
[4758]
[4759]
[4760]
[4761]
[4762]
[4763]
[4764]
[4765]
[4766]
[4767]
[4768]
[4769]
[4770]
[4771]
[4772]
[4773]
[4774]
[4775]
[4776]
[4777]
[4778]
[4779]
[4780]
[4781]
[4782]
[4783]
[4784]
[4785]
[4786]
[4787]
[4788]
[4789]
[4790]
[4791]
[4792]
[4793]
[4794]
[4795]
[4796]
[4797]
[4798]
[4799]
[4800]
[4801]
[4802]
[4803]
[4804]
[4805]
[4806]
[4807]
[4808]
[4809]
[4810]
[4811]
[4812]
[4813]
[4814]
[4815]
[4816]
[4817]
[4818]
[4819]
[4820]
[4821]
[4822]
[4823]
[4824]
[4825]
[4826]
[4827]
[4828]
[4829]
[4830]
[4831]
[4832]
[4833]
[4834]
[4835]
[4836]
[4837]
[4838]
[4839]
[4840]
[4841]
[4842]
[4843]
[4844]
[4845]
[4846]
[4847]
[4848]
[4849]
[4850]
[4851]
[4852]
[4853]
[4854]
[4855]
[4856]
[4857]
[4858]
[4859]
[4860]
[4861]
[4862]
[4863]
[4864]
[4865]
[4866]
[4867]
[4868]
[4869]
[4870]
[4871]
[4872]
[4873]
[4874]
[4875]
[4876]
[4877]
[4878]
[4879]
[4880]
[4881]
[4882]
[4883]
[4884]
[4885]
[4886]
[4887]
[4888]
[4889]
[4890]
[4891]
[4892]
[4893]
[4894]
[4895]
[4896]
[4897]
[4898]
[4899]
[4900]
[4901]
[4902]
[4903]
[4904]
[4905]
[4906]
[4907]
[4908]
[4909]
[4910]
[4911]
[4912]
[4913]
[4914]
[4915]
[4916]
[4917]
[4918]
[4919]
[4920]
[4921]
[4922]
[4923]
[4924]
[4925]
[4926]
[4927]
[4928]
[4929]
[4930]
[4931]
[4932]
[4933]
[4934]
[4935]
[4936]
[4937]
[4938]
[4939]
[4940]
[4941]
[4942]
[4943]
[4944]
[4945]
[4946]
[4947]
[4948]
[4949]
[4950]
[4951]
[4952]
[4953]
[4954]
[4955]
[4956]
[4957]
[4958]
[4959]
[4960]
[4961]
[4962]
[4963]
[4964]
[4965]
[4966]
[4967]
[4968]
[4969]
[4970]
[4971]
[4972]
[4973]
[4974]
[4975]
[4976]
[4977]
[4978]
[4979]
[4980]
[4981]
[4982]
[4983]
[4984]
[4985]
[4986]
[4987]
[4988]
[4989]
[4990]
[4991]
[4992]
[4993]
[4994]
[4995]
[4996]
[4997]
[4998]
[4999]
[5000]
[5001]
[5002]
[5003]
[5004]
[5005]
[5006]
[5007]
[5008]
[5009]
[5010]
[5011]
[5012]
[5013]
[5014]
[5015]
[5016]
[5017]
[5018]
[5019]
[5020]
[5021]
[5022]
[5023]
[5024]
[5025]
[5026]
[5027]
[5028]
[5029]
[5030]
[5031]
[5032]
[5033]
[5034]
[5035]
[5036]
[5037]
[5038]
[5039]
[5040]
[5041]
[5042]
[5043]
[5044]
[5045]
[5046]
[5047]
[5048]
[5049]
[5050]
[5051]
[5052]
[5053]
[5054]
[5055]
[5056]
[5057]
[5058]
[5059]
[5060]
[5061]
[5062]
[5063]
[5064]
[5065]
[5066]
[5067]
[5068]
[5069]
[5070]
[5071]
[5072]
[5073]
[5074]
[5075]
[5076]
[5077]
[5078]
[5079]
[5080]
[5081]
[5082]
[5083]
[5084]
[5085]
[5086]
[5087]
[5088]
[5089]
[5090]
[5091]
[5092]
[5093]
[5094]
[5095]
[5096]
[5097]
[5098]
[5099]
[5100]
[5101]
[5102]
[5103]
[5104]
[5105]
[5106]
[5107]
[5108]
[5109]
[5110]
[5111]
[5112]
[5113]
[5114]
[5115]
[5116]
[5117]
[5118]
[5119]
[5120]
[5121]
[5122]
[5123]
[5124]
[5125]
[5126]
[5127]
[5128]
[5129]
[5130]
[5131]
[5132]
[5133]
[5134]
[5135]
[5136]
[5137]
[5138]
[5139]
[5140]
[5141]
[5142]
[5143]
[5144]
[5145]
[5146]
[5147]
[5148]
[5149]
[5150]
[5151]
[5152]
[5153]
[5154]
[5155]
[5156]
[5157]
[5158]
[5159]
[5160]
[5161]
[5162]
[5163]
[5164]
[5165]
[5166]
[5167]
[5168]
[5169]
[5170]
[5171]
[5172]
[5173]
[5174]
[5175]
[5176]
[5177]
[5178]
[5179]
[5180]
[5181]
[5182]
[5183]
[5184]
[5185]
[5186]
[5187]
[5188]
[5189]
[5190]
[5191]
[5192]
[5193]
[5194]
[5195]
[5196]
[5197]
[5198]
[5199]
[5200]
[5201]
[5202]
[5203]
[5204]
[5205]
[5206]
[5207]
[5208]
[5209]
[5210]
[5211]
[5212]
[5213]
[5214]
[5215]
[5216]
[5217]
[5218]
[5219]
[5220]
[5221]
[5222]
[5223]
[5224]
[5225]
[5226]
[5227]
[5228]
[5229]
[5230]
[5231]
[5232]
[5233]
[5234]
[5235]
[5236]
[5237]
[5238]
[5239]
[5240]
[5241]
[5242]
[5243]
[5244]
[5245]
[5246]
[5247]
[5248]
[5249]
[5250]
[5251]
[5252]
[5253]
[5254]
[5255]
[5256]
[5257]
[5258]
[5259]
[5260]
[5261]
[5262]
[5263]
[5264]
[5265]
[5266]
[5267]
[5268]
[5269]
[5270]
[5271]
[5272]
[5273]
[5274]
[5275]
[5276]
[5277]
[5278]
[5279]
[5280]
[5281]
[5282]
[5283]
[5284]
[5285]
[5286]
[5287]
[5288]
[5289]
[5290]
[5291]
[5292]
[5293]
[5294]
[5295]
[5296]
[5297]
[5298]
[5299]
[5300]
[5301]
[5302]
[5303]
[5304]
[5305]
[5306]
[5307]
[5308]
[5309]
[5310]
[5311]
[5312]
[5313]
[5314]
[5315]
[5316]
[5317]
[5318]
[5319]
[5320]
[5321]
[5322]
[5323]
[5324]
[5325]
[5326]
[5327]
[5328]
[5329]
[5330]
[5331]
[5332]
[5333]
[5334]
[5335]
[5336]
[5337]
[5338]
[5339]
[5340]
[5341]
[5342]
[5343]
[5344]
[5345]
[5346]
[5347]
[5348]
[5349]
[5350]
[5351]
[5352]
[5353]
[5354]
[5355]
[5356]
[5357]
[5358]
[5359]
[5360]
[5361]
[5362]
[5363]
[5364]
[5365]
[5366]
[5367]
[5368]
[5369]
[5370]
[5371]
[5372]
[5373]
[5374]
[5375]
[5376]
[5377]
[5378]
[5379]
[5380]
[5381]
[5382]
[5383]
[5384]
[5385]
[5386]
[5387]
[5388]
[5389]
[5390]
[5391]
[5392]
[5393]
[5394]
[5395]
[5396]
[5397]
[5398]
[5399]
[5400]
[5401]
[5402]
[5403]
[5404]
[5405]
[5406]
[5407]
[5408]
[5409]
[5410]
[5411]
[5412]
[5413]
[5414]
[5415]
[5416]
[5417]
[5418]
[5419]
[5420]
[5421]
[5422]
[5423]
[5424]
[5425]
/*****************************************************************************/
#ifdef COMMENTS_WITH_COMMENTS
/*
                                   wsLIB.c

WebSocket library for WASD persistent WebSocket applications (CGIplus scripts).

  https://www.rfc-editor.org/rfc/rfc6455.txt

This library allows AST-driven, multi-client WebSocket applications.

It also abstracts as much of the required functionality into callable functions
using optional string descriptors so as to minimise dependency on the C
language and on knowing the internals of the library data structure. 
Supplementary but not necessarily general-purpose client functionality is
provided by WSLIBCL.C, and WSLIB.C contains some minor but necessary
functionality to support not only the primary IPC via the mailboxes of the WASD
CGIplus/WebSocket environment but also WebSocket comms via the TCP/IP socket
established by the client connection functionality.

The WebSocket read is best as a single thread (at least the client-driven
logic) within the processing and the writes  (and "WATCH:" callout) can support
multiple, concurrent I/O per thread.  Callout AST functions cannot themselves
call WsLibWatchScript() or WsLibCallout() (i.e. a callout inside a callout).

Check the application examples within the [SRC.WEBSOCKET] directory for
illustrations of use.

All sizes are unsigned longwords and allow ranges from 0 to 4294967295.

The default content (and frame type) is 8 bit ASCII text which is implicitly
converted to and from UTF-8 during reads and writes.  This can also be
explicitly set using WsLibSetAscii().  If the content is already UTF-8 or is
required to remain UTF-8 then using WsLibSetUtf8() sets the conversion off.  Of
course binary content, set using WsLibSetBinary(), is opaque.

The library contains WATCH points.  Network [x]Data and [x]Script provide a
useful combination of traffic data.  The library function WsLibWatchScript()
allows WebSocket applications (scripts) to provide additional WATCHable
information via the [x]Script item.


REQUEST SYNCHRONISATION
-----------------------
wsLIB start-of-request synchronisation is based on the wait-process-EOF-wait
loop model used by CGIlib, where WsLibCgiVar("") or WsLibCgiVarNull("") are
used to wait and continue when a new request is available.  The example
WebSocket scripts all use this approach by default.  wsLIB also provides an
AST-driven model.

The WsLibOnNextRequest() function should be called once during script
initialisation specifying the address of the function to be notified of a new
request.  At that point the CGI variables become available and remain so until
the script issues the CGIplus EOF sentinal, and the WsLibCgiVar..() family of
functions can be used to retrieve the CGI variable values.  This asynchronous
and the synchronous WsLibCgiVar("") approaches to request synchronisation are
mutually exclusive and once one is used using the other results in a bugcheck.


CGI VARIABLES
-------------
CGI variables using the WsLibCgiVar..() functions are only available after the
next request has been indicated and before the CGIplus EOF sentinal is
signalled.  Any that are required for further processing need to be copied into
separate storage from the pointer returned by the WsLibCgiVar..() functions. 
With the request synchronisation provided by WsLibCgiVar("") this is usually
obvious enough.  With the AST-driven request synchronisation provided by
WsLibOnNextRequest() it might be less obvious.  In any case the
WsLibCgiVarAvailable() function can be used to test whether CGI variable are
available at that time or not.


WEBSOCKET MESSAGES
------------------
WebSocket data are transfered as autonomous messages that have text (UTF-8) or
binary (opaque) content.  Messages may be transported as multiple frames of
data but this is transparent to the WebSocket application.  By default, when
writing WebSocket messages the data must remain available and unchanged for the
duration of the transfer.  With synchronous (blocking) writes this is implicit;
for asynchronous (non-blocking) writes this must be assured.  If
WsLibSetBuffer()ed the message is copied (more expensive) to an internal buffer
prior to being written, obviating the need to maintain a static source buffer.
WsLibSetNoBuffer() resets this to the default.

For WebSocket reads a fixed buffer may be provided, with a SS$_RESULTOVF error
reported if the message is larger than the buffer provided. 

  WsLibRead (<wsptr>, <buffer-addr>, <buffer-size>, <AST-addr>);

Alternatively, the wsLIB library can allocate a dynamic buffer appropriate to
the size of the message being received.  This allows considerable flexibility
for applications.  If the <buffer-addr> is NULL (zero) then dynamic message
buffering occurs.

  WsLibRead (<wsptr>, NULL, 0, <AST-addr>);

The default is for there to be no limit on the size of the buffered data (well,
actually 2^32-1 but who's counting?)  To provide an application limit on the
size of the read buffer specify NULL for the <buffer-addr> and an integer for
the maximum number of bytes allowed for the buffer.

  WsLibRead (<wsptr>, NULL, <max-buffer-size>, <AST-addr>);

For asynchronous I/O the allocated buffer is automatically freed unless
WsLibReadGrab() is used during the AST delivery routine.  For synchronous I/O a
dynamic buffer remains allocated and must be grabbed and then explicitly freed
by the in-line code after the I/O completes.


BASE FRAMING PROTOCOL
---------------------
http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10

Section 4.1

      0                   1                   2                   3
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     +-+-+-+-+-------+-+-------------+-------------------------------+
     |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
     |I|S|S|S|  (4)  |A|     (7)     |             (16/63)           |
     |N|V|V|V|       |S|             |   (if payload len==126/127)   |
     | |1|2|3|       |K|             |                               |
     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
     |     Extended payload length continued, if payload len == 127  |
     + - - - - - - - - - - - - - - - +-------------------------------+
     |                               |Masking-key, if MASK set to 1  |
     +-------------------------------+-------------------------------+
     | Masking-key (continued)       |          Payload Data         |
     +-------------------------------- - - - - - - - - - - - - - - - +
     :                     Payload Data continued ...                :
     + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
     |                     Payload Data continued ...                |
     +---------------------------------------------------------------+


FUNCTIONS
---------
struct WsLibStruct* WsLibCreate (void *UserDataPtr,
                                 void *DestroyFunction)

   Allocates a websocket structure, sets the user data pointer to the
   supplied address, and returns a pointer to the new structure.  All
   subsequent associated websocket processing requires that pointer.


void WsLibDestroy (struct WsLibStruct *wsptr)

   Deprecated.  (From v1.0.1 handled internally.)

struct WsLibStruct* WsLibNext (struct WsLibStruct **WsLibCtx)

   Step through the list of structures.  WsLibCtx is the address of pointer to
   WsLibStruct used to hold the context.  Set to NULL to initialise.  Returns
   non-null pointers for each structure in the list, then NULL when exhausted.
   Care must be exercised that multiple calls are not preempted by list
   modification (i.e. use within AST delivery or with ASTs disabled).


int WsLibMsgDsc (struct WsLibStruct *wsptr,
                 struct dsc$descriptor_s *MsgDsc);

   Sets the supplied descriptor to the latest error string.


int WsLibMsgLineNumber (struct WsLibStruct *wsptr)

   Return the wsLIB source code line at which the last message occured.


char* WsLibMsgString (struct WsLibStruct *wsptr)

   Returns a pointer to the latest error string.


int WsLibOpen (struct WsLibStruct *wsptr,
               void *AstFunction)

   Using the device names from the CGI variables WEBSOCKET_INPUT and
   WEBSOCKET_OUTPUT assign channels in preparation for asynchronous I/O.
   The AST function is called at websocket close.


int WsLibClose (struct WsLibStruct* wsptr,
                int StatusCode,
                char *StatusString)

   Send a close opcode and then shut the WebSocket down.
   Optional status code; if zero then a "normal" close status is added.
   Optional status string (description).


int WsLibIsClosed (struct WsLibStruct* wsptr)

   Return true if the WebSocket has been closed.


int WsLibConnected (struct WsLibStruct *wsptr)

   Return true if the input/output channels are connected.


void WsLibFree (void *cptr)

   Free the allocated memory (currently C-RTL but may not remain so).
   To be used after WsLibReadGrab().


int WsLibRead (struct WsLibStruct *wsptr,
               char *DataPtr,
               int DataSize,
               void *AstFunction)

   Read data from the websocket into the supplied buffer.
   Function is non-blocking if AST function is supplied, otherwise blocking.
   When the read completes the status can be returned by WsLibReadStatus(),
   the read count by WsLibReadCount(), a pointer to the data buffer using
   WsLibReadData(), and a pointer to the internal read string descriptor
   using WsLibReadDataDsc().


int WsLibReadDsc (struct WsLibStruct *wsptr,
                  struct dsc$descriptor_s *DataDsc,
                  struct dsc$descriptor_s *ReadDsc,
                  void *AstFunction)

   Read data from the websocket into the buffer described by DataDsc.
   Function is non-blocking if AST function is supplied, otherwise blocking.
   When completed ReadDsc (if supplied) is modified to reflect the read.


int WsLibReadIsBinary (struct WsLibStruct *wsptr)

   Return true if the most recent read used the BINARY opcode, false otherwise.


int WsLibReadIsText (struct WsLibStruct *wsptr)

   Return true if the most recent read used the TEXT opcode, false otherwise.


int WsLibReadStatus (struct WsLibStruct *wsptr)

   Return the status value of the most recent read.


int WsLibReadCount (struct WsLibStruct *wsptr)

   Return the number of bytes in the most recent read.


char* WsLibReadData (struct WsLibStruct *wsptr)

   Return a pointer to char of the most recent read buffer.


struct dsc$descriptor_s* WsLibReadDataDsc (struct WsLibStruct *wsptr)

   Return a pointer to the internal descriptor for the most recent read.


char* WsLibReadGrab (struct WsLibStruct *wsptr)

   Return a pointer to char of the most recent read buffer.
   The read buffer is removed from the WebSocket structure and becomes the
   resource of the grabber.  Must be WsLibFree()ed when no longer required.


unsigned long* WsLibReadTotal (struct WsLibStruct *wsptr)

   Return a pointer to a quadword containing the total number of bytes read.


void WsLibResetMsg (struct WsLibStruct *wsptr)

   Reset the latest message data.


unsigned int WsLibTime ();

   Return the current wsLIB time in seconds (i.e. C-RTL, Unix time).


int WsLibWrite (struct WsLibStruct *wsptr,
                char *DataPtr,
                int DataCount,
                void *AstFunction)

   Write the supplied data to the websocket.
   Function is non-blocking if AST function is supplied, otherwise blocking.
   If the AST function is supplied as (void*)-1 a non-blocking I/O is
   generated that does not require a target AST function.
   If DataPtr is NULL then a close is sent to the websocket.


int WsLibWriteDsc (struct WsLibStruct *wsptr,
                   struct dsc$descriptor_s *DataDsc,
                   void *AstFunction)

   Write the data supplied by the descriptor to the websocket.
   Function is non-blocking if AST function is supplied, otherwise blocking.


int WsLibWriteStatus (struct WsLibStruct *wsptr)

   Return the status value of the most recent write.


int WsLibWriteCount (struct WsLibStruct *wsptr)

   Return the number of bytes in the most recent write.


struct dsc$descriptor_s* WsLibWriteDataDsc (struct WsLibStruct *wsptr)

   Return a pointer to the internal descriptor for the most recent write.


unsigned long* WsLibWriteTotal (struct WsLibStruct *wsptr)

   Return a pointer to a quadword containing the total number of bytes written.


int WsLibPing (struct WsLibStruct *wsptr,
               char *DataPtr,
               int DataCount)

   Ping the remote end with 0 to 125 bytes of opaque data by char pointer.


int WsLibPingDsc (struct WsLibStruct *wsptr,
                  struct dsc$descriptor_s *DataDsc)

   Ping the remote end with 0 to 125 bytes of opaque data by string descr.


int WsLibPong (struct WsLibStruct *wsptr,
               char *DataPtr,
               int DataCount)

   Ping the remote end with an unsolicted pong.


int WsLibSetAscii (struct WsLibStruct *wsptr)

   Set the message content to 8 bit ASCII (and yes, I realise ASCII is
   only 7 bit but I needed a representative yet intuitive name for the
   function that's less easily confused with WedSocket 'text' content).
   A WebSocket set WsLibSetAscii() is implicitly converted to and from
   UTF-8 during writes and reads with WebSocket messages sent as 'text'.


int WsLibSetBuffer (struct WsLibStruct *wsptr)

   Set the message content to be buffered internally before sending.
   This means the calling code does not need to maintain the underlying
   message buffer for the duration of the write.


int WsLibSetNoBuffer (struct WsLibStruct *wsptr)

   Resets use of the internal buffer.


int WsLibSetBinary (struct WsLibStruct *wsptr)

   Set the WebSocket message content to 'binary' and treat all reads and
   writes as opaque data.


int WsLibSetUtf8 (struct WsLibStruct *wsptr)

   Set the WebSocket message content to 'text' and treat all reads and
   writes as UTF-8 data (no implicit conversions).  Data must be legal
   UTF-8 or will/should be rejected by the local and/or remote end.


void WsLibSetUserData (struct WsLibStruct *wsptr,
                       void *UserDataPtr)

   Set the websocket structure to point to the supplied user data.


void* WsLibGetUserData (struct WsLibStruct *wsptr)

   Return the current pointer to the supplied user data.


void* WsLibSetCallout (struct WsLibStruct *wsptr,
                       void *AstFunction)

   Set/reset the callout response AST.
   Returns the previous callout pointer.


void* WsLibSetMsgCallback (struct WsLibStruct *wsptr,
                           void *CallbackFunction)

   Set/reset the wsLIB error reporting callback.
   Returns the previous callback pointer.
   The error callback function is activated with a pointer to the WsLib
   data structure that has the MsgLineNumber and timestamp set and
   provides a descriptive string.


int WsLibSetFrameMax (struct WsLibStruct *wsptr,
                      int FrameMax);

   Set the maximum sized frame before a message is fragmented.


void WsLibSetCloseSecs (struct WsLibStruct *wsptr,
                        int CloseSecs);

   Set number of seconds before an unresponded-to WebSocket close is
   considered closed and disconnected.
   If a WebSocket is not specified then set global value.


void WsLibSetIdleSecs (struct WsLibStruct *wsptr,
                       int IdleSecs);

   Set number of seconds before a WebSocket is considered idle and closed.
   If a WebSocket is not specified then set global value.


void WsLibSetLifeSecs (int LifeSecs);

   Set number of seconds before the application is considered idle and exited.


void WsLibSetPingSecs (struct WsLibStruct *wsptr,
                       int IdleSecs);

   Set number of seconds for periodic ping (heartbeat) of remote end.
   If a WebSocket is not specified then set global value.
   Remote end received a string containing and unsigned int ping number
   (1..n) followed by an unsigned int time in seconds (U*x epoch).


void WsLibSetReadSecs (struct WsLibStruct *wsptr,
                       int IdleSecs);

   Set number of seconds to wait for a read from a WebSocket before close.
   If a WebSocket is not specified then set global value.


void* WsLibSetWakeCallback (struct WsLibStruct *wsptr,
                            void *CallbackFunction,
                            int WakesSecs)

   Set/reset the wsLIB periodic wakeup callback.
   Returns the previous callback pointer.
   Set the number of seconds between wakeup calls (zero defaults).


int WsLibFromUtf8 (char *UtfPtr,
                   int UtfCount,
                   char SubsChar)

   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 or -1 if there is an error. 
   The input string is mangled if an error.


int WsLibFromUtf8Dsc (struct dsc$descriptor_s *InDsc,
                      struct dsc$descriptor_s *OutDsc,
                      char SubsChar)

   Given a descriptor of UTF-8 convert in-situ to 8 bit ASCII.
   The output descriptor must be provided even if it points to the same
   storage as the input descriptor.  Return the length of the converted
   string or -1 to indicated a conversion error.
   The input string is mangled if an error.


int WsLibToUtf8 (char *InPtr,
                 int InLength,
                 char *OutPtr,
                 int SizeOfOut)

   Given a buffer of 8 bit ASCII text convert it to UTF-8. 
   This can be done in-situ with the 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 strin or -1 if the insufficient buffer.


int WsLibToUtf8Dsc (struct dsc$descriptor_s *InDsc,
                    struct dsc$descriptor_s *OutDsc)

   Given a descriptor of 8 bit ASCII convert to 8 bit ASCII.
   Can be done in-situ.  The output descriptor must be provided even
   if it points to the same storage as the input descriptor.
   Return the length of the converted string or -1 to indicated a
   conversion error.


void WsLibWatchScript (struct WsLibStruct *wsptr,
                       char *SourceModuleName,
                       int SourceLineNumber,
                       char *FormatString,
                       ...)

   Outputs the $FAO-formatted string to the WASD WATCH [x]Script item.



COPYRIGHT
---------
Copyright (C) 2010-2021 Mark G.Daniel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Function Utf8Legal() contains code ...
Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.


VERSION HISTORY
---------------
20-JAN-2021  MGD  v1.2.0, VAX dependecies removed
                          WASD_WSLIB_WATCH required to WATCH_SCRIPT
10-DEC-2016  MGD  v1.1.1, bugfix; WsLibCreate() watch log file open
                          bugfix; WsLibWatchScript() multiple
18-AUG-2014  MGD  v1.1.0, WsLibOnNextRequest() asynchronous next request,
                          WsLibSetBuffer(), WsLibSetNoBuffer() set the
                            message to be buffered internally before sending
                            (obviating he need to maintain a static buffer)
27-JUL-2013  MGD  v1.0.7, bugfix; sigh! $setast() around critical I/Os
14-JUL-2013  MGD  v1.0.6, add queueing to read operations
                          bugfix; WriteAst() next message dequeue
02-JAN-2013  MGD  v1.0.5, Shut() limit call count before $CANCEL
                          bugfix; WsLibWrite(),WsLib_WriteAst() multiple
                            concurrent messages (each may require multiple
                            frames) need to be queued so that the frames (and
                            messages) arrive in-order! 
08-DEC-2012  MGD  tidied some #includes
23-SEP-2012  MGD  v1.0.4, "clean"-up response to client close
15-AUG-2012  MGD  v1.0.3, refine WRITEOF and channel destruction
                          bugfix; remove WATCH from Shut() :-}
21-JUL-2012  MGD  v1.0.2, refine shut and destruction sequence
                          add WASD_WSLIB_WATCH_LOG mechanism
26-NOV-2011  MGD  v1.0.1, refine I/O removing all $QIOW
                          WsLibClose() on all I/O non-success status
30-SEP-2011  MGD  v1.0.0, draft-ietf-hybi-thewebsocketprotocol-17
31-AUG-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-13
23-AUG-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-11
11-JUL-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-10
13-JUN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-09
07-JUN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-07 (BANG!)
25-FEB-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-06
05-FEB-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-05
11-JAN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-04
17-OCT-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-03
24-SEP-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-02
26-AUG-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-01 (initial)
*/
#endif /* COMMENTS_WITH_COMMENTS */
/*****************************************************************************/

#define SOFTWAREVN "1.2.0"
#define SOFTWARENM "WSLIB"
#ifdef __ALPHA
#  define SOFTWAREID SOFTWARENM " AXP-" SOFTWAREVN
#endif
#ifdef __ia64
#  define SOFTWAREID SOFTWARENM " IA64-" SOFTWAREVN
#endif
#ifdef __VAX
#  error VAX no longer implemented
#endif
#ifdef __x86_64
#  define SOFTWAREID SOFTWARENM " X86-" SOFTWAREVN
#endif

#include <ctype.h>
#include <ints.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <descrip.h>
#include <iodef.h>
#include <iosbdef.h>
#include <lib$routines.h>
#include <ssdef.h>
#include <starlet.h>
#include <stsdef.h>
#include <unixlib.h>

#include "wslib.h"

/* from libmsg.h */
#define LIB$_INVSTRDES 1409572

#ifndef EFN$C_ENF
#  define EFN$C_ENF 128  /* Event No Flag (no stored state) */
#endif

#define AGN$M_READONLY 0x01
#define AGN$M_WRITEONLY 0x02

#define VMSok(x) ((x) & STS$M_SUCCESS)
#define VMSnok(x) !(((x) & STS$M_SUCCESS))

#define MAX16BIT      65535
#define MAX32BIT 4294967295

#define WSLIB_STRUCT_BUFFER_SIZE 4096

#define FI_LI "WSLIB", __LINE__

#if 1
#define WATCH_WSLIB if(wsptr->WatchScript)WsLibWatchScript
#else
#define WATCH_WSLIB if(0)WsLibWatchScript
#endif

/* used by WSLIBCL.C */
static int  EfnWait,
            EfnNoWait;

static int  CalloutDone = 0,
            CgiPlusEofLength = 0,
            CgiPlusEotLength = 0,
            CgiPlusEscLength = 0,
            CgiVariablesAvailable = 0,
            StructBufferLength = 0,
            StructBufferSize = 0,
            WwwPrefix = 0;

static unsigned short  CgiPlusInChannel;

static unsigned long  CurrentTime,
                      WatchDogWakeTime;
static unsigned long  CurrentBinTime [2];

#define DEFAULT_WATCHDOG_CLOSE_SECS  5
#define DEFAULT_WATCHDOG_IDLE_SECS 120
#define DEFAULT_WATCHDOG_LIFE_SECS 120
#define DEFAULT_WATCHDOG_PING_SECS 600
#define DEFAULT_WATCHDOG_READ_SECS  60
#define DEFAULT_WATCHDOG_WAKE_SECS  60

static unsigned long  WatchDogCloseSecs = DEFAULT_WATCHDOG_CLOSE_SECS,
                      WatchDogIdleSecs = DEFAULT_WATCHDOG_IDLE_SECS,
                      WatchDogLifeSecs = DEFAULT_WATCHDOG_LIFE_SECS,
                      WatchDogPingSecs = DEFAULT_WATCHDOG_PING_SECS,
                      WatchDogReadSecs = DEFAULT_WATCHDOG_READ_SECS,
                      WatchDogWakeSecs = DEFAULT_WATCHDOG_WAKE_SECS;

static char  *CgiPlusEofPtr = NULL,
             *CgiPlusEotPtr = NULL,
             *CgiPlusEscPtr = NULL,
             *NextVarNamePtr = NULL,
             *StructBufferPtr = NULL;

static struct WsLibStruct *ListHead; 

static void  (*RequestSynchronisation)() = NULL,
             (*PongCallbackFunction)() = NULL,
             (*WakeCallbackFunction)() = NULL;

static char  SoftwareID [] = SOFTWAREID;

#define SYI$_VERSION 4096
int sys$getsyi(__unknown_params);

/*****************************************************************************/
/*
Initialise the library.
*/

void WsLibInit ()

{
   static char  GetSyiVer [8];
   static struct
   {
      unsigned short  buf_len;
      unsigned short  item;
      void  *buf_addr;
      void  *ret_len;
   } SyiItem [] =
   {
     { sizeof(GetSyiVer)-1, SYI$_VERSION, &GetSyiVer, 0 },
     { 0,0,0,0 }
   };

   int  status,
        VersionInteger;

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

   /* just the once! */
   if (EfnWait) return;

   status = sys$getsyiw (0, 0, 0, &SyiItem, 0, 0, 0);
   if (VMSnok (status)) WsLibExit (NULL, FI_LI, status);
   VersionInteger = ((GetSyiVer[1]-48) * 100) + ((GetSyiVer[3]-48) * 10);
   if (GetSyiVer[4] == '-') VersionInteger += GetSyiVer[5]-48;
   if (VersionInteger >= 700)
      EfnWait = EfnNoWait = EFN$C_ENF;
   else
   {
      if (VMSnok (status = lib$get_ef (&EfnWait)))
         WsLibExit (NULL, FI_LI, status);;
      if (VMSnok (status = lib$get_ef (&EfnNoWait)))
         WsLibExit (NULL, FI_LI, status);;
   }

   WatchDog ();
}

/*****************************************************************************/
/*
Return a pointer to the wsLIB version string.
*/

char* WsLibVersion ()

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

   return (SoftwareID);
}

/*****************************************************************************/
/*
Sanity check the incoming request.  Provide error or continue/upgrade response.
Allocate a WebSocket I/O structure and set the internal user data storage. 
Insert at the head of the list.  Return a pointer to the allocated structure.
Can only WATCH_WSLIB after WsLibOpen().
*/

struct WsLibStruct* WsLibCreate
(
void *UserDataPtr,
void *DestroyFunction
)
{
   int  astatus,
        SecWebSocketVersion;
   char  *cptr, *sptr;
   struct WsLibStruct  *wsptr;

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

   if (!EfnWait) WsLibInit ();

   astatus = sys$setast (0); 

   wsptr = (struct WsLibStruct*) calloc (1, sizeof(struct WsLibStruct));
   if (!wsptr) WsLibExit (NULL, FI_LI, vaxc$errno);

   if ((cptr = getenv ("WASD_WSLIB_WATCH_LOG")) != NULL)
      if ((wsptr->WatchLog = fopen (cptr, "w", "shr=get")) == NULL)
         WsLibExit (NULL, FI_LI, vaxc$errno);

   /* if a scripting application running under the server */
   if (WsLibCgiVarNull ("SERVER_SOFTWARE"))
   {
      if (cptr = WsLibCgiVarNull("WEBSOCKET_INPUT_MRS"))
         wsptr->InputMrs = atoi(cptr);
      else
         WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      if (cptr = WsLibCgiVarNull("WEBSOCKET_OUTPUT_MRS"))
         wsptr->OutputMrs = atoi(cptr);
      else
         WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      if (!(cptr = WsLibCgiVarNull("HTTP_SEC_WEBSOCKET_VERSION")))
         WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      SecWebSocketVersion = atoi(cptr);
      if (SecWebSocketVersion <= 0) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      /* this logical name is also detected by the WASD server */
      if (!(sptr = getenv("WASD_WEBSOCKET_VERSION")))
         /* this is the wsLIB header string */
         sptr = WSLIB_WEBSOCKET_VERSION;
      while (*sptr)
      {
         if (atoi(sptr) == SecWebSocketVersion) break;
         while (isdigit(*sptr)) sptr++;
         while (*sptr && !isdigit(*sptr)) sptr++;
      }

      if (!*sptr)
      {
         /* WebSockets version not supported */
         fprintf (stdout,
"Status: 426 Upgrade Required\r\n\
Sec-Websocket-Version: %s\r\n\
\r\n",
                  WSLIB_WEBSOCKET_VERSION);
         fflush (stdout);
         free (wsptr);
         return (NULL);
      }

      wsptr->WebSocketVersion = SecWebSocketVersion;

      /* connection acceptance response */
      fprintf (stdout, "Status: 101 Switching Protocols\r\n\r\n");
      fflush (stdout);
   }
   else
   {
      /* first number listed in the macro should be the current version */
      wsptr->WebSocketVersion = atoi (WSLIB_WEBSOCKET_VERSION);

      /* the maximum socket record size is the maximum $QIO size */
      wsptr->InputMrs = wsptr->OutputMrs = MAX16BIT;
   }

   wsptr->FrameMaxSize = MAX32BIT;
   wsptr->UserDataPtr = UserDataPtr;
   wsptr->DestroyAstFunction = DestroyFunction;
   wsptr->NextPtr = ListHead;
   ListHead = wsptr;

   if (astatus == SS$_WASSET) sys$setast (1);
   return (wsptr);
}

/*****************************************************************************/
/*
Deprecated.  Now is just a stub.
*/

void* WsLibDestroy (struct WsLibStruct *wsptr)

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

   return (wsptr->UserDataPtr);
}

/*****************************************************************************/
/*
Remove from the list and free the allocated memory.  Return any user data.
*/

static void Destroy (struct WsLibStruct *wsptr)

{
   int  astatus, cnt, status;
   struct WsLibStruct  *wslptr;
   void  *UserDataPtr;
   FILE  *WatchLog;

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

   if (!wsptr) return;

   WATCH_WSLIB (wsptr, FI_LI, "DESTROY");

   WatchLog = wsptr->WatchLog;

   astatus = sys$setast (0); 
   UserDataPtr = wsptr->UserDataPtr;

   if (wsptr->InBufferSize) free (wsptr->InBufferPtr);
   if (wsptr->OutBufferSize) free (wsptr->OutBufferPtr);
   if (wsptr->MsgStringSize) free (wsptr->MsgStringPtr);
   if (wsptr->ClientHeaderSize)
   {
      /* free WLIBCL.C storage */
      free (wsptr->ClientHeaderPtr);
      if (wsptr->ClientAcceptSize) free (wsptr->ClientAcceptPtr);
      if (wsptr->ClientKeySize) free (wsptr->ClientKeyPtr);
      if (wsptr->ClientServerSize) free (wsptr->ClientServerPtr);
      if (wsptr->ClientUriSize) free (wsptr->ClientUriPtr);
   }

   if ((wslptr = ListHead) == wsptr)
      ListHead = wslptr->NextPtr;
   else
   {
      while (wslptr->NextPtr != wsptr) wslptr = wslptr->NextPtr;
      wslptr->NextPtr = wsptr->NextPtr;
   }

   if (!wsptr->SocketChannel && wsptr->OutputChannel)
      sys$dassgn (wsptr->OutputChannel);

   free (wsptr);
   if (astatus == SS$_WASSET) sys$setast (1);

   if (WatchLog) fclose (WatchLog);
}

/*****************************************************************************/
/*
Step through the list of structures.  WsLibCtx is the address of a pointer used
to hold the context.  Set to NULL to initialise.  Returns non-null pointers for
each structure in the list, then NULL when list exhausted, to begin non-null
pointers again.  Care must be exercised that multiple calls are not preempted
by a list modification (i.e. use within AST delivery or with ASTs disabled).
*/

struct WsLibStruct* WsLibNext (struct WsLibStruct **WsLibCtx)
                                                    
{
   int  astatus;
   struct WsLibStruct  *wsptr, *wslptr;

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

   astatus = sys$setast (0); 
   /* let's be overcautious and make sure it's still in the list! */
   if (wsptr = *WsLibCtx)
   {
      for (wslptr = ListHead; wslptr; wslptr = wslptr->NextPtr)
         if (wslptr == wsptr) break;
      if (!wslptr) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
      *WsLibCtx = wsptr->NextPtr;
   }
   else
      *WsLibCtx = ListHead;
   if (astatus == SS$_WASSET) sys$setast (1);
   return (*WsLibCtx);
}

/*****************************************************************************/
/*
Using the device names from the CGI variables WEBSOCKET_INPUT and
WEBSOCKET_OUTPUT assign channels in preparation for asynchronous I/O.
The AST function is called at WebSocket close.
Default data is 8 bit ASCII (that requires implicit UTF-8 encoding).
*/

int WsLibOpen (struct WsLibStruct *wsptr)

{
   int  status;
   char  *cptr, *sptr, *zptr;
   $DESCRIPTOR (MbxDsc, "");

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

   wsptr->InputDataDsc.dsc$b_class = 
      wsptr->OutputDataDsc.dsc$b_class = DSC$K_CLASS_S;
   wsptr->InputDataDsc.dsc$b_dtype =
      wsptr->OutputDataDsc.dsc$b_dtype = DSC$K_DTYPE_T;

   if (!(cptr = WsLibCgiVarNull("WEBSOCKET_INPUT"))) return (SS$_BUGCHECK);
   zptr = (sptr = wsptr->InputDevName) + sizeof(wsptr->InputDevName)-1;
   while (*cptr && sptr < zptr) *sptr++ = *cptr++;
   *sptr = '\0';

   wsptr->InputDevDsc.dsc$b_class = DSC$K_CLASS_S;
   wsptr->InputDevDsc.dsc$b_dtype = DSC$K_DTYPE_T;
   wsptr->InputDevDsc.dsc$a_pointer = wsptr->InputDevName;
   wsptr->InputDevDsc.dsc$w_length = sptr - wsptr->InputDevName;

   if (!(cptr = WsLibCgiVarNull("WEBSOCKET_OUTPUT"))) return (SS$_BUGCHECK);
   zptr = (sptr = wsptr->OutputDevName) + sizeof(wsptr->OutputDevName)-1;
   while (*cptr && sptr < zptr) *sptr++ = *cptr++;
   *sptr = '\0';

   wsptr->OutputDevDsc.dsc$b_class = DSC$K_CLASS_S;
   wsptr->OutputDevDsc.dsc$b_dtype = DSC$K_DTYPE_T;
   wsptr->OutputDevDsc.dsc$a_pointer = wsptr->OutputDevName;
   wsptr->OutputDevDsc.dsc$w_length = sptr - wsptr->OutputDevName;

   status = sys$assign (&wsptr->InputDevDsc, &wsptr->InputChannel, 0, 0,
                        AGN$M_READONLY);
   if (VMSnok (status)) return (status);

   status = sys$assign (&wsptr->OutputDevDsc, &wsptr->OutputChannel, 0, 0,
                        AGN$M_WRITEONLY);
   if (VMSnok (status))
   {
      sys$dassgn (wsptr->InputChannel);
      wsptr->InputChannel = 0;
      return (status);
   }

   if (VMSnok (status))
   {
      sys$dassgn (wsptr->InputChannel);
      sys$dassgn (wsptr->OutputChannel);
      wsptr->InputChannel = wsptr->OutputChannel = 0;
      return (status);
   }

   /* default data is 8 bit "ASCII" text (requiring implicit UTF-8 encoding) */
   wsptr->SetAscii = 1;

   if (!(wsptr->WatchScript = (wsptr->WatchLog != NULL)))
      if (getenv ("WASD_WSLIB_WATCH") || getenv ("WASD_WSLIB_WATCH_LOG"))
         wsptr->WatchScript = (WsLibCgiVarNull("WATCH_SCRIPT") != NULL);

   if (wsptr->WatchDogPingSecs)
      wsptr->WatchDogPingTime = CurrentTime + wsptr->WatchDogPingSecs;

   WATCH_WSLIB (wsptr, FI_LI, "OPEN !AZ", SOFTWAREID);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
Initiate a close from the application end.
Default status code is 1000 (normal closure).
To suppress any status code delivery specify -1.
WatchDog will shut if close doesn't happen in course.
*/

void WsLibClose
(
struct WsLibStruct *wsptr,
int StatusCode,
char *StatusString
)
{
   static char  DummyBuffer [125];

   int  astatus, status,
        FramePayload;
   unsigned char  *aptr, *cptr, *sptr, *zptr;
   struct WsLibFrmStruct  *frmptr;
   struct WsLibMsgStruct  *msgptr;

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

   WATCH_WSLIB (wsptr, FI_LI, "CLOSE closed:!UL code:!SL \"!AZ\"",
                wsptr->WebSocketClosed, StatusCode,
                StatusString ? StatusString : "(null)");

   if (wsptr->WebSocketClosed)
   {
      Shut (wsptr);
      return;
   }

   wsptr->WebSocketClosed = 1;

   /* allocate a pointer plus a structure (freed by OutputFreeAst()) */ 
   aptr = calloc (1, sizeof(struct WsLibStruct*) +
                     sizeof(struct WsLibFrmStruct));
   if (!aptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   *(struct WsLibStruct**)aptr = wsptr;
   frmptr = (struct WsLibFrmStruct*)(aptr + sizeof(struct WsLibStruct*));

   if (!StatusCode)
      StatusCode = WSLIB_CLOSE_NORMAL;
   else
   if (StatusCode == WSLIB_CLOSE_BANG)
      StatusCode = 0;

   if (!StatusString)
   {
      switch (StatusCode)
      {
         case 0    : break;
         case 1000 : StatusString = "normal closure"; break;
         case 1001 : StatusString = "bye-bye"; break;
         case 1002 : StatusString = "protocol error"; break;
         case 1003 : StatusString = "received data unacceptable"; break;
         case 1004 : StatusString = "RESERVED"; break;
         case 1005 : StatusString = "RESERVED"; break;
         case 1006 : StatusString = "RESERVED"; break;
         case 1007 : StatusString = "received data inconsistency"; break;
         case 1008 : StatusString = "policy violation"; break;
         case 1009 : StatusString = "received message too big"; break;
         case 1010 : StatusString = "expected extention negotiation"; break;
         case 1011 : StatusString = "unexpected condition"; break;
         default   : StatusString = "unknown opcode";
      }
   }

   FramePayload = 0;
   frmptr->FrameHeader[0] = WSLIB_BIT_FIN | WSLIB_OPCODE_CLOSE;
   if (wsptr->RoleClient)
   {
      /* little messier with masking required */
      MaskingKey (frmptr);
      frmptr->FrameHeader[2] = frmptr->MaskingKey[0];
      frmptr->FrameHeader[3] = frmptr->MaskingKey[1];
      frmptr->FrameHeader[4] = frmptr->MaskingKey[2];
      frmptr->FrameHeader[5] = frmptr->MaskingKey[3];
      if (StatusCode)
      {
         int  kcnt = 0;
         char  *kptr = (char*)frmptr->MaskingKey;
         frmptr->FrameHeader[6] =
            ((StatusCode & 0xff00) >> 8) ^ kptr[kcnt++&0x3];
         frmptr->FrameHeader[7] = (StatusCode & 0xff) ^ kptr[kcnt++&0x3];
         FramePayload = 2;
         if (StatusString)
         {
            zptr = (sptr = frmptr->FrameHeader + 8) + 123;
            for (cptr = (unsigned char*)StatusString;
                 *cptr && sptr < zptr;
                 *sptr++ = *cptr++ ^ kptr[kcnt++&0x3])
               FramePayload++;
         }
      }
      frmptr->FrameHeader[1] = frmptr->FrameMaskBit | FramePayload;
      FramePayload += 6;
   }
   else
   {
      if (StatusCode)
      {
         frmptr->FrameHeader[2] = (StatusCode & 0xff00) >> 8;
         frmptr->FrameHeader[3] = StatusCode & 0xff;
         FramePayload = 2;
         if (StatusString)
         {
            zptr = (sptr = frmptr->FrameHeader + 4) + 123;
            for (cptr = (unsigned char*)StatusString;
                 *cptr && sptr < zptr;
                 *sptr++ = *cptr++)
               FramePayload++;
         }
      }
      frmptr->FrameHeader[1] = FramePayload;
      FramePayload += 2;
   }

   astatus = sys$setast (0);
   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_WRITELBLK | IO$M_READERCHECK,
                     0, OutputFreeAst, aptr,
                     frmptr->FrameHeader, FramePayload, 0, 0, 0, 0);
   if (VMSok(status)) wsptr->QueuedOutput++;
   if (astatus == SS$_WASSET) sys$setast (1);

   if (StatusCode == WSLIB_CLOSE_NORMAL ||
       StatusCode == WSLIB_CLOSE_BYEBYE ||
       StatusCode == WSLIB_CLOSE_POLICY)
   {
      /************************************/
      /* receive any close response frame */
      /************************************/

      msgptr = calloc (1, sizeof(struct WsLibMsgStruct));
      if (!msgptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
      msgptr->WsLibPtr = wsptr;

      msgptr->DataMax = MAX32BIT;
      msgptr->DataPtr = DummyBuffer;
      msgptr->DataSize = sizeof(DummyBuffer);
      msgptr->AstFunction = DummyClose;
   
      ReadFrame (msgptr);
   }
   else
   {
      /*********************/
      /* significant error */
      /*********************/

      /* e.g. protocol error, do not try to continue */
      Shut (wsptr);
      return;
   }
}

/*****************************************************************************/
/*
Essentially just a dummy target for the dummy read initiated following an
application WebSocket close, a read that allows any client response close frame
to be received and processed.
*/

static void DummyClose (struct WsLibStruct *wsptr)

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

   WATCH_WSLIB (wsptr, FI_LI, "CLOSE response %X!8XL", wsptr->InputStatus);
}

/*****************************************************************************/
/*
Respond to a close opcode from the client.
*/

static void Close (struct WsLibFrmStruct *frmptr)

{
   int  status,
        CloseStatus,
        FramePayload;
   struct WsLibStruct  *wsptr;

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

   wsptr = frmptr->WsLibMsgPtr->WsLibPtr;

   if (frmptr->DataCount >= 2)
   {
      CloseStatus = ((unsigned char)frmptr->DataPtr[0] << 8) +
                     (unsigned char)frmptr->DataPtr[1];
      frmptr->DataCount -= 2;
   }
   else
      CloseStatus = 0;

   WATCH_WSLIB (wsptr, FI_LI, "CLOSE code:!UL!AZ!#AZ",
                CloseStatus, frmptr->DataCount ? " " : "",
                frmptr->DataCount, frmptr->DataPtr+2);

   if (frmptr->DataCount)
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "CLOSE !UL !#AZ",
                          CloseStatus, frmptr->DataCount, frmptr->DataPtr+2);
   else
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "CLOSE 0");

   if (!wsptr->WebSocketClosed)
   {
      /* send the close opcode */
      wsptr->WebSocketClosed = 1;

      WATCH_WSLIB (wsptr, FI_LI, "CLOSE response");

      /* allocate a frame structure (freed by CloseFreeAst()) */ 
      frmptr = (struct WsLibFrmStruct*)calloc(1,sizeof(struct WsLibFrmStruct));
      if (!frmptr) WsLibExit (wsptr, FI_LI, vaxc$errno);

      /* close frame header */
      frmptr->FrameHeader[0] = WSLIB_BIT_FIN | WSLIB_OPCODE_CLOSE;
      frmptr->FrameHeader[1] = 0;

      /* send the opcode asynchronously delivering to a specific AST */
      status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                        IO$_WRITELBLK | IO$M_READERCHECK,
                        0, CloseFreeAst, frmptr,
                        frmptr->FrameHeader, 2, 0, 0, 0, 0);
      /* immediately do the WebSocket shutdown, resulting a "clean" close */
   }

   Shut (wsptr); 
}

/*****************************************************************************/
/*
Just deallocate the memory containing the close frame from Close().
*/

void CloseFreeAst (struct WsLibFrmStruct *frmptr)

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

   free (frmptr);
}

/*****************************************************************************/
/*
Return true if the WebSocket has been closed.
*/

int WsLibIsClosed (struct WsLibStruct *wsptr)

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

   return (wsptr->WebSocketClosed);
}

/*****************************************************************************/
/*
Shutdown the websocket (this is different to the close handshake).
Cancel any outstanding I/O and deassign channels.
Call any supplied closure AST function.
This function call be called multiple times before full shutdown accomplished.
Returns success status when finally shut, abort if not (fully) shut (yet).
*/

int Shut (struct WsLibStruct *wsptr)

{
   int  astatus, status;

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

   if (wsptr->WatchLog)
      WATCH_WSLIB (wsptr, FI_LI,
"Shut() destroy:!8XL(!UL) shut:!UL closed:!UL input:!UL output:!UL(!UL)",
                   wsptr->DestroyAstFunction,
                   wsptr->DestroyAstFunction == &Destroy,
                   wsptr->WebSocketShut, wsptr->WebSocketClosed,
                   wsptr->QueuedInput, wsptr->QueuedOutput, wsptr->ShutCount);

   if (wsptr->DestroyAstFunction == &Destroy) return (SS$_NORMAL);

   if (!wsptr->WebSocketShut)
   {
      if (wsptr->QueuedInput) sys$cancel (wsptr->InputChannel);

      if (!wsptr->SocketChannel)
      {
         /* let the server know the client is being disconnected */
         astatus = sys$setast (0);
         status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                           IO$_WRITEOF | IO$M_NORSWAIT, 0,
                           WriteEofAst, wsptr,
                           0, 0, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedOutput++;
         if (astatus == SS$_WASSET) sys$setast (1);
      }

      /* can be shut without having been closed (e.g. network error) */
      wsptr->WebSocketShut = wsptr->WebSocketClosed = 1;
   }
   else
   if (wsptr->QueuedOutput)
      if (wsptr->ShutCount++ > wsptr->QueuedOutput)
         sys$cancel (wsptr->OutputChannel);

   /* if outstanding I/O */
   if (wsptr->QueuedInput || wsptr->QueuedOutput) return (SS$_ABORT);

   if (wsptr->SocketChannel)
   {
      /* client interface in use */
      sys$dassgn (wsptr->SocketChannel);
      wsptr->InputChannel = wsptr->OutputChannel = wsptr->SocketChannel = 0;
   }
   else
   {
      /* deassign of output channel is handled during destroy */
      sys$dassgn (wsptr->InputChannel);
      wsptr->InputChannel = 0;
   }

   /* first queue any client's destruction code */
   if (wsptr->DestroyAstFunction)
      sys$dclast (wsptr->DestroyAstFunction, wsptr, 0, 0);

   /* then queue the wsLIB structure destruction */
   sys$dclast ((wsptr->DestroyAstFunction = &Destroy), wsptr, 0, 0);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
Decrement the queued I/O counter and then recall the shut function.
*/

static void WriteEofAst (struct WsLibStruct *wsptr)

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

   if (wsptr->QueuedOutput)
      wsptr->QueuedOutput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   Shut (wsptr);
}

/*****************************************************************************/
/*
Send a ping to the client using the (optional) data pointed to by the supplied
string descriptor.  For the responding pong to be notified a
WsLibSetPongCallback() must previously have been set.
*/

int WsLibPingDsc
(
struct WsLibStruct *wsptr,
struct dsc$descriptor_s *DataDsc
)
{
   int  status;

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

   if (DataDsc == NULL)
      status = WsLibPing (wsptr, NULL, 0);
   else
   if (DataDsc->dsc$b_class != DSC$K_CLASS_S &&
       DataDsc->dsc$b_dtype != DSC$K_DTYPE_T)
      status = LIB$_INVSTRDES;
   else
      status = PingPong (wsptr,
                                DataDsc->dsc$a_pointer,
                                DataDsc->dsc$w_length,
                                WSLIB_OPCODE_PING);
   return (status);
}

/*****************************************************************************/
/*
Wrapper for PingPong() to send a ping.
*/

int WsLibPing
(
struct WsLibStruct *wsptr,
char *DataPtr,
int DataCount
)
{
   /*********/
   /* begin */
   /*********/

   return (PingPong (wsptr, DataPtr, DataCount, WSLIB_OPCODE_PING));
}

/*****************************************************************************/
/*
Wrapper for PingPong() to send an unsolicted pong.
*/

int WsLibPong
(
struct WsLibStruct *wsptr,
char *DataPtr,
int DataCount
)
{
   /*********/
   /* begin */
   /*********/

   return (PingPong (wsptr, DataPtr, DataCount, WSLIB_OPCODE_PONG));
}

/*****************************************************************************/
/*
Send a ping/pong to the client using the (optional) data supplied.  For the
responding pong to be notified a SetPongCallback() must have been set.
*/

static int PingPong
(
struct WsLibStruct *wsptr,
char *DataPtr,
int DataCount,
int OpCode
)
{
   int  astatus, cnt, hcnt, status;
   char  *aptr;
   struct WsLibFrmStruct  *frmptr;

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

   if (OpCode == WSLIB_OPCODE_PING)
      WATCH_WSLIB (wsptr, FI_LI, "PING");
   else
      WATCH_WSLIB (wsptr, FI_LI, "PONG");

   if (wsptr->WebSocketClosed)
   {
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "can't ping; closed");
      return (SS$_SHUT);
   }

   if (DataCount > 125) DataCount = 125;

   /* allocate a pointer plus a structure (freed by OutputFreeAst()) */ 
   aptr = calloc (1, sizeof(struct WsLibStruct*) +
                     sizeof(struct WsLibFrmStruct));
   if (!aptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   *(struct WsLibStruct**)aptr = wsptr;
   frmptr = (struct WsLibFrmStruct*)(aptr + sizeof(struct WsLibStruct*));

   hcnt = 0;
   frmptr->FrameHeader[hcnt++] = WSLIB_BIT_FIN | OpCode;
   if (wsptr->RoleClient)
   {
      MaskingKey (frmptr);
      frmptr->FrameHeader[hcnt++] = frmptr->FrameMaskBit | DataCount;
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[0];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[1];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[2];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[3];
      for (cnt = 0; cnt < DataCount; cnt++)
         frmptr->FrameHeader[hcnt+cnt] =
            DataPtr[cnt] ^ frmptr->MaskingKey[frmptr->MaskCount++&0x3];
   }
   else
   {
      frmptr->FrameHeader[hcnt++] = DataCount;
      for (cnt = 0; cnt < DataCount; cnt++)
         frmptr->FrameHeader[hcnt+cnt] = DataPtr[cnt];
   }

   astatus = sys$setast (0);
   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_WRITELBLK | IO$M_READERCHECK,
                     0, OutputFreeAst, aptr,
                     frmptr->FrameHeader, hcnt+cnt, 0, 0, 0, 0);
   if (VMSok(status)) wsptr->QueuedOutput++;
   if (astatus == SS$_WASSET) sys$setast (1);

   return (status);
}

/*****************************************************************************/
/*
A ping header has been detected.  Return a pong frame.
*/

static void Pong (struct WsLibFrmStruct *frmptr)

{
   int  astatus, cnt, hcnt, status,
        DataCount;
   char  *aptr,
         *DataPtr;
   struct WsLibStruct  *wsptr;

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

   wsptr = frmptr->WsLibMsgPtr->WsLibPtr;

   WATCH_WSLIB (wsptr, FI_LI, "PONG !UL", frmptr->DataCount);

   if (wsptr->WebSocketClosed)
   {
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "can't pong; closed");
      return;
   }

   /* retrieve any pinged data (max is 125 bytes) */
   DataPtr = frmptr->DataPtr;
   if ((DataCount = frmptr->DataCount) > 125) DataCount = 125;

   /* allocate a pointer plus a structure (freed by OutputFreeAst()) */ 
   aptr = calloc (1, sizeof(struct WsLibStruct*) +
                     sizeof(struct WsLibFrmStruct));
   if (!aptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   *(struct WsLibStruct**)aptr = wsptr;
   frmptr = (struct WsLibFrmStruct*)(aptr + sizeof(struct WsLibStruct*));

   hcnt = 0;
   frmptr->FrameHeader[hcnt++] = WSLIB_BIT_FIN | WSLIB_OPCODE_PONG;
   if (wsptr->RoleClient)
   {
      MaskingKey (frmptr);
      frmptr->FrameHeader[hcnt++] = frmptr->FrameMaskBit | DataCount;
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[0];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[1];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[2];
      frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[3];
      for (cnt = 0; cnt < DataCount; cnt++)
         frmptr->FrameHeader[hcnt+cnt] = DataPtr[cnt] ^
                                 frmptr->MaskingKey[frmptr->MaskCount++&0x3];
   }
   else
   {
      frmptr->FrameHeader[hcnt++] = DataCount;
      for (cnt = 0; cnt < DataCount; cnt++)
         frmptr->FrameHeader[hcnt+cnt] = DataPtr[cnt];
   }

   astatus = sys$setast (0);
   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_WRITELBLK | IO$M_READERCHECK,
                     0, OutputFreeAst, aptr,
                     frmptr->FrameHeader, hcnt+cnt, 0, 0, 0, 0);
   if (VMSok(status)) wsptr->QueuedOutput++;
   if (astatus == SS$_WASSET) sys$setast (1);

   if (VMSnok(status))
   {
      MsgCallback (wsptr, __LINE__, status, "pong");
      return;
   }
}

/*****************************************************************************/
/*
Return true if both input and output channel connected.
*/

int WsLibConnected (struct WsLibStruct *wsptr)

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

   return (wsptr->InputChannel && wsptr->OutputChannel);
}

/*****************************************************************************/
/*
Read data from the WebSocket client using a descriptor to describe the input
buffer.  If supplied the 'ReadDsc' is populated (pointer and length adjusted)
on a successful read.  If 'DataDsc' is NULL then a dynamic buffer with a
maximum size of 65535 will be allocated as per description in WsLibRead().
*/

int WsLibReadDsc
(
struct WsLibStruct *wsptr,
struct dsc$descriptor_s *DataDsc,
struct dsc$descriptor_s *ReadDsc,
void *AstFunction
)
{
   int  status;
   struct dsc$descriptor_s  ScratchDsc;

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

   if (!DataDsc)
   {
      DataDsc = &ScratchDsc;
      DataDsc->dsc$b_class = DSC$K_CLASS_S;
      DataDsc->dsc$b_dtype = DSC$K_DTYPE_T;
      DataDsc->dsc$a_pointer = NULL;
      DataDsc->dsc$w_length = MAX16BIT;
   }

   if (DataDsc->dsc$b_class != DSC$K_CLASS_S &&
       DataDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   if (ReadDsc->dsc$b_class != DSC$K_CLASS_S &&
       ReadDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   wsptr->ReadDscPtr = ReadDsc;
   status = WsLibRead (wsptr, DataDsc->dsc$a_pointer,
                       DataDsc->dsc$w_length, AstFunction);
   return (status);
}

/*****************************************************************************/
/*
Read a message from WEBSOCKET_INPUT.  A message can comprise of multiple frames
if the message is fragmented.  If an AST function is supplied then the read is
asynchronous, otherwise blocking.  When the read is complete the status can be
returned by WsLibReadStatus(), the read count by WsLibReadCount(), a pointer to
the data buffer using WsLibReadData(), and a pointer to the read string
descriptor with WsLibReadDataDsc().  A data pointer to NULL can be supplied in
which case data size becomes the maximum allowed frame size (if zero there is
no limit) and an appropriately sized buffer is dynamically allocated (and
deallocated) with each frame read.
*/

int WsLibRead
(
struct WsLibStruct *wsptr,
char *DataPtr,
int DataSize,
void *AstFunction
)
{
   struct WsLibMsgStruct  *msgptr,
                          *nsgptr;

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

   WATCH_WSLIB (wsptr, FI_LI, "READ size:!UL", DataSize);

   if (wsptr->WebSocketClosed)
   {
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "can't read; closed");
      wsptr->InputStatus = SS$_SHUT;
      if (AstFunction) ((void(*)())AstFunction)(wsptr);
      return (SS$_SHUT);
   }

   msgptr = calloc (1, sizeof(struct WsLibMsgStruct));
   if (!msgptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   msgptr->WsLibPtr = wsptr;

   if (DataPtr)
      msgptr->DataMax = 0;
   else
   {
      /* the maxiumum sized message allowed to be buffered */
      if (DataSize)
         msgptr->DataMax = DataSize;
      else
         msgptr->DataMax = MAX32BIT;
   }

   msgptr->DataPtr = DataPtr;
   msgptr->DataSize = DataSize;
   msgptr->AstFunction = AstFunction;
   
   if (wsptr->MsgReadQuePtr)
   {
      /* append this one to the read queue */
      for (nsgptr = wsptr->MsgReadQuePtr;
           nsgptr->MsgQuePtr;
           nsgptr = nsgptr->MsgQuePtr);
      nsgptr->MsgQuePtr = msgptr;
   }
   else
   {
      /* only this one in the queue */
      wsptr->MsgReadQuePtr = msgptr;
      /* either kick-off asynchronous I/O or return after blocking I/O */
      ReadFrame (msgptr);
   }

   return (wsptr->InputStatus);
}

/*****************************************************************************/
/*
Read a frame (can be a fragment).
Read six bytes.  See ReadHeader1Ast() for breakdown.
*/

static void ReadFrame (struct WsLibMsgStruct *msgptr)

{
   struct WsLibFrmStruct  *frmptr;
   struct WsLibStruct *wsptr;

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

   wsptr = msgptr->WsLibPtr;

   WATCH_WSLIB (wsptr, FI_LI, "READ frame");

   frmptr = &msgptr->FrameData;
   if (frmptr->IOsb.iosb$w_status)
      memset (frmptr, 0, sizeof(struct WsLibFrmStruct));
   frmptr->WsLibMsgPtr = msgptr;

   /* if the client interface is in use then it's TCP/IP */
   if (wsptr->SocketChannel)
      /* not really needed on a TCP connection but seems to do no harm! */
      frmptr->IoRead = IO$_READLBLK | IO$M_WRITERCHECK;
   else
      frmptr->IoRead = IO$_READLBLK | IO$M_STREAM | IO$M_WRITERCHECK;

   if (wsptr->RoleClient)
      frmptr->ReadSize = 2;
   else
      frmptr->ReadSize = 6;

   frmptr->IOsb.iosb$w_bcnt = 0;
   frmptr->IOsb.iosb$w_status = SS$_NORMAL;
   wsptr->QueuedInput++;
   ReadHeader1Ast (frmptr);
}

/*****************************************************************************/
/*
First two or six bytes of the frame have been read.

      0                   1                   2                   3
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     +-+-+-+-+-------+-+-------------+-------------------------------+
     |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
     |I|S|S|S|  (4)  |A|     (7)     |             (16/63)           |
     |N|V|V|V|       |S|             |   (if payload len==126/127)   |
     | |1|2|3|       |K|             |                               |
     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
     |     Extended payload length continued, if payload len == 127  |
     + - - - - - - - - - - - - - - - +-------------------------------+
     |                               |Masking-key, if MASK set to 1  |
     +-------------------------------+-------------------------------+
     | Masking-key (continued)       |          Payload Data         |
     +-------------------------------- - - - - - - - - - - - - - - - +
     :                     Payload Data continued ...                :
     + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
     |                     Payload Data continued ...                |
     +---------------------------------------------------------------+

These can be ...

1) the 9+7 header bits, includes payload length if less than 126 bytes.

2) the 9+7 header bits, includes payload length if less than 126 bytes, plus
any 32 bit masking-key.

3) the 9+7 header bits, plus the word of the 16 bit extended payload length,
plus the first two bytes of any masking-key.

4) the 9+7 header bits, plus the first 4 bytes of the 64 bit extended payload
length.

If the header payload length is less than 126 then the header is complete, if
126 then read another 4 bytes to get the  masking-key, if 127 then read another
10 bytes to complete the extended (64 bit) payload length and to get the
masking-key.

Perform header sanity checking.
*/

static void ReadHeader1Ast (struct WsLibFrmStruct *frmptr)

{
   int  astatus, status;
   struct WsLibStruct  *wsptr;
   struct WsLibMsgStruct  *msgptr;

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

   msgptr = frmptr->WsLibMsgPtr;
   wsptr = msgptr->WsLibPtr;

   if (wsptr->QueuedInput)
      wsptr->QueuedInput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   if (wsptr->WatchDogReadSecs)
      wsptr->WatchDogReadTime = CurrentTime + wsptr->WatchDogReadSecs;
   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;

   /* loop until the required number of bytes have been read */
   while (VMSok (frmptr->IOsb.iosb$w_status))
   {
      /* if data is not available mailbox $QIO stream reads return less */
      frmptr->FrameCount += frmptr->IOsb.iosb$w_bcnt;
      frmptr->ReadSize -= frmptr->IOsb.iosb$w_bcnt;
      if (!frmptr->ReadSize) break;

      if (msgptr->AstFunction)
      {
         /* a loop with an intermediate AST delivery! */
         astatus = sys$setast (0);
         status = sys$qio (EfnNoWait, wsptr->InputChannel, frmptr->IoRead,
                           &frmptr->IOsb, ReadHeader1Ast, frmptr,
                           frmptr->FrameHeader+frmptr->FrameCount,
                           frmptr->ReadSize,
                           0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedInput++;
         if (astatus == SS$_WASSET) sys$setast (1);
         return;
      }

      sys$qiow (EfnWait, wsptr->InputChannel, frmptr->IoRead,
                &frmptr->IOsb, 0, 0,
                frmptr->FrameHeader+frmptr->FrameCount, frmptr->ReadSize,
                0, 0, 0, 0);
   }

   if (VMSnok (frmptr->IOsb.iosb$w_status))
   {
      wsptr->QueuedInput++;
      ReadDataAst (frmptr);
      return;
   }

   /* check if frame data is masked */
   frmptr->FrameMaskBit = frmptr->FrameHeader[1] & 0x80;

   if (wsptr->RoleClient)
   {
      /* if the server is indicating it's masking frames */
      if (frmptr->FrameMaskBit && frmptr->FrameCount == 2)
      {
         /* need four more octets!! */
         frmptr->ReadSize = 4;
         wsptr->QueuedInput++;
         ReadHeader1Ast (frmptr);
         return;
      }
   }
   else
   {
      if (!frmptr->FrameMaskBit)
      {
         /* all client->server frames must be masked */
         WATCH_WSLIB (wsptr, FI_LI, "CLIENT frame not masked 0x!2XL!2XL",
                      frmptr->FrameHeader[0], frmptr->FrameHeader[1]);
         MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                             "client frame not masked 0x!2XL!2XL",
                             frmptr->FrameHeader[0], frmptr->FrameHeader[1]);
         strcpy (msgptr->CloseMsg, "client frame not masked");
         goto ProtocolError;
      }
   }

   /* process rest of frame header */
   frmptr->FrameFinBit = frmptr->FrameHeader[0] & 0x80;
   frmptr->FrameRsv = frmptr->FrameHeader[0] & 0x70;
   frmptr->FrameOpcode = frmptr->FrameHeader[0] & 0x0f;
   frmptr->FramePayload = frmptr->FrameHeader[1] & 0x7f;

   if (frmptr->FrameRsv)
   {
      /* reserve bits set */
      WATCH_WSLIB (wsptr, FI_LI, "RESERVE bit 0x!2XL", frmptr->FrameRsv);
      MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                          "reserve bit 0x!2XL", frmptr->FrameRsv); 
      sprintf (msgptr->CloseMsg, "reserve bit 0x%02x", frmptr->FrameRsv);
      goto ProtocolError;
   }

   switch (frmptr->FrameOpcode)
   {
      case WSLIB_OPCODE_CONTIN : break;
      case WSLIB_OPCODE_TEXT   : break;
      case WSLIB_OPCODE_BINARY : break;
      case WSLIB_OPCODE_CLOSE  : break;
      case WSLIB_OPCODE_PING   : break;
      case WSLIB_OPCODE_PONG   : break;
      default :
      {
         /* unknown opcode */
         WATCH_WSLIB (wsptr, FI_LI, "OPCODE unknown 0x!2XL",
                      frmptr->FrameOpcode);
         MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                             "unknown opcode 0x!2XL", frmptr->FrameOpcode);
         sprintf (msgptr->CloseMsg, "unknown opcode 0x%02x",
                  frmptr->FrameOpcode);
         goto ProtocolError;
      }
   }

   if (frmptr->FrameOpcode & 0x8)
   {
      /* control opcode */
      if (!frmptr->FrameFinBit)
      {
         WATCH_WSLIB (wsptr, FI_LI, "CONTROL frame fragmented");
         MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                             "control frame fragmented");
         strcpy (msgptr->CloseMsg, "control frame fragmented");
         goto ProtocolError;
      }
      if (frmptr->FramePayload > 125)
      {
         WATCH_WSLIB (wsptr, FI_LI, "CONTROL payload > 125 bytes");
         MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                             "control payload > 125 bytes");
         strcpy (msgptr->CloseMsg, "control payload > 125 bytes");
         goto ProtocolError;
      }
   }
   else
   if (frmptr->FrameFinBit)
   {
      /* FIN bit set */
      if (frmptr->FrameOpcode)
      {
         if (msgptr->MsgOpcode)
         {
            /* must not have an opcode */
            WATCH_WSLIB (wsptr, FI_LI, "FRAGMENT with opcode");
            MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                                "fragment with opcode");
            strcpy (msgptr->CloseMsg, "fragment with opcode");
            goto ProtocolError;
         }
         msgptr->MsgOpcode = frmptr->FrameOpcode;
      }
      else
      {
         if (!msgptr->MsgOpcode)
         {
            /* must have an opcode */
            WATCH_WSLIB (wsptr, FI_LI, "FRAME without opcode");
            MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                                "frame without opcode");
            strcpy (msgptr->CloseMsg, "frame without opcode");
            goto ProtocolError;
         }
      }
   }
   else
   {
      /* FIN bit reset */
      if (msgptr->MsgOpcode)
      {
         if (frmptr->FrameOpcode)
         {
            /* subsequent fragments must not have an opcode */
            WATCH_WSLIB (wsptr, FI_LI, "FRAGMENT with opcode");
            MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                                "fragment with opcode");
            strcpy (msgptr->CloseMsg, "fragment with opcode");
            goto ProtocolError;
         }
      }
      else
      {
         if (!frmptr->FrameOpcode)
         {
            /* fragments must have an initial opcode */
            WATCH_WSLIB (wsptr, FI_LI, "FRAGMENT without opcode");
            MsgCallback (wsptr, __LINE__, SS$_PROTOCOL,
                                "fragment without opcode");
            strcpy (msgptr->CloseMsg, "fragment without opcode");
            goto ProtocolError;
         }
         msgptr->MsgOpcode = frmptr->FrameOpcode;
      }
   }

   if (frmptr->FrameCount == 6)
   {
      /* essentially a longword integer in network byte order */
      frmptr->MaskingKey[0] = frmptr->FrameHeader[2];
      frmptr->MaskingKey[1] = frmptr->FrameHeader[3];
      frmptr->MaskingKey[2] = frmptr->FrameHeader[4];
      frmptr->MaskingKey[3] = frmptr->FrameHeader[5];
   }

   /* prepare to read more */
   frmptr->IOsb.iosb$w_bcnt = 0;
   frmptr->IOsb.iosb$w_status = SS$_NORMAL;
   wsptr->QueuedInput++;

   if (frmptr->FramePayload == 126)
   {
      /* read more of the header  */
      frmptr->ReadSize = 2;
      ReadHeader2Ast (frmptr);
      return;
   }

   if (frmptr->FramePayload == 127)
   {
      /* read more of the header  */
      frmptr->ReadSize = 8;
      ReadHeader2Ast (frmptr);
      return;
   }

   /* frame length is 125 bytes or less, begin reading data */
   ReadDataAst (frmptr);

   return;

ProtocolError:

   frmptr->IOsb.iosb$w_bcnt = 0;
   frmptr->IOsb.iosb$w_status = SS$_PROTOCOL;
   wsptr->QueuedInput++;
   ReadDataAst (frmptr);

   return;
}

/*****************************************************************************/
/*
The data $QIOed by ReadHeader1Ast() has been read.
For frames less than 126 bytes this function will not be called.
*/

static void ReadHeader2Ast (struct WsLibFrmStruct *frmptr)

{
   int  astatus, cnt, status;
   struct WsLibStruct  *wsptr;

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

   wsptr = frmptr->WsLibMsgPtr->WsLibPtr;

   if (wsptr->QueuedInput)
      wsptr->QueuedInput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   if (wsptr->WatchDogReadSecs)
      wsptr->WatchDogReadTime = CurrentTime + wsptr->WatchDogReadSecs;
   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;

   /* loop until the required number of bytes have been read */
   while (VMSok (frmptr->IOsb.iosb$w_status))
   {
      /* if data is not available mailbox $QIO stream reads return less */
      frmptr->FrameCount += frmptr->IOsb.iosb$w_bcnt;
      frmptr->ReadSize -= frmptr->IOsb.iosb$w_bcnt;
      if (!frmptr->ReadSize) break;

      if (frmptr->WsLibMsgPtr->AstFunction)
      {
         /* a loop with an intermediate AST delivery! */
         astatus = sys$setast (0);
         status = sys$qio (EfnNoWait, wsptr->InputChannel, frmptr->IoRead,
                           &frmptr->IOsb, ReadHeader2Ast, frmptr,
                           frmptr->FrameHeader+frmptr->FrameCount,
                           frmptr->ReadSize,
                           0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedInput++;
         if (astatus == SS$_WASSET) sys$setast (1);
         return;
      }

      sys$qiow (EfnWait, wsptr->InputChannel, frmptr->IoRead,
                &frmptr->IOsb, 0, 0,
                frmptr->FrameHeader+frmptr->FrameCount, frmptr->ReadSize,
                0, 0, 0, 0);
   }

   if (VMSnok (frmptr->IOsb.iosb$w_status))
   {
      wsptr->QueuedInput++;
      ReadDataAst (frmptr);
      return;
   }

   if (frmptr->FramePayload == 126)
   {
      /* word integer in network byte order */
      frmptr->FramePayload = (frmptr->FrameHeader[2] << 8) +
                             frmptr->FrameHeader[3];
      if (frmptr->FrameCount == 8)
      {
         /* essentially a longword integer in network byte order */
         frmptr->MaskingKey[0] = frmptr->FrameHeader[4];
         frmptr->MaskingKey[1] = frmptr->FrameHeader[5];
         frmptr->MaskingKey[2] = frmptr->FrameHeader[6];
         frmptr->MaskingKey[3] = frmptr->FrameHeader[7];
      }
   }
   else
   if (frmptr->FramePayload == 127)
   {
      /* protocol octaword integer in network byte order */
      if (frmptr->FrameHeader[2] || frmptr->FrameHeader[3] ||
          frmptr->FrameHeader[4] || frmptr->FrameHeader[5])
      {
         /* if >2^32 then something's probably wrong */
         MsgCallback (wsptr, __LINE__, SS$_BUGCHECK,
                             "frame length sanity check");
         frmptr->IOsb.iosb$w_bcnt = 0;
         frmptr->IOsb.iosb$w_status = SS$_BUGCHECK;
         wsptr->QueuedInput++;
         ReadDataAst (frmptr);
         return;
      }
      /* quadword integer in network byte order (lowest 32 bits anyway) */
      frmptr->FramePayload = (frmptr->FrameHeader[6] << 24) +
                             (frmptr->FrameHeader[7] << 16) +
                             (frmptr->FrameHeader[8] << 8) +
                              frmptr->FrameHeader[9];
      if (frmptr->FrameCount == 14)
      {
         /* essentially a longword integer in network byte order */
         frmptr->MaskingKey[0] = frmptr->FrameHeader[10];
         frmptr->MaskingKey[1] = frmptr->FrameHeader[11];
         frmptr->MaskingKey[2] = frmptr->FrameHeader[12];
         frmptr->MaskingKey[3] = frmptr->FrameHeader[13];
      }
   }
   else
     WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   /* the frame length is now known, begin reading data */
   frmptr->IOsb.iosb$w_bcnt = 0;
   frmptr->IOsb.iosb$w_status = SS$_NORMAL;
   wsptr->QueuedInput++;
   ReadDataAst (frmptr);
}

/*****************************************************************************/
/*
Called when the length of the frame has been determined.  This repeatedly reads
mailbox-sized chunks of the frame (if necessary), asynchronously or
synchronously as required, until the frame is completely read (or an error
occurs).  The '->AstFunction' or blocking caller should drive the client-end
business logic. 
*/

static void ReadDataAst (struct WsLibFrmStruct *frmptr)

{
   int  astatus, cnt, status,
        DataCount,
        DataSize;
   char  *DataPtr;
   struct WsLibStruct  *wsptr;
   struct WsLibMsgStruct  *msgptr;

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

   msgptr = frmptr->WsLibMsgPtr;
   wsptr = frmptr->WsLibMsgPtr->WsLibPtr;

   if (wsptr->QueuedInput)
      wsptr->QueuedInput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   if (wsptr->WatchDogReadSecs)
      wsptr->WatchDogReadTime = CurrentTime + wsptr->WatchDogReadSecs;
   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;

   while (VMSok (frmptr->IOsb.iosb$w_status))
   {
      if (!frmptr->DataPtr)
      {
         /* first call */
         WATCH_WSLIB (wsptr, FI_LI,
"READ header:!UL opcode:!2XL(!AZ) payload:!UL fin:!UL mask:!UL",
                      frmptr->FrameCount,
                      frmptr->FrameOpcode,
                      OpCodeName(frmptr->FrameOpcode),
                      frmptr->FramePayload,
                      frmptr->FrameFinBit ? 1 : 0,
                      frmptr->FrameMaskBit ? 1 : 0);

         /* establish buffer */
         if (frmptr->FramePayload <= 125)
         {
            /* use internal frame buffer */
            frmptr->DataPtr = (char*)frmptr->FrameHeader + frmptr->FrameCount;
            frmptr->DataSize = 125;
         }
         else
         {
            /* allocated frame data buffer */
            frmptr->DataSize = frmptr->FramePayload;
            /* ensure that even for zero payload some memory is allocated */
            frmptr->DataPtr = calloc (1, frmptr->DataSize+16);
            if (!frmptr->DataPtr) WsLibExit (wsptr, FI_LI, vaxc$errno);
         }
      }

      if (frmptr->IOsb.iosb$w_bcnt)
      {
         DataPtr = frmptr->DataPtr + frmptr->DataCount;
         if (msgptr->MsgOpcode == WSLIB_OPCODE_TEXT)
         {
            /* will also apply masking key if required */
            if (!Utf8Legal (frmptr))
            {
               WATCH_WSLIB (wsptr, FI_LI, "UTF-8 illegal (fast fail)");
               frmptr->IOsb.iosb$w_status = SS$_BADESCAPE;
               strcpy (msgptr->CloseMsg, "UTF-8 illegal");
               /* deliver the status to the application */
               break;
            }
         }
         else
         if (frmptr->FrameMaskBit)
         {
            /* apply masking key */
            for (cnt = 0; cnt < frmptr->IOsb.iosb$w_bcnt; cnt++)
               DataPtr[cnt] ^= frmptr->MaskingKey[frmptr->MaskCount++&0x3];
         }
         frmptr->DataCount += frmptr->IOsb.iosb$w_bcnt;
      }

      WATCH_WSLIB (wsptr, FI_LI, "READ inque:!UL payload:!UL/!UL !AZ",
                   wsptr->QueuedInput,
                   frmptr->DataCount, frmptr->FramePayload,
                   frmptr->DataCount >= frmptr->FramePayload ?
                      "COMPLETE" : "in-progress");

      if (frmptr->DataCount >= frmptr->FramePayload) break;

      DataPtr = frmptr->DataPtr + frmptr->DataCount;
      if (frmptr->FramePayload - frmptr->DataCount <= wsptr->InputMrs)
         DataCount = frmptr->FramePayload - frmptr->DataCount;
      else
         DataCount = wsptr->InputMrs;
      if (frmptr->DataCount + DataCount > frmptr->DataSize)
         DataCount = frmptr->DataSize - frmptr->DataCount;

      if (msgptr->AstFunction)
      {
         /* asynchronous read */
         astatus = sys$setast (0);
         status = sys$qio (EfnNoWait, wsptr->InputChannel, frmptr->IoRead,
                           &frmptr->IOsb, ReadDataAst, frmptr,
                           DataPtr, DataCount, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedInput++;
         if (astatus == SS$_WASSET) sys$setast (1);
         return;
      }

      /* synchronous read */
      sys$qiow (EfnWait, wsptr->InputChannel, frmptr->IoRead,
                &frmptr->IOsb, 0, 0,
                DataPtr, DataCount, 0, 0, 0, 0);
   }

   WATCH_WSLIB (wsptr, FI_LI, "READ %X!8XL", frmptr->IOsb.iosb$w_status);

   if (VMSnok (frmptr->IOsb.iosb$w_status) &&
       frmptr->IOsb.iosb$w_status != SS$_LINKDISCON &&
       !wsptr->MsgStringLength)
      MsgCallback (wsptr, __LINE__, frmptr->IOsb.iosb$w_status,
                          "frame read");

   /******************/
   /* frame complete */
   /******************/

   if (VMSok (frmptr->IOsb.iosb$w_status))
   {
      if (frmptr->FrameOpcode == WSLIB_OPCODE_PING)
      {
         Pong (frmptr);
         /* restart the read of the data */
         ReadFrame (msgptr);
         return;
      }

      if (frmptr->FrameOpcode == WSLIB_OPCODE_PONG)
      {
         /* process any (solicited or unsolicited) pong callback */
         if (wsptr->PongCallbackFunction)
            (*wsptr->PongCallbackFunction)(wsptr);
         else
         if (PongCallbackFunction)
            (*PongCallbackFunction)(wsptr);
         else
            MsgCallback (wsptr, __LINE__, SS$_NOTMODIFIED,
                                "no pong callback");
         /* restart the read of the data */
         ReadFrame (msgptr);
         return;
      }

      if (frmptr->FrameOpcode == WSLIB_OPCODE_CLOSE)
      {
         Close (frmptr);
         frmptr->IOsb.iosb$w_status = SS$_SHUT;
         /* deliver closed status to the application */
      }
   }

   /*****************/
   /* build message */
   /*****************/

   msgptr->MsgStatus = frmptr->IOsb.iosb$w_status;

   if (VMSok (msgptr->MsgStatus))
   {
      /* accumulate total message data transfered */
      *(int64*)wsptr->InputCount = *(int64*)wsptr->InputCount +
                                    (int32)frmptr->DataCount;

      if (!(DataSize = msgptr->DataMax)) DataSize = msgptr->DataSize;
      if (msgptr->DataCount + frmptr->DataCount > (unsigned)DataSize)
      {
         MsgCallback (wsptr, __LINE__, SS$_RESULTOVF,
                             "message !UL bytes > buffer !UL bytes",
                             msgptr->DataCount + frmptr->DataCount, DataSize);
         msgptr->MsgStatus = SS$_RESULTOVF;
         msgptr->DataCount = 0;
      }
      else
      if (msgptr->DataMax)
      {
         /* dynamic buffer */
         if (msgptr->DataPtr)
         {
            if (frmptr->DataCount)
            {
               /* append this fragment to previously buffered data */
               msgptr->DataPtr = realloc (msgptr->DataPtr,
                                          msgptr->DataCount +
                                             frmptr->DataCount);
               if (!msgptr->DataPtr) WsLibExit (wsptr, FI_LI, vaxc$errno);
               memcpy (msgptr->DataPtr + msgptr->DataCount,
                       frmptr->DataPtr,
                       frmptr->DataCount);
               msgptr->DataCount += frmptr->DataCount;
            }
         }
         else
         {
            /* ensure that even for zero payload some memory is allocated */
            msgptr->DataPtr = calloc (1, frmptr->DataCount+16);
            if (!msgptr->DataPtr) WsLibExit (wsptr, FI_LI, vaxc$errno);
            memcpy (msgptr->DataPtr, frmptr->DataPtr, frmptr->DataCount);
            msgptr->DataCount = frmptr->DataCount;
         }
      }
      else
      {
         /* supplied buffer */
         memcpy (msgptr->DataPtr + msgptr->DataCount,
                 frmptr->DataPtr,
                 frmptr->DataCount);
         msgptr->DataCount += frmptr->DataCount;
      }

      /* dispose any allocated frame data buffer */
      if (frmptr->FramePayload > 125) free (frmptr->DataPtr);

      if (VMSok (msgptr->MsgStatus))
      {
         /* if not the final fragment */
         if (!frmptr->FrameFinBit)
         {
            /* read the next fragment frame */
            ReadFrame (msgptr);
            return;
         }
      }
   }

   if (VMSok (msgptr->MsgStatus))
   {
      if (msgptr->MsgOpcode == WSLIB_OPCODE_TEXT)
      {
         /* ensure any code-point is complete */
         frmptr->IOsb.iosb$w_bcnt = 0;
         if (!Utf8Legal (frmptr))
         {
            WATCH_WSLIB (wsptr, FI_LI, "UTF-8 illegal (fast fail)");
            msgptr->MsgStatus = SS$_BADESCAPE;
            strcpy (msgptr->CloseMsg, "UTF-8 illegal");
            /* deliver the status to the application */
         }
      }
   }

   /****************************/
   /* deliver to read function */
   /****************************/

   if (VMSok (msgptr->MsgStatus))
   {
      *(int64*)wsptr->InputMsgCount = *(int64*)wsptr->InputMsgCount + 1;

      if (msgptr->MsgOpcode == WSLIB_OPCODE_TEXT)
      {
         /* for text always better if it's null-terminated */
         if (msgptr->DataMax ||
             msgptr->DataCount < msgptr->DataSize)
            msgptr->DataPtr[msgptr->DataCount] = '\0';
         else
            MsgCallback (wsptr, __LINE__, SS$_BUFFEROVF,
                                "no space for \\0");

         if (wsptr->SetAscii)
         {
            /* convert from UTF-8 to 8 bit "ASCII" */
            WATCH_WSLIB (wsptr, FI_LI, "UTF-8 decode");
            cnt = WsLibFromUtf8 (msgptr->DataPtr, msgptr->DataCount, 0);
            if (cnt >= 0)
               msgptr->DataCount = cnt;
            else
            {
               /* error in UTF-8 to ASCII conversion */
               WATCH_WSLIB (wsptr, FI_LI, "UTF-8 decode ERROR");
               MsgCallback (wsptr, __LINE__, SS$_DATALOST,
                                   "UTF-8 decode error");
               msgptr->MsgStatus = SS$_DATALOST;
               msgptr->DataCount = 0;
            }
         }
      }
   }

   if (msgptr->DataMax)
   {
      /* the ->MsgDataPtr is only used by WsLibReadGrab() for sanity check */
      wsptr->InputDataPtr = wsptr->MsgDataPtr = msgptr->DataPtr;
   }

   wsptr->InputStatus = msgptr->MsgStatus;
   wsptr->InputOpcode = msgptr->MsgOpcode;
   wsptr->InputDataCount = msgptr->DataCount;
   wsptr->InputDataPtr = wsptr->InputDataDsc.dsc$a_pointer = msgptr->DataPtr;
   if (msgptr->DataCount > MAX16BIT)
      wsptr->InputDataDsc.dsc$w_length = MAX16BIT;
   else
      wsptr->InputDataDsc.dsc$w_length = msgptr->DataCount;
   if (msgptr->DataMax)
      wsptr->InputDataMax = msgptr->DataMax;
   else
      wsptr->InputDataMax = msgptr->DataSize;

   if (wsptr->ReadDscPtr)
   {
      wsptr->ReadDscPtr->dsc$a_pointer = msgptr->DataPtr;
      if (msgptr->DataCount > MAX16BIT)
         wsptr->ReadDscPtr->dsc$w_length = MAX16BIT;
      else
         wsptr->ReadDscPtr->dsc$w_length = msgptr->DataCount;
   }

   if (wsptr->InputStatus == SS$_PROTOCOL)
      WsLibClose (wsptr, WSLIB_CLOSE_PROTOCOL, msgptr->CloseMsg);
   else
   if (wsptr->InputStatus == SS$_BADESCAPE)
      WsLibClose (wsptr, WSLIB_CLOSE_DATA, msgptr->CloseMsg);
   else
   if (VMSnok (wsptr->InputStatus))
      WsLibClose (wsptr, WSLIB_CLOSE_BANG, NULL);

   /* do not molest the buffer pointers if synchronous I/O */
   if (msgptr->AstFunction)
   {
      msgptr->AstFunction (wsptr);
      if (!DataPtr && msgptr->DataMax)
      {
         /* if the dynamic message buffer has not been grabbed then free it */
         if (wsptr->InputDataPtr) free (wsptr->InputDataPtr);
         /* reset the rest */
         wsptr->InputDataPtr = wsptr->MsgDataPtr = NULL;
         wsptr->InputDataCount = wsptr->InputDataMax = 0;
         wsptr->InputDataDsc.dsc$a_pointer = NULL;
         wsptr->InputDataDsc.dsc$w_length = 0;
         wsptr->ReadDscPtr = NULL;
      }
   }

   free (msgptr);

   /* message dequeue */
   if (msgptr = wsptr->MsgReadQuePtr = msgptr->MsgQuePtr)
   {
      /* kick-off the next queued read */
      ReadFrame (msgptr);
   }

   wsptr->WatchDogReadTime = 0;
   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;
   if (wsptr->WatchDogWakeSecs)
      wsptr->WatchDogWakeTime = CurrentTime + wsptr->WatchDogWakeSecs;

   if (wsptr->WebSocketShut) Shut (wsptr); 
}

/*****************************************************************************/
/*
When using dynamic message data buffer grab the allocated memory returning a
pointer or NULL.  The data count and other detail can be obtained using
WsLibReadCount(), WsLibReadIsText(), etc.  This should subsequently be freed
using WsLibFree() when no longer required.
*/

char* WsLibReadGrab (struct WsLibStruct *wsptr)

{
   char  *cptr;

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

   /* sanity check the call */
   if (wsptr->InputDataPtr &&
       wsptr->InputDataPtr == wsptr->MsgDataPtr)
   {
      cptr = wsptr->InputDataPtr;
      wsptr->InputDataPtr = wsptr->MsgDataPtr = NULL;
      wsptr->InputDataCount = wsptr->InputDataMax = 0;
      wsptr->InputDataDsc.dsc$a_pointer = NULL;
      wsptr->InputDataDsc.dsc$w_length = 0;
      return (cptr);
   }

   WATCH_WSLIB (wsptr, FI_LI, "GRAB sanity check");
   MsgCallback (wsptr, __LINE__, SS$_BUGCHECK, "GRAB sanity check (!AZ)",
                       wsptr->InputDataPtr ? "null" : "data");
   return (NULL);
}

/*****************************************************************************/
/*
Return true if the most recent read was BINARY opcode.
*/

int WsLibReadIsBinary (struct WsLibStruct *wsptr)

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

   return (wsptr->InputOpcode == WSLIB_OPCODE_BINARY);
}

/*****************************************************************************/
/*
Return true if the most recent read was TEXT opcode.
*/

int WsLibReadIsText (struct WsLibStruct *wsptr)

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

   return (wsptr->InputOpcode == WSLIB_OPCODE_TEXT);
}

/*****************************************************************************/
/*
Return the read status value.
*/

int WsLibReadStatus (struct WsLibStruct *wsptr)

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

   return (wsptr->InputStatus);
}

/*****************************************************************************/
/*
Return the (most recent) read count value (longword).
*/

int WsLibReadCount (struct WsLibStruct *wsptr)

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

   return (wsptr->InputDataCount);
}

/*****************************************************************************/
/*
Return a pointer to the (most recent) read buffer.
*/

char* WsLibReadData (struct WsLibStruct *wsptr)

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

   return (wsptr->InputDataPtr);
}

/*****************************************************************************/
/*
Return a pointer to the (most recent) read data descriptor.
*/

struct dsc$descriptor_s* WsLibReadDataDsc (struct WsLibStruct *wsptr)

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

   return (&wsptr->InputDataDsc);
}

/*****************************************************************************/
/*
Return a pointer to the total read count value (quadword).
*/

unsigned long* WsLibReadTotal (struct WsLibStruct *wsptr)

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

   return ((unsigned long*)&wsptr->InputCount);
}

/*****************************************************************************/
/*
Return a pointer to the total messages read value (quadword).
*/

unsigned long* WsLibReadMsgTotal (struct WsLibStruct *wsptr)

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

   return ((unsigned long*)&wsptr->InputMsgCount);
}

/*****************************************************************************/
/*
Generate a masking key for the supplied IO structure.
*/

static void MaskingKey (struct WsLibFrmStruct *frmptr)

{
   static unsigned long  RandomNumber,
                         RandomFiller;

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

   /* pseudo-random 32 bytes */
   if (!(RandomNumber & 0xff)) sys$gettim (&RandomNumber);
   RandomNumber = RandomNumber * 69069 + 1;

   frmptr->MaskCount = 0;
   frmptr->FrameMaskBit = 0x80;
   /* network byte order */
   frmptr->MaskingKey[0] = (RandomNumber & 0xff000000) >> 24;
   frmptr->MaskingKey[1] = (RandomNumber & 0x00ff0000) >> 16;
   frmptr->MaskingKey[2] = (RandomNumber & 0x0000ff00) >> 8;
   frmptr->MaskingKey[3] = RandomNumber & 0x000000ff;
}

/*****************************************************************************/
/*
Write the data pointed to by the supplied string descriptor.
*/

int WsLibWriteDsc
(
struct WsLibStruct *wsptr,
struct dsc$descriptor_s *DataDsc,
void *AstFunction
)
{
   int  status;

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

   if (DataDsc == NULL)
      status = WsLibWrite (wsptr, NULL, 0, AstFunction);
   else
   if (DataDsc->dsc$b_class != DSC$K_CLASS_S &&
       DataDsc->dsc$b_dtype != DSC$K_DTYPE_T)
      status = LIB$_INVSTRDES;
   else
   status = WsLibWrite (wsptr, DataDsc->dsc$a_pointer,
                        DataDsc->dsc$w_length, AstFunction);
   return (status);
}

/*****************************************************************************/
/*
Queue a write to the client WEBSOCKET_OUTPUT mailbox.  If an AST function is
supplied then the write is asynchronous, otherwise blocking.  If the data
pointer is NULL then send a close-connection frame.  If the AST function is
supplied as (void*)-1 a non-blocking I/O is generated that does not require a
target AST function.
*/

int WsLibWrite
(
struct WsLibStruct *wsptr,
char *DataPtr,
int DataCount,
void *AstFunction
)
{
   int  cnt, hcnt, status,
        Utf8Count;
   unsigned char  *cptr, *czptr, *sptr;
   struct WsLibFrmStruct  *frmptr;
   struct WsLibMsgStruct  *msgptr,
                          *nsgptr;

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

   WATCH_WSLIB (wsptr, FI_LI, "WRITE count:!UL", DataCount);

   if (wsptr->WebSocketClosed)
   {
      MsgCallback (wsptr, __LINE__, SS$_SHUT, "can't write; closed");
      wsptr->OutputStatus = SS$_SHUT;
      if (AstFunction && AstFunction != WSLIB_ASYNCH)
         ((void(*)())AstFunction)(wsptr);
      return (SS$_SHUT);
   }

   msgptr = calloc (1, sizeof(struct WsLibMsgStruct));
   if (!msgptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   msgptr->WsLibPtr = wsptr;

   /* null or empty writes send an empty message */
   if (!DataPtr)
   {
      DataPtr = "";
      DataCount = 0;
   }

   msgptr->DataPtr = DataPtr;
   msgptr->DataCount = DataCount;
   msgptr->AstFunction = AstFunction;

   if (wsptr->SetAscii)
   {
      /* test if any UTF-8 encoding required */
      Utf8Count = 0;
      czptr = (cptr = (unsigned char*)DataPtr) + DataCount;
      while (cptr < czptr) if (*cptr++ & 0x80) Utf8Count++;

      if (Utf8Count)
      {
         /********************/
         /* convert to UTF-8 */
         /********************/

         WATCH_WSLIB (wsptr, FI_LI, "UTF-8 encode");
         msgptr->Utf8Ptr = calloc (1, DataCount+Utf8Count); 
         if (!msgptr->Utf8Ptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
         czptr = (cptr = (unsigned char*)DataPtr) + DataCount;
         sptr = (unsigned char*)msgptr->Utf8Ptr;
         while (cptr < czptr)
         {
            if (*cptr & 0x80)
            {
               *sptr++ = ((*cptr & 0xc0) >> 6) | 0xc0;
               *sptr++ = (*cptr++ & 0x3f) | 0x80;
            }
            else
               *sptr++ = *cptr++;
         }
         msgptr->DataPtr = msgptr->Utf8Ptr;
         msgptr->DataCount = (char*)sptr - msgptr->Utf8Ptr;
      }
   }

   if (wsptr->SetBuffer && !msgptr->Utf8Ptr)
   {
      /* not UTF-8 and set to internally buffer message */
      msgptr->BufferPtr = calloc (1, DataCount); 
      if (!msgptr->BufferPtr) WsLibExit (wsptr, FI_LI, vaxc$errno);
      memcpy (msgptr->BufferPtr, DataPtr, DataCount);
      msgptr->DataPtr = msgptr->BufferPtr;
   }

   frmptr = &msgptr->FrameData;
   frmptr->WsLibMsgPtr = msgptr;
   frmptr->IOsb.iosb$w_status = SS$_NORMAL;
   msgptr->ThisFrmPtr = frmptr;

   if (wsptr->MsgWriteQuePtr)
   {
      /* append this one to the write queue */
      for (nsgptr = wsptr->MsgWriteQuePtr;
           nsgptr->MsgQuePtr;
           nsgptr = nsgptr->MsgQuePtr);
      nsgptr->MsgQuePtr = msgptr;
   }
   else
   {
      /* only this one in the queue */
      wsptr->MsgWriteQuePtr = msgptr;
      /* either kick-off asynchronous I/O or return after blocking I/O */
      wsptr->QueuedOutput++;
      WriteAst (frmptr);
   }

   return (frmptr->IOsb.iosb$w_status);
}

/*****************************************************************************/
/*
Write the message to the WebSocket.  Message may be automatically fragmented.
Can AST back to this function one or more times with asycnchronous I/O.
*/

static void WriteAst (struct WsLibFrmStruct *frmptr)

{
   int  astatus, cnt, count, hcnt, length, status,
        DataCount;
   char  *pointer,
         *DataPtr;
   struct WsLibStruct  *wsptr;
   struct WsLibMsgStruct  *msgptr;

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

   msgptr = frmptr->WsLibMsgPtr;
   wsptr = msgptr->WsLibPtr;

   if (wsptr->QueuedOutput)
      wsptr->QueuedOutput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;

   while (VMSok (frmptr->IOsb.iosb$w_status))
   {
      if (frmptr->MrsWriteCount)
      {
         /* accumulate total message data transfered */
         *(int64*)wsptr->OutputCount = *(int64*)wsptr->OutputCount +
                                        (int32)frmptr->MrsWriteCount;
         msgptr->WriteCount += frmptr->MrsWriteCount;
         frmptr->MrsWriteCount = 0;
      }

      WATCH_WSLIB (wsptr, FI_LI, "WRITE outque:!UL payload:!UL/!UL !AZ",
                   wsptr->QueuedOutput,
                   msgptr->WriteCount, msgptr->DataCount,
                   (frmptr->IOsb.iosb$w_bcnt &&
                    msgptr->WriteCount == msgptr->DataCount) ? "COMPLETE" :
                                                             "in-progress");

      /* if the header has been written and the required quantity of data */
      if (frmptr->IOsb.iosb$w_bcnt &&
          msgptr->WriteCount == msgptr->DataCount) break;

      if (wsptr->WebSocketClosed)
      {
         MsgCallback (wsptr, __LINE__, SS$_SHUT, "can't write; closed");
         frmptr->IOsb.iosb$w_status = SS$_SHUT;
         break;
      }

      /*********/
      /* frame */
      /*********/

      /* reset the frame structure (only needed on subsequent writes) */
      frmptr = &msgptr->FrameData;
      if (frmptr->IOsb.iosb$w_status)
         memset (frmptr, 0, sizeof(struct WsLibFrmStruct));
      frmptr->WsLibMsgPtr = msgptr;
      msgptr->ThisFrmPtr = frmptr;

      /* if being used as a client then mask the data */
      if (wsptr->RoleClient) MaskingKey (frmptr);

      DataPtr = msgptr->DataPtr + msgptr->WriteCount;
      if (msgptr->DataCount - msgptr->WriteCount > wsptr->FrameMaxSize)
         DataCount = wsptr->FrameMaxSize;
      else
         DataCount = msgptr->DataCount - msgptr->WriteCount;

      /* only indicate opcode on first write (in case of fragmentation) */
      if (frmptr->IOsb.iosb$w_bcnt)
         frmptr->FrameOpcode = 0;
      else
      if (wsptr->SetAscii || wsptr->SetUtf8)
         frmptr->FrameOpcode = WSLIB_OPCODE_TEXT;
      else
         frmptr->FrameOpcode = WSLIB_OPCODE_BINARY;

      /* if not the last fragment */
      if (msgptr->WriteCount + DataCount < msgptr->DataCount)
         frmptr->FrameFinBit = 0;
      else
         frmptr->FrameFinBit = WSLIB_BIT_FIN;

      WATCH_WSLIB (wsptr, FI_LI,
"WRITE opcode:!2XL(!AZ) fin:!UL mask:!UL data:!UL",
                   frmptr->FrameOpcode,
                   OpCodeName(frmptr->FrameOpcode),
                   frmptr->FrameFinBit ? 1 : 0,
                   frmptr->FrameMaskBit ? 1 : 0,
                   DataCount);

      hcnt = 0;
      frmptr->FrameHeader[hcnt++] = frmptr->FrameFinBit | frmptr->FrameOpcode;

      if (DataCount <= 125)
         frmptr->FrameHeader[hcnt++] = frmptr->FrameMaskBit + DataCount;
      else
      if (DataCount <= MAX16BIT)
      {
         frmptr->FrameHeader[hcnt++] = frmptr->FrameMaskBit + 126;
         /* network byte order */
         frmptr->FrameHeader[hcnt++] = (DataCount & 0xff00) >> 8;
         frmptr->FrameHeader[hcnt++] = DataCount & 0xff;
      }
      else
      {
         frmptr->FrameHeader[hcnt++] = frmptr->FrameMaskBit + 127;
         /* network byte order */
         frmptr->FrameHeader[hcnt++] = 0;
         frmptr->FrameHeader[hcnt++] = 0;
         frmptr->FrameHeader[hcnt++] = 0;
         frmptr->FrameHeader[hcnt++] = 0;
         frmptr->FrameHeader[hcnt++] = (DataCount & 0xff000000) >> 24;
         frmptr->FrameHeader[hcnt++] = (DataCount & 0xff0000) >> 16;
         frmptr->FrameHeader[hcnt++] = (DataCount & 0xff00) >> 8;
         frmptr->FrameHeader[hcnt++] = DataCount & 0xff;
      }

      if (frmptr->FrameMaskBit)
      {
         frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[0];
         frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[1];
         frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[2];
         frmptr->FrameHeader[hcnt++] = frmptr->MaskingKey[3];

         /* never apply apply the masking key to original data */
         if (!(frmptr->MaskedPtr = msgptr->Utf8Ptr) &&
             !(frmptr->MaskedPtr = msgptr->BufferPtr))
         {
            /* allocate a buffer for masked data */
            frmptr->MaskedPtr = calloc (1, DataCount); 
            if (!frmptr->MaskedPtr) WsLibExit (wsptr, FI_LI, vaxc$errno);
         }
         for (cnt = 0; cnt < DataCount; cnt++)
            frmptr->MaskedPtr[cnt] = DataPtr[cnt] ^
                                     frmptr->MaskingKey[cnt&0x03];
         DataPtr = frmptr->MaskedPtr;
      }

      /*********/
      /* write */
      /*********/

      if (DataCount && DataCount <= 125)
      {
         /* for efficiency append the data to the header */
         memcpy (frmptr->FrameHeader+hcnt, DataPtr, DataCount);
         hcnt += DataCount;
         frmptr->MrsWriteCount = DataCount;
         /* indicate that it's all contained in the header */
         DataCount = 0;
      }

      frmptr->MrsDataPtr = DataPtr;
      frmptr->MrsDataCount = DataCount;

      if (msgptr->AstFunction)
      {
         /* asynchronous */
         astatus = sys$setast (0);
         if (DataCount)
            status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                              IO$_WRITELBLK | IO$M_READERCHECK,
                              &frmptr->IOsb, WriteMrsAst, frmptr, 
                              frmptr->FrameHeader, hcnt, 0, 0, 0, 0);
         else
            status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                              IO$_WRITELBLK | IO$M_READERCHECK,
                              &frmptr->IOsb, WriteAst, frmptr,
                              frmptr->FrameHeader, hcnt, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedOutput++;
         if (astatus == SS$_WASSET) sys$setast (1);
         return;
      }

      /* synchronous */
      sys$qiow (EfnWait, wsptr->OutputChannel,
                IO$_WRITELBLK | IO$M_READERCHECK,
                &frmptr->IOsb, 0, 0,
                frmptr->FrameHeader, hcnt, 0, 0, 0, 0);
      if (DataCount)
      {
         wsptr->QueuedOutput++;
         WriteMrsAst (frmptr);
      }
   }

   WATCH_WSLIB (wsptr, FI_LI, "WRITE %X!8XL", frmptr->IOsb.iosb$w_status);

   /*******/
   /* end */
   /*******/

   if (VMSok (frmptr->IOsb.iosb$w_status))
      *(int64*)wsptr->OutputMsgCount = *(int64*)wsptr->OutputMsgCount + 1;
   else
      WsLibClose (wsptr, WSLIB_CLOSE_BANG, NULL);

   if (msgptr->AstFunction &&
       msgptr->AstFunction != WSLIB_ASYNCH)
   {
      /* status and count are I/O valid only during the AST function */
      status = wsptr->OutputStatus;
      count = wsptr->OutputDataCount;
      pointer = wsptr->OutputDataDsc.dsc$a_pointer;
      length = wsptr->OutputDataDsc.dsc$w_length;

      wsptr->OutputStatus = frmptr->IOsb.iosb$w_status;
      wsptr->OutputDataCount = frmptr->IOsb.iosb$w_bcnt;
      wsptr->OutputDataDsc.dsc$a_pointer = frmptr->DataPtr;
      wsptr->OutputDataDsc.dsc$w_length = wsptr->OutputDataCount;

      (*msgptr->AstFunction)(wsptr);

      wsptr->OutputStatus = status;
      wsptr->OutputDataCount = count;
      wsptr->OutputDataDsc.dsc$a_pointer = pointer;
      wsptr->OutputDataDsc.dsc$w_length = length;
   }

   if (msgptr->BufferPtr) free (msgptr->BufferPtr);
   if (msgptr->Utf8Ptr) free (msgptr->Utf8Ptr);
   free (msgptr);

   /* message dequeue */
   if (msgptr = wsptr->MsgWriteQuePtr = msgptr->MsgQuePtr)
   {
      /* kick-off the next queued write */
      frmptr = msgptr->ThisFrmPtr;
      wsptr->QueuedOutput++;
      WriteAst (frmptr);
   }

   if (wsptr->WatchDogIdleSecs)
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;
   if (wsptr->WatchDogWakeSecs)
      wsptr->WatchDogWakeTime = CurrentTime + wsptr->WatchDogWakeSecs;
}

/*****************************************************************************/
/*
Write the frame data in record MRS sized chunks (may only be one).  This
function is only used for payload greater than 125 bytes.  For 125 bytes or
less (including empty frames) it's all handled by WriteAst().
*/

static void WriteMrsAst (struct WsLibFrmStruct *frmptr)

{
   int  astatus, status,
        DataCount;
   char  *DataPtr;
   struct WsLibStruct  *wsptr;

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

   wsptr = frmptr->WsLibMsgPtr->WsLibPtr;

   if (wsptr->QueuedOutput)
      wsptr->QueuedOutput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   for (;;)
   {
      if (*(ULONGPTR)frmptr->FrameHeader)
      {
         /* if the header has just been written these are invalid */
         frmptr->MrsWriteCount = frmptr->IOsb.iosb$w_bcnt = 0;
         *(ULONGPTR)frmptr->FrameHeader = 0;
      }

      if (VMSnok (frmptr->IOsb.iosb$w_status)) break;

      frmptr->MrsWriteCount += frmptr->IOsb.iosb$w_bcnt;

      WATCH_WSLIB (wsptr, FI_LI, "WRITE outque:!UL mrs:!UL/!UL !AZ",
                   wsptr->QueuedOutput,
                   frmptr->MrsWriteCount, frmptr->MrsDataCount,
                   frmptr->MrsWriteCount == frmptr->MrsDataCount ?
                      "COMPLETE" : "in-progress");

      if (frmptr->MrsWriteCount == frmptr->MrsDataCount) break;

      DataPtr = frmptr->MrsDataPtr + frmptr->MrsWriteCount;
      if (frmptr->MrsDataCount - frmptr->MrsWriteCount > wsptr->OutputMrs)
         DataCount = wsptr->OutputMrs;
      else
         DataCount = frmptr->MrsDataCount - frmptr->MrsWriteCount;

      if (frmptr->WsLibMsgPtr->AstFunction)
      {
         /* asynchronous data */
         astatus = sys$setast (0);
         status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                           IO$_WRITELBLK | IO$M_READERCHECK,
                           &frmptr->IOsb, WriteMrsAst, frmptr,
                           DataPtr, DataCount, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedOutput++;
         if (astatus == SS$_WASSET) sys$setast (1);
         return;
      }

      /* synchronous data */
      sys$qiow (EfnWait, wsptr->OutputChannel,
                IO$_WRITELBLK | IO$M_READERCHECK,
                &frmptr->IOsb, 0, 0,
                DataPtr, DataCount, 0, 0, 0, 0);
   }

   if (frmptr->MaskedPtr)
   {
      free (frmptr->MaskedPtr);
      frmptr->MaskedPtr = NULL;
   }

   if (frmptr->WsLibMsgPtr->AstFunction)
   {
      wsptr->QueuedOutput++;
      WriteAst (frmptr);
   }
}

/*****************************************************************************/
/*
Return the write status value.
*/

int WsLibWriteStatus (struct WsLibStruct *wsptr)

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

   return (wsptr->OutputStatus);
}

/*****************************************************************************/
/*
Return the write count value (longword).
*/

int WsLibWriteCount (struct WsLibStruct *wsptr)

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

   return (wsptr->OutputDataCount);
}

/*****************************************************************************/
/*
Return a pointer to the (most recent) write data descriptor.
*/

struct dsc$descriptor_s* WsLibWriteDataDsc (struct WsLibStruct *wsptr)

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

   return (&wsptr->OutputDataDsc);
}

/*****************************************************************************/
/*
Return a pointer to the total write count value (quadword).
*/

unsigned long* WsLibWriteTotal (struct WsLibStruct *wsptr)

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

   return ((unsigned long*)&wsptr->OutputCount);
}

/*****************************************************************************/
/*
Return a pointer to the total write message value (quadword).
*/

unsigned long* WsLibWriteMsgTotal (struct WsLibStruct *wsptr)

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

   return ((unsigned long*)&wsptr->OutputMsgCount);
}

/*****************************************************************************/
/*
Set the pointer to the user data associated with the WebSocket structure.
*/

void WsLibSetUserData
(
struct WsLibStruct *wsptr,
void *UserDataPtr
)
{
   /*********/
   /* begin */
   /*********/

   wsptr->UserDataPtr = UserDataPtr;
}

/*****************************************************************************/
/*
Return the pointer to the user data.
*/

void* WsLibGetUserData (struct WsLibStruct *wsptr)

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

   return (wsptr->UserDataPtr);
}

/*****************************************************************************/
/*
Set/reset the callout response AST.  Returns the previous callout pointer.
*/

void* WsLibSetCallout
(
struct WsLibStruct *wsptr,
void *AstFunction
)
{
   void  *PrevCallout;

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

   PrevCallout = wsptr->CalloutAstFunction;
   wsptr->CalloutAstFunction = AstFunction;
   return (PrevCallout);
}

/*****************************************************************************/
/*
Set the maxium frame size before the message is fragmented.
*/

int WsLibSetFrameMax
(
struct WsLibStruct *wsptr,
int FrameMax
)
{
   int  PrevFrameMax;

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

   PrevFrameMax = wsptr->FrameMaxSize;
   wsptr->FrameMaxSize = FrameMax;
   return (PrevFrameMax);
}

/*****************************************************************************/
/*
Set wsLIB framing to binary.
*/

int WsLibSetBinary (struct WsLibStruct *wsptr)

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

   wsptr->SetBinary = 1;
   wsptr->SetAscii = wsptr->SetBuffer = wsptr->SetUtf8 = 0;
   return (1);
}

/*****************************************************************************/
/*
Is wsLIB message content binary?
*/

int WsLibIsSetBinary (struct WsLibStruct *wsptr)

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

   return (wsptr->SetBinary);
}

/*****************************************************************************/
/*
Set wsLIB framing to Text8, with 8 bit "ASCII" to UTF-8 implicit encoding.
*/

int WsLibSetAscii (struct WsLibStruct *wsptr)

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

   wsptr->SetAscii = 1;
   wsptr->SetBinary = wsptr->SetUtf8 = 0;
   return (1);
}

/*****************************************************************************/
/*
Is wsLIB message content set to be Text8 (8 bit "ASCII")?
*/

int WsLibIsSetAscii (struct WsLibStruct *wsptr)

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

   return (wsptr->SetAscii);
}

/*****************************************************************************/
/*
Set wsLIB to buffer the data internally before sending.
*/

int WsLibSetBuffer (struct WsLibStruct *wsptr)

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

   wsptr->SetBuffer = 1;
   return (1);
}

/*****************************************************************************/
/*
Set wsLIB NOT to buffer the data internally before sending.
*/

int WsLibSetNoBuffer (struct WsLibStruct *wsptr)

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

   wsptr->SetBuffer = 0;
   return (0);
}

/*****************************************************************************/
/*
Is wsLIB message set to buffer the conmtent internally before sending?
*/

int WsLibIsSetBuffer (struct WsLibStruct *wsptr)

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

   return (wsptr->SetBuffer);
}

/*****************************************************************************/
/*
Set wsLIB framing to text (i.e. must already be UTF-8 encoding).
*/

int WsLibSetUtf8 (struct WsLibStruct *wsptr)

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

   wsptr->SetUtf8 = 1;
   wsptr->SetBinary = wsptr->SetAscii = 0;
   return (1);
}

/*****************************************************************************/
/*
Is wsLIB message content UTF-8?
*/

int WsLibIsSetUtf8 (struct WsLibStruct *wsptr)

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

   return (wsptr->SetUtf8);
}

/*****************************************************************************/
/*
Set wsLIB framing write data masked.
*/

int WsLibSetRoleClient (struct WsLibStruct *wsptr)

{
   int PrevRoleClient;

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

   PrevRoleClient = wsptr->RoleClient;
   wsptr->RoleClient = 1;
   return (PrevRoleClient);
}

/*****************************************************************************/
/*
Set wsLIB framing write data not masked.
*/

int WsLibSetRoleServer (struct WsLibStruct *wsptr)

{
   int PrevRoleClient;

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

   PrevRoleClient = wsptr->RoleClient;
   wsptr->RoleClient = 0;
   return (PrevRoleClient);
}

/*****************************************************************************/
/*
Is wsLIB message content set to write masked?
*/

int WsLibIsRoleClient (struct WsLibStruct *wsptr)

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

   return (wsptr->RoleClient);
}

/*****************************************************************************/
/*
Set number of seconds before the application is considered idle and exited.
*/

void WsLibSetLifeSecs (int LifeSecs)

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

   if (!(WatchDogLifeSecs = LifeSecs))
     WatchDogLifeSecs = DEFAULT_WATCHDOG_IDLE_SECS;
}

/*****************************************************************************/
/*
Set number of seconds before an unresponded-to WebSocket close is considered
closed and disconnected. If a WebSocket is not specified then set global value.
*/

void WsLibSetCloseSecs
(
struct WsLibStruct *wsptr,
int CloseSecs
)
{
   /*********/
   /* begin */
   /*********/

   if (wsptr)
   {
      /* set WebSocket values */
      if (!(wsptr->WatchDogCloseSecs = CloseSecs))
         wsptr->WatchDogCloseSecs = WatchDogCloseSecs;
      wsptr->WatchDogCloseTime = CurrentTime + wsptr->WatchDogCloseSecs;
   }
   else
   {
      /* set global values */
      if (!(WatchDogCloseSecs = CloseSecs))
        WatchDogCloseSecs = DEFAULT_WATCHDOG_CLOSE_SECS;
   }
}

/*****************************************************************************/
/*
Set number of seconds before a WebSocket is considered idle and closed.
If a WebSocket is not specified then set global value.
*/

void WsLibSetIdleSecs
(
struct WsLibStruct *wsptr,
int IdleSecs
)
{
   /*********/
   /* begin */
   /*********/

   if (wsptr)
   {
      /* set WebSocket values */
      if (!(wsptr->WatchDogIdleSecs = IdleSecs))
         wsptr->WatchDogIdleSecs = WatchDogIdleSecs;
      wsptr->WatchDogIdleTime = CurrentTime + wsptr->WatchDogIdleSecs;
   }
   else
   {
      /* set global values */
      if (!(WatchDogIdleSecs = IdleSecs))
        WatchDogIdleSecs = DEFAULT_WATCHDOG_IDLE_SECS;
   }
}

/*****************************************************************************/
/*
Set number of seconds before a WebSocket is considered ping and closed.
If a WebSocket is not specified then set global value.
*/

void WsLibSetPingSecs
(
struct WsLibStruct *wsptr,
int PingSecs
)
{
   /*********/
   /* begin */
   /*********/

   if (wsptr)
   {
      /* set WebSocket values */
      if (!(wsptr->WatchDogPingSecs = PingSecs))
         wsptr->WatchDogPingSecs = WatchDogPingSecs;
      wsptr->WatchDogPingTime = CurrentTime + wsptr->WatchDogPingSecs;
   }
   else
   {
      /* set global values */
      if (!(WatchDogPingSecs = PingSecs))
        WatchDogPingSecs = DEFAULT_WATCHDOG_PING_SECS;
   }
}

/*****************************************************************************/
/*
Set number of seconds a WebSocket read will wait on the client.
If a WebSocket is not specified then set global value.
*/

void WsLibSetReadSecs
(
struct WsLibStruct *wsptr,
int ReadSecs
)
{
   /*********/
   /* begin */
   /*********/

   if (wsptr)
   {
      /* set WebSocket values */
      if (!(wsptr->WatchDogReadSecs = ReadSecs))
         wsptr->WatchDogReadSecs = WatchDogReadSecs;
   }
   else
   {
      /* set global values */
      if (!(WatchDogReadSecs = ReadSecs))
        WatchDogReadSecs = DEFAULT_WATCHDOG_IDLE_SECS;
   }
}

/*****************************************************************************/
/*
Set/reset the ping (actually pong) callback function.
*/

void* WsLibSetPongCallback
(
struct WsLibStruct *wsptr,
void *AstFunction
)
{
   void  *PrevCallback;

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

   if (wsptr)
   {
      PrevCallback = wsptr->PongCallbackFunction;
      wsptr->PongCallbackFunction = AstFunction;
   }
   else
   {
      PrevCallback = PongCallbackFunction;
      PongCallbackFunction = AstFunction;
   }
   return (PrevCallback);
}

/*****************************************************************************/
/*
Set/reset the wake callback function to the specified number of seconds
(defaults to the global setting).  Returns the previous callback pointer.
*/

void* WsLibSetWakeCallback
(
struct WsLibStruct *wsptr,
void *AstFunction,
int WakeSecs
)
{
   void  *PrevCallback;

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

   if (wsptr)
   {
      /* set WebSocket values */
      if (!(wsptr->WatchDogWakeSecs = WakeSecs))
         wsptr->WatchDogWakeSecs = WatchDogWakeSecs;
      wsptr->WatchDogWakeTime = CurrentTime + wsptr->WatchDogWakeSecs;

      PrevCallback = wsptr->WakeCallbackFunction;
      wsptr->WakeCallbackFunction = AstFunction;
   }
   else
   {
      /* set global values */
      if (!(WatchDogWakeSecs = WakeSecs))
         WatchDogWakeSecs = DEFAULT_WATCHDOG_WAKE_SECS;
      WatchDogWakeTime = CurrentTime + WatchDogWakeSecs;

      PrevCallback = WakeCallbackFunction;
      WakeCallbackFunction = AstFunction;
   }
   return (PrevCallback);
}

/*****************************************************************************/
/*
Set/reset the error callback function.  Returns the previous callback pointer.
*/

void* WsLibSetMsgCallback
(
struct WsLibStruct *wsptr,
void *AstFunction
)
{
   void  *PrevCallback;

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

   PrevCallback = wsptr->MsgCallbackFunction;
   wsptr->MsgCallbackFunction = AstFunction;
   return (PrevCallback);
}

/*****************************************************************************/
/*
Set wsLIB message data.  The 'FormatString' must be a $FAO compiliant
null-terminated string, with following parametersif required.  Activate any set
message callback.
*/

static void MsgCallback
(
struct WsLibStruct *wsptr,
int LineNumber,
int VmsStatus,
char *FormatString,
...
)
{
   static $DESCRIPTOR (FormatFaoDsc, "");

   int  argcnt, cnt, status;
   unsigned short  slen = 0;
   unsigned long  *vecptr;
   unsigned long  FaoVector [32];
   char  *cptr, *sptr, *zptr;
   char  FormatBuffer [128];
   va_list  argptr;

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

   va_count (argcnt);

   /* bit of a sanity check */
   if (argcnt-4 > 32) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   cnt = 0;
   /* some VERY basic sanity checking */
   for (cptr = FormatString; *cptr; cptr++)
   {
      if (*cptr != '!') continue;
      cptr++;
      if (*cptr == '!') continue;
      cnt++;
      if (*cptr == '#') cnt++;
      if (*(USHORTPTR)cptr == '%T') cnt++; 
      if (*(USHORTPTR)cptr == '%D') cnt++; 
   }
   if (argcnt-4 != cnt) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   zptr = (sptr = FormatBuffer) + sizeof(FormatBuffer)-1;
   for (cptr = "%X!8XL "; *cptr; *sptr++ = *cptr++);
   for (cptr = FormatString; *cptr && sptr < zptr; *sptr++ = *cptr++);
   *sptr = '\0';

   FormatFaoDsc.dsc$a_pointer = FormatBuffer;
   FormatFaoDsc.dsc$w_length = sptr - FormatBuffer;

   vecptr = FaoVector;
   *vecptr++ = VmsStatus;
   va_start (argptr, FormatString);
   for (argcnt -= 4; argcnt; argcnt--)
      *vecptr++ = va_arg (argptr, unsigned long);
   va_end (argptr);

   wsptr->MsgDsc.dsc$b_class = DSC$K_CLASS_S;
   wsptr->MsgDsc.dsc$b_dtype = DSC$K_DTYPE_T;

   for (;;)
   {
      if (wsptr->MsgStringSize)
      {
         wsptr->MsgDsc.dsc$a_pointer = wsptr->MsgStringPtr;
         wsptr->MsgDsc.dsc$w_length = wsptr->MsgStringSize;

         status = sys$faol (&FormatFaoDsc, &slen, &wsptr->MsgDsc, &FaoVector);
         if (VMSnok (status)) WsLibExit (NULL, FI_LI, status);
         if (status != SS$_BUFFEROVF) break;
      }

      if (wsptr->MsgStringSize) free (wsptr->MsgStringPtr);
      wsptr->MsgStringSize += 127;
      wsptr->MsgStringPtr = calloc (1, wsptr->MsgStringSize+1);
      if (!wsptr->MsgStringPtr) WsLibExit (NULL, FI_LI, vaxc$errno);
   }

   wsptr->MsgStringPtr[wsptr->MsgStringLength=slen] = '\0';
   wsptr->MsgDsc.dsc$w_length = slen;

   wsptr->MsgLineNumber = LineNumber;
   sys$gettim (&wsptr->MsgBinTime);

   if (wsptr->MsgCallbackFunction) (*wsptr->MsgCallbackFunction)(wsptr);
}

/****************************************************************************/
/*
Return a pointer to the string descriptor of the latest error string.
*/

struct dsc$descriptor_s* WsLibMsgDsc (struct WsLibStruct *wsptr)

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

   return (&wsptr->MsgDsc);
}

/****************************************************************************/
/*
Return a pointer to the latest error string.
*/

char* WsLibMsgString (struct WsLibStruct *wsptr)

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

   return (wsptr->MsgStringPtr);
}

/****************************************************************************/
/*
Return the wsLIB source code line at which the last error occured.
*/

int WsLibMsgLineNumber (struct WsLibStruct *wsptr)

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

   return (wsptr->MsgLineNumber);
}

/****************************************************************************/
/*
Given a descriptor of UTF-8 convert in-situ to 8 bit ASCII.  The output
descriptor must be provided even if it points to the same storage as the input
descriptor.  Return the length of the converted string or -1 to indicated a
conversion error.  The input string is mangled if an error.
*/

int WsLibFromUtf8Dsc
(
struct dsc$descriptor_s *InDsc,
struct dsc$descriptor_s *OutDsc,
char SubsChar
)
{
   int  len;

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

   if (InDsc->dsc$b_class != DSC$K_CLASS_S &&
       InDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   if (OutDsc->dsc$b_class != DSC$K_CLASS_S &&
       OutDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   len = WsLibFromUtf8 (InDsc->dsc$a_pointer, InDsc->dsc$w_length, SubsChar);
   if (len >= 0) OutDsc->dsc$w_length = len;

   return (SS$_NORMAL);
}

/****************************************************************************/
/*
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 input string is mangled if an error.
*/

int WsLibFromUtf8
(
char *UtfPtr,
int UtfCount,
char SubsChar
)
{
   unsigned char  ch;
   unsigned char  *cptr, *sptr, *zptr;

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

   if (!UtfPtr) return (-1);

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

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

    /* is there a potentially UTF-8 bit pattern here? */
    for (zptr = (cptr = (unsigned char*)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 - (unsigned char*)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 - (unsigned char*)UtfPtr);

   utf8_nbg:
      return (-1);
}

/****************************************************************************/
/*
Given a descriptor of 8 bit ASCII convert it to UTF-8.  Can be done in-situ.
The output descriptor must be provided even if it points to the same storage as
the input descriptor.  Return the length of the converted string or -1 to
indicated a conversion error.

*/

int WsLibToUtf8Dsc
(
struct dsc$descriptor_s *InDsc,
struct dsc$descriptor_s *OutDsc
)
{
   int  len;

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

   if (InDsc->dsc$b_class != DSC$K_CLASS_S &&
       InDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   if (OutDsc->dsc$b_class != DSC$K_CLASS_S &&
       OutDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   len = WsLibToUtf8 (InDsc->dsc$a_pointer, InDsc->dsc$w_length,
                      OutDsc->dsc$a_pointer, OutDsc->dsc$w_length);

   if (len == -1) return (SS$_ABORT);

   return (SS$_NORMAL);
}

/****************************************************************************/
/*
Given a buffer of 8 bit ASCII text convert it to UTF-8.  This can be done
in-situ with the 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 WsLibToUtf8
(
char *InPtr,
int InLength,
char *OutPtr,
int SizeOfOut
)
{
   int  Utf8Count = 0;
   char  *cptr, *czptr, *sptr;

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

   if (!InPtr) return (-1);

   if (InLength == -1) InLength = strlen(InPtr);

   for (czptr = (cptr = InPtr) + InLength; cptr < czptr; cptr++)
      if (*cptr & 0x80) Utf8Count++;

   if (!Utf8Count)
   {
      if (!OutPtr) return (InLength);
      if (OutPtr == InPtr) return (InLength);
      /* just copy to output buffer */
      if (InLength >= SizeOfOut - 1) return (-1);
      memcpy (OutPtr, InPtr, InLength);
      OutPtr[InLength] = '\0'; 
      return (InLength);
   }

   if (InLength + Utf8Count >= SizeOfOut - 1) return (-1);

   cptr = (czptr = InPtr) + InLength - 1;
   if (!(sptr = OutPtr)) sptr = InPtr;
   sptr += InLength - 1;
   sptr += Utf8Count + 1;
   *sptr-- = '\0';
   while (cptr >= czptr)
   {
      if (*cptr & 0x80)
      {
         *sptr-- = (*cptr & 0x3f) | 0x80;
         *sptr-- = ((*cptr-- & 0xc0) >> 6) | 0xc0;
      }
      else
         *sptr-- = *cptr--;
   }

   return (InLength + Utf8Count);
}

/****************************************************************************/
/*
Called with a frame pointer after reading UTF-8 data from the client.
The data is parsed as received (i.e. not necessarily a complete message) to
ensure the UTF-8 (received so far) appears legal.  Provides "fast fail" on
illegal UTF-8.  Return true if legal, false if not.

Algorithm and essential code ...

Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
*/

static int Utf8Legal (struct WsLibFrmStruct *frmptr)

{
static const unsigned char  utf8d[] =
{
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
  8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
  0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
  0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
  0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
  1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
  1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
  1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
};

   int  cnt,
        Utf8Count = 0;
   unsigned int  byte, state, type;
   unsigned char  *cptr, *czptr;
   struct WsLibMsgStruct  *msgptr;

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

   msgptr = frmptr->WsLibMsgPtr;
   state = msgptr->Utf8State;

   if (!frmptr->IOsb.iosb$w_bcnt)
   {
      /* checking code-point at end of message */
      return (state == 0);
   }

   cptr = (unsigned char*)frmptr->DataPtr + frmptr->DataCount;
   czptr = cptr + frmptr->IOsb.iosb$w_bcnt;
   while (cptr < czptr)
   {
      /* for efficiency, concurrently apply masking key */
      if (frmptr->FrameMaskBit)
         *cptr ^= frmptr->MaskingKey[frmptr->MaskCount++&0x3];
      byte = *cptr++;
      type = utf8d[byte];
      state = utf8d[256+(state*16)+type];
      if (!state) Utf8Count++;
   }
   msgptr->Utf8State = state;
   msgptr->Utf8Count += Utf8Count;

   return (state != 1);
}

/*****************************************************************************/
/*
Return the current wsLIB time in seconds (i.e. C-RTL, Unix time).
*/

unsigned int WsLibTime ()

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

   return (CurrentTime);
}

/*****************************************************************************/
/*
Called every second to maintain various events in the WebSocket and application
life-cycle.
*/

static void WatchDog ()

{
   static unsigned long  OneSecondDelta [2] = { -10000000, -1 };
   static unsigned long  ExitTime;

   int  status,
        StringLength;
   char  StringBuffer [256];
   struct WsLibStruct  *wsptr;

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

   sys$gettim (&CurrentBinTime);
   CurrentTime = decc$fix_time (&CurrentBinTime);

   if (0 && ListHead)
      WATCH_WSLIB (ListHead, FI_LI, "WATCHDOG !UL", CurrentTime);

   if (ListHead)
      ExitTime = 0;
   else
   if (!ExitTime)
      ExitTime = CurrentTime + WatchDogLifeSecs;
   else
   if (ExitTime < CurrentTime)
      exit (SS$_NORMAL);

   if (WatchDogWakeTime &&
       WatchDogWakeTime < CurrentTime)
   {
      /* wake globally if requested */
      WatchDogWakeTime = CurrentTime + WatchDogWakeSecs - 1;
      if (WakeCallbackFunction) sys$dclast (WakeCallbackFunction, 0, 0, 0);
   }

   for (wsptr = ListHead; wsptr; wsptr = wsptr->NextPtr)
   {
      /* flush any watch log to disk every second */
      if (wsptr->WatchLog) fsync (fileno(wsptr->WatchLog));

      if (wsptr->WebSocketClosed)
      {
         /* if not yet shut then do so after a little patience */
         if (!wsptr->WatchDogCloseTime)
            if (wsptr->WatchDogCloseSecs)
               wsptr->WatchDogCloseTime = CurrentTime +
                                          wsptr->WatchDogCloseSecs;
            else
               wsptr->WatchDogCloseTime = CurrentTime + WatchDogCloseSecs;
         else
         if (wsptr->WatchDogCloseTime < CurrentTime)
            sys$dclast (Shut, wsptr, 0, 0);
      }
      else
      if (wsptr->WatchDogReadTime &&
          wsptr->WatchDogReadTime < CurrentTime)
      {
         /* advise client to close WebSocket */
         WsLibClose (wsptr, WSLIB_CLOSE_POLICY, "read wait exceeded");
      }
      else
      if (wsptr->WatchDogIdleTime &&
          wsptr->WatchDogIdleTime < CurrentTime)
      {
         /* advise client to close WebSocket */
         WsLibClose (wsptr, WSLIB_CLOSE_POLICY, "idle connection");
      }
      else
      if (wsptr->WatchDogPingTime &&
          wsptr->WatchDogPingTime < CurrentTime)
      {
         /* periodic ping (heartbeat) to remote end */
         StringLength = sprintf (StringBuffer, "%u %u",
                                 ++wsptr->WatchDogPingCount, CurrentTime);
         WsLibPing (wsptr, StringBuffer, StringLength);
         if (wsptr->WatchDogPingSecs)
            wsptr->WatchDogPingTime = CurrentTime +
                                      wsptr->WatchDogPingSecs - 1;
         else
            wsptr->WatchDogPingCount = 0;
      }
      else
      if (wsptr->WatchDogWakeTime &&
          wsptr->WatchDogWakeTime < CurrentTime)
      {
         /* wake if requested */
         wsptr->WatchDogWakeTime = CurrentTime + wsptr->WatchDogWakeSecs - 1;
         if (wsptr->WakeCallbackFunction)
            sys$dclast (wsptr->WakeCallbackFunction, wsptr, 0, 0);
      }
   }

   status = sys$setimr (0, &OneSecondDelta, WatchDog, 0, 0);
   if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);
}                            

/*****************************************************************************/
/*
Exit from the current image reporting to the server WEBSOCKET_OUTPUT stream (if
a channel assigned) or (last-ditch) to <stdout> the exit code module name and
line number.
*/

void WsLibExit
(
struct WsLibStruct *wsptr,
char *SourceModuleName,
int SourceLineNumber,
int status
)
{
   static unsigned long  OneSecondDelta [2] = { -10000000, -1 };
   static $DESCRIPTOR (FaoDsc, "BYE-BYE [!AZ:!UL] %X!8XL\n\0");

   short  slen;
   char  MsgBuffer [256];
   $DESCRIPTOR (MsgDsc, MsgBuffer);

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

   sys$fao (&FaoDsc, &slen, &MsgDsc,
            SourceModuleName, SourceLineNumber, status);

   if (wsptr && wsptr->OutputChannel)
      sys$qiow (EfnWait, wsptr->OutputChannel,
                IO$_WRITELBLK | IO$M_READERCHECK, 0, 0, 0,
                MsgBuffer, slen-1, 0, 0, 0, 0);
   else
   {
      fputs (MsgBuffer, stdout);
      fflush (stdout);
   }

   sys$schdwk (0, 0, OneSecondDelta, 0);
   sys$hiber ();
   sys$delprc (0, 0, 0);
}

/*****************************************************************************/
/*
Free C-RTL allocated memory (such as that returned by WsLibReadGrab()) in an
application neutral fashion (in case non-C-RTL allocations are used in the
future).
*/

void WsLibFree (char *cptr)

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

   free (cptr);
}

/*****************************************************************************/
/*
The 'FormatString' must be a $FAO compliant null-terminated string, with
following parameters.
*/

void WsLibCallout
(
struct WsLibStruct *wsptr,
char *FormatString,
...
)
{
   static $DESCRIPTOR (ErrorFaoDsc, "!!WATCH: $FAO %X!8XL");

   int  argcnt, astatus, cnt, status;
   unsigned short  slen = 0;
   unsigned long  *vecptr;
   unsigned long  FaoVector [32];
   char  *aptr, *cptr;
   char  CalloutBuffer [1024];
   va_list  argptr;
   $DESCRIPTOR (CalloutBufferDsc, CalloutBuffer);
   $DESCRIPTOR (FormatDsc, "");

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

   if (!CgiPlusEscLength && !CgiPlusEotLength) return;

   if (!wsptr->OutputChannel) return;

   va_count (argcnt);

   /* can't call callout while in a callout response delivery */
   if (wsptr->CalloutInProgress) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   /* bit of a sanity check */
   if (argcnt-2 > 32) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   cnt = 0;
   for (cptr = FormatString; *cptr; cptr++)
      if (*cptr == '!' && *(USHORTPTR)cptr != '!!') cnt++;
   if (argcnt-2 != cnt) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   FormatDsc.dsc$a_pointer = FormatString;
   FormatDsc.dsc$w_length = strlen(FormatString);

   vecptr = FaoVector;
   va_start (argptr, FormatString);
   for (argcnt -= 2; argcnt; argcnt--)
      *vecptr++ = va_arg (argptr, unsigned long);
   va_end (argptr);

   status = sys$faol (&FormatDsc, &slen, &CalloutBufferDsc, &FaoVector);
   if (VMSnok(status))
      status = sys$fao (&ErrorFaoDsc, &slen, &CalloutBufferDsc, status);

   /* allocate a pointer plus a buffer (freed by OutputFreeAst()) */ 
   aptr = calloc (1, sizeof(struct WsLibStruct*) + slen);
   if (!aptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   *(struct WsLibStruct**)aptr = wsptr;
   cptr = aptr + sizeof(struct WsLibStruct*);
   memcpy (cptr, CalloutBuffer, slen);

   astatus = sys$setast (0);
   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_WRITELBLK | IO$M_READERCHECK,
                     0, OutputAst, wsptr,
                     CgiPlusEscPtr, CgiPlusEscLength, 0, 0, 0, 0);
   if (VMSok(status))
   {
      wsptr->QueuedOutput++;
      status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                        IO$_WRITELBLK | IO$M_READERCHECK,
                        0, OutputFreeAst, aptr,
                        cptr, slen, 0, 0, 0, 0);
      if (VMSok(status))
      {
         wsptr->QueuedOutput++;
         status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                           IO$_WRITELBLK | IO$M_READERCHECK,
                           0, OutputAst, wsptr,
                           CgiPlusEotPtr, CgiPlusEotLength, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedOutput++;
      }
   }
   if (astatus == SS$_WASSET) sys$setast (1);
}

/*****************************************************************************/
/*
Send a script "WATCH:" callout to the server (if [x]script enabled).  The
'SourceModuleName' can be NULL.  The 'FormatString' must be a $FAO compiliant
null-terminated string, with following parameters.  A specific channel is
assigned for WATCH output so that it can be deassigned as late in request
processing as possible.
*/

void WsLibWatchScript
(
struct WsLibStruct *wsptr,
char *SourceModuleName,
int SourceLineNumber,
char *FormatString,
...
)
{
   static $DESCRIPTOR (ErrorFaoDsc, "!!WATCH: $FAO %X!8XL");
   static $DESCRIPTOR (TimeFaoDsc, "!%T\0");
   static $DESCRIPTOR (Watch1FaoDsc, "!!!!WATCH: [!AZ:!4ZL] !AZ");
   static $DESCRIPTOR (Watch2FaoDsc, "!!!!WATCH: !AZ");
   static struct WsLibStruct  FakeIt;

   int  argcnt, astatus, cnt, status;
   unsigned short  slen = 0;
   unsigned long  *vecptr;
   unsigned long  FaoVector [32];
   char  *aptr, *cptr;
   char  TimeBuffer [32],
         WatchBuffer [1024],
         WatchFao [256];
   va_list  argptr;
   $DESCRIPTOR (FaoDsc, WatchFao);
   $DESCRIPTOR (TimeBufferDsc, TimeBuffer);
   $DESCRIPTOR (WatchBufferDsc, WatchBuffer);

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

   if (!wsptr)
   {
      wsptr = &FakeIt;

      if (!wsptr->WatchScript)
      {
         wsptr->WatchScript = 1;
         if ((cptr = getenv ("WASD_WSLIB_WATCH_LOG")) != NULL)
            if ((wsptr->WatchLog = fopen (cptr, "w", "shr=get")) != NULL)
               WsLibExit (NULL, FI_LI, vaxc$errno);
      }

      if (!wsptr->WatchLog) return;
   }
   else
   {
      if (!wsptr->WatchScript) return;

      if (!wsptr->OutputChannel) return;

      /* can't call callout while in a callout response delivery */
      if (wsptr->CalloutInProgress) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   }

   va_count (argcnt);

   /* bit of a sanity check */
   if (argcnt-4 > 32) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   cnt = 0;
   for (cptr = FormatString; *cptr; cptr++)
   {
      if (*cptr != '!') continue;
      cptr++;
      if (*cptr == '!') continue;
      cnt++;
      if (*cptr == '#') cnt++;
      if (*(USHORTPTR)cptr == '%T') cnt++; 
      if (*(USHORTPTR)cptr == '%D') cnt++; 
   }
   if (argcnt-4 != cnt) WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);

   if (SourceModuleName)
      sys$fao (&Watch1FaoDsc, &slen, &FaoDsc,
               SourceModuleName, SourceLineNumber, FormatString);
   else
      sys$fao (&Watch2FaoDsc, &slen, &FaoDsc, FormatString);
    FaoDsc.dsc$w_length = slen;

   vecptr = FaoVector;
   va_start (argptr, FormatString);
   for (argcnt -= 4; argcnt; argcnt--)
      *vecptr++ = va_arg (argptr, unsigned long);
   va_end (argptr);

   status = sys$faol (&FaoDsc, &slen, &WatchBufferDsc, &FaoVector);
   if (!(status & 1))
      status = sys$fao (&ErrorFaoDsc, &slen, &WatchBufferDsc, status);

   if (!CgiPlusEscLength && !CgiPlusEotLength)
   {
      fprintf (stdout, "%*.*s\n", slen, slen, WatchBuffer);
      return;
   }

   if (wsptr->WatchLog)
   {
      sys$fao (&TimeFaoDsc, 0, &TimeBufferDsc, 0);
      fprintf (wsptr->WatchLog, "%s %*.*s\n",
               TimeBuffer, slen-8, slen-8, WatchBuffer+8);
      return;
   }

   /* allocate a pointer plus a buffer (freed by OutputFreeAst()) */ 
   aptr = calloc (1, sizeof(struct WsLibStruct*) + slen);
   if (!aptr) WsLibExit (wsptr, FI_LI, vaxc$errno);
   *(struct WsLibStruct**)aptr = wsptr;
   cptr = aptr + sizeof(struct WsLibStruct*);
   memcpy (cptr, WatchBuffer, slen);

   astatus = sys$setast (0);
   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_WRITELBLK | IO$M_READERCHECK,
                     0, OutputAst, wsptr,
                     CgiPlusEscPtr, CgiPlusEscLength, 0, 0, 0, 0);
   if (VMSok(status))
   {
      wsptr->QueuedOutput++;
      status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                        IO$_WRITELBLK | IO$M_READERCHECK,
                        0, OutputFreeAst, aptr,
                        cptr, slen, 0, 0, 0, 0);
      if (VMSok(status))
      {
         wsptr->QueuedOutput++;
         status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                           IO$_WRITELBLK | IO$M_READERCHECK,
                           0, OutputAst, wsptr,
                           CgiPlusEotPtr, CgiPlusEotLength, 0, 0, 0, 0);
         if (VMSok(status)) wsptr->QueuedOutput++;
       }
   }
   if (VMSnok(status)) WsLibExit (wsptr, FI_LI, status);
   if (astatus == SS$_WASSET) sys$setast (1);
}

/*****************************************************************************/
/*
Just decrement the queued output counter.
*/

void OutputAst (struct WsLibStruct *wsptr)

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

   if (wsptr->QueuedOutput)
      wsptr->QueuedOutput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   if (wsptr->WebSocketShut) Shut (wsptr);
}

/*****************************************************************************/
/*
The parameter is an address to allocated memory.  The memory contains a pointer
to a WsLib structure and after that opaque data (actually just a write buffer
but what the hey).  Get the pointer to the owning WsLib structure.  Free the
allocated memory.  Decrement the WsLib structure's queued output counter.
*/

void OutputFreeAst (char *aptr)

{
   struct WsLibStruct  *wsptr;

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

   wsptr = *(struct WsLibStruct**)aptr;
   free (aptr);
   if (wsptr->QueuedOutput)
      wsptr->QueuedOutput--;
   else
      WsLibExit (wsptr, FI_LI, SS$_BUGCHECK);
   if (wsptr->WebSocketShut) Shut (wsptr);
}

/*****************************************************************************/
/*
Just used for WATCH purposes.
*/

static char* OpCodeName (int OpCode)

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

   switch (OpCode & 0xf)
   {
      case  0 : return ("continue");
      case  1 : return ("text");
      case  2 : return ("binary");
      case  8 : return ("close");
      case  9 : return ("ping");
      case 10 : return ("pong");
      default : return ("unknown");
   }
}

/*****************************************************************************/
/*
If a CGIplus environment then output the end-of-request sentinal.
*/

void WsLibCgiPlusEof ()

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

   if (!CgiPlusEofLength) return;
   fflush (stdout);
   fputs (CgiPlusEofPtr, stdout);
   fflush (stdout);
}

/*****************************************************************************/
/*
Output the callout end sentinal.
*/

void WsLibCgiPlusEot ()

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

   if (!CgiPlusEotLength) return;
   fflush (stdout);
   fputs (CgiPlusEotPtr, stdout);
   fflush (stdout);
}

/*****************************************************************************/
/*
Output the callout begin sentinal.
*/

void WsLibCgiPlusEsc ()

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

   if (!CgiPlusEscLength) return;
   fflush (stdout);
   fputs (CgiPlusEscPtr, stdout);
   fflush (stdout);
}

/*****************************************************************************/
/*
Output the callout begin sentinal.
*/

void WsLibCalloutStart ()

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

   if (!CgiPlusEscLength) return;
   fflush (stdout);
   fputs (CgiPlusEscPtr, stdout);
   fflush (stdout);
}

/*****************************************************************************/
/*
Return true if it's a CGIplus execution environment.
*/

int WsLibIsCgiPlus ()

{
   static int  InitIs;

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

   if (!InitIs)
   {
      InitIs = 1;
      if (CgiPlusEofPtr = getenv("CGIPLUSEOF"))
         CgiPlusEofLength = strlen(CgiPlusEofPtr);
      if (CgiPlusEscPtr = getenv("CGIPLUSESC"))
         CgiPlusEscLength = strlen(CgiPlusEscPtr);
      if (CgiPlusEotPtr = getenv("CGIPLUSEOT"))
         CgiPlusEotLength = strlen(CgiPlusEotPtr);
   }
   return (CgiPlusEofLength);
}

/*****************************************************************************/
/*
Are CGI variables currently available?  Returns true or false.
*/

int WsLibCgiVarAvailable ()
{
   /*********/
   /* begin */
   /*********/

   return (CgiVariablesAvailable);
}

/*****************************************************************************/
/*
String descriptor equivalent of WsLibCgiVar() with the same requirements and
constraints.  Set 'ValueDsc' to the value of the CGI variable (or empty if it
doesn't exist).  Return the length of the value of -1 if the CGI variable name
does not exist.
*/

int WsLibCgiVarDsc
(
struct dsc$descriptor_s *NameDsc,
struct dsc$descriptor_s *ValueDsc
)
{
   char  *cptr, *czptr, *sptr, *zptr;
   char  VarName [256];

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

   if (NameDsc->dsc$b_class != DSC$K_CLASS_S &&
       ValueDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   if (ValueDsc->dsc$b_class != DSC$K_CLASS_S &&
       ValueDsc->dsc$b_dtype != DSC$K_DTYPE_T) return (LIB$_INVSTRDES);

   zptr = (sptr = VarName) + sizeof(VarName)-1;
   czptr = (cptr = NameDsc->dsc$a_pointer) + NameDsc->dsc$w_length;
   while (cptr < czptr && sptr < zptr) *sptr++ = *cptr++;
   *sptr = '\0';

   if (cptr = WsLibCgiVarNull (VarName))
   {
      for (sptr = cptr; *sptr; sptr++);
      ValueDsc->dsc$w_length = sptr - cptr;
      ValueDsc->dsc$a_pointer = cptr;
      return (SS$_NORMAL);
   }
   else
      return (SS$_ITEMNOTFOUND);
}

/*****************************************************************************/
/*
Return empty string rather than NULL if the CGI variable does not exist.
*/

char* WsLibCgiVar (char* VarName)
{
   char  *cptr;

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

   if (cptr = WsLibCgiVarNull (VarName))
      return (cptr);
   else
      return ("");
}

/*****************************************************************************/
/*
Return the value of a CGI variable regardless of whether it is used in a
standard CGI environment or a WASD CGIplus environment.  Automatically switches
WASD V7.2 into 'struct' mode for significantly improved performance.

Call with 'VarName' empty ("") to synchronize CGIplus requests.  This waits for
a CGIplus variable stream, checks if it's in 'record' or 'struct' mode, reads
the stream appropriately before returning ready to provide variables.

DO NOT modify the character string returned by this function.  Copy to other
storage if this is necessary.  The behaviour is indeterminate if the returned
values are modified in any way!

All CGIplus variables may be returned by making successive calls using a
'VarName' of "*" (often useful when debugging).  NULL is returned when
variables exhausted.
*/

char* WsLibCgiVarNull (char *VarName)
{
#define SOUS sizeof(unsigned short)

   static FILE  *CgiPlusIn;

   int  Length;
   char  WwwVarName [256];
   char  *bptr, *cptr, *sptr;

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

   /* if not using the AST-driven WsLibOnNextRequest() */
   if (!CgiPlusInChannel)
   {
      if (!StructBufferSize)
      {
         /**************/
         /* initialise */
         /**************/

         if (WsLibIsCgiPlus())
         {
            if ((cptr = getenv ("WASD_WSLIB_STRUCT_BUFFER_SIZE")) != NULL)
               StructBufferSize = atoi(cptr);
            if (StructBufferSize < WSLIB_STRUCT_BUFFER_SIZE)
               StructBufferSize = WSLIB_STRUCT_BUFFER_SIZE;
            StructBufferPtr = calloc (1, StructBufferSize);
            if (!StructBufferPtr) WsLibExit (NULL, FI_LI, vaxc$errno);
            StructBufferLength = 0;
         }
         else
            StructBufferSize = -1;
      }

      if (VarName == NULL || !VarName[0])
      {
         /* initialize */
         StructBufferLength = WwwPrefix = 0;
         NextVarNamePtr = StructBufferPtr;
         if (CgiPlusEofPtr == NULL)
            WwwPrefix = (getenv ("WWW_SERVER_SOFTWARE") != NULL);
         if (VarName == NULL) return (NULL);
      }
   }

   if (VarName[0])
   {
      /***************************/
      /* return a variable value */
      /***************************/

      if (*(ULONGPTR)VarName == 'WWW_' && !WwwPrefix)
         VarName += 4;
      else
      if (*(ULONGPTR)VarName != 'WWW_' && WwwPrefix)
      {
         strcpy (WwwVarName, "WWW_");
         strcpy (WwwVarName+4, VarName);
         VarName = WwwVarName;
      }

      if (CgiPlusEofPtr == NULL)
      {
         /* standard CGI environment */
         CgiVariablesAvailable = 1;
         return (getenv (VarName));
      }

      if (!CgiVariablesAvailable) return (NULL);

      if (VarName[0] == '*')
      {
         /* return each CGIplus variable in successive calls */
         if (!(Length = *(USHORTPTR)NextVarNamePtr))
         {
            NextVarNamePtr = StructBufferPtr;
            return (NULL);
         }
         sptr = (NextVarNamePtr += SOUS);
         NextVarNamePtr += Length;
         return (sptr);
      }

      /* return a pointer to this CGIplus variable's value */
      for (bptr = StructBufferPtr;
           Length = *(USHORTPTR)bptr;
           bptr += Length)
      {
         sptr = (bptr += SOUS);
         for (cptr = VarName; *cptr && *sptr && *sptr != '='; cptr++, sptr++)
            if (toupper(*cptr) != toupper(*sptr)) break;
         /* if found return a pointer to the value */
         if (!*cptr && *sptr == '=') return (sptr+1);
      }
      /* not found */
      return (NULL);
   }

   /*****************************/
   /* get the CGIplus variables */
   /*****************************/

   if (RequestSynchronisation == NULL)
      RequestSynchronisation = (void*)WsLibCgiVarNull;
   else
   if (RequestSynchronisation != (void*)WsLibCgiVarNull)
      WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

   /* cannot "sync" in a non-CGIplus environment */
   if (CgiPlusEofPtr == NULL) return (NULL);

   /* the CGIPLUSIN stream can be left open */
   if (CgiPlusIn == NULL)
      if ((CgiPlusIn = fopen (getenv("CGIPLUSIN"), "r")) == NULL)
         WsLibExit (NULL, FI_LI, vaxc$errno);

   CgiVariablesAvailable = WwwPrefix = 0;
 
   /* get the starting record (the essentially discardable one) */
   for (;;)
   {
      cptr = fgets (StructBufferPtr, StructBufferSize, CgiPlusIn);
      if (cptr == NULL) WsLibExit (NULL, FI_LI, vaxc$errno);
      /* if the starting sentinal is detected then break */
      if (*(USHORTPTR)cptr == '!\0' ||
          *(USHORTPTR)cptr == '!\n' ||
          (*(USHORTPTR)cptr == '!!' && isdigit(*(cptr+2)))) break;
   }

   if (*(USHORTPTR)cptr == '!!')
   {
      /********************/
      /* CGIplus 'struct' */
      /********************/

      /* get the size of the binary structure */
      StructBufferLength = atoi(cptr+2);
      if (StructBufferLength <= 0) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      if (StructBufferLength > StructBufferSize)
      {
         while (StructBufferLength > StructBufferSize) StructBufferSize *= 2;
         free (StructBufferPtr);
         StructBufferPtr = calloc (1, StructBufferSize);
         if (!StructBufferPtr) WsLibExit (NULL, FI_LI, vaxc$errno);
         NextVarNamePtr = StructBufferPtr;
      }

      if (!fread (StructBufferPtr, 1, StructBufferLength, CgiPlusIn))
         WsLibExit (NULL, FI_LI, vaxc$errno);
   }
   else
   {
      /*********************/
      /* CGIplus 'records' */
      /*********************/

      /* reconstructs the original 'struct'ure from the records */
      sptr = (bptr = StructBufferPtr) + StructBufferSize;
      while (fgets (bptr+SOUS, sptr-(bptr+SOUS), CgiPlusIn) != NULL)
      {
         /* first empty record (line) terminates variables */
         if (bptr[SOUS] == '\n') break;
         /* note the location of the length word */
         cptr = bptr;
         for (bptr += SOUS; *bptr && *bptr != '\n'; bptr++);
         if (*bptr != '\n') WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
         *bptr++ = '\0';
         if (bptr >= sptr) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
         /* update the length word */
         *(USHORTPTR)cptr = bptr - (cptr + SOUS);
      }
      if (bptr >= sptr) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
      /* terminate with a zero-length entry */
      *(USHORTPTR)bptr = 0;
      StructBufferLength = (bptr + SOUS) - StructBufferPtr;
   }

   /***********************/
   /* variables available */
   /***********************/

   CgiVariablesAvailable = 1;

   sptr = StructBufferPtr + SOUS;
   if (*(ULONGPTR)sptr == 'WWW_') WwwPrefix = 1;

   if (!CalloutDone)
   {
      /* provide the CGI callout to set CGIplus into 'struct' mode */
      fflush (stdout);
      fputs (CgiPlusEscPtr, stdout);
      fflush (stdout);
      /* the leading '!' indicates we're not going to read the response */
      fputs ("!CGIPLUS: struct", stdout);
      fflush (stdout);
      fputs (CgiPlusEotPtr, stdout);
      fflush (stdout);
      /* don't need to do this again (the '!!' tells us what mode) */
      CalloutDone = 1;
   }

   return (NULL);

#  undef SOUS
}

/*****************************************************************************/
/*
Asynchronous notification of the next request becoming available.  This
function should be called once during script initialisation specifying the
address of the function to be notified of a new request.  Until the script
issues the CGIplus EOF sentinal the WsLibCgiVar..() family of functions can be
used to retrieve the CGI variable values.  This asynchronous and the
synchronous WsLibCgiVar("") approaches to request synchronisation are mutually
exclusive and once one is used, using the other results in a bugcheck.
*/

void WsLibOnNextRequest (void *AstFunction)
{
#define SOUS sizeof(unsigned short)

   static int  CGIplusStruct = 0,
               StartSentinal = 0;
   static char  SentinalBuffer [256];
   static $DESCRIPTOR (CgiPlusInDsc, "");
   static struct _iosb  IOsb;
   static void  *RequestAstFunction;

   int  bcnt, status;
   char  *bptr, *cptr;

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

   if (RequestSynchronisation == NULL)
      RequestSynchronisation = WsLibOnNextRequest;
   else
   if (RequestSynchronisation != WsLibOnNextRequest)
      WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

   if (!CgiPlusInChannel)
   {
      /**************/
      /* initialise */
      /**************/

      /* ensure initialisation */
      if (!WsLibIsCgiPlus())
      {
         /* hmmm, standard CGI environment - do something sensible */
         sys$dclast (AstFunction, 0, 0, 0);
         return;
      }

      if (!AstFunction) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
      RequestAstFunction = AstFunction;

      if ((cptr = getenv("CGIPLUSIN")) == NULL)
         WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

      CgiPlusInDsc.dsc$a_pointer = cptr;
      CgiPlusInDsc.dsc$w_length = strlen(cptr);

      status = sys$assign (&CgiPlusInDsc, &CgiPlusInChannel, 0, 0);
      if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);

      if ((cptr = getenv ("WASD_WSLIB_STRUCT_BUFFER_SIZE")) != NULL)
         StructBufferSize = atoi(cptr);
      if (StructBufferSize < WSLIB_STRUCT_BUFFER_SIZE)
         StructBufferSize = WSLIB_STRUCT_BUFFER_SIZE;
      StructBufferPtr = calloc (1, StructBufferSize+2);
      if (!StructBufferPtr) WsLibExit (NULL, FI_LI, vaxc$errno);
      StructBufferLength = 0;

      status = sys$qio (EfnNoWait, CgiPlusInChannel,
                        IO$_READLBLK, &IOsb,
                        WsLibOnNextRequest, NULL,
                        SentinalBuffer, sizeof(SentinalBuffer)-1,
                        0, 0, 0, 0);
      if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);

      return;
   }
   else
   if (AstFunction)
   {
      /* modify AST function */
      RequestAstFunction = AstFunction;
      return;
   }

   /***************/
   /* read stream */
   /***************/

   if (!CgiPlusInChannel) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
   if (AstFunction) WsLibExit (NULL, FI_LI, SS$_BUGCHECK);

   if (IOsb.iosb$w_status && VMSnok(IOsb.iosb$w_status))
      WsLibExit (NULL, FI_LI, IOsb.iosb$w_status);

   if (!StartSentinal)
   {
      cptr = SentinalBuffer;
      cptr[IOsb.iosb$w_bcnt] = '\0';

      /* if the starting sentinal is not detected then ignore */
      if (!(*(USHORTPTR)cptr == '!\0' ||
            *(USHORTPTR)cptr == '!\n' ||
            (*(USHORTPTR)cptr == '!!' && isdigit(*(cptr+2)))))
      {
         status = sys$qio (EfnNoWait, CgiPlusInChannel,
                           IO$_READLBLK, &IOsb,
                           WsLibOnNextRequest, NULL,
                           SentinalBuffer, sizeof(SentinalBuffer)-1,
                           0, 0, 0, 0);
         if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);
         return;
      }

      StartSentinal = 1;
      bcnt = CgiVariablesAvailable = StructBufferLength = 0;

      if (*(USHORTPTR)cptr == '!!' && isdigit(*(cptr+2)))
      {
         /* get the size of the binary structure */
         StructBufferLength = atoi(cptr+2);
         if (StructBufferLength <= 0)
            WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
         if (StructBufferLength > StructBufferSize)
         {
            while (StructBufferLength > StructBufferSize)
               StructBufferSize *= 2;
            free (StructBufferPtr);
            StructBufferPtr = calloc (1, StructBufferSize+2);
            if (!StructBufferPtr) WsLibExit (NULL, FI_LI, vaxc$errno);
            NextVarNamePtr = StructBufferPtr;
         }
         CGIplusStruct = 1;
         /* drop through to the structure processing code */
      }
      else
      {
         /* read the first variable record */
         status = sys$qio (EfnNoWait, CgiPlusInChannel,
                           IO$_READLBLK, &IOsb,
                           WsLibOnNextRequest, NULL,
                           StructBufferPtr + SOUS,
                           StructBufferSize - SOUS,
                           0, 0, 0, 0);
         if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);
         CGIplusStruct = 0;
         return;
      }
   }
   else
   if (CGIplusStruct)
   {
      cptr = StructBufferPtr + StructBufferLength;
      cptr[bcnt = IOsb.iosb$w_bcnt] = '\0';
   }
   else
   {
      cptr = StructBufferPtr + StructBufferLength + SOUS;
      cptr[bcnt = IOsb.iosb$w_bcnt] = '\0';
   }

   if (CGIplusStruct)
   {
      /********************/
      /* CGIplus 'struct' */
      /********************/

      if (bcnt < StructBufferLength)
      {
         status = sys$qio (EfnNoWait, CgiPlusInChannel,
                           IO$_READLBLK, &IOsb,
                           WsLibOnNextRequest, NULL,
                           StructBufferPtr,
                           StructBufferLength, 0, 0, 0, 0);
         if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);
         return;
      }

      if (bcnt > StructBufferLength)
         WsLibExit (NULL, FI_LI, SS$_BUGCHECK);
   }
   else
   {
      /*********************/
      /* CGIplus 'records' */
      /*********************/

      /* first empty record terminates variables */
      if (bcnt)
      {
         /* reconstructs the original 'struct'ure from the records */
         bptr = StructBufferPtr + StructBufferLength;
         /* update the length word */
         *(USHORTPTR)bptr = bcnt;
         /* terminate with a zero-length entry */
         bptr += bcnt + SOUS;
         *(USHORTPTR)bptr = 0;
         StructBufferLength = bptr - StructBufferPtr;

         status = sys$qio (EfnNoWait, CgiPlusInChannel,
                           IO$_READLBLK, &IOsb,
                           WsLibOnNextRequest, NULL,
                           StructBufferPtr + StructBufferLength + SOUS,
                           StructBufferSize - StructBufferLength - SOUS,
                           0, 0, 0, 0);
         if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);

         return;
      }
   }

   /***********************/
   /* variables available */
   /***********************/

   bptr = StructBufferPtr + SOUS;
   if (*(ULONGPTR)bptr == 'WWW_') WwwPrefix = 1;

   if (!CalloutDone)
   {
      /* provide the CGI callout to set CGIplus into 'struct' mode */
      fflush (stdout);
      fputs (CgiPlusEscPtr, stdout);
      fflush (stdout);
      /* the leading '!' indicates we're not going to read the response */
      fputs ("!CGIPLUS: struct", stdout);
      fflush (stdout);
      fputs (CgiPlusEotPtr, stdout);
      fflush (stdout);
      /* don't need to do this again */
      CalloutDone = 1;
   }

   CgiVariablesAvailable = 1;

   sys$dclast (RequestAstFunction, 0, 0, 0);

   /********************************/
   /* queue ready for next request */
   /********************************/

   StartSentinal = 0;

   status = sys$qio (EfnNoWait, CgiPlusInChannel,
                     IO$_READLBLK, &IOsb,
                     WsLibOnNextRequest, NULL,
                     SentinalBuffer, sizeof(SentinalBuffer)-1,
                     0, 0, 0, 0);
   if (VMSnok(status)) WsLibExit (NULL, FI_LI, status);

#  undef SOUS
}

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