aboutsummaryrefslogtreecommitdiff
path: root/jindroush/lib/cfs_dos3.cpp
blob: 2eca4b2b3c3afaf8f65352dbd0330b51fd1ab102 (plain)
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
//    This program 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 2 of the License, or
//    any later version.
//
//    This program 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, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "cfs_dos3.h"
#include "autil.h"

#define ROOT_DIR 0x10
#define FAT	0x18

CDos3::CDos3() : CFs()
{
	#ifdef _MEMORY_DUMP_
		printf( "CDos3 constructed: %08X\n", this );
	#endif
}

CDos3::~CDos3()
{
	Dismount();
	#ifdef _MEMORY_DUMP_
		printf( "CDos3 destructed: %08X\n", this );
	#endif
}

BOOL CDos3::Mount( ADisk* pDisk )
{
	m_iFilesValid = 0;
	m_iFilesInvalid = 0;
	m_pDisk = pDisk;
	m_pRoot = NULL;

	if ( m_pDisk->GetSectorSize() != 0x80 )
	{
		sprintf( m_szLastError, "DOS3: Can't process DD disk!" );
		return FALSE;
	}

	CDos3DirEntry* pPrev = NULL;

	DOS3_DIRENT dire;

	WORD wEntry = 1;

	if ( !pDisk->ReadSector( m_abtFat, FAT ) )
	{
		sprintf( m_szLastError, "DOS3: Can't read FAT sector because\n%s", m_pDisk->GetLastError() );
		return FALSE;
	}

	do
	{
		BYTE abtSec[ 0x100 ];

		if ( !pDisk->ReadSector( abtSec, ROOT_DIR + ( wEntry / 8 ) ) )
		{
			sprintf( m_szLastError, "DOS3: Can't read directory entry %04X because\n%s", wEntry, m_pDisk->GetLastError() );
			return FALSE;
		}

		memcpy( &dire, abtSec + ( wEntry % 8) * sizeof( DOS3_DIRENT ), sizeof( DOS3_DIRENT ) );

		if ( !dire.btFlags )
			break;

		CDos3DirEntry* pE = CreateEntry( &dire );

		if ( pE && ! ( pE->m_dwFlags & DIRE_DELETED ) )
		{
			if ( m_pRoot )
			{
				pPrev->m_pNext = pE;
				pE->m_pPrev = pPrev;
				pPrev = pE;
			}
			else
			{
				m_pRoot = pE;
				pPrev = pE;
			}

		}

		wEntry++;

	} while( dire.btFlags );

	return TRUE;
}

void CDos3::Dismount()
{
	DeleteList( m_pRoot );
}

CDos3DirEntry* CDos3::CreateEntry( DOS3_DIRENT* pDire )
{
	CDos3DirEntry* pE = new CDos3DirEntry();

	if ( !pE )
		return NULL;

	if ( pDire->btFlags == 0x80 )
	{
		pE->m_dwFlags |= DIRE_DELETED;
	}

	ADos2MsDos( pE->m_szFname, pDire->acAtariName );

	//sprintf( pE->m_szAscData, "%02X %02X %02X %04X", pDire->btSecStart, pDire->btSecCount, pDire->btFlags, pDire->wFileLen );
	sprintf( pE->m_szAscData, "%02X %04X", pDire->btFlags, pDire->wFileLen );

	pE->m_btSecStart = pDire->btSecStart;
	pE->m_btSecCount = pDire->btSecCount;
	pE->m_btFlags = pDire->btFlags;
	pE->m_wFileLen = pDire->wFileLen;

	if ( pE->m_dwFlags & DIRE_DELETED )
	{
	}
	else if ( ExportFile( NULL, pE ) )
		m_iFilesValid++;
	else
		m_iFilesInvalid++;

	return pE;
}

BOOL CDos3::ExportFile( char* szOutFile, CDirEntry* pDirE )
{
	int hOutfile = -1;

	if ( szOutFile )
	{
		hOutfile = open( szOutFile, O_BINARY | O_CREAT | O_TRUNC | O_WRONLY, 0666 );

		if ( -1 == hOutfile )
		{
			sprintf( m_szLastError, "DOS3: Unable to create file '%s'!", szOutFile );
			return FALSE;
		}
	}

	BYTE abtBuff[ 0x80 * 8 ];

	BYTE btSecCount = ((CDos3DirEntry*)pDirE) ->m_btSecCount;
	BYTE btSector = ((CDos3DirEntry*)pDirE) ->m_btSecStart;
	WORD wFileLen = ((CDos3DirEntry*)pDirE) ->m_wFileLen;

	while( btSecCount )
	{
		WORD wToCopy = ( wFileLen < 0x400 ) ? wFileLen : 0x400;

		if ( !m_pDisk->ReadSectors( abtBuff, ( btSector * 8 ) + FAT + 1, 8 ) )
		{
			{
				sprintf( m_szLastError, "DOS3: Corrupted file '%s'\n%s\n", szOutFile, m_pDisk->GetLastError() );
				return FALSE;
			}
		}

		if ( -1 != hOutfile )
			write( hOutfile, abtBuff, wToCopy );

		btSecCount--;
		wFileLen -= wToCopy;

		btSector = m_abtFat[ btSector ];

		if ( wToCopy == 0x400 )
		{
			if ( btSector >= 0xFD )
			{
				sprintf( m_szLastError, "DOS3: Corrupted file '%s' (unexpected EOF)", szOutFile );
				return FALSE;
			}

		}
		else
		{
			if ( btSector != 0xFD )
			{
				sprintf( m_szLastError, "DOS3: Corrupted file '%s' (missed EOF) %02X", szOutFile, btSector );
				return FALSE;
			}

		}


	}


	if ( -1 != hOutfile )
		close( hOutfile );

	return TRUE;
}