aboutsummaryrefslogtreecommitdiff
path: root/jindroush/aext/aext.cpp
blob: 75c94c3ba015dc32e8b22a6f9dade611b886835f (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
280
//    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 "pub.def"
#include "jintypes.h"

int g_iBlkEnd = 0;
int g_iBlkStart = 0;
int g_iBlkLength = 0;
int g_iBlkBase = 0;

char szStart[ MAX_PATH ];
char szEnd[ MAX_PATH ];
char szLength[ MAX_PATH ];

BOOL g_bStartUsed = FALSE;
BOOL g_bEndUsed = FALSE;
BOOL g_bLenUsed = FALSE;

int g_bOutputIsAtari = FALSE;
int g_bUseBase = FALSE;

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
   PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

#include "switches.cpp"

int ToNum( char*, int );

int main( int argc, char *argv[] )
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if ( argc < 2 )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	fprintf( stderr, SHEADER );

	char* szInfile = argv[ 1 ];

	char szOutfile[ MAX_PATH ];

	if ( argc >= 3 )
	{
		strcpy( szOutfile, argv[ 2 ] );
	}
	else
	{
		strcpy( szOutfile, "prg.axe" );
	}

	int hIn = open( szInfile, O_RDONLY | O_BINARY );

	if ( hIn == -1 )
	{
		fprintf( stderr, "Can't open infile '%s'!\n", szInfile );
		return -1;
	}

	if( g_bStartUsed )
	{
		g_iBlkStart = ToNum( szStart, 1 );
	}

	if( g_bEndUsed )
	{
		g_iBlkEnd = ToNum( szEnd, 1 );
	}

	if( g_bLenUsed )
	{
		g_iBlkLength = ToNum( szLength, 0 );
	}

	LONG lLen = lseek( hIn, 0, SEEK_END );
	lseek( hIn, 0, SEEK_SET );

	if( g_bEndUsed && g_iBlkEnd < 1 )
	{
		g_iBlkEnd = lLen - 1 + g_iBlkEnd;
	}

	if( g_bStartUsed )
	{
		if ( g_bEndUsed )
		{
			g_iBlkLength = g_iBlkEnd - g_iBlkStart + 1;
		}
		else
		{
			//START & ?
			if ( g_bLenUsed )
			{
				//start & length
				g_iBlkEnd = g_iBlkStart + g_iBlkLength - 1;		
			}
			else
			{
				//start & nothing
				g_iBlkEnd = lLen - 1;
				g_iBlkLength = g_iBlkEnd - g_iBlkStart + 1;
			}
		}

	}
	else if( g_bEndUsed )
	{
		if( g_bLenUsed )
		{
			//end & length
			g_iBlkStart = g_iBlkEnd - g_iBlkLength + 1;
		}
		else
		{
			//end only
			g_iBlkStart = 0;
			g_iBlkLength = g_iBlkEnd - g_iBlkStart + 1;
		}
	}
	else
	{
		if( g_bLenUsed )
		{
			//length only
			g_iBlkStart = 0;
			g_iBlkEnd = g_iBlkStart + g_iBlkLength - 1;		
		}
		else
		{
			//nothing
			g_iBlkStart = 0;
			g_iBlkLength = lLen;
			g_iBlkEnd = g_iBlkStart + g_iBlkLength - 1;		
		}
	}

	if( ( g_iBlkStart < 0 ) || ( g_iBlkStart >= lLen ) )
	{
		fprintf( stderr, "Invalid start ptr!\n" );
		close( hIn );
		return -1;
	}

	if( ( g_iBlkEnd < 0 ) || ( g_iBlkEnd >= lLen ) || ( g_iBlkEnd < g_iBlkStart ) )
	{
		fprintf( stderr, "Invalid end ptr!\n" );
		close( hIn );
		return -1;
	}

	int iBlkStartM = g_iBlkStart;
	int iBlkEndM = g_iBlkEnd;

	if( g_bUseBase )
	{
		iBlkStartM = g_iBlkBase;
		iBlkEndM = g_iBlkBase + g_iBlkLength - 1;
	}

	if ( ( ( iBlkEndM >= 0x10000 ) || ( iBlkStartM >= 0x10000 ) ) && g_bOutputIsAtari )
	{
		fprintf( stderr, "End goes past end of RAM in real Atari!\n" );
		close( hIn );
		return -1;
	}

	int hOut = open( szOutfile, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0666 );

	if ( hOut == -1 )
	{
		fprintf( stderr, "Can't open outfile '%s'!\n", szOutfile );
		close( hIn );
		return -1;
	}

	#define BUFLEN 0x10000

	BYTE* pbtPtr = new BYTE [ BUFLEN ];
	lseek( hIn, g_iBlkStart, SEEK_SET );

	if ( g_bOutputIsAtari )
	{
		WORD wStart = iBlkStartM;
		WORD wEnd = iBlkEndM;

		BYTE btPom = 0xFF;
		write( hOut, &btPom, 1 );
		write( hOut, &btPom, 1 );

		btPom = wStart & 0xFF;
		write( hOut, &btPom, 1 );

		btPom = wStart >> 8;
		write( hOut, &btPom, 1 );

		btPom = wEnd & 0xFF;
		write( hOut, &btPom, 1 );

		btPom = wEnd >> 8;
		write( hOut, &btPom, 1 );
	}

	int iLeft = g_iBlkLength;

	while( iLeft )
	{
		int iReadNow = iLeft > BUFLEN ? BUFLEN : iLeft;
		read( hIn, pbtPtr, iReadNow );
		write( hOut, pbtPtr, iReadNow );
		iLeft -= iReadNow;
	}

	close( hOut );
	close( hIn );

	printf( "Done!\n" );

	return 0;
}

int ToNum( char* szString, int iOffs )
{
	BOOL bSec = FALSE;

	if( *szString == 's' )
	{
		bSec = TRUE;
		szString++;
	}

	int iNum;
	sscanf( szString, "%X", &iNum );

	if( bSec )
	{
		if( iNum < iOffs )
		{
			printf( "Incorrect sector number %s\n", szString );
			return -1;
		}

		iNum -= iOffs;
		iNum *= 0x80;

		if( iOffs )
		{
			//begin & end
			iNum += 0x10;
		}

		//whole code assumes SD ATR file!!!
	}

	return iNum;
}