[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]
/*****************************************************************************/
/*
                                HyperReader.c


A CGI-compliant script that reads (many) Bookreader-format documents and
presents them as HTML documents.

AND REMEMBER ... HyperReader can be used as a CGIplus script. This may require
the script to be purged from the server before new startup parameters come
into effect. Use a command like the following:

  HTTPD /DO=DCL=PURGE


SPECIFYING BOOKS
----------------
Books are specified in one of two ways, with the indicated precedence.

  1.  ?file= .......... request query string form field (VMS file spec)
  2.  /path/to/file ... path into VMS file spec via WWW_PATH_TRANSLATED

If a book has a known shelf extension (see storage 'BookshelfTypes') a 302
redirection is generated to the shelf processor (/hypershelf), likewise if a
book is not supplied.  The assumption being that the user knows no better than
to use HyperReader for everything!


BOOKS ACCESSED VIA DECNET
-------------------------

If any books are being accessed via DECnet, that is if a shelf contains
anything like

  book\NODE::DISK:[DECW$BOOK]BOOK.DECW$BOOK\A Book

the script may return the following error

  %RMS-E-NETBTS, network buffer too small for !UL byte record

Hyperreader attempts to cope with varying record sizes by always providing
enough space ;^) ... the "rab$w_usz = 32767;" in GetChunk(). Add the following
to the HyperReader support DCL procedure, WASD_ROOT:[SCRIPT]HYPERREADER.COM for
WASD, WWW_ROOT:[BIN]HYPERREADER.COM for OSU ...

  $ SET RMS /NETWORK_BLOCK_COUNT=127


OSU ENVIRONMENT
---------------
Script responses are returned in OSU "raw" mode; the script taking care of the
full response header and correctly carriage-controlled data stream, text or
binary!!  Uses the CGILIB.C to engage in the dialog phase generating, storing
and then making available the equivalent of CGI variables.


"VANILLA" CGI ENVIRONMENT
-------------------------
Primarily for the likes of Netscape FastTrack.  This environment can accomodate
CGI variables that are not prefixed with "WWW_" and do not supply "KEY_xxxxx"
or "FORM_xxxxx" (which must be derived from "QUERY_STRING").  Full HTTP stream
(non-parsed header) is assumed as not supported so all output occurs with a
CGI-compliant header line (e.g. "Status: 200 Success") and record-oriented
output.


ABOUT HYPERREADER
-----------------

Sorry about this utility.  When I (MGD) first began reverse-engineering the 
structure I didn't include copious notes (or even any ... well it was just
investigation), so now (late '94) I have only a vague idea of what some 
sections are all about.  Any new development has better commentary!  Also, no 
guarantees are given on any of this code, its all fairly tenuous.  The program 
has grown by accretion, more a vehicle of experimentation than a paradigm of 
software engineering.  This utility will probably always exist in a below-
version-1, as it will probably never be entirely satisfactory.  Well, enough 
of apologies! 

Code is included that should provide an informational message within the 
generated HTML whenever the utility encounters Bookreader formatting it cannot 
process.  During development/debugging this can be checked for clues. 

The code that generates the GIF output is, in part, derived from other works
from the PBM suite.  Some is copyright and used within the original licence
conditions:

 * GIF Image compression - LZW algorithm implemented with Trie type
 *                         structure.
 *                         Written by Bailey Brown, Jr.
 *                         last change May 24, 1990
 *                         file: compgif.c
 *
 *  You may use or modify this code as you wish, as long as you mention
 *  my name in your documentation.
 *
 *                  - Bailey Brown, Jr.


BOOKREADER DOCUMENTS ARE NOT PLAIN TEXT
---------------------------------------

They are not even marked-up text (in the same sense as HTML documents).  
Essentially they are a series of images pasted onto an X window.  Most of the 
images happen to be from fonts, but even then characters are not handled as 
text.  There is essentially no information within the file on the documentary 
function of any text in a book.  This is one reason for the poor performance 
of the internal Bookreader print functionality (it must adopt a similar 
approach, mapping into PostScript "pages"). 

Bookreader format is not public.

At least not that I could find.  Hence considerable effort at reverse-
engineering the structure (most originally during January 1992 fortunately, I 
wouldn't have the patience, time or interest now!)  In any case, the structure 
is complex and I don't pretend to understand it all ... so the server tends to 
break occasionally. 

The server attempts to map a bit-addressed format (Bookreader) onto a 
character-cell format (text-page).  The Bookreader format permits parts of a 
page to be generated non-sequentially, for example to place normal text, then 
place bolded text within it.  This can occur on a per-line, per-section or 
per-page basis.  So the entire "page" for a hypertext enviroment must be 
created by mapping into a "page" in memory.  Sometimes this mapping is not 
perfect and character strings get garbled or poorly proportioned. 

Text is presented in a fixed font.  This is due to two requirements: 

  1.  The mapping from bit-addressed to cell-addressed described
      above requires a fixed character-cell matrix. 

  2.  Maintaining correct layout for examples, tables, etc.  As the
      Bookreader format specifies the positioning of the document's
      components, rather than information on their function, relative
      location must be maintained, generally eliminating the use of the
      more aesthetically pleasing proportional fonts. 

Some massaging of the resulting page occurs.  This often involves educated 
guesswork.  For instance, single blank lines preceding a line beginning with a 
lower-case alphabetic are most often an artifact of the mapping process rather 
than intended line breaks, and can be eliminated to produce a better layout.  
Similarly, bulleted and numbered lists can be enhanced by ensuring a blank 
line exists between elements.  Unfortunately there are not many instances 
where the original intent can be clearly identified. 

As this is a stateless, client-server application any dynamic memory allocated 
will be released on image exit, and does not need to be explicitly released.


-POSSIBLE- BOOKREADER BOOK STRUCTURE (REVERSE-ENGINEERED SO FAR!):
------------------------------------------------------------------

First Record
------------
fixed size of 0x3fe
part type:                      short   0x00 (type=0x0001)
part length:                    long    0x02
number of parts in book         long    0x46
number of sections in book      long    0x4a
number of fonts in book         long    0x52
VBN (512) of last part          long    0x5a
position in VBN of last part    short   0x5e


Text Records
------------
variable size
part type:                      short   0x00 (type=0x0003)
part length:                    long    0x02
previous text part number       long    0x06
continued part                  long    0x0a (if > 1 then continues previous)
sections of text in this part   long    0x0e
previous chunk part number      long    0x12
chunk ends at part number       long    0x16
.
then from offset 0x1a
.
Sections within a text record
-----------------------------
section type                    short   0x00
.
if section type is 0x0012 (printable text)
section length                  long    0x02
section data 2                  long    0x06
this section number             long    0x0a
length of on-screen section     long    0x1e
section data 5                  long    0x22
section data 6                  long    0x26
section data 7                  long    0x2a
section data 8                  long    0x2e
section data 9                  long    0x32
section data 10                 long    0x36
.
if section type is 0x0013 (figure)
.
if section type is 0x0014 (graphic (hotspots, etc))
.
Printable text section
----------------------
type of text                    char    0x00
length of text section          char    0x01
horizonal position              short   0x02
vertical position               short   0x04
font number                     char    0x06
unknown                         short   0x07
.
text begins at offset 0x09, number of printable characters:
text length                     char    0x00
1..text length characters
then byte indicating decipoint(?) spacing between next printable characters


SectionXref Record
------------------
variable size
part type:                      short   0x00 (type=0x0006)
part length:                    long    0x02
then starting at offset 0x06:

Table of Contents Record
------------------------
variable size
part type:                      short   0x00 (type=0x0007)
part length:                    long    0x02
data 1                          long    0x06
data 2                          long    0x0a
data 3                          long    0x0e
data 4                          long    0x12
data 5                          long    0x16
data 6                          long    0x1a
data 7                          long    0x1e
then starting at offset 0x22:
a number of variable length units each comprising:
(the series of units is terminated by a byte of 0x00 at offset 0x06)
unknown                         short   0x00
unknown                         short   0x02
unknown                         short   0x04
null ('\0') terminted string at offset 0x06.


References to Parts (symbols) Record 
------------------------------------
variable size
part type:                      short   0x00 (type=0x000d)
part length:                    long    0x02
then starting at offset 0x06:
(1 .. number of sections in the book) series of 32 byte units, each:
null ('\0') terminted symbol string (can be just the number of the section)


Font Record
-----------
variable size
part type:                      short   0x00 (type=0x0009)
part length:                    long    0x02
then starting at offset 0x06:
(1 .. number of fonts in the book) series of variable byte units, each:
unknown                         short   0x00
unknown                         short   0x02
unknown                         short   0x04
font number (used in text)      short   0x06
null ('\0') terminted font name string


"Last" Record
-------------
variable size
part type:                      short   0x00 (type=0x0005)
part length:                    long    0x02
then starting at offset 0x06:
(1 .. number of parts in the book) series of 10 byte units, each:
VBN (512) of part               long    0x00
position in VBN of part         short   0x04
length of this part             long    0x06


CGI VARIABLES
-------------

Server ...

WWW_HTTP_IF_MODIFIED_SINCE      if "304 Not modified" are desired (optional)
WWW_HTTP_PRAGMA         "no-cache" (optional)
WWW_HTTP_REFERER        "close" button becomes active
WWW_PATH_INFO           URL path to book
WWW_PATH_TRANSLATED     VMS file specification for book
WWW_REQUEST_METHOD      "GET" only is supported
WWW_SCRIPT_NAME         path to script (e.g. "/HyperReader")
WWW_SERVER_CHARSET      default character set of server
WWW_SERVER_NAME         host on which the script is executing
WWW_SERVER_PORT         server port
WWW_SERVER_SOFTWARE     HTTPd identifying string

HyperReader ...

WWW_FORM_CHUNK          "chunk" number (section) of the book
WWW_FORM_FILE           VMS file name for book (when path cannot be supplied)
WWW_FORM_GRAPHIC        if not empty the get an image from the book
WWW_FORM_MASSAGE        if not empty then "massage" extra white space out
WWW_FORM_REFERER        overrides the HTTP_REFERER URL
WWW_FORM_TITLE          explicit title of book
WWW_FORM_VARREC         force record/block IO (debugging only)


QUALIFIERS
----------
/BUTTONS=       string containing button names
/CHARSET=       "Content-Type: text/html; charset=...", empty suppresses charset
/DBUG           turns on all "if (Debug)" statements (don't use with OSU)
/HYPERSHELF=    name of 'HyperShelf' script (defaults to "/hypershelf")
/[NO]ODS5       control extended file specification (basically for testing)


LOGICAL NAMES
-------------
HYPERREADER$DBUG        turns on all "if (Debug)" statements
HYPERREADER$NODOWNLOAD  disable the ability to download books
HYPERREADER$PARAM       equivalent to (overrides) the command line
                        parameters/qualifiers (define as a system-wide logical)
HTTPD$GMT               timezone offset from Greenwich Mean Time
                        (e.g. "+09:30" and only needed if DTSS is not in use)


BUILD DETAILS
-------------
See BUILD_HYPERREADER.COM


COPYRIGHT
---------
Copyright (C) 1992-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.


HISTORY (also change SOFTWAREVN below!)
------- 
28-MAR-2021  MGD  v0.9.2, add [Download] button, then ...
                            $ set file /attr=(rfm:var,rat:none,lrl:32254)
                          bugfix; GetRecord() rabptr->rab$w_rfa4 = 512 - byte;
                          (who'd have thought I'd still be tinkering in 2021)
31-AUG-2020  MGD  v0.9.1, bugfix; eliminate &referer= XSS vulnerability
06-OCT-2014  MGD  v0.9.0, although now largely only of historical interest ...
                            ... a nod to the twenty-first century
                            (and happy 90th birthday Dad)
10-MAY-2005  MGD  v0.8.27, SWS 2.0 ignore query string components supplied as
                           command-line parameters differently to CSWS 1.2/3
23-DEC-2003  MGD  v0.8.26, minor conditional mods to support IA64
12-APR-2003  MGD  v0.8.25, link colour changed to 0000cc
20-AUG-2002  MGD  v0.8.24, allow for HTTP/1.1 'Cache-control:' field used
                           by Mozilla variants (instead of HTTP/1.0 'pragma:')
15-AUG-2002  MGD  v0.8.23, massage source code (can't resist a fiddle)
08-DEC-2001  MGD  v0.8.22, use RMS block IO for non-VAR record format books
01-JUL-2001  MGD  v0.8.21, add 'SkipParameters' for direct OSU support
28-OCT-2000  MGD  v0.8.20, use CGILIB object library
18-JAN-2000  MGD  v0.8.19, support extended file specifications (ODS-5),
                           improved GetChunk() record handling
07-AUG-1999  MGD  v0.8.18, use more of the CGILIB functionality
24-APR-1999  MGD  v0.8.17, use CGILIB.C,
                           standard CGI environment (Netscape FastTrack)
02-OCT-1998  MGD  v0.8.16, provide content-type "; charset=..."
08-AUG-1998  MGD  v0.8.15, redirection if looks like meant for shelf processor,
                           OSU output processing reworked,
                           bugfix; TimeSetTimezone() 'Seconds' unsigned->signed
06-AUG-1998  MGD  v0.8.14, accomodation for OSU ... reduce HTTP response
                           header carriage control from <CR><LF> to <LF> only
                           (OSU/IE4 combination problematic)
01-AUG-1998  MGD  v0.8.13, suppress table background colours if empty,
                           accomodations for OSU environment
28-APR-1998  MGD  v0.8.12, bit more playing around; book section descriptions
                           (table-of-contents, etc) now generated from book
                           content (non-English documents now work!!!!),
                           remove X-bitmap image generation,
                           some tidying up and cosmetic changes
13-MAR-1998  MGD  v0.8.11, TimeSetGmt() modified to be in line with HTTPd,
                           added check for too many chunks, sections and fonts
01-AUG-1997  MGD  v0.8.10, 'HttpHasBeenOutput' not initialized for CGIplus
20-JUL-1997  MGD  v0.8.9, added /BODY= qualifier (changed background colour)
07-JUL-1997  MGD  v0.8.8, CGIplus capable,
                          hopefully improved dynamic memory handling by setting
                          a minimum allocation reducing fragmentation
30-JUN-1997  MGD  v0.8.7, very minor changes to format,
                          "Pragma: no-cache" now overrides "If-Modified-Since:"
                          (as always, I look at this program and shudder :^)
16-AUG-1996  MGD  v0.8.6, presentation changes (XBMs to GIFs)
23-FEB-1996  MGD  v0.8.5, bugfix, after modifying the HTTPD$GMT format for the
                          HTTPd server I had forgotten about some scripts
12-OCT-1995  MGD  v0.8.4, 'path_translated' CGI variable now used to allow
                          books to be specified using HTTP URL syntax;
                          add 'Referer:', 'Last-Modified:'/'If-Modified-Since:'
02-AUG-1995  MGD  v0.8.3, changed detection of table of contents, figures, etc.,
                          to substring in function FirstChunk(); changes to
                          section processing, recognising figure types, etc.
24-MAY-1995  MGD  v0.8.2, minor changes for AXP compatibility
15-APR-1995  MGD  v0.8.1, made CGI-compliant, added "close" button functionality
12-NOV-1994  MGD  v0.8.0, breakthrough in understanding the layout of text,
                          I think!  All page layup functions revised.
01-NOV-1994  MGD  v0.7.0, support X-bitmap images, GIF images, X-bitmap ICON
                          buttons, direct RMS file access (much more efficient)
07-OCT-1994  MGD  v0.6.0, back from the dead ... for the hypertext environment
14-JAN-1992  MGD  v0.5.0, initial development as "BREAD v0.5.0" ...
                          because it was only half-baked  :^)
*/
/*****************************************************************************/

#define SOFTWAREVN "0.9.2"
#define SOFTWARENM "HYPERREADER"
#define SOFTWARECR "Copyright (C) 1996-2021 Mark G.Daniel"
#ifdef __ALPHA
#  define SOFTWAREID SOFTWARENM " AXP-" SOFTWAREVN
#endif
#ifdef __ia64
#  define SOFTWAREID SOFTWARENM " IA64-" SOFTWAREVN
#endif
#ifdef __VAX
#  define SOFTWAREID SOFTWARENM " VAX-" SOFTWAREVN
#endif
#ifdef __x86_64
#  define SOFTWAREID SOFTWARENM " X86-" SOFTWAREVN
#endif

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

/* VMS-related header files */
#include <descrip.h>
#include <file.h>
#include <iodef.h>
#include <libdef.h>
#include <lnmdef.h>
#include <rms.h>
#include <ssdef.h>
#include <stsdef.h>
#include <syidef.h>
#include <unixio.h>

/* application header file */
#include "enamel.h"
#include "hyperreader.h"
#include <cgilib.h>

#ifndef __VAX
#   pragma nomember_alignment
#endif

/***************/
/* definitions */
/***************/

#ifndef __VAX
#  ifndef NO_ODS_EXTENDED
#     define ODS_EXTENDED 1
      /* this is smaller than the technical maximum, but still quite large! */
#     define ODS_MAX_FILE_NAME_LENGTH 511
#     define ODS_MAX_FILESYS_NAME_LENGTH 264
#  endif
#endif
#define ODS2_MAX_FILE_NAME_LENGTH 255
#ifndef ODS_MAX_FILE_NAME_LENGTH
#  define ODS_MAX_FILE_NAME_LENGTH ODS2_MAX_FILE_NAME_LENGTH
#endif
#if ODS_MAX_FILE_NAME_LENGTH < ODS2_MAX_FILE_NAME_LENGTH
#  define ODS_MAX_FILE_NAME_LENGTH ODS2_MAX_FILE_NAME_LENGTH
#endif

#define BOOL int
#define true 1
#define false 0

#define VMSok(x) ((x) & STS$M_SUCCESS)
#define VMSnok(x) !(((x) & STS$M_SUCCESS))
#define VMSwarning(x) (((x) & 0x7) == STS$K_WARNING)
#define VMSerror(x) (((x) & 0x7) == STS$K_ERROR)
#define VMSinfo(x) (((x) & 0x7) == STS$K_INFO)
#define VMSfatal(x) (((x) & 0x7) == STS$K_SEVERR)
 
#define FI_LI __FILE__, __LINE__

#define MAX_OUTLINE_LENGTH 128
#define MAX_DIVISIONS 16

#define LINES_ON_PAGE_PREALLOCATE 10
/* maximum of 1 is hardly consecutive ... but experience shows it looks ok! */
#define MAX_CONSECUTIVE_BLANK_LINES 1

/*
   Whenever a non-printable charater is encountered the "interim" is
   substituted in the text.  Before the text is output these characters
   are used as markers for "lists" etc., as the text is "massaged".
   They are then replaced with the "final" character.  These appear most
   often to be "bullets" for lists.
*/
#define INTERIM_NON_ASCII_CHAR 0x7f
#define FINAL_NON_ASCII_CHAR '*'
/*
   For characters of any fonts not supported substitute this character.
   These are usually "graphic"/"symbol" characters/fonts.
*/
#define NON_SUPPORTED_CHAR '~'

/* seem to map bit-addressed to cell-addressed ok most of the time */
#define LINE_POSITIONING_UNITS 68
#define COLUMN_POSITIONING_UNITS 34

#define DEFAULT_BUTTONS \
"&larr;Previous$&#8630;Back=javascript:parent.history.go(-1)$\
Next&rarr;$Close$Download$^Help=" DEFAULT_HYPERREADER_HELP

/**********/
/* macros */
/**********/

/*
   This macro generates an address (pointer) to an output buffer character.
   It allows a linear array of structures to be addressed with a line/column
   value as if it was a matrix.  A macro is used for speed (no function 
   overhead), and readability (easier to read than the computation each time).
*/
#define OUT_CHAR_AT(Line, Column) \
((struct OutCharStruct*)(OutPtr+(Line*MAX_OUTLINE_LENGTH)+Column))

/* try to avoid excessive fragmentation by specifying a minimum chunk */
#define MIN_HTML_STRING_ALLOC 128

/**************/
/* data types */
/**************/

struct ChunkDataStruct
{
   unsigned long  VBN;
   unsigned short  VBNbyte;
   unsigned long  Length;
};

struct OutCharStruct
{
   char  c;
   char  *htptr;
};

struct FontStruct
{
   int  FontSize;
   BOOL  FontBold;
   BOOL  FontItalic;
   BOOL  FontUnsupported;
};

/* my divisions are the "Contents", "Index", etc. */
struct BookDivisionStruct
{
   char  *DescriptionPtr;
   int  ChunkNumber;
};

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

char ErrorBookType [] = "Doesn't look like a book type!",
     ErrorCalloc [] = "allocating memory.",
     ErrorCallocBytes [] = "memory allocation of %d bytes failed",
     ErrorChunkRead [] = "chunk read problem.",
     ErrorDownloadBook [] = "Download book",
     ErrorImageSpecification [] = "Image specification incorrect.",
     ErrorRequestedChunk [] = "Could not find requested part of book!",
     ErrorStringOverflow [] = "String overflow!",
     ErrorTOC [] = "Could not locate table-of-contents!",
     ErrorTooManyDivisions [] =
"Too many &quot;divisions&quot; in book! \
Recompile with increased MAX_DIVISIONS.";

char  *BookshelfTypes [] = { ".ODL", ".BKS", ".DECW$BOOKSHELF", "" };
char  *BookTypes [] = { ".BKB", ".DECW$BOOK", "" };

char  Utility [] = "HYPERREADER";

BOOL  BookFileVarRec,
      Debug,
      DebugBytes,
      DebugFigures,
      DebugFonts,
      DebugGraphics,
      DebugHotspots,
      DebugChunks,
      DebugSections,
      DebugSymbols,
      DebugText,
      DoMassage,
      DoMassageOutput,
      ErrorReported,
      EnhanceText,
      IsCgiPlus,
      NoDownloadBook,
      OsuEnvironment,
      StdCgiEnvironment,
      TimeAheadOfGmt;

int  BookFileLongestRecordLength,
     ChunkNumber,
     DynamicMemoryAllocated,
     EndGlobalVertical,
     GetChunkNumber,
     GlobalVertical,
     GlobalHorizontal,
     LastLineOnPage,
     LinesOnPage,
     LinesOutputToClient,
     NextChunkNumber,
     OdsExtended,
     PreviousChunkNumber,
     TablesChunkNumber,
     TotalBytesRead,
     TotalBytesWritten;

#define TBW TotalBytesWritten

long  NumberOfChunks,
      NumberOfSections,
      NumberOfFonts,
      CurrentVBN,
      LastVBN,
      LastVBNlen;

unsigned long  IfModifiedSinceBinaryTime [2],
               TimeGmtDeltaBinary [2];

short  LastVBNbyte;

char  BookSpecifiedByFile [ODS_MAX_FILE_NAME_LENGTH+1],
      BookTitle [256],
      ContentTypeCharset [64],
      DefaultButtons [] = DEFAULT_BUTTONS,
      DefaultStyle [] = DEFAULT_HYPERREADER_STYLE,
      ExpBookFileName [ODS_MAX_FILE_NAME_LENGTH+1],
      LastModifiedGmDateTime [32],
      SoftwareCopy [] = SOFTWARECR,
      SoftwareID [48],
      TimeGmtString [32],
      TimeGmtVmsString [32],
      UrlEncReferer [1024],
      UrlEncTitle [512];

char  *BookFileNamePtr,
      *ButtonsPtr = DefaultButtons,
      *CgiEnvironmentPtr,
      *CgiFormChunkPtr,
      *CgiFormFilePtr,
      *CgiFormGraphicPtr,
      *CgiFormMassagePtr,
      *CgiFormRefererPtr,
      *CgiFormTitlePtr,
      *CgiFormVarRecPtr,
      *CgiHttpCacheControlPtr,
      *CgiHttpHostPtr,
      *CgiHttpPragmaPtr,
      *CgiHttpIfModifiedSincePtr,
      *CgiPathInfoPtr,
      *CgiPathTranslatedPtr,
      *CgiQueryStringPtr,
      *CgiRequestMethodPtr,
      *CgiRequestSchemePtr,
      *CgiScriptNamePtr,
      *CgiServerNamePtr,
      *CgiServerSoftwarePtr,
      *CgiServerPortPtr,
      *CharsetPtr,
      *CliCharsetPtr,
      *ExpBookFileNamePtr,
      *HyperShelfScriptNamePtr,
      *StyleSheetPtr = "",
      *UrlDoMassage,
      *UrlEncodedCgiPathInfoPtr;

long  *SectionChunkNumbersArrayPtr;

struct FontStruct  *FontArrayPtr;

struct ChunkDataStruct  *ChunkArrayPtr;

/* HyperReader divisions are the "Contents", "Index", etc. */
struct BookDivisionStruct  BookDivision [MAX_DIVISIONS+1];
int  BookDivisionCount;

struct OutCharStruct  *OutPtr;

unsigned char  *ChunkBufferPtr;
int  PreviousChunkLength;
unsigned char  *ChunkPtr;
unsigned char  *EndChunkPtr;

/* the five long words in the part header */
long  PreviousChunk;
long  SectionsInChunk;
long  PreviousTopicChunk;
long  NextTopicChunk;

long  FinalChunkNumber;
long  CurrentSeek;
long  CurrentChunkLength;
int  CurrentChunk;
short  CurrentChunkType;

struct ChunkDataStruct  FirstChunk,
                        FontChunk,
                        ImageChunk,
                        LastChunk,
                        SectionXrefChunk,
                        SectionHeadingsChunk,
                        SymbolsChunk;

struct FAB  BookFileFab;
struct RAB  BookFileRab;
#ifdef ODS_EXTENDED
   struct NAML  BookFileNaml;
#endif /* ODS_EXTENDED */
struct NAM  BookFileNam;
struct XABDAT  BookFileXabDat;
struct XABFHC  BookFileXabFhc;

/***********************/
/* function prototypes */
/***********************/

void DownloadBook ();
char* HtmlString (char*, int);
char* ButtonBarButton (char*, char*, int, BOOL);
char* XSSuspect (char*);

/*****************************************************************************/
/*
'argc' and 'argv' are only required to support CgiLibEnvironmentInit(), in
particular the OSU environment.
*/

main
(
int argc,
char *argv[]
)
{
   int  status;

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

   sprintf (SoftwareID, "%s (%s)", SOFTWAREID, CgiLibEnvironmentVersion());

   CgiLibEnvironmentInit (argc, argv, false);

   if (getenv ("HYPERREADER$DBUG"))
   {
      fprintf (stdout, "Content-Type: text/plain\n\n");
      CgiLibEnvironmentSetDebug (Debug = true);
   }

   GetParameters ();

   CgiLibResponseSetCharset (CliCharsetPtr);
   CgiLibResponseSetSoftwareID (SoftwareID);
   CgiLibResponseSetErrorMessage ("Reported by HyperReader");

   if (StyleSheetPtr[0])
   {
      char *cptr = calloc (1, 64+strlen(StyleSheetPtr));
      sprintf (cptr, "<link rel=\"stylesheet\" \
type=\"text/css\" href=\"%s\">\n",
               StyleSheetPtr);
      StyleSheetPtr = cptr;
   }

   IsCgiPlus = CgiLibEnvironmentIsCgiPlus ();
   CgiEnvironmentPtr = CgiLibEnvironmentName ();

#ifdef ODS_EXTENDED
   OdsExtended = (GetVmsVersion() >= 72);
   ENAMEL_NAML_SANITY_CHECK
#endif /* ODS_EXTENDED */

   if (IsCgiPlus)
   {
      for (;;)
      {
         /* block waiting for the next request */
         CgiLibEnvironmentSetDebug (Debug = 0);
         CgiLibVar ("");
         if (getenv ("HYPERREADER$DBUG"))
         {
            fprintf (stdout, "Content-Type: text/plain\n\n");
            CgiLibEnvironmentSetDebug (Debug = 1);
         }
         ProcessRequest ();
         CgiLibCgiPlusEOF ();
      }
   }
   else
      ProcessRequest ();

   exit (SS$_NORMAL);
}

/*****************************************************************************/
/*
Get "command-line" parameters, whether from the command-line or from a
configuration symbol or logical containing the equivalent.  OSU scripts have
the 'method', 'url' and 'protocol' supplied as P1, P2, P3 (these being detected
and used by CGILIB), and are of no interest to this function.
*/

GetParameters ()

{
   static unsigned long  Flags = 0;
   static char  CommandLine [256];

   int  status,
        SkipParameters;
   unsigned short  Length;
   char  ch;
   char  *aptr, *cptr, *clptr, *sptr;
   $DESCRIPTOR (CommandLineDsc, CommandLine);

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

   if (!(clptr = getenv ("HYPERREADER$PARAM")))
   {
      /* get the entire command line following the verb */
      if (VMSnok (status =
          lib$get_foreign (&CommandLineDsc, 0, &Length, &Flags)))
         exit (status);
      (clptr = CommandLine)[Length] = '\0';
   }

   /* if [C]SWS (VMS Apache) */
   if (CgiLibEnvironmentIsApache())
   {
      /* CSWS 1.2/3 look for something non-space outside of quotes */
      for (cptr = clptr; *cptr; cptr++)
      {
         if (isspace(*cptr)) continue;
         if (*cptr != '\"') break;
         cptr++;
         while (*cptr && *cptr != '\"') cptr++;
         if (*cptr) cptr++;
      }
      /* CSWS 1.2/3 if nothing outside of quotes then ignore command line */
      if (!*cptr) return;
      /* SWS 2.0 doesn't begin with /APACHE from DCL procedure wrapper */
      if (!strsame (cptr, "/APACHE", 7)) return;
   }

   /* if OSU environment then skip P1, P2, P3 */
   if (CgiLibEnvironmentIsOsu())
      SkipParameters = 3;
   else
      SkipParameters = 0;

   aptr = NULL;
   ch = *clptr;
   for (;;)
   {
      if (aptr) *aptr = '\0';
      if (!ch) break;

      *clptr = ch;
      if (Debug) fprintf (stdout, "clptr |%s|\n", clptr);
      while (*clptr && isspace(*clptr)) *clptr++ = '\0';
      aptr = clptr;
      if (*clptr == '/') clptr++;
      while (*clptr && !isspace (*clptr) && *clptr != '/')
      {
         if (*clptr != '\"')
         {
            clptr++;
            continue;
         }
         cptr = clptr;
         clptr++;
         while (*clptr)
         {
            if (*clptr == '\"')
               if (*(clptr+1) == '\"')
                  clptr++;
               else
                  break;
            *cptr++ = *clptr++;
         }
         *cptr = '\0';
         if (*clptr) clptr++;
      }
      ch = *clptr;
      if (*clptr) *clptr = '\0';
      if (Debug) fprintf (stdout, "aptr |%s|\n", aptr);
      if (!*aptr) continue;

      if (SkipParameters)
      {
         SkipParameters--;
         continue;
      }

      if (strsame (aptr, "/APACHE", 4))
      {
         /* just skip this marker for command-line parameters under SWS 2.0 */
         continue;
      }
      if (strsame (aptr, "/BUTTONS=", 4))
      {
         for (cptr = aptr; *cptr && *cptr != '='; cptr++);
         if (*cptr) cptr++;
         if (*cptr == '+')
         {
            /* append buttons to defaults */
            aptr = calloc (1, strlen(ButtonsPtr)+strlen(cptr)+2);
            strcpy (aptr, ButtonsPtr);
            strcat (aptr, "$");
            strcat (aptr, cptr+1);
            ButtonsPtr = aptr;
         }
         else
            ButtonsPtr = cptr;
         continue;
      }
      if (strsame (aptr, "/CHARSET=", 4))
      {
         for (cptr = aptr; *cptr && *cptr != '='; cptr++);
         if (*cptr) cptr++;
         CliCharsetPtr = cptr;
         continue;
      }
      if (strsame (aptr, "/DBUG", -1))
      {
         Debug = true;
         continue;
      }
      if (strsame (aptr, "/HYPERSHELF=", 4))
      {
         for (cptr = aptr; *cptr && *cptr != '='; cptr++);
         if (*cptr) cptr++;
         HyperShelfScriptNamePtr = cptr;
         continue;
      }
      if (strsame (aptr, "/ODS5", 5))
      {
         OdsExtended = true;
         continue;
      }
      if (strsame (aptr, "/NOODS5", 7))
      {
         OdsExtended = false;
         continue;
      }
      if (strsame (aptr, "/STYLE=", 4))
      {
         for (cptr = aptr; *cptr && *cptr != '='; cptr++);
         if (*cptr) cptr++;
         StyleSheetPtr = cptr;
         continue;
      }

      /* various levels of debugging (sigh!) */
/*
      if (strsame (aptr, "/BYT", 4)) { DebugBytes = true; continue; }
      if (strsame (aptr, "/CHU", 4)) { DebugChunks = true; continue; }
      if (strsame (aptr, "/DBU", 4)) { Debug = true; continue; }
      if (strsame (aptr, "/DEB", 4)) { Debug = true; continue; }
      if (strsame (aptr, "/FIG", 4)) { DebugFigures = true; continue; }
      if (strsame (aptr, "/FON", 4)) { DebugFonts = true; continue; }
      if (strsame (aptr, "/GRA", 4)) { DebugGraphics = true; continue; }
      if (strsame (aptr, "/HOT", 4)) { DebugHotspots = true; continue; }
      if (strsame (aptr, "/SEC", 4)) { DebugSections = true; continue; }
      if (strsame (aptr, "/SYM", 4)) { DebugSymbols = true; continue; }
      if (strsame (aptr, "/TEX", 4)) { DebugText = true; continue; }
*/

      if (*aptr != '/')
      {
         fprintf (stdout, "%%%s-E-MAXPARM, too many parameters\n \\%s\\\n",
                  Utility, aptr);
         exit (STS$K_ERROR | STS$M_INHIB_MSG);
      }
      else
      {
         fprintf (stdout, "%%%s-E-IVQUAL, unrecognized qualifier\n \\%s\\\n",
                  Utility, aptr+1);
         exit (STS$K_ERROR | STS$M_INHIB_MSG);
      }
   }
}

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

ProcessRequest ()

{
   int  idx, status;
   char  *cptr;

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

   if (Debug) fprintf (stdout, "ProcessRequest()\n");

   CgiEnvironmentPtr = CgiLibEnvironmentName ();

   ErrorReported = false;

   BookDivisionCount = DynamicMemoryAllocated =
      EndGlobalVertical = GlobalVertical = GlobalHorizontal =
      LastLineOnPage = LinesOnPage = LinesOutputToClient =
      NextChunkNumber = NextTopicChunk = PreviousChunkNumber =
      PreviousTopicChunk = TablesChunkNumber = 0;

   /*************************/
   /* get the CGI variables */
   /*************************/

   /* initialize a timer for establishing page composition statistics */
   lib$init_timer (0);

   CgiServerSoftwarePtr = CgiLibVar ("WWW_SERVER_SOFTWARE");
   CgiRequestMethodPtr = CgiLibVar ("WWW_REQUEST_METHOD");
   if (strcmp (CgiRequestMethodPtr, "GET"))
   {
      CgiLibResponseHeader (501, "text/html");
      fprintf (stdout, "Not implemented!\n");
      return;
   }

   CgiPathInfoPtr = CgiLibVar ("WWW_PATH_INFO");
   CgiPathTranslatedPtr = CgiLibVar ("WWW_PATH_TRANSLATED");
   CgiScriptNamePtr = CgiLibVar ("WWW_SCRIPT_NAME");
   CgiQueryStringPtr = CgiLibVar ("WWW_QUERY_STRING");
   CgiHttpCacheControlPtr = CgiLibVar ("WWW_HTTP_CACHE_CONTROL");
   CgiHttpIfModifiedSincePtr = CgiLibVar ("WWW_HTTP_IF_MODIFIED_SINCE");
   CgiHttpPragmaPtr = CgiLibVar ("WWW_HTTP_PRAGMA");

   CgiFormFilePtr = CgiLibVar ("WWW_FORM_FILE");
   if (!CgiFormFilePtr[0]) CgiFormFilePtr = CgiLibVar ("WWW_FORM_BOOK");
   CgiFormChunkPtr = CgiLibVar ("WWW_FORM_CHUNK");
   CgiFormGraphicPtr = CgiLibVar ("WWW_FORM_GRAPHIC");
   CgiFormMassagePtr = CgiLibVar ("WWW_FORM_MASSAGE");
   CgiFormTitlePtr = CgiLibVar ("WWW_FORM_TITLE");

   /* this is really only for experimentation/debugging */
   CgiFormVarRecPtr = CgiLibVar ("WWW_FORM_VARREC");

   if (VMSnok (status = TimeSetGmt ()))
   {
      if (status != SS$_NOLOGNAM)
      {
         CgiLibResponseError (FI_LI, status, "GMT offset");
         exit (SS$_NORMAL);
      }
   }

   if (CgiHttpIfModifiedSincePtr[0])
   {
      if (VMSnok (HttpGmTime (CgiHttpIfModifiedSincePtr, 
                              &IfModifiedSinceBinaryTime)))
      {
         if (Debug) fprintf (stdout, "If-Modified-Since: NBG!\n");
         IfModifiedSinceBinaryTime[0] = IfModifiedSinceBinaryTime[0] = 0;
         CgiHttpIfModifiedSincePtr = "";
      }
   }

   /***********/
   /* process */
   /***********/

   if (!HyperShelfScriptNamePtr)
   {
      if (CgiLibEnvironmentIsWasd() ||
          CgiLibEnvironmentIsCgiPlus())
         HyperShelfScriptNamePtr = DEFAULT_WASD_HYPERSHELF;
      else
      if (CgiLibEnvironmentIsOsu())
         HyperShelfScriptNamePtr = DEFAULT_OSU_HYPERSHELF;
      else
         HyperShelfScriptNamePtr = DEFAULT_CGI_HYPERSHELF;
   }

   UrlEncodedCgiPathInfoPtr =
      (char*)CgiLibUrlEncode (CgiPathInfoPtr, -1, NULL, 0);

   if (CgiFormFilePtr[0])
   {
      BookFileNamePtr = CgiFormFilePtr;
      sprintf (BookSpecifiedByFile, "file=%s&", CgiFormFilePtr);
   }
   else
   {
      BookFileNamePtr = CgiPathTranslatedPtr;
      BookSpecifiedByFile[0] = '\0';
   }
   if (Debug) fprintf (stdout, "BookFileNamePtr |%s|\n", BookFileNamePtr);

   if (BookFileNamePtr[0])
   {
      /* find file type (extension) */
      for (cptr = BookFileNamePtr; *cptr; cptr++);
      if (*(cptr-1) == ';') *(cptr-1) = '\0';
      while (cptr > BookFileNamePtr && *cptr != '.' &&
             *cptr != ']' && *cptr != ':') cptr--;
      if (*cptr == '.')
      {
         /* check if looks like a bookshelf type */
         for (idx = 0; BookshelfTypes[idx][0]; idx++)
         {
            if (Debug) fprintf (stdout, "|%s|%s|\n", BookshelfTypes[idx], cptr);
            if (!strsame (BookshelfTypes[idx], cptr, -1)) continue;
            /* looks like a shelf type! */
            BookFileNamePtr = "";
            break;
         }
      }
   }
   if (Debug) fprintf (stdout, "BookFileNamePtr |%s|\n", BookFileNamePtr);

   if (!BookFileNamePtr[0])
   {
      /*******************************/
      /* redirect to shelf processor */
      /*******************************/

      CgiLibResponseRedirect ("%!%s%s%s%s",
         HyperShelfScriptNamePtr, CgiPathInfoPtr,
         CgiQueryStringPtr[0] ? "?" : "", CgiQueryStringPtr);

      return;
   }
   /* check if looks like book type */
   for (idx = 0; BookTypes[idx][0]; idx++)
   {
      if (Debug) fprintf (stdout, "|%s|%s|\n", BookTypes[idx], cptr);
      if (!strsame (BookTypes[idx], cptr, -1)) continue;
      /* looks like a book type! */
      break;
   }
   if (!BookTypes[idx][0])
   {
      CgiLibResponseError (FI_LI, 0, ErrorBookType);
      return;
   }

   if (!(NoDownloadBook = (getenv ("HYPERREADER$NODOWNLOAD") != NULL)))
      if (CgiLibVarNull ("WWW_FORM_DOWNLOAD"))
      {
         DownloadBook ();
         return;
      }

   CgiFormRefererPtr = CgiLibVar ("WWW_FORM_REFERER");
   if (!CgiFormRefererPtr[0])
      CgiFormRefererPtr = CgiLibVar ("WWW_HTTP_REFERER");
   if (cptr = XSSuspect (CgiFormRefererPtr))
   {
      CgiLibResponseError (FI_LI, 0, cptr);
      return;
   }
   if (CgiFormRefererPtr[0])
      CgiLibUrlEncode (CgiFormRefererPtr, -1,
                       UrlEncReferer, sizeof(UrlEncReferer));
   else
      UrlEncReferer[0] = '\0';

   EnhanceText = true;
   TotalBytesRead = TBW = 0;

   if (CgiFormTitlePtr[0])
      CgiLibUrlEncode (CgiFormTitlePtr, -1, UrlEncTitle, -1);

   if (toupper(CgiFormMassagePtr[0]) == 'N')
      DoMassage = false;
   else
      DoMassage = true;

   if (CgiFormGraphicPtr[0])
      ImageReader();
   else
      BookReader();

   /************/
   /* clean up */
   /************/

   if (BookFileFab.fab$w_ifi) sys$close (&BookFileFab, 0, 0);

   if (UrlEncodedCgiPathInfoPtr) free (UrlEncodedCgiPathInfoPtr);
   UrlEncodedCgiPathInfoPtr = NULL;

   if (ChunkBufferPtr) free (ChunkBufferPtr);
   ChunkBufferPtr = NULL;
   PreviousChunkLength = 0;

   if (ChunkArrayPtr) free (ChunkArrayPtr);
   ChunkArrayPtr = NULL;

   if (FontArrayPtr) free (FontArrayPtr);
   FontArrayPtr = NULL;

   if (SectionChunkNumbersArrayPtr) free (SectionChunkNumbersArrayPtr);
   SectionChunkNumbersArrayPtr = NULL;

   for (idx = 1; idx <= BookDivisionCount; idx++)
   {
      if (BookDivision[idx].DescriptionPtr)
         free (BookDivision[idx].DescriptionPtr);
      BookDivision[idx].DescriptionPtr = NULL;
   }

   PageSize (-1);

   MassageOutputLine (NULL, NULL, NULL);

   OutputPage (true);
}

/*****************************************************************************/
/*
Retrieve a BRF (BookReader Format) image from within a document and send it to 
the client.  Bookreader images are essentially uncompressed bitmaps.  
Horizontal and vertical coordinates position on the page, and height and width 
information allows the image to be reconstructed.
*/ 

ImageReader ()

{
   unsigned long  ChunkLength = 0,
                  DataLength,
                  Horiz,
                  ImageChunkOffset = 0,
                  StartOfImageOffset = 0,
                  VBN = 0,
                  VBNbyte = 0;
   unsigned short  HeightPixels,
                   WidthPixels,
                   SectionType;
   float  FloatWidth;

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

   if (Debug) fprintf (stdout, "ImageReader() |%s|\n", CgiFormGraphicPtr);

   /*
      The four numbers in the 'CgiFormGraphicPtr' are:
      (note that parts one and two constitute the RMS RFA of the record)

      1.  Virtual Block Number of the start of the part
      2.  starting byte position in the VBN
      3.  total length of the chunk
      4.  offset from start of chunk the image data begins

      This will allow the image to be retrieved very quickly by merely
      opening the file, reading the part (one or more records) via the RFA
      and jumping in to process the image data from the offset.
   */

   sscanf (CgiFormGraphicPtr, "%d,%d,%d,%d",
           &VBN, &VBNbyte, &ChunkLength, &ImageChunkOffset);

   /* bit of a sanity check, VBNbyte can be zero of course, others cannot */
   if (!VBN || VBNbyte > 511 || !ChunkLength || !ImageChunkOffset)
   {
      CgiLibResponseError (FI_LI, 0, ErrorImageSpecification);
      return;
   }

   if (VMSnok (OpenBookFile ())) return;

   ImageChunk.VBN = VBN;
   ImageChunk.VBNbyte = (unsigned short)VBNbyte;
   ImageChunk.Length = ChunkLength;
   if (VMSnok (GetChunk (&ImageChunk)))
      return;

   /* this is a good sanity check on the image specification */
   if (ChunkLength != CurrentChunkLength)
   {
      CgiLibResponseError (FI_LI, 0, ErrorImageSpecification);
      return;
   }

   ChunkPtr = ChunkBufferPtr + ImageChunkOffset;

   /*********************/
   /* process the image */
   /*********************/

   SectionType = *(unsigned short*)ChunkPtr;

   if (SectionType == 18)
   {
      /*******************************************************************/
      /* the section 18 processing is ALL CONJECTURE ... 03-AUG-1995 MGD */
      /*******************************************************************/

      StartOfImageOffset = 42;
      DataLength = *(unsigned long*)(ChunkPtr+2) - StartOfImageOffset;
      /* fudge factor of 5.33 seems to work!? (must be another way!) */
      WidthPixels =
      (unsigned short)((float)*(unsigned long*)(ChunkPtr+26) / 5.33);
      HeightPixels = DataLength / (WidthPixels / 8);
   }
   else
   if (SectionType == 19)
   {
      StartOfImageOffset = 50;
      DataLength = *(unsigned long*)(ChunkPtr+2);
      WidthPixels = *(unsigned short*)(ChunkPtr+46);
      HeightPixels = *(unsigned short*)(ChunkPtr+48);
   }

   if (DebugFigures)
   {
      DumpLongWords ("IMAGE: ", ChunkPtr+2, 12);
      fprintf (stdout, "DataLength : 0x%08.08lx (%d)\n",
               DataLength, DataLength);
      fprintf (stdout, "WidthPixels : %d\n", WidthPixels);
      fprintf (stdout, "HeightPixels : %d\n", HeightPixels);
   }

   GifImage (WidthPixels, HeightPixels,
             ChunkPtr+StartOfImageOffset, DataLength-StartOfImageOffset);
}

/*****************************************************************************/
/*
Construct the page (requested portion of the book), output HTML to provide a 
page (book title) heading, etc., output the page constructed, then some more 
HTML to finish off the page.
*/ 

BookReader ()

{
   int  idx;
   char  *sptr;
   char  EscapedBookFileName [ODS_MAX_FILE_NAME_LENGTH+1],
         UnixDateTime [32];
   unsigned long  UnixTime;
   struct tm  *UnixTmPtr;

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

   if (Debug) fprintf (stdout, "BookReader()\n");

   if (VMSnok (OpenBookFile ())) return;

   /* 
      This string is added to an HREF="..." link after the part number.
      It propagates whether the book contents are being "massaged" or not.
   */
   if (DoMassageOutput = DoMassage)
      UrlDoMassage = "";
   else
      UrlDoMassage = "&massage=no";

   /************************/
   /* process book details */
   /************************/

   /* get information of the physical "structure" of the book */
   GetFirstChunk ();
   GetLastChunk ();
   GetFonts ();
   GetSectionXref ();

   GetChunkNumber = atol (CgiFormChunkPtr);
   if (Debug)
      fprintf (stdout,
         "CgiFormChunkPtr |%s| GetChunkNumber: %d\n",
          CgiFormChunkPtr, GetChunkNumber);
   if (!GetChunkNumber)
   {
      if (!BookDivisionCount)
      {
         CgiLibResponseError (FI_LI, 0, ErrorTOC);
         return;
      }
      GetChunkNumber = BookDivision[1].ChunkNumber;
   }

   if (GetChunkNumber < 1 || GetChunkNumber > NumberOfChunks)
   {
      CgiLibResponseError (FI_LI, 0, ErrorRequestedChunk);
      return;
   }

   for (idx = 1; idx <= BookDivisionCount; idx++)
   {
      if (BookDivision[idx].ChunkNumber == GetChunkNumber)
      {
         /* 
             These pages do not have bold/italic-text tags and are not
             output-massaged at all.  They look and "perform" better that way!
         */
         DoMassageOutput = false;
         break;
      }
   }

   /*********************/
   /* HTTP header, etc. */
   /*********************/

   time (&UnixTime);
   UnixTmPtr = localtime (&UnixTime);
   if (!strftime (UnixDateTime, sizeof(UnixDateTime),
                  "%a, %d %b %Y %T", UnixTmPtr))
      strcpy (UnixDateTime, "[error]");
   if (Debug) fprintf (stdout, "UnixDateTime |%s|\n", UnixDateTime);

   TBW += CgiLibResponseHeader (200, "text/html",
                               "Last-Modified: %s\n", LastModifiedGmDateTime);

   CgiLibHtmlEscape (ExpBookFileName, -1,
                     EscapedBookFileName, sizeof(ExpBookFileName));

   TBW += fprintf (stdout,
"<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n\
<meta name=\"generator\" content=\"%s\">\n\
<meta name=\"copyright\" content=\"%s - GPL licensed\">\n\
<meta name=\"environment\" content=\"%s\">\n\
<meta name=\"date\" content=\"%s\">\n\
<meta name=\"file\" content=\"%s (%s)\">\n",
      SoftwareID, SoftwareCopy,
      CgiEnvironmentPtr, UnixDateTime,
      EscapedBookFileName, BookFileVarRec ? "record" : "block");

   /******************/
   /* construct page */
   /******************/

   if (VMSnok (GetChunk (&(ChunkArrayPtr[GetChunkNumber]))))
      return;

   ProcessChunk ();

   PreviousChunkNumber = PreviousTopicChunk;
   if (NextTopicChunk && NextTopicChunk < NumberOfChunks)
       NextChunkNumber = NextTopicChunk;

   /***************/
   /* output page */
   /***************/

   TBW += fprintf (stdout,
"<title>HyperReader ... %s</title>\n\
<style type=\"text/css\">\n\
%s\
</style>\n\
%s\
</head>\n\
<body>\n\
<div class=\"header\">%s</div>\n",
      BookTitle,
      DefaultStyle,
      StyleSheetPtr,
      BookTitle);

   ButtonBar (1);

   TBW += fprintf (stdout, "<pre>");

   OutputPage (false);

   TBW += fprintf (stdout, "</pre>\n");

   ButtonBar (2);

   Statistics ();
   TBW += fprintf (stdout, "</body>\n</html>\n");
}

/*****************************************************************************/
/*
Download the book as a binary object.

Once on the VMS system the RMS file attributes usually need to be adjusted via

$ set file /attr=(rfm:var,rat:none,lrl:32254) <book-file-name>
*/

void DownloadBook ()

{
   int  bytes, status;
   char  BlockBuffer [512*64];
   FILE  *binout;

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

   if (Debug) fprintf (stdout, "DownloadBook()\n");

   if (VMSnok (OpenBookFile ())) return;

   if (!(binout = fopen ("SYS$OUTPUT", "w", "ctx=bin")))
   {
      sys$close (&BookFileFab, 0, 0);
      CgiLibResponseError (FI_LI, vaxc$errno, ErrorDownloadBook);
      return;
   }

   if (BookFileXabFhc.xab$l_ebk)
      bytes = ((BookFileXabFhc.xab$l_ebk - 1) * 512) + BookFileXabFhc.xab$w_ffb;
   else
      bytes = BookFileXabFhc.xab$w_ffb;

   fprintf (binout,
"Status: 200 OK\r\n\
Content-Type: application/octet-stream\r\n\
Content-Length: %u\r\n\
Cache-Control: no-cache, no-store, must-revalidate\r\n\
Pragma: no-cache\r\n\
Expires: 0\r\n\
\r\n", bytes);


   BookFileRab.rab$l_bkt = 1;
   for (;;)
   {
      BookFileRab.rab$l_ubf = BlockBuffer; 
      BookFileRab.rab$w_usz = sizeof(BlockBuffer);

      status = sys$read (&BookFileRab, 0, 0);
      if (Debug) fprintf (stdout, "sys$read() %%X%08.08X\n", status);
      if (VMSnok (status)) break;

      fwrite (BookFileRab.rab$l_ubf, BookFileRab.rab$w_rsz, 1, binout);

      BookFileRab.rab$l_bkt += 64;
   }

   sys$close (&BookFileFab, 0, 0);

   fclose (binout);
}

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

Statistics ()

{
   static char  StatisticsFao [] =
"<!!--\n\
TIMES  layup: !%T\n\
       cpu:   00:!2ZL:!2ZL.!2ZL\n\
BYTES  read from book: !UL\n\
       written to client: !UL (approx.)\n\
       memory allocated: !UL\n\
LINES  allocated for page: !UL\n\
       last on page: !UL\n\
       output to client: !UL\n\
-->\n";

   static long  LibElapsedTime = 1,
                LibCpuTime = 2;

   unsigned long  ElapsedTime [2];
   unsigned long  CpuTime;
   unsigned short  Length;
   char  String [512];
   $DESCRIPTOR (StatisticsFaoDsc, StatisticsFao);
   $DESCRIPTOR (StringDsc, String);

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

   if (Debug) fprintf (stdout, "Statistics()\n");

   lib$stat_timer (&LibElapsedTime, &ElapsedTime, 0);
   lib$stat_timer (&LibCpuTime, &CpuTime, 0);

   sys$fao (&StatisticsFaoDsc, &Length, &StringDsc,
            &ElapsedTime, CpuTime/6000, CpuTime/100, CpuTime%100,
            TotalBytesRead,
            /* this is a close approximation, total so far plus guess-timate */
            TBW + sizeof(StatisticsFao),
            DynamicMemoryAllocated,
            LinesOnPage, LastLineOnPage, LinesOutputToClient);
   String[Length] = '\0';

   fputs (String, stdout);
}

/*****************************************************************************/
/*
The "page" of output has been constructed in the matrix of 'OutCharStruct'
elements.

Scan through this as if it was a page of text, starting at line one and 
character one, moving across the columns to the last character, then moving to 
the next line, etc. 

The page 'OutCharStruct' elements consist of a single character (any character 
mapped from the Bookreader source) and a pointer to 'char'.  This pointer 
will, most often, be NULL.  However, when HTML formatting (e.g. bolding) or 
linkage (e.g. a Bookreader "hotspot") is required a string is dynamically 
allocated to contain the required HTML and pointed to by this pointer in the 
appropriate character location.

If the HTML pointer exists copy the string to the output buffer first then any 
Bookreader character in the character variable.
*/ 

OutputPage (BOOL Reset)

{
   static int  PreBlankLineCount = 0;

   BOOL  TerminateAndOutputLine;
   int  lcnt, ccnt,
        PostBlankLineCount = 0,
        LineLength;
   char  *cptr, *eptr, *lptr, *zptr;
   char  Line [8192];
   struct OutCharStruct  *ocptr;

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

   if (Debug)
      fprintf (stdout, "OutputPage() %d/%d\n", LastLineOnPage, LinesOnPage);

   if (Reset)
   {
      /* reset after use */
      PreBlankLineCount = 0;
      return;
   }

   /*******************/
   /* loop thru lines */
   /*******************/

   for (lcnt = 1; lcnt <= LastLineOnPage; lcnt++)
   {
      if (Debug) fprintf (stdout, "%d\n", lcnt);

      /* 'eptr' points at last space character copied into output 'Line' */
      zptr = (eptr = lptr = Line) + sizeof(Line)-8;
      /* point at the first character structure in the output buffer */
      ocptr = OUT_CHAR_AT(lcnt,0);

      /************************/
      /* loop thru characters */
      /************************/

      for (ccnt = 0; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
      {
         if (lptr >= zptr)
         {
            CgiLibResponseError (FI_LI, 0, ErrorStringOverflow);
            return;
         }
         /* insert any HTML associated with the character position */
         cptr = ocptr->htptr;
         if (cptr)
         {
            while (*cptr && lptr < zptr) *lptr++ = *cptr++;
            /* be sure to terminate after any included HTML */
            eptr = lptr;
         }
         /* add the character from buffer, filter HTML-forbidden characters */
         switch (ocptr->c)
         {
            case '&' : strcpy (lptr, "&amp;"); lptr += 4; break;
            case '<' : strcpy (lptr, "&lt;"); lptr += 3; break;
            case '>' : strcpy (lptr, "&gt;"); lptr += 3; break;
            default : *lptr = ocptr->c;
         }
         /* if the character was not a space then note the position */
         if (*lptr != ' ') eptr = lptr+1;
         lptr++;
         ocptr++;
      }

      /**********************/
      /* end character loop */
      /**********************/

      if (eptr == Line)
      {
         /**************/
         /* blank line */
         /**************/

         PreBlankLineCount++;
      }
      else
      {
         /******************/
         /* non-blank line */
         /******************/

         /* go back to the last character copied, terminate line after that */
         lptr = eptr;
         *lptr++ = '\n';
         *lptr = '\0';
         LineLength = lptr - Line;

         if (DoMassageOutput)
            MassageOutputLine (Line, &PreBlankLineCount, &PostBlankLineCount);
         else
         {
            /* replace all of the substituted symbol characters */
            for (lptr = Line; *lptr; lptr++)
               if (*lptr == INTERIM_NON_ASCII_CHAR) *lptr = FINAL_NON_ASCII_CHAR;
         }

         if (PreBlankLineCount)
         {
            if (DoMassageOutput)
            {
               /* ensure multitudes of blank lines are not output */
               if (PreBlankLineCount > MAX_CONSECUTIVE_BLANK_LINES) 
                  PreBlankLineCount = MAX_CONSECUTIVE_BLANK_LINES;

               /* absorb any page-leading blank lines, output others */
               if (LinesOutputToClient)
                  while (PreBlankLineCount--)
                  {
                     TBW += fprintf (stdout, "\n");
                     LinesOutputToClient++;
                  }
               else;
            }
            else
            {
               /* when not massaging the text output all blank lines */
               while (PreBlankLineCount--)
               {
                  TBW += fprintf (stdout, "\n");
                  LinesOutputToClient++;
               }
            }
            PreBlankLineCount = 0;
         }

         TBW += fprintf (stdout, "%s", Line);
         LinesOutputToClient++;

         /* post-blank-lines are only created by the massage function */
         if (PostBlankLineCount)
         {
            PreBlankLineCount = PostBlankLineCount;
            PostBlankLineCount = 0;
         }
      }
   }

   if (!DoMassageOutput)
      while (PreBlankLineCount--)
      {
         TBW += fprintf (stdout, "\n");
         LinesOutputToClient++;
      }
}

/*****************************************************************************/
/*
"Massage" the string of characters constructed from the matrix of
'OutCharStruct's   before it is output to the client.  This "massage" attempts
to make certain  corrections to enhance the layout of the text, these are
clearly indicated and  described within the function.
*/ 

MassageOutputLine
(
char *Line,
int *PreBlankLineCountPtr,
int *PostBlankLineCountPtr
)
{
   static char  LastCharOfPreviousLine = '\0';

   char  *cptr, *lptr;

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

   if (Debug) fprintf (stdout, "MassageOutputLine() |%s|\n", Line);

   if (!Line)
   {
      /* initialize */
      LastCharOfPreviousLine = '\0';
      return;
   }

   /**************************/
   /* blank line suppression */
   /**************************/

   if (*PreBlankLineCountPtr == 1)
   {
      /*
         There is one blank line immediately before this line.
         Check to see if this line begins with a lower-case
         alphabetic.  If it does then it is probable (though
         not certain) that the blank line was inserted by
         bit-to-character mapping inconsistancy.  If it is
         lower-case then absorb the blank line.  Step over tags.
      */
      lptr = Line;
      while (*lptr == ' ') lptr++;
      while (*lptr == '<')
      {
         while (*lptr && *lptr != '>') lptr++;
         if (*lptr) lptr++;
         while (*lptr == ' ') lptr++;
      }
      if (*lptr == '(')
         *PreBlankLineCountPtr = 0;
      else
      if (islower(*lptr) &&
          (!ispunct(LastCharOfPreviousLine) ||
           LastCharOfPreviousLine == '-' ||
           LastCharOfPreviousLine == '_' ||
           LastCharOfPreviousLine == ',' ||
           LastCharOfPreviousLine == ';'))
         *PreBlankLineCountPtr = 0;
   }

   /*******************************/
   /* bulleted and numbered lists */
   /*******************************/

   for (cptr = lptr = Line; *lptr; lptr++)
   {
      if (cptr == Line && *lptr == NON_SUPPORTED_CHAR)
      {
         /*
            Symbol characters tend to be used for the "bullets" in lists.
            In this case they would be the first character on the line
            and followed by a space character.  Ensure that the line is
            preceded by at least one blank line for "good" spacing.  If 'cptr'
            equals the start of the line then no non-space characters have
            been encountered.
         */
         if (cptr == Line && *(lptr+1) == ' ' && !*PreBlankLineCountPtr)
            *PreBlankLineCountPtr += 1;
         *lptr = FINAL_NON_ASCII_CHAR;
      }
      else 
      if (cptr == Line && isdigit(*lptr))
      {
         /*
            Numbered lists and headings (at least section headings begin
            a line with (for example) "1. ", "1.1 ", etc.  Detect this and
            ensure the line is preceded by at least one blank line for "good"
            spacing.  If 'cptr' equals the start of the line then no
            non-space characters have been encountered.
         */
         while (isdigit(*lptr)) lptr++;
         if (*lptr == '.')
         {
            while (isdigit(*lptr) || *lptr == '.') lptr++;
            if (*lptr == ' ') *PreBlankLineCountPtr += 1;
         }
      }
      /* note the presence of a non-space character */
      if (*lptr != ' ') cptr = lptr + 1;
   }

   /***********************************************/
   /* take note of the last character on the line */
   /***********************************************/

   cptr = lptr = Line;
   while (*lptr)
      if (*lptr != ' ' && *lptr != '\n')
         cptr = lptr++;
      else
         lptr++;
   LastCharOfPreviousLine = *cptr;
}

/*****************************************************************************/
/*
Creates a "page" in-memory.  The page comprises 1..n of the structure 
'OutCharStruct'.  The macro OUT_CHAR_AT() allows this linear array of
structures to  be addressed with a line/column value as if it was a matrix. 
This essentially  allows it to be treated as a matrix corresponding to a page
of  'MAX_OUTLINE_LENGTH' characters wide and 'NewLinesOnPage' length.  To
increase  efficiency, more than the requested 'NewLinesOnPage' can be created,
reducing  the number of dynamic memory (re)allocations required.
*/ 

PageSize (int NewLinesOnPage)

{
   int  ccnt, status,
        Count,
        BytesToAllocate;
   struct OutCharStruct  *ocptr;

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

   if (Debug)
      fprintf (stdout, "PageSize() %d %d/%d\n",
               LastLineOnPage, LinesOnPage, NewLinesOnPage);

   if (NewLinesOnPage < 0)
   {
      LastLineOnPage = 0;
      if (!LinesOnPage) return;
      if (OutPtr)
      {
         for (Count = 0; Count <= LinesOnPage; Count++)
         {
            if (Debug) fprintf (stdout, "Count: %d\n", Count);
            ocptr = OUT_CHAR_AT(Count,0);
            for (ccnt = 0; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
            {
               if (ocptr->htptr) free (ocptr->htptr);
               ocptr++;
            }
         }
      }
      LinesOnPage = 0;
      if (OutPtr) free (OutPtr);
      OutPtr = NULL;
      return;
   }

   if (NewLinesOnPage <= LinesOnPage && OutPtr) return;

   /*
      If the number of lines on the page exceeds a certain threshold assume
      its going to be a large page with many lines and pre-allocate some.
      If it looks like being a very big page, increase preallocation.
   */
   if (LinesOnPage > LINES_ON_PAGE_PREALLOCATE * 30)
      NewLinesOnPage += LINES_ON_PAGE_PREALLOCATE * 10;
   else
   if (LinesOnPage > LINES_ON_PAGE_PREALLOCATE * 15)
      NewLinesOnPage += LINES_ON_PAGE_PREALLOCATE * 5;
   else
   if (LinesOnPage > LINES_ON_PAGE_PREALLOCATE * 5)
      NewLinesOnPage += LINES_ON_PAGE_PREALLOCATE * 2;
   else
      NewLinesOnPage += LINES_ON_PAGE_PREALLOCATE;

   BytesToAllocate = (NewLinesOnPage+1)*MAX_OUTLINE_LENGTH*sizeof(struct OutCharStruct);
   if (!OutPtr)
      OutPtr = calloc (1, BytesToAllocate);
   else
      OutPtr = realloc (OutPtr, BytesToAllocate);
   if (!OutPtr)
   {
      char String [256];

      status = vaxc$errno;
      sprintf (String, ErrorCallocBytes,
         (NewLinesOnPage+1)*MAX_OUTLINE_LENGTH*sizeof(struct OutCharStruct));
      CgiLibResponseError (FI_LI, status, String);

      /* definitely exit if we can't get memory!!! */
      exit (SS$_NORMAL);
   }
   DynamicMemoryAllocated += BytesToAllocate;

   /* scan through all added "lines" */
   if (!LinesOnPage) LinesOnPage = -1;
   while (LinesOnPage < NewLinesOnPage)
   {
      LinesOnPage++;
      ocptr = OUT_CHAR_AT(LinesOnPage,0);
      /* scan through all "characters" on that "line" */
      for (ccnt = 0; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
      {
         /* put a space into the character member, NULL the HTML pointer */
         ocptr->c = ' ';
         ocptr->htptr = NULL;
         ocptr++;
      }
   }
}

/*****************************************************************************/
/*
Dynamically allocate memory for a string.  If there is no existing string then 
allocate the specified size.  If there is an existing string pointed to 
allocate enough for the existing string plus the specified size.  Return a 
pointer to the dynamically allocated string.
*/ 

char* HtmlString
(
char *HtmlStringPtr,
int StringSize
)
{
   int  status;

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

   if (!HtmlStringPtr)
   {
      StringSize++;
      if (Debug) fprintf (stdout, "HtmlString() NULL %d\n", StringSize);
      StringSize = ((StringSize / MIN_HTML_STRING_ALLOC) + 1) *
                   MIN_HTML_STRING_ALLOC;
      if (Debug) fprintf (stdout, "StringSize: %d\n", StringSize);
      HtmlStringPtr = calloc (1, StringSize);
      *HtmlStringPtr = '\0';
   }
   else
   {
      StringSize += strlen(HtmlStringPtr);
      if (Debug)
         fprintf (stdout, "HtmlString() |%s| %d\n",
                  HtmlStringPtr, StringSize);
      StringSize = ((StringSize / MIN_HTML_STRING_ALLOC) + 1) *
                   MIN_HTML_STRING_ALLOC;
      if (Debug) fprintf (stdout, "StringSize: %d\n", StringSize);
      HtmlStringPtr = realloc (HtmlStringPtr, StringSize);
   }
   if (!HtmlStringPtr)
   {
      char String [256];

      status = vaxc$errno;
      sprintf (String, ErrorCallocBytes, StringSize);
      CgiLibResponseError (FI_LI, status, String);

      /* definitely exit if we can't get memory!!! */
      exit (SS$_NORMAL);
   }
   DynamicMemoryAllocated += StringSize;
   return (HtmlStringPtr);
}

/*****************************************************************************/
/*
This function comes from original code, so I'm a bit hazy on its functionality.
*/

ProcessChunk ()

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

   if (Debug) fprintf (stdout, "ProcessChunk()\n");

   if (DebugChunks)
   {
      fprintf (stdout, "ChunkType: %d\n", CurrentChunkType);
      fprintf (stdout, "ChunkLength: %ld\n", CurrentChunkLength);
   }

   if (CurrentChunkType == 3)
   {
      /*****************/
      /* document text */
      /*****************/

      ProcessDocument ();
      return;
   }
   else
   if (CurrentChunkType == 4)
   {
      /***********************************/
      /* contents, tables, figure, index */
      /***********************************/

      ProcessContentsEtc();
      return;
   }
   else
   {
      /***********/
      /* unknown */
      /***********/

      InsertComment (GlobalHorizontal, GlobalVertical,
         "Sorry.  HyperReader does not recognize the document format.",
         __LINE__);

      ChunkPtr += CurrentChunkLength;
      return;
   }
}

/*****************************************************************************/
/*
Process a series of sections of the document body.  These sections can 
comprise text, graphics, figures, etc.
*/ 

ProcessDocument ()

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

   if (Debug) fprintf (stdout, "ProcessDocument()\n");

   PreviousChunk = *(long*)(ChunkBufferPtr+6);
   SectionsInChunk = *(long*)(ChunkBufferPtr+14);
   PreviousTopicChunk = *(long*)(ChunkBufferPtr+18);
   NextTopicChunk = *(long*)(ChunkBufferPtr+22);

   if (DebugChunks)
   {
      DumpLongWords ("DOCUMENT BODY:", ChunkPtr+2, 6);
      fprintf (stdout, "Chunk: %d\n", CurrentChunk);
      fprintf (stdout, "ChunkType: %d\n", CurrentChunkType);
      fprintf (stdout, "ChunkLength: %d\n", CurrentChunkLength);
      fprintf (stdout, "PreviousChunk: %d\n", PreviousChunk);
      fprintf (stdout, "SectionsInChunk: %d\n", SectionsInChunk);
      fprintf (stdout, "PreviousTopicChunk %d\n", PreviousTopicChunk);
      fprintf (stdout, "NextTopicChunk: %d\n", NextTopicChunk);
   }

   ChunkPtr = ChunkBufferPtr + 26;
   while (ChunkPtr < EndChunkPtr) ProcessSection ();
}

/*****************************************************************************/
/*
Process a single section of the document body.  A section can comprise 
text, graphics, figures, etc.
*/ 

ProcessSection ()

{
   unsigned long  SectionHorizOffset,
                  SectionHorizUnits,
                  SectionLength,
                  SectionSubType,
                  SectionVertOffset,
                  SectionVertUnits;
   unsigned short  SectionType;
   unsigned char  *EndSectionChunkPtr;

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

   if (Debug) fprintf (stdout, "ProcessSection()\n");

   SectionType = *(unsigned short*)ChunkPtr;
   SectionSubType = *(unsigned long*)(ChunkPtr+6);
   SectionLength = *(unsigned long*)(ChunkPtr+2);
   SectionHorizOffset = *(unsigned long*)(ChunkPtr+18);
   SectionVertOffset = *(unsigned long*)(ChunkPtr+22);
   SectionHorizUnits = *(unsigned long*)(ChunkPtr+26);
   SectionVertUnits = *(unsigned long*)(ChunkPtr+30);

   EndSectionChunkPtr = ChunkPtr + SectionLength;

   if (Debug)
   {
      fprintf (stdout, "\nSectionType: %d\n", SectionType);
      fprintf (stdout, "SectionSubType: %d\n", SectionSubType);
      fprintf (stdout, "SectionLength: %d\n", SectionLength);
      fprintf (stdout, "SectionHorizOffset: %d\n", SectionHorizOffset);
      fprintf (stdout, "SectionVertOffset: %d\n", SectionVertOffset);
      fprintf (stdout, "SectionHorizUnits: %d\n", SectionHorizUnits);
      fprintf (stdout, "SectionVertUnits: %d\n", SectionVertUnits);
   }

   if (SectionType == 18 || SectionType == 19)
   {
      /********************/
      /* multi-functional */
      /********************/

      if (SectionType == 18)
      {
         /* adjust the vertical origin for all parts in this section */
         GlobalVertical = EndGlobalVertical;
         EndGlobalVertical += SectionVertUnits;
      }

      if (DebugSections)
      {
         DumpLongWords ("SECTION 18:", ChunkPtr+2, 10);
         fprintf (stdout, "ChunkPtr: %d\n", ChunkPtr);
         fprintf (stdout, "next ChunkPtr %d\n", ChunkPtr+SectionLength);
      }

      if (SectionSubType == 2)
      {
         /********/
         /* text */
         /********/

         ChunkPtr += 42;
         while (ChunkPtr < EndSectionChunkPtr)
            if (!ProcessTextSection (GlobalHorizontal, GlobalVertical,
                                     NULL, NULL))
               break;
         ChunkPtr = EndSectionChunkPtr;
      }
      else
      if (SectionSubType == 3)
      {
         /****************************************/
         /* some sort of X line-drawing language */
         /****************************************/
/*
         fprintf (stdout, "\n%s\n", ChunkPtr+42); 
         DumpBytes ("Section 18 SubType 3", ChunkPtr+42, SectionLength-42);
*/
         InsertComment (GlobalHorizontal, GlobalVertical,
            "Sorry.  HyperReader cannot reproduce this figure.",
            __LINE__);

         ChunkPtr = EndSectionChunkPtr;
      }
      else
      if (SectionSubType == 4 || SectionSubType == 5)
      {
         /****************/
         /* bitmap image */
         /****************/

         PlaceImage (GlobalHorizontal+SectionHorizOffset, GlobalVertical);
      }
      else
      {
         /***********/
         /* unknown */
         /***********/

         InsertComment (GlobalHorizontal, GlobalVertical,
            "Sorry.  HyperReader does not recognize the document format.",
            __LINE__);

         ChunkPtr = EndSectionChunkPtr;
      }

      return;
   }
   else
   if (SectionType == 20 || SectionType == 21)
   {
      /***********/
      /* hotspot */
      /***********/

      ProcessHotSpot (GlobalHorizontal, GlobalVertical);
      return;
   }
   else
   if (SectionType == 22 || SectionType == 23)
   {
      /*******************/
      /* graphic overlay */
      /*******************/

      ProcessGraphicOverlay ();
      return;
   }
   else
   {
      /***********/
      /* unknown */
      /***********/

      InsertComment (GlobalHorizontal, GlobalVertical,
         "Sorry.  HyperReader does not recognize the document format.",
         __LINE__);

      ChunkPtr = EndSectionChunkPtr;
      return;
   }
}

/*****************************************************************************/
/*
Process a chunk containing a table of contents, tables, figures, or an index.
Strictly control the line and column placement according to what we want, 
don't pay attention to any information associated with the text.
*/

ProcessContentsEtc ()

{
   int  Count;
   unsigned short  DataType;
   unsigned long  DataLength,
                  HotChunkNumber,
                  Column,
                  Line = 0,
                  Horizontal,
                  Vertical,
                  Length,
                  Width;
   char  NestedLevel;
   unsigned char  *NextChunkPtr,
                  *chptr;
   struct OutCharStruct  *ocptr;

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

   if (Debug) fprintf (stdout, "ProcessContentsEtc()\n");

   EnhanceText = false;
   ChunkPtr = ChunkBufferPtr + 0x06;

   while (ChunkPtr < EndChunkPtr)
   {
      DataType = *(unsigned short*)ChunkPtr;
      DataLength = *(unsigned long*)(ChunkPtr+2);
      NestedLevel = *(ChunkPtr+6);

      if (DebugChunks)
      {
         DumpLongWords ("CONTENTS:", ChunkPtr, 5);
         fprintf (stdout, "DataType: %d\n", DataType);
         fprintf (stdout, "DataLength: %d\n", DataLength);
         fprintf (stdout, "NestedLevel: %d\n", NestedLevel);
      }

      NextChunkPtr = ChunkPtr + DataLength;
      chptr = ChunkPtr + 20;

      while (*chptr == 2 || *chptr == 3) chptr += (unsigned char)*(chptr+1);
      while (*chptr++);

      /* I'm buggered if I know how this next statement decides! */
      if (chptr == NextChunkPtr-4)
         HotChunkNumber = *(unsigned long*)chptr;
      else
         HotChunkNumber = 0;

      ChunkPtr += 20;

      /* on all but the first line add a blank line before level 1 items */
      if (Line++)
         if (NestedLevel == 1) Line++;

      /* indent the lines of text according to the nesting level */
      Column = 0;
      for (Count = NestedLevel * 2; Count; Count--) Column++;

      while (*ChunkPtr == 3 || *ChunkPtr == 2)
      {
         Column = PlaceText (Line, Column);
         Column++;
      }
      Column--;

      if (HotChunkNumber)
      {
         Horizontal = SkipContentsItemNumber (Line) * COLUMN_POSITIONING_UNITS;
         Vertical = Line * LINE_POSITIONING_UNITS;
         Length = (Column * COLUMN_POSITIONING_UNITS) - Horizontal;
         Width = LINE_POSITIONING_UNITS;
         PlaceHotSpot (0, 0, Horizontal, Vertical,
                       Length, Width, HotChunkNumber);
      }

      /* scan past null-terminated text */
      while (*ChunkPtr++);

      ChunkPtr = NextChunkPtr;
   }
}

/*****************************************************************************/
/*
Return the column position of the first character following an item number 
such as "1.", "1.2", "1.2.3", "1-2", "1-2-3", "A.", "A.1", "A-1", "REF-1", 
etc.  If the line does not begin with such a string then return the column 
position of the first non-space character.  This allows HTML links to start on 
the description rather than item number of a contents line.
*/ 

int SkipContentsItemNumber (int Line)

{
   int  Column,
        FirstNonSpaceColumn;
   char  ccnt;
   struct OutCharStruct  *ocptr;

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

   if (Debug) fprintf (stdout, "SkipContentsItemNumber()\n");

   Column = 0;
   ocptr = OUT_CHAR_AT(Line,Column);

   /* skip leading white-space */
   while (Column < MAX_OUTLINE_LENGTH && ocptr->c == ' ')
   {
      ocptr++;
      Column++;
   }
   if (Column >= MAX_OUTLINE_LENGTH) return (0);
   FirstNonSpaceColumn = Column;

   if (isdigit(ocptr->c))
   {
      /*
         Line starts with a digit.  Scan across anything like
         a heading number, e.g. "1", "1.1", "1-1", etc.
      */
      while (Column < MAX_OUTLINE_LENGTH && 
             (isalnum(ocptr->c) || ocptr->c == '.' || ocptr->c == '-'))
      {
         ocptr++;
         Column++;
      }
      if (Column >= MAX_OUTLINE_LENGTH)
         return (FirstNonSpaceColumn);
      else;
   }
   else
   if (isalpha(ocptr->c))
   {
      /*
         Line starts with an alphabetic.  Check if its a single
         alphabetic such an Appendix (e.g. "A Topic"), or an alphabetic
         with sub-numbering (e.g. "A-1 Topic", "REF-1 Topic").
      */
      ocptr++;
      Column++;
      if (Column >= MAX_OUTLINE_LENGTH) return (FirstNonSpaceColumn);
      /* so far its been a single alphabetic, check if followed by a space */
      if (ocptr->c != ' ')
      {
         /* not a space, step over any contiguous alphas or digits */
         while (Column < MAX_OUTLINE_LENGTH &&
                (isalpha(ocptr->c) || isdigit(ocptr->c)))
         {
            ocptr++;
            Column++;
         }
         if (Column >= MAX_OUTLINE_LENGTH) return (FirstNonSpaceColumn);
         /* if its not one of these two then its not a contents-"number" */
         if (!(ocptr->c == '.' || ocptr->c == '-'))
            return (FirstNonSpaceColumn);
         /* step over any following alphas, digits, "." or "-" */
         while (Column < MAX_OUTLINE_LENGTH &&
                (isalpha(ocptr->c) || isdigit(ocptr->c) ||
                 ocptr->c == '.' || ocptr->c == '-'))
         {
            ocptr++;
            Column++;
         }
         if (Column >= MAX_OUTLINE_LENGTH)
            return (FirstNonSpaceColumn);
         else;
      }
      else;
   }
   else
      return (FirstNonSpaceColumn);

   /* scan across the intervening white-space */
   while (Column < MAX_OUTLINE_LENGTH && ocptr->c == ' ')
   {
      ocptr++;
      Column++;
   }
   if (Column >= MAX_OUTLINE_LENGTH) return (FirstNonSpaceColumn);
   return (Column);
}

/*****************************************************************************/
/*
Process a section of document text.  Locate it from the horizontal and 
vertical origins provided.
*/

ProcessTextSection
(
unsigned long H_Origin,
unsigned long V_Origin
)
{
   int  Line,
        Column;
   long  DataLength;
   unsigned short  Horizontal;
   unsigned short  Vertical;

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

   if (Debug) 
      fprintf (stdout, "ProcessTextSection() H_Origin: %d V_Origin: %d\n",
               H_Origin, V_Origin);

   if (*ChunkPtr == 1)
   {
      /***********/
      /* graphic */
      /***********/

      ProcessGraphicLine ();
      return (true);
   }
   else
   if (*ChunkPtr == 2 || *ChunkPtr == 3)
   {
      /***************/
      /* actual text */
      /***************/

      Horizontal = *(unsigned short*)(ChunkPtr+2);
      Vertical = *(unsigned short*)(ChunkPtr+4);

      Line =  (V_Origin + Vertical) / LINE_POSITIONING_UNITS;
      Column = (H_Origin + Horizontal) / COLUMN_POSITIONING_UNITS;

      PlaceText (Line, Column);

      return (true);
   }
   else
   {
      /***********/
      /* unknown */
      /***********/

      InsertComment (H_Origin, V_Origin,
         "Sorry.  HyperReader does not recognize the document format.",
         __LINE__);

      return (false);
   }
}

/*****************************************************************************/
/*
These seem to do things like draw boxes around SDML <BOX>() text and 
horizontal lines (e.g. the lines delimiting <NOTE>s) etc.  It looks like a 
single line drawing functionality.  These are ignored for HTML.
*/ 

ProcessGraphicLine ()

{
   long  DataLength;

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

   if (Debug) fprintf (stdout, "ProcessGraphicLine()\n");

   DataLength = *(unsigned char*)(ChunkPtr+1);

   if (DebugGraphics)
   {
      fprintf (stdout,"DataLength: %08.08X (%d)\n", DataLength, DataLength);
      DumpLongWords ("GRAPHIC LINE:", ChunkPtr+2, (DataLength-2)/4);
   }

   ChunkPtr += DataLength;
}

/*****************************************************************************/
/*
These seem to do things like place a block of shading (stipple) over a section 
of text.  It must be some sort of graphical "block" functionality.  The data 
structure appears similar to figures (images).  These are ignored for HTML.
*/ 

ProcessGraphicOverlay ()

{
   long  DataLength;

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

   if (Debug) fprintf (stdout, "ProcessGraphicOverlay()\n");

   DataLength = *(long*)(ChunkPtr+2);

   if (DebugGraphics)
   {
      fprintf (stdout,"DataLength: %08.08X (%d)\n", DataLength, DataLength);
      DumpLongWords ("GRAPHIC OVERLAY:", ChunkPtr+2, (DataLength-2)/4);
   }

   ChunkPtr += DataLength;
}

/*****************************************************************************/
/*
Create a hotspot by placing HTML link(s) around text in the specified 
location(s).  Handles single and two-line-spanning hotspots.
*/

ProcessHotSpot
(
unsigned long H_Origin,
unsigned long V_Origin
)
{
   unsigned char  *chptr;
   unsigned long  DataLength,
                  Horizontal,
                  Vertical,
                  Length,
                  Width,
                  HotChunkNumber,
                  GraphicCount,
                  SpanningDataLength;

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

   if (Debug) fprintf (stdout, "ProcessHotSpot()\n");

   DataLength = *(unsigned long*)(ChunkPtr+2);
   HotChunkNumber = *(unsigned long*)(ChunkPtr+34);
   SpanningDataLength = *(unsigned long*)(ChunkPtr+38);

   if (DebugHotspots)
   {
      DumpLongWords ("HOTSPOT:", ChunkPtr+2, 10);
      fprintf (stdout,"DataLength: %d\n", DataLength);
      fprintf (stdout,"HotChunkNumber: %d\n", HotChunkNumber);
      fprintf (stdout,"SpanningDataLength: %d\n", SpanningDataLength);
      if (SpanningDataLength)
         DumpLongWords ("SPANNING HOTSPOT:", ChunkPtr+42,
                        SpanningDataLength / sizeof(long));
   }

   if (SpanningDataLength)
   {
      /*
         "Spanning hotspots" are generated when a hotspot spans more
         than one line.  Bookreader seems to draw a series of graphic 
         lines starting with the top text line, bottom left, up and
         around and then down to the next line, and around that text.
         Emulate this by drawing two hotspots.
         If the hotspot spans three lines?  Tough!
      */

      chptr = ChunkPtr + 42;

      GraphicCount = *(unsigned long*)chptr;
      if (DebugHotspots) fprintf (stdout,"GraphicCount: %d\n", GraphicCount);

      chptr += 4;
      Horizontal = *(unsigned long*)(chptr+8);
      Vertical = *(unsigned long*)(chptr+12);
      Length = *(unsigned long*)(chptr+16) - Horizontal;
      Width = *(unsigned long*)(chptr+4) - Vertical;
      PlaceHotSpot (H_Origin, V_Origin, Horizontal, Vertical,
                    Length, Width, HotChunkNumber);

      chptr += 32;
      Horizontal = *(unsigned long*)(chptr+16);
      Vertical = *(unsigned long*)(chptr+28);
      Length = *(unsigned long*)chptr - Horizontal;
      Width = *(unsigned long*)(chptr+20) - Vertical;
      PlaceHotSpot (H_Origin, V_Origin, Horizontal, Vertical,
                    Length, Width, HotChunkNumber);
   }
   else
   {
      /* single line hotspot */

      Horizontal = *(unsigned long*)(ChunkPtr+18);
      Vertical = *(unsigned long*)(ChunkPtr+22);
      Length = *(unsigned long*)(ChunkPtr+26);
      Width = *(unsigned long*)(ChunkPtr+30);

      PlaceHotSpot (H_Origin, V_Origin, Horizontal, Vertical,
                    Length, Width, HotChunkNumber);
   }

   ChunkPtr += DataLength;
}

/*****************************************************************************/
/*
Create a hotspot by placing the start of an HTML link at the line and column 
represented by horizontal/vertical, and then placing the end of the link at 
horizontal+length/vertical+width.
*/

PlaceHotSpot
(
unsigned long H_Origin,
unsigned long V_Origin,
unsigned long Horizontal,
unsigned long Vertical,
unsigned long Length,
unsigned long Width,
unsigned long HotChunkNumber
)
{
   int  ccnt,
        StringLength,
        Line,
        Column;
   char  String [1024];
   struct OutCharStruct  *ocptr;

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

   if (Debug) fprintf (stdout, "PlaceHotSpot()\n");

   if (DebugHotspots)
   {
      fprintf (stdout,"H_Origin: %d\n", H_Origin);
      fprintf (stdout,"V_Origin: %d\n", V_Origin);
      fprintf (stdout,"Horizontal: %d\n", Horizontal);
      fprintf (stdout,"Vertical: %d\n", Vertical);
      fprintf (stdout,"Length: %d\n", Length);
      fprintf (stdout,"Width: %d\n", Width);
      fprintf (stdout,"HotChunkNumber: %d\n", HotChunkNumber);
   }

   /* 
       Attempt to place this on the line containing the text to be 
       "hotspot"ed.  'Vert' is the vertical location,  'Width' must
       be the the width of the hotspot area.  Three-quarters down
       the 'Width' value is used to calculate the line position.

       BTW; seems to work well ... most of the time!

       Use bit-wise shifting to enhance the speed of the divisions:
       excerpt equals: "(((Vert + (Width / 2) + (Width / 4))"
   */

   Line = (V_Origin + Vertical + (Width >> 1) + (Width >> 2))
          / LINE_POSITIONING_UNITS;
   if (Line > LinesOnPage) PageSize (Line);
   if (Line > LastLineOnPage) LastLineOnPage = Line;

   /*
      If this is a totally blank line then its probably an artifact of the
      mapping process.  Attempt a correction by stepping to the next line.
   */
   for (;;)
   {
      ocptr = OUT_CHAR_AT(Line,0);
      for (ccnt = 0; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
      {
         if (ocptr->c != ' ') break;
         ocptr++;
      }
      if (ccnt < MAX_OUTLINE_LENGTH || Line >= LinesOnPage) break;
      Line++;
   }

   /*
      Calculate the hotspot start column.  If it lands on a space then
      scan forward to the first non-space character encountered.
   */

   Column = (H_Origin + Horizontal) / COLUMN_POSITIONING_UNITS;
   if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;

   ocptr = OUT_CHAR_AT(Line,Column);
   for (ccnt = Column; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
   {
      if (ocptr->c != ' ') break;
      ocptr++;
   }
   if (ccnt < MAX_OUTLINE_LENGTH) Column = ccnt;

   /* place the start of hotspot into the text */

   StringLength = sprintf (String,
      "<a href=\"%s%s?%schunk=%d&referer=%s&title=%s%s\">",
      CgiScriptNamePtr, UrlEncodedCgiPathInfoPtr, BookSpecifiedByFile,
      (int)SectionChunkNumbersArrayPtr[HotChunkNumber],
      UrlEncReferer, UrlEncTitle, UrlDoMassage);

   ocptr = OUT_CHAR_AT(Line,Column);
   ocptr->htptr = HtmlString (ocptr->htptr, StringLength);
   strcat (ocptr->htptr, String);

   /*
      Calculate the hotspot end column.  If it lands on a space then
      scan backward to the first non-space character encountered.
   */

   if (Column + Length / COLUMN_POSITIONING_UNITS <= Column)
   {
      Column++;
      if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;
   }
   else
   {
      Column += (Length / COLUMN_POSITIONING_UNITS) - 1;
      if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;

      ocptr = OUT_CHAR_AT(Line,Column);
      for (ccnt = Column; ccnt > 0; ccnt--)
      {
         if (ocptr->c != ' ') break;
         ocptr--;
      }
      if (ccnt > 0) Column = ccnt + 1;
      if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;
   }

   /* place the end of hotspot into the text */
   ocptr = OUT_CHAR_AT(Line,Column);
   ocptr->htptr = HtmlString (ocptr->htptr, 4);
   strcat (ocptr->htptr, "</A>");
}

/*****************************************************************************/
/*
Place a "string" of text characters into the "page".  With Bookreader 
documents a single "string" of characters NEVER spans multiple lines.  The 
"string" is pointed to by 'ChunkPtr'.
*/

int PlaceText
(
int Line,
int Column
)
{
   static int  CallCount = 0;

   int  idx, count,
        FontNumber;
   unsigned char  TextType;
   unsigned char  Length;
   unsigned short  Horizontal;
   unsigned short  Vertical;
   unsigned short  Unknown;
   unsigned char  *Text;
   struct OutCharStruct  *ocptr;

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

   if (Line > LinesOnPage) PageSize (Line);
   if (Line > LastLineOnPage) LastLineOnPage = Line;
   if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;

   TextType = *ChunkPtr;
   Length = *(ChunkPtr+1);
   FontNumber = *(ChunkPtr+6);
   Unknown = *(unsigned short*)(ChunkPtr+7);

   if (DebugText)
   {
      fprintf (stdout,
"\nPlaceText()\n\
Len: %d  ?: %d  Line: %d  Column: %d  Font: %d  Bld: %d  It: %d  Sym: %d\n",
      Length, Unknown, Line, Column,
      FontNumber,
      FontArrayPtr[FontNumber].FontBold,
      FontArrayPtr[FontNumber].FontItalic,
      FontArrayPtr[FontNumber].FontUnsupported);
   }

   /********************/
   /* text enhancement */
   /********************/

   if (EnhanceText)
   {
      if (FontArrayPtr[FontNumber].FontBold)
      {
         ocptr = OUT_CHAR_AT(Line,Column);
         ocptr->htptr = HtmlString (ocptr->htptr, 3);
         strcat (ocptr->htptr, "<b>");
      }
      else
      if (FontArrayPtr[FontNumber].FontItalic)
      {
         ocptr = OUT_CHAR_AT(Line,Column);
         ocptr->htptr = HtmlString (ocptr->htptr, 3);
         strcat (ocptr->htptr, "<i>");
      }
   }

   /*********************************************/
   /* put the characters into the output buffer */
   /*********************************************/

   Text = ChunkPtr+9;
   idx = 0;
   while (idx < Length-8)
   {
      count = (int)Text[idx++];
      while (count-- > 0)
      {
         if (Column < MAX_OUTLINE_LENGTH)
         {
            if (FontArrayPtr[FontNumber].FontUnsupported ||
                Text[idx] < 0x20 ||
                (Text[idx] > 0x7f && Text[idx] < 0xa0))
               OUT_CHAR_AT(Line,Column)->c = NON_SUPPORTED_CHAR;
            else
               OUT_CHAR_AT(Line,Column)->c = Text[idx];

            if (DebugText)
            {
/*
               fprintf (stdout, "%c", OUT_CHAR_AT(Line,Column)->c);
*/
               fprintf (stdout, "%c(%02.02x)[%d] ",
                       OUT_CHAR_AT(Line,Column)->c,
                       OUT_CHAR_AT(Line,Column)->c,
                       Column);
            }
         }
         Column++;
         idx++;
      }
      if (idx < Length-9)
         Column += (Text[idx] / COLUMN_POSITIONING_UNITS) + 1;
      idx++;
   }

   if (DebugText) fputc ('\n', stdout);
   if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;

   /************************/
   /* end text enhancement */
   /************************/

   if (EnhanceText)
   {
      if (FontArrayPtr[FontNumber].FontBold)
      {
         ocptr = OUT_CHAR_AT(Line,Column);
         ocptr->htptr = HtmlString (ocptr->htptr, 4);
         strcat (ocptr->htptr, "</b>");
      }
      else
      if (FontArrayPtr[FontNumber].FontItalic)
      {
         ocptr = OUT_CHAR_AT(Line,Column);
         ocptr->htptr = HtmlString (ocptr->htptr, 4);
         strcat (ocptr->htptr, "</i>");
      }
   }

   ChunkPtr += Length;
   return (Column);
}

/*****************************************************************************/
/*
Put a link into the text to retrieve the image.
*/

PlaceImage
(
unsigned long H_Origin,
unsigned long V_Origin
)
{
   int  ccnt,
         Length,
        Line,
        Column;
   long  DataLength,
         Horizontal,
         Offset,
         Vertical;
   char  String [2048];
   struct OutCharStruct  *ocptr;

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

   if (Debug)
      fprintf (stdout, "PlaceImage() H_Origin: %d V_Origin: %d\n",
               H_Origin, V_Origin);

   DataLength = *(long*)(ChunkPtr+2);
   Horizontal = *(long*)(ChunkPtr+18);
   Vertical = *(long*)(ChunkPtr+22);

   if (Debug)
   {
      DumpLongWords ("FIGURE:", ChunkPtr+2, 12);
      fprintf (stdout,"DataLength : %d\n", DataLength);
      fprintf (stdout,"Horizontal : %d\n", Horizontal);
      fprintf (stdout,"Vertical : %d\n", Vertical);
   }

   Line = (V_Origin + Vertical) / LINE_POSITIONING_UNITS;
   if (!Line) Line = 1;
   if (Line > LinesOnPage) PageSize (Line);
   if (Line > LastLineOnPage) LastLineOnPage = Line;

   /*
      Without the ability to flow text around an image (which current
      browsers lack) and we're on a line containing text attempt to step
      back to a blank line.
   */
   for (;;)
   {
      ocptr = OUT_CHAR_AT(Line,0);
      for (ccnt = 0; ccnt < MAX_OUTLINE_LENGTH; ccnt++)
      {
         if (ocptr->c != ' ') break;
         ocptr++;
      }
      if (ccnt >= MAX_OUTLINE_LENGTH || Line <= 1) break;
      Line--;
   }

   Column = (H_Origin + Horizontal) / COLUMN_POSITIONING_UNITS;
   if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;
   if (DebugFigures) fprintf (stdout,"Line: %d Column: %d\n", Line, Column);

   /*
      The four numbers in the URI are:
      (note that parts one and two constitute the RMS RFA of the record)

      1.  Virtual Block Number of the start of the part
      2.  starting byte position in the VBN
      3.  total length of the chunk
      4.  offset from start of chunk the image data begins

      This will allow the image to be retrieved very quickly by merely
      opening the file, reading the part (one or more records) via the RFA
      and jumping in to process the image data from the offset.
   */

   Offset = ChunkPtr - ChunkBufferPtr;

   Length = sprintf (String,
"<img src=\"%s%s?%sgraphic=%d,%d,%d,%d\" align=\"top\" alt=\"&lt;image&gt;\">\n",
   CgiScriptNamePtr, UrlEncodedCgiPathInfoPtr, BookSpecifiedByFile,
   ChunkArrayPtr[GetChunkNumber].VBN, ChunkArrayPtr[GetChunkNumber].VBNbyte,
   ChunkArrayPtr[GetChunkNumber].Length, Offset);

   ocptr = OUT_CHAR_AT(Line,Column);
   ocptr->htptr = HtmlString (ocptr->htptr, Length);
   strcat (ocptr->htptr, String);

   ChunkPtr += DataLength;
}

/*****************************************************************************/
/*
Read the font information from the respective part of the Bookreader file.  
Font 'numbers' represent the font being specified within bookreader text.  
Note the point value of each font, and which font 'number' is bold, italic, 
symbol, etc.  This information is used when mapping the text.
*/ 

GetFonts ()

{
   int  count,
        FontCount;
   short  Short1,
          Short2,
          Short3,
          FontNumber;
   char  *fptr, *cptr;

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

   if (Debug) fprintf (stdout, "GetFonts()\n");

   GetChunk (&FontChunk);
   fptr = (char*)ChunkBufferPtr+6;
   for (FontCount = 0; FontCount < NumberOfFonts; FontCount++)
   {
      Short1 = *(short*)fptr;
      Short2 = *(short*)(fptr+2);
      Short3 = *(short*)(fptr+4);
      FontNumber = *(short*)(fptr+6);

      if (DebugFonts)
         fprintf (stdout, "Font \
#%03d 0x%04.04x  0x%04.04x  0x%04.04x  0x%04.04x\n|%s|\n",
FontNumber, Short1, Short2, Short3, FontNumber, fptr+8);

      FontArrayPtr[FontNumber].FontSize = 0;
      count = 0;
      cptr = (char*)fptr+8;
      while (*cptr && count < 8)
          if (*cptr++ == '-') count++;
      FontArrayPtr[FontNumber].FontSize = atoi (cptr) / 10;

      /* push the font name to upper case for easy strstr() below */
      for (cptr = fptr+8; *cptr; cptr++) *cptr = toupper(*cptr);

      FontArrayPtr[FontNumber].FontBold =
      FontArrayPtr[FontNumber].FontItalic =
      FontArrayPtr[FontNumber].FontUnsupported = false;

      /* "*-interim dm-*" has some interesting graphics/symbol characters */
      if (strstr (fptr+8, "INTERIM DM"))
         FontArrayPtr[FontNumber].FontUnsupported = true;
      else
      if (strstr (fptr+8, "SYMBOL"))
         FontArrayPtr[FontNumber].FontUnsupported = true;
      else
      if (strstr (fptr+8, "BOLD"))
         FontArrayPtr[FontNumber].FontBold = true;
      else
      if (strstr (fptr+8, "-I-"))
         FontArrayPtr[FontNumber].FontItalic = true;

      fptr += 8;
      while (*fptr) fptr++;
      fptr++;
   }
}

/*****************************************************************************/
/*
This function comes from original code, so I'm a bit hazy on its functionality.
*/

GetSectionXref ()

{
   int  idx;
   char  *cptr;

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

   if (Debug) fprintf (stdout, "GetSectionXref()\n");

   GetChunk (&SectionXrefChunk);

   cptr = (char*)ChunkBufferPtr + 0x06;
   for (idx = 0; idx < NumberOfSections; idx++)
   {
       SectionChunkNumbersArrayPtr[idx] = *(long*)cptr;
/*
       if (DebugChunks)
          fprintf (stdout, "Content %d.  0x%08.08lx\n", idx, *(long*)cptr);
*/
       cptr += sizeof(long);
   }
}

/*****************************************************************************/
/*
This function comes from original code, so I'm a bit hazy on its functionality.
*/

GetFirstChunk ()

{
   int  status,
        SegmentNameLength;
   short  SegmentType,
          SegmentLength;
   long  SegmentData1,
         SegmentData2,
         SegmentData3,
         SegmentChunkNumber;
   char  *chptr,
         *SegmentNamePtr;

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

   if (Debug) fprintf (stdout, "GetFirstChunk()\n");

   CurrentSeek = 0;
   ChunkNumber = 0;

   FirstChunk.VBN = 1;
   FirstChunk.VBNbyte = 0;
   FirstChunk.Length = 1022;
   GetChunk (&FirstChunk);

   NumberOfChunks = *(long*)(ChunkBufferPtr+0x46);
   NumberOfSections = *(long*)(ChunkBufferPtr+0x4a);
   NumberOfFonts = *(long*)(ChunkBufferPtr+0x52);
   LastVBN = *(long*)(ChunkBufferPtr+0x5a);
   LastVBNbyte = *(short*)(ChunkBufferPtr+0x5e);
   LastVBNlen = *(short*)(ChunkBufferPtr+0x60);

   LastChunk.VBN = LastVBN;
   LastChunk.VBNbyte = LastVBNbyte;
   LastChunk.Length = LastVBNlen;

   strcpy (BookTitle, (char*)ChunkBufferPtr+0x7f);

   if (DebugChunks)
   {
      fprintf (stdout, "ChunkType: %04.04X (%d)\n",
               CurrentChunkType, CurrentChunkType);
      fprintf (stdout, "ChunkLength: %08.08X (%ld)\n",
               CurrentChunkLength, CurrentChunkLength);
      fprintf (stdout, "NumberOfChunks: %08.08X (%ld)\n",
              NumberOfChunks, NumberOfChunks);
      fprintf (stdout, "NumberOfSections: %08.08X (%ld)\n",
              NumberOfSections, NumberOfSections);
      fprintf (stdout, "NumberOfFonts: %08.08X (%ld)\n",
              NumberOfFonts, NumberOfFonts);
      fprintf (stdout, "LastVBN: %08.08X (%ld)\n",
               LastVBN, LastVBN);
      fprintf (stdout, "LastVBNbyte: %04.04X (%d)\n",
              (int)LastVBNbyte, (int)LastVBNbyte);
      fprintf (stdout, "LastVBNlen: %04.04X (%d)\n",
              (int)LastVBNlen, (int)LastVBNlen);
      fprintf (stdout, "BookTitle: |%s|\n", BookTitle);
   }

   ChunkArrayPtr = calloc (sizeof(struct ChunkDataStruct), NumberOfChunks);
   if (!ChunkArrayPtr)
   {
      status = vaxc$errno;
      CgiLibResponseError (FI_LI, status, ErrorCalloc);
      /* definitely exit if we can't get memory!!! */
      exit (SS$_NORMAL);
   }

   SectionChunkNumbersArrayPtr = calloc (sizeof(int), NumberOfSections);
   if (!SectionChunkNumbersArrayPtr)
   {
      status = vaxc$errno;
      CgiLibResponseError (FI_LI, status, ErrorCalloc);
      /* definitely exit if we can't get memory!!! */
      exit (SS$_NORMAL);
   }

   FontArrayPtr = calloc (sizeof(struct FontStruct), NumberOfFonts);
   if (!FontArrayPtr)
   {
      status = vaxc$errno;
      CgiLibResponseError (FI_LI, status, ErrorCalloc);
      /* definitely exit if we can't get memory!!! */
      exit (SS$_NORMAL);
   }

   chptr = (char*)ChunkBufferPtr + 0x128;
   while (*(short*)chptr)
   {
      SegmentType = *(short*)chptr;
      SegmentLength = *(short*)(chptr+2);
      SegmentData1 = *(long*)(chptr+4);
      SegmentData2 = *(long*)(chptr+10);
      SegmentData3 = *(long*)(chptr+11);
      SegmentChunkNumber = *(long*)(chptr+15);
      SegmentNamePtr = chptr+20;

      if (Debug && SegmentType == 12)
      {
         fprintf (stdout,
"Segment; Type: %04.04X Length: %04.04X SegmentNamePtr: |%s|\n\
Data1: %08.08X Data2: %08.08X Data3: %08.08X ChunkNumber: %08.08X\n",
            SegmentType, SegmentLength,  SegmentNamePtr,
            SegmentData1, SegmentData2, SegmentData3, SegmentChunkNumber);
      }

      if (SegmentType == 12)
      {
         if (++BookDivisionCount >= MAX_DIVISIONS)
         {
            CgiLibResponseError (FI_LI, 0, ErrorTooManyDivisions);
            return;
         }

         BookDivision[BookDivisionCount].DescriptionPtr =
            calloc (1, SegmentNameLength = strlen(SegmentNamePtr) + 1);
         if (!BookDivision[BookDivisionCount].DescriptionPtr)
         {
            status = vaxc$errno;
            CgiLibResponseError (FI_LI, status, ErrorCalloc);

            /* definitely exit if we can't get memory!!! */
            exit (SS$_NORMAL);
         }
         memcpy (BookDivision[BookDivisionCount].DescriptionPtr,
                 SegmentNamePtr,
                 SegmentNameLength);
         BookDivision[BookDivisionCount].ChunkNumber = SegmentChunkNumber;
      }

      chptr += SegmentLength;
   }
}

/*****************************************************************************/
/*
The last "chunk" contains a series of ten-byte structures containing the 
Record File Address (RFA) of each "chunk" (the VBN and byte of VBN), and the 
total length of the "chunk", which can span individual records.
*/ 

GetLastChunk ()

{
   int  count,
        SeekPos;
   short  ChunkType;
   char  *chptr;

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

   if (Debug) fprintf (stdout, "GetLastChunk()\n");

   /* last record/part is the table of parts */
   GetChunk (&LastChunk);

   if (DebugChunks)
       fprintf (stdout,
"(Xref = 2, Headings = 3, Symbols = 4, Fonts = 5)\n\
Chunks \
num   VBN          VBNbyte   len\n");

   chptr = (char*)ChunkBufferPtr + 6;
   for (count = 0; count < NumberOfChunks; count++)
   {
      ChunkArrayPtr[count].VBN = *(long*)chptr;
      ChunkArrayPtr[count].VBNbyte = *(short*)(chptr+4);
      ChunkArrayPtr[count].Length = *(long*)(chptr+6);
      chptr += 10;

/*
      if (DebugChunks)
      {
         fprintf (stdout, "Chunk \
%3d   0x%08.08lx   0x%04.04x   0x%08.08lx\n",
         count, ChunkArrayPtr[count].VBN, (int)ChunkArrayPtr[count].VBNbyte,
         ChunkArrayPtr[count].Length);
      }
*/
   }

   /* >>>>> THE FOLLOWING IS AN AREA FOR POSSIBLE IMPROVEMENT <<<<< */

   for (count = 0; count < NumberOfChunks; count++)
   {
      GetChunk (&ChunkArrayPtr[count]);
      if (CurrentChunkType == 0x0006)
      {
         SectionXrefChunk.VBN = ChunkArrayPtr[count].VBN;
         SectionXrefChunk.VBNbyte = ChunkArrayPtr[count].VBNbyte;
         SectionXrefChunk.Length = ChunkArrayPtr[count].Length;
      }
      else
      if (CurrentChunkType == 0x0007)
      {
         SectionHeadingsChunk.VBN = ChunkArrayPtr[count].VBN;
         SectionHeadingsChunk.VBNbyte = ChunkArrayPtr[count].VBNbyte;
         SectionHeadingsChunk.Length = ChunkArrayPtr[count].Length;
      }
      else
      if (CurrentChunkType == 0x0009)
      {
         FontChunk.VBN = ChunkArrayPtr[count].VBN;
         FontChunk.VBNbyte = ChunkArrayPtr[count].VBNbyte;
         FontChunk.Length = ChunkArrayPtr[count].Length;
         break;
      }
      else
      if (CurrentChunkType == 0x000d)
      {
         SymbolsChunk.VBN = ChunkArrayPtr[count].VBN;
         SymbolsChunk.VBNbyte = ChunkArrayPtr[count].VBNbyte;
         SymbolsChunk.Length = ChunkArrayPtr[count].Length;
      }
   }
}

/*****************************************************************************/
/*
A 'chunk' consist of 1..n bytes, read beginning a specified byte within a 
specified VBN of the Bookreader file.  Dynamically allocate (or reallocate) 
space for the "chunk" buffer.

The record structure is a little complex, and I haven't bothered to fathom it 
all out ... this works for HYPERREADER.  If there is only one record in the
chunk  then it is straight-forward, read the record, end of story.  If there
are two  or more records then ALL BUT THE LAST record have record-related data
(not  Bookreader information) in the LAST 10 BYTES and ALL in the FIRST 6 BYTES
of  each record.  HYPERREADER concatenates all records making up a chunk and
these 16  bytes information can be safely ignored.  Instead of expensive memory
copying  of record data to adjust these offsets on subsequent reads, the last
10 bytes  of the previous record are just written over and the first 6 bytes of
the next  record positioned to overwrite the 6 bytes prior to the last 10 of
the  previous record.  These 6 bytes are buffered and restored.
*/ 

GetChunk (struct ChunkDataStruct *ChunkDataPtr)

{
   int  status,
        RecordCount = 0,
        ChunkBufferRemaining;
   unsigned char  Buffer6 [6];

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

   if (Debug)
      fprintf (stdout, "GetChunk() VBN: %d VBNbyte %d Length: %d\n",
               ChunkDataPtr->VBN, ChunkDataPtr->VBNbyte, ChunkDataPtr->Length);

   /* allocate enough memory to buffer the entire "chunk" of the book */
   if (Debug)
      fprintf (stdout, "PreviousChunkLength: %d ChunkDataPtr->Length: %d\n",
               PreviousChunkLength, ChunkDataPtr->Length);
   if (PreviousChunkLength < ChunkDataPtr->Length)
   {
      if (ChunkBufferPtr) free (ChunkBufferPtr);
      ChunkBufferPtr = calloc (1, PreviousChunkLength =
                                  ChunkDataPtr->Length +
                                  BookFileLongestRecordLength);
      if (!ChunkBufferPtr)
      {
         status = vaxc$errno;
         CgiLibResponseError (FI_LI, status, ErrorCalloc);

         /* definitely exit if we can't get memory!!! */
         exit (SS$_NORMAL);
      }
   }
   DynamicMemoryAllocated += ChunkDataPtr->Length;

   /* prepare for reading the record via Record File Address (RFA) */
   BookFileRab.rab$l_rfa0 = ChunkDataPtr->VBN;
   BookFileRab.rab$w_rfa4 = ChunkDataPtr->VBNbyte;
   BookFileRab.rab$b_rac = RAB$C_RFA;

   BookFileRab.rab$l_ubf = (char*)ChunkBufferPtr;
   ChunkBufferRemaining = ChunkDataPtr->Length +
                          BookFileLongestRecordLength;
   if (ChunkBufferRemaining <= BookFileLongestRecordLength)
      BookFileRab.rab$w_usz = ChunkBufferRemaining;
   else
      BookFileRab.rab$w_usz = BookFileLongestRecordLength;

   /* accumulate the total bytes read in global storage to be used later */
   CurrentChunkLength = 0;

   for (;;)
   {
      if (Debug)
         fprintf (stdout,
                  "ChunkBufferRemaining: %d BookFileRab.rab$w_usz: %d\n",
                  ChunkBufferRemaining, BookFileRab.rab$w_usz);
      if (BookFileVarRec)
         status = sys$get (&BookFileRab, 0, 0);
      else
         status = GetRecord (&BookFileRab);
      if (VMSnok (status)) break;
      if (Debug)
         fprintf (stdout, "RecordCount: %d BookFileRab.rab$w_rsz: %d\n",
                  RecordCount, BookFileRab.rab$w_rsz);
      TotalBytesRead += BookFileRab.rab$w_rsz;

      /* on record 2 ... last restore the overwritten 6 bytes */
      if (RecordCount++) memcpy (BookFileRab.rab$l_ubf, Buffer6, 6);

      /* break if we've reached the requested chunk size */
      if ((CurrentChunkLength += BookFileRab.rab$w_rsz) >=
          ChunkDataPtr->Length) break;

      /*
          Append any subsequent record to what has been read before.
          Ignore tha last 10 bytes of the record just read, and also
          overwrite the 6 bytes prior to that, buffering it first
          so that it may be restored afterwards.  As this is not the
          last record reduce the accumulated chunk length by 10 + 6.
      */
      BookFileRab.rab$l_ubf += BookFileRab.rab$w_rsz - 16;
      ChunkBufferRemaining -= BookFileRab.rab$w_rsz - 16; 
      if (ChunkBufferRemaining <= BookFileLongestRecordLength)
         BookFileRab.rab$w_usz = ChunkBufferRemaining;
      else
         BookFileRab.rab$w_usz = BookFileLongestRecordLength;
      CurrentChunkLength -= 16;
      memcpy (Buffer6, BookFileRab.rab$l_ubf, 6); 

      /* the first read is by RFA, subsequent reads are done sequentially */
      BookFileRab.rab$b_rac = RAB$C_SEQ;
   }
   if (VMSnok (status))
   {
      CgiLibResponseError (FI_LI, status, ErrorChunkRead);
      exit (status | STS$M_INHIB_MSG);
   }

   ChunkPtr = ChunkBufferPtr;
   EndChunkPtr = ChunkBufferPtr + CurrentChunkLength;
   CurrentChunkType = *(short*)ChunkBufferPtr;
   if (Debug)
      fprintf (stdout, "ChunkPtr: %d EndChunkPtr: %d CurrentChunkLength: %d\n",
               ChunkPtr, EndChunkPtr, CurrentChunkLength);

   if (DebugBytes) DumpBytes ("GETCHUNK:", ChunkBufferPtr, CurrentChunkLength);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
For file formats that are non-VAR, as are (often?) NFS-served books which are
(always?) UDF (undefined), use this function to emulate VAR record format
record 'get's from the file.  Uses block IO and some jiggery-pokery with the
RFA and previous RSZ fields as appropriate.  Bit clunky but I guess that fits
in well with the rest of the application.
*/
 
int GetRecord (struct RAB *rabptr)

{
   int  status,
        BlockCount,
        BlockNumber;
   unsigned long  vbn;
   unsigned short  byte, usz, rsz;
   char  *ubf, *recptr;
   /* maximum buffer space needed will be 32767 plus two blocks */
   char  BlockBuffer [(512*64)+(512*2)];

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

   if (Debug)
      fprintf (stdout, "GetRecord() rac:%d usz:%d rsz:%d rfa0:%d rfa4:%d\n",
               rabptr->rab$b_rac, rabptr->rab$w_usz, rabptr->rab$w_rsz,
               rabptr->rab$l_rfa0, rabptr->rab$w_rfa4);

   rabptr->rab$w_usz = BookFileLongestRecordLength;
   if (rabptr->rab$b_rac != RAB$C_RFA)
   {
      /* calculate VBN/byte of the next record using previous record size */
      rabptr->rab$w_rsz += 2;
      vbn = rabptr->rab$w_rsz / 512;
      byte = rabptr->rab$w_rsz % 512;
      rabptr->rab$l_rfa0 += vbn;
      if (rabptr->rab$w_rfa4 + byte < 512)
         rabptr->rab$w_rfa4 += byte;
      else
      {
         rabptr->rab$l_rfa0++;
         rabptr->rab$w_rfa4 = 512 - byte;
      }
      if (Debug)
         fprintf (stdout, "rfa0:%d rfa4:%d\n",
                  rabptr->rab$l_rfa0, rabptr->rab$w_rfa4);
   }

   BlockNumber = rabptr->rab$l_rfa0;
   BlockCount = (rabptr->rab$w_usz / 512) + 2;
   if (Debug) fprintf (stdout, "bkt:%d cnt:%d\n", BlockNumber, BlockCount);

   ubf = rabptr->rab$l_ubf; 
   usz = rabptr->rab$w_usz; 
   rabptr->rab$l_bkt = BlockNumber; 
   rabptr->rab$l_ubf = BlockBuffer; 
   rabptr->rab$w_usz = BlockCount * 512; 

   status = sys$read (&BookFileRab, 0, 0);
   if (Debug) fprintf (stdout, "sys$read() %%X%08.08X\n", status);

   if (rabptr->rab$w_rfa4 > sizeof(BlockBuffer)) return (RMS$_EOF);

   rabptr->rab$l_ubf = ubf; 
   rabptr->rab$w_usz = usz; 

   if (VMSnok (status)) return (status);

   recptr = BlockBuffer + rabptr->rab$w_rfa4;
   rsz = *(unsigned short*)recptr;
   if (Debug) fprintf (stdout, "rsz:%d\n", rsz);
   if (rsz > usz) return (RMS$_UBF); 
   memcpy (rabptr->rab$l_ubf, recptr+2, rsz); 
   rabptr->rab$w_rsz = rsz;

   return (status);
}

/*****************************************************************************/
/*
*/
 
int OpenBookFile ()

{
   int  status;

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

   if (Debug) fprintf (stdout, "OpenBookFile() %s\n", BookFileNamePtr);

   /* file access block */
   BookFileFab = cc$rms_fab;
   BookFileFab.fab$b_fac = FAB$M_GET | FAB$M_BRO;
   BookFileFab.fab$l_dna = "DECW$BOOK:.DECW$BOOK";
   BookFileFab.fab$b_dns = 20;
   BookFileFab.fab$b_shr = FAB$M_SHRGET;
   BookFileFab.fab$l_xab = &BookFileXabDat;
   BookFileXabDat = cc$rms_xabdat;
   BookFileXabDat.xab$l_nxt = &BookFileXabFhc;
   BookFileXabFhc = cc$rms_xabfhc;

#ifdef ODS_EXTENDED
   if (OdsExtended)
   {
      BookFileFab.fab$l_fna = (char*)-1;
      BookFileFab.fab$b_fns = 0;
      BookFileFab.fab$l_nam = (struct namdef*)&BookFileNaml;

      ENAMEL_RMS_NAML(BookFileNaml)
      BookFileNaml.naml$l_long_filename = BookFileNamePtr;
      BookFileNaml.naml$l_long_filename_size = strlen(BookFileNamePtr);
      BookFileNaml.naml$l_long_expand = ExpBookFileName;
      BookFileNaml.naml$l_long_expand_alloc = sizeof(ExpBookFileName)-1;
   }
   else
#endif /* ODS_EXTENDED */
   {
      BookFileFab.fab$l_fna = BookFileNamePtr;
      BookFileFab.fab$b_fns = strlen(BookFileNamePtr);
      BookFileFab.fab$l_nam = &BookFileNam;

      BookFileNam = cc$rms_nam;
      BookFileNam.nam$l_esa = ExpBookFileName;
      BookFileNam.nam$b_ess = ODS2_MAX_FILE_NAME_LENGTH;
   }

   if (VMSok (status = sys$open (&BookFileFab, 0, 0)))
   {
      /* terminate 'ExpBookFileName' at the version delimiter */
#ifdef ODS_EXTENDED
      if (OdsExtended)
      {
         *BookFileNaml.naml$l_long_ver = '\0';
         ExpBookFileNamePtr = BookFileNaml.naml$l_long_name;
      }
      else
#endif /* ODS_EXTENDED */
      {
         *BookFileNam.nam$l_ver = '\0';
         ExpBookFileNamePtr = BookFileNam.nam$l_name;
      }

      if (*CgiFormVarRecPtr)
      {
         if (*CgiFormVarRecPtr == '1' ||
             toupper(*CgiFormVarRecPtr) == 'Y')
            BookFileVarRec = true;
         else
            BookFileVarRec = false;
      }
      else
      if (BookFileFab.fab$b_rfm == FAB$C_VAR)
         BookFileVarRec = true;
      else
         BookFileVarRec = false;

      /* record access block */
      BookFileRab = cc$rms_rab;
      BookFileRab.rab$l_fab = &BookFileFab;
      if (BookFileVarRec)
      {
         /* 3 buffers, read ahead performance option */
         BookFileRab.rab$b_mbf = 3;
         BookFileRab.rab$l_rop = RAB$M_RAH;
      }
      else
         BookFileRab.rab$l_rop = RAB$M_BIO;

      if (CgiHttpIfModifiedSincePtr[0])
      {
         if (VMSnok (status =
             ModifiedSince (&IfModifiedSinceBinaryTime,
                            &BookFileXabDat.xab$q_rdt)))
         {
            /* book has not been modified since the date/time, don't send */
            return (status);
         }
      }

      HttpGmTimeString (LastModifiedGmDateTime, &BookFileXabDat.xab$q_rdt);

      if (Debug) fprintf (stdout, "lrl: %d\n", BookFileXabFhc.xab$w_lrl);
      if (!(BookFileLongestRecordLength = BookFileXabFhc.xab$w_lrl))
         BookFileLongestRecordLength = 32767;

      return (sys$connect (&BookFileRab, 0, 0));
   }

   if (CgiFormTitlePtr[0])
      CgiLibResponseError (FI_LI, status, CgiFormTitlePtr);
   else
   if (CgiPathInfoPtr[0])
      CgiLibResponseError (FI_LI, status, CgiPathInfoPtr);
   else
      CgiLibResponseError (FI_LI, status, CgiFormFilePtr);
   return (status);
}

/*****************************************************************************/
/*
Create a series of navigation buttons, e.g. "next", "previous", "contents", 
"tables", "figures", "index", etc.  These are provided at the top and bottom 
of each page.  The buttons can be either highlighted anchors (if the button 
represents an available link) or just "there" (if there is no relevant link, 
i.e. on the "contents" page it is redundant to have a "contents" link) as 
appropriate.
*/ 
ButtonBar (int Top1Bottom2)

{
   int  idx;
   char  *cptr, *sptr;

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

   if (Debug) fprintf (stdout, "ButtonBar()\n");

   TBW += fprintf (stdout, "<div class=\"buttonbar%s\">\n",
                   Top1Bottom2 == 1 ? " butbartop" : "");

   cptr = ButtonsPtr;

   /* previous */
   cptr = ButtonBarButton (cptr, NULL, PreviousChunkNumber, DoMassage);

   /* back */
   cptr = ButtonBarButton (cptr, "", -1, false);

   /* next */
   cptr = ButtonBarButton (cptr, NULL, NextChunkNumber, DoMassage);

   /* divisions provided in the book itself */
   for (idx = 1; idx <= BookDivisionCount; idx++)
   {
      if (GetChunkNumber == BookDivision[idx].ChunkNumber)
         ButtonBarButton (BookDivision[idx].DescriptionPtr, "", 0, DoMassage);
      else
         ButtonBarButton (BookDivision[idx].DescriptionPtr, "",
                          BookDivision[idx].ChunkNumber, DoMassage);
   }

   /* close */
   if (CgiFormRefererPtr[0])
      cptr = ButtonBarButton (cptr, CgiFormRefererPtr, -1, false);
   else
      cptr = ButtonBarButton (cptr, NULL, -1, false);

   /* download */
   cptr = ButtonBarButton (cptr, NULL, -1, false);

   /* further buttons (starting with "help") */
   while (*cptr) cptr = ButtonBarButton (cptr, NULL, -1, false);

   TBW += fprintf (stdout, "</div>\n");
}

/*****************************************************************************/
/*
Generate a single "button" inside the context created by ButtonBar().
*/

char* ButtonBarButton
(
char *ButtonLabel,
char *ButtonPath,
int ChunkNumber,
BOOL NavMassage
)
{
   char  *cptr, *mptr, *sptr, *tptr, *uptr;

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

   if (Debug) fprintf (stdout, "ButtonBarButton() |%s|\n", ButtonLabel);

   if (NavMassage)
      mptr = "";
   else
      mptr = "&massage=no";

   if (!*ButtonLabel)
      for (cptr = ButtonLabel = "*ERROR*"; *cptr; cptr++);
   else
      for (cptr = ButtonLabel; *cptr && *cptr != '=' && *cptr != '$'; cptr++);

   if (*ButtonLabel == '^')
   {
      ButtonLabel++;
      tptr = " target=\"_blank\"";
   }
   else
      tptr = "";

   if (ChunkNumber == 0)
   {
      /* no chunk number therefore no link, just a label */
      TBW += fprintf (stdout, "<a class=\"nolink\">%*.*s</a>\n",
                      cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
   }
   else
   if (ChunkNumber > 0)
   {
      /* link to this particular chunk of the book */
      TBW += fprintf (stdout,
"<a href=\"%s%s?%schunk=%d&referer=%s&title=%s%s\">%*.*s</a>\n",
                      CgiScriptNamePtr, UrlEncodedCgiPathInfoPtr,
                      BookSpecifiedByFile, ChunkNumber, UrlEncReferer,
                      UrlEncTitle, mptr,
                      cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
   }
   else
   {
      /* -1 == SPECIAL CASE, create non-chunk button */
      if (*cptr == '=')
         for (sptr = uptr = cptr+1; *sptr && *sptr != '$'; sptr++);
      else
      if (ButtonPath)
         sptr = (uptr = ButtonPath) + strlen(ButtonPath);
      else
         sptr = (uptr = "*ERROR*") + 8;

      if (!memcmp(uptr,"javascript:", 11))
         TBW += fprintf (stdout,
"<script type=\"text/javascript\">\
document.write(\'<a%s href=\"%*.*s\">%*.*s</a>\\n\');\
</script>\n",
                         tptr, sptr-uptr, sptr-uptr, uptr,
                         cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
      else
      if (!memcmp (ButtonLabel, "Download", 8))
      {
         if (NoDownloadBook)
            TBW += fprintf (stdout, "<a class=\"nolink\">%*.*s</a>\n",
                            cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
         else
         if (BookSpecifiedByFile[0])
            TBW += fprintf (stdout, "<a href=\"%s/%s?%sdownload=1\">%*.*s</a>\n",
                            CgiScriptNamePtr,
                            ExpBookFileNamePtr,
                            BookSpecifiedByFile,
                            cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
         else
            TBW += fprintf (stdout, "<a href=\"%s%s?download=1\">%*.*s</a>\n",
                            CgiScriptNamePtr,
                            UrlEncodedCgiPathInfoPtr,
                            cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
      }
      else
         TBW += fprintf (stdout, "<a%s href=\"%*.*s\">%*.*s</a>\n",
                         tptr, sptr-uptr, sptr-uptr, uptr,
                         cptr-ButtonLabel, cptr-ButtonLabel, ButtonLabel);
   }

   while (*cptr && *cptr != '$') cptr++;
   while (*cptr && *cptr == '$') cptr++;

   return (cptr);
}

/*****************************************************************************/
/*
Return a bitmap image to the client as a GIF image.

The code in these functions is implemented in accordance with Compuserve's 
Graphic Interchange Format Programming Reference specification, version 89a, 
31st July 1990. 

The LZW compression employed by the GIF algorithm is implemented using code 
derived from the PBM suite.  Two functions, virtually unmodified, are 
employed, GifCompress() ... formally called 'compgif()', and GifPackBits() ... 
formally called 'pack_bits()'.  The original commentary and copyright notice 
remains as per the code author's request.
*/ 

GifImage
(
int WidthPixels, 
int HeightPixels, 
unsigned char *ImageBytePtr,
int  ByteCount
)
{
   static int  Background = 0,
               BitsPerPixel = 1;
   /* background (index 0) is white, foreground (index 1) is black */
   static unsigned char  Red [] = { 0xff, 0x00 },
                         Green [] = { 0xff, 0x00 },
                         Blue [] = { 0xff, 0x00 };

   int  idx,
        Byte,
        LeftOffset,
        TopOffset,
        Resolution,
        ColorMapSize,
        InitCodeSize;
   char  *bptr;
   char  Buffer [512];

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

   if (Debug) fprintf (stdout, "GifImage()\n");

   CgiLibResponseHeader (200, "image/gif",
                         "Last-Modified: %s\n", LastModifiedGmDateTime);

   /* initialize the following functions */
   GifNextPixel (ImageBytePtr, ByteCount);
   GifPackBits (-1, -1);
   
   ColorMapSize = 1 << BitsPerPixel;
   LeftOffset = TopOffset = 0;
   Resolution = BitsPerPixel;

   /* 
      BookReader Format (BRF) images are always whole bytes.
      Image width specifications (in bits) do not always appear to be so!
      If the width is not an even number of bits (making up a whole byte)
      then make it so by rounding up to the next whole byte number of bits.
      Seems to work OK!
   */
   if (WidthPixels & 0x7) WidthPixels = ((WidthPixels >> 3) + 1) << 3;

   /* the initial code size */
   if( BitsPerPixel <= 1 )
      InitCodeSize = 2;
   else
      InitCodeSize = BitsPerPixel;

   /**************************/
   /* GIF Data Stream header */
   /**************************/

   /* accumulate bytes into buffer before output */
   bptr = Buffer;

   strcpy (bptr, "GIF89a");
   bptr += 6;

   /*****************************/
   /* Logical Screen Descriptor */
   /*****************************/

   /* width and height of logical screen */
   *bptr++ = WidthPixels & 0xff;
   *bptr++ = (WidthPixels >> 8) & 0xff;
   *bptr++ = HeightPixels & 0xff;
   *bptr++ = (HeightPixels >> 8) & 0xff;

   /* indicate that there is a global colour map */
   Byte = 0x80;
   /* OR in the resolution */
   Byte |= (Resolution - 1) << 5;
   /* OR in the Bits per Pixel */
   Byte |= (BitsPerPixel - 1);
   /* write it out */
   *bptr++ = Byte;

   /* Background colour */
   *bptr++ = Background;

   /* pixel aspect ratio */
   *bptr++ = 0;

   /***********************/
   /* Global Colour Table */
   /***********************/

   for (idx = 0; idx < ColorMapSize; idx++)
   {
      *bptr++ = Red[idx];
      *bptr++ = Green[idx];
      *bptr++ = Blue[idx];
   }

   /****************************************/
   /* Graphic Control Extension descriptor */
   /****************************************/

   /* extension introducer and graphic control label */
   *bptr++ = 0x21;
   *bptr++ = 0xf9;
   /* fixed size of the following data block */
   *bptr++ = 0x04;
   /* Transparency Index is provided */
   *bptr++ = 0x01;
   /* no data in these */
   *bptr++ = 0x00;
   *bptr++ = 0x00;
   /* Transparent Color Index value, BACKGROUND SHOULD BE TRANSPARENT */
   *bptr++ = 0x00;
   /* block terminator */
   *bptr++ = 0x00;

   /********************/
   /* Image descriptor */
   /********************/

   /* write an Image separator */
   *bptr++ = 0x2c;

   /* location of image within logical screen */
   *bptr++ = LeftOffset & 0xff;
   *bptr++ = (LeftOffset >> 8) & 0xff;
   *bptr++ = TopOffset & 0xff;
   *bptr++ = (TopOffset >> 8) & 0xff;

   /* width and height of image within logical screen */
   *bptr++ = WidthPixels & 0xff;
   *bptr++ = (WidthPixels >> 8) & 0xff;
   *bptr++ = HeightPixels & 0xff;
   *bptr++ = (HeightPixels >> 8) & 0xff;

   /* no local color table, image is not interlaced, not ordered, etc. */
   *bptr++ = 0x00;

   /**************************/
   /* table-based image data */
   /**************************/

   /* write out the initial code size */
   *bptr++ = InitCodeSize;

   /* transfer what we've accumlated in the local buffer */
   fwrite (Buffer, bptr-Buffer, 1, stdout);

   /* LZW compress the data using PBM-derived algorithm and code */
   GifCompress (InitCodeSize + 1);

   /****************************************/
   /* end of image data and GIF terminator */
   /****************************************/

   /* write out a zero-length packet (to end the series), and terminator */
   fwrite ("\0;", 2, 1, stdout);
}

/*****************************************************************************/
/*
Get a series of pixels from the bitmap.  This function is called by 
GifCompress() to scan the image.  Bit 0 through to bit 7 of the current byte 
are returned as 1 or 0 before the next byte is pointed to.  When the total 
number of bytes have been transmitted return EOF.  This function is 
initialized by a first call, setting the values of the byte pointer and number 
of bytes in the image.
*/ 

GifNextPixel
(
unsigned char *ImageBytePtr,
int ImageByteCount
)
{
   static unsigned int  BitPosition,
                        ByteCount;
   static unsigned char  *BytePtr;

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

   if (ImageBytePtr)
   {
      /* initialize */
      BytePtr = ImageBytePtr;
      ByteCount = ImageByteCount;
      BitPosition = 1;
      return;
   }

   if (!BitPosition)
   {
      /* bits 0 through to 7 have been returned, move to next byte */
      if (!ByteCount) return (EOF);
      ByteCount--;
      BytePtr++;
      BitPosition = 1;
   }

   if (*BytePtr & BitPosition)
   {
      /* next call will return the next most significant bit (if <= 7) */
      BitPosition = (BitPosition << 1) & 0xff;
      /* the tested bit position contained a 1 (foreground) */
      return (1);
   }
   else
   {
      /* next call will return the next most significant bit (if <= 7) */
      BitPosition = (BitPosition << 1) & 0xff;
      /* the tested bit position contained a 0 (background) */
      return (0);
   }
}

/****************************************************************************/
/*
 * This software is copyrighted as noted below.  It may be freely copied,
 * modified, and redistributed, provided that the copyright notice is 
 * preserved on all copies.
 * 
 * There is no warranty or other guarantee of fitness for this software,
 * it is provided solely "as is".  Bug reports or fixes may be sent
 * to the author, who may or may not act on them as he desires.
 *
 * You may not include this software in a program or other software product
 * without supplying the source, or without informing the end-user that the 
 * source is available for no extra charge.
 *
 * If you modify this software, you should include a notice giving the
 * name of the person performing the modification, the date of modification,
 * and the reason for such modification.
 */

/*  compgif.c */
/*
 *
 * GIF Image compression - LZW algorithm implemented with Trie type
 *                         structure.
 *                         Written by Bailey Brown, Jr.
 *                         last change May 24, 1990
 *                         file: compgif.c
 *
 *  You may use or modify this code as you wish, as long as you mention
 *  my name in your documentation.
 *
 *                  - Bailey Brown, Jr.
 *
 */

#define MAXIMUMCODE 4095   /* 2**maximum_code_size */
#define BLOCKSIZE 256   /* max block byte count + 1 */
#define NULLPREFIX -1

typedef struct str_table_entry {
        int code;
        int prefix;
        int suffix;
}  strTableEntry;

typedef struct str_table_node {
        strTableEntry entry;
    struct str_table_node *left;
    struct str_table_node *right;
struct str_table_node *children;
} strTableNode, *strTableNodePtr, **strTable;

/*
 ********************************************************************
 * compgif() recieves pointers to an input function and an output    *
 * stream, and the code size as parameters and outputs successive   *
 * blocks of LZW compressed gif data.  The calling routine should   *
 * have aready written the GIF file header out to the output file.  *
 * It assumes that there will be no more than 8 bits/pixel and that *
 * each data item comes from successive bytes returned by infun.    *
 ********************************************************************
 */

int GifCompress (int code_size)

{
    strTable heap; /* our very own memory manager */
    int heap_index;
    int clear_code, end_code, cur_code;
    int i, found, num_colors, prefix, compress_size;
    int cur_char, end_of_data, bits_per_pix;
    strTableNodePtr cur_node;
    strTable root;  /* root of string table for LZW compression is */
                    /* an array of 2**bits_per_pix pointers to atomic nodes */
    heap_index = 0;
    heap = (strTable)malloc(sizeof(strTableNodePtr)*MAXIMUMCODE);
    if (!heap) printf("can't allocate heap");
    for (i=0; i < MAXIMUMCODE; i++) {
        heap[i] = (strTableNodePtr)malloc(sizeof(strTableNode));
        if (!heap[i])
        {
/*
           printf("can't allocate heap");
*/
           int  status;

           status = vaxc$errno;
           CgiLibResponseError (FI_LI, status, ErrorCalloc);

           /* definitely exit if we can't get memory!!! */
           exit (SS$_NORMAL);
        }
    }
    bits_per_pix = code_size - 1;
    compress_size = code_size;
    num_colors = 1<<(bits_per_pix);
    clear_code = num_colors;
    end_code = clear_code + 1;
    cur_code = end_code + 1;
    prefix = NULLPREFIX;
    root = (strTable)malloc(sizeof(strTableNodePtr)*num_colors);
    if (!root)
    {
/*
       printf("memory allocation failure (root)");
*/
       int  status;

       status = vaxc$errno;
       CgiLibResponseError (FI_LI, status, "malloc()");
       /* definitely exit if we can't get memory!!! */
       exit (SS$_NORMAL);
    }
    for(i=0; i<num_colors; i++) {
        root[i] = heap[heap_index++];
        root[i]->entry.code = i;
        root[i]->entry.prefix = NULLPREFIX;
        root[i]->entry.suffix = i;
        root[i]->left = NULL;
        root[i]->right = NULL;
        root[i]->children = NULL;
    }
    /* initialize  output block */
    GifPackBits(compress_size, -1);
    GifPackBits(compress_size, clear_code);
    end_of_data = 0;
    if ((cur_char = GifNextPixel(NULL, 0)) == EOF)
       printf("premature end of data");
    while (!end_of_data) {
        prefix = cur_char;
        cur_node = root[prefix];
        found = 1;
        if((cur_char = GifNextPixel(NULL, 0)) == EOF) {
            end_of_data = 1; break;
        }
        while(cur_node->children && found) {
            cur_node = cur_node->children;
            while(cur_node->entry.suffix != cur_char) {
                if (cur_char < cur_node->entry.suffix) {
                    if (cur_node->left) cur_node = cur_node->left;
                    else {
                        cur_node->left = heap[heap_index++];
                        cur_node = cur_node->left;
                        found = 0; break;
                    }
                }
                else {
                    if (cur_node->right) cur_node = cur_node->right;
                    else {
                        cur_node->right = heap[heap_index++];
                        cur_node = cur_node->right;
                        found = 0; break;
                    }
                }
            }
            if (found) {
                prefix = cur_node->entry.code;
                if((cur_char = GifNextPixel(NULL, 0)) == EOF) {
                    end_of_data = 1; break;
                }
            }
        }
        if (end_of_data) break;
        if (found) {
            cur_node->children = heap[heap_index++];
            cur_node = cur_node->children;
        }
        cur_node->children = NULL;
        cur_node->left = NULL;
        cur_node->right = NULL;
        cur_node->entry.code = cur_code;
        cur_node->entry.prefix = prefix;
        cur_node->entry.suffix = cur_char;
        GifPackBits(compress_size, prefix);
        if (cur_code > ((1<<(compress_size))-1))
            compress_size++;
        if (cur_code < MAXIMUMCODE) {
            cur_code++;
        }
        else {
            heap_index = num_colors;  /* reinitialize string table */
            for (i=0; i < num_colors; i++ ) root[i]->children = NULL;
            GifPackBits(compress_size, clear_code);
            compress_size = bits_per_pix + 1;
            cur_code = end_code + 1;
        }
    }
    GifPackBits(compress_size, prefix);
    GifPackBits(compress_size, end_code);
    GifPackBits(compress_size, -1);
    for (i=0; i < MAXIMUMCODE; i++) free(heap[i]);
    free(heap);
    free(root);
    return (1);
}

/*
 ************************************************************************
 * GifPackBits() packs the bits of the codes generated by gifenc() into   *
 * a 1..256 byte output block.  The first byte of the block is the      *
 * number 0..255 of data bytes in the block.  To flush or initialize    *
 * the block, pass a negative argument.                                 *
 ************************************************************************
 */

GifPackBits (int compress_size, int prefix)

{
    static int cur_bit = 8;
    static unsigned char block[BLOCKSIZE] = { 0 };
    int i, left_over_bits;

    if (compress_size == -1 && prefix == -1)
    {
       /* initialize */
       cur_bit = 8;
       block[0] = 0;
       return;
    }

    /* if we are about to excede the bounds of block or if the flush
       code (code_bis < 0) we output the block */
    if((cur_bit + compress_size > (BLOCKSIZE-1)*8) || (prefix < 0)) {
        /* handle case of data overlapping blocks */
        if ((left_over_bits = (((cur_bit>>3) +
                ((cur_bit & 7) != 0))<<3) - cur_bit) != 0) {
            for (i=0; i < left_over_bits; i++) {
                if (prefix & (1<<i))
                   block[cur_bit>>3] |= (char)(1<<(cur_bit & 7));
                /* note n>>3 == n/8 and n & 7 == n % 8 */
                cur_bit++;
            }
        }
        compress_size -= left_over_bits;
        prefix = prefix>>left_over_bits;
        block[0] =  (unsigned char)((cur_bit>>3) - 1);

        if (block[0]) fwrite (block, block[0]+1, 1, stdout);

        for(i=0; i < BLOCKSIZE; i++) block[i] = 0;
        cur_bit = 8;
    }
    if (prefix >= 0) {
        for (i=0; i < compress_size; i++) {
           if (prefix & (1<<i))
           block[cur_bit>>3] |= (unsigned char)(1<<(cur_bit & 7));
           /* note n>>3 == n/8 and n & 7 == n % 8 */
           cur_bit++;
        }
    }
    return (1);
}

/*****************************************************************************/
/*
*/
 
InsertComment
(
int Horizontal, 
int Vertical, 
char *Explanation,
int SourceCodeLineNumber
)
{
   int  Length,
        Line,
        Column;
   char  String [256];
   struct OutCharStruct  *ocptr;

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

   if (Debug) fprintf (stdout, "InsertComment()\n");

   Line = Vertical / LINE_POSITIONING_UNITS;
   if (!Line) Line = 1;
   if (Line > LinesOnPage) PageSize (Line);
   if (Line > LastLineOnPage) LastLineOnPage = Line;
   Column = Horizontal / COLUMN_POSITIONING_UNITS;
   if (Column >= MAX_OUTLINE_LENGTH) Column = MAX_OUTLINE_LENGTH-1;
   if (Debug) fprintf (stdout, "Line: %d Column: %d\n", Line, Column);

   Length = sprintf (String, "%s <!-- source code line number: %d -->",
                     Explanation, SourceCodeLineNumber);

   ocptr = OUT_CHAR_AT(Line,Column);
   ocptr->htptr = HtmlString (ocptr->htptr, Length);
   strcat (ocptr->htptr, String);
}

/*****************************************************************************/
/*
For debugging purposes.
*/
 
DumpBytes
(
char *Comment,
char *BytePtr,
int NumberOfBytes
) 
{
   static char  HexDigits [] = "0123456789abcdef";

   int  bcnt, ccnt;
   char  *bptr, *sptr;
   char  String [256];

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

   fprintf (stdout, "\n%s Byte DUMP beginning: %08.08x  Bytes: %d\n",
            Comment, BytePtr, NumberOfBytes);
   while (NumberOfBytes > 0)
   {
      sptr = String;
      bptr = BytePtr;
      sprintf (sptr, "\n%08.08x : ", bptr);
      sptr += 12;
      ccnt = 16;
      bcnt = NumberOfBytes;
      while (ccnt-- && bcnt--)
      {
         *sptr++ = HexDigits[*bptr >> 4];
         *sptr++ = HexDigits[*bptr++ & 0x0f];
         *sptr++ = ' ';
      }
      bptr = BytePtr;
      strcpy (sptr, "\n           ");
      sptr += 12;
      ccnt = 16;
      bcnt = NumberOfBytes;
      while (ccnt-- && bcnt--)
      {
         if (isprint(*bptr))
         {
            *sptr++ = ' ';
            *sptr++ = *bptr++;
            *sptr++ = ' ';
         }
         else
         {
            *sptr++ = ' ';
            *sptr++ = '^';
            *sptr++ = ' ';
            bptr++;
         }
      }
      *sptr++ = '\n';
      *sptr = '\0';
      fputs (String, stdout);
      NumberOfBytes -= 16;
      BytePtr += 16;
   }
   fputs ("\n", stdout);
}

/*****************************************************************************/
/*
For debugging purposes.
*/
 
DumpLongWords
(
char *Comment,
unsigned long *lwptr,
int NumberOfLongWords
) 
{
   int  count = 1;

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

   fprintf (stdout, "\n%s Longword DUMP beginning: %08.08x, Longwords: %d\n",
            Comment, lwptr, NumberOfLongWords);
   while (NumberOfLongWords-- > 0)
   {
      fprintf (stdout,
      "%2d.  %08.08x : 0x%08.08x : %10d : %5d %5d : %3d %3d %3d %3d\n",
      count++, lwptr, *lwptr, *lwptr,
      *lwptr >> 16, *lwptr & 0xffff,
      (*lwptr & 0xff000000) >> 24,
      (*lwptr & 0xff0000) >> 16,
      (*lwptr & 0xff00) >> 8,
      *lwptr & 0xff);
      lwptr++;
   }
   fputs ("\n", stdout);
}

/*****************************************************************************/
/*
If the object has been modified since the specified date and time then return 
a normal status indicating that the data transfer is to continue.  If not 
modified then send a "not modified" HTTP header and return an error status to 
indicate the object should not be sent.
*/ 
 
int ModifiedSince
(
unsigned long *SinceBinaryTimePtr,
unsigned long *BinaryTimePtr
)
{
   static unsigned long  OneSecondDelta [2] = { -10000000, -1 };

   int  status;
   unsigned long  AdjustedBinTime [2],
                  ScratchBinTime [2];

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

   if (Debug) fprintf (stdout, "ModifiedSince()\n");

   /* if request asks for a "reload" (not cached) then give it regardless */
   if (strsame (CgiHttpPragmaPtr, "no-cache", -1)) return (SS$_NORMAL);
   if (strsame (CgiHttpCacheControlPtr, "no-cache", 8)) return (SS$_NORMAL);
   if (strsame (CgiHttpCacheControlPtr, "no-store", 8)) return (SS$_NORMAL);
   if (strsame (CgiHttpCacheControlPtr, "max-age=0", 9)) return (SS$_NORMAL);

   /*
      Add one second to the modified time.  Ensures a negative time
      for VMS where fractional seconds may result in inconclusive
      results when the target time is being specified in whole seconds.
   */ 
   if (VMSnok (status =
       lib$add_times (SinceBinaryTimePtr, &OneSecondDelta, &AdjustedBinTime)))
   {
      CgiLibResponseError (FI_LI, status, "lib$add_times()");
      return (status);
   }
   if (Debug) fprintf (stdout, "sys$add_times() %%X%08.08X\n", status);

   /* if a positive time results the file has been modified */
   if (VMSok (status =
       lib$sub_times (BinaryTimePtr, &AdjustedBinTime, &ScratchBinTime)))
      return (status);

   if (Debug) fprintf (stdout, "sys$sub_times() %%X%08.08X\n", status);
   if (status != LIB$_NEGTIM)
   {
      CgiLibResponseError (FI_LI, status, "sys$sub_times()");
      return (status);
   }

   CgiLibResponseHeader (304, "text/html");
   fprintf (stdout, "Not modified!\n");

   return (LIB$_NEGTIM);
}

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

int HttpGmTimeString
(
char *TimeString,
unsigned long *BinTimePtr
)
{
   static char  *DayNames [] =
      { "", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
   static char  *MonthName [] =
      { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

   static $DESCRIPTOR (HttpTimeFaoDsc, "!AZ, !2ZW !AZ !4ZW !2ZW:!2ZW:!2ZW GMT");
   static $DESCRIPTOR (TimeStringDsc, "");

   int  status;
   unsigned long  BinTime [2],
                  GmTime [2];
   unsigned short  Length;
   unsigned short  NumTime [7];
   unsigned long  DayOfWeek;

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

   if (Debug) fprintf (stdout, "HttpGmTimeString()\n");

   if (!BinTimePtr)
      sys$gettim (&GmTime);
   else
   {
      GmTime[0] = BinTimePtr[0];
      GmTime[1] = BinTimePtr[1];
   }
   if (VMSnok (status = TimeAdjustGMT (true, &GmTime)))
      return (status);

   status = sys$numtim (&NumTime, &GmTime);
   if (Debug)
      fprintf (stdout, "sys$numtim() %%X%08.08X %d %d %d %d %d %d %d\n",
               status, NumTime[0], NumTime[1], NumTime[2],
               NumTime[3], NumTime[4], NumTime[5], NumTime[6]);

   if (VMSnok (status = lib$day_of_week (&GmTime, &DayOfWeek)))
      return (status);
   if (Debug)
      fprintf (stdout, "lib$day_of_week() %%X%08.08X is %d\n",
               status, DayOfWeek);

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

   if (VMSnok (status =
       sys$fao (&HttpTimeFaoDsc, &Length, &TimeStringDsc,
                DayNames[DayOfWeek], NumTime[2], MonthName[NumTime[1]],
                NumTime[0], NumTime[3], NumTime[4], NumTime[5])))
   {
      if (Debug) fprintf (stdout, "sys$fao() %%X%08.08X\n", status);
      TimeString[0] = '\0';
   }
   else
      TimeString[Length] = '\0';
   if (Debug) fprintf (stdout, "|%s|\n", TimeString);

   return (status);
}

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

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

   int  status;
   unsigned short  Length;
   unsigned short  NumTime [7] = { 0,0,0,0,0,0,0 };
   unsigned long  DayOfWeek;
   char  *tptr;

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

   if (Debug) fprintf (stdout, "HttpGmTime() |%s|\n", TimeString);

   tptr = TimeString;
   /* hunt straight for the comma after the weekday name! */
   while (*tptr && *tptr != ',') tptr++;
   if (*tptr) tptr++;
   /* span white space between weekday name and date */
   while (*tptr && isspace(*tptr)) tptr++;
   if (Debug) fprintf (stdout, "tptr |%s|\n", tptr);
   if (!*tptr) return (STS$K_ERROR);

   /* get the date and then skip to month name */
   if (isdigit(*tptr)) NumTime[2] = atoi (tptr);
   while (*tptr && isdigit(*tptr)) tptr++;
   while (*tptr && (*tptr == '-' || isspace(*tptr))) tptr++;
   if (Debug) fprintf (stdout, "tptr |%s|\n", tptr);
   if (!*tptr) return (STS$K_ERROR);

   /* get the month number from the name and skip to the year */
   for (NumTime[1] = 1; NumTime[1] <= 12; NumTime[1]++)
      if (strsame (tptr, MonthName[NumTime[1]], 3)) break;
   if (NumTime[1] > 12) return (STS$K_ERROR);
   while (*tptr && isalpha(*tptr)) tptr++;
   while (*tptr && (*tptr == '-' || isspace(*tptr))) tptr++;
   if (Debug) fprintf (stdout, "tptr |%s|\n", tptr);
   if (!*tptr) return (STS$K_ERROR);

   /* get the year and then skip to the hour */
   if (isdigit(*tptr))
   {
      NumTime[0] = atoi (tptr);
      if (NumTime[0] < 100) NumTime[0] += 1900;
   }
   while (*tptr && isdigit(*tptr)) tptr++;
   while (*tptr && isspace(*tptr)) tptr++;
   if (Debug) fprintf (stdout, "tptr |%s|\n", tptr);
   if (!*tptr) return (STS$K_ERROR);

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

   /* the only thing remaining should be the "GMT" */
   while (*tptr && isspace(*tptr)) tptr++;
   if (Debug) fprintf (stdout, "tptr |%s|\n", tptr);
   if (!strsame (tptr, "GMT", 3)) return (STS$K_ERROR);

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

   if (Debug)
      fprintf (stdout, "NumTime[] %d %d %d %d %d %d %d\n",
               NumTime[0], NumTime[1], NumTime[2], NumTime[3], 
               NumTime[4], NumTime[5], NumTime[6]); 
   status = lib$cvt_vectim (&NumTime, BinTimePtr);
   if (VMSnok (status)) return (status);
   if (Debug) fprintf (stdout, "lib$cvt_vectim() %%X%08.08X\n", status);

   return (TimeAdjustGMT (false, BinTimePtr));
}

/*****************************************************************************/
/*
Determine the offset from GMT (UTC) using either the HTTPD$GMT or
SYS$TIMEZONE_DIFFERENTIAL logicals. If HTTPD$GMT is not defined time should be
set from the timezone differential logical. Function RequestBegin() calls
this every hour to recheck GMT offset and detect daylight saving or other
timezone changes.
*/

int TimeSetGmt ()

{
   static BOOL  UseTimezoneDifferential = false;

   int  status;

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

   if (Debug) fprintf (stdout, "TimeSetGmt()\n");

   if (!UseTimezoneDifferential)
   {
      status = TimeSetHttpdGmt();
      /* return if error and that error was not that the name did not exist */
      if (VMSok (status) || status != SS$_NOLOGNAM) return (status);
   }

   UseTimezoneDifferential = true;
   return (TimeSetTimezone());
}

/*****************************************************************************/
/*
The SYS$TIMEZONE_DIFFERENTIAL logical contains the number of seconds offset
from GMT (UTC) as a positive (ahead) or negative (behind) number.  Set the
'TimeGmtString' global storage to a "+hh:mm" or "-hh:mm" string equivalent, and
the 'TimeAheadOfGmt' global boolean and 'TimeGmtVmsString' delta-time global
string.
*/

int TimeSetTimezone ()

{
   static unsigned short  Length;
   static $DESCRIPTOR (TimezoneLogicalNameDsc, "SYS$TIMEZONE_DIFFERENTIAL");
   static $DESCRIPTOR (LnmSystemDsc, "LNM$SYSTEM");
   static $DESCRIPTOR (TimeGmtStringFaoDsc, "!AZ!2ZL:!2ZL");
   static $DESCRIPTOR (TimeGmtVmsStringFaoDsc, "0 !2ZL:!2ZL");
   static $DESCRIPTOR (TimeGmtStringDsc, TimeGmtString);
   static struct {
      short int  buf_len;
      short int  item;
      void  *buf_addr;
      unsigned short  *ret_len;
   } LnmItems [] =
   {
      { sizeof(TimeGmtString)-1, LNM$_STRING, TimeGmtString, &Length },
      { 0,0,0,0 }
   };

   int  status;
   long  Hours,
         Minutes,
         Seconds;
   char  *SignPtr;
   $DESCRIPTOR (TimeGmtVmsStringDsc, TimeGmtVmsString);

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

   if (Debug) fprintf (stdout, "TimeSetTimezone()\n");

   status = sys$trnlnm (0, &LnmSystemDsc, &TimezoneLogicalNameDsc, 0, &LnmItems);
   if (Debug) fprintf (stdout, "sys$trnlnm() %%X%08.08X\n", status);
   if (VMSnok (status)) return (status);

   TimeGmtString[Length] = '\0';
   if (Debug) fprintf (stdout, "TimeGmtString |%s|\n", TimeGmtString);
   Seconds = atol(TimeGmtString);
   if (Seconds < 0)
   {
      TimeAheadOfGmt = false;
      Seconds = -Seconds;
      SignPtr = "-";
   }
   else
   {
      TimeAheadOfGmt = true;
      SignPtr = "+";
   }
   Hours = Seconds / 3600;
   Minutes = (Seconds - Hours * 3600) / 60;
   if (Debug)
      fprintf (stdout, "%d %s%d:%d\n", Seconds, SignPtr, Hours, Minutes);
   sys$fao (&TimeGmtStringFaoDsc, &Length, &TimeGmtStringDsc,
            SignPtr, Hours, Minutes);
   TimeGmtString[Length] = '\0';
   if (Debug) fprintf (stdout, "TimeGmtString |%s|\n", TimeGmtString);

   sys$fao (&TimeGmtVmsStringFaoDsc, &Length, &TimeGmtVmsStringDsc,
            Hours, Minutes);
   TimeGmtVmsString[Length] = '\0';
   if (Debug) fprintf (stdout, "TimeGmtVmsString |%s|\n", TimeGmtVmsString);

   TimeGmtVmsStringDsc.dsc$w_length = Length;

   if (VMSnok (status =
       sys$bintim (&TimeGmtVmsStringDsc, &TimeGmtDeltaBinary)))
      return (status);
   if (TimeGmtDeltaBinary[0] || TimeGmtDeltaBinary[1])
      return (status);
   /* time must have been zero, make it one, one-hundreth of a second */
   TimeGmtDeltaBinary[0] = -100000;
   TimeGmtDeltaBinary[1] = -1;
   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
Translate the logical HTTPD$GMT (defined to be something like "+10:30" or "-
01:15") and convert it into a delta time structure and store in 
'TimeGmtDeltaBinary'.  Store whether it is in advance or behind GMT in boolean 
'TimeAheadOfGmt'.  Store the logical string in 'TimeGmtString'.
*/

int TimeSetHttpdGmt ()

{
   static unsigned short  Length;
   static $DESCRIPTOR (GmtLogicalNameDsc, "HTTPD$GMT");
   static $DESCRIPTOR (LnmFileDevDsc, "LNM$FILE_DEV");
   static struct {
      short int  buf_len;
      short int  item;
      void  *buf_addr;
      unsigned short  *ret_len;
   } LnmItems [] =
   {
      { sizeof(TimeGmtString)-1, LNM$_STRING, TimeGmtString, &Length },
      { 0,0,0,0 }
   };

   int  status;
   char  *cptr, *sptr;
   $DESCRIPTOR (TimeGmtVmsStringDsc, TimeGmtVmsString);

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

   if (Debug) fprintf (stdout, "TimeSetHttpdGmt()\n");

   status = sys$trnlnm (0, &LnmFileDevDsc, &GmtLogicalNameDsc, 0, &LnmItems);
   if (Debug) fprintf (stdout, "sys$trnlnm() %%X%08.08X\n", status);
   if (VMSnok (status)) return (status);

   TimeGmtString[Length] = '\0';
   if (Debug) fprintf (stdout, "TimeGmtString |%s|\n", TimeGmtString);

   if (TimeGmtString[0] == '$') return (SS$_NORMAL);

   if (*(cptr = TimeGmtString) == '-')
      TimeAheadOfGmt = false;
   else
      TimeAheadOfGmt = true;
   if (*cptr == '+' || *cptr == '-') cptr++;
   sptr = TimeGmtVmsString;
   *sptr++ = '0';
   *sptr++ = ' ';
   while (*cptr) *sptr++ = *cptr++;
   *sptr = '\0';
   if (Debug) fprintf (stdout, "TimeGmtVmsString |%s|\n", TimeGmtVmsString);

   TimeGmtVmsStringDsc.dsc$w_length = sptr - TimeGmtVmsString;

   if (VMSnok (status =
       sys$bintim (&TimeGmtVmsStringDsc, &TimeGmtDeltaBinary)))
      return (status);
   if (TimeGmtDeltaBinary[0] || TimeGmtDeltaBinary[1])
      return (status);
   /* time must have been zero, make it one, one-hundreth of a second */
   TimeGmtDeltaBinary[0] = -100000;
   TimeGmtDeltaBinary[1] = -1;
   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
The GMT is generated by calculating using an offset using 
'TimeGmtDeltaOffset' and boolean 'TimeAheadOfGmt'.  Adjust either to or from 
GMT.
*/ 

int TimeAdjustGMT
(
BOOL ToGmTime,
unsigned long *BinTimePtr
)
{
   int  status;
   unsigned long  AdjustedTime [2];

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

   if (Debug) fprintf (stdout, "TimeAdjustGMT() ToGmTime: %d\n", ToGmTime);

   if ((ToGmTime && TimeAheadOfGmt) || (!ToGmTime && !TimeAheadOfGmt))
   {
      /* to GMT from local and ahead of GMT, or to local from GMT and behind */
      status = lib$sub_times (BinTimePtr, &TimeGmtDeltaBinary, &AdjustedTime);
      if (Debug) fprintf (stdout, "lib$sub_times() %%X%08.08X\n", status);
   }
   else
   {
      /* to GMT from local and behind GMT, or to local from GMT and ahead */
      status = lib$add_times (BinTimePtr, &TimeGmtDeltaBinary, &AdjustedTime);
      if (Debug) fprintf (stdout, "lib$add_times() %%X%08.08X\n", status);
   }

   if (Debug)
   {
      unsigned short  Length;
      char  String [64];
      $DESCRIPTOR (AdjustedTimeFaoDsc, "AdjustedTime: |!%D|\n");
      $DESCRIPTOR (StringDsc, String);

      sys$fao (&AdjustedTimeFaoDsc, &Length, &StringDsc, &AdjustedTime);
      String[Length] = '\0';
      fputs (String, stdout);
   }

   BinTimePtr[0] = AdjustedTime[0];
   BinTimePtr[1] = AdjustedTime[1];

   return (status);
}

/****************************************************************************/
/*
Return an integer reflecting the major and minor version of VMS (e.g. 60, 61,
62, 70, 71, 72, etc.)
*/ 

#ifdef ODS_EXTENDED

int GetVmsVersion ()

{
   static char  SyiVersion [16];

   static struct {
      short int  buf_len;
      short int  item;
      void  *buf_addr;
      unsigned short  *ret_len;
   }
   SyiItems [] =
   {
      { 8, SYI$_VERSION, &SyiVersion, 0 },
      { 0,0,0,0 }
   };

   int  status,
        version;

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

   if (Debug) fprintf (stdout, "GetVmsVersion()\n");

   if (VMSnok (status = sys$getsyiw (0, 0, 0, &SyiItems, 0, 0, 0)))
      exit (status);
   SyiVersion[8] = '\0';
   version = ((SyiVersion[1]-48) * 10) + (SyiVersion[3]-48);
   if (Debug) fprintf (stdout, "|%s| %d\n", SyiVersion, version);
   return (version);
}

#endif /* ODS_EXTENDED */

/*****************************************************************************/
/*
Return non-NULL pointer if looks a bit suspect!
*/

char* XSSuspect (char *cptr)

{
   static char  XSSredacted [] = "XSS redacted! (tsk-tsk)";

   char  *sptr, *zptr;
   char buf [1024];

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

   if (Debug) fprintf (stdout, "XSSuspect() |%s|\n", cptr);

   if (!cptr || !*cptr) return (NULL);
   if (strstr (cptr, "</")) return (XSSredacted);
   zptr = (sptr = buf) + sizeof(buf)-1;
   while (*cptr && *cptr && sptr < zptr) *sptr++ = tolower(*cptr++);
   *sptr = '\0';
   if (sptr >= zptr || strstr (buf, "<script>")) return (XSSredacted);
   return (NULL);
}

/****************************************************************************/
/*
Does a case-insensitive, character-by-character string compare and returns 
true if two strings are the same, or false if not.  If a maximum number of 
characters are specified only those will be compared, if the entire strings 
should be compared then specify the number of characters as 0.
*/ 
 
BOOL strsame
(
char *sptr1,
char *sptr2,
int  count
)
{
   while (*sptr1 && *sptr2)
   {
      if (toupper (*sptr1++) != toupper (*sptr2++)) return (false);
      if (count)
         if (!--count) return (true);
   }
   if (*sptr1 || *sptr2)
      return (false);
   else
      return (true);
}
 
/*****************************************************************************/