File: cdromlib.c

package info (click to toggle)
ncbi-tools6 6.1.20170106%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 468,492 kB
  • sloc: ansic: 1,474,204; pascal: 6,740; cpp: 6,248; xml: 3,390; sh: 2,139; perl: 1,084; csh: 508; makefile: 437; javascript: 198; ruby: 93; lisp: 81
file content (2531 lines) | stat: -rw-r--r-- 66,613 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
/* 
 * ===========================================================================
 *
 *                             COPYRIGHT NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a Government employee and thus cannot
 *  be copyrighted.  This software/database is freely available to the
 *  public for use without a copyright notice.  Restrictions cannot be
 *  placed on its present or future use.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the National Library of
 *  Medicine (NLM) and the U. S. Government do not and cannot warrant the
 *  performance or results that may be obtained by using this software or
 *  data.  The NLM and the U. S. Government disclaim all warranties as to
 *  performance, merchantability or fitness for any particular purpose.
 *
 *  Please see that the author is suitably cited in any work or product
 *  based on this material.
 *
 * ===========================================================================
 *
 * RCS $Id: cdromlib.c,v 6.1 2004/04/01 13:43:05 lavr Exp $
 *
 * Authors: Greg Schuler, Jim Ostell, Jonathan Kans, Jonathan Epstein
 * 
 * Original Creation Date:   9-91
 *
 * File Description: 
 *  Functions in this file are the I/O primitives needed to retrieve data
 *  from the Entrez CD-ROMs, releases 12.0 and earlier.  For releases 13.0
 *  and later, use the functions in cdnewlib.c
 *
 *
 * Modifications:
 * --------------------------------------------------------------------------
 * Date     Name        Description of modification
 * -------  ----------  -----------------------------------------------------
 * 06-29-94 Schuler     AsnRead/AsnWrite functions moved to objentr.c
 * 07-11-94 Schuler     Removed #include <cdrom.h> (no longer needed!)
 * 07-13-94 Schuler     Moved CdTermFree to cdentrez.c
 * 08-04-94 Kans        Fixed bug resulting in reading too many term pages
 * 11-16-94 Schuler     Typecasts for picky compilers
 *
 * 05-19-95 Schuler     Added rcs Log directive for automatic insertion of
 *                      modification comments.
 *
 * Revision $Log: cdromlib.c,v $
 * Revision Revision 6.1  2004/04/01 13:43:05  lavr
 * Revision Spell "occurred", "occurrence", and "occurring"
 * Revision
 * Revision Revision 6.0  1997/08/25 18:13:10  madden
 * Revision Revision changed to 6.0
 * Revision
 * Revision Revision 5.0  1996/05/28 13:55:34  ostell
 * Revision Set to revision 5.0
 * Revision
 * Revision 4.0  1995/07/26  13:50:32  ostell
 * force revision to 4.0
 *
 * Revision 2.48  1995/05/16  14:36:20  schuler
 * Automatic comment insertion enabled
 *
 *
 * ==========================================================================
 */

#define REVISION_STR "$Revision: 6.1 $"

#include <cdromlib.h>

#ifdef _OLD_CdEntrez_

static char * _this_module = "CdEntrez";
#undef  THIS_MODULE
#define THIS_MODULE _this_module
static char * _this_file = __FILE__;
#undef  THIS_FILE
#define THIS_FILE _this_file

/*  =========================================================================
 *      CONSTANTS & MACROS
 */

#define CURRENT_FORMAT_VERSION 0

#define BLKSIZE   ((size_t)vi->field_bucket_size)  /* sizeof term index block on cdrom */

#define PREF_ML     TYP_ML
#define PREF_AA     TYP_AA
#define PREF_NT     TYP_NT
#define PREF_MED    (NTYPE+0)
#define PREF_SEQ    (NTYPE+1)

#define SUF_ML      TYP_ML
#define SUF_AA      TYP_AA
#define SUF_NT      TYP_NT
#define SUF_WORD    (NTYPE+FLD_WORD)
#define SUF_MESH    (NTYPE+FLD_MESH)
#define SUF_KYWD    (NTYPE+FLD_KYWD)
#define SUF_AUTH    (NTYPE+FLD_AUTH)
#define SUF_JOUR    (NTYPE+FLD_JOUR)
#define SUF_ORGN    (NTYPE+FLD_ORGN)
#define SUF_ACCN    (NTYPE+FLD_ACCN)
#define SUF_GENE    (NTYPE+FLD_GENE)
#define SUF_PROT    (NTYPE+FLD_PROT)
#define SUF_ECNO    (NTYPE+FLD_ECNO)
#define SUF_HIER    (NTYPE+FLD_ORGN_HIER)
#define SUF_DATE    (NTYPE+FLD_DATE)
#define SUF_ASN     (NTYPE+NFLD)
#define SUF_REC     (NTYPE+NFLD+1)
#define SUF_UID     (NTYPE+NFLD+2)

#define EXT_DAT     0
#define EXT_IDX     1
#define EXT_LST     2
#define EXT_PST     3
#define EXT_LNK     4

#define COMPR_NONE	0
#define COMPR_HUFFMAN	1
#define COMPR_LZW1	2
/* etc...*/
#define COMPR_DONT_KNOW	0xFF


#define HUFFMAN_SENTINEL 256


typedef struct {
    DocUid  uid;                  /* MEDLINE UI or seq-id of Bioseq       */
    DocType type;                  /* document type code (ml/nt/aa)        */
    Int4  entry_offset ,        /* offset into entry file(s)            */
        sum_offset ,            /* offset into summary file    */
        link_offset ;            /* offset into link file       */
} UidIdx,  PNTR UidIdxPtr;

typedef struct decompinfo {
    AsnIoPtr aip;
    FILE *fp;
    Uint1 compr;	/* compression protocol */
    unsigned int mask;		/* used internally for Huffman */
    unsigned int byte;		/* used internally for Huffman */
    Uint4 bytes_left;   /* count of remaining bytes for uncompressed protocol */
    struct decompinfo PNTR next;
} DecompInfo, PNTR DecompInfoPtr;
    

/*  =========================================================================
 *      VARIABLES
 */

static DecompInfoPtr DecompInfoList = NULL;
static Int4 numinits;
static CharPtr buffer;

static CharPtr sPath [NDIR];

static char  *sSdir[] = { "", "data", "sequence", "medline", "terms",
                          "index", "links", "" };
static char  *sPref[] = { "ml", "aa", "nt", "med", "seq" };
static char  *sSuff[] = {  "word", "mesh", "kywd", "auth", "jour", "orgn",
                           "accn", "gene", "prot", "ecno", "hier", "date",
                           "fkey", "prop", "subs", "mloc",
                           "ml", "aa", "nt",
                           "asn", "rec", "uid" };
static char  *sExtn[] = { "dat", "idx", "lst", "pst", "lnk" };

static Boolean bAppendVer = FALSE;
static Boolean upperCaseIt = FALSE;

static EntrezInfoPtr vi = NULL;
static Int4Ptr type_bucket_index[NTYPE];  /* from the .idx files */

static size_t detInfoCharCount;
static CharPtr CdDetailedBuf = NULL;
static Boolean countOnly;

                    /* for saving the last term.idx file used */

static DocType term_idx_type = -1;   
static DocField term_idx_field = -1;
static Int2 term_idx_count = 0;
static CharPtr PNTR term_idx_str;
static FILE *IdxFilePtr[NTYPE+2];
static Boolean HoldIdxOpen = FALSE;


#ifdef IS_BIG_ENDIAN
/* no swapping needed:  define do-nothing macros */
#define SwapInt2(X)  (X)
#define SwapInt4(X)  (X)
#else
/* give prototypes for byte swapping functions */
static Int2 NEAR  SwapInt2  PROTO((Int2));
static Int4 NEAR  SwapInt4  PROTO((Int4));
#endif


/*****************************************************************************
*
*   Private Function Prototypes
*
*****************************************************************************/
static Boolean NEAR CdInitialize PROTO((CharPtr,CharPtr,CharPtr,Int2Ptr));
static Boolean NEAR CdSetPath    PROTO((Int2,CharPtr));
static Boolean NEAR SaveCdMediaContext PROTO((CharPtr media_name));
static void    NEAR ExtraInitWork PROTO((void));
static Boolean NEAR ValidateType PROTO((DocType type));
static Boolean NEAR ValidateField PROTO((DocType type, DocField field));
static Boolean NEAR ValidateUid PROTO((DocType type, DocUid uid));
static CharPtr NEAR MakePath PROTO((Int2 nSdir,Int2 nPref,Int2 nSuf, Int2 nExtn));
static Boolean NEAR LoadUidIndex PROTO((DocType type));
static Int2    NEAR LoadTrmIndex PROTO((DocType type, DocField field));
static void    NEAR FreeTrmIndex PROTO((void));
/**** not used in reading cdrom ******
static Int4 NEAR MergeSegOffset PROTO((Int2 seg, Int4 offset));
*************************************/
static Boolean NEAR SplitSegOffset PROTO((Int4 value, Int2Ptr segptr, Int4Ptr offsetptr));
static FILE * NEAR CdDocFil PROTO((DocType type, DocUid uid, UidIdxPtr idx));

static Boolean SwapOutCd PROTO((VoidPtr med));
static Boolean SwapInCd PROTO((VoidPtr med));
static void NEAR ForceCdFini PROTO((void));
static Boolean CdInitMedia PROTO((VoidPtr med));
static Boolean CdFmtInfo PROTO((VoidPtr medName));

static CdTermPtr NEAR CdTrmLocate PROTO((CharPtr term, Int2 page));
static UidIdxPtr NEAR UidIdxGet PROTO((DocType type, DocUid uid, UidIdxPtr idx));
static void NEAR linksort PROTO((Int4Ptr uids, Int4Ptr wts, Int4 n));
static DecompInfoPtr NEAR DecompInit PROTO((FILE *fp));
static Boolean NEAR DecompFini PROTO((AsnIoPtr aip, DecompInfoPtr dip));
static void NEAR DecompInfoFree PROTO((DecompInfoPtr dcp));
static Int2 LIBCALLBACK DecompReadFunc PROTO((Pointer p, CharPtr buff, Uint2 count));
static Int2 HuffmanRead PROTO((DecompInfoPtr dcp, CharPtr buff, Uint2 count));
static Boolean NEAR IsOKMagic PROTO((Uint4 magic, CharPtr volume_label));
static CdTermPtr  CdTermRead PROTO((Int2 type, Int2 field, CharPtr ptr, CharPtr bufr, Int2 page));

/*****************************************************************************
*
*   General purpose public functions
*
*****************************************************************************/

/*****************************************************************************
*
*   CdInit()
*
*****************************************************************************/
static CharPtr trmbuf;   /* for term pages */
static DocType trmtype;  /* type of last term used in trmbuf */
static DocField trmfield;  /* field of last term used in trmbuf */
static Int2 trmpage,       /* page # of first page in trmbuf */
			trmpages; 			/* number of pages in memory */
static size_t trmpagesrequest;     /* how bytes to read (5 * BLKSIZE) */

static Boolean oldStyleCfgFile;


static Int2 nCdVer;
static char *sCdError [] = {
    "",
    "Memory allocation error",
    "File create error",
#ifdef WIN_MSWIN
	"File open error on %Fs",
#else
    "File open error on %s",
#endif
    "File seek error",
    "File read error",
    "File write error",
    "Bad database type code [%d]",
    "Bad field code [%d]",
    "No terms for type/field [%d/%d]",
    "Bad uid number [%ld]",
    "Bad directory number [%d]",
    "Cannot read new data format",
    "Index files out of date",
    "Data decompression error",
    "Programmer error"
};

static CdTermPtr cdtrmcache [10]; /* cache of most recent CdTrmFind results */

/*****************************************************************************
*
*   CdInit()
*     uses environment variables to configure initialization
*
*****************************************************************************/

Boolean  CdInit (void)

{
    char media[64];

	ConfigInit();

    if (nCdVer) {
        numinits++;
        return TRUE;   /* already setup */
    }

	oldStyleCfgFile = FALSE;

    GetAppParam ("ncbi", "NCBI", "MEDIA", "", media, sizeof media);

	/* This is a work-around to provide backwards compatibility for old       */
	/* config files which do not specify MEDIA                                */
	if (media[0] == '\0')
	{
		StrCpy(media, "NCBI");
		SetSoleMedia();
		oldStyleCfgFile = TRUE;
	}

    return (ParseMedia(CdInitMedia, MEDIUM_CD | MEDIUM_DISK) != 0);
}


static Boolean CdInitMedia(VoidPtr med)

{
    char CdRootPath[PATH_MAX];
    char sVol[32];
	char datvalpath[PATH_MAX];
	CharPtr mediaName = (CharPtr) med;

    GetAppParam ("ncbi", mediaName, "ROOT", "", CdRootPath, sizeof CdRootPath);

    vi = NULL;

    bAppendVer = FALSE;
    upperCaseIt = FALSE;

    /* "VAL" overrides "ROOT" for purposes of finding first copy of .val */
    if (GetAppParam ("ncbi", mediaName, "VAL", CdRootPath, datvalpath, sizeof datvalpath))
        CdSetPath (DIR_VAL, CdRootPath);

    FileBuildPath(datvalpath, NULL, NULL);
    if (! CdInitialize (CdRootPath, sVol, datvalpath, &nCdVer)) {
        return  FALSE;
    }

    trmpagesrequest =(size_t)(5 * BLKSIZE);   /* number of termpages to request */

    if (GetAppParam ("ncbi", mediaName, "IDX", "", CdRootPath, sizeof CdRootPath))
        CdSetPath (DIR_IDX, CdRootPath);

    /* work-around to find alternate index files when using old-style    */
    /* configuration file                                                */
    if (oldStyleCfgFile)
    {
        if (StrICmp(sVol, "SeqData") == 0 &&
            GetAppParam ("ncbi", mediaName, "SEQIDX", "", CdRootPath,
            sizeof CdRootPath))
        {
            CdSetPath (DIR_IDX, CdRootPath);
        }
        if (StrICmp(sVol, "MedData") == 0 &&
            GetAppParam ("ncbi", mediaName, "MEDIDX", "", CdRootPath,
            sizeof CdRootPath))
        {
            CdSetPath (DIR_IDX, CdRootPath);
        }
    }

    if (GetAppParam ("ncbi", mediaName, "LNK", "", CdRootPath, sizeof CdRootPath))
        CdSetPath (DIR_LNK, CdRootPath);
    if (GetAppParam ("ncbi", mediaName, "MED", "", CdRootPath, sizeof CdRootPath))
        CdSetPath (DIR_MED, CdRootPath);
    if (GetAppParam ("ncbi", mediaName, "SEQ", "", CdRootPath, sizeof CdRootPath))
        CdSetPath (DIR_SEQ, CdRootPath);
    if (GetAppParam ("ncbi", mediaName, "TRM", "", CdRootPath, sizeof CdRootPath))
        CdSetPath (DIR_TRM, CdRootPath);
    SaveCdMediaContext(mediaName);

    return TRUE;
}


static Boolean NEAR SaveCdMediaContext(CharPtr media_name)

{
	MediaPtr media;
	CdMediaInfoPtr cdm;
	int i;
	char ejectable[10];
	char buffer[100];

	media = PreInitMedia(media_name);

	if (media == NULL)
		return FALSE;

	if (media->inited_partial || (media->media_type != MEDIUM_CD &&
		media->media_type != MEDIUM_DISK))
		return TRUE;

	media->swapOutMedia = SwapOutCd;
	media->swapInMedia = SwapInCd;
    GetAppParam ("ncbi", media_name, "EJECTABLE", "0", ejectable, sizeof ejectable);

	cdm = (CdMediaInfoPtr) MemNew(sizeof(CdMediaInfo));
	cdm->ejectable = atoi(ejectable);
	cdm->device_name = NULL;
	cdm->raw_device_name = NULL;
	cdm->mount_point = NULL;
	cdm->mount_cmd = NULL;

    if (GetAppParam ("ncbi", media_name, "DEVICE_NAME", "", buffer, sizeof buffer))
	{
    	cdm->device_name = StringSave(buffer);
	}
    if (GetAppParam ("ncbi", media_name, "RAW_DEVICE_NAME", "", buffer, sizeof buffer))
	{
    	cdm->raw_device_name = StringSave(buffer);
	}
    if (GetAppParam ("ncbi", media_name, "MOUNT_POINT", "", buffer, sizeof buffer))
	{
    	cdm->mount_point = StringSave(buffer);
	}
    if (GetAppParam ("ncbi", media_name, "MOUNT_CMD", "", buffer, sizeof buffer))
	{
    	cdm->mount_cmd = StringSave(buffer);
	}
	cdm->hold_idx_open = FALSE;
    if (GetAppParam ("ncbi", media_name, "HOLD_IDX_OPEN", "", buffer, sizeof buffer))
	{
    	cdm->hold_idx_open = StringICmp(buffer, "TRUE") == 0;
	}

	media->media_info = (VoidPtr) cdm;

	for (i = 0; i < NDIR; i++)
	{
		cdm->sPath[i] = sPath[i];
		sPath[i] = NULL;
	}

	media->entrez_info = vi;
	cdm->bAppendVer = bAppendVer;
	cdm->upperCaseIt = upperCaseIt;

	media->inited_partial = TRUE;
	
	return TRUE;
}


static Boolean SwapOutCd(VoidPtr curm)
{
	int i;
	MediaPtr CurMedia = (MediaPtr) curm;
	CdMediaInfoPtr cmip;

	if (CurMedia != NULL)
	{
		cmip = (CdMediaInfoPtr) CurMedia->media_info;
		CurMedia->entrez_info = vi;
		vi = NULL; /* avoid freeing it */

		for (i = 0; i < NDIR; i++)
		{ /* copy and avoid freeing */
			cmip->sPath[i] = sPath[i];
			sPath[i] = NULL;
		}

		ForceCdFini();
	}

	return TRUE;
}


static Boolean SwapInCd(VoidPtr med)
{
	MediaPtr newMedia = (MediaPtr) med;
	int i;
	CdMediaInfoPtr cmip;

	cmip = (CdMediaInfoPtr) newMedia->media_info;

	for (i = 0; i < NDIR; i++)
	{ /* load up sPath */
		sPath[i] = cmip->sPath[i];
	}
	vi = newMedia->entrez_info;

	bAppendVer = cmip->bAppendVer;
	upperCaseIt = cmip->upperCaseIt;
    HoldIdxOpen = cmip->hold_idx_open;

	ExtraInitWork();

	return TRUE;
}


static void NEAR ExtraInitWork()

{
    size_t bufsize;
	int i;

    /* initialize cached CdTermPtr array */
    for (i = 0; i < 10; i++) {
      cdtrmcache [i] = NULL;
    }

    term_idx_type = -1;
    term_idx_field = -1;

    if (buffer == NULL) {
        bufsize = (size_t) MAX (MAX ((size_t) vi->type_bucket_size, (size_t) vi->field_bucket_size), sizeof (Int4) * 512);
        buffer = (CharPtr) MemNew(bufsize);
    }
}


static void NEAR ForceCdFini(void)

{
	Int4 sav_numinits = numinits;
	CharPtr savDetailedBuf;

	ConfigInit(); /* simulate Init() to balance Fini() */
    savDetailedBuf = CdDetailedBuf;
	CdDetailedBuf = NULL; /* avoid freeing in Fini() */
	numinits = 1;
	CdFini();
	numinits = sav_numinits;
    CdDetailedBuf = savDetailedBuf;
}


/*****************************************************************************
*
*   CdFini()
*      closes cdromlib session
*   
*****************************************************************************/
Boolean  CdFini (void)

{
	Int2 i;
	CdTermPtr trmptr;

	ConfigFini();
	numinits--;
	if (numinits)          /* haven't fixed all initializations yet */
		return TRUE;

    /* free cached CdTermPtr array */
    for (i = 0; i < 10; i++) {
      trmptr = cdtrmcache [i];
      if (trmptr != NULL) {
        if (trmptr->term != NULL) {
          MemFree (trmptr->term);
        }
        MemFree (trmptr);
      }
      cdtrmcache [i] = NULL;
    }

    buffer = (CharPtr) MemFree(buffer);
	FreeTrmIndex();
	for (i = 0; i < NDIR; i++)
		sPath[i] = (CharPtr) MemFree(sPath[i]);
	for (i = 0; i < NTYPE; i++)
		if (i != TYP_NT)
			type_bucket_index[i] = (Int4Ptr) MemFree(type_bucket_index[i]);
		else
			type_bucket_index[i] = NULL;  /* NT and AA use same index */
	vi = EntrezInfoFree(vi);
	trmbuf = (CharPtr) MemFree(trmbuf);
	trmpages = 0;
	nCdVer = 0;
	bAppendVer = FALSE;
	upperCaseIt = FALSE;

    for (i = 0; i < NTYPE+2; i++)
	{
        if (IdxFilePtr[i] != NULL)
		{
			FileClose(IdxFilePtr[i]);
			IdxFilePtr[i] = NULL;
		}
	}

    CdDetailedBuf = (CharPtr) MemFree(CdDetailedBuf);

	return TRUE;
}


/*  =========================================================================
 *      PUBLIC FUNCTION BODIES
 */


/*  -------------------- CdInitialize() --------------------------------
 *  CdInitialize -- Initializes the library
 *
 *  Parameters:    sCdRoot:   CD-ROM root path
 *                 sVolume:   pointer to volume name buffer (VOLUME_MAX)
 *                 ver:       pointer to version number buffer
 *
 *  Return value:  TRUE:      Success.
 *                 FALSE:     Failure;  refer to error code.
 *
 *  Notes:  1. The file cdromdat.val must be in the specified root path.
 *          2. Default paths strings for various subdirectories are 
 *             created by this function below the specified root path.
 *             Use CdSetPath() to override the defaults.
 */

static Boolean NEAR CdInitialize (CharPtr sCdRoot, CharPtr sVolume, CharPtr datvalpath, Int2Ptr ver)

{
    Int2   i;
    AsnIoPtr aip;
    Char drctry [16];
    CharPtr p;
    size_t bufsize;


    *sVolume = '\0';
    *ver = 0;

	numinits++;     /* count the number of initialization calls */

    if (vi != NULL) {          /* already initialized ! */
        StringCpy (sVolume, vi->volume_label);
        *ver = vi->version;
        return  TRUE;
    }

    /* initialize cached CdTermPtr array */
    for (i = 0; i < 10; i++) {
      cdtrmcache [i] = NULL;
    }

    term_idx_type = -1;
    term_idx_field = -1;
    
    for (i = 0; i < NTYPE+2; i++)
        IdxFilePtr[i] = NULL;

    /* initialize storage for path names */
    for (i = 0; i < NDIR; i++)
        if (sPath[i] == NULL)
            sPath[i] = (CharPtr) MemNew(PATH_MAX + 1);

    /* initialize root path string variable */
    StringNCpy (sPath[DIR_ROOT], sCdRoot, PATH_MAX);
    FileBuildPath(sPath[DIR_ROOT], NULL, NULL);

    /* read the CDROMLIB.INF file */
    if ((aip = EntrezInfoOpen (datvalpath)) == NULL)
        return FALSE;

    /* set default paths for subdirectories */
    for (i=1; i<NDIR; i++) {
        StringCpy (sPath[i], sPath[DIR_ROOT]);
        StringCpy (drctry, sSdir[i]);
        if (upperCaseIt) {
          p = drctry;
          while (*p != '\0') {
            *p = TO_UPPER (*p);
            p++;
          }
        }
        FileBuildPath(sPath[i], drctry, NULL);
    }

    vi = EntrezInfoAsnRead(aip, NULL);
    AsnIoClose(aip);
    if (vi == NULL)
        return FALSE;

    /* check for incompatible format */
    if (vi->format != CURRENT_FORMAT_VERSION) {
        ErrPostEx(SEV_ERROR, ERR_CD_BADFORMAT, 0, sCdError[ERR_CD_BADFORMAT]);
        return FALSE;
    }  

    if (buffer == NULL) {
        bufsize = (size_t) MAX (MAX ((size_t) vi->type_bucket_size, (size_t) vi->field_bucket_size), sizeof (Int4) * 512);
        buffer = (CharPtr) MemNew(bufsize);
    }

    StringCpy (sVolume, vi->volume_label);
    *ver = vi->version;
    return  TRUE;
}

/*****************************************************************************
*
*   CdGetInfo()
*   	Gets Entrez info pointer
*
*****************************************************************************/
EntrezInfoPtr CdGetInfo (void)

{
	return vi;
}


/*****************************************************************************
*
*   CdFmtInfo()
*   	Formats CD-ROM specific "detailed info" and either stores the number
*   	of characters required to format the text, or concatentates the
*   	formatted string to a global string
*
*****************************************************************************/
static Boolean CdFmtInfo(VoidPtr medName)
{
  char buf[256];
  MediaPtr media;
  CharPtr mediaName = (CharPtr) medName;
  CdMediaInfoPtr cdm;


  if ((media = PreInitMedia(mediaName)) == NULL || media->invalid ||
	  (cdm = (CdMediaInfoPtr) media->media_info) == NULL)
  {
    return FALSE;
  }

  if (media->media_type == MEDIUM_CD)
    StrCpy(buf, "\n  CD-ROM image from ");
  else
    StrCpy(buf, "\n  Hard disk image from ");
  if (cdm->sPath[DIR_ROOT] == NULL)
  {
	StrCat(buf, "<location unknown>");
  } else {
    StrCat(buf, cdm->sPath[DIR_ROOT]);
  }
  if (media->entrez_info != NULL && media->entrez_info->volume_label != NULL)
  {
    StrCat(buf, "\n    Volume label is ");
    StrCat(buf, media->entrez_info->volume_label);
  }
  if (media->formal_name == NULL)
  {
    StrCat(buf, "\n    [ this medium has no formal name ]");
  }
  else {
    StrCat(buf, "\n    Formal name is ");
    StrCat(buf, media->formal_name);
  }
  StrCat(buf, "\n");

  if (countOnly)
  {
	detInfoCharCount += StringLen(buf);
  } else {
	StrCat(CdDetailedBuf, buf);
  }

  /* always return FALSE, so that ParseMedia() will refrain from setting */
  /* validity flags                                                      */
  return FALSE;
}
	 
  
/*****************************************************************************
*
*   CdDetailedInfo()
*   	Gets formatted text information about the current status, or returns
*   	NULL; the text (if any) is stored in a statically allocated buffer
*
*****************************************************************************/

CharPtr CdDetailedInfo (void)

{
  if (numinits == 0) /* not yet initialized */
  {
	if (CdDetailedBuf == NULL)
	{
	  CdDetailedBuf = StringSave("CD-ROM and HARD DISK access information is not currently available\n");
	}
	return CdDetailedBuf;
  }
  detInfoCharCount = 0;
  countOnly = TRUE;
  ParseMedia(CdFmtInfo, MEDIUM_CD | MEDIUM_DISK);
  countOnly = FALSE;
  if (detInfoCharCount == 0)
    return NULL;
  if (CdDetailedBuf != NULL)
  {
    CdDetailedBuf = (CharPtr) MemFree(CdDetailedBuf);
  }
  CdDetailedBuf = (CharPtr) MemNew(detInfoCharCount + 200);
  StrCpy(CdDetailedBuf, "CD-ROM and HARD DISK ACCESS\n");
  if (CurMediaType() == MEDIUM_CD || CurMediaType() == MEDIUM_DISK)
  {
    StrCat(CdDetailedBuf, "  Currently active medium is ");
    StrCat(CdDetailedBuf, (GetCurMedia())->formal_name);
    StrCat(CdDetailedBuf, "\n");
  }
  ParseMedia(CdFmtInfo, MEDIUM_CD | MEDIUM_DISK);
  return CdDetailedBuf;
}

/*  -------------------- CdSetPath() ---------------------------------
 */
static Boolean NEAR CdSetPath (Int2 dir, CharPtr path)

{
    int  k = 0;

	if (path != NULL)
		k = StringLen(path);

    if ((dir<2) || (dir>=NDIR) || (k==0)) {
		ErrPostEx(SEV_ERROR, ERR_CD_BADDIR, 0, sCdError[ERR_CD_BADDIR], dir);
        return FALSE;
    }
    StringCpy (sPath[dir], path);
    FileBuildPath(sPath[dir], NULL, NULL);
    return TRUE;
}

/*****************************************************************************
*
*   UidIdxGet(type, uid, idx)
*
*****************************************************************************/
static UidIdxPtr NEAR UidIdxGet (DocType type, DocUid uid, UidIdxPtr idx)

{
	Int4Ptr ip;
	Int2 i, j, l, r;
    FILE * fp;
    CharPtr path;
	struct idxrec {
		DocUid uid;
		Int4 entry_offset,
			link_offset;
	} PNTR idxptr;

	if (! ValidateUid(type, uid))
		return NULL;

	if (type == TYP_SEQ)    /* AA, NT, SEQ all the same */
		type = TYP_AA;
		
    if (type_bucket_index[type] == NULL)
	{
        if (! LoadUidIndex(type))
			return NULL;
	}

	ip = type_bucket_index[type];
	r = vi->types[type].num_bucket - 1;
	l = 0;
	j = 0;
	while ((l <= r) && (! ((ip[j] <= uid) && (ip[j+1] > uid))))
	{
		j = (l + r) / 2;
		if (uid > ip[j])
			l = j + 1;
		else
			r = j - 1;
	}
		
	if (type != TYP_ML)
		type = TYP_SEQ;

	if ((fp = IdxFilePtr[type]) == NULL)
	{
		path = MakePath (DIR_IDX, type, SUF_UID, EXT_LST);
		if ((fp = FileOpen(path, "rb")) == NULL)
		{
			ErrPostEx(SEV_ERROR, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
			return NULL;
		}
	}

	fseek(fp, (long)j * (long)vi->type_bucket_size, SEEK_SET);
	
	j = FileRead(buffer, 1, vi->type_bucket_size, fp);

	if (HoldIdxOpen)
	{
		IdxFilePtr[type] = fp;
	} else {
	    FileClose(fp);
	}

	if (j == 0)
	{
		ErrPostEx(SEV_ERROR, ERR_CD_FILEREAD, 0, sCdError[ERR_CD_FILEREAD]);
		return NULL;
	}

	idxptr = (struct idxrec PNTR) buffer;
	j = vi->type_bucket_size / sizeof(struct idxrec);
	for (i = 0; i < j; i++, idxptr++)
	{
		if (uid == SwapInt4(idxptr->uid))
		{
	 	    if (idx == NULL)
       		    idx = (UidIdxPtr) MemNew(sizeof(UidIdx));
	  	    else
        		MemFill(idx, '\0', sizeof(UidIdx));

		   	idx->type = type;
		    idx->uid = uid;
			idx->entry_offset = SwapInt4(idxptr->entry_offset);
			idx->sum_offset = 0;
			idx->link_offset = SwapInt4(idxptr->link_offset);
			if (type == TYP_SEQ)
			{
				if (idx->entry_offset & 0x80000000)
					idx->type = TYP_AA;
				else
					idx->type = TYP_NT;
			}
			return idx;
		}
	}

	return NULL;
}

/*  -------------------- CdTrmPageCt() --------------------------------
 *  CdTrmPageCt -- returns the number of term pages for a type/field pair.
 *
 *  Parameters:     type:     database code.
 *                  field:    field code.
 *
 *  Return value:   non-zero:   Success;  page count.
 *                  zero:       Failure;  refer to error code.
 */

Int2    CdTrmPageCt (DocType type, DocField field)

{
    if (!ValidateType (type))  return  0;
    if (!ValidateField (type, field))  return 0;
    return (Int2)  vi->types[type].fields[field].num_bucket;
}


/*  -------------------- CdTrmLookup() --------------------------------
 *  CdTrmLookup -- returns the first page that COULD contain a term.
 *
 *  Parameters:     type:     database code.
 *                  field:    field code.
 *                  term:   term (or term fragment) to lookup.
 *
 *  Return value:   non-negative:   Success;  page number. (zero-based)
 *                  negative:       Failure;  refer to error code.
 */

Int2    CdTrmLookup (DocType type, DocField field, CharPtr term)

{
    int  i;

    if (!LoadTrmIndex (type, field))
		return(-1);

    for (i=0; i< term_idx_count; i++) {
        if (MeshStringICmp (term_idx_str[i], term) >= 0) 
            return  MAX (0,i-2);
    }
    return  MAX (0,term_idx_count-2);
}

 
/*  -------------------- CdTrmPages() ---------------------------------
 *  CdTrmPages -- fetches a range of term pages from the CD-ROM.
 *
 *  Parameters:     type:     database code.
 *                  field:    field code.
 *                  pg:     page number of first page to read.
 *                  ct:     number of pages to read.
 *                  buffer: buffer to receive the data.
 *
 *  Return value:   non-zero:   Success;  number of pages read.
 *                  zero:       Failure;  refer to error code.
 *
 *  Notes:  The term pages contain a series of variable-length term records,
 *      each of which is an ASCII string with the following structure:
 *
 *      <term>\t<c1>\t<c2>\t<offset>\n
 *
 *      term:       term
 *      c1:         count of 'special' occurrences.
 *      c2:         count of total occurrences.  ** NOTE **
 *      offset:     offset in postings file of list of document numbers.
 *      \t:         tab character  (?).
 *      \n:         newline character  ('\x0A').
 *
 *      A term record may cross a page boundary.
 */

Int2    CdTrmPages (DocType type, DocField field, Int2 pg)

{
    CharPtr path, buff;
    FILE   *fd;
    Int4    offset;
    size_t    bytes;

	if ((type == trmtype) && (field == trmfield) && (pg == trmpage) && (trmpages))
		return trmpages;

	if (trmbuf == NULL)
		trmbuf = (CharPtr) MemNew(trmpagesrequest + 2); /* allow terminating 00 */
	buff = trmbuf;   /* use local static buffer */
    /* need to fill buffer with NULL's */
    MemFill(buff, 0, trmpagesrequest + 2);
	trmpages = 0;    /* no pages loaded */

    if (!ValidateType (type))  return 0;
    if (!ValidateField (type, field))  return 0;

    path = MakePath (DIR_TRM, type, field, EXT_LST);
    if ((fd = FileOpen(path, "rb")) ==NULL)  {
		ErrPostEx(SEV_WARNING, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return  0;
    }
    offset = (long) pg * BLKSIZE;
    fseek (fd, offset, SEEK_SET);
	bytes = FileRead(buff, 1, trmpagesrequest, fd);
    FileClose (fd);

	if (bytes == trmpagesrequest)   /* got the extra page */
		bytes -= BLKSIZE;
	trmpages = (Int2)(bytes/BLKSIZE);
	if (bytes % BLKSIZE)   /* got a partial last page */
		trmpages++;
	trmtype = type;
	trmfield = field;
	trmpage = pg;
	                          /* may have to switch \n for \r */
    return  trmpages;
}


/*  -------------------- CdTrmUidsFil () --------------------------------
 *  CdTrmUids -- retrieves a list of uids for a term.
 *
 *  Parameters:     type:         database code.
 *                  field:        field code.
 *                  offset:     offset into postings file.
 *                  count:      number of uids.
 *                  filename:   name of file to receive the results.
 *
 *  Return value:   non-zero:   Success; number of documents (same as count).
 *                  zero:       Failure;  refer to error code.
 *
 *  Notes:   the offset value is obtained by:  
 *      1)  looking up a term (using CdTrmLookup()).
 *      2)  loading term pages (using CdTrmPages()).
 *      3)  finding the term in the loaded pages.
 */

Int4    CdTrmUidsFil (DocType type, DocField field, Int4 offset, Int4 count, CharPtr filename, Boolean append)

{
    Int4  i;
    FILE *fd1;
    FILE *fd2;
    Char mode [4];
    CharPtr path;
    Int4Ptr ptr;
    size_t cnt;
    Int4 cntr;

    if (!ValidateType (type))   return  0;
    if (!ValidateField (type, field))  return  0;

    path = MakePath (DIR_TRM, type, field, EXT_PST);
    if ((fd1 = FileOpen(path, "rb")) == NULL)
	{
		ErrPostEx(SEV_WARNING, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return 0;
    }

    if (append) {
      StringCpy (mode, "ab");
    } else {
      StringCpy (mode, "wb");
    }
    if ((fd2 = FileOpen(filename, mode)) == NULL)
	{
        FileClose (fd1);
		ErrPostEx(SEV_WARNING, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], filename);
        return  0;
    }

    fseek (fd1, offset, SEEK_SET);

    cntr = count;
    cnt = (size_t) MIN (cntr, (Int4)(BLKSIZE / sizeof(Int4)));
	ptr = (Int4Ptr) buffer;
    while (cnt > 0)
	{
        FileRead (buffer, sizeof (Int4), cnt, fd1);
        for (i = 0; i < (Int4) cnt; i++)
			ptr[i] = SwapInt4(ptr[i]);
        if (! FileWrite (buffer, sizeof(Int4), cnt, fd2))
		{
			ErrPostEx(SEV_ERROR, ERR_CD_FILEWRITE, 0, sCdError[ERR_CD_FILEWRITE]);
            break;
        }
        cntr -= cnt;
	    cnt = (size_t) MIN (cntr, (Int4)(BLKSIZE / sizeof(Int4)));
    }

    FileClose (fd1);
    FileClose (fd2);
	if (cntr)    /* didn't finish */
		return 0;
	else
		return count;
}

/*  -------------------- CdTrmUidsMem () --------------------------------
 *  CdTrmUidsMem -- retrieves a list of uids for a term.
 *
 *  Parameters:     type:         database code.
 *                  field:        field code.
 *                  offset:     offset into postings file.
 *                  count:      number of uids.
 *                  mem:       storage to receive the results.
 *
 *  Return value:   non-zero:   Success; number of documents (same as count).
 *                  zero:       Failure;  refer to error code.
 *
 *  Notes:   the offset value is obtained by:  
 *      1)  looking up a term (using CdTrmLookup()).
 *      2)  loading term pages (using CdTrmPages()).
 *      3)  finding the term in the loaded pages.
 */

Int4    CdTrmUidsMem (DocType type, DocField field, Int4 offset, Int4 count, DocUidPtr mem)

{
    Int4  i;
    FILE *fd1;
    CharPtr path;
    size_t cnt;

    if (!ValidateField (type, field))
		return  0;
	if (mem == NULL)
		return 0;

    path = MakePath (DIR_TRM, type, field, EXT_PST);
    if ((fd1 = FileOpen(path, "rb")) == NULL)
	{
		ErrPostEx(SEV_WARNING, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return 0;
    }

    fseek (fd1, offset, SEEK_SET);

    cnt = FileRead ((VoidPtr)mem, sizeof (Int4), (size_t) count, fd1);
    for (i = 0; i < (Int4) cnt; i++)
		mem[i] = SwapInt4(mem[i]);

    FileClose (fd1);
	return (Int4) cnt;
}

/*  -------------------- CdDocAsnOpen() -----------------------------------
 *  CdDocAsnOpen -- returns an active AsnIoPtr for a document.
 *
 *  Parameters:     type:        class code (ML/AA/NT).
*   				uid:         unique identifier
 *
 *  Return value:   non-null:   Success;  active asnioptr
 *                  null:       Failure;  refer to error code.
 *
 *  For TYP_ML, the value is a Medline-entry
*   For TYP_AA or TYP_NT it is a Bioseq-set.
 */

AsnIoPtr  CdDocAsnOpen (DocType type, DocUid uid)

{
    FILE * fd2;
    AsnIoPtr aip;
    DecompInfoPtr decomp;

    fd2 = CdDocFil (type, uid, NULL);
    if (fd2 == NULL)
        return NULL;

	if (vi->no_compression)
	{ /* no compression on this data source */
		aip = AsnIoNew(ASNIO_BIN_IN, fd2, NULL, NULL, NULL);
	}
	else { /* use alternate read function for compressed data sources */
    	decomp = DecompInit(fd2);
    	aip = AsnIoNew(ASNIO_BIN_IN, fd2, decomp, DecompReadFunc, NULL);
    	if (aip == NULL)
			DecompFini(NULL, decomp);
    	decomp->aip = aip;
	}

    return aip;
}

/*****************************************************************************
*
*   CdDocAsnClose(aip)
*       closes an aip opened by CdDocAsnOpen
*
*****************************************************************************/
AsnIoPtr  CdDocAsnClose (AsnIoPtr aip)

{
	if (!vi->no_compression)
	{
    	DecompFini(aip, NULL);
	}
	
    AsnIoClose(aip);

    return NULL;
}

/*  =========================================================================
 *      PRIVATE FUNCTION BODIES
 */

static Boolean NEAR  ValidateUid (DocType type, DocUid uid)

{
	EntrezTypeDataPtr tdp;
	DocType tmp;

	if (! ValidateType(type))
		return FALSE;

	tmp = type;
	if (tmp == TYP_SEQ)
		tmp = TYP_AA;

	tdp = &vi->types[tmp];
	if ((uid >= tdp->minuid) && (uid <= tdp->maxuid))
		return TRUE;

	if (type == TYP_SEQ)
	{
		tdp = &vi->types[TYP_NT];
		if ((uid >= tdp->minuid) && (uid <= tdp->maxuid))
			return TRUE;
	}

	return FALSE;
}

static Boolean NEAR  ValidateType (DocType type)

{
    if (((type < 0) || (type >= NTYPE)) && (type != TYP_SEQ)) {
		ErrPostEx(SEV_ERROR, ERR_CD_BADTYPE, 0, sCdError[ERR_CD_BADTYPE], type);
        return  FALSE;
    }
    return  TRUE;
}

static Boolean NEAR  ValidateField (DocType type, DocField field)

{
    if (type<0 || type>=NTYPE) {
		ErrPostEx(SEV_ERROR, ERR_CD_BADTYPE, 0, sCdError[ERR_CD_BADTYPE], type);
        return  FALSE;
    }
    if (field<0 || field>=NFLD) {
		ErrPostEx(SEV_ERROR, ERR_CD_BADFIELD, 0, sCdError[ERR_CD_BADFIELD], field);
        return  FALSE;
    }
    if (vi->types[type].fields[field].num_bucket == 0) {
		ErrPostEx(SEV_ERROR, ERR_CD_NOTERMS, 0, sCdError[ERR_CD_NOTERMS], type, field);
        return  FALSE;
    }
    return  TRUE;
}

static CharPtr NEAR  MakePath (Int2 nSdir, Int2 nPref, Int2 nSuff, Int2 nExtn)

{
    Char ltemp[8], filename[60];
    Char   c;
    CharPtr p;

    StringCpy (buffer, sPath[nSdir]);

    StringCpy (filename, sPref[nPref]);
    StringCat (filename, sSuff[nSuff]);
    StringCat (filename, ".");
        if (nExtn <0) {
        c = (char) -nExtn;
        ltemp[0] = (char) ('0' + (c/100));
        ltemp[1] = (char) ('0' + ((c%100)/10));
        ltemp[2] = (char) ('0' + (c%10));
        ltemp[3] = '\0';
        StringCat (filename, ltemp);
    }
    else
        StringCat (filename, sExtn[nExtn]);

    if (bAppendVer) 
        StringCat (filename, ";1");

	if (upperCaseIt) {
      p = filename;
	  while (*p != '\0') {
	    *p = TO_UPPER (*p);
	    p++;
	  }
	}

    FileBuildPath(buffer, NULL, filename);
    return  buffer;
}

static Boolean NEAR  LoadUidIndex (DocType type)

{
    Int2  i;
    size_t n;
    Int4Ptr p;
    CharPtr path;
    FILE   *fd;
    Int4 header [3];
    Int4 version;
    Int4 issue;

    if (!ValidateType (type))
		return FALSE;

	if ((type == TYP_SEQ) || (type == TYP_NT))
		type = TYP_AA;

    n = (size_t) vi->types[type].num_bucket + 1; 
    p = type_bucket_index[type];
    if (p != NULL)
        return TRUE;

	p = (Int4Ptr) MemNew(sizeof(Int4) * n);
	p[n-1] = INT4_MAX;            /* put sentinel at end */
	n--;         

	if (type == TYP_AA)
	{
		type = TYP_SEQ;
	}

    path = MakePath (DIR_IDX, type, SUF_UID, EXT_IDX);
    if ((fd = FileOpen (path, "rb")) ==NULL)  {
		MemFree (p);
		ErrPostEx(SEV_ERROR, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return FALSE;
    }
    if (vi->version != 0 || vi->issue != 0) { /* for compatibility with pre-release 6 data */
      if (FileRead ((CharPtr)header, sizeof(Int4), 3, fd) != 3) {
        FileClose (fd);
        MemFree (p);
        ErrPostEx(SEV_ERROR, ERR_CD_FILEREAD, 0, sCdError[ERR_CD_FILEREAD]);
        return FALSE;
      }
	  if (! IsOKMagic((Uint4) SwapInt4(header[1]), vi->volume_label))
	  {
        ErrPostEx(SEV_ERROR,  ERR_CD_BADINDEX, 0, sCdError[ERR_CD_BADINDEX]);
		return FALSE;
      }
      header [2] = SwapInt4 (header [2]);
      version = (Int4) vi->version;
      issue = (Int4) vi->issue;
      if (header [2] != ((version << 16) | issue)) {
        ErrPostEx(SEV_ERROR,  ERR_CD_BADINDEX, 0, sCdError[ERR_CD_BADINDEX]);
		return FALSE;
      }
    }
    if (FileRead ((CharPtr)p, sizeof(Int4), n, fd) !=n) {
        FileClose (fd);
		MemFree (p);
		ErrPostEx(SEV_ERROR, ERR_CD_FILEREAD, 0, sCdError[ERR_CD_FILEREAD]);
        return FALSE;
    }
    FileClose (fd);

	if (type == TYP_SEQ)
	{
		type_bucket_index[TYP_NT] = p;
		type_bucket_index[TYP_AA] = p;
	} else {
		type_bucket_index[type] = p;
	}
    for (i=0; i< (Int2) n; i++, p++) 
        *p = SwapInt4 (*p);
    return TRUE;
}

static Int2 NEAR  LoadTrmIndex (DocType type, DocField field)

{
    Int2   i, k, c, buckets;
    CharPtr path, p;
    Int4  bytes;
    FILE   *fd;

    if (!ValidateType(type))  return 0;
    if (!ValidateField(type, field))  return  0;

    if (type == term_idx_type && field == term_idx_field)
        return  term_idx_count;

    path = MakePath (DIR_TRM, type, field, EXT_IDX);
    if ((fd = FileOpen (path, "r")) ==NULL)  {
		ErrPostEx(SEV_ERROR, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return  0;
    }

    if (term_idx_count > 0)  FreeTrmIndex();

    buckets = (Int2)vi->types[type].fields[field].num_bucket;
    bytes = (buckets + 1) * sizeof(CharPtr);
    if ((term_idx_str = (CharPtr PNTR) MemNew((size_t)bytes)) ==NULL) {
        FileClose (fd);
		ErrPostEx(SEV_ERROR, ERR_CD_MEMORY, 0, sCdError[ERR_CD_MEMORY]);
        return(0);
    }

    for (i=0,c=0; c!=EOF; ) {
        for (p=buffer, k=0; k<128; k++) {
            c = fgetc(fd);
            if (c == EOF)  break;
            if (c == '\n' || c == '\r') {
                *p = '\0';
                break;
            }
            *p++ = (char) TO_LOWER(c);
        }
        while (c != '\n' && c != '\r' && c != EOF) {
            c = fgetc(fd);
        }
        *p = '\0';
        if (c != EOF && i < buckets) {
            if ((term_idx_str[i] = StringSave(buffer)) ==NULL)  {
                FileClose(fd);
                term_idx_count = i;
                FreeTrmIndex();
				ErrPostEx(SEV_ERROR, ERR_CD_MEMORY, 0, sCdError[ERR_CD_MEMORY]);
                return  0;
            }
			i++;
        }
    }
    FileClose (fd);
    term_idx_count = i;
    term_idx_type = type;
    term_idx_field = field;
    return  term_idx_count;
}

static void NEAR  FreeTrmIndex (void)

{
    int  i;

    for (i=0; i<term_idx_count; i++)
	{
		MemFree(term_idx_str[i]);
	}
    term_idx_str = (CharPtr PNTR) MemFree(term_idx_str);
    term_idx_count = 0;
    term_idx_type = -1;
    term_idx_field = -1;
}

extern AsnIoPtr   EntrezInfoOpen (CharPtr dirname)

{
    CharPtr p, buf, endpath;
    AsnIoPtr aip = NULL;
    FILE * fp;
	

	buf = (CharPtr) MemNew(PATH_MAX);
    p = StringMove(buf, dirname);
    endpath = buf + StringLen (buf);
    p = StringMove(p , "cdromdat.val;1");  
    p -= 2;           /* point to the semi-colon */
    *p = '\0';        /* wipe-out the semi-colon */
    if ((fp = FileOpen(buf, "rb")) == NULL)
    {
        *p = ';';     /* put back the semi-colon */
        if ((fp = FileOpen(buf, "rb")) != NULL)
            bAppendVer = TRUE;
    }

    if (fp == NULL) {
    	StringCat (buf, ";1");
    	p = endpath;
    	while (*p != '\0') {
    	  *p = TO_UPPER (*p);
    	  p++;
    	}
    	upperCaseIt = TRUE;
        p -= 2;           /* point to the semi-colon */
        *p = '\0';        /* wipe-out the semi-colon */
        if ((fp = FileOpen (buf, "rb")) == NULL) {
          *p = ';';     /* put back the semi-colon */
          if ((fp = FileOpen(buf, "rb")) != NULL)
            bAppendVer = TRUE;
        }
    }

    if (fp != NULL)
        aip = AsnIoNew(ASNIO_BIN_IN, fp, NULL, NULL, NULL);
	else
		ErrPostEx(SEV_WARNING, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], (CharPtr) "cdromdat.val");
	MemFree(buf);
    return aip;
}


#ifdef IS_LITTLE_ENDIAN

static Int2 NEAR  SwapInt2 (Int2 k)

{
    Uint2  j, l;
	Int2 m;

	l = (Uint2)k;
    j  = ((l & (Uint2)0xFF00) >> 8);
    j |= ((l & (Uint2)0x00FF) << 8);
	m = (Int2)j;
    return  m;
}

static Int4 NEAR  SwapInt4 (Int4 k)

{
    Uint4  j, l;
	Int4 m;

	l = (Uint4)k;
    j  = ((l & (Uint4)0xFF000000) >> 24);
    j |= ((l & (Uint4)0x00FF0000) >> 8);
    j |= ((l & (Uint4)0x0000FF00) << 8);
    j |= ((l & (Uint4)0x000000FF) << 24);
	m = (Int4)j;
    return  m;
}

#endif

/****** not used in reading cdrom **********************
static Int4 NEAR MergeSegOffset (Int2 seg, Int4 offset)

{
	Int4 value;

	value = (seg - 1) << 25;
	value += offset;
	return value;
}
******************************************************/
/***
bit 31 = if 1, is a protein, else is not
bits 30-25 = segment (file number)
bits 24-0  = offset into file up to 32 mbytes big
****************/
static Boolean NEAR SplitSegOffset (Int4 value, Int2Ptr segptr, Int4Ptr offsetptr)

{
	*segptr = (Int2)(((value >> 25) & 0x0000003F) + 1);
	*offsetptr = value & 0x01FFFFFF;
	return TRUE;
}

/*****************************************************************************
*
*   FILE * CdDocFil (type, uid, dat, &size)
*       opens a binary asn file, seeks to doc, returns a FILE * and size
*
*****************************************************************************/
static FILE * NEAR CdDocFil (DocType type, DocUid uid, UidIdxPtr idx)

{
    Int4 offset;
	Int2 seg, dir, db;
    CharPtr path;
    FILE   *fd2;
    UidIdx ui;

	if (idx == NULL)
	{
		idx = UidIdxGet(type, uid, &ui);
		if (idx == NULL)
			return NULL;
		if ((type == TYP_AA || type == TYP_NT) && idx->type != type)
			return NULL;
	}

	SplitSegOffset(idx->entry_offset, &seg, &offset);

    dir = (idx->type==TYP_ML) ? DIR_MED : DIR_SEQ;
    db = (idx->type==TYP_ML) ? PREF_MED : PREF_SEQ;

    path = MakePath (dir, db, SUF_ASN, (Int2) (-seg));
    if ((fd2=FileOpen (path, "rb")) == NULL)
	{
		ErrPostEx(SEV_ERROR, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return  NULL;
    }
    fseek (fd2, offset, SEEK_SET);
    return fd2;
}


/*****************************************************************************
*
*   CdTrmFind(type, field, term)
*      returns a pointer to a CdTerm structure
*
*****************************************************************************/

static CdTermPtr  CdTrmDup (CdTermPtr trmptr)

{
	CdTermPtr ctp;

	ctp = NULL;
	if (trmptr != NULL) {
		ctp = (CdTermPtr) MemNew (sizeof (CdTerm));
		if (ctp != NULL) {
			ctp->type = trmptr->type;
			ctp->field = trmptr->field;
			ctp->term = StringSave (trmptr->term);
			ctp->special_count = trmptr->special_count;
			ctp->total_count = trmptr->total_count;
			ctp->offset = trmptr->offset;
			ctp->page = trmptr->page;
			ctp->next = NULL;
		}
	}
	return ctp;
}

static CdTermPtr  CdTrmCache (CdTermPtr trmptr)

{
	CdTermPtr ctp;
	Int2 i;

	ctp = cdtrmcache [9];
	if (ctp != NULL) {
		if (ctp->term != NULL) {
			MemFree (ctp->term);
		}
		MemFree (ctp);
		cdtrmcache [9] = NULL;
	}
	for (i = 9; i > 0; i--) {
		cdtrmcache [i] = cdtrmcache [i - 1];
	}
	ctp = CdTrmDup (trmptr);
	cdtrmcache [0] = ctp;
	return trmptr;
}

CdTermPtr  CdTrmFind (DocType type, DocField field, CharPtr term)

{
    Int2 i;
	Int2 termpage;
	CdTermPtr ctp = NULL;
	CdTermPtr trmptr;

    for (i = 0; i < 10; i++) {
      trmptr = cdtrmcache [i];
      if (trmptr != NULL && trmptr->type == type && trmptr->field == field &&
          StringICmp (trmptr->term, term) == 0) {
            return CdTrmDup (trmptr);
      }
    }
	termpage = CdTrmLookup(type,field,term);
	if (termpage < 0)
		return NULL;

				/** could it already be cached? ***/
	if ((trmtype == type) && (trmfield == field) && (trmpages > 0))
	{
	 	if ((termpage <= (trmpage + trmpages - 1)) &&
			((termpage + 3) >= trmpage))     /* overlapping range */
		{
			ctp = CdTrmLocate(term, termpage);
			if (ctp != NULL)	/* found it */
				return CdTrmCache (ctp);
			if (termpage == trmpage)   /* not possible to find it */
				return NULL;
		}
	}

				/** Load term pages from disk ***/

	termpage = CdTrmPages(type, field, termpage);
	if (termpage == 0)
		return NULL;

	ctp = CdTrmLocate(term, termpage);
	return CdTrmCache(ctp);
}

/*****************************************************************************
*
*   CdTrmLocate(term, page)
*   	locates a term in a term list already in cache
*
*****************************************************************************/
static CdTermPtr NEAR CdTrmLocate (CharPtr term, Int2 page)

{
	Int2 size, ctr, cmpval;
	CharPtr ret;

    size = trmpages * BLKSIZE;     /* bytes in term cache */
    ctr = 0;
    ret = trmbuf;
	size--;    /* have to have at least one space for test below */
    while (ctr < size)
    {
	    while (*ret != '\n' && *ret != '\r')
		{
    	  ret++;
	      ctr++;
		  if (ctr >= size)
			return NULL;
    	}
	    ret++;
    	ctr++;

		cmpval = MeshStringICmp(ret, term);
		if (! cmpval)     /* found it */
			return CdTermRead(trmtype, trmfield, ret, trmbuf, page);
		else if (cmpval > 0)   /* gone past */
			return NULL;
    }
	return NULL;
}

/*****************************************************************************
*
*   CdTermRead(type, field, ptr, bufr, page)
*   	creates and returns a CdTermPtr from a CdTermPage
*   	ptr should point at the start of a record (the term)
*
*****************************************************************************/
static CdTermPtr  CdTermRead (Int2 type, Int2 field, CharPtr ptr, CharPtr bufr, Int2 page)

{
	CdTermPtr trmptr;
	CharPtr tmp, tmp2;
	Char localbuf[10];
	Int4 vals[3];
	Int2 i;

	if (ptr == NULL)
		return NULL;
	if (*ptr == '\0')
		return NULL;
	trmptr = (CdTermPtr) MemNew(sizeof(CdTerm));
	trmptr->type = type;
	trmptr->field = field;
	tmp = ptr;
	tmp2 = tmp;
	while (*tmp2 != '\t')
		tmp2++;
	*tmp2 = '\0';
	trmptr->term = StringSave(tmp);
	*tmp2 = '\t';
	tmp2++;
	for (i = 0; i < 3; i++)
	{
		tmp = &localbuf[0];
		while (*tmp2 >= ' ')
		{
			*tmp = *tmp2;
			tmp++; tmp2++;
		}
		*tmp = '\0';
		vals[i] = atol(localbuf);
		tmp2++;
	}
	trmptr->special_count = vals[0];
	trmptr->total_count = vals[1];
	trmptr->offset = vals[2];
	trmptr->page = page + (Int2) (((size_t) (ptr - bufr - 1)) / (size_t) BLKSIZE);
	return trmptr;
}

/*****************************************************************************
*
*   CdTermScan(type, field, page, numpage, proc)
*   	returns terms found to proc until
*   	1) no more pages
*   	2) numpage pages have been read
*   	3) proc returns FALSE
*   returns number of complete pages read
*   if numpage=0, scans until EOF or proc returns FALSE
*
*****************************************************************************/
Int2  CdTermScan (DocType type, DocField field, Int2 page, Int2 numpage, CdTermProc proc)

{
	Boolean    goOn;
	CharPtr    ptr;
	Int2       pages, size, pagectr, startpage;
	CdTermPtr  trmptr;

	startpage = page;
	pagectr = 0;
	if (proc == NULL)
		return pagectr;

	goOn = TRUE;
    while (goOn)
	{
		startpage = page;
		pages = CdTrmPages (type, field, page);
		if (pages == 0)
			return pagectr;
		ptr = trmbuf;
		size = pages * BLKSIZE;     /* bytes available */
		pages = BLKSIZE;      /* bytes per page */
		while ((size > 0) && (goOn))
		{
			while (*ptr != '\n' && *ptr != '\r' && *ptr != '\0')
			{
				size--;
				pages--;
				ptr++;
			}
			if (*ptr == '\0')
				return (Int2) (pagectr + 1);   /* last page */
			size--;
			pages--;
			ptr++;
			if (size > 0)
			{
				trmptr = CdTermRead(type, field, ptr, trmbuf, startpage);
				if (trmptr != NULL) {
					goOn = proc (trmptr);
				}
			}
			while (*ptr != '\n' && *ptr != '\r' && *ptr != '\0')
			{
				size--;
				pages--;
				ptr++;
			}
			if (pages < 0)   /* crossed a page boundary */
			{
				pages = BLKSIZE + pages;
				numpage--;
				pagectr++;
				page++;
				if (! numpage)
					goOn = FALSE;
			}
		}
	}
	return pagectr;
}

/*****************************************************************************
*
*   CdLinkUidGet(type, link_to_type, numuid, uids, max)
*   	returns count of input uids processed
*       returns -1 on error
*       if neighbors (type == link_to_type)
*   		sums weights for same uids
*   	if (more than max uids, frees uids and weights, but leaves num set)
*
*****************************************************************************/
Int2  CdLinkUidGet (LinkSetPtr PNTR result, DocType type, DocType link_to_type, Int2 numuid, Int4Ptr uids, Boolean mark_missing, Int4 maxlink)

{
	UidIdxPtr query;
    UidIdx local;
	DocType querytype;
	LinkSetPtr lsp = NULL;
	Int2 counts[NTYPE];
	FILE * fp;
	Int4 offset;
	CharPtr path;
	Uint1Ptr ptr1;
	Int2 numfound = 0;
	Int4 j, l, r, k;
	Boolean first = TRUE;
	Boolean sorted;
	Int4 cursize = 0, finalsize, finalcount = 0, count, i;
	Int4Ptr newuids = NULL,
			newwts = NULL,
			finaluids = NULL,
			finalwts = NULL,
			tmp;

	*result = NULL;
	
	if (! ValidateType(link_to_type))
		return -1;

	for (i = 0, query = NULL; i < numuid && query == NULL; i++)
	{
		query = UidIdxGet(type, uids[i], &local);
		if ((mark_missing) && (query == NULL))
			uids[i] *= -1;
		if (query != NULL)
			j = i;
	}
	if ((i == numuid) && (query == NULL)) { /* none found */
		lsp = (LinkSetPtr) MemNew(sizeof(LinkSet));
		lsp->uids = NULL;
		lsp->weights = NULL;
		*result = lsp;
		return 0;
	}

	querytype = query->type;   /* record, to allow for TYP_SEQ */
	if (link_to_type == TYP_SEQ)
	{
		if (type != TYP_SEQ)
			return -1;           /* can't do it */
		else
			link_to_type = querytype;   /* neighbors */
	}

	path = MakePath (DIR_LNK, query->type, SUF_REC, EXT_LNK);
    if ((fp = FileOpen(path, "rb")) == NULL)
	{
		ErrPostEx(SEV_ERROR, ERR_CD_FILEOPEN, 0, sCdError[ERR_CD_FILEOPEN], path);
        return -1;
	}

	if (numuid > 1)
	{
		if (numuid > 320) {
			finalsize = 16000;
		} else {
			finalsize = MIN((numuid * 50), 16000);     /* make a guess */
		}
		finaluids = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * finalsize));  /* make a guess */
		if (link_to_type == querytype)
			finalwts = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * finalsize));
	}

	for (; j < numuid; j++)
	{
		
		if (! first)
		{
		    if ((query = UidIdxGet(type, uids[j], &local)) == NULL)
			{
				if (mark_missing)
					uids[j] *= -1;
				continue; /* must examine remaining UIDs */
			}
		}
		else
			first = FALSE;

		numfound++;         /* count how many uids we process */

	                            /* read the link counts for all types */

	    fseek (fp, query->link_offset, SEEK_SET);
		FileRead((CharPtr)&counts[0], sizeof(Int2), NTYPE, fp);
		for (i = 0; i < NTYPE; i++)
			counts[i] = SwapInt2(counts[i]);

		offset = 0;
		for (i = 0; i < link_to_type; i++)
		{
			offset += counts[i] * sizeof(DocUid);
			if (i == query->type) {  /* has weights */
				offset += counts[i] * sizeof(Uint1);
			}
		}
		if (offset)						   /* skip preceeding link types */
			fseek(fp, offset, SEEK_CUR);

		count = (Int4)counts[link_to_type];

		if (count > cursize)
		{
			MemFree(newuids);
			newuids = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * (count + 1)));
			if (querytype == link_to_type)
			{
				MemFree(newwts);
				newwts = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * (count + 1)));
			}
			cursize = count;
		}
		FileRead((CharPtr)newuids, sizeof(DocUid), (size_t)count, fp);
		for (i = 0; i < count; i++)
			newuids[i] = SwapInt4(newuids[i]);
		if (link_to_type == querytype)    /* get the weights */
		{
			ptr1 = (Uint1Ptr) newwts;
			FileRead((CharPtr)ptr1, sizeof(Uint1), (size_t)count, fp);
			for (i = count - 1; i >= 0; i--) {
				newwts[i] = (Int4) (ptr1[i]);
			}
		}
		if (numuid > 1)           /* merging lists */
		{
			if ((finalcount + count) > finalsize)
			{
				finalsize += count;
				if (finalsize > 16000)
				{
					MemFree(newuids);
					MemFree(newwts);
					MemFree(finaluids);
					MemFree(finalwts);
					ErrPostEx(SEV_WARNING, ERR_CD_MEMORY, 0, sCdError[ERR_CD_MEMORY]);
					return -1;
				}
				tmp = finaluids;
				finaluids = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * finalsize));
				MemCopy(finaluids, tmp, (size_t)(finalcount * sizeof(Int4)));
				MemFree(tmp);
				if (querytype == link_to_type)
				{
				    tmp = finalwts;
					finalwts = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * finalsize));
					MemCopy(finalwts, tmp, (size_t)(finalcount * sizeof(Int4)));
					MemFree(tmp);
				}
			}
			for (i = 0; i < count; i++)
			{
				l = 0;               /* binary search */
				r = (finalcount - 1);
				k = 0;
				while ((l <= r) && (finaluids[k] != newuids[i]))
				{
					k = (l + r)/ 2;
					if (newuids[i] < finaluids[k])
						r = k - 1;
					else
						l = k + 1;
				}
				if (finaluids[k] == newuids[i])   /* merge */
				{
					if (querytype == link_to_type)
						finalwts[k] += newwts[i];
				}
				else
				{
					if (finalcount)
					{
						if (finaluids[k] < newuids[i])
							k++;
						l = (finalcount - k);
						r = l;
						tmp = &finaluids[finalcount];
						while (r)
						{
							*tmp = *(tmp-1);
							tmp--; r--;
						}
						if (querytype == link_to_type)
						{
							r = l;
							tmp = &finalwts[finalcount];
							while (r)
							{
								*tmp = *(tmp-1);
								tmp--; r--;
							}
						}
					}
					finaluids[k] = newuids[i];
					if (querytype == link_to_type)
						finalwts[k] = newwts[i];
					finalcount++;
				}
			}
		}
	}

	FileClose(fp);

	lsp = (LinkSetPtr) MemNew(sizeof(LinkSet));
	if (maxlink <= 0)
		maxlink = 16000;    /* default */

 	if (numuid == 1)
	{
		lsp->num = count;
		if (lsp->num <= maxlink)
		{
			lsp->uids = newuids;
			lsp->weights = newwts;
		}
		else
		{
			MemFree(newuids);
			MemFree(newwts);
		}
	}
	else
	{
		MemFree(newuids);
		MemFree(newwts);
		lsp->num = finalcount;
		if (lsp->num <= maxlink)
		{
			lsp->uids = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * (finalcount + 1)));
			MemCopy(lsp->uids, finaluids, (size_t)(finalcount * sizeof(Int4)));
			MemFree(finaluids);
			if (querytype == link_to_type)
			{
				lsp->weights = (Int4Ptr) MemNew((size_t)(sizeof(Int4) * (finalcount + 1)));
				MemCopy(lsp->weights, finalwts, (size_t)(finalcount * sizeof(Int4)));
				MemFree(finalwts);
				linksort(lsp->uids, lsp->weights, lsp->num);
			}
		}
		else
		{
			MemFree(finaluids);
			MemFree(finalwts);
		}
	}

	if (lsp->num > 1 && querytype != link_to_type && link_to_type == TYP_ML)
	{
		/* try to sort MEDLINE uids in descending order */

		for (sorted = TRUE, k = 1; k < lsp->num; k++)
		{
			if (lsp->uids[k-1] < lsp->uids[k])
			{
				sorted = FALSE;
				break;
			}
		}

		if (! sorted)
		{   /* assume that the existing order is reversed */
		    for (k = (lsp->num / 2) - 1; k >= 0; k--)
		    {
		    	j = lsp->uids[k];
		    	lsp->uids[k] = lsp->uids[lsp->num - 1 - k];
		    	lsp->uids[lsp->num - 1 - k] = j;
		    }

			/* now check that it's sorted */
	    	for (sorted = TRUE, k = 1; k < lsp->num; k++)
	    	{
	    		if (lsp->uids[k-1] < lsp->uids[k])
	    		{
	    			sorted = FALSE;
	    			break;
	    		}
	    	}

			if (! sorted)
			{ /* as a last resort, sort them using quicksort */
				/* dummy array */
				finaluids = (Int4Ptr) MemDup(lsp->uids, (size_t) (sizeof(Int4) * lsp->num));
				linksort(finaluids, lsp->uids, lsp->num);
				MemFree(finaluids);
			}
		}
	}

	*result = lsp;
	return numfound;
}

/*****************************************************************************
*
*   linksort(uids, wts, n)
*   	quicksort into descending wts order
*
*****************************************************************************/
static void NEAR linksort (Int4Ptr uids, Int4Ptr wts, Int4 n)

{
   Int4 tp, tp2;
   Int4 l, r, i, j, m, scnt;
   Int4 pstack[100];
   Int4Ptr p;

   if (n < 2)
       return;

   scnt = 2;
   l = 0; r = n - 1; p = pstack + 2;

   do
   {
       if ((r - l) > 15)
       {
           i = l; j = r;
                                     /* median of three */

           m = ((j - i) / 2) + i;    /* get middle element */
           /* partitioning operation */
           do
           {
               while((j > i) && (wts[j] <= wts[i]))
                   j--;
               if(j != i)
               {
                   tp = wts[j]; wts[j] = wts[i]; wts[i] = tp;
                   tp = uids[j]; uids[j] = uids[i]; uids[i] = tp;
                   while((i < j) && (wts[i] >= wts[j]))
                       i++;
                   if(i != j)
                      {tp = wts[j]; wts[j] = wts[i]; wts[i] = tp;
                       tp = uids[j]; uids[j] = uids[i]; uids[i] = tp;}
               }
           }while(i != j); /* end do */

           /* recursion elimination */
           if(i)
           {
               if((i - l) > (r - i))  /* put long segment on "stack" */
                   {*p = l; p++; *p = i - 1; p++; l = i + 1;}
               else
                   {*p = i + 1; p++; *p = r; p++; r = i - 1;}
               scnt += 2;
               if (scnt >= 100)
				{
					ErrPostEx(SEV_ERROR, ERR_CD_MEMORY, 0, "linksort > 100");
					return;
				}
           }
           else
           {
               l = i + 1;
           }
       }
                     /* if done with this segment, "pop" next */
       else
       {
            p--; r = *p; p--; l = *p; scnt -= 2;
       }
   }
   while (p > pstack);      /* end do */


   /* do the final insertion sort */

   for(i = 1; i < n; i++)
   {
       tp = wts[i]; tp2 = uids[i]; j = i; m = j - 1;
       while ((j > 0) && (wts[m] < tp))
           {wts[j] = wts[m]; uids[j] = uids[m]; j--; m--;}
       wts[j] = tp;
	   uids[j] = tp2;
   }
   return;
}

/*****************************************************************************
*
*   DecompReadFunc:
*   	substituted read function for compressed data sources (for Sequence
*   	and Medline data).
*
*****************************************************************************/
static Int2 LIBCALLBACK DecompReadFunc (Pointer p, CharPtr buff, Uint2 count)
{
	DecompInfoPtr dcp = (DecompInfoPtr) p;
	Uint1 loc_buff[3];
	int bytes_to_request;
	int bytes_read;

	if (dcp->compr == COMPR_DONT_KNOW)
	{
		int c;

		/* read the "decompression protocol identifier" */
		if ((c = fgetc(dcp->fp)) == EOF)  
			return 0;
		dcp->compr = (Uint1) c;

		if (dcp->compr == COMPR_NONE)
		{
			/* for no decompression, we still have 4 bytes of overhead;     */
			/* 1 byte for the protocol identifier, and 3 bytes for a length */
			/* field of what follows                                        */
			if (FileRead((CharPtr) loc_buff,1,3,dcp->fp) != 3)
			{
				ErrPostEx(SEV_ERROR, ERR_CD_BADDECOMP, 0,
				    	"No length field detected for uncompressed data");
				return 0;
			}

			/* interpret the 3-byte length in a machine-independant order;  */
			/* BIG ENDIAN (first byte is most significant)                  */
			dcp->bytes_left = (((int) loc_buff[0]) * 256 + loc_buff[1]) * 256 +
					          loc_buff[2];
		}
	}

	switch (dcp->compr)
	{
		case COMPR_NONE :
			/* based on knowledge of how many bytes are in this compressed  */
			/* ASN.1 object, return only as many bytes as the caller really */
			/* needs                                                        */
			bytes_to_request = (int) MIN((Uint4) count, dcp->bytes_left);
			bytes_read = FileRead(buff,1,bytes_to_request,dcp->fp);
			dcp->bytes_left -= bytes_read;
			if (dcp->bytes_left <= 0)
			{
				/* reset for stream read of next entry */
				dcp->compr = COMPR_DONT_KNOW;
			}
			return bytes_read;
		
		case COMPR_HUFFMAN :
			return HuffmanRead(dcp,buff,count);
		
		/* others ?? */

		default:
			ErrPostEx(SEV_ERROR, ERR_CD_BADDECOMP, 0,
				    "Invalid decompression code detected <%d>", dcp->compr);
			return 0;
	}
}

/*****************************************************************************
*
*   HuffmanRead:
*   	read Huffman compressed data
*
*****************************************************************************/
static Int2 HuffmanRead (DecompInfoPtr dcp, CharPtr buff, Uint2 count)
{
	register unsigned int mask = dcp->mask;
	register unsigned int byte = dcp->byte;
	CharPtr p = buff;
	int i, cnt = 0;
	int c;
	int k;
	FILE *fd1 = dcp->fp;


	while (cnt < (int) count)
	{
		for (i=0; i>=0; )
		{
			if (mask == 0)
			{
				if ((c = fgetc(fd1)) == EOF)
				{
					/* should never reach this point */
					i = HUFFMAN_SENTINEL - 257;
					break;
				}
				else
				{
					byte = (unsigned int) c;
					mask = 0x80;
				}
			}

			if (byte & mask)
				i = vi->huff_left[i];
			else
				i = vi->huff_right[i];

			mask >>= 1;
		}

		if ((k = i + 257) == HUFFMAN_SENTINEL)
		{
			mask = 0; /* to skip remaining bits in current byte */
			dcp->compr = COMPR_DONT_KNOW; /* reset for next record */
			break;
		}

		*p++ = (char) k;
		cnt++;
	}

	dcp->mask = mask;
	dcp->byte = byte;
	return cnt;
}

/*****************************************************************************
*
*   DecompInit:
*   	Create a data structure to be used in decompression; the data structures
*   	are stored in a linked list. While no mutual exclusion is provided on
*   	list access, each decompression is independent ... therefore, many
*   	compressed ASN.1 data streams may be open and used simultaneously
*
*****************************************************************************/
static DecompInfoPtr NEAR DecompInit (FILE *fp)
{
	DecompInfoPtr dcp;

	dcp = (DecompInfoPtr) MemNew(sizeof(DecompInfo));

	if (dcp == NULL)
		return NULL;

	dcp->fp = fp;
	dcp->compr = COMPR_DONT_KNOW;
	dcp->mask = 0;
	dcp->bytes_left = 0;

	/* insert it */
	dcp->next = DecompInfoList;
	DecompInfoList = dcp;

	return dcp;
}

/*****************************************************************************
*
*   DecompFini:
*   	Find and destroy the specified decompression data structure. The
*   	data structures, in addition to having an address known to its user,
*   	also contains a copy of the AsnIoPtr for that data stream. This
*   	enables the Fini() operation to be performed using either the address
*   	of this structure as a key, or the address of the AsnIoPtr as a key.
*
*****************************************************************************/
static Boolean NEAR DecompFini (AsnIoPtr aip, DecompInfoPtr dcp)
{
	DecompInfoPtr dtrail;
	DecompInfoPtr temp;

	if (DecompInfoList == NULL)
		return FALSE; /* not found */
	
	/* check for first element in list */
	if ((DecompInfoList == dcp && dcp != NULL) ||
	    (DecompInfoList->aip == aip && aip != NULL))
	{ /* unlink and delete */
		temp = DecompInfoList->next;
		DecompInfoFree(DecompInfoList);
		DecompInfoList = temp;
		return TRUE;
	}

	if (DecompInfoList->next == NULL)
	{ /* single-element list, and it's not the first element in list */
		return FALSE;
	}

	for (dtrail = DecompInfoList; dtrail->next != NULL;
	     dtrail = dtrail->next)
	{ /* search remainder of list */
		if ((dtrail->next == dcp && dcp != NULL) ||
		    (dtrail->next->aip == aip && aip != NULL))
		{
			temp = dtrail->next->next;
			DecompInfoFree(dtrail->next);
			dtrail->next = temp;
			return TRUE;
		}
	}

	return FALSE;
}


/*****************************************************************************
*
*   DecompInfoFree:
*   	Free a decompression data structure
*****************************************************************************/
static void NEAR DecompInfoFree(DecompInfoPtr dcp)
{
	MemFree(dcp);
}


/*****************************************************************************
*
*   IsOKMagic:
*   	Validate the magic number for a file
*****************************************************************************/
static Boolean NEAR IsOKMagic(Uint4 magic, CharPtr volume_label)
{
	/* check for a match with the "base" magic number; supported for        */
	/* backwards compatability                                              */
	if (magic == CD_MAGIC_BASE)
		return TRUE;

	/* now check if the magic number equals the "base" plus the checksum of */
    /* the volume-label (so as to be able to distinguish between index      */
	/* files associated with different CDs)                                 */
	while (*volume_label)
	{
		magic -= (int) (*volume_label++);
	}
	return (magic == CD_MAGIC_BASE);
}


#endif