aboutsummaryrefslogtreecommitdiff
path: root/jindroush/lib/cfs_b2b.cpp
blob: 8b62dedffabaf70235c8acfcab3a1d2ea9a3f23e (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
//    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_b2b.h"
#include "autil.h"

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

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

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

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

	m_pRoot = CreateEntry();

	return m_pRoot ? TRUE : FALSE;
}

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

CBas2BootDirEntry* CBas2Boot::CreateEntry()
{
	CBas2BootDirEntry* pE = new CBas2BootDirEntry();

	if ( !pE )
		return NULL;

	BYTE abtSec[ 0x100 ];

	if ( !m_pDisk->ReadSector( abtSec, 1 ) )
	{								
		sprintf( m_szLastError, "BAS2BOOT: Can't read boot sector because\n%s", m_pDisk->GetLastError() );
		delete pE;
		return NULL;
	}

	pE->m_dwFileLen = abtSec[ 8 ] + ( abtSec[ 9 ] << 8 );

	if ( !m_pDisk->ReadSector( abtSec, 2 ) )
	{
		sprintf( m_szLastError, "BAS2BOOT: Can't read boot sector because\n%s", m_pDisk->GetLastError() );
		delete pE;
		return NULL;
	}

	int iSectors = ( ( pE->m_dwFileLen - 0x0E + 0x7F ) / 0x80 );
	int iStoredSectors = abtSec[ 0x70 ] + ( abtSec[ 0x71 ] << 8 );

	if ( ( iSectors - 1 ) != iStoredSectors )
	{
		sprintf( m_szLastError, "BAS2BOOT: Not a Bas2Boot image or invalid! (%d<>%d)", iSectors - 1, iStoredSectors );
		delete pE;
		return NULL;
	}


	if ( ( iSectors + 2 ) > m_pDisk->GetSectorCount() )
	{
		sprintf( m_szLastError, "BAS2BOOT: Not a Bas2Boot image or invalid! (%d<>%d)", iSectors + 2, m_pDisk->GetSectorCount() );
		delete pE;
		return NULL;
	}

	sprintf( pE->m_szAscData, "%06lX", pE->m_dwFileLen );

	GuessBestFnameFromPC( pE->m_szFname, m_pDisk->GetImageName(), "bas" );

	return pE;
}

BOOL CBas2Boot::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, "BAS2BOOT: Unable to create file '%s'!", szOutFile );
			return FALSE;
		}
	}

	DWORD dwFileLen = ((CBas2BootDirEntry*)pDirE) ->m_dwFileLen;

	BYTE abtBuff[ 0x80 ];

	int iSector = 3;

	m_pDisk->ReadSector( abtBuff, 2 );
	if ( -1 != hOutfile )
		write( hOutfile, abtBuff + 0x72, 0x0E );

	dwFileLen -= 0xE;

	while( dwFileLen )
	{
		WORD wToCopy = ( dwFileLen < 0x80 ) ? dwFileLen : 0x80;

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

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

		dwFileLen -= wToCopy;
		iSector++;

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

	return TRUE;
}