File: fosfat.c

package info (click to toggle)
fosfat 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 440 kB
  • sloc: ansic: 2,857; sh: 834; makefile: 524
file content (2245 lines) | stat: -rw-r--r-- 54,186 bytes parent folder | download | duplicates (5)
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
/*
 * FOS libfosfat: API for Smaky file system
 * Copyright (C) 2006-2010 Mathieu Schroeter <mathieu.schroeter@gamesover.ch>
 *
 * Thanks to Pierre Arnaud for his help and the documentation
 *    And to Epsitec SA for the Smaky computers
 *
 * This file is part of Fosfat.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <inttypes.h>
#include <ctype.h>      /* tolower */
#include <string.h>     /* strcasecmp strncasecmp strdup strlen strtok
                           memcmp memcpy strcasestr */

#ifdef _WIN32
#include <w32disk.h>
#endif /* _WIN32 */

#include "fosfat.h"

#define MAX_SPLIT             64

/* Block size (256 bytes) */
#define FOSFAT_BLK            256

#define FOSFAT_NBL            4
#define FOSFAT_Y2K            70

#define FOSFAT_BLOCK0         0x00
#define FOSFAT_SYSLIST        0x01

#define FOSBOOT_FD            0x10
#define FOSBOOT_HD            0x20

#define FOSFAT_DEV            FILE

/* FOS attributes and type */
#define FOSFAT_ATT_OPENEX     (1 <<  0)
#define FOSFAT_ATT_MULTIPLE   (1 <<  1)
#define FOSFAT_ATT_DIR        (1 << 12)
#define FOSFAT_ATT_VISIBLE    (1 << 13)
#define FOSFAT_ATT_ENCODED    (1 << 17)
#define FOSFAT_ATT_LINK       (1 << 24)
#define FOSFAT_TYPE_SYSTEM    0xF8

/* List of all block types */
typedef enum block_type {
  B_B0,                        /* Block 0                               */
  B_BL,                        /* Block List                            */
  B_BD,                        /* Block Description                     */
  B_DATA                       /* Only DATA                             */
} fosfat_type_t;

/* Search type */
typedef enum search_type {
  S_BD,                        /* Search BD                             */
  S_BLF                        /* Search BL File                        */
} fosfat_search_t;

/* foslog type */
typedef enum foslog {
  FOSLOG_ERROR,                /* Error log                             */
  FOSLOG_WARNING,              /* Warning log                           */
  FOSLOG_NOTICE                /* Notice log                            */
} foslog_t;

/* Data Block (256 bytes) */
typedef struct block_data_s {
  uint8_t data[256];           /* Data                                  */
  /* Linked list */
  struct  block_data_s *next_data;
} fosfat_data_t;

/* Block 0 (256 bytes) */
typedef struct block_0_s {
  uint8_t sys[44];             /* SYSTEM folder                         */
  int8_t  nlo[16];             /* Disk name                             */
  uint8_t chk[4];              /* Check control                         */
  uint8_t mes[172];            /* Message                               */
  uint8_t change;              /* Need of change the CHK number         */
  uint8_t bonchk;              /* New format                            */
  uint8_t oldchk[4];           /* Old CHK value                         */
  uint8_t newchk[4];           /* New CHK value                         */
  uint8_t reserve[10];         /* Unused                                */
} fosfat_b0_t;

/* File in a Block List (60 bytes) */
typedef struct block_listf_s {
  int8_t  name[16];            /* Filename                              */
  uint8_t typ;                 /* Filetype                              */
  uint8_t ope;                 /* Open counter                          */
  uint8_t att[4];              /* Attributes                            */
  uint8_t lg[4];               /* Length in blocks                      */
  uint8_t lgb[2];              /* Number of bytes in the last block     */
  uint8_t cd[3];               /* Date of Creation                      */
  uint8_t ch[3];               /* Hour of Creation                      */
  uint8_t wd[3];               /* Date of the last Write                */
  uint8_t wh[3];               /* Hour of the last Write                */
  uint8_t rd[3];               /* Date of the last Read                 */
  uint8_t rh[3];               /* Hour of the last Read                 */
  uint8_t secid[3];            /* Security ID                           */
  uint8_t clope;               /* Open mode before CLOSE                */
  uint8_t pt[4];               /* Pointer on the BD                     */
  uint8_t lgf[4];              /* File size in bytes                    */
  uint8_t code[2];             /* Code control                          */
} fosfat_blf_t;

/* Block List (256 bytes) */
typedef struct block_list_s {
  fosfat_blf_t file[4];        /* 4 BL files (240 bytes)                */
  uint8_t      next[4];        /* Next BL                               */
  uint8_t      chk[4];         /* Check control                         */
  uint8_t      prev[4];        /* Previous BL                           */
  uint8_t      reserve[4];     /* Unused                                */
  /* Not in the block */
  uint32_t     pt;             /* Block's number of this BL             */
  /* Linked list */
  struct       block_list_s *next_bl;
} fosfat_bl_t;

/* Block Description (256 bytes) */
typedef struct block_desc_s {
  uint8_t next[4];             /* Next BD                               */
  uint8_t prev[4];             /* Previous BD                           */
  uint8_t npt[2];              /* Number of tranches in the BD          */
  uint8_t pts[42][4];          /* Pointers on the tranches (max 42)     */
  int8_t  name[16];            /* Filename                              */
  uint8_t nbs[42];             /* Length (in blocks) of each tranches   */
  uint8_t reserve[4];          /* Unused                                */
  uint8_t lst[2];              /* Number of byte in the last tranche    */
  uint8_t hac[2];              /* Hashing function if LIST              */
  uint8_t nbl[2];              /* Number of BL used if LIST             */
  uint8_t chk[4];              /* Check control                         */
  uint8_t off[4];              /* Offset (in blocks) of all previous BD */
  uint8_t free[2];             /* Unused                                */
  /* Linked list */
  struct  block_desc_s *next_bd;
  struct  block_list_s *first_bl;
} fosfat_bd_t;

/* Cache list for name, BD and BL blocks */
typedef struct cache_list_s {
  char    *name;
  uint32_t bl;                 /* BL Address                            */
  uint32_t bd;                 /* BD Address                            */
  int      isdir;              /* If is a directory                     */
  int      islink;             /* If is a soft link                     */
  int      isdel;              /* If is deleted                         */
  /* Linked list */
  struct   cache_list_s *sub;
  struct   cache_list_s *next;
} cachelist_t;

/* Main fosfat structure */
struct fosfat_s {
#ifdef _WIN32
  w32disk_t   *dev;            /* device object                         */
#else
  FOSFAT_DEV  *dev;            /* physical device                       */
#endif /* !_WIN32 */
  int          fosboot;        /* FOSBOOT address                       */
  uint32_t     foschk;         /* CHK                                   */
  int          viewdel;        /* list deleted files                    */
  cachelist_t *cachelist;      /* cache data                            */
};


/* Global variable for internal logger */
static int g_logger = 0;


#define FOSFAT_IS(handle, loc, att)                   \
  {                                                   \
    int res = 0;                                      \
    fosfat_blf_t *entry;                              \
                                                      \
    entry = fosfat_search_insys (handle, loc, S_BLF); \
    if (!entry)                                       \
      return 0;                                       \
                                                      \
    if (fosfat_in_is##att (entry))                    \
      res = 1;                                        \
                                                      \
    free (entry);                                     \
    return res;                                       \
  }
#define FOSFAT_IS_DIR(handle, loc)     FOSFAT_IS(handle, loc, dir)
#define FOSFAT_IS_LINK(handle, loc)    FOSFAT_IS(handle, loc, link)
#define FOSFAT_IS_VISIBLE(handle, loc) FOSFAT_IS(handle, loc, visible)
#define FOSFAT_IS_ENCODED(handle, loc) FOSFAT_IS(handle, loc, encoded)
#define FOSFAT_IS_OPENEXM(handle, loc) FOSFAT_IS(handle, loc, openexm)

#ifndef _WIN32
/*
 * Translate a block number to an address.
 *
 * This function depend if the media is an HARD DISK or an 3"1/2 DISK.
 * The FOSBOOT is the double in number of blocks for an hard disk.
 * The real address is the address given by the disk + the size of the
 * FOSBOOT part.
 *
 * block        the block's number given by the disk
 * fosboot      offset in the FOS address
 * return the address of this block on the disk
 */
static inline uint32_t
blk2add (uint32_t block, int fosboot)
{
  return ((block + fosboot) * FOSFAT_BLK);
}
#endif /* !_WIN32 */

#ifdef _WIN32
/*
 * Translate a block number to a sector.
 *
 * This function depend if the media is an HARD DISK or an 3"1/2 DISK.
 * The FOSBOOT is the double in number of blocks for an hard disk.
 * The real address is the sector given by the disk with the size of the
 * FOSBOOT part.
 *
 * block        the block's number given by the disk
 * fosboot      offset in the FOS address
 * return the sector on the disk
 */
static inline uint32_t
blk2sector (uint32_t block, int fosboot)
{
  /* FIXME: div by 2 only right if the sector size is 512 */
  return ((block + fosboot) / 2);
}

/*
 * Return the offset in the block to get the right Smaky's block.
 *
 * Smaky's blocks are of 256 bytes and Win32 uses 512. This function search
 * if an offset of 256 is necessary to get the right Smaky's block, or not.
 *
 * block        the block's number given by the disk
 * fosboot      offset in the FOS address
 * return the offset in the Win32's block
 */
static inline uint32_t
sec_offset (uint32_t block, int fosboot)
{
  /* FIXME: modulo by 2 only right if the sector size is 512 */
  return (((block + fosboot) % 2) ? FOSFAT_BLK : 0);
}
#endif /* _WIN32 */

/*
 * Convert char table to an integer.
 *
 * This convertion will swapped all bytes and returned an int value.
 *
 * value        pointer on the char table
 * size         size of the table (number of bytes)
 * return the integer value
 */
static uint32_t
c2l (uint8_t *value, int size)
{
  int i, j;
  uint32_t res = 0;

  if (value)
    for (i = size - 1, j = 0; i >= 0; i--, j++)
      res += (*(value + j) << i * 8);

  return res;
}

/*
 * Change a string in lower case.
 *
 * data         the string
 * return a pointer on this string
 */
static char *
lc (char *data)
{
  int i;

  if (data)
    for (i = 0; data[i] != '\0'; i++)
      data[i] = (char) tolower ((int) (unsigned char) data[i]);

  return data;
}

/*
 * Hex to dec convertion.
 *
 * Convert an integer in base 10 with the value shown in base 16, in an
 * integer with the value shown in base 10.
 *
 * val          the value
 * return the new integer
 */
static int
h2d (int val)
{
  char conv[16];
  int res = 0;

  snprintf (conv, sizeof (conv), "%X", val);
  res = atoi (conv);

  return res;
}

/*
 * Convert the year to y2k.
 *
 * y            year on two digits
 * return the year on four digits
 */
static inline int
y2k (int y)
{
  return ((y < FOSFAT_Y2K) ? 2000 : 1900) + y;
}

/*
 * Like strchr but limited to a length.
 */
static char *
my_strnchr (const char *s, size_t count, int c)
{
  size_t i;

  for (i = 0; i < count; i++)
    if (*(s + i) == (const char) c)
      return (char *) (s + i);

  return NULL;
}

static inline char *
my_strcasestr (const char *s1, const char *s2)
{
#ifdef _WIN32
  char *res, *_s1, *_s2;

  _s1 = strdup (s1);
  _s2 = strdup (s2);
  res = strstr (lc (_s1), lc (_s2));
  free (_s1);
  free (_s2);
  return res;
#else
  return strcasestr (s1, s2);
#endif /* !_WIN32 */
}

static inline char *
my_strndup (const char *src, size_t n)
{
#ifdef _WIN32
  char *dst;

  dst = malloc (n + 1);
  memcpy (dst, src, n);
  dst[n] = '\0';
  return dst;
#else
  return strndup (src, n);
#endif /* !_WIN32 */
}

/*
 * Enable or disable the internal FOS logger.
 */
void
fosfat_logger (int state)
{
  if (state)
    g_logger = 1;
  else
    g_logger = 0;
}

/*
 * Print function for the internal FOS logger.
 */
static void
foslog (foslog_t type, const char *msg, ...)
{
  va_list va;
  char log[32] = "[fosfat] ";

  if (!g_logger || !msg)
    return;

  va_start (va, msg);

  switch (type)
  {
  case FOSLOG_ERROR:
    strcat (log, "error");
    break;

  case FOSLOG_WARNING:
    strcat (log, "warning");
    break;

  case FOSLOG_NOTICE:
    strcat (log, "notice");
  }

  fprintf (stderr, "%s: ", log);
  vfprintf (stderr, msg, va);
  fprintf (stderr, "\n");

  va_end (va);
}

/*
 * Free a DATA file variable.
 *
 * It will be used only when a data is loaded to copy a file on the PC,
 * to free each block after each write.
 *
 * var          pointer on the data block
 */
static void
fosfat_free_data (fosfat_data_t *var)
{
  fosfat_data_t *d, *free_d;

  d = var;
  while (d)
  {
    free_d = d;
    d = d->next_data;
    free (free_d);
  }
}

/*
 * Free a BD file variable.
 *
 * It must be used only with a BD that is (or can be) use as the first
 * member of a linked list.
 * This function must be used after all fosfat_read_file()!
 *
 * var          pointer on the description block
 */
static void
fosfat_free_file (fosfat_bd_t *var)
{
  fosfat_bd_t *bd, *free_bd;

  bd = var;
  while (bd)
  {
    free_bd = bd;
    bd = bd->next_bd;
    free (free_bd);
  }
}

/*
 * Free a BD dir variable.
 *
 * It must be used only with a BD that is (or can be) use as the first
 * member of a linked list for a dir (with BL linked list into).
 * This function must be used after all fosfat_read_dir()!
 *
 * var          pointer on the description block
 */
static void
fosfat_free_dir (fosfat_bd_t *var)
{
  fosfat_bl_t *bl, *free_bl;
  fosfat_bd_t *bd, *free_bd;

  if (!var)
    return;
  bd = var;

  do
  {
    bl = bd->first_bl;

    /* Freed all BL */
    while (bl)
    {
      free_bl = bl;
      bl = bl->next_bl;
      free (free_bl);
    }

    /* And after, freed the BD */
    free_bd = bd;
    bd = bd->next_bd;
    if (free_bd)
      free (free_bd);
  }
  while (bd);
}

/*
 * Free a List Dir variable.
 *
 * This function must be used after all fosfat_list_dir()!
 *
 * var          pointer on the description block
 */
void
fosfat_free_listdir (fosfat_file_t *var)
{
  fosfat_file_t *ld, *free_ld;

  ld = var;
  while (ld)
  {
    free_ld = ld;
    ld = ld->next_file;
    free (free_ld);
  }
}

/*
 * Test if the file is a directory.
 *
 * This function read the ATT and return the value of the 12th bit,
 * mask 0x1000.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_isdir (fosfat_blf_t *file)
{
  return (file ? (c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_DIR) : 0);
}

/*
 * Test if the file is a soft-link.
 *
 * This function read the ATT and return the value of the 24th bit,
 * mask 0x1000000.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_islink (fosfat_blf_t *file)
{
  return (file ? (c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_LINK) : 0);
}

/*
 * Test if the file is visible.
 *
 * This function read the ATT and return the value of the 13th bit,
 * mask 0x2000.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_isvisible (fosfat_blf_t *file)
{
  return (file
          ? (c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_VISIBLE)
          : 0);
}

/*
 * Test if the file is 'open exclusif' and 'multiple'.
 *
 * This function read the ATT and return the value of the 1st or 2th bit,
 * mask 0x1 and 0x2.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_isopenexm (fosfat_blf_t *file)
{
  return (file
          ? (((c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_OPENEX))
             || ((c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_MULTIPLE)))
          : 0);
}

/*
 * Test if the file is encoded.
 *
 * This function read the ATT and return the value of the 17th bit,
 * mask 0x20000.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_isencoded (fosfat_blf_t *file)
{
  return (file
          ? (c2l (file->att, sizeof (file->att)) & FOSFAT_ATT_ENCODED)
          : 0);
}

/*
 * Test if the file is system (5 MSB bits).
 *
 * This function read the TYP and return the value of the 3-7th bits,
 * mask 0xF8.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_issystem (fosfat_blf_t *file)
{
  return (file ? ((file->typ & FOSFAT_TYPE_SYSTEM)) : 0);
}

/*
 * Test if the file is not deleted.
 *
 * file         pointer on the file (in the BL)
 * return a boolean (true for success)
 */
static inline int
fosfat_in_isnotdel (fosfat_blf_t *file)
{
  return file && strlen ((char *) file->name) > 0;
}

/*
 * Read a block defined by a type.
 *
 * This function read a block on the disk and return the structure in
 * function of the type chosen. Each type use 256 bytes, but the structures
 * are always bigger. The order of the attributes in each structures is
 * very important, because the informations from the device are just copied
 * directly without parsing.
 *
 * fosfat       handle
 * block        block position
 * type         type of this block (B_B0, B_BL, B_BD or B_DATA)
 * return a pointer on the new block or NULL if broken
 */
static void *
fosfat_read_b (fosfat_t *fosfat, uint32_t block, fosfat_type_t type)
{
#ifdef _WIN32
  size_t ssize, csector;
  int8_t *buffer;
#endif /* _WIN32 */

  if (!fosfat || !fosfat->dev)
    return NULL;

#ifdef _WIN32
  /* sector seems to be always 512 with Window$ */
  ssize = w32disk_sectorsize (fosfat->dev);

  csector = (size_t) FOSFAT_BLK / ssize;
  if (!csector)
    csector = 1;

  buffer = calloc (1, csector * ssize);
  if (!buffer)
    return NULL;
#else
  /* Move the pointer on the block */
  if (fseek (fosfat->dev, blk2add (block, fosfat->fosboot), SEEK_SET))
    return NULL;
#endif /* !_WIN32 */

  switch (type)
  {
  case B_B0:
  {
    fosfat_b0_t *blk;

    blk = malloc (sizeof (fosfat_b0_t));
    if (!blk)
      break;

#ifdef _WIN32
    if (w32disk_readsectors (fosfat->dev, buffer,
                             blk2sector (block, fosfat->fosboot), csector))
    {
      memcpy (blk, buffer + sec_offset (block, fosfat->fosboot),
              (size_t) FOSFAT_BLK);
      free (buffer);
#else
    if (fread ((fosfat_b0_t *) blk, 1, (size_t) FOSFAT_BLK, fosfat->dev)
        == (size_t) FOSFAT_BLK)
    {
#endif /* !_WIN32 */
      return blk;
    }
    free (blk);
    break;
  }

  case B_BL:
  {
    fosfat_bl_t *blk;

    blk = malloc (sizeof (fosfat_bl_t));
    if (!blk)
      break;

#ifdef _WIN32
    if (w32disk_readsectors (fosfat->dev, buffer,
                             blk2sector (block, fosfat->fosboot), csector))
    {
      memcpy (blk, buffer + sec_offset (block, fosfat->fosboot),
              (size_t) FOSFAT_BLK);
      free (buffer);
#else
    if (fread ((fosfat_bl_t *) blk, 1, (size_t) FOSFAT_BLK, fosfat->dev)
        == (size_t)FOSFAT_BLK)
    {
#endif /* !_WIN32 */
      blk->next_bl = NULL;

      /* Check the CHK value */
      if (!fosfat->foschk)
        fosfat->foschk = c2l (blk->chk, sizeof (blk->chk));
      if (fosfat->foschk == c2l (blk->chk, sizeof (blk->chk)))
        return blk;

      foslog (FOSLOG_ERROR, "bad FOSCHK for this BL (block:%li)", block);
    }
    free (blk);
    break;
  }

  case B_BD:
  {
    fosfat_bd_t *blk;

    blk = malloc (sizeof (fosfat_bd_t));
    if (!blk)
      break;

#ifdef _WIN32
    if (w32disk_readsectors (fosfat->dev, buffer,
                             blk2sector (block, fosfat->fosboot), csector))
    {
      memcpy (blk, buffer + sec_offset (block, fosfat->fosboot),
              (size_t) FOSFAT_BLK);
      free (buffer);
#else
    if (fread ((fosfat_bd_t *) blk, 1, (size_t) FOSFAT_BLK, fosfat->dev)
        == (size_t) FOSFAT_BLK)
    {
#endif /* !_WIN32 */
      blk->next_bd = NULL;
      blk->first_bl = NULL;

      /* Check the CHK value */
      if (!fosfat->foschk)
        fosfat->foschk = c2l (blk->chk, sizeof (blk->chk));
      if (fosfat->foschk == c2l (blk->chk, sizeof (blk->chk)))
        return blk;

      foslog (FOSLOG_ERROR, "bad FOSCHK for this BD (block:%li)", block);
    }
    free (blk);
    break;
  }

  case B_DATA:
  {
    fosfat_data_t *blk;

    blk = malloc (sizeof (fosfat_data_t));
    if (!blk)
      break;

#ifdef _WIN32
    if (w32disk_readsectors (fosfat->dev, buffer,
                             blk2sector (block, fosfat->fosboot), csector))
    {
      memcpy (blk, buffer + sec_offset (block, fosfat->fosboot),
              (size_t) FOSFAT_BLK);
      free (buffer);
#else
    if (fread ((fosfat_data_t *) blk, 1, (size_t) FOSFAT_BLK, fosfat->dev)
        == (size_t) FOSFAT_BLK)
    {
#endif /* !_WIN32 */
      blk->next_data = NULL;
      return blk;
    }
    free (blk);
    break;
  }
  }

#ifdef _WIN32
  if (buffer)
    free (buffer);
#endif /* _WIN32 */

  return NULL;
}

/*
 * Read the first useful block (0).
 *
 * This block contents some informations on the disk. But no information
 * are critical to read the file list.
 *
 * fosfat       handle
 * block        block position
 * return the block0 or NULL if broken
 */
static inline fosfat_b0_t *
fosfat_read_b0 (fosfat_t *fosfat, uint32_t block)
{
  return (fosfat
          ? ((fosfat_b0_t *) fosfat_read_b (fosfat, block, B_B0))
          : NULL);
}

/*
 * Read data block.
 *
 * This block contents only a char table of 256 bytes for the raw data.
 *
 * fosfat       handle
 * block        block position
 * return the data or NULL if broken
 */
static inline fosfat_data_t *
fosfat_read_d (fosfat_t *fosfat, uint32_t block)
{
  return (fosfat
          ? ((fosfat_data_t *) fosfat_read_b (fosfat, block, B_DATA))
          : NULL);
}

/*
 * Read a Description Block (BD).
 *
 * fosfat       handle
 * block        block position
 * return the BD or NULL if broken
 */
static inline fosfat_bd_t *
fosfat_read_bd (fosfat_t *fosfat, uint32_t block)
{
  return (fosfat
          ? ((fosfat_bd_t *) fosfat_read_b (fosfat, block, B_BD))
          : NULL);
}

/*
 * Read a Block List (BL).
 *
 * fosfat       handle
 * block        block position
 * return the BL or NULL if broken
 */
static inline fosfat_bl_t *
fosfat_read_bl (fosfat_t *fosfat, uint32_t block)
{
  return (fosfat
          ? ((fosfat_bl_t *) fosfat_read_b (fosfat, block, B_BL))
          : NULL);
}

/*
 * Read data of some blocks and create linked list if necessary.
 *
 * When a directory or a file is read, the content is shown as data. But
 * for a directory, really it is a BL. This function create the linked list
 * for a DATA block or a BL for a number of consecutive blocks.
 *
 * fosfat       handle
 * block        the first block (start) for the linked list
 * nbs          number of consecutive blocks
 * type         type of this block (B_B0, B_BL, B_BD or B_DATA)
 * return the first block of the linked list created
 */
static void *
fosfat_read_data (fosfat_t *fosfat, uint32_t block,
                  uint8_t nbs, fosfat_type_t type)
{
  if (!fosfat)
    return NULL;

  switch (type)
  {
  case B_BL:
  {
    int i;
    fosfat_bl_t *block_list, *first_bl;

    first_bl = fosfat_read_bl (fosfat, block);
    if (!first_bl)
      break;

    block_list = first_bl;
    block_list->pt = block;

    for (i = 1; block_list && i < nbs; i++)
    {
      block_list->next_bl = fosfat_read_bl (fosfat, block + (uint32_t) i);
      block_list = block_list->next_bl;

      if (block_list)
        block_list->pt = block + (uint32_t) i;
    }
    return first_bl;
  }

  case B_DATA:
  {
    int i;
    fosfat_data_t *block_data, *first_data;

    first_data = fosfat_read_d (fosfat, block);
    if (!first_data)
      break;

    block_data = first_data;

    for (i = 1; block_data && i < nbs; i++)
    {
      block_data->next_data = fosfat_read_d (fosfat, block + (uint32_t) i);
      block_data = block_data->next_data;
    }
    return first_data;
  }

  default:
    break;
  }

  return NULL;
}

/*
 * Read a file description.
 *
 * A linked list is created to found all BD. And each BD have a linked list
 * to found all DATA.
 *
 * fosfat       handle
 * block        the file BD position
 * return the first BD of the linked list
 */
static fosfat_bd_t *
fosfat_read_file (fosfat_t *fosfat, uint32_t block)
{
  uint32_t next;
  fosfat_bd_t *file_desc, *first_bd;

  if (!fosfat)
    return NULL;

  file_desc = fosfat_read_bd (fosfat, block);
  if (!file_desc)
    return NULL;

  file_desc->next_bd = NULL;
  file_desc->first_bl = NULL;     /* Useless in this case */
  first_bd = file_desc;

  /* Go to the next BD if exists (create the linked list for BD) */
  while (file_desc && file_desc->next
         && (next = c2l (file_desc->next, sizeof (file_desc->next))))
  {
    file_desc->next_bd = fosfat_read_bd (fosfat, next);
    file_desc = file_desc->next_bd;
  }

  /* End of the BD linked list */
  if (file_desc)
    file_desc->next_bd = NULL;

  return first_bd;
}

/*
 * Get a file and put this in a location on the PC or in a buffer.
 *
 * This function read all BD->DATA of a file BD, and write the data in a
 * new file on your disk. An output variable can be used to print the
 * current size for each PTS. The properties like "Creation Date" are not
 * saved in the new file. All Linux file system are the same attributes for
 * them files. And for example, ext2/3 have no "Creation Date".
 * The data can be saved in a buffer if the flag is used with an offset,
 * size and a buffer.
 *
 * fosfat       handle
 * file         file description block
 * dst          destination on your PC
 * output       TRUE to print the size
 * flag         use the optional arguments or not
 * ...          offset, size and buffer
 * return a boolean (true for success)
 */
static int
fosfat_get (fosfat_t *fosfat, fosfat_bd_t *file,
            const char *dst, int output, int flag, ...)
{
  va_list pp;
  int op_offset = 0;
  int op_size = 0;
  uint8_t *op_buffer = NULL;
  int op_inoff = 0;
  unsigned int i;
  int res = 1;
  size_t check_last;
  size_t size = 0;
  FILE *f_dst = NULL;
  fosfat_data_t *file_d, *first_d;

  va_start (pp, flag);
  if (flag)
  {
    op_offset = va_arg (pp, int);
    op_size = va_arg (pp, int);
    op_buffer = va_arg (pp, uint8_t *);
  }
  va_end(pp);

  if (!fosfat || !file)
    return 0;

  if (!flag)
  {
    f_dst = fopen (dst, "w");
    if (!f_dst)
      return 0;
  }

  /* Loop for all BD */
  do
  {
    /* Loop for all pointers */
    for (i = 0; res && i < c2l (file->npt, sizeof (file->npt)); i++)
    {
      file_d = fosfat_read_data (fosfat, c2l (file->pts[i],
                                 sizeof (file->pts[i])), file->nbs[i], B_DATA);
      if (!file_d)
      {
        res = 0;
        break;
      }

      first_d = file_d;

      /* Loop for all data blocks */
      do
      {
        check_last = (i == c2l (file->npt, sizeof (file->npt)) - 1
                      && !file_d->next_data)
                      ? (size_t) c2l (file->lst, sizeof (file->lst))
                      : (size_t) FOSFAT_BLK;

        /* When the result is written in a file */
        if (!flag)
        {
          /* Write the block */
          if (fwrite ((uint8_t *) file_d->data, 1, check_last, f_dst)
              != check_last)
            res = 0;
        }
        /* When the result is written in RAM (offset and size) */
        else if (op_inoff || ((unsigned) op_offset >= size &&
                              (unsigned) op_offset < size + check_last))
        {
          int first_pts = op_offset + op_inoff - (signed) size;
          int cp = (op_size - op_inoff > (signed) check_last - first_pts)
                   ? ((signed) check_last - first_pts)
                   : (op_size - op_inoff);

          /* Copy the tranche */
          memcpy (op_buffer + op_inoff, file_d->data + first_pts, cp);
          op_inoff += check_last - first_pts;

          if (op_size <= op_inoff)
            res = 0;
        }
        size += check_last;
      }
      while (res && file_d->next_data && (file_d = file_d->next_data));

      /* Freed all data */
      fosfat_free_data (first_d);

      if (res && output)
        fprintf (stdout, " %i bytes\n", (int) size);
    }
  }
  while (res && file->next_bd && (file = file->next_bd));

  if (!flag)
  {
    fclose (f_dst);

    /* If fails then remove the incomplete file */
    if (!res)
      remove (dst);
  }

  return res;
}

/*
 * Read a complete .DIR (or SYS_LIST).
 *
 * A linked list is created to found all files. The first BD is returned.
 * You can use this function to read all SYS_LIST, even in each folder. But
 * the result will be always the same (recursive). Warning to don't create an
 * infinite loop /!\
 *
 * fosfat       handle
 * block        DIR (or SYS_LIST) BD position
 * return the first BD of the linked list
 */
static fosfat_bd_t *
fosfat_read_dir (fosfat_t *fosfat, uint32_t block)
{
  unsigned int i;
  uint32_t next;
  fosfat_bd_t *dir_desc, *first_bd;
  fosfat_bl_t *dir_list;

  if (!fosfat)
    return NULL;

  dir_desc = fosfat_read_bd (fosfat, block);
  if (!dir_desc)
  {
    foslog (FOSLOG_ERROR, "directory to block %i cannot be read", block);
    return NULL;
  }

  dir_desc->next_bd = NULL;
  dir_desc->first_bl = NULL;
  first_bd = dir_desc;

  do
  {
    /* Get the first pointer */
    dir_desc->first_bl =
      fosfat_read_data (fosfat, c2l(dir_desc->pts[0],
                        sizeof (dir_desc->pts[0])), dir_desc->nbs[0], B_BL);
    dir_list = dir_desc->first_bl;

    /* Go to the last BL */
    while (dir_list && dir_list->next_bl)
      dir_list = dir_list->next_bl;

    /* Loop all others pointers */
    for (i = 1; dir_list && i < c2l (dir_desc->npt,
                                     sizeof (dir_desc->npt)); i++)
    {
      dir_list->next_bl =
        fosfat_read_data (fosfat, c2l (dir_desc->pts[i],
                          sizeof (dir_desc->pts[i])), dir_desc->nbs[i], B_BL);
      dir_list = dir_list->next_bl;

      /* Go to the last BL */
      while (dir_list && dir_list->next_bl)
        dir_list = dir_list->next_bl;
    }

    /* End of the BL linked list */
    if (dir_list)
      dir_list->next_bl = NULL;
  } /* Go to the next BD if exists (create the linked list for BD) */
  while ((next = c2l (dir_desc->next, sizeof (dir_desc->next)))
         && (dir_desc->next_bd = fosfat_read_bd (fosfat, next))
         && (dir_desc = dir_desc->next_bd));

  /* End of the BD linked list */
  dir_desc->next_bd = NULL;

  return first_bd;
}

/*
 * Test if two names are the same or not.
 *
 * This function must be used only for particular purpose, because that will
 * no test if it is a directory or not, but only the names. That is very
 * useful for fosfat_search_incache().
 *
 * realname     the name of the file in the FOS
 * searchname   the name in the path
 * return true if it seems to be a dir name
 */
static inline int
fosfat_isdirname (const char *realname, const char *searchname)
{
      /* Test with a name as foobar.dir */
  if ((
        my_strcasestr (realname, ".dir")
        && !strncasecmp (realname, searchname, strlen (realname) - 4)
        && strlen (searchname) == strlen (realname) - 4
      ) ||
      /* Test with a name as sys_list (without .dir) */
      (
        !strncasecmp (realname, searchname, strlen (realname))
        && strlen (searchname) == strlen (realname)
      ))
    return 1;

  return 0;
}

/*
 * Search a BD or a BLF from a location in the cache.
 *
 * The location must not be bigger than MAX_SPLIT /!\
 *
 * fosfat       handle
 * location     path to found the BD/BLF (foo/bar/file)
 * type         S_BD or S_BLF
 * return the BD, BLF or NULL is nothing found
 */
static void *
fosfat_search_incache (fosfat_t *fosfat, const char *location,
                       fosfat_search_t type)
{
  int i, nb = 0, ontop = 1, isdir = 0;
  char *tmp, *path, *name = NULL;
  char dir[MAX_SPLIT][FOSFAT_NAMELGT];
  cachelist_t *list;
  uint32_t bd_block = 0, bl_block = 0;
  fosfat_bl_t *bl_found = NULL;
  fosfat_blf_t *blf_found = NULL;
  fosfat_bd_t *bd_found = NULL;

  if (!fosfat || !location)
    return NULL;

  list = fosfat->cachelist;
  path = strdup (location);

  /* Split the path into a table */
  if ((tmp = strtok ((char *) path, "/")))
  {
    snprintf (dir[nb], sizeof (dir[nb]), "%s", tmp);
    while ((tmp = strtok (NULL, "/")) && nb < MAX_SPLIT - 1)
      snprintf (dir[++nb], sizeof (dir[nb]), "%s", tmp);
  }
  else
    snprintf (dir[nb], sizeof (dir[nb]), "%s", path);
  nb++;

  /* Loop for all directories in the path */
  for (i = 0; list && i < nb; i++)
    do
    {
      ontop = 1;

      /* test if the file is deleted or not */
      if (!fosfat->viewdel && list->isdel)
        continue;

      /* Test if it is a directory */
      if (list->isdir && fosfat_isdirname (list->name, dir[i]))
      {
        bd_block = list->bd;
        bl_block = list->bl;

        if (name)
          free (name);

        name = strdup (list->name);

        /* Go to the next level */
        list = list->sub;
        ontop = 0;
        isdir = 1;
      }
      /* Test if it is a file or a soft-link */
      else if (!list->isdir
               && (!strcasecmp (list->name, dir[i])
                   || (list->islink
                       && fosfat_isdirname (list->name, dir[i]))
                  )
              )
      {
        bd_block = list->bd;
        bl_block = list->bl;

        if (name)
          free (name);

        name = strdup (list->name);
        ontop = 0;
        isdir = 0;
      }
    }
    while (ontop && list && (list = list->next));

  free (path);

  if (ontop)
  {
    if (name)
      free (name);
    return NULL;
  }

  switch (type)
  {
  case S_BD:
  {
    if (name)
      free (name);

    if (isdir)
      bd_found = fosfat_read_dir (fosfat, bd_block);
    else
      bd_found = fosfat_read_file (fosfat, bd_block);

    return bd_found;
  }

  case S_BLF:
  {
    bl_found = fosfat_read_bl (fosfat, bl_block);

    for (i = 0; bl_found && i < FOSFAT_NBL; i++)
    {
      char name_r[FOSFAT_NAMELGT];
      snprintf (name_r, FOSFAT_NAMELGT, "%s",
                (char) bl_found->file[i].name[0]
                ? (char *) bl_found->file[i].name
                : (char *) bl_found->file[i].name + 1);

      if (!strcasecmp (name_r, name))
      {
        free (name);

        blf_found = malloc (sizeof (fosfat_blf_t));
        if (blf_found)
        {
          memcpy (blf_found, &bl_found->file[i], sizeof (*blf_found));
          free (bl_found);

          return blf_found;
        }
      }
    }

    if (bl_found)
      free (bl_found);
  }
  }

  if (name)
    free (name);

  return NULL;
}

/*
 * Search a BD or a BLF from a location in the first SYS_LIST.
 *
 * That uses fosfat_search_incache().
 *
 * fosfat       handle
 * location     path to found the BD (foo/bar/file)
 * type         S_BD or S_BLF
 * return the BD, BLF or NULL is nothing found
 */
static void *
fosfat_search_insys (fosfat_t *fosfat, const char *location,
                     fosfat_search_t type)
{
  fosfat_bd_t *syslist;
  void *search = NULL;

  if (!fosfat || !location)
    return NULL;

  if (type == S_BD && (*location == '\0' || !strcmp (location, "/")))
  {
    syslist = fosfat_read_dir (fosfat, FOSFAT_SYSLIST);
    return syslist;
  }

  search = fosfat_search_incache (fosfat, location, type);
  if (search)
    return search;

  foslog (FOSLOG_WARNING, "file or directory \"%s\" not found", location);

  return NULL;
}

/*
 * Test if the file is a directory.
 *
 * This function uses a string location.
 *
 * fosfat       handle
 * location     file in the path
 * return a boolean (true for success)
 */
int
fosfat_isdir (fosfat_t *fosfat, const char *location)
{
  if (!fosfat || !location)
    return 0;

  if (!strcmp (location, "/"))
    return 1;

  FOSFAT_IS_DIR (fosfat, location)
}

/*
 * Test if the file is a link.
 *
 * This function uses a string location.
 *
 * fosfat       handle
 * location     file in the path
 * return a boolean (true for success)
 */
int
fosfat_islink (fosfat_t *fosfat, const char *location)
{
  if (!fosfat || !location)
    return 0;

  FOSFAT_IS_LINK (fosfat, location)
}

/*
 * Test if the file is visible.
 *
 * This function uses a string location.
 *
 * fosfat       handle
 * location     file in the path
 * return a boolean (true for success)
 */
int
fosfat_isvisible (fosfat_t *fosfat, const char *location)
{
  if (!fosfat || !location)
    return 0;

  FOSFAT_IS_VISIBLE (fosfat, location)
}

/*
 * Test if the file is encoded.
 *
 * This function uses a string location.
 *
 * fosfat       handle
 * location     file in the path
 * return a boolean (true for success)
 */
int
fosfat_isencoded (fosfat_t *fosfat, const char *location)
{
  if (!fosfat || !location)
    return 0;

  FOSFAT_IS_ENCODED (fosfat, location)
}

/*
 * Test if the file is valid.
 *
 * This function uses a string location.
 *
 * fosfat       handle
 * location     file in the path
 * return a boolean (true for success)
 */
int
fosfat_isopenexm (fosfat_t *fosfat, const char *location)
{
  if (!fosfat || !location)
    return 0;

  FOSFAT_IS_OPENEXM (fosfat, location)
}

/*
 * Read the data for get the target path of a symlink.
 *
 * A Smaky's symlink is a simple file with the target's path in the data.
 * This function read the data and extract the right path.
 *
 * fosfat       handle
 * file         BD of the symlink
 * return the target path
 */
static char *
fosfat_get_link (fosfat_t *fosfat, fosfat_bd_t *file)
{
  fosfat_data_t *data;
  char *path = NULL;
  char *start, *it;

  data = fosfat_read_data (fosfat, c2l (file->pts[0], sizeof (file->pts[0])),
                           file->nbs[0], B_DATA);
  if (!data)
    return NULL;

  start = (char *) data->data + 3;

  while ((it = my_strnchr (start, strlen (start), ':')))
    *it = '/';

  it = strrchr (start, '/');
  if (it)
    *it = '\0';

  path = strdup (start);
  if (path)
    lc (path);

  fosfat_free_data (data);

  return path;
}

/*
 * Get the target of a symlink.
 *
 * This function is high level.
 *
 * fosfat       handle
 * location     file in the path
 * return the target path
 */
char *
fosfat_symlink (fosfat_t *fosfat, const char *location)
{
  fosfat_bd_t *entry;
  char *link = NULL;

  if (!fosfat || !location)
    return NULL;

  entry = fosfat_search_insys (fosfat, location, S_BD);
  if (entry)
  {
    link = fosfat_get_link (fosfat, entry);
    free (entry);
  }

  if (!link)
    foslog (FOSLOG_ERROR, "target of symlink \"%s\" not found", location);

  return link;
}

/*
 * Return all informations on one file.
 *
 * This function uses the BLF and get only useful attributes.
 *
 * file         BLF on the file
 * return the stat
 */
static fosfat_file_t *
fosfat_stat (fosfat_blf_t *file)
{
  fosfat_file_t *stat = NULL;

  if (!file)
    return NULL;

  stat = malloc (sizeof (fosfat_file_t));
  if (!stat)
    return NULL;

  /* Size (bytes) */
  stat->size = c2l (file->lgf, sizeof (file->lgf));

  /* Attributes (field bits) */
  stat->att.isdir     = !!fosfat_in_isdir (file);
  stat->att.isvisible = !!fosfat_in_isvisible (file);
  stat->att.isencoded = !!fosfat_in_isencoded (file);
  stat->att.islink    = !!fosfat_in_islink (file);
  stat->att.isdel     = !fosfat_in_isnotdel (file);

  /* Creation date */
  stat->time_c.year   = y2k (h2d (file->cd[2]));
  stat->time_c.month  = h2d (file->cd[1]);
  stat->time_c.day    = h2d (file->cd[0]);
  stat->time_c.hour   = h2d (file->ch[0]);
  stat->time_c.minute = h2d (file->ch[1]);
  stat->time_c.second = h2d (file->ch[2]);
  /* Writing date */
  stat->time_w.year   = y2k (h2d (file->wd[2]));
  stat->time_w.month  = h2d (file->wd[1]);
  stat->time_w.day    = h2d (file->wd[0]);
  stat->time_w.hour   = h2d (file->wh[0]);
  stat->time_w.minute = h2d (file->wh[1]);
  stat->time_w.second = h2d (file->wh[2]);
  /* Use date */
  stat->time_r.year   = y2k (h2d (file->rd[2]));
  stat->time_r.month  = h2d (file->rd[1]);
  stat->time_r.day    = h2d (file->rd[0]);
  stat->time_r.hour   = h2d (file->rh[0]);
  stat->time_r.minute = h2d (file->rh[1]);
  stat->time_r.second = h2d (file->rh[2]);

  /* Name */
  if (stat->att.isdel)
  {
    strncpy (stat->name, (char *) file->name + 1, sizeof (stat->name));
    stat->name[15] = '\0';
  }
  else
    strncpy (stat->name, (char *) file->name, sizeof (stat->name));
  lc (stat->name);

  stat->next_file = NULL;

  return stat;
}

/*
 * Return all informations on one file.
 *
 * This function is high level.
 *
 * fosfat       handle
 * location     file in the path
 * return the stat
 */
fosfat_file_t *
fosfat_get_stat (fosfat_t *fosfat, const char *location)
{
  fosfat_blf_t *entry;
  fosfat_file_t *stat = NULL;

  if (!fosfat || !location)
    return NULL;

  entry = fosfat_search_insys (fosfat, location, S_BLF);
  if (entry)
  {
    stat = fosfat_stat (entry);
    free (entry);
  }

  if (!stat)
    foslog (FOSLOG_WARNING, "stat of \"%s\" not found", location);

  return stat;
}

/*
 * Return a linked list with all files of a directory.
 *
 * This function is high level.
 *
 * fosfat       handle
 * location     directory in the path
 * return the linked list
 */
fosfat_file_t *
fosfat_list_dir (fosfat_t *fosfat, const char *location)
{
  int i;
  fosfat_bd_t *dir;
  fosfat_bl_t *files;
  fosfat_file_t *sysdir = NULL;
  fosfat_file_t *listdir = NULL;
  fosfat_file_t *firstfile = NULL;
  fosfat_file_t *res = NULL;

  if (!fosfat || !location)
    return NULL;

  if (!fosfat_isdir (fosfat, location))
  {
    foslog (FOSLOG_WARNING, "directory \"%s\" is unknown", location);
    return NULL;
  }

  dir = fosfat_search_insys (fosfat, location, S_BD);
  if (!dir)
    return NULL;

  files = dir->first_bl;
  if (!files)
  {
    fosfat_free_dir (dir);
    return NULL;
  }

  do
  {
    /* Check all files in the BL */
    for (i = 0; i < FOSFAT_NBL; i++)
    {
      if (fosfat_in_issystem (&files->file[i]))
      {
        if (!strcasecmp ((char *) files->file[i].name, "sys_list"))
        {
          sysdir = fosfat_stat (&files->file[i]);
          strcpy (sysdir->name, "..dir");
        }
        continue;
      }

      if (!fosfat_in_isopenexm (&files->file[i]))
        continue;

      if (fosfat->viewdel || fosfat_in_isnotdel (&files->file[i]))
      {
        /* Complete the linked list with all files */
        if (listdir)
        {
          listdir->next_file = fosfat_stat (&files->file[i]);
          listdir = listdir->next_file;
        }
        else
        {
          firstfile = fosfat_stat (&files->file[i]);
          listdir = firstfile;
        }
      }
    }
    files = files->next_bl;
  }
  while (files);

  fosfat_free_dir (dir);

  if (sysdir)
  {
    sysdir->next_file = firstfile;
    res = sysdir;
  }
  else
    res = firstfile;

  if (res)
    foslog (FOSLOG_NOTICE, "directory \"%s\" is read successfully", location);

  return res;
}

/*
 * Get a file and put this in a location on the PC.
 *
 * This function create a copy from src to dst. An output variable can be
 * used to print the current size for each PTS.
 *
 * fosfat       handle
 * src          source on the Smaky disk
 * dst          destination on your PC
 * output       TRUE to print the size
 * return a boolean (true for success)
 */
int
fosfat_get_file (fosfat_t *fosfat, const char *src,
                 const char *dst, int output)
{
  int res = 0;
  fosfat_blf_t *file;
  fosfat_bd_t *file2;

  if (!fosfat || !src || !dst)
    return 0;

  file = fosfat_search_insys (fosfat, src, S_BLF);
  if (file)
  {
    if (!fosfat_in_isdir (file))
    {
      file2 = fosfat_read_file (fosfat, c2l (file->pt, sizeof (file->pt)));

      if (file2 && fosfat_get (fosfat, file2, dst, output, 0))
        res = 1;

      fosfat_free_file (file2);
    }

    free (file);
  }

  if (!res)
    foslog (FOSLOG_WARNING, "file \"%s\" cannot be copied", src);
  else
    foslog (FOSLOG_NOTICE, "get file \"%s\" and save to \"%s\"", src, dst);

  return res;
}

/*
 * Get a buffer from a file in the FOS.
 *
 * The buffer can be selected with an offset in the file and with a size.
 *
 * fosfat       handle
 * path         source on the Smaky disk
 * offset       start byte in the file
 * size         length of the buffer
 * return the buffer with the data
 */
uint8_t *
fosfat_get_buffer (fosfat_t *fosfat, const char *path, int offset, int size)
{
  uint8_t *buffer = NULL;
  fosfat_blf_t *file;
  fosfat_bd_t *file2;

  if (!fosfat || !path)
    return NULL;

  file = fosfat_search_insys (fosfat, path, S_BLF);
  if (file && !fosfat_in_isdir (file))
  {
    buffer = calloc (1, size);

    if (buffer)
    {
      file2 = fosfat_read_file (fosfat, c2l (file->pt, sizeof (file->pt)));

      if (file2)
        fosfat_get (fosfat, file2, NULL, 0, 1, offset, size, buffer);
      else
      {
        free (buffer);
        buffer = NULL;
      }

      free (file);

      if (file2)
        fosfat_free_file (file2);
    }
  }

  if (!buffer)
    foslog (FOSLOG_ERROR, "data (offset:%i size:%i) of \"%s\" not read",
            offset, size, path);
  else
    foslog (FOSLOG_NOTICE, "data (offset:%i size:%i) of \"%s\" correctly read",
            offset, size, path);

  return buffer;
}

/*
 * Get the name of a disk.
 *
 * fosfat       handle
 * return the name
 */
char *
fosfat_diskname (fosfat_t *fosfat)
{
  fosfat_b0_t *block0;
  char *name = NULL;

  if (!fosfat)
    return NULL;

  block0 = fosfat_read_b0 (fosfat, FOSFAT_BLOCK0);
  if (block0)
  {
    name = strdup ((char *) block0->nlo);
    free (block0);
  }

  if (!name)
    foslog (FOSLOG_ERROR, "the disk name cannot be found");
  else
    foslog (FOSLOG_NOTICE, "disk name found (%s)", name);

  return name;
}

/*
 * Put file information in the cache list structure.
 *
 * file         BLF element in the BL
 * bl           BL block's number
 * return the cache for a file
 */
static cachelist_t *
fosfat_cache_file (fosfat_blf_t *file, uint32_t bl)
{
  cachelist_t *cachefile = NULL;

  if (!file)
    return NULL;

  cachefile = malloc (sizeof (cachelist_t));
  if (!cachefile)
    return NULL;

  cachefile->next   = NULL;
  cachefile->sub    = NULL;
  cachefile->isdir  = !!fosfat_in_isdir (file);
  cachefile->islink = !!fosfat_in_islink (file);

  /* if the first char is NULL, then the file is deleted */
  if ((char) file->name[0] == '\0')
  {
    cachefile->isdel = 1;
    cachefile->name =
      my_strndup ((char *) file->name + 1, sizeof (file->name - 1));
  }
  else
  {
    cachefile->isdel = 0;
    cachefile->name = strdup ((char *) file->name);
  }

  cachefile->bl = bl;
  cachefile->bd = c2l (file->pt, sizeof (file->pt));

  return cachefile;
}

/*
 * List all files on the disk to fill the global cache list.
 *
 * This function is recursive!
 *
 * fosfat       handle
 * pt           block's number of the BD
 * return the first element of the cache list.
 */
static cachelist_t *
fosfat_cache_dir (fosfat_t *fosfat, uint32_t pt)
{
  int i;
  fosfat_bd_t *dir = NULL;
  fosfat_bl_t *files;
  cachelist_t *firstfile = NULL;
  cachelist_t *list = NULL;

  if (!fosfat)
    return NULL;

  dir = fosfat_read_dir (fosfat, pt);
  if (!dir)
    return NULL;

  files = dir->first_bl;
  if (!files)
  {
    fosfat_free_dir (dir);
    return NULL;
  }

  do
  {
    /* Check all files in the BL */
    for (i = 0; i < FOSFAT_NBL; i++)
    {
      if (!fosfat_in_isopenexm (&files->file[i]))
        continue;

      if (fosfat->viewdel || fosfat_in_isnotdel(&files->file[i]))
      {
        /* Complete the linked list with all files */
        if (list)
        {
          list->next = fosfat_cache_file (&files->file[i], files->pt);
          list = list->next;
        }
        else
        {
          firstfile = fosfat_cache_file (&files->file[i], files->pt);
          list = firstfile;
        }

        /* If the file is a directory, then do a recursive cache */
        if (list && fosfat_in_isdir (&files->file[i])
            && !fosfat_in_issystem (&files->file[i]))
          list->sub = fosfat_cache_dir (fosfat, list->bd);
      }
    }
    files = files->next_bl;
  }
  while (files);

  fosfat_free_dir (dir);

  if (!firstfile)
    foslog (FOSLOG_ERROR, "cache to block %i not correctly loaded", pt);

  return firstfile;
}

/*
 * Unload the cache.
 *
 * This function releases all the cache when the device is closed.
 *
 * cache        the first element of the cache list
 */
static void
fosfat_cache_unloader (cachelist_t *cache)
{
  cachelist_t *it, *tofree;

  it = cache;
  while (it)
  {
    if (it->sub)
      fosfat_cache_unloader (it->sub);

    tofree = it;
    it = it->next;
    free (tofree->name);
    free (tofree);
  }
}

/*
 * Auto detection of the FOSBOOT length.
 *
 * This function detects if the device is an hard disk or a floppy disk.
 * There is no explicit information for know that, then the CHK is tested
 * between the SYS_LIST and the first BL and a test to know if the BL's
 * pointer in the BD is right.
 *
 * fosfat       handle
 * return the type of disk or FOSFAT_ED
 */
static fosfat_disk_t
fosfat_diskauto (fosfat_t *fosfat)
{
  int i, loop = 1;
  int fboot = -1;
  fosfat_disk_t res = FOSFAT_ED;
  fosfat_bd_t *sys_list;
  fosfat_bl_t *first_bl;

  if (!fosfat)
    return FOSFAT_ED;

  fosfat->fosboot = FOSBOOT_FD;

  /* for i = 0, test with FD and when i = 1, test for HD */
  for (i = 0; loop && i < 2; i++)
  {
    sys_list = fosfat_read_bd (fosfat, FOSFAT_SYSLIST);
    fosfat->foschk = 0;
    first_bl = fosfat_read_bl (fosfat, FOSFAT_SYSLIST + 1);
    fosfat->foschk = 0;

    if (sys_list && first_bl
        && !strncmp ((char *) sys_list->chk, (char *) first_bl->chk,
                     sizeof (sys_list->chk))
        && c2l (*sys_list->pts, sizeof (*sys_list->pts))
        == FOSFAT_SYSLIST + 1)
    {
      loop = 0;
      fboot = fosfat->fosboot;
    }

    if (sys_list)
      free (sys_list);
    if (first_bl)
      free (first_bl);

    if (loop && !i)
      fosfat->fosboot = FOSBOOT_HD;
  }

  /* Select the right fosboot */
  switch (fboot)
  {
  case FOSBOOT_FD:
    res = FOSFAT_FD;
    break;

  case FOSBOOT_HD:
    res = FOSFAT_HD;
    break;

  default:
    res = FOSFAT_ED;
  }

  /* Restore the fosboot */
  fosfat->fosboot = -1;

  return res;
}

/*
 * Open the device.
 *
 * That hides the fopen processing. A device can be read like a file.
 * But for Win32, the w32disk library is used for Win9x and WinNT low
 * level access on the disk.
 *
 * dev          the device name
 * disk         disk type
 * flag         F_UNDELETE or 0 for nothing
 * return the device handle
 */
fosfat_t *
fosfat_open (const char *dev, fosfat_disk_t disk, unsigned int flag)
{
  fosfat_disk_t fboot;
  fosfat_t *fosfat = NULL;

  if (!dev)
    return NULL;

  fosfat = malloc (sizeof (fosfat_t));
  if (!fosfat)
    return NULL;

  fosfat->fosboot   = -1;
  fosfat->foschk    = 0;
  fosfat->viewdel   = (flag & F_UNDELETE) == F_UNDELETE;
  fosfat->cachelist = NULL;

#ifdef _WIN32
  fosfat->dev = w32disk_new (*dev - 'a');
#else
  fosfat->dev = fopen (dev, "r");
#endif /* !_WIN32 */
  if (!fosfat->dev)
    goto err_dev;

  /* Open the device */
  foslog (FOSLOG_NOTICE, "device is opening ...");

  if (disk == FOSFAT_AD)
  {
    foslog (FOSLOG_NOTICE, "auto detection in progress ...");

    disk = fosfat_diskauto (fosfat);
    fboot = disk;
  }
  else
    fboot = fosfat_diskauto (fosfat);

  /* Test if the auto detection and the user param are the same */
  if (fboot != disk)
    foslog (FOSLOG_WARNING, "disk type forced seems to be false");

  switch (disk)
  {
  case FOSFAT_FD:
    fosfat->fosboot = FOSBOOT_FD;
    foslog (FOSLOG_NOTICE, "floppy disk selected");
    break;

  case FOSFAT_HD:
    fosfat->fosboot = FOSBOOT_HD;
    foslog (FOSLOG_NOTICE, "hard disk selected");
    break;

  case FOSFAT_ED:
    foslog (FOSLOG_ERROR, "disk auto detection for \"%s\" has failed", dev);

  default:
    goto err;
  }

  foslog (FOSLOG_NOTICE, "cache file is loading ...");

  fosfat->cachelist = fosfat_cache_dir (fosfat, FOSFAT_SYSLIST);
  if (!fosfat->cachelist)
    goto err;

  foslog (FOSLOG_NOTICE, "fosfat is ready");

  return fosfat;

 err:
#ifdef _WIN32
  w32disk_free (fosfat->dev);
#else
  fclose (fosfat->dev);
#endif /* !_WIN32 */
 err_dev:
  free (fosfat);
  return NULL;
}

/*
 * Close the device.
 *
 * That hides the fclose processing. And it uses the w32disk library with
 * Win9x and WinNT.
 *
 * fosfat       handle
 */
void
fosfat_close (fosfat_t *fosfat)
{
  if (!fosfat)
    return;

  /* Unload the cache if is loaded */
  if (fosfat->cachelist)
  {
    foslog (FOSLOG_NOTICE, "cache file is unloading ...");
    fosfat_cache_unloader (fosfat->cachelist);
  }

  foslog (FOSLOG_NOTICE, "device is closing ...");

  if (fosfat->dev)
#ifdef _WIN32
    w32disk_free (fosfat->dev);
#else
    fclose (fosfat->dev);
#endif /* !_WIN32 */

  free (fosfat);
}