trunk/src/lib/formats/csw_cas.c
r22561 | r22562 | |
40 | 40 | |
41 | 41 | static int mycaslen; |
42 | 42 | |
43 | | static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) { |
44 | | UINT32 SampleRate; |
45 | | UINT32 NumberOfPulses; |
46 | | UINT8 MajorRevision; |
47 | | UINT8 MinorRevision; |
48 | | UINT8 CompressionType; |
49 | | UINT8 Flags; |
50 | | UINT8 HeaderExtensionLength; |
51 | | UINT8 *gz_ptr = NULL; |
| 43 | static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 44 | { |
| 45 | UINT32 SampleRate; |
| 46 | UINT32 NumberOfPulses; |
| 47 | UINT8 MajorRevision; |
| 48 | UINT8 MinorRevision; |
| 49 | UINT8 CompressionType; |
| 50 | UINT8 Flags; |
| 51 | UINT8 HeaderExtensionLength; |
| 52 | UINT8 *gz_ptr = NULL; |
52 | 53 | |
53 | | int total_size; |
54 | | z_stream d_stream; |
55 | | int err; |
56 | | UINT8 *in_ptr; |
57 | | int bsize=0; |
| 54 | int total_size; |
| 55 | z_stream d_stream; |
| 56 | int err; |
| 57 | UINT8 *in_ptr; |
| 58 | int bsize=0; |
58 | 59 | |
59 | | if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) { |
| 60 | if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) |
| 61 | { |
60 | 62 | LOG_FORMATS( "csw_cas_to_wav_size: cassette image has incompatible header\n" ); |
61 | 63 | goto cleanup; |
62 | 64 | } |
63 | 65 | |
64 | | if (casdata[0x16]!=0x1a) { |
| 66 | if (casdata[0x16]!=0x1a) |
| 67 | { |
65 | 68 | LOG_FORMATS( "csw_cas_to_wav_size: Terminator Code Not Found\n" ); |
66 | 69 | goto cleanup; |
67 | 70 | } |
r22561 | r22562 | |
71 | 74 | |
72 | 75 | LOG_FORMATS("Version %d : %d\n",MajorRevision,MinorRevision); |
73 | 76 | |
74 | | if (casdata[0x17]!=2){ |
| 77 | if (casdata[0x17]!=2) |
| 78 | { |
75 | 79 | LOG_FORMATS( "csw_cas_to_wav_size: Unsuported Major Version\n" ); |
76 | 80 | goto cleanup; |
77 | 81 | } |
r22561 | r22562 | |
110 | 114 | d_stream.data_type=0; |
111 | 115 | |
112 | 116 | err = inflateInit( &d_stream ); |
113 | | if ( err != Z_OK ) { |
| 117 | if ( err != Z_OK ) |
| 118 | { |
114 | 119 | LOG_FORMATS( "inflateInit2 error: %d\n", err ); |
115 | 120 | goto cleanup; |
116 | 121 | } |
r22561 | r22562 | |
122 | 127 | d_stream.next_out = gz_ptr; |
123 | 128 | d_stream.avail_out=1; |
124 | 129 | err=inflate( &d_stream, Z_SYNC_FLUSH ); |
125 | | if (err==Z_OK) { |
| 130 | if (err==Z_OK) |
| 131 | { |
126 | 132 | bsize=gz_ptr[0]; |
127 | | if (bsize==0) { |
| 133 | if (bsize==0) |
| 134 | { |
128 | 135 | d_stream.avail_out=4; |
129 | 136 | d_stream.next_out = gz_ptr; |
130 | 137 | err=inflate( &d_stream, Z_SYNC_FLUSH ); |
r22561 | r22562 | |
135 | 142 | } |
136 | 143 | while (err==Z_OK); |
137 | 144 | |
138 | | if ( err != Z_STREAM_END ) { |
| 145 | if ( err != Z_STREAM_END ) |
| 146 | { |
139 | 147 | LOG_FORMATS( "inflate error: %d\n", err ); |
140 | 148 | goto cleanup; |
141 | 149 | } |
142 | 150 | |
143 | 151 | err = inflateEnd( &d_stream ); |
144 | | if ( err != Z_OK ) { |
| 152 | if ( err != Z_OK ) |
| 153 | { |
145 | 154 | LOG_FORMATS( "inflateEnd error: %d\n", err ); |
146 | 155 | goto cleanup; |
147 | 156 | } |
148 | 157 | |
149 | | |
150 | | |
151 | | |
152 | | |
153 | | |
154 | | if ( gz_ptr ) { |
| 158 | if ( gz_ptr ) |
| 159 | { |
155 | 160 | free( gz_ptr ); |
156 | 161 | gz_ptr = NULL; |
157 | 162 | } |
158 | 163 | |
159 | | |
160 | | |
161 | 164 | return total_size; |
162 | 165 | |
163 | 166 | cleanup: |
164 | | if ( gz_ptr ) { |
| 167 | if ( gz_ptr ) |
| 168 | { |
165 | 169 | free( gz_ptr ); |
166 | 170 | gz_ptr = NULL; |
167 | 171 | } |
168 | 172 | return -1; |
169 | | |
170 | 173 | } |
171 | 174 | |
172 | | static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) { |
173 | | UINT32 SampleRate; |
174 | | UINT32 NumberOfPulses; |
175 | | UINT8 CompressionType; |
176 | | UINT8 Flags; |
177 | | UINT8 HeaderExtensionLength; |
178 | | INT8 Bit; |
| 175 | static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 176 | { |
| 177 | UINT32 SampleRate; |
| 178 | UINT32 NumberOfPulses; |
| 179 | UINT8 CompressionType; |
| 180 | UINT8 Flags; |
| 181 | UINT8 HeaderExtensionLength; |
| 182 | INT8 Bit; |
179 | 183 | |
180 | | UINT8 *gz_ptr = NULL; |
181 | | int total_size; |
182 | | z_stream d_stream; |
183 | | int err; |
184 | | UINT8 *in_ptr; |
185 | | int bsize=0; |
186 | | int i; |
| 184 | UINT8 *gz_ptr = NULL; |
| 185 | int total_size; |
| 186 | z_stream d_stream; |
| 187 | int err; |
| 188 | UINT8 *in_ptr; |
| 189 | int bsize=0; |
| 190 | int i; |
187 | 191 | |
188 | 192 | |
189 | 193 | LOG_FORMATS("Length %d\n",length); |
r22561 | r22562 | |
201 | 205 | if ((Flags&0)==0) |
202 | 206 | { |
203 | 207 | Bit=-100; |
204 | | } else { |
| 208 | } |
| 209 | else |
| 210 | { |
205 | 211 | Bit=100; |
206 | 212 | } |
207 | 213 | |
r22561 | r22562 | |
213 | 219 | |
214 | 220 | gz_ptr = (UINT8*)malloc( 8 ); |
215 | 221 | |
216 | | |
217 | 222 | d_stream.next_in = (unsigned char *)in_ptr; |
218 | 223 | d_stream.avail_in = mycaslen - ( in_ptr - bytes ); |
219 | 224 | d_stream.total_in=0; |
r22561 | r22562 | |
228 | 233 | d_stream.data_type=0; |
229 | 234 | |
230 | 235 | err = inflateInit( &d_stream ); |
231 | | if ( err != Z_OK ) { |
| 236 | if ( err != Z_OK ) |
| 237 | { |
232 | 238 | LOG_FORMATS( "inflateInit2 error: %d\n", err ); |
233 | 239 | goto cleanup; |
234 | 240 | } |
r22561 | r22562 | |
240 | 246 | d_stream.next_out = gz_ptr; |
241 | 247 | d_stream.avail_out=1; |
242 | 248 | err=inflate( &d_stream, Z_SYNC_FLUSH ); |
243 | | if (err==Z_OK) { |
| 249 | if (err==Z_OK) |
| 250 | { |
244 | 251 | bsize=gz_ptr[0]; |
245 | | if (bsize==0) { |
| 252 | if (bsize==0) |
| 253 | { |
246 | 254 | d_stream.avail_out=4; |
247 | 255 | d_stream.next_out = gz_ptr; |
248 | 256 | err=inflate( &d_stream, Z_SYNC_FLUSH ); |
r22561 | r22562 | |
257 | 265 | } |
258 | 266 | while (err==Z_OK); |
259 | 267 | |
260 | | if ( err != Z_STREAM_END ) { |
| 268 | if ( err != Z_STREAM_END ) |
| 269 | { |
261 | 270 | LOG_FORMATS( "inflate error: %d\n", err ); |
262 | 271 | goto cleanup; |
263 | 272 | } |
264 | 273 | |
265 | 274 | err = inflateEnd( &d_stream ); |
266 | | if ( err != Z_OK ) { |
| 275 | if ( err != Z_OK ) |
| 276 | { |
267 | 277 | LOG_FORMATS( "inflateEnd error: %d\n", err ); |
268 | 278 | goto cleanup; |
269 | 279 | } |
270 | 280 | |
271 | | |
272 | | |
273 | | |
274 | | |
275 | | |
276 | | if ( gz_ptr ) { |
| 281 | if ( gz_ptr ) |
| 282 | { |
277 | 283 | free( gz_ptr ); |
278 | 284 | gz_ptr = NULL; |
279 | 285 | } |
280 | 286 | |
281 | | |
282 | | |
283 | | |
284 | 287 | return length; |
285 | 288 | |
286 | | |
287 | | |
288 | | cleanup: |
289 | | if ( gz_ptr ) { |
290 | | free( gz_ptr ); |
291 | | gz_ptr = NULL; |
292 | | } |
| 289 | cleanup: |
| 290 | if ( gz_ptr ) |
| 291 | { |
| 292 | free( gz_ptr ); |
| 293 | gz_ptr = NULL; |
| 294 | } |
293 | 295 | return -1; |
294 | 296 | } |
295 | 297 | |
r22561 | r22562 | |
304 | 306 | 0 /* trailer_samples */ |
305 | 307 | }; |
306 | 308 | |
307 | | static casserr_t csw_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) { |
| 309 | static casserr_t csw_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 310 | { |
308 | 311 | return cassette_legacy_identify( cassette, opts, &csw_legacy_fill_wave ); |
309 | 312 | } |
310 | 313 | |
311 | | static casserr_t csw_cassette_load( cassette_image *cassette ) { |
| 314 | static casserr_t csw_cassette_load( cassette_image *cassette ) |
| 315 | { |
312 | 316 | return cassette_legacy_construct( cassette, &csw_legacy_fill_wave ); |
313 | 317 | } |
314 | 318 | |