-
Notifications
You must be signed in to change notification settings - Fork 2
/
dck.c
240 lines (195 loc) · 6.55 KB
/
dck.c
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
/* dck.c: Routines for handling Warajevo DCK files
Copyright (c) 2003-2015 Darren Salt, Fredrick Meunier
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
(at your option) 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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: [email protected]
Darren: [email protected]
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "internals.h"
static const int DCK_PAGE_SIZE = 0x2000;
/* Initialise a libspectrum_dck_block structure */
static void
libspectrum_dck_block_alloc( libspectrum_dck_block **dck )
{
size_t i;
*dck = libspectrum_new( libspectrum_dck_block, 1 );
(*dck)->bank = LIBSPECTRUM_DCK_BANK_DOCK;
for( i = 0; i < 8; i++ ) {
(*dck)->access[i] = LIBSPECTRUM_DCK_PAGE_NULL;
(*dck)->pages[i] = NULL;
}
}
/* Free all memory used by a libspectrum_dck_block structure */
static libspectrum_error
libspectrum_dck_block_free( libspectrum_dck_block *dck, int keep_pages )
{
size_t i;
if( !keep_pages )
for( i = 0; i < 8; i++ )
if( dck->pages[i] )
libspectrum_free( dck->pages[i] );
libspectrum_free( dck );
return LIBSPECTRUM_ERROR_NONE;
}
/* Initialise a libspectrum_dck structure (constructor!) */
libspectrum_dck*
libspectrum_dck_alloc( void )
{
libspectrum_dck *dck = libspectrum_new( libspectrum_dck, 1 );
size_t i;
for( i=0; i<256; i++ ) dck->dck[i] = NULL;
return dck;
}
/* Free all memory used by a libspectrum_dck structure (destructor...) */
libspectrum_error
libspectrum_dck_free( libspectrum_dck *dck, int keep_pages )
{
size_t i;
for( i=0; i<256; i++ )
if( dck->dck[i] ) {
libspectrum_dck_block_free( dck->dck[i], keep_pages );
dck->dck[i] = NULL;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* Read in the DCK file */
libspectrum_error
libspectrum_dck_read( libspectrum_dck *dck, const libspectrum_byte *buffer,
const size_t length )
{
return libspectrum_dck_read2( dck, buffer, length, NULL );
}
libspectrum_error
libspectrum_dck_read2( libspectrum_dck *dck, const libspectrum_byte *buffer,
const size_t clength, const char *filename )
{
int i;
int num_dck_block = 0;
libspectrum_error error;
const libspectrum_byte *end;
libspectrum_id_t raw_type;
libspectrum_class_t class;
libspectrum_byte *new_buffer;
size_t length;
/* Ugly but necessary to get around the fact that 'clength' is a const
parameter, but we now modify it */
length = clength;
/* For storing the uncompressed data in if the input file was
compressed */
new_buffer = NULL;
error = libspectrum_identify_file_raw( &raw_type, filename, buffer, length );
if( error ) return error;
error = libspectrum_identify_class( &class, raw_type );
if( error ) return error;
if( class == LIBSPECTRUM_CLASS_COMPRESSED ) {
size_t new_length;
error = libspectrum_uncompress_file( &new_buffer, &new_length, NULL,
raw_type, buffer, length, NULL );
if( error ) return error;
buffer = new_buffer; length = new_length;
}
end = buffer + length;
for( i=0; i<256; i++ ) dck->dck[i]=NULL;
while( buffer < end ) {
int pages = 0;
if( buffer + 9 > end ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_dck_read: not enough data in buffer"
);
error = LIBSPECTRUM_ERROR_CORRUPT;
goto end;
}
switch( buffer[0] ) {
case LIBSPECTRUM_DCK_BANK_DOCK:
case LIBSPECTRUM_DCK_BANK_EXROM:
case LIBSPECTRUM_DCK_BANK_HOME:
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_dck_read: unknown bank ID %d",
buffer[0] );
error = LIBSPECTRUM_ERROR_UNKNOWN;
goto end;
}
for( i = 1; i < 9; i++ )
switch( buffer[i] ) {
case LIBSPECTRUM_DCK_PAGE_NULL:
case LIBSPECTRUM_DCK_PAGE_RAM_EMPTY:
break;
case LIBSPECTRUM_DCK_PAGE_ROM:
case LIBSPECTRUM_DCK_PAGE_RAM:
pages++;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_dck_read: unknown page type %d",
buffer[i] );
error = LIBSPECTRUM_ERROR_UNKNOWN;
goto end;
}
if( buffer + 9 + DCK_PAGE_SIZE * pages > end ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_dck_read: not enough data in buffer"
);
error = LIBSPECTRUM_ERROR_CORRUPT;
goto end;
}
/* Allocate new dck_block */
libspectrum_dck_block_alloc( &dck->dck[num_dck_block] );
/* Copy the bank ID */
dck->dck[num_dck_block]->bank = *buffer++;
/* Copy the page types */
for( i = 0; i < 8; i++ ) dck->dck[num_dck_block]->access[i] = *buffer++;
/* Allocate the pages */
for( i = 0; i < 8; i++ ) {
switch( dck->dck[num_dck_block]->access[i] ) {
case LIBSPECTRUM_DCK_PAGE_NULL:
break;
case LIBSPECTRUM_DCK_PAGE_RAM_EMPTY:
dck->dck[num_dck_block]->pages[i] =
libspectrum_new0( libspectrum_byte, DCK_PAGE_SIZE );
if( !dck->dck[num_dck_block]->pages[i] ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_MEMORY,
"libspectrum_dck_read: out of memory" );
error = LIBSPECTRUM_ERROR_MEMORY;
goto end;
}
break;
case LIBSPECTRUM_DCK_PAGE_ROM:
case LIBSPECTRUM_DCK_PAGE_RAM:
dck->dck[num_dck_block]->pages[i] =
libspectrum_new( libspectrum_byte, DCK_PAGE_SIZE );
memcpy( dck->dck[num_dck_block]->pages[i], buffer, DCK_PAGE_SIZE );
buffer += DCK_PAGE_SIZE;
break;
}
}
num_dck_block++;
if( num_dck_block == 256 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_MEMORY,
"libspectrum_dck_read: more than 256 banks" );
error = LIBSPECTRUM_ERROR_MEMORY;
goto end;
}
}
/* Finished successfully */
error = LIBSPECTRUM_ERROR_NONE;
end:
libspectrum_free( new_buffer );
return error;
}