-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnd.cpp
1267 lines (916 loc) · 37.7 KB
/
nd.cpp
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
/**
@file nd.cpp
@brief based on TRS-80 Virtual Disk Kit v1.7 for Windows by Miguel Dutra
Linux port VDK-80-Linux done by Mike Gore, 2016
@par Tools to Read and Write files inside common TRS-80 emulator images
@par Copyright © 2016 Miguel Dutra, GPL License
@par You are free to use this code under the terms of GPL
please retain a copy of this notice in any code you use it in.
This 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.
The software 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/>.
@par Original Windows Code
@see http://www.trs-80.com/wordpress/category/contributors/miguel-dutra/
@see http://www.trs-80.com/wordpress/dsk-and-dmk-image-utilities/
@see Miguel Dutra www.mdutra.com
*/
//---------------------------------------------------------------------------------
// Operating System Interface for NewDOS/80
//---------------------------------------------------------------------------------
#include "windows.h"
#include <stdio.h>
#include "v80.h"
#include "vdi.h"
#include "osi.h"
#include "nd.h"
//---------------------------------------------------------------------------------
// Initialize member variables
//---------------------------------------------------------------------------------
CND::CND()
: m_pDir(NULL), m_wDirSector(0), m_nDirSectors(0), m_nSides(0), m_nDensity(VDI_DENSITY_SINGLE),
m_dwFilePos(0), m_wSector(0), m_Buffer(), m_nLumps(0), m_nFlags1(0), m_nFlags2(0), m_nTC(0),
m_nSPC(0), m_nGPL(0), m_nDDSL(0), m_nDDGA(0), m_nSPG(0), m_wTI(0), m_nTD(0)
{
}
//---------------------------------------------------------------------------------
// Release allocated memory
//---------------------------------------------------------------------------------
CND::~CND()
{
if (m_pDir != NULL)
free(m_pDir);
}
//---------------------------------------------------------------------------------
// Validate DOS version and define operating parameters
//---------------------------------------------------------------------------------
DWORD CND::Load(CVDI* pVDI, DWORD dwFlags)
{
ND_PDRIVE* pPDRIVE;
BYTE nFT;
BYTE nTC;
BYTE nSPT;
DWORD dwBytes;
DWORD dwError = NO_ERROR;
// Copy VDI pointer and user flags to member variables
m_pVDI = pVDI;
m_dwFlags = dwFlags;
// Get disk geometry
m_pVDI->GetDG(m_DG);
// Calculate the number of disk sides
if (m_dwFlags & V80_FLAG_SS)
m_nSides = 1;
else if (m_dwFlags & V80_FLAG_DS)
m_nSides = 2;
else
m_nSides = m_DG.LT.nLastSide - m_DG.LT.nFirstSide + 1;
// Determine the disk density
m_nDensity = ((m_DG.FT.nLastSector - m_DG.FT.nFirstSector + 1) != (m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1) || (m_DG.FT.nDensity != m_DG.LT.nDensity) ? VDI_DENSITY_MIXED : m_DG.LT.nDensity);
// Determine the first track depending on disk density
nFT = m_DG.FT.nTrack + (m_nDensity == VDI_DENSITY_MIXED ? 1 : 0);
// Calculate the number of tracks depending on disk density
nTC = (m_DG.LT.nTrack - m_DG.FT.nTrack + 1) - (m_nDensity == VDI_DENSITY_MIXED ? 1 : 0);
// Calculate the number of sectors per track
nSPT = m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1;
// Check internal buffer size (just in case)
if (sizeof(m_Buffer) < m_DG.FT.wSectorSize)
{
dwError = ERROR_INVALID_USER_BUFFER;
goto Done;
}
// Load the PDRIVE table from first track, third sector
if ((dwError = m_pVDI->Read(nFT, m_DG.LT.nFirstSide, m_DG.LT.nFirstSector + 2, m_Buffer, m_DG.LT.wSectorSize)) != NO_ERROR)
goto Done;
// Find the proper table entry
for (int i = 0; i < 16; i++)
{
// Point to entry[i] in the table
pPDRIVE = (ND_PDRIVE*)&m_Buffer[i * 16];
// Get parameters
m_nLumps = pPDRIVE->nLumps;
m_nFlags1 = pPDRIVE->nFlags1;
m_nFlags2 = pPDRIVE->nFlags2;
m_nTC = pPDRIVE->nTC;
m_nSPC = pPDRIVE->nSPC;
m_nGPL = pPDRIVE->nGPL;
m_nDDSL = pPDRIVE->nDDSL;
m_nDDGA = pPDRIVE->nDDGA;
m_nSPG = pPDRIVE->nSPG;
m_nTSR = pPDRIVE->nTSR;
m_wTI = pPDRIVE->wTI;
m_nTD = pPDRIVE->nTD;
// Check match between disk geometry and PDRIVE parameters
if (!(m_dwFlags & V80_FLAG_CHKDSK) && (nTC != m_nTC || nSPT != (m_nSPC / m_nSides)))
continue;
// Check some other entry parameters
if ((m_nLumps == 0) || (m_nGPL < 2) || (m_nGPL > 8) || (m_nDDSL > m_nLumps) || (m_nDDGA < 2) || (m_nDDGA > 8) || (m_nTD > 7))
continue;
goto Success;
}
dwError = ERROR_NOT_DOS_DISK;
goto Done;
Success:
// SPG = Total Sectors / Total Granules
if (m_nSPG == 0)
m_nSPG = (m_nTC * m_nSPC) / (m_nLumps * m_nGPL);
// Convert DDSA into DDGA
if (m_nDDGA % m_nSPG == 0) // The division of DDGA by SPG must be an integer
m_nDDGA /= m_nSPG;
// Calculate directory relative sector
m_wDirSector = m_nDDSL * m_nGPL * m_nSPG;
// Calculate number of directory sectors
m_nDirSectors = m_nDDGA * m_nSPG;
// If not first Load, release the previously allocated memory
if (m_pDir != NULL)
free(m_pDir);
// Calculate needed memory to hold the entire directory
dwBytes = m_nDirSectors * m_DG.LT.wSectorSize + (V80_MEM - (m_nDirSectors * m_DG.LT.wSectorSize) % V80_MEM);
// Allocate memory for the entire directory
if ((m_pDir = (BYTE*)calloc(dwBytes,1)) == NULL)
{
dwError = ERROR_OUTOFMEMORY;
goto Done;
}
// Load directory into memory
if ((dwError = DirRW(ND_DIR_READ)) != NO_ERROR)
goto Done;
// Check directory structure
if (!(m_dwFlags & V80_FLAG_CHKDIR))
if ((dwError = CheckDir()) != NO_ERROR)
goto Done;
if (m_dwFlags & V80_FLAG_INFO)
printf("DOS: TI=%s TD=%c TC=%d SPT=%d TSR=%d GPL=%d DDSL=%d DDGA=%d Lumps=%d\r\n", TI(m_wTI), 'A' + m_nTD, m_nTC, m_nSPC, m_nTSR, m_nGPL, m_nDDSL, m_nDDGA, m_nLumps);
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the first/next directory entry
//---------------------------------------------------------------------------------
DWORD CND::Dir(void** pFile, OSI_DIR nFlag)
{
ND_HIT nMode = (nFlag == OSI_DIR_FIND_FIRST ? ND_HIT_FIND_FIRST_USED : ND_HIT_FIND_NEXT_USED);
DWORD dwError = NO_ERROR;
do
{
// Return a pointer to the first/next non-empty file entry
if ((dwError = ScanHIT(pFile, nMode)) != NO_ERROR)
break;
// Change mode to "next" for the remaining searches
nMode = ND_HIT_FIND_NEXT_USED;
} // Repeat until we get a pointer to a file entry which is Active and Primary
while (!(((ND_FPDE*)(*pFile))->wAttributes & ND_ATTR_ACTIVE) || (((ND_FPDE*)(*pFile))->wAttributes & ND_ATTR_EXTENDED));
return dwError;
}
//---------------------------------------------------------------------------------
// Return a pointer to the directory entry matching the file name
//---------------------------------------------------------------------------------
DWORD CND::Open(void** pFile, const char cName[11])
{
ND_HIT nMode = ND_HIT_FIND_FIRST_USED;
BYTE nHash = Hash(cName);
DWORD dwError = NO_ERROR;
// Invalidate any previous Seek()
m_wSector = 0xFFFF;
// Repeat while there is no match
while (true)
{
// Get first/next file whose hash matches the requested name
if ((dwError = ScanHIT(pFile, nMode, nHash)) != NO_ERROR)
break;
// Change mode to "next" for the remaining searches
nMode = ND_HIT_FIND_NEXT_USED;
// If file entry is not Active or Primary, skip to the next
if (!(((ND_FPDE*)(*pFile))->wAttributes & ND_ATTR_ACTIVE) || (((ND_FPDE*)(*pFile))->wAttributes & ND_ATTR_EXTENDED))
continue;
// If entry's filename matches the caller's filename, we are done
// FIXME if (memcmp(((ND_FPDE*)(*pFile))->cName, cName, sizeof(cName)) == 0)
if (memcmp(((ND_FPDE*)(*pFile))->cName, cName, 11) == 0)
break;
}
return dwError;
}
//---------------------------------------------------------------------------------
// Create a new file with the indicated name and size
//---------------------------------------------------------------------------------
DWORD CND::Create(void** pFile, OSI_FILE& File)
{
ND_EXTENT Extent;
void* pPrevious;
BYTE nGranules;
BYTE nExtent = 0;
DWORD dwError = NO_ERROR;
// Invalidate any previous Seek()
m_wSector = 0xFFFF;
// Get a new directory entry
if ((dwError = GetFDE(pFile)) != NO_ERROR)
goto Abort;
// Set directory entry as Active
((ND_FPDE*)(*pFile))->wAttributes = ND_ATTR_ACTIVE;
// Set file password
((ND_FPDE*)(*pFile))->wUpdatePassword = 0x4296;
((ND_FPDE*)(*pFile))->wAccessPassword = 0x4296;
// Set file properties as indicated by the caller
SetFile(*pFile, File);
// Calculate number of granules needed
nGranules = ((ND_FPDE*)(*pFile))->wNext / m_nSPG + (((ND_FPDE*)(*pFile))->wNext % m_nSPG > 0 ? 1 : 0);
if (nGranules == 0 && ((ND_FPDE*)(*pFile))->nEOF > 0)
nGranules++;
// Repeat while the number of needed granules is greater than zero
while (nGranules > 0)
{
// Create one extent with as many granules as possible, based on the calculated quantity
if ((dwError = CreateExtent(Extent, nGranules)) != NO_ERROR)
goto Abort;
// Increment extent counter
nExtent++;
// Copy extent data to the directory entry
if ((dwError = CopyExtent(*pFile, ND_EXTENT_SET, nExtent, Extent)) != NO_ERROR)
{
// Abort if error is other than extent not found
if (dwError != ERROR_NOT_FOUND)
goto Abort;
// Preserve current file handle
pPrevious = *pFile;
// Get an additional directory entry
if ((dwError = GetFDE(pFile)) != NO_ERROR)
goto Abort;
// Set a forward link to the newly created directory entry
((ND_FPDE*)pPrevious)->Link.nLump = 0xFE;
((ND_FPDE*)pPrevious)->Link.nGranules = FDE2DEC(*pFile);
// Set a backward link from the previously existing directory entry
((ND_FXDE*)(*pFile))->nAttributes = ND_ATTR_ACTIVE|ND_ATTR_EXTENDED;
((ND_FXDE*)(*pFile))->nDEC = FDE2DEC(pPrevious);
// Copy file name and extention to the new directory entry
memcpy(((ND_FXDE*)pFile)->cName, File.szName, sizeof(File.szName) - 1);
memcpy(((ND_FXDE*)pFile)->cType, File.szType, sizeof(File.szType) - 1);
// Set corresponding HIT DEC with the calculated name hash
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = Hash((const char *) ((ND_FXDE*)pFile)->cName);
continue;
}
// Subtract number of allocated granules in this extent from the total required
nGranules -= (Extent.nGranules & ND_GRANULE_COUNT) + 1;
}
// Write updated directory data and exit
dwError = DirRW(ND_DIR_WRITE);
goto Done;
// Restore previous directory state
Abort:
DirRW(ND_DIR_READ);
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Move the file pointer
//---------------------------------------------------------------------------------
DWORD CND::Seek(void* pFile, DWORD dwPos)
{
ND_EXTENT Extent;
int nGranuleSectors;
int nRemainingSectors;
int nExtent = 0;
DWORD dwError = NO_ERROR;
// Calculate number of sectors required to reach the requested file position
nRemainingSectors = dwPos / m_DG.LT.wSectorSize;
while (true)
{
// Increment extent counter
nExtent++;
// Retrieve corresponding extent data
if ((dwError = CopyExtent(pFile, ND_EXTENT_GET, nExtent, Extent)) != NO_ERROR)
{
m_wSector = 0xFFFF;
break;
}
// Calculate number of contiguous sectors contained in this extent
nGranuleSectors = ((Extent.nGranules & ND_GRANULE_COUNT) + 1) * m_nSPG;
// Check whether it exceeds our need
if (nGranuleSectors > nRemainingSectors)
{ // RelativeSector = (Lump * GranulesPerLump + InitialGranule) * SectorsPerGranule + RemainingSectors
m_wSector = (((Extent.nLump - m_DG.FT.nTrack) * m_nGPL) + ((Extent.nGranules & ND_GRANULE_INITIAL) >> 5)) * m_nSPG + nRemainingSectors;
m_dwFilePos = dwPos;
break;
}
// Otherwise, subtract the number of extent sectors from the total required
nRemainingSectors -= nGranuleSectors;
}
return dwError;
}
//---------------------------------------------------------------------------------
// Read data from file
//---------------------------------------------------------------------------------
DWORD CND::Read(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwRead = 0;
DWORD dwError = NO_ERROR;
// Get file size
dwFileSize = GetFileSize(pFile);
// Repeat while there is something left to read
while (dwBytes - dwRead > 0)
{
// Check whether last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_READ_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Read sector into internal buffer
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Calculate starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwRead) ? wSectorEnd - wSectorBegin : dwBytes - dwRead);
// Copy data from internal buffer to the caller's buffer
memcpy(pBuffer, &m_Buffer[wSectorBegin], wLength);
// Update bytes count and caller's buffer pointer
dwRead += wLength;
pBuffer += wLength;
// Advance file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwRead;
return dwError;
}
//---------------------------------------------------------------------------------
// Save data to file
//---------------------------------------------------------------------------------
DWORD CND::Write(void* pFile, BYTE* pBuffer, DWORD& dwBytes)
{
VDI_TRACK* pTrack;
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwFileSize;
WORD wSectorBegin;
WORD wSectorEnd;
WORD wLength;
DWORD dwWritten = 0;
DWORD dwError = NO_ERROR;
dwFileSize = GetFileSize(pFile);
while (dwBytes - dwWritten > 0)
{
// Check whether last Seek() was successful
if (m_wSector == 0xFFFF)
{
dwError = ERROR_SEEK;
break;
}
// Check whether requested position doesn't exceed the file size
if (m_dwFilePos > dwFileSize)
{
dwError = ERROR_WRITE_FAULT;
break;
}
// Convert relative sector into Track, Side, Sector
CHS(m_wSector, nTrack, nSide, nSector);
// Get a pointer to the correct track descriptor
pTrack = (nTrack == m_DG.FT.nTrack ? &m_DG.FT : &m_DG.LT);
// Calculate starting transfer point
wSectorBegin = m_dwFilePos % pTrack->wSectorSize;
// Calculate ending transfer point
wSectorEnd = (dwFileSize - m_dwFilePos >= pTrack->wSectorSize ? pTrack->wSectorSize : dwFileSize % pTrack->wSectorSize);
// Calculate transfer length
wLength = ((wSectorEnd - wSectorBegin) <= (dwBytes - dwWritten) ? wSectorEnd - wSectorBegin : dwBytes - dwWritten);
// If needed, read sector before writing to it
if (wLength < pTrack->wSectorSize)
{
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
}
// Copy data from caller's buffer to the internal buffer
memcpy(&m_Buffer[wSectorBegin], pBuffer, wLength);
// Write sector from internal buffer
if ((dwError = m_pVDI->Write(nTrack, nSide, nSector, m_Buffer, pTrack->wSectorSize)) != NO_ERROR)
break;
// Update bytes count and caller's buffer pointer
dwWritten += wLength;
pBuffer += wLength;
// Advance file pointer
Seek(pFile, m_dwFilePos + wLength);
}
dwBytes = dwWritten;
return dwError;
}
//---------------------------------------------------------------------------------
// Delete file
//---------------------------------------------------------------------------------
DWORD CND::Delete(void* pFile)
{
ND_EXTENT Extent;
DWORD dwError = NO_ERROR;
// Invalidate any previous Seek()
m_wSector = 0xFFFF;
// Inactivate directory entry
((ND_FPDE*)pFile)->wAttributes &= !ND_ATTR_ACTIVE;
// Release corresponding HIT slot
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = 0;
// Loop through all extents releasing every allocated granule
for (int x = 1; (dwError = CopyExtent(pFile, ND_EXTENT_GET, x, Extent)) == NO_ERROR; x++)
{
if ((dwError = DeleteExtent(Extent)) != NO_ERROR)
break;
}
// If exited loop because reached the end of the extents table
if (dwError == ERROR_NO_MATCH)
dwError = DirRW(ND_DIR_WRITE); // Save the directory
else
DirRW(ND_DIR_READ); // Otherwise, restore its previous state
return dwError;
}
//---------------------------------------------------------------------------------
// Get DOS information (NewDOS/80 doesn't support DOS Version)
//---------------------------------------------------------------------------------
void CND::GetDOS(OSI_DOS& DOS)
{
// Zero the structure
memset(&DOS, 0, sizeof(OSI_DOS));
// Get disk name
memcpy(DOS.szName, ((ND_GAT*)m_pDir)->cDiskName, sizeof(DOS.szName) - 1);
// Get disk date
memcpy(&DOS.szDate, ((ND_GAT*)m_pDir)->cDiskDate, sizeof(DOS.szDate) - 1);
}
//---------------------------------------------------------------------------------
// Set DOS information
//---------------------------------------------------------------------------------
DWORD CND::SetDOS(OSI_DOS& DOS)
{
// Set disk name
memcpy(((ND_GAT*)m_pDir)->cDiskName, DOS.szName, sizeof(DOS.szName) - 1);
// Set disk date
memcpy(((ND_GAT*)m_pDir)->cDiskDate, &DOS.szDate, sizeof(DOS.szDate) - 1);
return DirRW(ND_DIR_WRITE);
}
//---------------------------------------------------------------------------------
// Get file information (NewDOS/80 doesn't support file dates)
//---------------------------------------------------------------------------------
void CND::GetFile(void* pFile, OSI_FILE& File)
{
// Zero the structure
memset(&File, 0, sizeof(OSI_FILE));
// Copy file name and extention
memcpy(File.szName, ((ND_FPDE*)pFile)->cName, sizeof(File.szName) - 1);
memcpy(File.szType, ((ND_FPDE*)pFile)->cType, sizeof(File.szType) - 1);
// Get file size
File.dwSize = GetFileSize(pFile);
// Get file attributes
File.nAccess = ((ND_FPDE*)pFile)->wAttributes & ND_ATTR_ACCESS;
File.bSystem = ((ND_FPDE*)pFile)->wAttributes & ND_ATTR_SYSTEM;
File.bInvisible = ((ND_FPDE*)pFile)->wAttributes & ND_ATTR_INVISIBLE;
File.bModified = ((ND_FPDE*)pFile)->wAttributes & ND_ATTR_MODIFIED;
}
//---------------------------------------------------------------------------------
// Set file information (public)
//---------------------------------------------------------------------------------
DWORD CND::SetFile(void* pFile, OSI_FILE& File)
{
return SetFile(pFile, File, true);
}
//---------------------------------------------------------------------------------
// Set file information (protected)
//---------------------------------------------------------------------------------
DWORD CND::SetFile(void* pFile, OSI_FILE& File, bool bCommit)
{
// Copy file name and extention
memcpy(((ND_FPDE*)pFile)->cName, File.szName, sizeof(File.szName) - 1);
memcpy(((ND_FPDE*)pFile)->cType, File.szType, sizeof(File.szType) - 1);
// Set corresponding HIT DEC to the newly calculated name hash
m_pDir[m_DG.LT.wSectorSize + FDE2DEC(pFile)] = Hash((const char *) ((ND_FPDE*)pFile)->cName);
// Set file size
((ND_FPDE*)pFile)->nEOF = File.dwSize % m_DG.LT.wSectorSize;
((ND_FPDE*)pFile)->wNext = File.dwSize / m_DG.LT.wSectorSize + (((ND_FPDE*)pFile)->nEOF > 0 ? 1 : 0);
// Set file attributes
((ND_FPDE*)pFile)->wAttributes &= ~ND_ATTR_ACCESS;
((ND_FPDE*)pFile)->wAttributes |= File.nAccess;
((ND_FPDE*)pFile)->wAttributes &= ~ND_ATTR_SYSTEM;
((ND_FPDE*)pFile)->wAttributes |= (ND_ATTR_SYSTEM * File.bSystem);
((ND_FPDE*)pFile)->wAttributes &= ~ND_ATTR_INVISIBLE;
((ND_FPDE*)pFile)->wAttributes |= (ND_ATTR_INVISIBLE * File.bInvisible);
((ND_FPDE*)pFile)->wAttributes &= ~ND_ATTR_MODIFIED;
((ND_FPDE*)pFile)->wAttributes |= (ND_ATTR_MODIFIED * File.bModified);
return (bCommit ? DirRW(ND_DIR_WRITE) : NO_ERROR);
}
//---------------------------------------------------------------------------------
// Get file size
//---------------------------------------------------------------------------------
DWORD CND::GetFileSize(void* pFile)
{
WORD wNext = ((ND_FPDE*)pFile)->wNext;
BYTE nEOF = ((ND_FPDE*)pFile)->nEOF;
return (wNext * m_DG.LT.wSectorSize - (nEOF > 0 ? m_DG.LT.wSectorSize - nEOF : 0));
}
//---------------------------------------------------------------------------------
// Read or Write the entire directory
//---------------------------------------------------------------------------------
DWORD CND::DirRW(ND_DIR nMode)
{
BYTE nTrack;
BYTE nSide;
BYTE nSector;
DWORD dwOffset = 0;
DWORD dwError = NO_ERROR;
// Go through every relative sector
for (BYTE nIndex = 0; nIndex < m_nDirSectors; nIndex++)
{
// Convert relative sector into Track/Side/Sector
CHS(m_wDirSector + nIndex, nTrack, nSide, nSector);
// Read or write the sector according to the requested mode
if (nMode == ND_DIR_WRITE)
{
if ((dwError = m_pVDI->Write(nTrack, nSide, nSector, &m_pDir[dwOffset], m_DG.LT.wSectorSize)) != NO_ERROR)
break;
}
else
{
if ((dwError = m_pVDI->Read(nTrack, nSide, nSector, &m_pDir[dwOffset], m_DG.LT.wSectorSize)) != NO_ERROR)
break;
}
// Advance buffer pointer
dwOffset += m_DG.LT.wSectorSize;
}
return dwError;
}
//---------------------------------------------------------------------------------
// Check the directory structure
//---------------------------------------------------------------------------------
DWORD CND::CheckDir(void)
{
int x;
void* pFile = NULL;
int nFiles = 0;
DWORD dwError = NO_ERROR;
// Validate disk name
for (x = 0; x < 8; x++)
{
if (((ND_GAT*)m_pDir)->cDiskName[x] < ' ' || ((ND_GAT*)m_pDir)->cDiskName[x] > 'z')
goto Error;
}
// Validate disk date
for (x = 0; x < 8; x++)
{
if (((ND_GAT*)m_pDir)->cDiskDate[x] < ' ' || ((ND_GAT*)m_pDir)->cDiskDate[x] > 'z')
goto Error;
}
// Validate each file name in the directory (while counting the number of files)
for (nFiles = 0; (dwError = Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == NO_ERROR; nFiles++)
{
// First 8 characters can be non-blanks
for (x = 0; x < 8 && ((ND_FPDE*)pFile)->cName[x] >= '0' && ((ND_FPDE*)pFile)->cName[x] <= 'z'; x++);
// But first one must be non-blank to be valid
if (x == 0)
goto Error;
// If a blank is found, then only blanks can be used up to the end of the name
for ( ; x < 8 && ((ND_FPDE*)pFile)->cName[x] == ' '; x++);
// If not, then this name is invalid
if (x < 8)
goto Error;
// The extension can have up to 3 non-blank characters
for (x = 0; x < 3 && ((ND_FPDE*)pFile)->cType[x] >= '0' && ((ND_FPDE*)pFile)->cType[x] <= 'z'; x++);
// If a blank is found, then only blanks can be used up to the end of the extension
for ( ; x < 3 && ((ND_FPDE*)pFile)->cType[x] == ' '; x++);
// If not, then this extension is invalid
if (x < 3)
goto Error;
}
// No error if exited on "no more files" // but found files
if (dwError == ERROR_NO_MORE_FILES) // && nFiles > 0)
{
dwError = NO_ERROR;
goto Done;
}
Error:
dwError = ERROR_FILE_CORRUPT;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Scan the Hash Index Table
//---------------------------------------------------------------------------------
DWORD CND::ScanHIT(void** pFile, ND_HIT nMode, BYTE nHash)
{
static int nLastCol = -1;
static int nLastRow = 0;
int nCols, nCol;
int nRows, nRow;
int nSlot;
DWORD dwError = NO_ERROR;
// If mode is any "Find First", reset static variables
if (nMode != ND_HIT_FIND_NEXT_USED)
{
nLastCol = -1;
nLastRow = 0;
}
// Retrieve last values from static variables
nCol = nLastCol;
nRow = nLastRow;
// Calculates max HIT columns and rows
nCols = m_nDirSectors - 2;
nRows = m_DG.LT.wSectorSize / sizeof(ND_FPDE);
while (true)
{
// Increment column
nCol++;
// If column reaches max, reset it and advance to the next row
if (nCol >= nCols)
{
nCol = 0;
nRow++;
}
// If row reaches max, then we have reached the end of the HIT
if (nRow >= nRows)
{
if (nHash != 0)
dwError = ERROR_FILE_NOT_FOUND;
else if (nMode == ND_HIT_FIND_FIRST_FREE)
dwError = ERROR_DISK_FULL;
else
dwError = ERROR_NO_MORE_FILES;
break;
}
// The 32nd byte of a NewDOS/80 HIT contains the count of extra FDE sectors
if (nRow * sizeof(ND_FPDE) + nCol == 31)
continue;
// Get value in HIT[Row * Cols + Col]
nSlot = m_pDir[m_DG.LT.wSectorSize + (nRow * nCols) + nCol];
// If this is not what we are looking for, skip to the next
if ((nMode == ND_HIT_FIND_FIRST_FREE && nSlot != 0) || (nMode != ND_HIT_FIND_FIRST_FREE && nSlot == 0))
continue;
// If there is a hash to match and it doesn't match, skip to the next
if (nHash != 0 && nHash != nSlot)
continue;
// Return pointer corresponding to the current Directory Entry Code (DEC)
if ((*pFile = DEC2FDE((nRow << 5) + nCol)) == NULL)
dwError = ERROR_INVALID_ADDRESS;
break;
}
// Update static variables
nLastCol = nCol;
nLastRow = nRow;
return dwError;
}
//---------------------------------------------------------------------------------
// Allocate disk space
//---------------------------------------------------------------------------------
DWORD CND::CreateExtent(ND_EXTENT& Extent, BYTE nGranules)
{
BYTE nInitialLump = 0;
BYTE nInitialGranule = 0;
BYTE nCurrentLump = 0;
BYTE nCurrentGranule = 0;
BYTE nAllocatedGranules = 0;
DWORD dwError = NO_ERROR;
// Requested number of granules must be greater than zero
if (nGranules == 0)
{
dwError = ERROR_INVALID_PARAMETER;
goto Done;
}
// Find first empty slot (0) then continue up to the first non-empty slot (1)
for (int nExpectedBit = 0; nExpectedBit < 2; nExpectedBit++)
{
// Test state of 'CurrentGranule' at GAT[CurrentCylinder]
while ((m_pDir[nCurrentLump] & (1 << nCurrentGranule)) != nExpectedBit)
{
// Check if we are in the "continue up to the first non-empty slot" phase
if (nExpectedBit == 1)
{
// Set granule as reserved
m_pDir[nCurrentLump] |= (1 << nCurrentGranule);
nAllocatedGranules++;
nGranules--;
// Count of allocated granules must fit in 5 bits (so the max is 32)
// Also exit when number of requested granules have been reached
if (nAllocatedGranules == 32 || nGranules == 0)
goto Stop;
}
// Advance to next granule
nCurrentGranule++;
// Check whether we've reached the cylinder's limit
if (nCurrentGranule == m_nGPL)
{
// Reset granule index and advance to the next cylinder
nCurrentGranule = 0;
nCurrentLump++;
// Check whether we've reached the disk's limit
if (nCurrentLump == m_nLumps)
{
dwError = ERROR_DISK_FULL;
goto Done;
}
}
}
// Check whether we are just leaving the "find the first empty slot" phase
if (nExpectedBit == 0)
{
nInitialLump = nCurrentLump;
nInitialGranule = nCurrentGranule;