aboutsummaryrefslogtreecommitdiff
path: root/jindroush/lib/cdsk_scp.cpp
blob: eb2e0f2f154a93797046d96fb7c12fc82f59e123 (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
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
//    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 "cdsk_scp.h"
#include "autil.h"
#include "cfile.h"

#define SCP_MAGIC 0xFDFD

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

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

BOOL CScp::Load( char* szFname, BOOL, BOOL )
{
	WORD	wMagic;
	BYTE	btSectorSize;
	BYTE	btTracks;
	BYTE	btSectorsPerTrack;

	CFile cf;

	if( !cf.Open( szFname ) )
	{
		sprintf( m_szLastError, "SCP: Can't open '%s'", szFname );
		return FALSE;
	}

	strcpy( m_szFname, szFname );

	wMagic = cf.readLEw();

	if ( wMagic != SCP_MAGIC )
	{
		sprintf( m_szLastError, "SCP: File '%s' is not an SCP file!", szFname );
		return FALSE;
	}

	btSectorSize = cf.readb();
	btTracks = cf.readb();
	btSectorsPerTrack = cf.readb();

	int iSectorSize;

	switch( btSectorSize )
	{
		case 0x80:
			iSectorSize = 0x80;
			break;

		case 0x00:
			iSectorSize = 0x100;
			break;

		default:
		{
			sprintf( m_szLastError, "SCP: Invalid sector size: %02X", btSectorSize );
			return FALSE;
		}
	}

	DISK_GEOMETRY dg;
	dg.iSides = 1;
	dg.iTracks = btTracks;
	dg.iSectorsPerTrack = btSectorsPerTrack;
	dg.iBytesPerSector = iSectorSize;

	if ( !Format( &dg ) )
		return FALSE;

	BYTE *pbtTable = new BYTE [ btSectorsPerTrack * btTracks ];

	if ( ! pbtTable )
	{
	 	sprintf( m_szLastError, "SCP: Not enough memory for sector table!" );
	 	return FALSE;
	}

	if ( !cf.Read( pbtTable, btSectorsPerTrack * btTracks ) )
	{
	 	sprintf( m_szLastError, "SCP: Can't read sector table!" );
	 	return FALSE;
	}

	BYTE abtBuff[ 0x100 ];
	memset( abtBuff, 0, 0x100 );

	BYTE* pbtPtr = pbtTable;

	for( int iTrack = 0; iTrack < btTracks; iTrack++ )
	{
		for( int iSector = 0; iSector < btSectorsPerTrack; iSector++ )
		{
			if ( *pbtPtr )
			{
				int iNowRead;

				if ( !iTrack && ( iSector < 3 ) )
					iNowRead = 0x80;
				else
					iNowRead = iSectorSize;

				int iReallyRead;

				if ( !cf.Read( abtBuff, iNowRead, &iReallyRead ) || ( iNowRead != iReallyRead ) )
				{
					delete [] pbtTable;
	 				sprintf( m_szLastError, "SCP: Image broken!" );
	 				return FALSE;
				}

				if ( !WriteSector( *pbtPtr + iTrack* btSectorsPerTrack, abtBuff ) )
				{
					delete [] pbtTable;
					return FALSE;
				}

			}
			pbtPtr++;
		}

	}

	delete [] pbtTable;

	cf.Close();
	return TRUE;

}

#ifdef __CDISK_WRITE__

BOOL CScp::Save( char* szOutFile, BOOL bOverWrite )
{
	if ( !bOverWrite && !access( szOutFile, F_OK ) )
	{
		sprintf( m_szLastError, "SCP: File already exists! '%s'", szOutFile );
		return FALSE;
	}

	BYTE btSize;
	BYTE btTracks;
	BYTE btSpT;

	switch ( m_geometry.iBytesPerSector )
	{
		case 0x80:
			btSize = 0x80;
			break;

		case 0x100:
		default:
			btSize = 0x00;
			break;
	}

	BOOL bGood = FALSE;

	btTracks = m_geometry.iTracks;
	btSpT = m_geometry.iSectorsPerTrack;

	if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 18 ) )
		bGood = TRUE;

	if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 26 ) )
		bGood = TRUE;

	if ( !bGood )
	{
		sprintf( m_szLastError, "SCP: Can't export, because of invalid disk size!" );
		return FALSE;
	}

	int iMapSize = m_geometry.iTracks * m_geometry.iSectorsPerTrack;

	BYTE* pMap = new BYTE [ iMapSize ];

	if ( !pMap )
	{
		sprintf( m_szLastError, "SCP: Can't allocate memory for map!" );
		return FALSE;
	}

	memset( pMap, 0, iMapSize );


	CFile cf;

	if ( !cf.Create( szOutFile ) )
	{
		sprintf( m_szLastError, "SCP: Can't create '%s'", szOutFile );
		delete [] pMap;
		return FALSE;
	}

	WORD wMagic = SCP_MAGIC;

	cf.writeLEw( wMagic );
	cf.writeb( btSize );
	cf.writeb( btTracks );
	cf.writeb( btSpT );

	cf.Seek( iMapSize, SEEK_CUR );

	BYTE abtBuff[ 0x100 ];

	int iSectors = m_geometry.iSectors;

	for( int i = 0; i < iSectors; i++ )
	{
		if ( !ReadSector( abtBuff, i + 1 ) )
		{
			delete [] pMap;
			cf.Close();
			unlink( szOutFile );
			return FALSE;
		}

		int iBytesNow = ( i < 3 ) ? 0x80 : m_geometry.iBytesPerSector;

		if ( !IsBlockEmpty( abtBuff, iBytesNow ) )
		{
			int iWritten;
			if ( !cf.Write( abtBuff, iBytesNow, &iWritten ) || ( iBytesNow != iWritten ) )
			{
				sprintf( m_szLastError, "SCP: Error writing to '%s'", szOutFile );
				delete [] pMap;
				cf.Close( );
				unlink( szOutFile );
				return FALSE;
			
			}

			pMap[ i ] = ( i % btSpT ) + 1;
		}
	}

	cf.Seek( 5, SEEK_SET );

	if ( !cf.Write( pMap, iMapSize ) )
	{
		sprintf( m_szLastError, "SCP: Can't write!" );
		delete [] pMap;
		cf.Close( );
		unlink( szOutFile );
		return FALSE;
	
	}

	delete [] pMap;

	cf.Close();

	return TRUE;
}

#endif //__CDISK_WRITE__