src/mess/tools/imgtool/modules/imgwave.c
| r13274 | r13275 | |
| 1 | | #include <string.h> |
| 2 | | #include "imgtoolx.h" |
| 3 | | |
| 4 | | typedef struct { |
| 5 | | imgtool_image base; |
| 6 | | imgtool_stream *f; |
| 7 | | int basepos; |
| 8 | | int length; |
| 9 | | int curpos; |
| 10 | | |
| 11 | | int channels; |
| 12 | | int resolution; |
| 13 | | int frequency; |
| 14 | | INT16 lastsample; |
| 15 | | } waveimage; |
| 16 | | |
| 17 | | typedef struct { |
| 18 | | imgtool_directory base; |
| 19 | | waveimage *wimg; |
| 20 | | int pos; |
| 21 | | } waveimageenum; |
| 22 | | |
| 23 | | static int find_wavtag(imgtool_stream *f, int filelen, const char *tag, int *offset, UINT32 *blocklen) |
| 24 | | { |
| 25 | | char buf[4]; |
| 26 | | |
| 27 | | while(1) { |
| 28 | | *offset += stream_read(f, buf, sizeof(buf)); |
| 29 | | *offset += stream_read(f, blocklen, sizeof(*blocklen)); |
| 30 | | *blocklen = LITTLE_ENDIANIZE_INT32(*blocklen); |
| 31 | | |
| 32 | | if (!memcmp(buf, tag, 4)) |
| 33 | | break; |
| 34 | | |
| 35 | | stream_seek(f, *blocklen, SEEK_CUR); |
| 36 | | *offset += *blocklen; |
| 37 | | |
| 38 | | if (*offset >= filelen) |
| 39 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 40 | | } |
| 41 | | return 0; |
| 42 | | } |
| 43 | | |
| 44 | | int imgwave_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 45 | | { |
| 46 | | int err; |
| 47 | | waveimage *wimg; |
| 48 | | struct WaveExtra *extra; |
| 49 | | char buf[4]; |
| 50 | | UINT16 format, channels, resolution; |
| 51 | | UINT32 filelen, blocklen, frequency; |
| 52 | | int offset = 0; |
| 53 | | |
| 54 | | offset += stream_read(f, buf, sizeof(buf)); |
| 55 | | if (offset < 4) |
| 56 | | goto initalt; |
| 57 | | if (memcmp(buf, "RIFF", 4)) |
| 58 | | goto initalt; |
| 59 | | |
| 60 | | offset += stream_read(f, &filelen, sizeof(filelen)); |
| 61 | | if (offset < 8) |
| 62 | | goto initalt; |
| 63 | | filelen = LITTLE_ENDIANIZE_INT32(filelen); |
| 64 | | |
| 65 | | offset += stream_read(f, buf, sizeof(buf)); |
| 66 | | if (offset < 12) |
| 67 | | goto initalt; |
| 68 | | if (memcmp(buf, "WAVE", 4)) |
| 69 | | goto initalt; |
| 70 | | |
| 71 | | err = find_wavtag(f, filelen, "fmt ", &offset, &blocklen); |
| 72 | | if (err) |
| 73 | | return err; |
| 74 | | |
| 75 | | offset += stream_read(f, &format, sizeof(format)); |
| 76 | | format = LITTLE_ENDIANIZE_INT16(format); |
| 77 | | if (format != 1) |
| 78 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 79 | | |
| 80 | | offset += stream_read(f, &channels, sizeof(channels)); |
| 81 | | channels = LITTLE_ENDIANIZE_INT16(channels); |
| 82 | | if (channels != 1 && channels != 2) |
| 83 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 84 | | |
| 85 | | offset += stream_read(f, &frequency, sizeof(frequency)); |
| 86 | | frequency = LITTLE_ENDIANIZE_INT32(frequency); |
| 87 | | |
| 88 | | stream_seek(f, 6, SEEK_CUR); |
| 89 | | |
| 90 | | offset += stream_read(f, &resolution, sizeof(resolution)); |
| 91 | | resolution = LITTLE_ENDIANIZE_INT16(resolution); |
| 92 | | if ((resolution != 8) && (resolution != 16)) |
| 93 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 94 | | |
| 95 | | stream_seek(f, blocklen - 16, SEEK_CUR); |
| 96 | | |
| 97 | | err = find_wavtag(f, filelen, "data", &offset, &blocklen); |
| 98 | | if (err) |
| 99 | | return err; |
| 100 | | |
| 101 | | wimg = malloc(sizeof(waveimage)); |
| 102 | | if (!wimg) |
| 103 | | return IMGTOOLERR_OUTOFMEMORY; |
| 104 | | |
| 105 | | wimg->base.module = mod; |
| 106 | | wimg->f = f; |
| 107 | | wimg->basepos = wimg->curpos = offset; |
| 108 | | wimg->length = blocklen; |
| 109 | | wimg->channels = channels; |
| 110 | | wimg->frequency = frequency; |
| 111 | | wimg->resolution = resolution; |
| 112 | | wimg->lastsample = 0; |
| 113 | | *outimg = &wimg->base; |
| 114 | | return 0; |
| 115 | | |
| 116 | | initalt: |
| 117 | | extra = (struct WaveExtra *) mod->extra; |
| 118 | | if (!extra->initalt) |
| 119 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 120 | | |
| 121 | | stream_seek(f, 0, SEEK_SET); |
| 122 | | |
| 123 | | wimg = malloc(sizeof(waveimage)); |
| 124 | | if (!wimg) |
| 125 | | return IMGTOOLERR_OUTOFMEMORY; |
| 126 | | |
| 127 | | |
| 128 | | err = extra->initalt(f, &wimg->f, &wimg->basepos, &wimg->length, |
| 129 | | &wimg->channels, &wimg->frequency, &wimg->resolution); |
| 130 | | if (err) { |
| 131 | | free(wimg); |
| 132 | | return err; |
| 133 | | } |
| 134 | | |
| 135 | | wimg->base.module = mod; |
| 136 | | wimg->curpos = wimg->basepos; |
| 137 | | wimg->lastsample = 0; |
| 138 | | stream_seek(wimg->f, wimg->basepos, SEEK_SET); |
| 139 | | *outimg = &wimg->base; |
| 140 | | |
| 141 | | if (wimg->f != f) |
| 142 | | stream_close(f); |
| 143 | | |
| 144 | | return 0; |
| 145 | | } |
| 146 | | |
| 147 | | void imgwave_exit(imgtool_image *img) |
| 148 | | { |
| 149 | | waveimage *wimg = (waveimage *) img; |
| 150 | | stream_close(wimg->f); |
| 151 | | free(wimg); |
| 152 | | } |
| 153 | | |
| 154 | | int imgwave_seek(imgtool_image *img, int pos) |
| 155 | | { |
| 156 | | waveimage *wimg = (waveimage *) img; |
| 157 | | |
| 158 | | if (wimg->curpos != pos) { |
| 159 | | stream_seek(wimg->f, pos, SEEK_SET); |
| 160 | | wimg->curpos = pos; |
| 161 | | } |
| 162 | | return 0; |
| 163 | | } |
| 164 | | |
| 165 | | static int imgwave_readsample(imgtool_image *img, INT16 *sample) |
| 166 | | { |
| 167 | | int len; |
| 168 | | INT16 s = 0; |
| 169 | | waveimage *wimg = (waveimage *) img; |
| 170 | | |
| 171 | | union { |
| 172 | | UINT8 buf8[2]; |
| 173 | | INT16 buf16[2]; |
| 174 | | } u; |
| 175 | | |
| 176 | | if (wimg->curpos >= wimg->basepos + wimg->length) { |
| 177 | | *sample = 0; |
| 178 | | return IMGTOOLERR_INPUTPASTEND; |
| 179 | | } |
| 180 | | |
| 181 | | if (wimg->resolution == 8) { |
| 182 | | len = stream_read(wimg->f, u.buf8, wimg->channels); |
| 183 | | wimg->curpos += len; |
| 184 | | if (len < wimg->channels) |
| 185 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 186 | | |
| 187 | | switch(wimg->channels) { |
| 188 | | case 1: |
| 189 | | s = ((INT16) u.buf8[0]) - 0x80; |
| 190 | | s *= 0x100; |
| 191 | | break; |
| 192 | | case 2: |
| 193 | | s = ((INT16) u.buf8[0]) - 0x80; |
| 194 | | s += ((INT16) u.buf8[1]) - 0x80; |
| 195 | | s *= 0x80; |
| 196 | | break; |
| 197 | | } |
| 198 | | } |
| 199 | | else { |
| 200 | | len = stream_read(wimg->f, u.buf16, wimg->channels * 2); |
| 201 | | wimg->curpos += len; |
| 202 | | if (len < (wimg->channels * 2)) |
| 203 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 204 | | |
| 205 | | switch(wimg->channels) { |
| 206 | | case 1: |
| 207 | | s = u.buf16[0]; |
| 208 | | break; |
| 209 | | case 2: |
| 210 | | s = (u.buf16[0] / 2) + (u.buf16[1] / 2); |
| 211 | | break; |
| 212 | | } |
| 213 | | } |
| 214 | | *sample = s; |
| 215 | | return 0; |
| 216 | | } |
| 217 | | |
| 218 | | static int imgwave_readtransition(imgtool_image *img, int *frequency) |
| 219 | | { |
| 220 | | int err; |
| 221 | | int count = 0; |
| 222 | | int transitioned; |
| 223 | | INT16 sample; |
| 224 | | waveimage *wimg; |
| 225 | | |
| 226 | | wimg = (waveimage *) img; |
| 227 | | |
| 228 | | do { |
| 229 | | err = imgwave_readsample(img, &sample); |
| 230 | | if (err) |
| 231 | | return err; |
| 232 | | |
| 233 | | /* Problem - I should be checking for both types of transitions, somehow */ |
| 234 | | #if 0 |
| 235 | | transitioned = (wimg->lastsample >= 0) && (sample < 0); |
| 236 | | #else |
| 237 | | transitioned = (wimg->lastsample < 0) && (sample >= 0); |
| 238 | | #endif |
| 239 | | wimg->lastsample = sample; |
| 240 | | count++; |
| 241 | | } |
| 242 | | while(!transitioned); |
| 243 | | |
| 244 | | *frequency = wimg->frequency / count; |
| 245 | | return 0; |
| 246 | | } |
| 247 | | |
| 248 | | static int imgwave_readbit(imgtool_image *img, UINT8 *bit) |
| 249 | | { |
| 250 | | int err, freq; |
| 251 | | waveimage *wimg; |
| 252 | | struct WaveExtra *extra; |
| 253 | | |
| 254 | | wimg = (waveimage *) img; |
| 255 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 256 | | |
| 257 | | err = imgwave_readtransition(img, &freq); |
| 258 | | if (err) |
| 259 | | return err; |
| 260 | | |
| 261 | | if (extra->zeropulse > extra->onepulse) |
| 262 | | *bit = extra->threshpulse > freq; |
| 263 | | else |
| 264 | | *bit = extra->threshpulse <= freq; |
| 265 | | return 0; |
| 266 | | } |
| 267 | | |
| 268 | | int imgwave_forward(imgtool_image *img) |
| 269 | | { |
| 270 | | int err; |
| 271 | | int i; |
| 272 | | UINT8 carry, newcarry; |
| 273 | | UINT8 *buffer; |
| 274 | | waveimage *wimg; |
| 275 | | struct WaveExtra *extra; |
| 276 | | |
| 277 | | wimg = (waveimage *) img; |
| 278 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 279 | | |
| 280 | | buffer = malloc(extra->blockheadersize); |
| 281 | | if (!buffer) |
| 282 | | return IMGTOOLERR_OUTOFMEMORY; |
| 283 | | |
| 284 | | memset(buffer, 0, extra->blockheadersize); |
| 285 | | |
| 286 | | do { |
| 287 | | err = imgwave_readbit(img, &carry); |
| 288 | | if (err) { |
| 289 | | free(buffer); |
| 290 | | return err; |
| 291 | | } |
| 292 | | |
| 293 | | for (i = extra->blockheadersize-1; i >= 0; i--) { |
| 294 | | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 295 | | /* Most significant bit first */ |
| 296 | | newcarry = buffer[i] & 0x80; |
| 297 | | buffer[i] <<= 1; |
| 298 | | if (carry) |
| 299 | | buffer[i] |= 1; |
| 300 | | } |
| 301 | | else { |
| 302 | | /* Least significant bit first */ |
| 303 | | newcarry = buffer[i] & 0x01; |
| 304 | | buffer[i] >>= 1; |
| 305 | | if (carry) |
| 306 | | buffer[i] |= 0x80; |
| 307 | | } |
| 308 | | carry = newcarry; |
| 309 | | } |
| 310 | | } |
| 311 | | while(memcmp(buffer, extra->blockheader, extra->blockheadersize)); |
| 312 | | |
| 313 | | free(buffer); |
| 314 | | return 0; |
| 315 | | } |
| 316 | | |
| 317 | | int imgwave_read(imgtool_image *img, UINT8 *buf, int bufsize) |
| 318 | | { |
| 319 | | int err; |
| 320 | | int i, j; |
| 321 | | UINT8 b, bit; |
| 322 | | waveimage *wimg; |
| 323 | | struct WaveExtra *extra; |
| 324 | | |
| 325 | | wimg = (waveimage *) img; |
| 326 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 327 | | |
| 328 | | for (i = 0; i < bufsize; i++) { |
| 329 | | b = 0; |
| 330 | | for (j = 0; j < 8; j++) { |
| 331 | | err = imgwave_readbit(img, &bit); |
| 332 | | if (err) |
| 333 | | return err; |
| 334 | | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 335 | | /* Most significant bit first */ |
| 336 | | b <<= 1; |
| 337 | | if (bit) |
| 338 | | b |= 1; |
| 339 | | } |
| 340 | | else { |
| 341 | | /* Least significant bit first */ |
| 342 | | b >>= 1; |
| 343 | | if (bit) |
| 344 | | b |= 0x80; |
| 345 | | } |
| 346 | | } |
| 347 | | *(buf++) = b; |
| 348 | | } |
| 349 | | return 0; |
| 350 | | } |
| 351 | | |
| 352 | | int imgwave_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 353 | | { |
| 354 | | /* TODO - Enumerating wave images should be cached */ |
| 355 | | |
| 356 | | waveimage *wimg = (waveimage *) img; |
| 357 | | waveimageenum *wenum; |
| 358 | | |
| 359 | | wenum = malloc(sizeof(waveimageenum)); |
| 360 | | if (!wenum) |
| 361 | | return IMGTOOLERR_OUTOFMEMORY; |
| 362 | | |
| 363 | | wenum->base.module = img->module; |
| 364 | | wenum->wimg = wimg; |
| 365 | | wenum->pos = wimg->basepos; |
| 366 | | *outenum = (imgtool_directory *) wenum; |
| 367 | | return 0; |
| 368 | | } |
| 369 | | |
| 370 | | int imgwave_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 371 | | { |
| 372 | | int err; |
| 373 | | waveimageenum *wenum; |
| 374 | | |
| 375 | | wenum = (waveimageenum *) enumeration; |
| 376 | | |
| 377 | | err = imgwave_seek((imgtool_image *) wenum->wimg, wenum->pos); |
| 378 | | if (err) |
| 379 | | return err; |
| 380 | | |
| 381 | | err = ((struct WaveExtra *) wenum->wimg->base.module->extra)->nextfile((imgtool_image *) wenum->wimg, ent); |
| 382 | | if (err) |
| 383 | | return err; |
| 384 | | |
| 385 | | wenum->pos = wenum->wimg->curpos; |
| 386 | | return 0; |
| 387 | | } |
| 388 | | |
| 389 | | void imgwave_closeenum(imgtool_directory *enumeration) |
| 390 | | { |
| 391 | | waveimageenum *wenum = (waveimageenum *) enumeration; |
| 392 | | free(wenum); |
| 393 | | } |
| 394 | | |
| 395 | | int imgwave_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 396 | | { |
| 397 | | int err, pos; |
| 398 | | imgtool_dirent ent; |
| 399 | | imgtool_directory *enumeration; |
| 400 | | waveimageenum *wenum; |
| 401 | | waveimage *wimg; |
| 402 | | struct WaveExtra *extra; |
| 403 | | char fnamebuf[256]; |
| 404 | | |
| 405 | | wimg = (waveimage *) img; |
| 406 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 407 | | |
| 408 | | err = imgwave_beginenum(img, &enumeration); |
| 409 | | if (err) |
| 410 | | return err; |
| 411 | | wenum = (waveimageenum *) enumeration; |
| 412 | | |
| 413 | | memset(&ent, 0, sizeof(ent)); |
| 414 | | ent.fname = fnamebuf; |
| 415 | | ent.fname_len = sizeof(fnamebuf); |
| 416 | | |
| 417 | | do { |
| 418 | | pos = wenum->pos; |
| 419 | | err = imgwave_nextenum(enumeration, &ent); |
| 420 | | if (ent.eof) |
| 421 | | err = IMGTOOLERR_FILENOTFOUND; |
| 422 | | if (err) { |
| 423 | | imgwave_closeenum(enumeration); |
| 424 | | return err; |
| 425 | | } |
| 426 | | } |
| 427 | | while(strcmp(ent.fname, fname)); |
| 428 | | |
| 429 | | imgwave_closeenum(enumeration); |
| 430 | | enumeration = NULL; |
| 431 | | |
| 432 | | err = imgwave_seek(img, pos); |
| 433 | | if (err) |
| 434 | | return err; |
| 435 | | |
| 436 | | err = extra->readfile(img, destf); |
| 437 | | if (err) |
| 438 | | return err; |
| 439 | | |
| 440 | | return 0; |
| 441 | | } |
| 442 | | |
| 443 | | |
src/mess/tools/imgtool/modules/crt.c
| r13274 | r13275 | |
| 1 | | #include <stdio.h> |
| 2 | | #include <string.h> |
| 3 | | #include <stdlib.h> |
| 4 | | #include "zlib.h" |
| 5 | | #include "imgtoolx.h" |
| 6 | | |
| 7 | | #ifdef LSB_FIRST |
| 8 | | typedef struct { |
| 9 | | unsigned char high, low; |
| 10 | | } biguword; |
| 11 | | typedef struct { |
| 12 | | unsigned char highest, high, mid, low; |
| 13 | | } bigulong; |
| 14 | | #define GET_UWORD(a) (a.low|(a.high<<8)) |
| 15 | | #define GET_ULONG(a) (a.low|(a.mid<<8)|(a.high<<16)|(a.highest<<24)) |
| 16 | | #define SET_UWORD(a,v) a.low=(v)&0xff;a.high=((v)>>8)&0xff |
| 17 | | #define SET_ULONG(a,v) a.low=(v)&0xff;a.mid=((v)>>8)&0xff;a.high=((v)>>16)&0xff;a.highest=((v)>>24)&0xff |
| 18 | | #else |
| 19 | | typedef UINT32 bigulong; |
| 20 | | typedef UINT16 biguword; |
| 21 | | #define GET_UWORD(a) a |
| 22 | | #define GET_ULONG(a) a |
| 23 | | #define SET_UWORD(a,v) a=v |
| 24 | | #define SET_ULONG(a,v) a=v |
| 25 | | #endif |
| 26 | | |
| 27 | | /* format description found in the ccs64 emulator |
| 28 | | |
| 29 | | CARTRIDGE FILE FORMAT FOR CCS64 (using fileextension .CRT) |
| 30 | | |
| 31 | | 0000 'C64 CARTRIDGE ' |
| 32 | | 0010 ULONG fileheader-length (counted from offset 0000, default=0040) |
| 33 | | 0014 UWORD Version (=0100) |
| 34 | | 0016 UWORD Hardware type |
| 35 | | 0018 UBYTE Exrom line |
| 36 | | 0019 UBYTE Game line |
| 37 | | 001A UBYTE[6] For future use... |
| 38 | | 0020 UBYTE[20] Name (null-terminated string) |
| 39 | | 0040 Chip Packets |
| 40 | | ... |
| 41 | | |
| 42 | | Chip Packets: |
| 43 | | |
| 44 | | 0000 'CHIP' |
| 45 | | 0004 ULONG packetlength (counted from offset 0000) |
| 46 | | 0008 UWORD chiptype |
| 47 | | 000A UWORD bank (for normal cartridges = 0) |
| 48 | | 000C UWORD address |
| 49 | | 000E UWORD length |
| 50 | | 0010 UBYTE[] data |
| 51 | | ... |
| 52 | | Hardware Types: |
| 53 | | |
| 54 | | 0 - Normal cartridge |
| 55 | | 1 - Action Replay |
| 56 | | 2 - KCS Power Cartridge |
| 57 | | 3 - Final Cartridge III |
| 58 | | 4 - Simons Basic |
| 59 | | 5 - Ocean type 1 (256 and 128 Kb) |
| 60 | | 6 - Expert Cartridge |
| 61 | | 7 - Fun Play |
| 62 | | 8 - Super Games |
| 63 | | 9 - Atomic Power |
| 64 | | 10 - Epyx Fastload |
| 65 | | 11 - Westermann |
| 66 | | |
| 67 | | |
| 68 | | Chip Types: |
| 69 | | |
| 70 | | 0 - ROM |
| 71 | | 1 - RAM, no data field |
| 72 | | 2 - Flash ROM |
| 73 | | |
| 74 | | |
| 75 | | All UWORD and ULONG's are in (MSB,LSB) format, i.e. $1234 will be $12,$34 in bytes. |
| 76 | | |
| 77 | | EXAMPLE FOR ACTION REPLAY CARTRIDGE |
| 78 | | |
| 79 | | $0000: 'C64 CARTRIDGE ' |
| 80 | | $0010: 00 00 00 40 01 00 00 01 00 00 00 00 00 00 00 00 |
| 81 | | $0020: 'Action Replay V' 00 |
| 82 | | $0030: 00 00 ... 00 |
| 83 | | |
| 84 | | $0040: 'CHIP' |
| 85 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 86 | | $0050: <data 8192 bytes for bank 0>... |
| 87 | | |
| 88 | | $2050: 'CHIP' |
| 89 | | $2054: 00 00 20 10 00 00 00 01 80 00 20 00 |
| 90 | | $2060: <data 8192 bytes for bank 1>... |
| 91 | | |
| 92 | | $4060: 'CHIP' |
| 93 | | $4064: 00 00 20 10 00 00 00 02 80 00 20 00 |
| 94 | | $4070: <data 8192 bytes for bank 2>... |
| 95 | | |
| 96 | | $6070: 'CHIP' |
| 97 | | $6074: 00 00 20 10 00 00 00 03 80 00 20 00 |
| 98 | | $6080: <data 8192 bytes for bank 3>... |
| 99 | | |
| 100 | | $8080: |
| 101 | | |
| 102 | | |
| 103 | | |
| 104 | | EXAMPLE FOR KCS POWER CARTRIDGE |
| 105 | | |
| 106 | | $0000: 'C64 CARTRIDGE ' |
| 107 | | $0010: 00 00 00 40 01 00 00 02 00 00 00 00 00 00 00 00 |
| 108 | | $0020: 'KCS Power Cartridge' 00 |
| 109 | | $0030: 00 00 ... 00 |
| 110 | | |
| 111 | | $0040: 'CHIP' |
| 112 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 113 | | $0050: <data 8192 bytes for 8000-9fff>... |
| 114 | | |
| 115 | | $2050: 'CHIP' |
| 116 | | $2054: 00 00 20 10 00 00 00 00 A0 00 20 00 |
| 117 | | $2060: <data 8192 bytes for a000-bfff>... |
| 118 | | |
| 119 | | $4060: |
| 120 | | |
| 121 | | |
| 122 | | |
| 123 | | EXAMPLE FOR FINAL CARTRIDGE III |
| 124 | | |
| 125 | | $0000: 'C64 CARTRIDGE ' |
| 126 | | $0010: 00 00 00 40 01 00 00 03 01 01 00 00 00 00 00 00 |
| 127 | | $0020: 'Final cartridge' 00 |
| 128 | | $0030: 00 00 ... 00 |
| 129 | | |
| 130 | | $0040: 'CHIP' |
| 131 | | $0044: 00 00 40 10 00 00 00 00 80 00 40 00 |
| 132 | | $0050: <data 16384 bytes for bank 0>... |
| 133 | | |
| 134 | | $4050: 'CHIP' |
| 135 | | $4054: 00 00 40 10 00 00 00 01 80 00 40 00 |
| 136 | | $4060: <data 16384 bytes for bank 1>... |
| 137 | | |
| 138 | | $8060: 'CHIP' |
| 139 | | $8064: 00 00 40 10 00 00 00 02 80 00 40 00 |
| 140 | | $8070: <data 16384 bytes for bank 2>... |
| 141 | | |
| 142 | | $C070: 'CHIP' |
| 143 | | $C074: 00 00 40 10 00 00 00 03 80 00 40 00 |
| 144 | | $C080: <data 16384 bytes for bank 3>... |
| 145 | | |
| 146 | | $10080: |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | EXAMPLE FOR SIMONS BASIC |
| 151 | | |
| 152 | | $0000: 'C64 CARTRIDGE ' |
| 153 | | $0010: 00 00 00 40 01 00 00 04 00 01 00 00 00 00 00 00 |
| 154 | | $0020: 'Simons Basic' 00 |
| 155 | | $0030: 00 00 ... 00 |
| 156 | | |
| 157 | | $0040: 'CHIP' |
| 158 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 159 | | $0050: <data 8192 bytes for 8000-9fff>... |
| 160 | | |
| 161 | | $2050: 'CHIP' |
| 162 | | $2054: 00 00 20 10 00 00 00 00 A0 00 20 00 |
| 163 | | $2060: <data 8192 bytes for a000-bfff>... |
| 164 | | |
| 165 | | $4060: |
| 166 | | |
| 167 | | |
| 168 | | |
| 169 | | EXAMPLE FOR OCEAN TYPE1 |
| 170 | | |
| 171 | | $0000: 'C64 CARTRIDGE ' |
| 172 | | $0010: 00 00 00 40 01 00 00 05 00 00 00 00 00 00 00 00 |
| 173 | | $0020: 'Robocop2' 00 |
| 174 | | $0030: 00 00 ... 00 |
| 175 | | |
| 176 | | $0040: 'CHIP' |
| 177 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 178 | | $0050: <data 8192 bytes for 8000-9fff, bank 0>... |
| 179 | | |
| 180 | | $2050: 'CHIP' |
| 181 | | $2054: 00 00 20 10 00 00 00 01 80 00 20 00 |
| 182 | | $2060: <data 8192 bytes for 8000-9fff, bank 1>... |
| 183 | | ... |
| 184 | | $20140: 'CHIP' |
| 185 | | $20144: 00 00 20 10 00 00 00 10 A0 00 20 00 |
| 186 | | $20150: <data 8192 bytes for a000-bfff, bank 16>... |
| 187 | | |
| 188 | | $22150: 'CHIP' |
| 189 | | $22154: 00 00 20 10 00 00 00 11 A0 00 20 00 |
| 190 | | $22160: <data 8192 bytes for a000-bfff, bank 17>... |
| 191 | | ... |
| 192 | | $40240: |
| 193 | | |
| 194 | | |
| 195 | | |
| 196 | | EXAMPLE FOR FUN PLAY TYPE |
| 197 | | |
| 198 | | $0000: 'C64 CARTRIDGE ' |
| 199 | | $0010: 00 00 00 40 01 00 00 07 00 00 00 00 00 00 00 00 |
| 200 | | $0020: 'FUN PLAY' 00 00 00 00 00 00 00 00 |
| 201 | | $0030: 00 00 ... 00 |
| 202 | | |
| 203 | | $0040: 'CHIP' |
| 204 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 205 | | $0050: <data 8192 bytes for 8000-9fff, bank 0>... |
| 206 | | |
| 207 | | $2050: 'CHIP' |
| 208 | | $2054: 00 00 20 10 00 00 00 08 80 00 20 00 |
| 209 | | $2060: <data 8192 bytes for 8000-9fff, bank 1>... |
| 210 | | |
| 211 | | $4060: 'CHIP' |
| 212 | | $2054: 00 00 20 10 00 00 00 10 80 00 20 00 |
| 213 | | $2060: <data 8192 bytes for 8000-9fff, bank 2>... |
| 214 | | ... |
| 215 | | |
| 216 | | $1E130: 'CHIP' |
| 217 | | $1E134: 00 00 20 10 00 00 00 39 80 00 20 00 |
| 218 | | $1E140: <data 8192 bytes for 8000-9fff, bank 15>... |
| 219 | | |
| 220 | | $20140: |
| 221 | | |
| 222 | | |
| 223 | | |
| 224 | | EXAMPLE FOR SUPER GAMES TYPE |
| 225 | | |
| 226 | | $0000: 'C64 CARTRIDGE ' |
| 227 | | $0010: 00 00 00 40 01 00 00 08 00 00 00 00 00 00 00 00 |
| 228 | | $0020: 'SUPER GAMES' 00 00 00 00 00 |
| 229 | | $0030: 00 00 ... 00 |
| 230 | | |
| 231 | | $0040: 'CHIP' |
| 232 | | $0044: 00 00 40 10 00 00 00 00 80 00 40 00 |
| 233 | | $0050: <data 16384 bytes for 8000-bfff, bank 0>... |
| 234 | | |
| 235 | | $4050: 'CHIP' |
| 236 | | $4054: 00 00 40 10 00 00 00 01 80 00 40 00 |
| 237 | | $4060: <data 16384 bytes for 8000-bfff, bank 0>... |
| 238 | | |
| 239 | | $8060: 'CHIP' |
| 240 | | $8064: 00 00 40 10 00 00 00 02 80 00 40 00 |
| 241 | | $8070: <data 16384 bytes for 8000-bfff, bank 0>... |
| 242 | | |
| 243 | | $C070: 'CHIP' |
| 244 | | $C074: 00 00 40 10 00 00 00 03 80 00 40 00 |
| 245 | | $C080: <data 16384 bytes for 8000-bfff, bank 0>... |
| 246 | | |
| 247 | | $10080: |
| 248 | | |
| 249 | | |
| 250 | | |
| 251 | | EXAMPLE FOR ATOMIC POWER CARTRIDGE |
| 252 | | |
| 253 | | $0000: 'C64 CARTRIDGE ' |
| 254 | | $0010: 00 00 00 40 01 00 00 09 00 00 00 00 00 00 00 00 |
| 255 | | $0020: 'Atomic Power' 00 |
| 256 | | $0030: 00 00 ... 00 |
| 257 | | |
| 258 | | $0040: 'CHIP' |
| 259 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 260 | | $0050: <data 8192 bytes for bank 0>... |
| 261 | | |
| 262 | | $2050: 'CHIP' |
| 263 | | $2054: 00 00 20 10 00 00 00 01 80 00 20 00 |
| 264 | | $2060: <data 8192 bytes for bank 1>... |
| 265 | | |
| 266 | | $4060: 'CHIP' |
| 267 | | $4064: 00 00 20 10 00 00 00 02 80 00 20 00 |
| 268 | | $4070: <data 8192 bytes for bank 2>... |
| 269 | | |
| 270 | | $6070: 'CHIP' |
| 271 | | $6074: 00 00 20 10 00 00 00 03 80 00 20 00 |
| 272 | | $6080: <data 8192 bytes for bank 3>... |
| 273 | | |
| 274 | | $8080: |
| 275 | | |
| 276 | | |
| 277 | | |
| 278 | | EXAMPLE FOR EPYX FASTLOAD TYPE |
| 279 | | |
| 280 | | $0000: 'C64 CARTRIDGE ' |
| 281 | | $0010: 00 00 00 40 01 00 00 0A 01 01 00 00 00 00 00 00 |
| 282 | | $0020: 'EPYX FASTLOAD' 00 00 00 |
| 283 | | $0030: 00 00 ... 00 |
| 284 | | |
| 285 | | $0040: 'CHIP' |
| 286 | | $0044: 00 00 20 10 00 00 00 00 80 00 20 00 |
| 287 | | $0050: <data 8192 bytes for 8000-9fff> |
| 288 | | |
| 289 | | $2050: |
| 290 | | |
| 291 | | |
| 292 | | |
| 293 | | EXAMPLE FOR WESTERMANN TYPE |
| 294 | | |
| 295 | | $0000: 'C64 CARTRIDGE ' |
| 296 | | $0010: 00 00 00 40 01 00 00 0A 00 01 00 00 00 00 00 00 |
| 297 | | $0020: 'Westermann' 00 00 00 |
| 298 | | $0030: 00 00 ... 00 |
| 299 | | |
| 300 | | $0040: 'CHIP' |
| 301 | | $0044: 00 00 40 10 00 00 00 00 80 00 40 00 |
| 302 | | $0050: <data 16384 bytes for 8000-bfff> |
| 303 | | |
| 304 | | $4050: |
| 305 | | */ |
| 306 | | |
| 307 | | static const char *const hardware_types[]={ |
| 308 | | "Normal cartridge", |
| 309 | | "Action Replay", |
| 310 | | "KCS Power Cartridge", |
| 311 | | "Final Cartridge III", |
| 312 | | "Simons Basic", |
| 313 | | "Ocean type 1 (256 and 128 Kb)", |
| 314 | | "Expert Cartridge", |
| 315 | | "Fun Play", |
| 316 | | "Super Games", |
| 317 | | "Atomic Power", |
| 318 | | "Epyx Fastload", |
| 319 | | "Westermann" |
| 320 | | }; |
| 321 | | typedef struct{ |
| 322 | | char id[0x10]; // C64 CARTRIDGE |
| 323 | | bigulong length; |
| 324 | | biguword version; |
| 325 | | biguword hardware_type; |
| 326 | | unsigned char exrom_line; |
| 327 | | unsigned char game_line; |
| 328 | | unsigned char reserved[6]; |
| 329 | | char name[0x20]; |
| 330 | | } crt_header; |
| 331 | | |
| 332 | | static const char *const chip_types[]={ "ROM", "RAM", "FLASH" }; |
| 333 | | typedef struct { |
| 334 | | char id[4]; // CHIP |
| 335 | | bigulong packet_length; |
| 336 | | biguword chip_type; |
| 337 | | biguword bank; |
| 338 | | biguword address; |
| 339 | | biguword length; |
| 340 | | } crt_packet; |
| 341 | | |
| 342 | | typedef struct { |
| 343 | | imgtool_image base; |
| 344 | | imgtool_stream *file_handle; |
| 345 | | int size; |
| 346 | | int modified; |
| 347 | | unsigned char *data; |
| 348 | | } crt_image; |
| 349 | | |
| 350 | | #define HEADER(image) ((crt_header*)image->data) |
| 351 | | #define PACKET(image, pos) ((crt_packet*)(image->data+pos)) |
| 352 | | |
| 353 | | typedef struct { |
| 354 | | imgtool_directory base; |
| 355 | | crt_image *image; |
| 356 | | int pos; |
| 357 | | int number; |
| 358 | | } crt_iterator; |
| 359 | | |
| 360 | | static int crt_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 361 | | static void crt_image_exit(imgtool_image *img); |
| 362 | | static void crt_image_info(imgtool_image *img, char *string, const int len); |
| 363 | | static int crt_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 364 | | static int crt_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 365 | | static void crt_image_closeenum(imgtool_directory *enumeration); |
| 366 | | //static size_t crt_image_freespace(imgtool_image *img); |
| 367 | | static int crt_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 368 | | static int crt_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *_options); |
| 369 | | static int crt_image_deletefile(imgtool_image *img, const char *fname); |
| 370 | | static int crt_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *_options); |
| 371 | | |
| 372 | | /* |
| 373 | | IMAGE_USES_FTYPE|IMAGE_USES_FADDR|IMAGE_USES_FBANK |
| 374 | | |IMAGE_USES_HARDWARE_TYPE|IMAGE_USES_GAME_LINE |
| 375 | | |IMAGE_USES_EXROM_LINE|IMAGE_USES_LABEL, //flags |
| 376 | | */ |
| 377 | | |
| 378 | | static const struct OptionTemplate c64crt_fileeopts[] = |
| 379 | | { |
| 380 | | { "ftype", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0xffff, NULL }, /* [0] */ |
| 381 | | { "faddr", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0xffff, NULL }, /* [1] */ |
| 382 | | { "fbank", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0xffff, NULL }, /* [2] */ |
| 383 | | { NULL, NULL, 0, 0, 0, 0 } |
| 384 | | }; |
| 385 | | |
| 386 | | #define C64CRT_FILEOPTION_FTYPE 0 |
| 387 | | #define C64CRT_FILEOPTION_FADDR 1 |
| 388 | | #define C64CRT_FILEOPTION_FBANK 2 |
| 389 | | |
| 390 | | static const struct OptionTemplate c64crt_createopts[] = |
| 391 | | { |
| 392 | | { "hardwaretype", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0xffff, NULL }, /* [0] */ |
| 393 | | { "gameline", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0x00ff, NULL }, /* [1] */ |
| 394 | | { "exromline", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 0x00ff, NULL }, /* [2] */ |
| 395 | | { "label", NULL, IMGOPTION_FLAG_TYPE_STRING | IMGOPTION_FLAG_HASDEFAULT, 0, 0, NULL }, /* [3] */ |
| 396 | | { NULL, NULL, 0, 0, 0, 0 } |
| 397 | | }; |
| 398 | | |
| 399 | | #define C64CRT_CREATEOPTION_HARDWARETYPE 0 |
| 400 | | #define C64CRT_CREATEOPTION_GAMELINE 1 |
| 401 | | #define C64CRT_CREATEOPTION_EXROMLINE 2 |
| 402 | | #define C64CRT_CREATEOPTION_LABEL 3 |
| 403 | | |
| 404 | | IMAGEMODULE( |
| 405 | | c64crt, |
| 406 | | "Commodore 64 Cartridge", /* human readable name */ |
| 407 | | "crt", /* file extension */ |
| 408 | | "c64.crc", /* crcfile */ |
| 409 | | "c64", /* crc system name */ |
| 410 | | NULL, /* eoln */ |
| 411 | | 0, /* flags */ |
| 412 | | crt_image_init, /* init function */ |
| 413 | | crt_image_exit, /* exit function */ |
| 414 | | crt_image_info, /* info function */ |
| 415 | | crt_image_beginenum, /* begin enumeration */ |
| 416 | | crt_image_nextenum, /* enumerate next */ |
| 417 | | crt_image_closeenum, /* close enumeration */ |
| 418 | | NULL, //crt_image_freespace, /* free space on image */ |
| 419 | | crt_image_readfile, /* read file */ |
| 420 | | crt_image_writefile, /* write file */ |
| 421 | | crt_image_deletefile, /* delete file */ |
| 422 | | crt_image_create, /* create image */ |
| 423 | | NULL, /* read sector */ |
| 424 | | NULL, /* write sector */ |
| 425 | | c64crt_fileeopts, /* file _options */ |
| 426 | | c64crt_createopts /* create options */ |
| 427 | | ) |
| 428 | | |
| 429 | | static int crt_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 430 | | { |
| 431 | | crt_image *image; |
| 432 | | |
| 433 | | image=*(crt_image**)outimg=(crt_image *) malloc(sizeof(crt_image)); |
| 434 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 435 | | |
| 436 | | memset(image, 0, sizeof(crt_image)); |
| 437 | | image->base.module = mod; |
| 438 | | image->size=stream_size(f); |
| 439 | | image->file_handle=f; |
| 440 | | |
| 441 | | image->data = (unsigned char *) malloc(image->size); |
| 442 | | if ( (!image->data) |
| 443 | | ||(stream_read(f, image->data, image->size)!=image->size) ) { |
| 444 | | free(image); |
| 445 | | *outimg=NULL; |
| 446 | | return IMGTOOLERR_OUTOFMEMORY; |
| 447 | | } |
| 448 | | |
| 449 | | return 0; |
| 450 | | } |
| 451 | | |
| 452 | | static void crt_image_exit(imgtool_image *img) |
| 453 | | { |
| 454 | | crt_image *image=(crt_image*)img; |
| 455 | | if (image->modified) { |
| 456 | | stream_clear(image->file_handle); |
| 457 | | stream_write(image->file_handle, image->data, image->size); |
| 458 | | } |
| 459 | | stream_close(image->file_handle); |
| 460 | | free(image->data); |
| 461 | | free(image); |
| 462 | | } |
| 463 | | |
| 464 | | static void crt_image_info(imgtool_image *img, char *string, const int len) |
| 465 | | { |
| 466 | | crt_image *image=(crt_image*)img; |
| 467 | | sprintf(string, "%-32s\nversion:%.4x type:%d:%s exrom:%d game:%d", |
| 468 | | HEADER(image)->name, |
| 469 | | GET_UWORD(HEADER(image)->version), |
| 470 | | GET_UWORD(HEADER(image)->hardware_type), |
| 471 | | hardware_types[GET_UWORD(HEADER(image)->hardware_type)], |
| 472 | | HEADER(image)->exrom_line, HEADER(image)->game_line); |
| 473 | | return; |
| 474 | | } |
| 475 | | |
| 476 | | static int crt_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 477 | | { |
| 478 | | crt_image *image=(crt_image*)img; |
| 479 | | crt_iterator *iter; |
| 480 | | |
| 481 | | iter=*(crt_iterator**)outenum = (crt_iterator *) malloc(sizeof(crt_iterator)); |
| 482 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 483 | | |
| 484 | | iter->base.module = img->module; |
| 485 | | |
| 486 | | iter->image=image; |
| 487 | | iter->pos = GET_ULONG( HEADER(image)->length ); |
| 488 | | iter->number = 0; |
| 489 | | return 0; |
| 490 | | } |
| 491 | | |
| 492 | | static int crt_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 493 | | { |
| 494 | | crt_iterator *iter=(crt_iterator*)enumeration; |
| 495 | | |
| 496 | | ent->corrupt=0; |
| 497 | | |
| 498 | | if (!(ent->eof=(iter->pos>=iter->image->size))) { |
| 499 | | sprintf(ent->fname,"%d", iter->number); |
| 500 | | ent->filesize=GET_UWORD( PACKET(iter->image, iter->pos)->length ); |
| 501 | | if (ent->attr) { |
| 502 | | unsigned crc=crc32(0, iter->image->data+iter->pos+sizeof(crt_packet), ent->filesize); |
| 503 | | sprintf(ent->attr,"%-4s %s bank:%-2d addr:%.4x crc:%8x", |
| 504 | | (char*)PACKET(iter->image, iter->pos), |
| 505 | | chip_types[GET_UWORD(PACKET(iter->image,iter->pos)->chip_type)], |
| 506 | | GET_UWORD( PACKET(iter->image,iter->pos)->bank), |
| 507 | | GET_UWORD( PACKET(iter->image,iter->pos)->address), |
| 508 | | crc); |
| 509 | | } |
| 510 | | iter->number++; |
| 511 | | |
| 512 | | iter->pos+=GET_ULONG( PACKET(iter->image, iter->pos)->packet_length ); |
| 513 | | } |
| 514 | | return 0; |
| 515 | | } |
| 516 | | |
| 517 | | static void crt_image_closeenum(imgtool_directory *enumeration) |
| 518 | | { |
| 519 | | free(enumeration); |
| 520 | | } |
| 521 | | |
| 522 | | #if 0 |
| 523 | | static size_t crt_image_freespace(imgtool_image *img) |
| 524 | | { |
| 525 | | int i; |
| 526 | | rsdos_diskimage *rsimg = (rsdos_diskimage *) img; |
| 527 | | size_t s = 0; |
| 528 | | |
| 529 | | for (i = 0; i < GRANULE_COUNT; i++) |
| 530 | | if (rsimg->granulemap[i] == 0xff) |
| 531 | | s += (9 * 256); |
| 532 | | return s; |
| 533 | | } |
| 534 | | #endif |
| 535 | | |
| 536 | | static int crt_image_findfile(crt_image *image, const char *fname) |
| 537 | | { |
| 538 | | int nr=0; |
| 539 | | char name[6]; |
| 540 | | int i=sizeof(crt_header); |
| 541 | | |
| 542 | | while (i<image->size) { |
| 543 | | sprintf(name, "%d",nr); |
| 544 | | if (!strcmp(fname, name) ) return i; |
| 545 | | i+=GET_ULONG( PACKET(image, i)->packet_length ); |
| 546 | | nr++; |
| 547 | | } |
| 548 | | return 0; |
| 549 | | } |
| 550 | | |
| 551 | | static int crt_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 552 | | { |
| 553 | | crt_image *image=(crt_image*)img; |
| 554 | | int size; |
| 555 | | int pos; |
| 556 | | |
| 557 | | if (!(pos=crt_image_findfile(image, fname)) ) |
| 558 | | return IMGTOOLERR_MODULENOTFOUND; |
| 559 | | |
| 560 | | size=GET_UWORD( PACKET(image, pos)->length ); |
| 561 | | if (stream_write(destf, image->data+pos+sizeof(crt_packet), size)!=size) { |
| 562 | | return IMGTOOLERR_WRITEERROR; |
| 563 | | } |
| 564 | | |
| 565 | | return 0; |
| 566 | | } |
| 567 | | |
| 568 | | static int crt_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, |
| 569 | | const ResolvedOption *_options) |
| 570 | | { |
| 571 | | crt_image *image=(crt_image*)img; |
| 572 | | int size; |
| 573 | | int pos; |
| 574 | | |
| 575 | | size=stream_size(sourcef); |
| 576 | | if (!(pos=crt_image_findfile(image, fname)) ) { |
| 577 | | // appending |
| 578 | | pos=image->size; |
| 579 | | if (!(image->data=realloc(image->data, image->size+size+sizeof(crt_packet))) ) |
| 580 | | return IMGTOOLERR_OUTOFMEMORY; |
| 581 | | image->size+=size+sizeof(crt_packet); |
| 582 | | } else { |
| 583 | | int oldsize=GET_ULONG(PACKET(image,pos)->packet_length); |
| 584 | | // overwriting |
| 585 | | if (!(image->data=realloc(image->data, image->size+size+sizeof(crt_packet)-oldsize) ) ) |
| 586 | | return IMGTOOLERR_OUTOFMEMORY; |
| 587 | | if (image->size-pos-oldsize!=0) { |
| 588 | | memmove(image->data+pos+size+sizeof(crt_packet), image->data+pos+oldsize, |
| 589 | | image->size-pos-oldsize); |
| 590 | | } |
| 591 | | image->size+=size+sizeof(crt_packet)-oldsize; |
| 592 | | } |
| 593 | | if (stream_read(sourcef, image->data+pos+sizeof(crt_packet), size)!=size) { |
| 594 | | return IMGTOOLERR_READERROR; |
| 595 | | } |
| 596 | | memset(image->data+pos, 0, sizeof(crt_packet)); |
| 597 | | memcpy(PACKET(image,pos)->id,"CHIP",4); |
| 598 | | SET_ULONG( PACKET(image, pos)->packet_length, size+sizeof(crt_packet)); |
| 599 | | SET_UWORD(PACKET(image, pos)->chip_type, _options[C64CRT_FILEOPTION_FTYPE].i); |
| 600 | | SET_UWORD( PACKET(image, pos)->address, _options[C64CRT_FILEOPTION_FADDR].i); |
| 601 | | SET_UWORD( PACKET(image, pos)->bank, _options[C64CRT_FILEOPTION_FBANK].i); |
| 602 | | SET_UWORD( PACKET(image, pos)->length, size); |
| 603 | | |
| 604 | | image->modified=1; |
| 605 | | |
| 606 | | return 0; |
| 607 | | } |
| 608 | | |
| 609 | | static int crt_image_deletefile(imgtool_image *img, const char *fname) |
| 610 | | { |
| 611 | | crt_image *image=(crt_image*)img; |
| 612 | | int size; |
| 613 | | int pos; |
| 614 | | |
| 615 | | if (!(pos=crt_image_findfile(image, fname)) ) { |
| 616 | | return IMGTOOLERR_MODULENOTFOUND; |
| 617 | | } |
| 618 | | size=GET_ULONG(PACKET(image, pos)->packet_length); |
| 619 | | if (image->size-pos-size>0) |
| 620 | | memmove(image->data+pos, image->data+pos+size, image->size-pos-size); |
| 621 | | image->size-=size; |
| 622 | | image->modified=1; |
| 623 | | |
| 624 | | return 0; |
| 625 | | } |
| 626 | | |
| 627 | | static int crt_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *_options) |
| 628 | | { |
| 629 | | crt_header header={ "C64 CARTRIDGE " }; |
| 630 | | SET_ULONG(header.length, sizeof(header)); |
| 631 | | SET_UWORD(header.version, 0x100); |
| 632 | | SET_UWORD(header.hardware_type, _options[C64CRT_CREATEOPTION_HARDWARETYPE].i); |
| 633 | | header.game_line=_options[C64CRT_CREATEOPTION_GAMELINE].i; |
| 634 | | header.exrom_line=_options[C64CRT_CREATEOPTION_EXROMLINE].i; |
| 635 | | if (_options[C64CRT_CREATEOPTION_LABEL].s) strcpy(header.name, _options[C64CRT_CREATEOPTION_LABEL].s); |
| 636 | | return (stream_write(f, &header, sizeof(crt_header)) == sizeof(crt_header)) |
| 637 | | ? 0 : IMGTOOLERR_WRITEERROR; |
| 638 | | } |
| 639 | | |
src/mess/tools/imgtool/modules/ti85.c
| r13274 | r13275 | |
| 1 | | #include "imgtoolx.h" |
| 2 | | #include "formats/ti85_ser.h" |
| 3 | | |
| 4 | | static const char *const var_type[] = { |
| 5 | | "Real number", //0x00 |
| 6 | | "Complex number", //0x01 |
| 7 | | "Real vector", //0x02 |
| 8 | | "Complex vector", //0x03 |
| 9 | | "Real list", //0x04 |
| 10 | | "Complex list", //0x05 |
| 11 | | "Real matrix", //0x06 |
| 12 | | "Complex matrix", //0x07 |
| 13 | | "Real constatnt", //0x08 |
| 14 | | "Complex constant", //0x09 |
| 15 | | "Equation", //0x0a |
| 16 | | "Range settings", //0x0b |
| 17 | | "String", //0x0c |
| 18 | | "Graphics database", //0x0d |
| 19 | | "Graphics database", //0x0e |
| 20 | | "Graphics database", //0x0f |
| 21 | | "Graphics database", //0x10 |
| 22 | | "Picture", //0x11 |
| 23 | | "Program", //0x12 |
| 24 | | "Range settings", //0x13 |
| 25 | | "Unknown", //0x14 |
| 26 | | "Range settings", //0x15 |
| 27 | | "Range settings", //0x16 |
| 28 | | "Range settings", //0x17 |
| 29 | | "Range settings", //0x18 |
| 30 | | "Range settings", //0x19 |
| 31 | | "Range settings", //0x1a |
| 32 | | "Range settings" //0x1b |
| 33 | | }; |
| 34 | | |
| 35 | | typedef struct { |
| 36 | | char signature[0x08]; |
| 37 | | unsigned char marker[0x03]; |
| 38 | | char comment[0x2a]; |
| 39 | | unsigned char data_size[0x02]; |
| 40 | | } ti85_header; |
| 41 | | |
| 42 | | typedef struct { |
| 43 | | imgtool_image base; |
| 44 | | imgtool_stream *file_handle; |
| 45 | | unsigned int size; |
| 46 | | int modified; |
| 47 | | UINT8 *data; |
| 48 | | ti85_entry * entries; |
| 49 | | int number_of_entries; |
| 50 | | } ti85_file; |
| 51 | | |
| 52 | | typedef struct { |
| 53 | | imgtool_image base; |
| 54 | | imgtool_stream *file_handle; |
| 55 | | unsigned int size; |
| 56 | | int modified; |
| 57 | | UINT8 *data; |
| 58 | | } ti85b_file; |
| 59 | | |
| 60 | | typedef struct { |
| 61 | | imgtool_directory base; |
| 62 | | ti85_file *image; |
| 63 | | int index; |
| 64 | | } ti85_iterator; |
| 65 | | |
| 66 | | typedef struct { |
| 67 | | imgtool_directory base; |
| 68 | | ti85b_file *image; |
| 69 | | int index; |
| 70 | | } ti85b_iterator; |
| 71 | | |
| 72 | | #define HEADER(file) ((ti85_header*)file->data) |
| 73 | | |
| 74 | | static const struct OptionTemplate ti85_fileoptions[] = |
| 75 | | { |
| 76 | | { "ftype", "File type", IMGOPTION_FLAG_TYPE_INTEGER | IMGOPTION_FLAG_HASDEFAULT,0,27,"12"}, |
| 77 | | { NULL, 0, 0, 0, 0 } |
| 78 | | }; |
| 79 | | |
| 80 | | static const struct OptionTemplate ti85_createoptions[] = |
| 81 | | { |
| 82 | | { "comment", "Comment", IMGOPTION_FLAG_TYPE_STRING | IMGOPTION_FLAG_HASDEFAULT,0,0,"TI-85 file created with ImgTool"}, |
| 83 | | { NULL, NULL, 0, 0, 0, 0 } |
| 84 | | }; |
| 85 | | |
| 86 | | #define TI85_OPTION_FTYPE 0 |
| 87 | | #define TI85_OPTION_COMMENT 0 |
| 88 | | |
| 89 | | static void ti85_file_info(imgtool_image *img, char *string, const int len); |
| 90 | | static int ti85_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 91 | | static void ti85_file_exit(imgtool_image *img); |
| 92 | | static int ti85_file_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 93 | | static int ti85_file_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 94 | | static void ti85_file_closeenum(imgtool_directory *enumeration); |
| 95 | | static int ti85_file_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 96 | | static int ti85_file_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_); |
| 97 | | static int ti85_file_deletefile(imgtool_image *img, const char *fname); |
| 98 | | static int ti85_file_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 99 | | |
| 100 | | static void ti85b_file_info(imgtool_image *img, char *string, const int len); |
| 101 | | static int ti85b_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 102 | | static void ti85b_file_exit(imgtool_image *img); |
| 103 | | static int ti85b_file_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 104 | | static int ti85b_file_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 105 | | static void ti85b_file_closeenum(imgtool_directory *enumeration); |
| 106 | | |
| 107 | | |
| 108 | | #define TI85FILE(name,human_name,extension) \ |
| 109 | | IMAGEMODULE( \ |
| 110 | | name, \ |
| 111 | | human_name, \ |
| 112 | | extension, \ |
| 113 | | NULL, \ |
| 114 | | NULL, \ |
| 115 | | NULL, \ |
| 116 | | 0, \ |
| 117 | | ti85_file_init, \ |
| 118 | | ti85_file_exit, \ |
| 119 | | ti85_file_info, \ |
| 120 | | ti85_file_beginenum, \ |
| 121 | | ti85_file_nextenum, \ |
| 122 | | ti85_file_closeenum, \ |
| 123 | | NULL, \ |
| 124 | | ti85_file_readfile, \ |
| 125 | | ti85_file_writefile, \ |
| 126 | | ti85_file_deletefile, \ |
| 127 | | ti85_file_create, \ |
| 128 | | NULL, \ |
| 129 | | NULL, \ |
| 130 | | ti85_fileoptions, \ |
| 131 | | ti85_createoptions \ |
| 132 | | ) |
| 133 | | |
| 134 | | TI85FILE (ti85p, "TI-85 program file", "85p") |
| 135 | | TI85FILE (ti85s, "TI-85 string file (also ZShell programs)", "85s") |
| 136 | | TI85FILE (ti85i, "TI-85 picture file (85-image)", "85i") |
| 137 | | TI85FILE (ti85n, "TI-85 real number file", "85n") |
| 138 | | TI85FILE (ti85c, "TI-85 complex number file", "85c") |
| 139 | | TI85FILE (ti85l, "TI-85 list (real or complex) file", "85l") |
| 140 | | TI85FILE (ti85k, "TI-85 constant file", "85k") |
| 141 | | TI85FILE (ti85m, "TI-85 matrix (real or complex) file", "85m") |
| 142 | | TI85FILE (ti85v, "TI-85 vector (real or complex) file", "85v") |
| 143 | | TI85FILE (ti85d, "TI-85 graphics database file", "85d") |
| 144 | | TI85FILE (ti85e, "TI-85 equation file", "85e") |
| 145 | | TI85FILE (ti85r, "TI-85 range settings file", "85r") |
| 146 | | TI85FILE (ti85g, "TI-85 grouped file", "85g") |
| 147 | | TI85FILE (ti85, "TI-85 file", "85?") |
| 148 | | TI85FILE (ti86p, "TI-86 program file", "86p") |
| 149 | | TI85FILE (ti86s, "TI-86 string file (also ZShell programs)", "86s") |
| 150 | | TI85FILE (ti86i, "TI-86 picture file (85-image)", "86i") |
| 151 | | TI85FILE (ti86n, "TI-86 real number file", "86n") |
| 152 | | TI85FILE (ti86c, "TI-86 complex number file", "86c") |
| 153 | | TI85FILE (ti86l, "TI-86 list (real or complex) file", "86l") |
| 154 | | TI85FILE (ti86k, "TI-86 constant file", "86k") |
| 155 | | TI85FILE (ti86m, "TI-86 matrix (real or complex) file", "86m") |
| 156 | | TI85FILE (ti86v, "TI-86 vector (real or complex) file", "86v") |
| 157 | | TI85FILE (ti86d, "TI-86 graphics database file", "86d") |
| 158 | | TI85FILE (ti86e, "TI-86 equation file", "86e") |
| 159 | | TI85FILE (ti86r, "TI-86 range settings file", "86r") |
| 160 | | TI85FILE (ti86g, "TI-86 grouped file", "86g") |
| 161 | | TI85FILE (ti86, "TI-86 file", "86?") |
| 162 | | |
| 163 | | |
| 164 | | IMAGEMODULE( |
| 165 | | ti85b, |
| 166 | | "TI-85 memory backup file", |
| 167 | | "85b", |
| 168 | | NULL, |
| 169 | | NULL, |
| 170 | | NULL, |
| 171 | | 0, |
| 172 | | ti85b_file_init, |
| 173 | | ti85b_file_exit, |
| 174 | | ti85b_file_info, |
| 175 | | ti85b_file_beginenum, |
| 176 | | ti85b_file_nextenum, |
| 177 | | ti85b_file_closeenum, |
| 178 | | NULL, |
| 179 | | NULL, |
| 180 | | NULL, |
| 181 | | NULL, |
| 182 | | NULL, |
| 183 | | NULL, |
| 184 | | NULL, |
| 185 | | NULL, |
| 186 | | NULL |
| 187 | | ) |
| 188 | | |
| 189 | | static int ti85_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 190 | | { |
| 191 | | ti85_file *file; |
| 192 | | |
| 193 | | file=*(ti85_file**)outimg=(ti85_file *) malloc(sizeof(ti85_file)); |
| 194 | | if (!file) return IMGTOOLERR_OUTOFMEMORY; |
| 195 | | |
| 196 | | file->base.module = mod; |
| 197 | | file->size=stream_size(f); |
| 198 | | file->file_handle=f; |
| 199 | | |
| 200 | | file->data = (unsigned char *) malloc(file->size); |
| 201 | | if (!file->data) return IMGTOOLERR_OUTOFMEMORY; |
| 202 | | |
| 203 | | if (stream_read(f, file->data, file->size)!=file->size) |
| 204 | | { |
| 205 | | free(file); |
| 206 | | *outimg=NULL; |
| 207 | | return IMGTOOLERR_READERROR; |
| 208 | | } |
| 209 | | |
| 210 | | if ( strncmp((char *) file->data, (char *) ti85_file_signature, 11) && |
| 211 | | strncmp((char *) file->data, (char *) ti86_file_signature, 11) ) |
| 212 | | { |
| 213 | | free(file); |
| 214 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 215 | | } |
| 216 | | |
| 217 | | |
| 218 | | if (file->data[0x3b]==0x1d) |
| 219 | | { |
| 220 | | free(file); |
| 221 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 222 | | } |
| 223 | | |
| 224 | | file->number_of_entries = ti85_variables_count(file->data, file->size); |
| 225 | | |
| 226 | | |
| 227 | | file->entries = (ti85_entry*) malloc (file->number_of_entries*sizeof(ti85_entry)); |
| 228 | | if (!file->entries) return IMGTOOLERR_OUTOFMEMORY; |
| 229 | | |
| 230 | | |
| 231 | | ti85_variables_read(file->data, file->size, file->entries); |
| 232 | | |
| 233 | | return 0; |
| 234 | | } |
| 235 | | |
| 236 | | static void ti85_file_exit(imgtool_image * img) |
| 237 | | { |
| 238 | | ti85_file *file=(ti85_file*)img; |
| 239 | | if (file->modified) { |
| 240 | | stream_clear(file->file_handle); |
| 241 | | stream_write(file->file_handle, file->data, file->size); |
| 242 | | } |
| 243 | | stream_close(file->file_handle); |
| 244 | | free(file->entries); |
| 245 | | free(file->data); |
| 246 | | free(file); |
| 247 | | } |
| 248 | | |
| 249 | | static void ti85_file_info(imgtool_image *img, char *string, const int len) |
| 250 | | { |
| 251 | | UINT16 file_checksum; |
| 252 | | UINT16 checksum; |
| 253 | | unsigned int data_size; |
| 254 | | |
| 255 | | ti85_file *file=(ti85_file*)img; |
| 256 | | char comment_with_null[0x2a+1]={ 0 }; |
| 257 | | |
| 258 | | strncpy(comment_with_null, HEADER(file)->comment, 0x2a); |
| 259 | | |
| 260 | | data_size = HEADER(file)->data_size[1]*256 + HEADER(file)->data_size[0]; |
| 261 | | file_checksum = file->data[file->size-1]*256+file->data[file->size-2]; |
| 262 | | checksum = ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE); |
| 263 | | |
| 264 | | sprintf(string,"\nComment : %s\nData size : %04xh\nEntries : %d\nChecksum : %04xh", |
| 265 | | comment_with_null, |
| 266 | | data_size, |
| 267 | | file->number_of_entries, |
| 268 | | file_checksum); |
| 269 | | if (checksum!=file_checksum) strcat (string, " (BAD)"); |
| 270 | | } |
| 271 | | |
| 272 | | static int ti85_file_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 273 | | { |
| 274 | | ti85_file *file=(ti85_file*)img; |
| 275 | | ti85_iterator *iter; |
| 276 | | |
| 277 | | iter=*(ti85_iterator**)outenum = (ti85_iterator *) malloc(sizeof(ti85_iterator)); |
| 278 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 279 | | |
| 280 | | iter->base.module = img->module; |
| 281 | | |
| 282 | | iter->image=file; |
| 283 | | iter->index = 0; |
| 284 | | return 0; |
| 285 | | } |
| 286 | | |
| 287 | | static int ti85_file_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 288 | | { |
| 289 | | ti85_iterator *iter=(ti85_iterator*)enumeration; |
| 290 | | |
| 291 | | ent->eof=iter->index>=iter->image->number_of_entries; |
| 292 | | if (!ent->eof) |
| 293 | | { |
| 294 | | memset(ent->fname,0,iter->image->entries[iter->index].name_size+1); |
| 295 | | strncpy (ent->fname, |
| 296 | | (char*)iter->image->data+iter->image->entries[iter->index].offset+0x06, |
| 297 | | iter->image->entries[iter->index].name_size); |
| 298 | | if (ent->attr) |
| 299 | | sprintf(ent->attr,"%s (%02xh)", |
| 300 | | var_type[iter->image->entries[iter->index].type], |
| 301 | | iter->image->entries[iter->index].type); |
| 302 | | ent->filesize = iter->image->entries[iter->index].data_size; |
| 303 | | ent->corrupt=0; |
| 304 | | iter->index++; |
| 305 | | } |
| 306 | | |
| 307 | | return 0; |
| 308 | | } |
| 309 | | |
| 310 | | static void ti85_file_closeenum(imgtool_directory *enumeration) |
| 311 | | { |
| 312 | | free(enumeration); |
| 313 | | } |
| 314 | | |
| 315 | | static int ti85_findfile (ti85_file * file, const char * fname) |
| 316 | | { |
| 317 | | int i; |
| 318 | | |
| 319 | | for (i = 0; i < file->number_of_entries; i++) |
| 320 | | if (!strnicmp(fname, (char *) file->data+file->entries[i].offset+0x06, file->entries[i].name_size) ) |
| 321 | | return i; |
| 322 | | |
| 323 | | return -1; |
| 324 | | } |
| 325 | | |
| 326 | | |
| 327 | | static int ti85_file_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 328 | | { |
| 329 | | ti85_file *file=(ti85_file*)img; |
| 330 | | int start, size; |
| 331 | | int ind; |
| 332 | | |
| 333 | | if ((ind=ti85_findfile(file, fname))==-1) return IMGTOOLERR_MODULENOTFOUND; |
| 334 | | |
| 335 | | size = file->entries[ind].data_size; |
| 336 | | start = file->entries[ind].offset+file->entries[ind].head_size+0x04; |
| 337 | | |
| 338 | | if (stream_write(destf, file->data+start, size) != size) |
| 339 | | return IMGTOOLERR_WRITEERROR; |
| 340 | | return 0; |
| 341 | | } |
| 342 | | |
| 343 | | static int ti85_file_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_) |
| 344 | | { |
| 345 | | ti85_file *file=(ti85_file*)img; |
| 346 | | int ind; |
| 347 | | |
| 348 | | unsigned char name_size = strlen (fname); |
| 349 | | UINT16 head_size = 0x04 + name_size; |
| 350 | | UINT16 data_size = stream_size(sourcef); |
| 351 | | unsigned char type = options_[TI85_OPTION_FTYPE].i; |
| 352 | | unsigned int offset = file->size-2; |
| 353 | | |
| 354 | | if (!(file->data=realloc(file->data, file->size+data_size+head_size+4)) ) |
| 355 | | return IMGTOOLERR_OUTOFMEMORY; |
| 356 | | |
| 357 | | file->size+=data_size+head_size+4; |
| 358 | | |
| 359 | | if (!(file->entries=realloc(file->entries, sizeof(ti85_entry)*(file->number_of_entries+1))) ) |
| 360 | | return IMGTOOLERR_OUTOFMEMORY; |
| 361 | | file->number_of_entries++; |
| 362 | | |
| 363 | | ind = file->number_of_entries-1; |
| 364 | | |
| 365 | | file->entries[ind].head_size = head_size; |
| 366 | | file->entries[ind].data_size = data_size; |
| 367 | | file->entries[ind].type = type; |
| 368 | | file->entries[ind].name_size = name_size; |
| 369 | | file->entries[ind].offset = offset; |
| 370 | | |
| 371 | | HEADER(file)->data_size[0] = (file->size-0x36)&0x00ff; |
| 372 | | HEADER(file)->data_size[1] = ((file->size-0x36)&0xff00)>>8; |
| 373 | | |
| 374 | | file->data[offset] = head_size&0x00ff; |
| 375 | | file->data[offset+0x01] = (head_size&0xff00)>>8; |
| 376 | | file->data[offset+0x02] = data_size&0x00ff; |
| 377 | | file->data[offset+0x03] = (data_size&0xff00)>>8; |
| 378 | | file->data[offset+0x04] = type; |
| 379 | | file->data[offset+0x05] = name_size; |
| 380 | | |
| 381 | | strncpy ((char*) file->data+offset+0x06, fname, name_size); |
| 382 | | |
| 383 | | file->data[offset+head_size+0x02] = data_size%0x00ff; |
| 384 | | file->data[offset+head_size+0x03] = (data_size%0xff00)>>8; |
| 385 | | |
| 386 | | if (stream_read(sourcef, file->data+offset+head_size+0x04, data_size)!=data_size) |
| 387 | | return IMGTOOLERR_READERROR; |
| 388 | | |
| 389 | | file->data[file->size-2] = ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0x00ff; |
| 390 | | file->data[file->size-1] = (ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0xff00)>>8; |
| 391 | | |
| 392 | | file->modified=1; |
| 393 | | |
| 394 | | return 0; |
| 395 | | } |
| 396 | | |
| 397 | | static int ti85_file_deletefile(imgtool_image *img, const char *fname) |
| 398 | | { |
| 399 | | ti85_file *file=(ti85_file*)img; |
| 400 | | int ind,i; |
| 401 | | int size, start; |
| 402 | | |
| 403 | | if ((ind=ti85_findfile(file, fname))==-1 ) |
| 404 | | return IMGTOOLERR_MODULENOTFOUND; |
| 405 | | |
| 406 | | size = file->entries[ind].data_size+file->entries[ind].head_size+0x04; |
| 407 | | start = file->entries[ind].offset; |
| 408 | | |
| 409 | | memmove(file->data+start, file->data+start+size, file->size-start-size); |
| 410 | | memmove(file->entries+ind*sizeof(ti85_entry), |
| 411 | | file->entries+(ind+1)*sizeof(ti85_entry), |
| 412 | | (file->number_of_entries-ind)*sizeof(ti85_entry)); |
| 413 | | |
| 414 | | for (i = ind; i < file->number_of_entries; i++) |
| 415 | | file->entries[i].offset-=size; |
| 416 | | |
| 417 | | file->size-=size; |
| 418 | | file->data[file->size-2] = ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0x00ff; |
| 419 | | file->data[file->size-1] = (ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0xff00)>>8; |
| 420 | | file->modified=1; |
| 421 | | |
| 422 | | return 0; |
| 423 | | } |
| 424 | | |
| 425 | | static int ti85_file_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 426 | | { |
| 427 | | |
| 428 | | ti85_header header = { {'*','*','T','I','8','5','*','*'}, |
| 429 | | {0x1a, 0x0c, 0x00}, |
| 430 | | "", |
| 431 | | {0x00, 0x00} |
| 432 | | }; |
| 433 | | |
| 434 | | char checksum[] = {0x00, 0x00}; |
| 435 | | |
| 436 | | strncpy(header.comment, options_[TI85_OPTION_COMMENT].s,0x2a); |
| 437 | | if (stream_write(f, &header, sizeof(ti85_header)) != sizeof(ti85_header)) |
| 438 | | return IMGTOOLERR_WRITEERROR; |
| 439 | | if (stream_write(f, &checksum, 2) != 2) |
| 440 | | return IMGTOOLERR_WRITEERROR; |
| 441 | | |
| 442 | | return 0; |
| 443 | | } |
| 444 | | |
| 445 | | static int ti85b_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 446 | | { |
| 447 | | ti85b_file *file; |
| 448 | | |
| 449 | | file=*(ti85b_file**)outimg=(ti85b_file *) malloc(sizeof(ti85b_file)); |
| 450 | | if (!file) return IMGTOOLERR_OUTOFMEMORY; |
| 451 | | |
| 452 | | file->base.module = mod; |
| 453 | | file->size=stream_size(f); |
| 454 | | file->file_handle=f; |
| 455 | | |
| 456 | | file->data = (unsigned char *) malloc(file->size); |
| 457 | | if (!file->data) return IMGTOOLERR_OUTOFMEMORY; |
| 458 | | |
| 459 | | if (stream_read(f, file->data, file->size)!=file->size) |
| 460 | | { |
| 461 | | free(file); |
| 462 | | *outimg=NULL; |
| 463 | | return IMGTOOLERR_READERROR; |
| 464 | | } |
| 465 | | |
| 466 | | if ( strncmp((char *) file->data, (char *) ti85_file_signature, 11)) |
| 467 | | { |
| 468 | | free(file); |
| 469 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 470 | | } |
| 471 | | |
| 472 | | if (file->data[0x3b]!=0x1d) |
| 473 | | { |
| 474 | | free(file); |
| 475 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 476 | | } |
| 477 | | |
| 478 | | return 0; |
| 479 | | } |
| 480 | | |
| 481 | | static void ti85b_file_exit(imgtool_image * img) |
| 482 | | { |
| 483 | | ti85b_file *file=(ti85b_file*)img; |
| 484 | | stream_close(file->file_handle); |
| 485 | | free(file->data); |
| 486 | | free(file); |
| 487 | | } |
| 488 | | |
| 489 | | static void ti85b_file_info(imgtool_image *img, char *string, const int len) |
| 490 | | { |
| 491 | | UINT16 file_checksum; |
| 492 | | UINT16 checksum; |
| 493 | | unsigned int data_size; |
| 494 | | |
| 495 | | ti85b_file *file=(ti85b_file*)img; |
| 496 | | char comment_with_null[0x2a+1]={ 0 }; |
| 497 | | |
| 498 | | strncpy(comment_with_null, HEADER(file)->comment, 0x2a); |
| 499 | | |
| 500 | | data_size = HEADER(file)->data_size[1]*256 + HEADER(file)->data_size[0] - 17; |
| 501 | | file_checksum = file->data[file->size-1]*256+file->data[file->size-2]; |
| 502 | | checksum = ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE); |
| 503 | | |
| 504 | | sprintf(string,"\nComment : %s\nData size : %04xh\nChecksum : %04xh", |
| 505 | | comment_with_null, |
| 506 | | data_size, |
| 507 | | file_checksum); |
| 508 | | if (checksum!=file_checksum) strcat (string, " (BAD)"); |
| 509 | | } |
| 510 | | |
| 511 | | static int ti85b_file_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 512 | | { |
| 513 | | ti85b_file *file=(ti85b_file*)img; |
| 514 | | ti85b_iterator *iter; |
| 515 | | |
| 516 | | iter=*(ti85b_iterator**)outenum = (ti85b_iterator *) malloc(sizeof(ti85b_iterator)); |
| 517 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 518 | | |
| 519 | | iter->base.module = img->module; |
| 520 | | |
| 521 | | iter->image=file; |
| 522 | | iter->index = 0; |
| 523 | | return 0; |
| 524 | | } |
| 525 | | |
| 526 | | static int ti85b_file_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 527 | | { |
| 528 | | ti85b_iterator *iter=(ti85b_iterator*)enumeration; |
| 529 | | |
| 530 | | ent->eof=iter->index>=3; |
| 531 | | if (!ent->eof) |
| 532 | | { |
| 533 | | switch (iter->index) { |
| 534 | | case 0 : |
| 535 | | strcpy (ent->fname, "TI-85 Memory"); |
| 536 | | ent->filesize = iter->image->data[0x3a]*256 +iter->image->data[0x39]; |
| 537 | | break; |
| 538 | | case 1 : |
| 539 | | strcpy (ent->fname, "User Memory"); |
| 540 | | ent->filesize = iter->image->data[0x3d]*256 +iter->image->data[0x3c]; |
| 541 | | break; |
| 542 | | case 2 : |
| 543 | | strcpy (ent->fname, "VAT"); |
| 544 | | ent->filesize = iter->image->data[0x3f]*256 +iter->image->data[0x3e]; |
| 545 | | break; |
| 546 | | } |
| 547 | | |
| 548 | | |
| 549 | | ent->corrupt=0; |
| 550 | | iter->index++; |
| 551 | | } |
| 552 | | |
| 553 | | return 0; |
| 554 | | } |
| 555 | | |
| 556 | | static void ti85b_file_closeenum(imgtool_directory *enumeration) |
| 557 | | { |
| 558 | | free(enumeration); |
| 559 | | } |
src/mess/tools/imgtool/modules/nccard.c
| r13274 | r13275 | |
| 1 | | #include <stdio.h> |
| 2 | | #include <string.h> |
| 3 | | #include <stdlib.h> |
| 4 | | #include <ctype.h> |
| 5 | | #include "imgtoolx.h" |
| 6 | | |
| 7 | | /* NC Card image handling code by Kevin Thacker. February 2001 */ |
| 8 | | |
| 9 | | static int nc_card_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 10 | | static void nc_card_image_exit(imgtool_image *img); |
| 11 | | static size_t nc_card_image_freespace(imgtool_image *img); |
| 12 | | static int nc_card_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 13 | | static int nc_card_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_); |
| 14 | | static int nc_card_image_deletefile(imgtool_image *img, const char *fname); |
| 15 | | static int nc_card_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 16 | | static int nc_card_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 17 | | static int nc_card_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 18 | | static void nc_card_image_closeenum(imgtool_directory *enumeration); |
| 19 | | |
| 20 | | IMAGEMODULE( |
| 21 | | nccard, |
| 22 | | "NC100/200 PCMCIA Ram Card Image", /* human readable name */ |
| 23 | | "crd", /* file extension */ |
| 24 | | NULL, /* crcfile */ |
| 25 | | NULL, /* crc system name */ |
| 26 | | EOLN_CRLF, /* eoln */ |
| 27 | | IMGMODULE_FLAG_FILENAMES_PREFERUCASE, /* flags */ |
| 28 | | nc_card_image_init, /* init function */ |
| 29 | | nc_card_image_exit, /* exit function */ |
| 30 | | NULL, /* info function */ |
| 31 | | nc_card_image_beginenum, /* begin enumeration */ |
| 32 | | nc_card_image_nextenum, /* enumerate next */ |
| 33 | | nc_card_image_closeenum, /* close enumeration */ |
| 34 | | nc_card_image_freespace, /* free space on image */ |
| 35 | | nc_card_image_readfile, /* read file */ |
| 36 | | nc_card_image_writefile, /* write file */ |
| 37 | | nc_card_image_deletefile, /* delete file */ |
| 38 | | nc_card_image_create, /* create image */ |
| 39 | | NULL, |
| 40 | | NULL, |
| 41 | | NULL, /* file options */ |
| 42 | | NULL /* create options */ |
| 43 | | ) |
| 44 | | |
| 45 | | /* |
| 46 | | NC PCMCIA RAM Card uses a FAT-like filesystem |
| 47 | | |
| 48 | | Blocks are fixed at 256 bytes in size. |
| 49 | | |
| 50 | | Block 0 and Block 1 are reserved for a boot program. |
| 51 | | |
| 52 | | Block 2 is reserved for root directory. |
| 53 | | |
| 54 | | Block 3..(3+num_fat_blocks-1) are reserved for fat table. |
| 55 | | |
| 56 | | Block (3+num_fat_blocks).. are used for data storage. |
| 57 | | |
| 58 | | The FAT table uses two bytes for each entry. |
| 59 | | |
| 60 | | The blocks used by a file are stored in the table in a link-list |
| 61 | | structure. |
| 62 | | |
| 63 | | The index of the first block is stored in the directory entry. |
| 64 | | This entry is looked up in the FAT table. If the entry contains |
| 65 | | another number (other than 0x0ffff and 0x0fffe), this is the next |
| 66 | | block used by the file. If the number is 0x0ffff this means this |
| 67 | | block is the last used by the file. |
| 68 | | |
| 69 | | 0x0fffe indicates a reserved block. |
| 70 | | |
| 71 | | The maximum supported PCMCIA Ram card size is 1mb, and there are |
| 72 | | 4096 256-byte blocks on each card. Therefore a FAT entry uses 12-bits |
| 73 | | to store the block index, leaving the top 4-bits as a status?? |
| 74 | | |
| 75 | | Maximum size of a file on the PCMCIA Ram card is 65536 bytes. |
| 76 | | */ |
| 77 | | struct nc_card_dir_entry |
| 78 | | { |
| 79 | | /* filename, up to 12 chars, any unused chars are padded with zero's */ |
| 80 | | /* if first char is 0, this entry has been deleted */ |
| 81 | | unsigned char filename[12]; |
| 82 | | /* unused?? */ |
| 83 | | unsigned char unused[1]; |
| 84 | | /* 5 = basic, 0 = ascii text, 3=directory entry? */ |
| 85 | | unsigned char type; |
| 86 | | /* size of file in bytes */ |
| 87 | | unsigned char size_low_byte; |
| 88 | | unsigned char size_high_byte; |
| 89 | | /* format unknown */ |
| 90 | | unsigned char packed_date[4]; |
| 91 | | /* 2-byte block index. first block used by file, other blocks |
| 92 | | found in fat table */ |
| 93 | | unsigned char first_block_low; |
| 94 | | unsigned char first_block_high; |
| 95 | | /* unused?? */ |
| 96 | | unsigned char pad[32 - 22]; |
| 97 | | }; |
| 98 | | |
| 99 | | #define NC_FAT_END_OF_LIST 0x0ffff |
| 100 | | #define NC_FAT_RESERVED 0x0fffe |
| 101 | | |
| 102 | | struct nc_memcard |
| 103 | | { |
| 104 | | unsigned char *memcard_data; |
| 105 | | unsigned long memcard_size; |
| 106 | | int fat_num_blocks; |
| 107 | | int max_block_index; |
| 108 | | }; |
| 109 | | |
| 110 | | /* each block is 256 bytes in size */ |
| 111 | | #define NC_BLOCK_SIZE 256 |
| 112 | | /* fat begins at block 3 */ |
| 113 | | #define NC_FAT_BLOCK 3 |
| 114 | | |
| 115 | | #define NC_DIR_ENTRY_TYPE_ASCII_FILE 0 |
| 116 | | #define NC_DIR_ENTRY_TYPE_BASIC_FILE 5 |
| 117 | | #define NC_DIR_ENTRY_TYPE_DIR 3 |
| 118 | | |
| 119 | | |
| 120 | | static int memcard_init(struct nc_memcard *memcard, int size) |
| 121 | | { |
| 122 | | /* set size of memcard */ |
| 123 | | memcard->memcard_size = size; |
| 124 | | /* allocate memory for it */ |
| 125 | | memcard->memcard_data = malloc(size); |
| 126 | | |
| 127 | | if (memcard->memcard_data==0) |
| 128 | | return 0; |
| 129 | | |
| 130 | | /* calculate maximum number of blocks for fat to use */ |
| 131 | | memcard->max_block_index = (memcard->memcard_size + NC_BLOCK_SIZE-1)/NC_BLOCK_SIZE; |
| 132 | | |
| 133 | | memcard->fat_num_blocks = ((memcard->max_block_index<<1) + NC_BLOCK_SIZE-1)/NC_BLOCK_SIZE; |
| 134 | | |
| 135 | | return 1; |
| 136 | | } |
| 137 | | |
| 138 | | static void memcard_free(struct nc_memcard *memcard) |
| 139 | | { |
| 140 | | if (memcard->memcard_data!=NULL) |
| 141 | | { |
| 142 | | free(memcard->memcard_data); |
| 143 | | memcard->memcard_data = NULL; |
| 144 | | } |
| 145 | | } |
| 146 | | |
| 147 | | |
| 148 | | /* write to fat */ |
| 149 | | static void memcard_fat_write(struct nc_memcard *memcard,int index1, int data) |
| 150 | | { |
| 151 | | unsigned long offset; |
| 152 | | |
| 153 | | offset = (NC_FAT_BLOCK*NC_BLOCK_SIZE) + (index1<<1); |
| 154 | | |
| 155 | | memcard->memcard_data[offset] = data & 0x0ff; |
| 156 | | memcard->memcard_data[offset+1] = (data>>8); |
| 157 | | } |
| 158 | | |
| 159 | | /* read from fat */ |
| 160 | | static int memcard_fat_read(struct nc_memcard *memcard,int index1) |
| 161 | | { |
| 162 | | int data; |
| 163 | | unsigned long offset; |
| 164 | | |
| 165 | | offset = (NC_FAT_BLOCK*NC_BLOCK_SIZE) + (index1<<1); |
| 166 | | |
| 167 | | data = (memcard->memcard_data[offset] & 0x0ff) | ((memcard->memcard_data[offset+1] & 0x0ff)<<8); |
| 168 | | |
| 169 | | return data; |
| 170 | | } |
| 171 | | |
| 172 | | /* get block index stored at the specified block index */ |
| 173 | | static int memcard_fat_get_next_block(struct nc_memcard *memcard,int index1) |
| 174 | | { |
| 175 | | return memcard_fat_read(memcard,index1); |
| 176 | | } |
| 177 | | |
| 178 | | /* get index of free block */ |
| 179 | | static int memcard_fat_get_free_block(struct nc_memcard *memcard) |
| 180 | | { |
| 181 | | int i; |
| 182 | | |
| 183 | | for (i=0; i<memcard->max_block_index; i++) |
| 184 | | { |
| 185 | | if (memcard_fat_read(memcard,i)==0) |
| 186 | | { |
| 187 | | return i; |
| 188 | | } |
| 189 | | } |
| 190 | | return -1; |
| 191 | | } |
| 192 | | |
| 193 | | |
| 194 | | static unsigned long memcard_block_to_offset(int block) |
| 195 | | { |
| 196 | | return block*NC_BLOCK_SIZE; |
| 197 | | } |
| 198 | | |
| 199 | | |
| 200 | | /* returns offset to directory entry */ |
| 201 | | static unsigned long memcard_get_free_dir_entry(struct nc_memcard *memcard) |
| 202 | | { |
| 203 | | /* offset within memcard data of this directory entry inside the parent */ |
| 204 | | int offset_in_parent_dir = NC_BLOCK_SIZE*2; |
| 205 | | int previous_block; |
| 206 | | int current_block = 2; |
| 207 | | unsigned long offset; |
| 208 | | |
| 209 | | while (current_block!=NC_FAT_END_OF_LIST) |
| 210 | | { |
| 211 | | int i; |
| 212 | | offset = memcard_block_to_offset(current_block); |
| 213 | | |
| 214 | | for (i=0; i<(NC_BLOCK_SIZE/32); i++) |
| 215 | | { |
| 216 | | if (memcard->memcard_data[offset]==0) |
| 217 | | { |
| 218 | | return offset; |
| 219 | | } |
| 220 | | |
| 221 | | offset+=32; |
| 222 | | } |
| 223 | | |
| 224 | | previous_block = current_block; |
| 225 | | current_block = memcard_fat_get_next_block(memcard,current_block); |
| 226 | | |
| 227 | | if (current_block!=NC_FAT_END_OF_LIST) |
| 228 | | { |
| 229 | | current_block &= 0x0fff; |
| 230 | | } |
| 231 | | } |
| 232 | | |
| 233 | | /* out of entries within current blocks occupied by directory. |
| 234 | | try to expand directory by one more block */ |
| 235 | | |
| 236 | | current_block = memcard_fat_get_free_block(memcard); |
| 237 | | |
| 238 | | if (current_block!=-1) |
| 239 | | { |
| 240 | | int size; |
| 241 | | unsigned long block_offset; |
| 242 | | struct nc_card_dir_entry *dir_entry; |
| 243 | | |
| 244 | | /* update size of directory */ |
| 245 | | dir_entry = (struct nc_card_dir_entry *)&memcard->memcard_data[offset_in_parent_dir]; |
| 246 | | |
| 247 | | /* get size of file from directory entry */ |
| 248 | | size = ((dir_entry->size_high_byte & 0x0ff)<<8) | (dir_entry->size_low_byte & 0x0ff); |
| 249 | | |
| 250 | | size+=NC_BLOCK_SIZE; |
| 251 | | |
| 252 | | /* set size of file in directory entry */ |
| 253 | | dir_entry->size_high_byte = (size>>8) & 0x0ff; |
| 254 | | dir_entry->size_low_byte = size & 0x0ff; |
| 255 | | |
| 256 | | /* got a new block */ |
| 257 | | block_offset = memcard_block_to_offset(current_block); |
| 258 | | |
| 259 | | /* clear block */ |
| 260 | | /* only really necessary to clear first byte of each dir entry - but I'll clear |
| 261 | | it all to give decent compressable cards */ |
| 262 | | memset(&memcard->memcard_data[block_offset], 0, NC_BLOCK_SIZE); |
| 263 | | |
| 264 | | /* setup fat */ |
| 265 | | memcard_fat_write(memcard,previous_block, current_block|0x02000); |
| 266 | | memcard_fat_write(memcard,current_block, NC_FAT_END_OF_LIST); |
| 267 | | |
| 268 | | /* return first offset in new block */ |
| 269 | | return block_offset; |
| 270 | | } |
| 271 | | |
| 272 | | return -1; |
| 273 | | } |
| 274 | | |
| 275 | | const char *nc_memcard_executable_id = "NC100PRG"; |
| 276 | | |
| 277 | | /* if memory card has "NC100PRG" at 0x0200 then the card is executable! */ |
| 278 | | static int memcard_is_executable(struct nc_memcard *memcard) |
| 279 | | { |
| 280 | | if (memcmp(&memcard->memcard_data[0x0200], nc_memcard_executable_id, 8)==0) |
| 281 | | { |
| 282 | | return 1; |
| 283 | | } |
| 284 | | |
| 285 | | return 0; |
| 286 | | } |
| 287 | | |
| 288 | | |
| 289 | | /* format a blank card */ |
| 290 | | static void memcard_format(struct nc_memcard *memcard) |
| 291 | | { |
| 292 | | int i; |
| 293 | | |
| 294 | | /* initialise all to zero */ |
| 295 | | /* real NC100 does not do this */ |
| 296 | | memset(memcard->memcard_data, 0, memcard->memcard_size); |
| 297 | | |
| 298 | | /* not executable */ |
| 299 | | /* real NC100 does not do this */ |
| 300 | | memcard->memcard_data[0] = 0x0c9; |
| 301 | | |
| 302 | | /* mark boot block as used */ |
| 303 | | memcard_fat_write(memcard,0, NC_FAT_END_OF_LIST); |
| 304 | | memcard_fat_write(memcard,1, NC_FAT_END_OF_LIST); |
| 305 | | /* mark directory block as used */ |
| 306 | | memcard_fat_write(memcard,2, NC_FAT_END_OF_LIST); |
| 307 | | |
| 308 | | /* mark fat blocks as used */ |
| 309 | | for (i=0; i<memcard->fat_num_blocks; i++) |
| 310 | | { |
| 311 | | memcard_fat_write(memcard,3+i, NC_FAT_END_OF_LIST); |
| 312 | | } |
| 313 | | |
| 314 | | /* reserved at end of fat table - required?? */ |
| 315 | | memcard_fat_write(memcard,memcard->max_block_index-1, NC_FAT_RESERVED); |
| 316 | | |
| 317 | | { |
| 318 | | /* format for NC100 */ |
| 319 | | unsigned long dir_entry_offset = memcard_get_free_dir_entry(memcard); |
| 320 | | int dir_size = NC_BLOCK_SIZE; |
| 321 | | struct nc_card_dir_entry *dir_entry; |
| 322 | | |
| 323 | | dir_entry = (struct nc_card_dir_entry *)&memcard->memcard_data[dir_entry_offset]; |
| 324 | | |
| 325 | | strcpy((char *)dir_entry->filename,(char *)"NC100"); |
| 326 | | dir_entry->size_high_byte = (dir_size>>8) & 0x0ff; |
| 327 | | dir_entry->size_low_byte = (dir_size & 0x0ff); |
| 328 | | dir_entry->first_block_high = 0x020; |
| 329 | | dir_entry->first_block_low = 0x02; |
| 330 | | dir_entry->type = NC_DIR_ENTRY_TYPE_DIR; |
| 331 | | } |
| 332 | | } |
| 333 | | |
| 334 | | |
| 335 | | static unsigned long memcard_locate_dir_entry(struct nc_memcard *memcard,char *pNCFilename) |
| 336 | | { |
| 337 | | int current_block = 2; |
| 338 | | unsigned long offset; |
| 339 | | |
| 340 | | while (current_block!=NC_FAT_END_OF_LIST) |
| 341 | | { |
| 342 | | int i; |
| 343 | | |
| 344 | | offset = memcard_block_to_offset(current_block); |
| 345 | | |
| 346 | | for (i=0; i<(NC_BLOCK_SIZE/32); i++) |
| 347 | | { |
| 348 | | if (memcard->memcard_data[offset]!=0) |
| 349 | | { |
| 350 | | if (memcmp(&memcard->memcard_data[offset], pNCFilename,12)==0) |
| 351 | | { |
| 352 | | return offset; |
| 353 | | } |
| 354 | | } |
| 355 | | |
| 356 | | offset+=32; |
| 357 | | } |
| 358 | | |
| 359 | | current_block = memcard_fat_get_next_block(memcard,current_block); |
| 360 | | |
| 361 | | if (current_block!=NC_FAT_END_OF_LIST) |
| 362 | | { |
| 363 | | current_block = current_block & 0x0fff; |
| 364 | | } |
| 365 | | } |
| 366 | | |
| 367 | | return -1; |
| 368 | | } |
| 369 | | |
| 370 | | static int memcard_del(struct nc_memcard *memcard,char *pNCFilename) |
| 371 | | { |
| 372 | | unsigned long dir_entry_offset = memcard_locate_dir_entry(memcard,pNCFilename); |
| 373 | | int success_code = 0; |
| 374 | | |
| 375 | | if (dir_entry_offset!=-1) |
| 376 | | { |
| 377 | | int current_block; |
| 378 | | struct nc_card_dir_entry *dir_entry; |
| 379 | | |
| 380 | | dir_entry = (struct nc_card_dir_entry *)&memcard->memcard_data[dir_entry_offset]; |
| 381 | | |
| 382 | | /* get first block index from directory entry */ |
| 383 | | current_block = ((dir_entry->first_block_high & 0x0f)<<8) | dir_entry->first_block_low; |
| 384 | | |
| 385 | | while (current_block!=0) |
| 386 | | { |
| 387 | | int next_block; |
| 388 | | |
| 389 | | /* get next block in list */ |
| 390 | | next_block = memcard_fat_get_next_block(memcard,current_block); |
| 391 | | /* zero out this block */ |
| 392 | | memcard_fat_write(memcard,current_block,0); |
| 393 | | |
| 394 | | /* go to next block */ |
| 395 | | current_block = next_block; |
| 396 | | |
| 397 | | if (current_block!=NC_FAT_END_OF_LIST) |
| 398 | | { |
| 399 | | current_block &= 0x0fff; |
| 400 | | } |
| 401 | | } |
| 402 | | |
| 403 | | /* clear first char of filename */ |
| 404 | | dir_entry->filename[0] = '\0'; |
| 405 | | success_code = 1; |
| 406 | | } |
| 407 | | |
| 408 | | return success_code; |
| 409 | | } |
| 410 | | |
| 411 | | |
| 412 | | struct nc_card_image |
| 413 | | { |
| 414 | | imgtool_image base; |
| 415 | | imgtool_stream *file_handle; |
| 416 | | struct nc_memcard memcard; |
| 417 | | }; |
| 418 | | |
| 419 | | static int nc_card_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 420 | | { |
| 421 | | struct nc_card_image *nc_card; |
| 422 | | |
| 423 | | *outimg = NULL; |
| 424 | | |
| 425 | | nc_card = (struct nc_card_image *)malloc(sizeof(struct nc_card_image)); |
| 426 | | |
| 427 | | if (!nc_card) |
| 428 | | { |
| 429 | | return IMGTOOLERR_OUTOFMEMORY; |
| 430 | | } |
| 431 | | |
| 432 | | nc_card->file_handle = f; |
| 433 | | nc_card->base.module = mod; |
| 434 | | |
| 435 | | /* allocate a big chunk of memory for memcard data - max size will be 1mb! */ |
| 436 | | if (!memcard_init(&nc_card->memcard, stream_size(f))) |
| 437 | | { |
| 438 | | free(nc_card); |
| 439 | | return IMGTOOLERR_OUTOFMEMORY; |
| 440 | | } |
| 441 | | |
| 442 | | /* read data into memcard */ |
| 443 | | if (stream_read(f, nc_card->memcard.memcard_data, nc_card->memcard.memcard_size)!=nc_card->memcard.memcard_size) |
| 444 | | { |
| 445 | | memcard_free(&nc_card->memcard); |
| 446 | | |
| 447 | | return IMGTOOLERR_READERROR; |
| 448 | | } |
| 449 | | |
| 450 | | *outimg = (imgtool_image *)nc_card; |
| 451 | | return 0; |
| 452 | | } |
| 453 | | |
| 454 | | static void nc_card_image_exit(imgtool_image *img) |
| 455 | | { |
| 456 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 457 | | |
| 458 | | stream_seek(nc_card->file_handle, 0, SEEK_SET); |
| 459 | | stream_write(nc_card->file_handle, nc_card->memcard.memcard_data, nc_card->memcard.memcard_size); |
| 460 | | stream_close(nc_card->file_handle); |
| 461 | | |
| 462 | | memcard_free(&nc_card->memcard); |
| 463 | | } |
| 464 | | |
| 465 | | |
| 466 | | /* count number of free blocks and calculate freespace */ |
| 467 | | static size_t nc_card_image_freespace(imgtool_image *img) |
| 468 | | { |
| 469 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 470 | | int i; |
| 471 | | int count; |
| 472 | | |
| 473 | | count = 0; |
| 474 | | for (i=0; i<nc_card->memcard.max_block_index; i++) |
| 475 | | { |
| 476 | | if (memcard_fat_read(&nc_card->memcard,i)==0) |
| 477 | | { |
| 478 | | count++; |
| 479 | | } |
| 480 | | } |
| 481 | | return count*NC_BLOCK_SIZE; |
| 482 | | } |
| 483 | | |
| 484 | | /* create a empty image */ |
| 485 | | /* in this case a formatted image */ |
| 486 | | static int nc_card_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 487 | | { |
| 488 | | int code; |
| 489 | | |
| 490 | | struct nc_memcard memcard; |
| 491 | | |
| 492 | | /* allocate a big chunk of memory for memcard data - max size will be 1mb! */ |
| 493 | | if (!memcard_init(&memcard, 1024*1024)) |
| 494 | | { |
| 495 | | return IMGTOOLERR_OUTOFMEMORY; |
| 496 | | } |
| 497 | | |
| 498 | | /* format the card */ |
| 499 | | memcard_format(&memcard); |
| 500 | | |
| 501 | | /* write out */ |
| 502 | | code = 0; |
| 503 | | if (stream_write(f, memcard.memcard_data, memcard.memcard_size) != memcard.memcard_size) |
| 504 | | { |
| 505 | | /* write error! */ |
| 506 | | code = IMGTOOLERR_WRITEERROR; |
| 507 | | } |
| 508 | | |
| 509 | | /* free memcard data */ |
| 510 | | memcard_free(&memcard); |
| 511 | | |
| 512 | | /* return success or failure code */ |
| 513 | | return code; |
| 514 | | } |
| 515 | | |
| 516 | | static void setup_nc_filename(char *nc_filename, const char *fname) |
| 517 | | { |
| 518 | | char ch; |
| 519 | | int offset; |
| 520 | | |
| 521 | | memset(nc_filename, 0, 12); |
| 522 | | |
| 523 | | offset = 0; |
| 524 | | do |
| 525 | | { |
| 526 | | /* get char */ |
| 527 | | ch = fname[offset]; |
| 528 | | |
| 529 | | if (ch!=0) |
| 530 | | { |
| 531 | | /* convert to upper case */ |
| 532 | | ch = toupper(ch); |
| 533 | | nc_filename[offset] = ch; |
| 534 | | offset++; |
| 535 | | } |
| 536 | | } |
| 537 | | while ((ch!=0) && (offset<12)); |
| 538 | | } |
| 539 | | |
| 540 | | static int nc_card_image_deletefile(imgtool_image *img, const char *fname) |
| 541 | | { |
| 542 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 543 | | char nc_filename[12]; |
| 544 | | |
| 545 | | setup_nc_filename(nc_filename,fname); |
| 546 | | |
| 547 | | if (memcard_del(&nc_card->memcard,nc_filename)) |
| 548 | | { |
| 549 | | return IMGTOOLERR_MODULENOTFOUND; |
| 550 | | } |
| 551 | | |
| 552 | | return 0; |
| 553 | | } |
| 554 | | |
| 555 | | |
| 556 | | |
| 557 | | /* get a file from the memcard */ |
| 558 | | static int nc_card_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 559 | | { |
| 560 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 561 | | char nc_filename[12]; |
| 562 | | unsigned long dir_entry_offset; |
| 563 | | |
| 564 | | setup_nc_filename(nc_filename, fname); |
| 565 | | |
| 566 | | dir_entry_offset = memcard_locate_dir_entry(&nc_card->memcard,(char *)nc_filename); |
| 567 | | |
| 568 | | if (dir_entry_offset!=-1) |
| 569 | | { |
| 570 | | struct nc_card_dir_entry *dir_entry; |
| 571 | | int current_block; |
| 572 | | int length_remaining; |
| 573 | | |
| 574 | | dir_entry = (struct nc_card_dir_entry *)&nc_card->memcard.memcard_data[dir_entry_offset]; |
| 575 | | |
| 576 | | /* get size of file from directory entry */ |
| 577 | | length_remaining = ((dir_entry->size_high_byte & 0x0ff)<<8) | (dir_entry->size_low_byte & 0x0ff); |
| 578 | | |
| 579 | | /* get first block index from directory entry */ |
| 580 | | current_block = ((dir_entry->first_block_high & 0x0f)<<8) | dir_entry->first_block_low; |
| 581 | | |
| 582 | | /* keep going until we have written all data or we have got to end of block list */ |
| 583 | | while ((length_remaining!=0) && (current_block!=NC_FAT_END_OF_LIST)) |
| 584 | | { |
| 585 | | unsigned long block_offset; |
| 586 | | int copy_length; |
| 587 | | |
| 588 | | /* get offset of block data in memcard data */ |
| 589 | | block_offset = memcard_block_to_offset(current_block); |
| 590 | | |
| 591 | | /* calc amount of data to copy from block */ |
| 592 | | if (length_remaining<NC_BLOCK_SIZE) |
| 593 | | { |
| 594 | | copy_length = length_remaining; |
| 595 | | } |
| 596 | | else |
| 597 | | { |
| 598 | | copy_length = NC_BLOCK_SIZE; |
| 599 | | } |
| 600 | | |
| 601 | | /* output to file */ |
| 602 | | if (stream_write(destf, &nc_card->memcard.memcard_data[block_offset], copy_length)!=copy_length) |
| 603 | | return IMGTOOLERR_WRITEERROR; |
| 604 | | |
| 605 | | /* update length of data remaining to write */ |
| 606 | | length_remaining -=copy_length; |
| 607 | | |
| 608 | | /* get next block in list */ |
| 609 | | current_block = memcard_fat_get_next_block(&nc_card->memcard,current_block); |
| 610 | | |
| 611 | | if (current_block!=NC_FAT_END_OF_LIST) |
| 612 | | { |
| 613 | | current_block &= 0x0fff; |
| 614 | | } |
| 615 | | } |
| 616 | | } |
| 617 | | |
| 618 | | return 0; |
| 619 | | } |
| 620 | | |
| 621 | | /* put a file to the memcard */ |
| 622 | | static int nc_card_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_) |
| 623 | | { |
| 624 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 625 | | unsigned long length; |
| 626 | | unsigned long offset; |
| 627 | | |
| 628 | | char nc_filename[12]; |
| 629 | | |
| 630 | | length = stream_size(sourcef); |
| 631 | | |
| 632 | | /* locate a free directory entry */ |
| 633 | | offset = memcard_get_free_dir_entry(&nc_card->memcard); |
| 634 | | |
| 635 | | setup_nc_filename(nc_filename, fname); |
| 636 | | |
| 637 | | if (offset!=-1) |
| 638 | | { |
| 639 | | /* got a directory entry */ |
| 640 | | |
| 641 | | int current_block; |
| 642 | | int previous_block; |
| 643 | | unsigned long block_offset; |
| 644 | | //unsigned long file_offset = 0; |
| 645 | | unsigned long length_remaining = length; |
| 646 | | struct nc_card_dir_entry *dir_entry; |
| 647 | | int copy_length; |
| 648 | | |
| 649 | | /* do not transfer more then 65536 bytes */ |
| 650 | | /* this is max size of file on card */ |
| 651 | | /* file size specified with 2 bytes only! */ |
| 652 | | if (length_remaining>65536) |
| 653 | | { |
| 654 | | length_remaining = 65536; |
| 655 | | } |
| 656 | | |
| 657 | | /* locate the first free block */ |
| 658 | | current_block = memcard_fat_get_free_block(&nc_card->memcard); |
| 659 | | |
| 660 | | if (current_block!=-1) |
| 661 | | { |
| 662 | | /* initialise directory entry - blank all entries */ |
| 663 | | memset(&nc_card->memcard.memcard_data[offset], 0, 32); |
| 664 | | |
| 665 | | dir_entry = (struct nc_card_dir_entry *)&nc_card->memcard.memcard_data[offset]; |
| 666 | | /* set size of file in directory entry */ |
| 667 | | dir_entry->size_high_byte = (length>>8) & 0x0ff; |
| 668 | | dir_entry->size_low_byte = length & 0x0ff; |
| 669 | | /* set first block in directory entry */ |
| 670 | | dir_entry->first_block_high = ((current_block>>8) & 0x0f) | 0x020; |
| 671 | | dir_entry->first_block_low = (current_block & 0x0ff); |
| 672 | | /* setup filename */ |
| 673 | | strncpy((char *)dir_entry->filename,(char *)nc_filename, 12); |
| 674 | | |
| 675 | | while (length_remaining!=0) |
| 676 | | { |
| 677 | | /* get offset of block data */ |
| 678 | | block_offset = memcard_block_to_offset(current_block); |
| 679 | | |
| 680 | | /* calc amount of data to copy to block */ |
| 681 | | if (length_remaining<NC_BLOCK_SIZE) |
| 682 | | { |
| 683 | | copy_length = length_remaining; |
| 684 | | } |
| 685 | | else |
| 686 | | { |
| 687 | | copy_length = NC_BLOCK_SIZE; |
| 688 | | } |
| 689 | | |
| 690 | | /* copy data to memcard */ |
| 691 | | if (stream_read(sourcef, &nc_card->memcard.memcard_data[block_offset], copy_length)!=copy_length) |
| 692 | | return IMGTOOLERR_WRITEERROR; |
| 693 | | |
| 694 | | length_remaining -=copy_length; |
| 695 | | //file_offset+=copy_length; |
| 696 | | |
| 697 | | /* mark block as used */ |
| 698 | | memcard_fat_write(&nc_card->memcard,current_block, NC_FAT_END_OF_LIST); |
| 699 | | |
| 700 | | previous_block = current_block; |
| 701 | | current_block = memcard_fat_get_free_block(&nc_card->memcard); |
| 702 | | |
| 703 | | if (current_block==-1) |
| 704 | | break; |
| 705 | | |
| 706 | | /* link up block chain */ |
| 707 | | memcard_fat_write(&nc_card->memcard,previous_block, current_block|0x02000); |
| 708 | | } |
| 709 | | |
| 710 | | /* end block chain */ |
| 711 | | memcard_fat_write(&nc_card->memcard,current_block, NC_FAT_END_OF_LIST); |
| 712 | | } |
| 713 | | } |
| 714 | | |
| 715 | | |
| 716 | | return 0; |
| 717 | | } |
| 718 | | |
| 719 | | /* directory fills 1 or more blocks. |
| 720 | | current_block holds index of current block we are accessing, |
| 721 | | offset_in_block holds offset within block referenced by current_block. |
| 722 | | eod is 1 if end of directory (no more blocks) or 0 if not end of directory */ |
| 723 | | |
| 724 | | struct nc_card_direnum |
| 725 | | { |
| 726 | | imgtool_directory base; |
| 727 | | /* card image */ |
| 728 | | struct nc_card_image *img; |
| 729 | | /* directory block index */ |
| 730 | | int current_block; |
| 731 | | /* index of entry within block */ |
| 732 | | int offset_in_block; |
| 733 | | /* true if reached last entry in last directory block */ |
| 734 | | int eod; |
| 735 | | }; |
| 736 | | |
| 737 | | |
| 738 | | static int nc_card_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 739 | | { |
| 740 | | struct nc_card_image *nc_card = (struct nc_card_image *)img; |
| 741 | | struct nc_card_direnum *card_enum; |
| 742 | | |
| 743 | | if (memcard_is_executable(&nc_card->memcard)) |
| 744 | | { |
| 745 | | return IMGTOOLERR_MODULENOTFOUND; |
| 746 | | } |
| 747 | | |
| 748 | | card_enum = (struct nc_card_direnum *) malloc(sizeof(struct nc_card_direnum)); |
| 749 | | if (!card_enum) |
| 750 | | return IMGTOOLERR_OUTOFMEMORY; |
| 751 | | |
| 752 | | card_enum->base.module = img->module; |
| 753 | | card_enum->img = (struct nc_card_image *) img; |
| 754 | | /* directory always starts on block 2 */ |
| 755 | | card_enum->current_block = 2; |
| 756 | | card_enum->offset_in_block = 0; |
| 757 | | card_enum->eod = 0; |
| 758 | | *outenum = &card_enum->base; |
| 759 | | return 0; |
| 760 | | } |
| 761 | | |
| 762 | | static int nc_card_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 763 | | { |
| 764 | | struct nc_card_direnum *card_enum = (struct nc_card_direnum *) enumeration; |
| 765 | | |
| 766 | | /* got to end of directory? */ |
| 767 | | if (!card_enum->eod) |
| 768 | | { |
| 769 | | int quit = 0; |
| 770 | | /* no */ |
| 771 | | |
| 772 | | /* loop until we get a used directory entry */ |
| 773 | | while (quit==0) |
| 774 | | { |
| 775 | | int dir_block_offset; |
| 776 | | struct nc_card_dir_entry *dir_entry; |
| 777 | | |
| 778 | | /* gone over end of block? */ |
| 779 | | if (card_enum->offset_in_block==NC_BLOCK_SIZE) |
| 780 | | { |
| 781 | | /* yes */ |
| 782 | | |
| 783 | | /* reset offset */ |
| 784 | | card_enum->offset_in_block = 0; |
| 785 | | /* get next block */ |
| 786 | | card_enum->current_block = memcard_fat_get_next_block(&card_enum->img->memcard,card_enum->current_block); |
| 787 | | |
| 788 | | /* block index = end of list? */ |
| 789 | | if (card_enum->current_block==NC_FAT_END_OF_LIST) |
| 790 | | { |
| 791 | | /* yes */ |
| 792 | | |
| 793 | | /* no more directory blocks, end of directory reached */ |
| 794 | | card_enum->eod = 1; |
| 795 | | |
| 796 | | /* no more blocks - so quit */ |
| 797 | | quit = 1; |
| 798 | | } |
| 799 | | else |
| 800 | | { |
| 801 | | /* strip off upper 4 bits */ |
| 802 | | card_enum->current_block &=0x0fff; |
| 803 | | } |
| 804 | | } |
| 805 | | |
| 806 | | if (quit==0) |
| 807 | | { |
| 808 | | dir_block_offset = memcard_block_to_offset(card_enum->current_block); |
| 809 | | |
| 810 | | dir_entry = (struct nc_card_dir_entry *)&card_enum->img->memcard.memcard_data[dir_block_offset + card_enum->offset_in_block]; |
| 811 | | |
| 812 | | if (dir_entry->filename[0]!=0) |
| 813 | | { |
| 814 | | /* got a used directory entry */ |
| 815 | | |
| 816 | | if (ent->fname_len<13) |
| 817 | | return IMGTOOLERR_BUFFERTOOSMALL; |
| 818 | | |
| 819 | | memcpy(ent->fname, dir_entry->filename,12); |
| 820 | | ent->fname[12] = '\0'; |
| 821 | | |
| 822 | | /* set file-size -> from directory */ |
| 823 | | ent->filesize = ((dir_entry->size_high_byte & 0x0ff)<<8) | (dir_entry->size_low_byte & 0x0ff); |
| 824 | | |
| 825 | | /* not last entry */ |
| 826 | | ent->eof = 0; |
| 827 | | /* not corrupt */ |
| 828 | | ent->corrupt = 0; |
| 829 | | |
| 830 | | if (ent->attr_len) |
| 831 | | { |
| 832 | | switch (dir_entry->type) |
| 833 | | { |
| 834 | | case 0: |
| 835 | | { |
| 836 | | sprintf(ent->attr,"ASCII FILE"); |
| 837 | | } |
| 838 | | break; |
| 839 | | |
| 840 | | case 5: |
| 841 | | { |
| 842 | | sprintf(ent->attr,"BASIC FILE"); |
| 843 | | } |
| 844 | | break; |
| 845 | | |
| 846 | | case 3: |
| 847 | | { |
| 848 | | sprintf(ent->attr,"DIRECTORY"); |
| 849 | | } |
| 850 | | break; |
| 851 | | } |
| 852 | | } |
| 853 | | |
| 854 | | quit = 1; |
| 855 | | } |
| 856 | | } |
| 857 | | |
| 858 | | /* update offset for next enum */ |
| 859 | | card_enum->offset_in_block+=32; |
| 860 | | } |
| 861 | | } |
| 862 | | |
| 863 | | /* got to end of directory? */ |
| 864 | | if (card_enum->eod) |
| 865 | | { |
| 866 | | /* yes */ |
| 867 | | ent->filesize = 0; |
| 868 | | ent->corrupt = 0; |
| 869 | | ent->eof = 1; |
| 870 | | if (ent->fname_len > 0) |
| 871 | | ent->fname[0] = '\0'; |
| 872 | | } |
| 873 | | |
| 874 | | return 0; |
| 875 | | } |
| 876 | | |
| 877 | | static void nc_card_image_closeenum(imgtool_directory *enumeration) |
| 878 | | { |
| 879 | | free(enumeration); |
| 880 | | } |
src/mess/tools/imgtool/modules/d64.c
| r13274 | r13275 | |
| 1 | | #include <stdio.h> |
| 2 | | #include <string.h> |
| 3 | | #include <stdlib.h> |
| 4 | | #include "imgtoolx.h" |
| 5 | | #include "d64.h" |
| 6 | | |
| 7 | | #ifdef LSB_FIRST |
| 8 | | typedef UINT16 littleuword; |
| 9 | | #define GET_UWORD(a) a |
| 10 | | #define SET_UWORD(a,v) a=v |
| 11 | | #else |
| 12 | | typedef struct { |
| 13 | | unsigned char low, high; |
| 14 | | } littleuword; |
| 15 | | #define GET_UWORD(a) (a.low|(a.high<<8)) |
| 16 | | #define SET_UWORD(a,v) a.low=(v)&0xff;a.high=((v)>>8)&0xff |
| 17 | | #endif |
| 18 | | |
| 19 | | /* tracks 1 to 35 |
| 20 | | * sectors number from 0 |
| 21 | | * each sector holds 256 data bytes |
| 22 | | * directory and Bitmap Allocation Memory in track 18 |
| 23 | | * sector 0: |
| 24 | | * 0: track# of directory begin (this linkage of sector often used) |
| 25 | | * 1: sector# of directory begin |
| 26 | | * |
| 27 | | * BAM entries (one per track) |
| 28 | | * offset 0: # of free sectors |
| 29 | | * offset 1: sector 0 (lsb) free to sector 7 |
| 30 | | * offset 2: sector 8 to 15 |
| 31 | | * offset 3: sector 16 to whatever the number to sectors in track is |
| 32 | | * |
| 33 | | * directory sector: |
| 34 | | * 0,1: track sector of next directory sector |
| 35 | | * 2, 34, 66, ... : 8 directory entries |
| 36 | | * |
| 37 | | * directory entry: |
| 38 | | * 0: file type |
| 39 | | * (0x = scratched/splat, 8x = alive, Cx = locked |
| 40 | | * where x: 0=DEL, 1=SEQ, 2=PRG, 3=USR, 4=REL) |
| 41 | | * 1,2: track and sector of file |
| 42 | | * 3..18: file name padded with a0 |
| 43 | | * 19,20: REL files side sector |
| 44 | | * 21: REL files record length |
| 45 | | * 28,29: number of blocks in file |
| 46 | | * ended with illegal track and sector numbers |
| 47 | | */ |
| 48 | | |
| 49 | | // x64 d64 image with 64 byte header |
| 50 | | |
| 51 | | #define D64_MAX_TRACKS 40 |
| 52 | | |
| 53 | | static const int d64_sectors_per_track[] = |
| 54 | | { |
| 55 | | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 56 | | 19, 19, 19, 19, 19, 19, 19, |
| 57 | | 18, 18, 18, 18, 18, 18, |
| 58 | | 17, 17, 17, 17, 17, |
| 59 | | 17, 17, 17, 17, 17 // non standard |
| 60 | | }; |
| 61 | | |
| 62 | | static int d64_offset[D64_MAX_TRACKS+1]; /* offset of begin of track in d64 file */ |
| 63 | | |
| 64 | | typedef struct { |
| 65 | | unsigned char track, sector, // of next directory |
| 66 | | format, doublesided; |
| 67 | | struct { unsigned char free, map[3]; } bam[35]; |
| 68 | | unsigned char name[0x10], filler[2], id[2], filler2, version[2], filler3[4]; |
| 69 | | unsigned char unused[50]; // zero |
| 70 | | unsigned char free2[35]; //freecount for 1751 tracks 36..70 |
| 71 | | } D64_HEADER; |
| 72 | | |
| 73 | | typedef struct { |
| 74 | | unsigned char unused[0x6a]; // zero |
| 75 | | struct { unsigned char map2[3]; } bam2[35]; |
| 76 | | unsigned char unused2[0x2d]; // zer0 |
| 77 | | } D71_HEADER; |
| 78 | | |
| 79 | | static const D64_HEADER d64_header={ |
| 80 | | 18,1,'A', |
| 81 | | 0, // 1571 0x80 |
| 82 | | { { 0 } }, |
| 83 | | { '\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0', |
| 84 | | '\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0' |
| 85 | | }, |
| 86 | | { 0xa0, 0xa0 }, { 0xa0, 0xa0 } , 0xa0, { '2', 'A' } , { '\xa0','\xa0','\xa0','\xa0' }, |
| 87 | | }; |
| 88 | | |
| 89 | | typedef struct { |
| 90 | | unsigned char not_usable[2]; |
| 91 | | unsigned char type; |
| 92 | | #define FILE_TYPE_DEL 0 |
| 93 | | #define FILE_TYPE_SEQ 1 |
| 94 | | #define FILE_TYPE_PRG 2 |
| 95 | | #define FILE_TYPE_USR 3 |
| 96 | | #define FILE_TYPE_REL 4 |
| 97 | | #define FILE_TYPE_MASK 0x3f |
| 98 | | unsigned char track, sector, name[0x10]; |
| 99 | | unsigned char rel_track, rel_sector, rel_length; |
| 100 | | unsigned char unused[4]; |
| 101 | | unsigned char replace_track, replace_sector; |
| 102 | | littleuword blocks; |
| 103 | | } D64_ENTRY; |
| 104 | | |
| 105 | | typedef union { |
| 106 | | struct { |
| 107 | | unsigned char track, sector; //linkage to next track=0 for last |
| 108 | | } linkage; |
| 109 | | D64_ENTRY entry[8]; |
| 110 | | } D64_DIRECTORY; |
| 111 | | |
| 112 | | typedef struct { |
| 113 | | unsigned char track, sector, // of next directory |
| 114 | | format, unused; |
| 115 | | unsigned char name[0x12], id[2], filler2, version[2], filler3[2]; |
| 116 | | unsigned char unused1[227]; // zero |
| 117 | | } D81_HEADER; |
| 118 | | |
| 119 | | static const D81_HEADER d81_header={ |
| 120 | | 40,3,'D', |
| 121 | | 0, |
| 122 | | { '\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0', |
| 123 | | '\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0','\xa0', 0xa0, 0xa0 |
| 124 | | }, |
| 125 | | { 0xa0, 0xa0 } , 0xa0, { '3', 'D' } , { '\xa0','\xa0' }, |
| 126 | | }; |
| 127 | | |
| 128 | | typedef struct { |
| 129 | | unsigned char track, sector; |
| 130 | | unsigned char version, complement; |
| 131 | | unsigned char id[2], iobyte, autoloader; |
| 132 | | unsigned char reserved[8]; |
| 133 | | struct { unsigned char free, map[5]; } bam[40]; |
| 134 | | } D81_BAM; |
| 135 | | |
| 136 | | static const D81_BAM d81_bam={ |
| 137 | | 40, 2,0x44, 0xbb, |
| 138 | | }; |
| 139 | | |
| 140 | | #define D71_MAX_TRACKS 70 |
| 141 | | |
| 142 | | static const int d71_sectors_per_track[] = |
| 143 | | { |
| 144 | | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 145 | | 19, 19, 19, 19, 19, 19, 19, |
| 146 | | 18, 18, 18, 18, 18, 18, |
| 147 | | 17, 17, 17, 17, 17, |
| 148 | | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 149 | | 19, 19, 19, 19, 19, 19, 19, |
| 150 | | 18, 18, 18, 18, 18, 18, |
| 151 | | 17, 17, 17, 17, 17 |
| 152 | | }; |
| 153 | | |
| 154 | | static int d71_offset[D71_MAX_TRACKS+1]; /* offset of begin of track in d64 file */ |
| 155 | | |
| 156 | | /* must be called before other functions */ |
| 157 | | void d64_open_helper (void) |
| 158 | | { |
| 159 | | int i; |
| 160 | | |
| 161 | | d64_offset[0] = 0; |
| 162 | | for (i = 1; i <= D64_MAX_TRACKS; i++) |
| 163 | | d64_offset[i] = d64_offset[i - 1] + d64_sectors_per_track[i - 1] * 256; |
| 164 | | |
| 165 | | d71_offset[0] = 0; |
| 166 | | for (i = 1; i <= D71_MAX_TRACKS; i++) |
| 167 | | d71_offset[i] = d71_offset[i - 1] + d71_sectors_per_track[i - 1] * 256; |
| 168 | | } |
| 169 | | |
| 170 | | /* calculates offset to beginning of d64 file for sector beginning */ |
| 171 | | static int d64_tracksector2offset (int track, int sector) |
| 172 | | { |
| 173 | | return d64_offset[track - 1] + sector * 256; |
| 174 | | } |
| 175 | | |
| 176 | | /* calculates offset to beginning of d71 file for sector beginning */ |
| 177 | | int d71_tracksector2offset (int track, int sector) |
| 178 | | { |
| 179 | | return d71_offset[track - 1] + sector * 256; |
| 180 | | } |
| 181 | | |
| 182 | | /* calculates offset to beginning of d71 file for sector beginning */ |
| 183 | | int d81_tracksector2offset (int track, int sector) |
| 184 | | { |
| 185 | | return ((track - 1)*20*2 + sector )* 256; |
| 186 | | } |
| 187 | | |
| 188 | | static void cbm_filename_to_cstring(unsigned char *cbmstring, int size, unsigned char* cstring) |
| 189 | | { |
| 190 | | int i; |
| 191 | | for (i=0; (i<size)&&(cbmstring[i]!=0xa0); i++) cstring[i]=cbmstring[i]; |
| 192 | | cstring[i]=0; |
| 193 | | } |
| 194 | | |
| 195 | | int cbm_compareNames (const unsigned char *left, const unsigned char *right) |
| 196 | | { |
| 197 | | int i; |
| 198 | | |
| 199 | | for (i = 0; i < 16; i++) |
| 200 | | { |
| 201 | | if ((left[i] == '*') || (right[i] == '*')) |
| 202 | | return 1; |
| 203 | | |
| 204 | | if (left[i] == right[i]) { |
| 205 | | if (left[i]==0) return 1; |
| 206 | | continue; |
| 207 | | } |
| 208 | | if ((left[i] == 0xa0) && (right[i] == 0)) |
| 209 | | return 1; |
| 210 | | if ((right[i] == 0xa0) && (left[i] == 0)) |
| 211 | | return 1; |
| 212 | | return 0; |
| 213 | | } |
| 214 | | return 1; |
| 215 | | } |
| 216 | | |
| 217 | | typedef struct _d64_image { |
| 218 | | imgtool_image base; |
| 219 | | imgtool_stream *file_handle; |
| 220 | | int d64; |
| 221 | | int x64; // x64 header at the beginning |
| 222 | | int d71; // c128d/1571 diskette |
| 223 | | int d81; // c65/1581/1565 diskette |
| 224 | | |
| 225 | | int (*get_offset)(int track, int sector); |
| 226 | | void (*alloc_sector)(struct _d64_image *image, int *track, int *sector); |
| 227 | | void (*free_sector)(struct _d64_image *image, int track, int sector); |
| 228 | | struct { int track, sector; } directory; |
| 229 | | int bam_bytes_dir_track; |
| 230 | | |
| 231 | | int tracks; //35 or 40 tracks |
| 232 | | int crc; // crc data at the end of file |
| 233 | | int size; |
| 234 | | int modified; |
| 235 | | unsigned char *realimage; //inclusive x64 header |
| 236 | | unsigned char *data; // pointer to start of d64 image |
| 237 | | } d64_image; |
| 238 | | |
| 239 | | typedef struct { |
| 240 | | imgtool_directory base; |
| 241 | | d64_image *image; |
| 242 | | int track, sector; |
| 243 | | int offset; |
| 244 | | } d64_iterator; |
| 245 | | |
| 246 | | static void d64_alloc_sector(struct _d64_image *image, int *track, int *sector) |
| 247 | | { |
| 248 | | static int tracks[35]; |
| 249 | | int i, j; |
| 250 | | D64_HEADER *header=(D64_HEADER*) |
| 251 | | (image->data+image->get_offset(image->directory.track,image->directory.sector)); |
| 252 | | |
| 253 | | // this contains the tracks for the search strategy for free sectors |
| 254 | | for (i=0,j=17; i<35; i+=2,j--) tracks[i]=j; |
| 255 | | for (i=1,j=19; i<35; i+=2,j++) tracks[i]=j; |
| 256 | | tracks[35-1]=18; |
| 257 | | |
| 258 | | for (i=0; i<35; i++) { |
| 259 | | *track=tracks[i]; |
| 260 | | if (header->bam[*track-1].free) { |
| 261 | | for (j=0,*sector=0;j<3; j++) { |
| 262 | | int mask=1; |
| 263 | | for (; mask<=0x80; mask<<=1, (*sector)++) { |
| 264 | | if (header->bam[*track-1].map[j]&mask) { |
| 265 | | header->bam[*track-1].map[j]&=~mask; |
| 266 | | header->bam[*track-1].free--; |
| 267 | | return; |
| 268 | | } |
| 269 | | } |
| 270 | | } |
| 271 | | } |
| 272 | | } |
| 273 | | } |
| 274 | | |
| 275 | | static void d64_free_sector(struct _d64_image *image, int track, int sector) |
| 276 | | { |
| 277 | | D64_HEADER *header=(D64_HEADER*) |
| 278 | | (image->data+image->get_offset(image->directory.track,image->directory.sector)); |
| 279 | | int i=sector>>3; |
| 280 | | int mask=1<<(sector&7); |
| 281 | | header->bam[track-1].free++; |
| 282 | | header->bam[track-1].map[i]|=mask; |
| 283 | | } |
| 284 | | |
| 285 | | static void d71_alloc_sector(struct _d64_image *image, int *track, int *sector) |
| 286 | | { |
| 287 | | static int tracks[70]; |
| 288 | | int i, j; |
| 289 | | D64_HEADER *header=(D64_HEADER*) |
| 290 | | (image->data+image->get_offset(image->directory.track,image->directory.sector)); |
| 291 | | D71_HEADER *header2=(D71_HEADER*) |
| 292 | | (image->data+image->get_offset(image->directory.track+35,image->directory.sector)); |
| 293 | | |
| 294 | | // this contains the tracks for the search strategy for free sectors |
| 295 | | for (i=0,j=17; i<35; i+=4,j--) { tracks[i]=j; tracks[i+1]=j+35; } |
| 296 | | for (i=2,j=19; i<35; i+=4,j++) { tracks[i]=j; tracks[i+1]=j+35; } |
| 297 | | tracks[70-1]=18; |
| 298 | | |
| 299 | | for (i=0; i<35; i++) { |
| 300 | | *track=tracks[i]; |
| 301 | | if (*tracks<=35) { |
| 302 | | if (header->bam[*track-1].free) { |
| 303 | | for (j=0,*sector=0;j<3; j++) { |
| 304 | | int mask=1; |
| 305 | | for (; mask<=0x80; mask<<=1, (*sector)++) { |
| 306 | | if (header->bam[*track-1].map[j]&mask) { |
| 307 | | header->bam[*track-1].map[j]&=~mask; |
| 308 | | header->bam[*track-1].free--; |
| 309 | | return; |
| 310 | | } |
| 311 | | } |
| 312 | | } |
| 313 | | } |
| 314 | | } else { |
| 315 | | if (header->free2[*track-36]) { |
| 316 | | for (j=0,*sector=0;j<3; j++) { |
| 317 | | int mask=1; |
| 318 | | for (; mask<=0x80; mask<<=1, (*sector)++) { |
| 319 | | if (header2->bam2[*track-36].map2[j]&mask) { |
| 320 | | header2->bam2[*track-36].map2[j]&=~mask; |
| 321 | | header->free2[*track-36]--; |
| 322 | | return; |
| 323 | | } |
| 324 | | } |
| 325 | | } |
| 326 | | } |
| 327 | | } |
| 328 | | } |
| 329 | | } |
| 330 | | |
| 331 | | static void d71_free_sector(struct _d64_image *image, int track, int sector) |
| 332 | | { |
| 333 | | D64_HEADER *header=(D64_HEADER*) |
| 334 | | (image->data+image->get_offset(image->directory.track,image->directory.sector)); |
| 335 | | D71_HEADER *header2=(D71_HEADER*) |
| 336 | | (image->data+image->get_offset(image->directory.track+35,image->directory.sector)); |
| 337 | | int i=sector>>3; |
| 338 | | int mask=1<<(sector&7); |
| 339 | | if (track<=35) { |
| 340 | | header->bam[track-1].free++; |
| 341 | | header->bam[track-1].map[i]|=mask; |
| 342 | | } else { |
| 343 | | header->free2[track-36]++; |
| 344 | | header2->bam2[track-36].map2[i]|=mask; |
| 345 | | } |
| 346 | | } |
| 347 | | |
| 348 | | static void d81_alloc_sector(struct _d64_image *image, int *track, int *sector) |
| 349 | | { |
| 350 | | static int tracks[80]; |
| 351 | | int i, j; |
| 352 | | D81_BAM *bam[2]; |
| 353 | | bam[0] = (D81_BAM*) (image->data + image->get_offset(image->directory.track, image->directory.sector+1)), |
| 354 | | bam[1] = (D81_BAM*) (image->data + image->get_offset(image->directory.track, image->directory.sector+1)); |
| 355 | | |
| 356 | | /* this contains the tracks for the search strategy for free sectors */ |
| 357 | | for (i=0,j=image->directory.track-1; j>0; i++,j--) tracks[i]=j; |
| 358 | | for (j=image->directory.track+1;j<80;i++,j++) tracks[i]=j; |
| 359 | | tracks[i]=image->directory.track; |
| 360 | | |
| 361 | | for (i=0; i<80; i++) { |
| 362 | | D81_BAM *b; |
| 363 | | *track=tracks[i]; |
| 364 | | if (*track<=40) b=bam[0]; |
| 365 | | else b=bam[1]; |
| 366 | | if (b->bam[*track-1].free) { |
| 367 | | for (j=0,*sector=0;j<6; j++) { |
| 368 | | int mask=0; |
| 369 | | for (; mask<=0x80; mask<<=1, (*sector)++) { |
| 370 | | if (b->bam[*track-1].map[j]&mask) { |
| 371 | | b->bam[*track-1].map[j]&=~mask; |
| 372 | | b->bam[*track-1].free--; |
| 373 | | return; |
| 374 | | } |
| 375 | | } |
| 376 | | } |
| 377 | | } |
| 378 | | } |
| 379 | | } |
| 380 | | |
| 381 | | static void d81_free_sector(struct _d64_image *image, int track, int sector) |
| 382 | | { |
| 383 | | D81_BAM *bam; |
| 384 | | int i=sector>>3; |
| 385 | | int mask=1<<(sector&7); |
| 386 | | |
| 387 | | if (track<=40) { |
| 388 | | bam=(D81_BAM*) |
| 389 | | (image->data |
| 390 | | +image->get_offset(image->directory.track, |
| 391 | | image->directory.sector+1)); |
| 392 | | } else { |
| 393 | | track-=40; |
| 394 | | bam=(D81_BAM*) |
| 395 | | (image->data |
| 396 | | +image->get_offset(image->directory.track, |
| 397 | | image->directory.sector+2)); |
| 398 | | } |
| 399 | | |
| 400 | | bam->bam[track-1].free++; |
| 401 | | bam->bam[track-1].map[i]|=mask; |
| 402 | | } |
| 403 | | |
| 404 | | int d64_getcrc(d64_image *image, int track, int sector) |
| 405 | | { |
| 406 | | int pos=d64_tracksector2offset(track, sector)>>8; |
| 407 | | if (!image->crc) return 0; |
| 408 | | return image->data[d64_tracksector2offset(image->tracks+1,0)+pos]; |
| 409 | | } |
| 410 | | |
| 411 | | static D64_ENTRY *d64_get_free_entry(d64_image *image) |
| 412 | | { |
| 413 | | D64_HEADER *header; |
| 414 | | D64_DIRECTORY *dir = (D64_DIRECTORY*) |
| 415 | | (image->data+image->get_offset (image->directory.track, image->directory.sector)); |
| 416 | | |
| 417 | | int i, mask, s; |
| 418 | | |
| 419 | | while ((dir->linkage.track >= 1) && (dir->linkage.track <= image->tracks)) |
| 420 | | { |
| 421 | | dir=(D64_DIRECTORY*) |
| 422 | | (image->data+image->get_offset(dir->linkage.track, dir->linkage.sector)); |
| 423 | | for (i = 0; i < 8; i ++) |
| 424 | | { |
| 425 | | if (!(dir->entry[i].type & 0x80)) |
| 426 | | return dir->entry + i; |
| 427 | | } |
| 428 | | } |
| 429 | | |
| 430 | | // add new sector to directory |
| 431 | | header = (D64_HEADER*) |
| 432 | | (image->data+image->get_offset (image->directory.track, image->directory.sector)); |
| 433 | | |
| 434 | | if (header->bam[image->directory.track-1].free) { |
| 435 | | for (i=0, s=0; i<image->bam_bytes_dir_track; i++) { |
| 436 | | for (mask=0x80; mask>0; mask>>=1, s++) { |
| 437 | | if (header->bam[image->directory.track-1].map[i]&mask) { |
| 438 | | header->bam[image->directory.track-1].free--; |
| 439 | | header->bam[image->directory.track-1].map[i]&=~mask; |
| 440 | | dir->linkage.track=image->directory.track-1; |
| 441 | | dir->linkage.sector=s; |
| 442 | | return (D64_ENTRY*)(image->data+image->get_offset(image->directory.track, s)); |
| 443 | | } |
| 444 | | } |
| 445 | | } |
| 446 | | } |
| 447 | | // no sector in directory track free |
| 448 | | return NULL; |
| 449 | | } |
| 450 | | |
| 451 | | /* searches program with given name in directory */ |
| 452 | | static D64_ENTRY *d64_image_findfile (d64_image *image, const unsigned char *name) |
| 453 | | { |
| 454 | | int i; |
| 455 | | |
| 456 | | D64_DIRECTORY *dir = (D64_DIRECTORY*) |
| 457 | | (image->data+image->get_offset (image->directory.track, image->directory.sector)); |
| 458 | | |
| 459 | | while ((dir->linkage.track >= 1) && (dir->linkage.track <= image->tracks)) |
| 460 | | { |
| 461 | | dir=(D64_DIRECTORY*) |
| 462 | | (image->data+image->get_offset(dir->linkage.track, dir->linkage.sector)); |
| 463 | | for (i = 0; i < 8; i ++) |
| 464 | | { |
| 465 | | if (dir->entry[i].type & 0x80) |
| 466 | | { |
| 467 | | if (core_stricmp ((char *) name, (char *) "*") == 0) |
| 468 | | return dir->entry + i; |
| 469 | | if (cbm_compareNames (name, dir->entry[i].name)) |
| 470 | | return dir->entry + i; |
| 471 | | } |
| 472 | | } |
| 473 | | } |
| 474 | | return NULL; |
| 475 | | } |
| 476 | | |
| 477 | | #if 0 |
| 478 | | static int d64_filesize(d64_image *image, D64_ENTRY *entry) |
| 479 | | { |
| 480 | | int size = 0; |
| 481 | | int i=image->get_offset(entry->track, entry->sector); |
| 482 | | |
| 483 | | while (image->data[i] != 0) |
| 484 | | { |
| 485 | | size += 254; |
| 486 | | i = image->get_offset (image->data[i], image->data[i + 1]); |
| 487 | | } |
| 488 | | size += image->data[i + 1]-1; |
| 489 | | return size; |
| 490 | | } |
| 491 | | #endif |
| 492 | | |
| 493 | | static int d64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 494 | | static void d64_image_exit(imgtool_image *img); |
| 495 | | static void d64_image_info(imgtool_image *img, char *string, const int len); |
| 496 | | static int d64_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 497 | | static int d64_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 498 | | static void d64_image_closeenum(imgtool_directory *enumeration); |
| 499 | | static size_t d64_image_freespace(imgtool_image *img); |
| 500 | | static int d64_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 501 | | static int d64_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_); |
| 502 | | static int d64_image_deletefile(imgtool_image *img, const char *fname); |
| 503 | | static int d64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 504 | | |
| 505 | | static int x64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 506 | | static int x64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 507 | | |
| 508 | | static int d71_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 509 | | static size_t d71_image_freespace(imgtool_image *img); |
| 510 | | static int d71_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 511 | | |
| 512 | | static int d81_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 513 | | static void d81_image_info(imgtool_image *img, char *string, const int len); |
| 514 | | static size_t d81_image_freespace(imgtool_image *img); |
| 515 | | static int d81_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_); |
| 516 | | |
| 517 | | static int d64_read_sector(imgtool_image *img, UINT8 head, UINT8 track, UINT8 sector, int offset, void *buffer, int length); |
| 518 | | static int d64_write_sector(imgtool_image *img, UINT8 head, UINT8 track, UINT8 sector, int offset, const void *buffer, int length); |
| 519 | | |
| 520 | | static const struct OptionTemplate d64_createopts[] = |
| 521 | | { |
| 522 | | { "label", NULL, IMGOPTION_FLAG_TYPE_STRING | IMGOPTION_FLAG_HASDEFAULT, 0, 0, NULL }, /* [3] */ |
| 523 | | { NULL, NULL, 0, 0, 0, 0 } |
| 524 | | }; |
| 525 | | |
| 526 | | #define D64_CREATEOPTION_LABEL 0 |
| 527 | | |
| 528 | | IMAGEMODULE( |
| 529 | | d64, |
| 530 | | "Commodore SX64/VC1541/2031/1551 Diskette", /* human readable name */ |
| 531 | | "d64", /* file extension */ |
| 532 | | NULL, /* crcfile */ |
| 533 | | NULL, /* crc system name */ |
| 534 | | EOLN_CR, /* eoln */ |
| 535 | | 0, /* flags */ |
| 536 | | d64_image_init, /* init function */ |
| 537 | | d64_image_exit, /* exit function */ |
| 538 | | d64_image_info, /* info function */ |
| 539 | | d64_image_beginenum, /* begin enumeration */ |
| 540 | | d64_image_nextenum, /* enumerate next */ |
| 541 | | d64_image_closeenum, /* close enumeration */ |
| 542 | | d64_image_freespace, /* free space on image */ |
| 543 | | d64_image_readfile, /* read file */ |
| 544 | | d64_image_writefile, /* write file */ |
| 545 | | d64_image_deletefile, /* delete file */ |
| 546 | | d64_image_create, /* create image */ |
| 547 | | d64_read_sector, |
| 548 | | d64_write_sector, |
| 549 | | NULL, /* file options */ |
| 550 | | d64_createopts /* create options */ |
| 551 | | ) |
| 552 | | |
| 553 | | IMAGEMODULE( |
| 554 | | x64, |
| 555 | | "Commodore VC1541 Diskette", /* human readable name */ |
| 556 | | "x64", /* file extension */ |
| 557 | | NULL, /* crcfile */ |
| 558 | | NULL, /* crc system name */ |
| 559 | | EOLN_CR, /* eoln */ |
| 560 | | 0, /* flags */ |
| 561 | | x64_image_init, /* init function */ |
| 562 | | d64_image_exit, /* exit function */ |
| 563 | | d64_image_info, /* info function */ |
| 564 | | d64_image_beginenum, /* begin enumeration */ |
| 565 | | d64_image_nextenum, /* enumerate next */ |
| 566 | | d64_image_closeenum, /* close enumeration */ |
| 567 | | d64_image_freespace, /* free space on image */ |
| 568 | | d64_image_readfile, /* read file */ |
| 569 | | d64_image_writefile, /* write file */ |
| 570 | | d64_image_deletefile, /* delete file */ |
| 571 | | x64_image_create, /* create image */ |
| 572 | | d64_read_sector, |
| 573 | | d64_write_sector, |
| 574 | | NULL, /* file options */ |
| 575 | | d64_createopts /* create options */ |
| 576 | | ) |
| 577 | | |
| 578 | | IMAGEMODULE( |
| 579 | | d71, |
| 580 | | "Commodore 128D/1571 Diskette", /* human readable name */ |
| 581 | | "d71", /* file extension */ |
| 582 | | NULL, /* crcfile */ |
| 583 | | NULL, /* crc system name */ |
| 584 | | EOLN_CR, /* eoln */ |
| 585 | | 0, /* flags */ |
| 586 | | d71_image_init, /* init function */ |
| 587 | | d64_image_exit, /* exit function */ |
| 588 | | d64_image_info, /* info function */ |
| 589 | | d64_image_beginenum, /* begin enumeration */ |
| 590 | | d64_image_nextenum, /* enumerate next */ |
| 591 | | d64_image_closeenum, /* close enumeration */ |
| 592 | | d71_image_freespace, /* free space on image */ |
| 593 | | d64_image_readfile, /* read file */ |
| 594 | | d64_image_writefile, /* write file */ |
| 595 | | d64_image_deletefile, /* delete file */ |
| 596 | | d71_image_create, /* create image */ |
| 597 | | d64_read_sector, |
| 598 | | d64_write_sector, |
| 599 | | NULL, /* file options */ |
| 600 | | d64_createopts /* create options */ |
| 601 | | ) |
| 602 | | |
| 603 | | IMAGEMODULE( |
| 604 | | d81, |
| 605 | | "Commodore 65/1565/1581 Diskette", /* human readable name */ |
| 606 | | "d81", /* file extension */ |
| 607 | | NULL, /* crcfile */ |
| 608 | | NULL, /* crc system name */ |
| 609 | | EOLN_CR, /* eoln */ |
| 610 | | 0, /* flags */ |
| 611 | | d81_image_init, /* init function */ |
| 612 | | d64_image_exit, /* exit function */ |
| 613 | | d81_image_info, /* info function */ |
| 614 | | d64_image_beginenum, /* begin enumeration */ |
| 615 | | d64_image_nextenum, /* enumerate next */ |
| 616 | | d64_image_closeenum, /* close enumeration */ |
| 617 | | d81_image_freespace, /* free space on image */ |
| 618 | | d64_image_readfile, /* read file */ |
| 619 | | d64_image_writefile, /* write file */ |
| 620 | | d64_image_deletefile, /* delete file */ |
| 621 | | d81_image_create, /* create image */ |
| 622 | | d64_read_sector, |
| 623 | | d64_write_sector, |
| 624 | | NULL, /* file options */ |
| 625 | | d64_createopts /* create options */ |
| 626 | | ) |
| 627 | | |
| 628 | | static int d64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 629 | | { |
| 630 | | d64_image *image; |
| 631 | | |
| 632 | | d64_open_helper(); |
| 633 | | image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image)); |
| 634 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 635 | | |
| 636 | | memset(image, 0, sizeof(d64_image)); |
| 637 | | image->base.module = mod; |
| 638 | | image->size=stream_size(f); |
| 639 | | image->file_handle=f; |
| 640 | | image->get_offset=d64_tracksector2offset; |
| 641 | | image->alloc_sector=d64_alloc_sector; |
| 642 | | image->free_sector=d64_free_sector; |
| 643 | | image->directory.track=18;image->directory.sector=0; |
| 644 | | image->bam_bytes_dir_track=3; |
| 645 | | image->d64=1; |
| 646 | | |
| 647 | | switch (image->size) { |
| 648 | | case 174848: image->tracks=35;break; |
| 649 | | case 175531: image->tracks=35;image->crc=1;break; |
| 650 | | case 196608: image->tracks=40;break; |
| 651 | | case 197376: image->tracks=40;image->crc=1;break; |
| 652 | | default: |
| 653 | | free(image); |
| 654 | | *outimg=NULL; |
| 655 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 656 | | } |
| 657 | | |
| 658 | | image->realimage = (unsigned char *) malloc(image->size); |
| 659 | | image->data = image->realimage; |
| 660 | | if ( (!image->data) |
| 661 | | ||(stream_read(f, image->data, image->size)!=image->size) ) { |
| 662 | | free(image); |
| 663 | | *outimg=NULL; |
| 664 | | return IMGTOOLERR_OUTOFMEMORY; |
| 665 | | } |
| 666 | | |
| 667 | | return 0; |
| 668 | | } |
| 669 | | |
| 670 | | static int x64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 671 | | { |
| 672 | | d64_image *image; |
| 673 | | |
| 674 | | d64_open_helper(); |
| 675 | | image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image)); |
| 676 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 677 | | |
| 678 | | memset(image, 0, sizeof(d64_image)); |
| 679 | | image->base.module = mod; |
| 680 | | image->size=stream_size(f); |
| 681 | | image->file_handle=f; |
| 682 | | image->get_offset=d64_tracksector2offset; |
| 683 | | image->alloc_sector=d64_alloc_sector; |
| 684 | | image->free_sector=d64_free_sector; |
| 685 | | image->directory.track=18;image->directory.sector=0; |
| 686 | | image->bam_bytes_dir_track=3; |
| 687 | | image->x64=1; |
| 688 | | switch (image->size) { |
| 689 | | case 174848+0x40: image->tracks=35;break; |
| 690 | | case 175531+0x40: image->tracks=35;image->crc=1;break; |
| 691 | | case 196608+0x40: image->tracks=40;break; |
| 692 | | case 197376+0x40: image->tracks=40;image->crc=1;break; |
| 693 | | default: |
| 694 | | free(image); |
| 695 | | *outimg=NULL; |
| 696 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 697 | | } |
| 698 | | |
| 699 | | image->realimage = (unsigned char *) malloc(image->size); |
| 700 | | image->data = image->realimage+0x40; |
| 701 | | if ( (!image->realimage) |
| 702 | | ||(stream_read(f, image->realimage, image->size)!=image->size) ) { |
| 703 | | free(image); |
| 704 | | *outimg=NULL; |
| 705 | | return IMGTOOLERR_OUTOFMEMORY; |
| 706 | | } |
| 707 | | |
| 708 | | return 0; |
| 709 | | } |
| 710 | | |
| 711 | | static int d71_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 712 | | { |
| 713 | | d64_image *image; |
| 714 | | |
| 715 | | d64_open_helper(); |
| 716 | | image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image)); |
| 717 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 718 | | |
| 719 | | memset(image, 0, sizeof(d64_image)); |
| 720 | | image->base.module = mod; |
| 721 | | image->size=stream_size(f); |
| 722 | | image->file_handle=f; |
| 723 | | image->get_offset=d71_tracksector2offset; |
| 724 | | image->alloc_sector=d71_alloc_sector; |
| 725 | | image->free_sector=d71_free_sector; |
| 726 | | image->directory.track=18;image->directory.sector=0; |
| 727 | | image->d71=1; |
| 728 | | image->tracks=70; |
| 729 | | |
| 730 | | image->realimage = (unsigned char *) malloc(image->size); |
| 731 | | image->data = image->realimage; |
| 732 | | if ( (!image->data) |
| 733 | | ||(stream_read(f, image->data, image->size)!=image->size) ) { |
| 734 | | free(image); |
| 735 | | *outimg=NULL; |
| 736 | | return IMGTOOLERR_OUTOFMEMORY; |
| 737 | | } |
| 738 | | |
| 739 | | return 0; |
| 740 | | } |
| 741 | | |
| 742 | | static int d81_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 743 | | { |
| 744 | | d64_image *image; |
| 745 | | |
| 746 | | d64_open_helper(); |
| 747 | | image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image)); |
| 748 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 749 | | |
| 750 | | memset(image, 0, sizeof(d64_image)); |
| 751 | | image->base.module = mod; |
| 752 | | image->size=stream_size(f); |
| 753 | | image->get_offset=d81_tracksector2offset; |
| 754 | | image->alloc_sector=d81_alloc_sector; |
| 755 | | image->free_sector=d81_free_sector; |
| 756 | | image->directory.track=40;image->directory.sector=0; |
| 757 | | image->d81=1; |
| 758 | | image->tracks=80; |
| 759 | | image->bam_bytes_dir_track=6; |
| 760 | | image->file_handle=f; |
| 761 | | |
| 762 | | image->realimage = (unsigned char *) malloc(image->size); |
| 763 | | image->data = image->realimage; |
| 764 | | if ( (!image->data) |
| 765 | | ||(stream_read(f, image->data, image->size)!=image->size) ) { |
| 766 | | free(image); |
| 767 | | *outimg=NULL; |
| 768 | | return IMGTOOLERR_OUTOFMEMORY; |
| 769 | | } |
| 770 | | |
| 771 | | return 0; |
| 772 | | } |
| 773 | | |
| 774 | | static void d64_image_exit(imgtool_image *img) |
| 775 | | { |
| 776 | | d64_image *image=(d64_image*)img; |
| 777 | | if (image->modified) { |
| 778 | | stream_seek(image->file_handle, 0, SEEK_SET); |
| 779 | | stream_write(image->file_handle, image->realimage, image->size); |
| 780 | | } |
| 781 | | stream_close(image->file_handle); |
| 782 | | free(image->realimage); |
| 783 | | free(image); |
| 784 | | } |
| 785 | | |
| 786 | | static void d64_image_info(imgtool_image *img, char *string, const int len) |
| 787 | | { |
| 788 | | d64_image *image=(d64_image*)img; |
| 789 | | char name[17]={ 0 }; |
| 790 | | |
| 791 | | int pos=image->get_offset(image->directory.track,image->directory.sector); |
| 792 | | |
| 793 | | cbm_filename_to_cstring((unsigned char *)image->data+pos+0x90, 16, |
| 794 | | (unsigned char*)name); |
| 795 | | if ((image->d64)||(image->x64)) { |
| 796 | | sprintf(string, "%d tracks%s\n%-16s %c%c %c%c", |
| 797 | | image->tracks, image->crc?" with crc":"", |
| 798 | | name, |
| 799 | | image->data[pos + 162], image->data[pos + 163], |
| 800 | | image->data[pos + 165], image->data[pos + 166] ); |
| 801 | | } else { |
| 802 | | sprintf(string, "%-16s %c%c %c%c", |
| 803 | | name, |
| 804 | | image->data[pos + 162], image->data[pos + 163], |
| 805 | | image->data[pos + 165], image->data[pos + 166] ); |
| 806 | | } |
| 807 | | } |
| 808 | | |
| 809 | | static void d81_image_info(imgtool_image *img, char *string, const int len) |
| 810 | | { |
| 811 | | d64_image *image=(d64_image*)img; |
| 812 | | char name[17]={ 0 }; |
| 813 | | |
| 814 | | int pos=image->get_offset(image->directory.track,image->directory.sector); |
| 815 | | |
| 816 | | cbm_filename_to_cstring((unsigned char *)image->data+pos+4, 16, |
| 817 | | (unsigned char *)name); |
| 818 | | sprintf(string, "%d tracks%s\n%-16s %c%c %c%c", |
| 819 | | image->tracks, image->crc?" with crc":"", |
| 820 | | name, |
| 821 | | image->data[pos + 22], image->data[pos + 23], |
| 822 | | image->data[pos + 25], image->data[pos + 26] ); |
| 823 | | } |
| 824 | | |
| 825 | | static int d64_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 826 | | { |
| 827 | | d64_image *image=(d64_image*)img; |
| 828 | | d64_iterator *iter; |
| 829 | | int pos; |
| 830 | | |
| 831 | | iter=*(d64_iterator**)outenum = (d64_iterator *) malloc(sizeof(d64_iterator)); |
| 832 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 833 | | |
| 834 | | iter->base.module = img->module; |
| 835 | | |
| 836 | | iter->image=image; |
| 837 | | pos = image->get_offset (image->directory.track, image->directory.sector); |
| 838 | | iter->track = iter->image->data[pos]; |
| 839 | | iter->sector = iter->image->data[pos + 1]; |
| 840 | | iter->offset = 2; |
| 841 | | return 0; |
| 842 | | } |
| 843 | | |
| 844 | | static int d64_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 845 | | { |
| 846 | | d64_iterator *iter=(d64_iterator*)enumeration; |
| 847 | | int pos; |
| 848 | | |
| 849 | | ent->corrupt=0; |
| 850 | | ent->eof=0; |
| 851 | | |
| 852 | | while ((iter->track >= 1) && (iter->track <= iter->image->tracks)) //safer |
| 853 | | // while (iter->track != 0) |
| 854 | | { |
| 855 | | pos = iter->image->get_offset(iter->track, iter->sector); |
| 856 | | for (; iter->offset<256; iter->offset += 32) |
| 857 | | { |
| 858 | | if (iter->image->data[pos + iter->offset] & 0x80) |
| 859 | | { |
| 860 | | cbm_filename_to_cstring( |
| 861 | | (unsigned char*)iter->image->data+pos+iter->offset+3, |
| 862 | | 16, (unsigned char *)ent->fname); |
| 863 | | |
| 864 | | ent->filesize=iter->image->data[pos+iter->offset+28] |
| 865 | | +(iter->image->data[pos+iter->offset+29]<<8); // in blocks |
| 866 | | |
| 867 | | if (ent->attr) { |
| 868 | | switch (iter->image->data[pos+iter->offset]&FILE_TYPE_MASK) { |
| 869 | | case FILE_TYPE_DEL: sprintf(ent->attr,"DEL");break; |
| 870 | | case FILE_TYPE_SEQ: sprintf(ent->attr,"SEQ");break; |
| 871 | | case FILE_TYPE_PRG: sprintf(ent->attr,"PRG");break; |
| 872 | | case FILE_TYPE_USR: sprintf(ent->attr,"USR");break; |
| 873 | | case FILE_TYPE_REL: sprintf(ent->attr,"REL");break; |
| 874 | | default: sprintf(ent->attr,"unknown!");break; |
| 875 | | } |
| 876 | | } |
| 877 | | iter->offset+=32; |
| 878 | | return 0; |
| 879 | | } |
| 880 | | } |
| 881 | | iter->track = iter->image->data[pos]; |
| 882 | | iter->sector = iter->image->data[pos + 1]; |
| 883 | | iter->offset = 2; |
| 884 | | } |
| 885 | | |
| 886 | | ent->eof=1; |
| 887 | | return 0; |
| 888 | | } |
| 889 | | |
| 890 | | static void d64_image_closeenum(imgtool_directory *enumeration) |
| 891 | | { |
| 892 | | free(enumeration); |
| 893 | | } |
| 894 | | |
| 895 | | static size_t d64_image_freespace(imgtool_image *img) |
| 896 | | { |
| 897 | | d64_image *image=(d64_image*)img; |
| 898 | | int i, j, pos; |
| 899 | | size_t blocksfree = 0; |
| 900 | | |
| 901 | | pos = image->get_offset(image->directory.track, image->directory.sector); |
| 902 | | blocksfree = 0; |
| 903 | | for (j = 1, i = 4; j <= 35; j++, i += 4) // no more room for BAM! |
| 904 | | { |
| 905 | | blocksfree += image->data[pos + i]; |
| 906 | | } |
| 907 | | |
| 908 | | return blocksfree; |
| 909 | | } |
| 910 | | |
| 911 | | static size_t d71_image_freespace(imgtool_image *img) |
| 912 | | { |
| 913 | | d64_image *image=(d64_image*)img; |
| 914 | | int i, j, pos; |
| 915 | | size_t blocksfree = 0; |
| 916 | | |
| 917 | | pos = image->get_offset(image->directory.track, image->directory.sector); |
| 918 | | blocksfree = 0; |
| 919 | | for (j = 1, i = 4; j <= 35; j++, i += 4) |
| 920 | | { |
| 921 | | blocksfree += image->data[pos + i]; |
| 922 | | } |
| 923 | | for (i = 221; j <= 70; j++, i ++) |
| 924 | | { |
| 925 | | blocksfree += image->data[pos + i]; |
| 926 | | } |
| 927 | | |
| 928 | | return blocksfree; |
| 929 | | } |
| 930 | | |
| 931 | | static size_t d81_image_freespace(imgtool_image *img) |
| 932 | | { |
| 933 | | d64_image *image=(d64_image*)img; |
| 934 | | int j,k; |
| 935 | | size_t blocksfree = 0; |
| 936 | | |
| 937 | | for (k=1; k<=2; k++) { |
| 938 | | D81_BAM *bam=(D81_BAM*)(image->data+image->get_offset(40,k)); |
| 939 | | for (j = 1; j <= 40; j++) |
| 940 | | { |
| 941 | | blocksfree += bam->bam[j-1].free; |
| 942 | | } |
| 943 | | } |
| 944 | | |
| 945 | | return blocksfree; |
| 946 | | } |
| 947 | | |
| 948 | | static int d64_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 949 | | { |
| 950 | | d64_image *image=(d64_image*)img; |
| 951 | | int pos; |
| 952 | | D64_ENTRY *entry; |
| 953 | | |
| 954 | | if ((entry=d64_image_findfile(image, (const unsigned char *)fname))==NULL ) |
| 955 | | return IMGTOOLERR_MODULENOTFOUND; |
| 956 | | |
| 957 | | pos = image->get_offset(entry->track, entry->sector); |
| 958 | | |
| 959 | | while (image->data[pos]!=0) { |
| 960 | | if (stream_write(destf, image->data+pos+2, 254)!=254) |
| 961 | | return IMGTOOLERR_WRITEERROR; |
| 962 | | |
| 963 | | pos = image->get_offset (image->data[pos + 0], image->data[pos + 1]); |
| 964 | | } |
| 965 | | if (image->data[pos+1]-1>0) { |
| 966 | | if (stream_write(destf, image->data+pos+2, image->data[pos+1]-1)!=image->data[pos+1]-1) |
| 967 | | return IMGTOOLERR_WRITEERROR; |
| 968 | | } |
| 969 | | |
| 970 | | return 0; |
| 971 | | } |
| 972 | | |
| 973 | | static int d64_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_) |
| 974 | | { |
| 975 | | d64_image *image=(d64_image*)img; |
| 976 | | int fsize, pos, i, b, freespace; |
| 977 | | int track, sector; |
| 978 | | D64_ENTRY *entry; |
| 979 | | |
| 980 | | fsize=stream_size(sourcef)+1; |
| 981 | | |
| 982 | | imgtool_partition_get_free_space(img, &freespace); |
| 983 | | |
| 984 | | if ((entry=d64_image_findfile(image, (const unsigned char *)fname))!=NULL ) { |
| 985 | | /* overriding */ |
| 986 | | if ((freespace + GET_UWORD(entry->blocks))*254<fsize) |
| 987 | | return IMGTOOLERR_NOSPACE; |
| 988 | | track=entry->track; |
| 989 | | sector=entry->sector; |
| 990 | | |
| 991 | | while (track!=0) { |
| 992 | | image->free_sector(image, track, sector); |
| 993 | | pos = image->get_offset(track, sector); |
| 994 | | track=image->data[pos]; |
| 995 | | sector=image->data[pos+1]; |
| 996 | | } |
| 997 | | } else { |
| 998 | | if (freespace*254<fsize) return IMGTOOLERR_NOSPACE; |
| 999 | | // search free entry |
| 1000 | | entry=d64_get_free_entry(image); |
| 1001 | | } |
| 1002 | | |
| 1003 | | entry->type=FILE_TYPE_PRG|0x80; |
| 1004 | | memset(entry->name, 0xa0, sizeof(entry->name)); |
| 1005 | | memcpy(entry->name, fname, strlen(fname)); |
| 1006 | | |
| 1007 | | image->alloc_sector(image, &track, §or); |
| 1008 | | entry->track=track; |
| 1009 | | entry->sector=sector; |
| 1010 | | pos = image->get_offset(track, sector); |
| 1011 | | |
| 1012 | | for (i = 0, b=0; i + 254 < fsize; i += 254, b++) |
| 1013 | | { |
| 1014 | | if (stream_read(sourcef, image->data+pos+2, 254)!=254) |
| 1015 | | return IMGTOOLERR_READERROR; |
| 1016 | | |
| 1017 | | image->alloc_sector(image, &track, §or); |
| 1018 | | image->data[pos]=track; |
| 1019 | | image->data[pos+1]=sector; |
| 1020 | | pos = image->get_offset (track, sector); |
| 1021 | | } |
| 1022 | | b++; |
| 1023 | | image->data[pos]=0; |
| 1024 | | image->data[pos+1]=fsize-i; |
| 1025 | | if (fsize-i-1>0) { |
| 1026 | | if (stream_read(sourcef, image->data+pos+2, fsize-i-1)!=fsize-i-1) |
| 1027 | | return IMGTOOLERR_READERROR; |
| 1028 | | } |
| 1029 | | image->data[pos+2+fsize-i]=0; |
| 1030 | | |
| 1031 | | SET_UWORD(entry->blocks, b); |
| 1032 | | image->modified=1; |
| 1033 | | |
| 1034 | | return 0; |
| 1035 | | } |
| 1036 | | |
| 1037 | | static int d64_image_deletefile(imgtool_image *img, const char *fname) |
| 1038 | | { |
| 1039 | | d64_image *image=(d64_image*)img; |
| 1040 | | int pos; |
| 1041 | | int sector, track; |
| 1042 | | D64_ENTRY *entry; |
| 1043 | | |
| 1044 | | if ((entry=d64_image_findfile(image, (const unsigned char *)fname))==NULL ) |
| 1045 | | return IMGTOOLERR_MODULENOTFOUND; |
| 1046 | | |
| 1047 | | track=entry->track; |
| 1048 | | sector=entry->sector; |
| 1049 | | |
| 1050 | | while (track!=0) { |
| 1051 | | image->free_sector(image, track, sector); |
| 1052 | | pos = image->get_offset(track, sector); |
| 1053 | | track=image->data[pos]; |
| 1054 | | sector=image->data[pos+1]; |
| 1055 | | } |
| 1056 | | |
| 1057 | | image->modified=1; |
| 1058 | | return 0; |
| 1059 | | } |
| 1060 | | |
| 1061 | | static int d64_read_sector(imgtool_image *img, UINT8 head, UINT8 track, UINT8 sector, int offset, void *buffer, int length) |
| 1062 | | { |
| 1063 | | /* not yet implemented */ |
| 1064 | | assert(0); |
| 1065 | | return IMGTOOLERR_UNEXPECTED; |
| 1066 | | } |
| 1067 | | |
| 1068 | | static int d64_write_sector(imgtool_image *img, UINT8 head, UINT8 track, UINT8 sector, int offset, const void *buffer, int length) |
| 1069 | | { |
| 1070 | | /* not yet implemented */ |
| 1071 | | assert(0); |
| 1072 | | return IMGTOOLERR_UNEXPECTED; |
| 1073 | | } |
| 1074 | | |
| 1075 | | static int d64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 1076 | | { |
| 1077 | | unsigned char sector[0x100]={0}; |
| 1078 | | int tracks=35; |
| 1079 | | int crc=0; |
| 1080 | | int t,s,i; |
| 1081 | | |
| 1082 | | for (t=1; t<=tracks; t++) { |
| 1083 | | for (s=0; s<d64_sectors_per_track[t-1]; s++) { |
| 1084 | | if ((t==18)&&(s==0)) { |
| 1085 | | for (i=1; i<=35; i++) { |
| 1086 | | d64_header.bam[i-1].free=d64_sectors_per_track[i-1]; |
| 1087 | | d64_header.bam[i-1].map[0]=0xff; |
| 1088 | | d64_header.bam[i-1].map[1]=0xff; |
| 1089 | | switch (d64_header.bam[i-1].free) { |
| 1090 | | case 17: d64_header.bam[i-1].map[2]=1;break; |
| 1091 | | case 18: d64_header.bam[i-1].map[2]=3;break; |
| 1092 | | case 19: d64_header.bam[i-1].map[2]=7;break; |
| 1093 | | case 21: d64_header.bam[i-1].map[2]=0x1f;break; |
| 1094 | | } |
| 1095 | | if (i==18) { // allocate the 2 directory sectors |
| 1096 | | d64_header.bam[i-1].free-=2; |
| 1097 | | d64_header.bam[i-1].map[0]&=~3; |
| 1098 | | } |
| 1099 | | } |
| 1100 | | //d64_header.name=0xa0; |
| 1101 | | //d64_header.id=0xa0; |
| 1102 | | if (stream_write(f, &d64_header, sizeof(d64_header)) != sizeof(d64_header)) |
| 1103 | | return IMGTOOLERR_WRITEERROR; |
| 1104 | | } else { |
| 1105 | | if (stream_write(f, §or, sizeof(sector)) != sizeof(sector)) |
| 1106 | | return IMGTOOLERR_WRITEERROR; |
| 1107 | | } |
| 1108 | | } |
| 1109 | | } |
| 1110 | | if (crc) { |
| 1111 | | for (t=1; t<=tracks; t++) { |
| 1112 | | if (stream_write(f, &d64_header, d64_sectors_per_track[t-1]) |
| 1113 | | != d64_sectors_per_track[t-1]) |
| 1114 | | return IMGTOOLERR_WRITEERROR; |
| 1115 | | } |
| 1116 | | } |
| 1117 | | return 0; |
| 1118 | | } |
| 1119 | | |
| 1120 | | static int x64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 1121 | | { |
| 1122 | | static const unsigned char x64_header[40] = |
| 1123 | | { 0x43, 0x15, 0x41, 0x64, |
| 1124 | | 0x01, 0x02, 0x01, 0x23, |
| 1125 | | 0x01 |
| 1126 | | }; |
| 1127 | | if (stream_write(f, &x64_header, sizeof(x64_header)) != sizeof(x64_header)) |
| 1128 | | return IMGTOOLERR_WRITEERROR; |
| 1129 | | |
| 1130 | | d64_image_create(mod, f, options_); |
| 1131 | | return 0; |
| 1132 | | } |
| 1133 | | |
| 1134 | | static int d71_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 1135 | | { |
| 1136 | | D71_HEADER d71_header= { { 0 } }; |
| 1137 | | unsigned char sector[0x100]={0}; |
| 1138 | | int t,s,i; |
| 1139 | | |
| 1140 | | for (t=1; t<=70; t++) { |
| 1141 | | for (s=0; s<d71_sectors_per_track[t-1]; s++) { |
| 1142 | | if ((t==18)&&(s==0)) { |
| 1143 | | d64_header.doublesided=0x80; |
| 1144 | | for (i=1; i<=35; i++) { |
| 1145 | | d64_header.bam[i-1].free=d71_sectors_per_track[i-1]; |
| 1146 | | d64_header.bam[i-1].map[0]=0xff; |
| 1147 | | d64_header.bam[i-1].map[1]=0xff; |
| 1148 | | switch (d64_header.bam[i-1].free) { |
| 1149 | | case 17: d64_header.bam[i-1].map[2]=1;break; |
| 1150 | | case 18: d64_header.bam[i-1].map[2]=3;break; |
| 1151 | | case 19: d64_header.bam[i-1].map[2]=7;break; |
| 1152 | | case 21: d64_header.bam[i-1].map[2]=0x1f;break; |
| 1153 | | } |
| 1154 | | if (i==18) { // allocate the 2 directory sectors |
| 1155 | | d64_header.bam[i-1].free-=2; |
| 1156 | | d64_header.bam[i-1].map[0]&=~3; |
| 1157 | | } |
| 1158 | | } |
| 1159 | | for (;i<=70;i++) { |
| 1160 | | if (i!=18+35) |
| 1161 | | d64_header.free2[i-36]=d71_sectors_per_track[i-1]; |
| 1162 | | else |
| 1163 | | d64_header.free2[i-36]=0; |
| 1164 | | } |
| 1165 | | //d64_header.name=0xa0; |
| 1166 | | //d64_header.id=0xa0; |
| 1167 | | if (stream_write(f, &d64_header, sizeof(d64_header)) != sizeof(d64_header)) |
| 1168 | | return IMGTOOLERR_WRITEERROR; |
| 1169 | | } else if ((t==35+18)&&(s==0)) { |
| 1170 | | for (i=36;i<=70;i++) { |
| 1171 | | if (i!=18+35) { |
| 1172 | | d71_header.bam2[i-36].map2[0]=0xff; |
| 1173 | | d71_header.bam2[i-36].map2[1]=0xff; |
| 1174 | | switch (d64_header.free2[i-36]) { |
| 1175 | | case 17: d71_header.bam2[i-36].map2[2]=1;break; |
| 1176 | | case 18: d71_header.bam2[i-36].map2[2]=3;break; |
| 1177 | | case 19: d71_header.bam2[i-36].map2[2]=7;break; |
| 1178 | | case 21: d71_header.bam2[i-36].map2[2]=0x1f;break; |
| 1179 | | } |
| 1180 | | } else { |
| 1181 | | d71_header.bam2[i-36].map2[0]=0; |
| 1182 | | d71_header.bam2[i-36].map2[1]=0; |
| 1183 | | d71_header.bam2[i-36].map2[2]=0; |
| 1184 | | } |
| 1185 | | } |
| 1186 | | if (stream_write(f, &d71_header, sizeof(d71_header)) |
| 1187 | | != sizeof(d71_header)) |
| 1188 | | return IMGTOOLERR_WRITEERROR; |
| 1189 | | } else { |
| 1190 | | if (stream_write(f, §or, sizeof(sector)) != sizeof(sector)) |
| 1191 | | return IMGTOOLERR_WRITEERROR; |
| 1192 | | } |
| 1193 | | } |
| 1194 | | } |
| 1195 | | return 0; |
| 1196 | | } |
| 1197 | | |
| 1198 | | static int d81_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *options_) |
| 1199 | | { |
| 1200 | | unsigned char sector[0x100]={0}; |
| 1201 | | static const unsigned char id[2]={'1','2'}; |
| 1202 | | int t,s,i; |
| 1203 | | |
| 1204 | | for (t=1; t<=80; t++) { |
| 1205 | | for (s=0; s<40; s++) { |
| 1206 | | if ((t==40)&&(s==0)) { |
| 1207 | | //d81_header.name=0xa0; |
| 1208 | | d81_header.id[0]=id[0]; |
| 1209 | | d81_header.id[0]=id[1]; |
| 1210 | | if (stream_write(f, &d81_header, sizeof(d81_header)) != sizeof(d81_header)) |
| 1211 | | return IMGTOOLERR_WRITEERROR; |
| 1212 | | } else if ((t==40)&&(s==1)){ |
| 1213 | | d81_bam.id[0]=id[0]; |
| 1214 | | d81_bam.id[0]=id[1]; |
| 1215 | | for (i=1; i<=40; i++) { |
| 1216 | | d81_bam.bam[i-1].free=40; |
| 1217 | | d81_bam.bam[i-1].map[0]=0xff; |
| 1218 | | d81_bam.bam[i-1].map[1]=0xff; |
| 1219 | | d81_bam.bam[i-1].map[2]=0xff; |
| 1220 | | d81_bam.bam[i-1].map[3]=0xff; |
| 1221 | | d81_bam.bam[i-1].map[4]=0xff; |
| 1222 | | } |
| 1223 | | d81_bam.bam[40-1].free=36; |
| 1224 | | d81_bam.bam[40-1].map[0]=0xf0; |
| 1225 | | if (stream_write(f, &d81_bam, sizeof(d81_bam)) != sizeof(d81_bam)) |
| 1226 | | return IMGTOOLERR_WRITEERROR; |
| 1227 | | d81_bam.bam[40-1].free=40; |
| 1228 | | d81_bam.bam[40-1].map[0]=0xff; |
| 1229 | | } else if ((t==40)&&(s==2)){ |
| 1230 | | d81_bam.track=0; |
| 1231 | | if (stream_write(f, &d81_bam, sizeof(d81_bam)) != sizeof(d81_bam)) |
| 1232 | | return IMGTOOLERR_WRITEERROR; |
| 1233 | | } else { |
| 1234 | | if (stream_write(f, §or, sizeof(sector)) != sizeof(sector)) |
| 1235 | | return IMGTOOLERR_WRITEERROR; |
| 1236 | | } |
| 1237 | | } |
| 1238 | | } |
| 1239 | | return 0; |
| 1240 | | } |
src/mess/tools/imgtool/modules/vmsx_tap.c
| r13274 | r13275 | |
| 1 | | /* |
| 2 | | vMSX tap format |
| 3 | | |
| 4 | | Virtual MSX 1.x (being replaced by the MSX driver in mess) had |
| 5 | | it's on format for cassette recordings. The format is similar |
| 6 | | to .cas files; however several recordings could be stored in |
| 7 | | one file. |
| 8 | | |
| 9 | | Virtual MSX was never really popular; I haven't seen any of these |
| 10 | | files on the internet. However for completeness sake I've added |
| 11 | | this converter. It's read only; the MSX driver cannot read |
| 12 | | these files, but can read the .cas files (like most other MSX |
| 13 | | emulators). Further use and specially distribution of the .tap files |
| 14 | | is discouraged as Virtual MSX is obsolete (along with its private |
| 15 | | file formats). |
| 16 | | |
| 17 | | These files used the M$ mmio functions (similar to .aiff files). There |
| 18 | | is an INFO block which lists all files in the archive. The TAPE block |
| 19 | | contains the actual data. |
| 20 | | |
| 21 | | Original code in Virtual MSX: |
| 22 | | |
| 23 | | ftp://ftp.komkon.org/pub/EMUL8/MSX/Emulators/VMSX1SRC.zip |
| 24 | | |
| 25 | | It's in the file TAPE.C. |
| 26 | | |
| 27 | | RIFF format specs: |
| 28 | | |
| 29 | | http://www.saettler.com/RIFFMCI/riffmci.html |
| 30 | | |
| 31 | | |
| 32 | | This module converts the files to .cas files. Implemented are: |
| 33 | | |
| 34 | | dir - list all the files in the archive as "%title%-%type%.cas" |
| 35 | | getall - writes all the files to disk |
| 36 | | get - gets one file. If "getall" fails, due to characters in the |
| 37 | | filename your FS/OS doesn't support, use: |
| 38 | | |
| 39 | | imgtool get vmsx_tap monmsx.tap monmsx-binary.cas monmsx.cas |
| 40 | | |
| 41 | | for example. |
| 42 | | |
| 43 | | Sean Young |
| 44 | | */ |
| 45 | | |
| 46 | | #include "imgtoolx.h" |
| 47 | | |
| 48 | | typedef struct |
| 49 | | { |
| 50 | | char title[32]; |
| 51 | | char type[10]; |
| 52 | | char chunk[4]; |
| 53 | | } TAP_ENTRY; |
| 54 | | |
| 55 | | typedef struct |
| 56 | | { |
| 57 | | imgtool_image base; |
| 58 | | imgtool_stream *file_handle; |
| 59 | | int size; |
| 60 | | UINT8 *data; |
| 61 | | int count; |
| 62 | | TAP_ENTRY *entries; |
| 63 | | } TAP_IMAGE; |
| 64 | | |
| 65 | | typedef struct |
| 66 | | { |
| 67 | | imgtool_directory base; |
| 68 | | TAP_IMAGE *image; |
| 69 | | int index; |
| 70 | | } TAP_ITERATOR; |
| 71 | | |
| 72 | | static int vmsx_tap_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 73 | | static void vmsx_tap_image_exit(imgtool_image *img); |
| 74 | | static int vmsx_tap_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 75 | | static int vmsx_tap_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 76 | | static void vmsx_tap_image_closeenum(imgtool_directory *enumeration); |
| 77 | | static int vmsx_tap_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 78 | | |
| 79 | | IMAGEMODULE( |
| 80 | | vmsx_tap, |
| 81 | | "Virtual MSX Tape archive", /* human readable name */ |
| 82 | | "tap", /* file extension */ |
| 83 | | NULL, /* crcfile */ |
| 84 | | NULL, /* crc system name */ |
| 85 | | NULL, /* eoln */ |
| 86 | | 0, /* flags */ |
| 87 | | vmsx_tap_image_init, /* init function */ |
| 88 | | vmsx_tap_image_exit, /* exit function */ |
| 89 | | NULL, /* info function */ |
| 90 | | vmsx_tap_image_beginenum, /* begin enumeration */ |
| 91 | | vmsx_tap_image_nextenum, /* enumerate next */ |
| 92 | | vmsx_tap_image_closeenum, /* close enumeration */ |
| 93 | | NULL, /* free space on image */ |
| 94 | | vmsx_tap_image_readfile, /* read file */ |
| 95 | | NULL,/* write file */ |
| 96 | | NULL,/* delete file */ |
| 97 | | NULL,/* create image */ |
| 98 | | NULL, |
| 99 | | NULL, |
| 100 | | NULL, |
| 101 | | NULL |
| 102 | | ) |
| 103 | | |
| 104 | | static const UINT8 CasHeader[8] = { 0x1F,0xA6,0xDE,0xBA,0xCC,0x13,0x7D,0x74 }; |
| 105 | | |
| 106 | | |
| 107 | | static int vmsx_tap_read_image (TAP_IMAGE *image) |
| 108 | | { |
| 109 | | unsigned char *p, *pmem; |
| 110 | | unsigned int pos, i, size; |
| 111 | | UINT32 offset; |
| 112 | | |
| 113 | | pmem = image->data; |
| 114 | | size = image->size; |
| 115 | | pos = 0; |
| 116 | | while ( (pos + 8) < size) |
| 117 | | { |
| 118 | | offset = *((UINT32*)(pmem + pos + 4)); |
| 119 | | offset = LITTLE_ENDIANIZE_INT32 (offset); |
| 120 | | if (strncmp ((char*)pmem + pos, "INFO", 4) ) |
| 121 | | { |
| 122 | | /* not this chunk, skip */ |
| 123 | | if (offset & 1) offset++; /* pad byte */ |
| 124 | | pos += offset + 8; |
| 125 | | continue; |
| 126 | | } |
| 127 | | /* found it */ |
| 128 | | pos += 8; |
| 129 | | p = pmem + pos; |
| 130 | | /* intregity check */ |
| 131 | | if ( (offset + pos) > size) |
| 132 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 133 | | if (p[0] != 0 || p[1] != 1) |
| 134 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 135 | | |
| 136 | | image->count = p[2] + p[3] * 256; |
| 137 | | p += 4; |
| 138 | | if (offset < (image->count * (32 + 10 + 4) + 4) ) |
| 139 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 140 | | |
| 141 | | image->entries = (TAP_ENTRY*)malloc ( |
| 142 | | sizeof (TAP_ENTRY) * image->count); |
| 143 | | if (!image->entries) |
| 144 | | return IMGTOOLERR_OUTOFMEMORY; |
| 145 | | |
| 146 | | for (i=0;i<image->count;i++) |
| 147 | | { |
| 148 | | /* this should really check the data */ |
| 149 | | strncpy (image->entries[i].title, (char*)p, 32); p += 32; |
| 150 | | strncpy (image->entries[i].type, (char*)p, 10); p += 10; |
| 151 | | memcpy (image->entries[i].chunk, (char*)p, 4); p += 4; |
| 152 | | } |
| 153 | | |
| 154 | | return 0; |
| 155 | | } |
| 156 | | |
| 157 | | return IMGTOOLERR_MODULENOTFOUND; |
| 158 | | } |
| 159 | | |
| 160 | | static int vmsx_tap_image_read_data (TAP_IMAGE *image, char *chunk, unsigned char **pcas, int *psize) |
| 161 | | { |
| 162 | | unsigned int caspos, pos = 0, found = 0, offset, size, tappos; |
| 163 | | UINT32 tapblock; |
| 164 | | unsigned char *p, *pmem; |
| 165 | | |
| 166 | | size = image->size; |
| 167 | | pmem = image->data; |
| 168 | | |
| 169 | | while ( (pos + 8) < size) |
| 170 | | { |
| 171 | | offset = *((UINT32*)(pmem + pos + 4)); |
| 172 | | offset = LITTLE_ENDIANIZE_INT32 (offset); |
| 173 | | if (memcmp (pmem + pos, "LIST", 4) ) |
| 174 | | { |
| 175 | | if (offset & 1) offset++; |
| 176 | | pos += offset + 8; |
| 177 | | continue; |
| 178 | | } |
| 179 | | |
| 180 | | if (memcmp (pmem + pos + 8, "TAPE", 4) ) |
| 181 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 182 | | |
| 183 | | pos += 12; |
| 184 | | offset -= 8; |
| 185 | | if ( (pos + offset) > size) |
| 186 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 187 | | |
| 188 | | pmem += pos; |
| 189 | | size = offset; |
| 190 | | found = 1; |
| 191 | | break; |
| 192 | | } |
| 193 | | |
| 194 | | if (!found) |
| 195 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 196 | | |
| 197 | | /* OK we've got the right data chunk. Now we can start looking for |
| 198 | | the blocks */ |
| 199 | | pos = 0; |
| 200 | | while ( (pos + 8) < size) |
| 201 | | { |
| 202 | | offset = *((UINT32*)(pmem + pos + 4)); |
| 203 | | offset = LITTLE_ENDIANIZE_INT32 (offset); |
| 204 | | if (memcmp (pmem + pos, chunk, 4) ) |
| 205 | | { |
| 206 | | if (offset & 1) offset++; |
| 207 | | pos += offset + 8; |
| 208 | | continue; |
| 209 | | } |
| 210 | | |
| 211 | | pos += 8; |
| 212 | | caspos = tappos = 0; |
| 213 | | p = malloc (offset * 2); |
| 214 | | if (!p) return IMGTOOLERR_OUTOFMEMORY; |
| 215 | | |
| 216 | | while ( (tappos + 4) <= offset) |
| 217 | | { |
| 218 | | tapblock = *((UINT32*)(pmem + pos + tappos)); |
| 219 | | tappos += 4; |
| 220 | | tapblock = LITTLE_ENDIANIZE_INT32 (tapblock); |
| 221 | | if (tapblock == 0xffffffff) break; |
| 222 | | memcpy (p + caspos, CasHeader, 8); |
| 223 | | caspos += 8; |
| 224 | | memcpy (p + caspos, pmem + pos + tappos, tapblock); |
| 225 | | caspos += tapblock; |
| 226 | | tappos += tapblock; |
| 227 | | } |
| 228 | | |
| 229 | | |
| 230 | | *psize = caspos; |
| 231 | | *pcas = p; |
| 232 | | |
| 233 | | return 0; |
| 234 | | } |
| 235 | | |
| 236 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 237 | | } |
| 238 | | |
| 239 | | static int vmsx_tap_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 240 | | { |
| 241 | | TAP_IMAGE *image; |
| 242 | | int rc; |
| 243 | | |
| 244 | | image = (TAP_IMAGE*)malloc (sizeof (TAP_IMAGE) ); |
| 245 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 246 | | |
| 247 | | *outimg = (imgtool_image*)image; |
| 248 | | |
| 249 | | memset(image, 0, sizeof(TAP_IMAGE)); |
| 250 | | image->base.module = mod; |
| 251 | | image->size=stream_size(f); |
| 252 | | image->file_handle=f; |
| 253 | | |
| 254 | | image->data = (UINT8*) malloc(image->size); |
| 255 | | if (!image->data) |
| 256 | | { |
| 257 | | free(image); |
| 258 | | *outimg=NULL; |
| 259 | | return IMGTOOLERR_OUTOFMEMORY; |
| 260 | | } |
| 261 | | |
| 262 | | if (stream_read(f, image->data, image->size)!=image->size) |
| 263 | | { |
| 264 | | free(image); |
| 265 | | *outimg=NULL; |
| 266 | | return IMGTOOLERR_READERROR; |
| 267 | | } |
| 268 | | |
| 269 | | if ( (rc=vmsx_tap_read_image(image)) ) |
| 270 | | { |
| 271 | | if (image->entries) free(image->entries); |
| 272 | | free(image); |
| 273 | | *outimg=NULL; |
| 274 | | return rc; |
| 275 | | } |
| 276 | | |
| 277 | | return 0; |
| 278 | | } |
| 279 | | |
| 280 | | static void vmsx_tap_image_exit(imgtool_image *img) |
| 281 | | { |
| 282 | | TAP_IMAGE *image=(TAP_IMAGE*)img; |
| 283 | | stream_close(image->file_handle); |
| 284 | | free(image->entries); |
| 285 | | free(image->data); |
| 286 | | free(image); |
| 287 | | } |
| 288 | | |
| 289 | | static int vmsx_tap_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 290 | | { |
| 291 | | TAP_IMAGE *image=(TAP_IMAGE*)img; |
| 292 | | TAP_ITERATOR *iter; |
| 293 | | |
| 294 | | iter=*(TAP_ITERATOR**)outenum = (TAP_ITERATOR*) malloc(sizeof(TAP_ITERATOR)); |
| 295 | | if (!iter) |
| 296 | | return IMGTOOLERR_OUTOFMEMORY; |
| 297 | | |
| 298 | | iter->base.module = img->module; |
| 299 | | |
| 300 | | iter->image=image; |
| 301 | | iter->index = 0; |
| 302 | | return 0; |
| 303 | | } |
| 304 | | |
| 305 | | static int vmsx_tap_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 306 | | { |
| 307 | | TAP_ITERATOR *iter=(TAP_ITERATOR*)enumeration; |
| 308 | | unsigned char *p; |
| 309 | | int size, rc; |
| 310 | | |
| 311 | | ent->eof=iter->index>=iter->image->count; |
| 312 | | if (!ent->eof) |
| 313 | | { |
| 314 | | sprintf (ent->fname, "%s-%s.cas", |
| 315 | | iter->image->entries[iter->index].title, |
| 316 | | iter->image->entries[iter->index].type); |
| 317 | | size = 0; |
| 318 | | p = NULL; |
| 319 | | rc = vmsx_tap_image_read_data (iter->image, |
| 320 | | iter->image->entries[iter->index].chunk, &p, &size); |
| 321 | | |
| 322 | | ent->corrupt=rc; |
| 323 | | if (!rc) ent->filesize = size; |
| 324 | | iter->index++; |
| 325 | | |
| 326 | | if (p) free (p); |
| 327 | | } |
| 328 | | |
| 329 | | return 0; |
| 330 | | } |
| 331 | | |
| 332 | | static void vmsx_tap_image_closeenum(imgtool_directory *enumeration) |
| 333 | | { |
| 334 | | free (enumeration); |
| 335 | | } |
| 336 | | |
| 337 | | static TAP_ENTRY* vmsx_tap_image_findfile(TAP_IMAGE *image, const char *fname) |
| 338 | | { |
| 339 | | int i; |
| 340 | | char filename[64]; /* that's always enough! */ |
| 341 | | |
| 342 | | for (i=0; i<image->count; i++) |
| 343 | | { |
| 344 | | sprintf (filename, "%s-%s.cas", |
| 345 | | image->entries[i].title, image->entries[i].type); |
| 346 | | if (!strcmp(fname, filename) ) return image->entries+i; |
| 347 | | } |
| 348 | | return NULL; |
| 349 | | } |
| 350 | | |
| 351 | | static int vmsx_tap_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 352 | | { |
| 353 | | TAP_IMAGE *image=(TAP_IMAGE*)img; |
| 354 | | TAP_ENTRY *entry; |
| 355 | | unsigned char* p; |
| 356 | | int size, rc; |
| 357 | | |
| 358 | | if ((entry=vmsx_tap_image_findfile(image, fname))==NULL ) |
| 359 | | return IMGTOOLERR_MODULENOTFOUND; |
| 360 | | |
| 361 | | rc = vmsx_tap_image_read_data (image, entry->chunk, &p, &size); |
| 362 | | if (!rc) |
| 363 | | { |
| 364 | | if (stream_write(destf, p, size)!=size) |
| 365 | | { |
| 366 | | free (p); |
| 367 | | return IMGTOOLERR_WRITEERROR; |
| 368 | | } |
| 369 | | } |
| 370 | | |
| 371 | | free (p); |
| 372 | | |
| 373 | | return rc; |
| 374 | | } |
| 375 | | |
src/mess/tools/imgtool/modules/msx_dsk.c
| r13274 | r13275 | |
| 1 | | /* |
| 2 | | * msx_dsk.c : converts .ddi/.img/.msx/multidisk disk images to .dsk image |
| 3 | | * |
| 4 | | * Sean Young 2001 |
| 5 | | */ |
| 6 | | |
| 7 | | /* |
| 8 | | * .msx files are a certain type of disk dumps. They always seem to be |
| 9 | | * double sided 720kB ones. There are no extra headers, but the order of |
| 10 | | * the sectors is all messed up. This little program should put that |
| 11 | | * straight. :) |
| 12 | | * |
| 13 | | * The structure is as follows: the first 9 sectors (=0x1200 bytes) of the |
| 14 | | * first side, then the first 9 sectors of the other side, the next 9 |
| 15 | | * sectors of the first site, the next 9 sectors of the other side.. etc. |
| 16 | | * |
| 17 | | * These weird .msx files are found at: ftp://jazz.snu.ac.kr/pub/msx/ |
| 18 | | */ |
| 19 | | |
| 20 | | /* |
| 21 | | * .ddi DiskDupe images are always double sided 720kB images (at least in the |
| 22 | | * MSX world). There is a header of 0x1200 bytes (in the version known to the |
| 23 | | * MSX world; this is version is 5.12 -- and probably others). This format |
| 24 | | * is used by the CompuJunks MSX2 emulator (MSX2EMUL). |
| 25 | | * |
| 26 | | * The exact format of the header is unknown to me. However, this does not |
| 27 | | * seem to be important; the last 720kB is a complete continuous dump of |
| 28 | | * the disk. |
| 29 | | */ |
| 30 | | |
| 31 | | /* |
| 32 | | * .img files have the following format: first a one byte header, which |
| 33 | | * is 1 (01h) for singled sided 360kB images, and 2 (02h) for double sided |
| 34 | | * 720kB images. These files can be found at ftp://ftp.funet.fi/pub/msx/ |
| 35 | | */ |
| 36 | | |
| 37 | | /* |
| 38 | | * .dsk multidisks are used by fmsx-dos 1.6. These files are always double |
| 39 | | * sided 3.5" disks (720kB), simply appended to one another. |
| 40 | | */ |
| 41 | | |
| 42 | | #include "imgtoolx.h" |
| 43 | | |
| 44 | | |
| 45 | | typedef struct |
| 46 | | { |
| 47 | | imgtool_image base; |
| 48 | | char *file_name; |
| 49 | | imgtool_stream *file_handle; |
| 50 | | int size, format, disks; |
| 51 | | } DSK_IMAGE; |
| 52 | | |
| 53 | | typedef struct |
| 54 | | { |
| 55 | | imgtool_directory base; |
| 56 | | DSK_IMAGE *image; |
| 57 | | int index; |
| 58 | | } DSK_ITERATOR; |
| 59 | | |
| 60 | | static int msx_dsk_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 61 | | static void msx_dsk_image_exit(imgtool_image *img); |
| 62 | | static int msx_dsk_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 63 | | static int msx_dsk_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 64 | | static void msx_dsk_image_closeenum(imgtool_directory *enumeration); |
| 65 | | static int msx_dsk_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 66 | | |
| 67 | | IMAGEMODULE( |
| 68 | | msx_img, |
| 69 | | "MSX .img diskimage", /* human readable name */ |
| 70 | | "img", /* file extension */ |
| 71 | | NULL, /* crcfile */ |
| 72 | | NULL, /* crc system name */ |
| 73 | | NULL, /* eoln */ |
| 74 | | 0, /* flags */ |
| 75 | | msx_dsk_image_init, /* init function */ |
| 76 | | msx_dsk_image_exit, /* exit function */ |
| 77 | | NULL, /* info function */ |
| 78 | | msx_dsk_image_beginenum, /* begin enumeration */ |
| 79 | | msx_dsk_image_nextenum, /* enumerate next */ |
| 80 | | msx_dsk_image_closeenum, /* close enumeration */ |
| 81 | | NULL, /* free space on image */ |
| 82 | | msx_dsk_image_readfile, /* read file */ |
| 83 | | NULL, /* write file */ |
| 84 | | NULL, /* delete file */ |
| 85 | | NULL, /* create image */ |
| 86 | | NULL, /* read sector */ |
| 87 | | NULL, /* write sector */ |
| 88 | | NULL, /* file options */ |
| 89 | | NULL /* create options */ |
| 90 | | ) |
| 91 | | |
| 92 | | IMAGEMODULE( |
| 93 | | msx_ddi, |
| 94 | | "MSX DiskDupe .ddi diskimage", /* human readable name */ |
| 95 | | "ddi", /* file extension */ |
| 96 | | NULL, /* crcfile */ |
| 97 | | NULL, /* crc system name */ |
| 98 | | NULL, /* eoln */ |
| 99 | | 0, /* flags */ |
| 100 | | msx_dsk_image_init, /* init function */ |
| 101 | | msx_dsk_image_exit, /* exit function */ |
| 102 | | NULL, /* info function */ |
| 103 | | msx_dsk_image_beginenum, /* begin enumeration */ |
| 104 | | msx_dsk_image_nextenum, /* enumerate next */ |
| 105 | | msx_dsk_image_closeenum, /* close enumeration */ |
| 106 | | NULL, /* free space on image */ |
| 107 | | msx_dsk_image_readfile, /* read file */ |
| 108 | | NULL, /* write file */ |
| 109 | | NULL, /* delete file */ |
| 110 | | NULL, /* create image */ |
| 111 | | NULL, /* read sector */ |
| 112 | | NULL, /* write sector */ |
| 113 | | NULL, /* file options */ |
| 114 | | NULL /* create options */ |
| 115 | | ) |
| 116 | | |
| 117 | | IMAGEMODULE( |
| 118 | | msx_msx, |
| 119 | | "MSX .msx diskimage", /* human readable name */ |
| 120 | | "ddi", /* file extension */ |
| 121 | | NULL, /* crcfile */ |
| 122 | | NULL, /* crc system name */ |
| 123 | | NULL, /* eoln */ |
| 124 | | 0, /* flags */ |
| 125 | | msx_dsk_image_init, /* init function */ |
| 126 | | msx_dsk_image_exit, /* exit function */ |
| 127 | | NULL, /* info function */ |
| 128 | | msx_dsk_image_beginenum, /* begin enumeration */ |
| 129 | | msx_dsk_image_nextenum, /* enumerate next */ |
| 130 | | msx_dsk_image_closeenum, /* close enumeration */ |
| 131 | | NULL, /* free space on image */ |
| 132 | | msx_dsk_image_readfile, /* read file */ |
| 133 | | NULL, /* write file */ |
| 134 | | NULL, /* delete file */ |
| 135 | | NULL, /* create image */ |
| 136 | | NULL, /* read sector */ |
| 137 | | NULL, /* write sector */ |
| 138 | | NULL, /* file options */ |
| 139 | | NULL /* create options */ |
| 140 | | ) |
| 141 | | |
| 142 | | IMAGEMODULE( |
| 143 | | msx_mul, |
| 144 | | "MSX multi disk image", /* human readable name */ |
| 145 | | "dsk", /* file extension */ |
| 146 | | NULL, /* crcfile */ |
| 147 | | NULL, /* crc system name */ |
| 148 | | NULL, /* eoln */ |
| 149 | | 0, /* flags */ |
| 150 | | msx_dsk_image_init, /* init function */ |
| 151 | | msx_dsk_image_exit, /* exit function */ |
| 152 | | NULL, /* info function */ |
| 153 | | msx_dsk_image_beginenum, /* begin enumeration */ |
| 154 | | msx_dsk_image_nextenum, /* enumerate next */ |
| 155 | | msx_dsk_image_closeenum, /* close enumeration */ |
| 156 | | NULL, /* free space on image */ |
| 157 | | msx_dsk_image_readfile, /* read file */ |
| 158 | | NULL, /* write file */ |
| 159 | | NULL, /* delete file */ |
| 160 | | NULL, /* create image */ |
| 161 | | NULL, /* read sector */ |
| 162 | | NULL, /* write sector */ |
| 163 | | NULL, /* file options */ |
| 164 | | NULL /* create options */ |
| 165 | | ) |
| 166 | | |
| 167 | | #define FORMAT_IMG (0) |
| 168 | | #define FORMAT_DDI (1) |
| 169 | | #define FORMAT_MSX (2) |
| 170 | | #define FORMAT_MULTI (3) |
| 171 | | |
| 172 | | static int msx_dsk_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 173 | | { |
| 174 | | DSK_IMAGE *image; |
| 175 | | int size, disks, correct, format; |
| 176 | | UINT8 header; |
| 177 | | |
| 178 | | size = stream_size (f); |
| 179 | | if (size < (360*1024) ) |
| 180 | | return IMGTOOLERR_MODULENOTFOUND; |
| 181 | | |
| 182 | | disks = 1; |
| 183 | | |
| 184 | | correct = 0; |
| 185 | | |
| 186 | | if (!strcmp(mod->name, "msx_img")) |
| 187 | | { |
| 188 | | format = FORMAT_IMG; |
| 189 | | if (1 != stream_read (f, &header, 1) ) |
| 190 | | return IMGTOOLERR_READERROR; |
| 191 | | |
| 192 | | if ( (size == (720*1024+1) ) && (header == 2) ) |
| 193 | | { |
| 194 | | correct = 1; |
| 195 | | size--; |
| 196 | | } |
| 197 | | if ( (size == (360*1024+1) ) && (header == 1) ) |
| 198 | | { |
| 199 | | correct = 1; |
| 200 | | size--; |
| 201 | | } |
| 202 | | } |
| 203 | | else if (!strcmp(mod->name, "msx_msx")) |
| 204 | | { |
| 205 | | format = FORMAT_MSX; |
| 206 | | if (size == (720*1024) ) |
| 207 | | correct = 1; |
| 208 | | } |
| 209 | | else if (!strcmp(mod->name, "msx_ddi")) |
| 210 | | { |
| 211 | | format = FORMAT_DDI; |
| 212 | | if (size == (720*1024+0x1200) ) |
| 213 | | { |
| 214 | | size -= 0x1200; |
| 215 | | correct = 1; |
| 216 | | } |
| 217 | | } |
| 218 | | else if (!strcmp(mod->name, "msx_mul")) |
| 219 | | { |
| 220 | | format = FORMAT_MULTI; |
| 221 | | if ( (size > 720*1024) && !(size % (720*1024) ) ) |
| 222 | | { |
| 223 | | disks = size / (720*1024); |
| 224 | | correct = 1; |
| 225 | | size = 720*1024; |
| 226 | | } |
| 227 | | } |
| 228 | | else |
| 229 | | { |
| 230 | | assert(0); |
| 231 | | return IMGTOOLERR_UNEXPECTED; |
| 232 | | } |
| 233 | | |
| 234 | | if (!correct) return IMGTOOLERR_MODULENOTFOUND; |
| 235 | | |
| 236 | | image = (DSK_IMAGE*)malloc (sizeof (DSK_IMAGE) ); |
| 237 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 238 | | |
| 239 | | *outimg = (imgtool_image*)image; |
| 240 | | |
| 241 | | memset(image, 0, sizeof(DSK_IMAGE)); |
| 242 | | image->base.module = mod; |
| 243 | | image->file_handle = f; |
| 244 | | image->size = size; |
| 245 | | image->format = format; |
| 246 | | image->file_name = NULL; |
| 247 | | image->disks = disks; |
| 248 | | |
| 249 | | return 0; |
| 250 | | } |
| 251 | | |
| 252 | | static void msx_dsk_image_exit(imgtool_image *img) |
| 253 | | { |
| 254 | | DSK_IMAGE *image=(DSK_IMAGE*)img; |
| 255 | | stream_close(image->file_handle); |
| 256 | | if (image->file_name) free(image->file_name); |
| 257 | | free(image); |
| 258 | | } |
| 259 | | |
| 260 | | static int msx_dsk_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 261 | | { |
| 262 | | DSK_IMAGE *image=(DSK_IMAGE*)img; |
| 263 | | DSK_ITERATOR *iter; |
| 264 | | |
| 265 | | iter=*(DSK_ITERATOR**)outenum = (DSK_ITERATOR*) malloc(sizeof(DSK_ITERATOR)); |
| 266 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 267 | | |
| 268 | | iter->base.module = image->base.module; |
| 269 | | iter->image=image; |
| 270 | | iter->index = 1; |
| 271 | | |
| 272 | | return 0; |
| 273 | | } |
| 274 | | |
| 275 | | static int msx_dsk_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 276 | | { |
| 277 | | DSK_ITERATOR *iter=(DSK_ITERATOR*)enumeration; |
| 278 | | |
| 279 | | ent->eof = (iter->index > iter->image->disks); |
| 280 | | if (!ent->eof) |
| 281 | | { |
| 282 | | if (iter->image->file_name) |
| 283 | | strcpy (ent->fname, iter->image->file_name); |
| 284 | | else |
| 285 | | { |
| 286 | | if (iter->image->disks == 1) |
| 287 | | strcpy (ent->fname, "msx.dsk"); |
| 288 | | else |
| 289 | | sprintf (ent->fname, "msx-%d.dsk", iter->index); |
| 290 | | } |
| 291 | | |
| 292 | | ent->corrupt=0; |
| 293 | | ent->filesize = iter->image->size; |
| 294 | | |
| 295 | | iter->index++; |
| 296 | | } |
| 297 | | |
| 298 | | return 0; |
| 299 | | } |
| 300 | | |
| 301 | | static void msx_dsk_image_closeenum(imgtool_directory *enumeration) |
| 302 | | { |
| 303 | | free(enumeration); |
| 304 | | } |
| 305 | | |
| 306 | | static int msx_dsk_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 307 | | { |
| 308 | | DSK_IMAGE *image=(DSK_IMAGE*)img; |
| 309 | | UINT8 buf[0x1200]; |
| 310 | | int i, offset, n1, n2, disks = 0; |
| 311 | | |
| 312 | | /* check file name */ |
| 313 | | switch (image->format) |
| 314 | | { |
| 315 | | case FORMAT_IMG: |
| 316 | | case FORMAT_DDI: |
| 317 | | case FORMAT_MSX: |
| 318 | | if (strcmpi (fname, "msx.dsk") ) |
| 319 | | return IMGTOOLERR_MODULENOTFOUND; |
| 320 | | break; |
| 321 | | case FORMAT_MULTI: |
| 322 | | if (strncmpi (fname, "msx-", 4) ) |
| 323 | | return IMGTOOLERR_MODULENOTFOUND; |
| 324 | | offset = 4; disks = 0; |
| 325 | | while ( (fname[offset] >= '0') || (fname[offset] <= '9') ) |
| 326 | | disks = disks * 10 + (fname[offset++] - '0'); |
| 327 | | |
| 328 | | if (core_stricmp (fname + offset, ".dsk") ) |
| 329 | | return IMGTOOLERR_MODULENOTFOUND; |
| 330 | | |
| 331 | | if ( (disks < 1) || (disks > image->disks) ) |
| 332 | | return IMGTOOLERR_MODULENOTFOUND; |
| 333 | | |
| 334 | | break; |
| 335 | | } |
| 336 | | |
| 337 | | /* copy the file */ |
| 338 | | switch (image->format) |
| 339 | | { |
| 340 | | case FORMAT_MSX: |
| 341 | | i = 80; n1 = 0; n2 = 80; |
| 342 | | while (i--) |
| 343 | | { |
| 344 | | stream_seek (image->file_handle, n1++ * 0x1200, SEEK_SET); |
| 345 | | if (0x1200 != stream_read (image->file_handle, buf, 0x1200) ) |
| 346 | | return IMGTOOLERR_READERROR; |
| 347 | | |
| 348 | | if (0x1200 != stream_write (destf, buf, 0x1200) ) |
| 349 | | return IMGTOOLERR_WRITEERROR; |
| 350 | | |
| 351 | | stream_seek (image->file_handle, n2++ * 0x1200, SEEK_SET); |
| 352 | | if (0x1200 != stream_read (image->file_handle, buf, 0x1200) ) |
| 353 | | return IMGTOOLERR_READERROR; |
| 354 | | |
| 355 | | if (0x1200 != stream_write (destf, buf, 0x1200) ) |
| 356 | | return IMGTOOLERR_WRITEERROR; |
| 357 | | } |
| 358 | | |
| 359 | | return 0; |
| 360 | | case FORMAT_IMG: |
| 361 | | offset = 1; i = (image->size / 0x1200); |
| 362 | | break; |
| 363 | | case FORMAT_DDI: |
| 364 | | offset = 0x1200; i = 160; |
| 365 | | break; |
| 366 | | case FORMAT_MULTI: /* multi disk */ |
| 367 | | i = 160; offset = 720*1024 * (disks - 1); |
| 368 | | break; |
| 369 | | default: |
| 370 | | return IMGTOOLERR_UNEXPECTED; |
| 371 | | } |
| 372 | | |
| 373 | | stream_seek (image->file_handle, offset, SEEK_SET); |
| 374 | | |
| 375 | | while (i--) |
| 376 | | { |
| 377 | | if (0x1200 != stream_read (image->file_handle, buf, 0x1200) ) |
| 378 | | return IMGTOOLERR_READERROR; |
| 379 | | |
| 380 | | if (0x1200 != stream_write (destf, buf, 0x1200) ) |
| 381 | | return IMGTOOLERR_WRITEERROR; |
| 382 | | } |
| 383 | | |
| 384 | | return 0; |
| 385 | | } |
| 386 | | |
src/mess/tools/imgtool/modules/t64.c
| r13274 | r13275 | |
| 1 | | #include <stdio.h> |
| 2 | | #include <string.h> |
| 3 | | #include <stdlib.h> |
| 4 | | #include "imgtoolx.h" |
| 5 | | |
| 6 | | #ifdef LSB_FIRST |
| 7 | | typedef UINT16 littleuword; |
| 8 | | typedef UINT32 littleulong; |
| 9 | | #define GET_UWORD(a) a |
| 10 | | #define GET_ULONG(a) a |
| 11 | | #define SET_UWORD(a,v) a=v |
| 12 | | #define SET_ULONG(a,v) a=v |
| 13 | | #else |
| 14 | | typedef struct { |
| 15 | | unsigned char low, high; |
| 16 | | } littleuword; |
| 17 | | typedef struct { |
| 18 | | unsigned char low, mid, high, highest; |
| 19 | | } littleulong; |
| 20 | | #define GET_UWORD(a) (a.low|(a.high<<8)) |
| 21 | | #define GET_ULONG(a) (a.low|(a.mid<<8)|(a.high<<16)|(a.highest<<24)) |
| 22 | | #define SET_UWORD(a,v) a.low=(v)&0xff;a.high=((v)>>8)&0xff |
| 23 | | #define SET_ULONG(a,v) a.low=(v)&0xff;a.mid=((v)>>8)&0xff;a.high=((v)>>16)&0xff;a.highest=((v)>>24)&0xff |
| 24 | | #endif |
| 25 | | |
| 26 | | /* |
| 27 | | T64 File Structure was developed by Miha Peternel for use in C64S. |
| 28 | | It is easy to use and allows future extensions. |
| 29 | | |
| 30 | | ______________________ |
| 31 | | T64 file structure |
| 32 | | |
| 33 | | Offset, size, description |
| 34 | | 0, 64 tape record |
| 35 | | 64, 32*n file records for n directory entries |
| 36 | | 64+32*n, ??? binary contents of the files |
| 37 | | |
| 38 | | _______________ |
| 39 | | Tape Record |
| 40 | | |
| 41 | | Offset, size, description |
| 42 | | 0, 32 DOS tape description + EOF (for type) |
| 43 | | 32, 2 tape version ($0101) |
| 44 | | 34, 2 number of directory entries |
| 45 | | 36, 2 number of used entries (can be 0 in my loader) |
| 46 | | 38, 2 free |
| 47 | | 40, 24 user description as displayed in tape menu |
| 48 | | |
| 49 | | _______________ |
| 50 | | File Record |
| 51 | | |
| 52 | | Offset, size, description |
| 53 | | 0, 1 entry type |
| 54 | | 0 = free entry |
| 55 | | 1 = normal tape file |
| 56 | | 3 = memory snapshot v0.9, uncompressed |
| 57 | | 2..255 reserved (for memory snapshots...) |
| 58 | | 1, 1 C64 file type |
| 59 | | 2, 2 start address |
| 60 | | 4, 2 end address |
| 61 | | 6, 2 free |
| 62 | | 8, 4 offset of file contents start within T64 file |
| 63 | | 12, 4 free |
| 64 | | 16, 16 C64 file name |
| 65 | | |
| 66 | | */ |
| 67 | | |
| 68 | | typedef struct{ |
| 69 | | char dostext[0x20]; // dos text |
| 70 | | littleuword version; // packed bcd |
| 71 | | littleuword max_entries; |
| 72 | | littleuword used_entries; |
| 73 | | littleuword reserved; |
| 74 | | char description[24]; |
| 75 | | } t64_header; |
| 76 | | |
| 77 | | typedef struct { |
| 78 | | unsigned char type; |
| 79 | | unsigned char file_type; |
| 80 | | littleuword start_address; |
| 81 | | littleuword end_address; |
| 82 | | littleuword reserved; |
| 83 | | littleulong offset; |
| 84 | | littleulong reserved2; |
| 85 | | char name[0x10]; |
| 86 | | } t64_entry; |
| 87 | | |
| 88 | | typedef struct { |
| 89 | | imgtool_image base; |
| 90 | | imgtool_stream *file_handle; |
| 91 | | int size; |
| 92 | | int modified; |
| 93 | | unsigned char *data; |
| 94 | | } t64_image; |
| 95 | | |
| 96 | | #define HEADER(image) ((t64_header*)image->data) |
| 97 | | #define ENTRY(image, index) ((t64_entry*)(image->data+sizeof(t64_header))+index) |
| 98 | | |
| 99 | | typedef struct { |
| 100 | | imgtool_directory base; |
| 101 | | t64_image *image; |
| 102 | | int index; |
| 103 | | } t64_iterator; |
| 104 | | |
| 105 | | static int t64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 106 | | static void t64_image_exit(imgtool_image *img); |
| 107 | | static void t64_image_info(imgtool_image *img, char *string, const int len); |
| 108 | | static int t64_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 109 | | static int t64_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 110 | | static void t64_image_closeenum(imgtool_directory *enumeration); |
| 111 | | //static size_t t64_image_freespace(imgtool_image *img); |
| 112 | | static int t64_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 113 | | static int t64_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *_options); |
| 114 | | static int t64_image_deletefile(imgtool_image *img, const char *fname); |
| 115 | | static int t64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *_options); |
| 116 | | |
| 117 | | static const struct OptionTemplate t64_createopts[] = |
| 118 | | { |
| 119 | | { "entries", NULL, IMGOPTION_FLAG_TYPE_INTEGER, 0, 3, NULL }, /* [0] */ |
| 120 | | { "label", NULL, IMGOPTION_FLAG_TYPE_STRING | IMGOPTION_FLAG_HASDEFAULT, 0, 0, NULL }, /* [1] */ |
| 121 | | { NULL, NULL, 0, 0, 0, 0 } |
| 122 | | }; |
| 123 | | |
| 124 | | #define T64_OPTION_ENTRIES 0 |
| 125 | | #define T64_OPTION_LABEL 1 |
| 126 | | |
| 127 | | IMAGEMODULE( |
| 128 | | t64, |
| 129 | | "Commodore 64 Archive for Tapes", /* human readable name */ |
| 130 | | "t64", /* file extension */ |
| 131 | | NULL, /* crcfile */ |
| 132 | | NULL, /* crc system name */ |
| 133 | | EOLN_CR, /* eoln */ |
| 134 | | 0, /* flags */ |
| 135 | | t64_image_init, /* init function */ |
| 136 | | t64_image_exit, /* exit function */ |
| 137 | | t64_image_info, /* info function */ |
| 138 | | t64_image_beginenum, /* begin enumeration */ |
| 139 | | t64_image_nextenum, /* enumerate next */ |
| 140 | | t64_image_closeenum, /* close enumeration */ |
| 141 | | NULL, //t64_image_freespace, /* free space on image */ |
| 142 | | t64_image_readfile, /* read file */ |
| 143 | | t64_image_writefile, /* write file */ |
| 144 | | t64_image_deletefile, /* delete file */ |
| 145 | | t64_image_create, /* create image */ |
| 146 | | NULL, /* read sector */ |
| 147 | | NULL, /* write sector */ |
| 148 | | NULL, /* file _options */ |
| 149 | | t64_createopts /* create options */ |
| 150 | | ) |
| 151 | | |
| 152 | | static int t64_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 153 | | { |
| 154 | | t64_image *image; |
| 155 | | |
| 156 | | image=*(t64_image**)outimg=(t64_image *) malloc(sizeof(t64_image)); |
| 157 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 158 | | |
| 159 | | memset(image, 0, sizeof(t64_image)); |
| 160 | | image->base.module = mod; |
| 161 | | image->size=stream_size(f); |
| 162 | | image->file_handle=f; |
| 163 | | |
| 164 | | image->data = (unsigned char *) malloc(image->size); |
| 165 | | if ( (!image->data) |
| 166 | | ||(stream_read(f, image->data, image->size)!=image->size) ) { |
| 167 | | free(image); |
| 168 | | *outimg=NULL; |
| 169 | | return IMGTOOLERR_OUTOFMEMORY; |
| 170 | | } |
| 171 | | |
| 172 | | return 0; |
| 173 | | } |
| 174 | | |
| 175 | | static void t64_image_exit(imgtool_image *img) |
| 176 | | { |
| 177 | | t64_image *image=(t64_image*)img; |
| 178 | | if (image->modified) { |
| 179 | | stream_clear(image->file_handle); |
| 180 | | stream_write(image->file_handle, image->data, image->size); |
| 181 | | } |
| 182 | | stream_close(image->file_handle); |
| 183 | | free(image->data); |
| 184 | | free(image); |
| 185 | | } |
| 186 | | |
| 187 | | static void t64_image_info(imgtool_image *img, char *string, const int len) |
| 188 | | { |
| 189 | | t64_image *image=(t64_image*)img; |
| 190 | | char dostext_with_null[33]= { 0 }; |
| 191 | | char name_with_null[25]={ 0 }; |
| 192 | | strncpy(dostext_with_null, HEADER(image)->dostext, 32); |
| 193 | | strncpy(name_with_null, HEADER(image)->description, 24); |
| 194 | | sprintf(string,"%s\n%s\nversion:%.4x max entries:%d", |
| 195 | | dostext_with_null, |
| 196 | | name_with_null, |
| 197 | | GET_UWORD(HEADER(image)->version), |
| 198 | | HEADER(image)->max_entries); |
| 199 | | } |
| 200 | | |
| 201 | | static int t64_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 202 | | { |
| 203 | | t64_image *image=(t64_image*)img; |
| 204 | | t64_iterator *iter; |
| 205 | | |
| 206 | | iter=*(t64_iterator**)outenum = (t64_iterator *) malloc(sizeof(t64_iterator)); |
| 207 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 208 | | |
| 209 | | iter->base.module = img->module; |
| 210 | | |
| 211 | | iter->image=image; |
| 212 | | iter->index = 0; |
| 213 | | return 0; |
| 214 | | } |
| 215 | | |
| 216 | | static int t64_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 217 | | { |
| 218 | | t64_iterator *iter=(t64_iterator*)enumeration; |
| 219 | | ent->corrupt=0; |
| 220 | | |
| 221 | | for (;!(ent->eof=(iter->index>=GET_UWORD(HEADER(iter->image)->max_entries)));iter->index++ ){ |
| 222 | | if (ENTRY(iter->image, iter->index)->type==0) continue; |
| 223 | | memset(ent->fname,0,17); |
| 224 | | strncpy(ent->fname, ENTRY(iter->image, iter->index)->name, 16); |
| 225 | | if (ent->attr) |
| 226 | | sprintf(ent->attr,"start:%.4x end:%.4x type:%d file:%d", |
| 227 | | GET_UWORD( ENTRY(iter->image,iter->index)->start_address), |
| 228 | | GET_UWORD( ENTRY(iter->image,iter->index)->end_address), |
| 229 | | ENTRY(iter->image,iter->index)->type, |
| 230 | | ENTRY(iter->image,iter->index)->file_type ); |
| 231 | | ent->filesize=GET_UWORD( ENTRY(iter->image, iter->index)->end_address ) |
| 232 | | -GET_UWORD( ENTRY(iter->image, iter->index)->start_address ); |
| 233 | | iter->index++; |
| 234 | | break; |
| 235 | | } |
| 236 | | return 0; |
| 237 | | } |
| 238 | | |
| 239 | | static void t64_image_closeenum(imgtool_directory *enumeration) |
| 240 | | { |
| 241 | | free(enumeration); |
| 242 | | } |
| 243 | | |
| 244 | | #if 0 |
| 245 | | static size_t t64_image_freespace(imgtool_image *img) |
| 246 | | { |
| 247 | | int i; |
| 248 | | rsdos_diskimage *rsimg = (rsdos_diskimage *) img; |
| 249 | | size_t s = 0; |
| 250 | | |
| 251 | | for (i = 0; i < GRANULE_COUNT; i++) |
| 252 | | if (rsimg->granulemap[i] == 0xff) |
| 253 | | s += (9 * 256); |
| 254 | | return s; |
| 255 | | } |
| 256 | | #endif |
| 257 | | |
| 258 | | static int t64_image_findfile(t64_image *image, const char *fname) |
| 259 | | { |
| 260 | | int i=0; |
| 261 | | |
| 262 | | for (i=0; i<GET_UWORD(HEADER(image)->max_entries); i++) { |
| 263 | | if (ENTRY(image, i)->type==0) continue; |
| 264 | | if (!strnicmp(fname, ENTRY(image,i)->name, strlen(fname)) ) return i; |
| 265 | | } |
| 266 | | return -1; |
| 267 | | } |
| 268 | | |
| 269 | | static int t64_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 270 | | { |
| 271 | | t64_image *image=(t64_image*)img; |
| 272 | | int size; |
| 273 | | int ind; |
| 274 | | |
| 275 | | if ((ind=t64_image_findfile(image, fname))==-1 ) return IMGTOOLERR_MODULENOTFOUND; |
| 276 | | |
| 277 | | size=GET_UWORD( ENTRY(image, ind)->end_address )-GET_UWORD( ENTRY(image, ind)->start_address ); |
| 278 | | |
| 279 | | if (stream_write(destf, &ENTRY(image, ind)->start_address, 2)!=2) { |
| 280 | | return IMGTOOLERR_WRITEERROR; |
| 281 | | } |
| 282 | | if (stream_write(destf, image->data+GET_ULONG(ENTRY(image, ind)->offset), size)!=size) { |
| 283 | | return IMGTOOLERR_WRITEERROR; |
| 284 | | } |
| 285 | | |
| 286 | | return 0; |
| 287 | | } |
| 288 | | |
| 289 | | static int t64_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *_options) |
| 290 | | { |
| 291 | | t64_image *image=(t64_image*)img; |
| 292 | | int size, fsize; |
| 293 | | int ind; |
| 294 | | int pos, i, t; |
| 295 | | |
| 296 | | fsize=stream_size(sourcef); |
| 297 | | if ((ind=t64_image_findfile(image, fname))==-1 ) { |
| 298 | | // appending |
| 299 | | for (ind=0; ind<GET_UWORD(HEADER(image)->max_entries)&&(ENTRY(image,ind)->type!=0); ind++) ; |
| 300 | | if (ind>=GET_UWORD(HEADER(image)->max_entries)) return IMGTOOLERR_NOSPACE; |
| 301 | | pos=image->size; |
| 302 | | if (!(image->data=realloc(image->data, image->size+fsize-2)) ) |
| 303 | | return IMGTOOLERR_OUTOFMEMORY; |
| 304 | | image->size+=fsize-2; |
| 305 | | #ifdef LSB_FIRST |
| 306 | | HEADER(image)->used_entries++; |
| 307 | | #else |
| 308 | | if (++HEADER(image)->used_entries.low == 0) |
| 309 | | HEADER(image)->used_entries.high++; |
| 310 | | #endif |
| 311 | | } else { |
| 312 | | pos=GET_ULONG(ENTRY(image,ind)->offset); |
| 313 | | // find the size of the data in this area |
| 314 | | for (size=image->size-pos, i=0; i<GET_UWORD(HEADER(image)->max_entries); i++) { |
| 315 | | if (ENTRY(image,i)->type==0) continue; |
| 316 | | if (GET_UWORD(ENTRY(image,i)->offset)<pos) continue; |
| 317 | | if (GET_ULONG(ENTRY(image,i)->offset)-pos>=size) continue; |
| 318 | | size=GET_ULONG(ENTRY(image, i)->offset)-pos; |
| 319 | | } |
| 320 | | if ((size!=0)&&(image->size-pos-size!=0)) |
| 321 | | memmove(image->data+pos, image->data+pos+size, image->size-pos-size); |
| 322 | | // correct offset positions in other entries |
| 323 | | for (i=0;i<GET_UWORD(HEADER(image)->max_entries); i++) { |
| 324 | | if (i==ind) continue; |
| 325 | | if (ENTRY(image,i)->type==0) continue; |
| 326 | | if (GET_ULONG(ENTRY(image,i)->offset)<pos) continue; |
| 327 | | t=GET_ULONG(ENTRY(image,i)->offset)-size; |
| 328 | | SET_ULONG( ENTRY(image,i)->offset, t); |
| 329 | | } |
| 330 | | // overwriting |
| 331 | | if (!(image->data=realloc(image->data, image->size+fsize-2-size))) |
| 332 | | return IMGTOOLERR_OUTOFMEMORY; |
| 333 | | pos=image->size; |
| 334 | | image->size=image->size+fsize-2-size; |
| 335 | | } |
| 336 | | memset(ENTRY(image,ind), 0, sizeof(t64_entry)); |
| 337 | | if (stream_read(sourcef, &ENTRY(image,ind)->start_address, 2)!=2) { |
| 338 | | return IMGTOOLERR_READERROR; |
| 339 | | } |
| 340 | | if (stream_read(sourcef, image->data+pos, fsize-2)!=fsize-2) { |
| 341 | | return IMGTOOLERR_READERROR; |
| 342 | | } |
| 343 | | SET_UWORD( ENTRY(image, ind)->end_address, |
| 344 | | GET_UWORD( ENTRY(image, ind)->start_address)+fsize-2); |
| 345 | | SET_ULONG( ENTRY(image, ind)->offset, pos); |
| 346 | | strncpy(ENTRY(image, ind)->name, fname, 16); |
| 347 | | ENTRY(image,ind)->type=1; // normal file |
| 348 | | ENTRY(image,ind)->file_type=1; // prg |
| 349 | | image->modified=1; |
| 350 | | |
| 351 | | return 0; |
| 352 | | } |
| 353 | | |
| 354 | | static int t64_image_deletefile(imgtool_image *img, const char *fname) |
| 355 | | { |
| 356 | | t64_image *image=(t64_image*)img; |
| 357 | | int pos, size; |
| 358 | | int ind, i; |
| 359 | | |
| 360 | | if ((ind=t64_image_findfile(image, fname))==-1 ) { |
| 361 | | return IMGTOOLERR_MODULENOTFOUND; |
| 362 | | } |
| 363 | | pos=GET_ULONG(ENTRY(image,ind)->offset); |
| 364 | | // find the size of the data in this area |
| 365 | | for (size=image->size-pos, i=0; i<GET_UWORD(HEADER(image)->max_entries); i++) { |
| 366 | | if (ENTRY(image,i)->type==0) continue; |
| 367 | | if (GET_UWORD(ENTRY(image,i)->offset)<pos) continue; |
| 368 | | if (GET_ULONG(ENTRY(image,i)->offset)-pos>=size) continue; |
| 369 | | size=GET_ULONG(ENTRY(image, i)->offset)-pos; |
| 370 | | } |
| 371 | | if ((size!=0)&&(image->size-pos-size!=0)) |
| 372 | | memmove(image->data+pos, image->data+pos+size, image->size-pos-size); |
| 373 | | // correct offset positions in other entries |
| 374 | | for (i=0;i<GET_UWORD(HEADER(image)->max_entries); i++) { |
| 375 | | if (i==ind) continue; |
| 376 | | if (ENTRY(image,i)->type==0) continue; |
| 377 | | if (GET_ULONG(ENTRY(image,i)->offset)<pos) continue; |
| 378 | | SET_ULONG( ENTRY(image,i)->offset, GET_ULONG(ENTRY(image,i)->offset)-size ); |
| 379 | | } |
| 380 | | image->size-=size; |
| 381 | | ENTRY(image,ind)->type=0; // normal file |
| 382 | | image->modified=1; |
| 383 | | #ifdef LSB_FIRST |
| 384 | | HEADER(image)->used_entries--; |
| 385 | | #else |
| 386 | | if (HEADER(image)->used_entries.low-- == 0) |
| 387 | | HEADER(image)->used_entries.high--; |
| 388 | | #endif |
| 389 | | |
| 390 | | return 0; |
| 391 | | } |
| 392 | | |
| 393 | | static int t64_image_create(const imgtool_module *mod, imgtool_stream *f, const ResolvedOption *_options) |
| 394 | | { |
| 395 | | int entries; |
| 396 | | t64_header header={ "T64 Tape archiv created by MESS\x1a" }; |
| 397 | | t64_entry entry= { 0 }; |
| 398 | | int i; |
| 399 | | |
| 400 | | entries = _options[T64_OPTION_ENTRIES].i; |
| 401 | | if (entries==0) entries=10; |
| 402 | | SET_UWORD(header.version, 0x0101); |
| 403 | | SET_UWORD(header.max_entries, entries); |
| 404 | | if (_options[T64_OPTION_LABEL].s) strcpy(header.description, _options[T64_OPTION_LABEL].s); |
| 405 | | if (stream_write(f, &header, sizeof(t64_header)) != sizeof(t64_header)) |
| 406 | | return IMGTOOLERR_WRITEERROR; |
| 407 | | for (i=0; i<entries; i++) { |
| 408 | | if (stream_write(f, &entry, sizeof(t64_entry)) != sizeof(t64_entry)) |
| 409 | | return IMGTOOLERR_WRITEERROR; |
| 410 | | } |
| 411 | | |
| 412 | | return 0; |
| 413 | | } |
| 414 | | |
src/mess/tools/imgtool/modules/xsa.c
| r13274 | r13275 | |
| 1 | | /* |
| 2 | | * xsa.c : decompresses XelaSoft Archives. |
| 3 | | * |
| 4 | | * Sean Young |
| 5 | | */ |
| 6 | | |
| 7 | | #include "imgtoolx.h" |
| 8 | | |
| 9 | | |
| 10 | | typedef struct |
| 11 | | { |
| 12 | | imgtool_image base; |
| 13 | | char *file_name; |
| 14 | | imgtool_stream *file_handle; |
| 15 | | int size; |
| 16 | | } XSA_IMAGE; |
| 17 | | |
| 18 | | typedef struct |
| 19 | | { |
| 20 | | imgtool_directory base; |
| 21 | | XSA_IMAGE *image; |
| 22 | | int index; |
| 23 | | } XSA_ITERATOR; |
| 24 | | |
| 25 | | static int xsa_extract (imgtool_stream *in, imgtool_stream *out); |
| 26 | | static int xsa_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg); |
| 27 | | static void xsa_image_exit(imgtool_image *img); |
| 28 | | static int xsa_image_beginenum(imgtool_image *img, imgtool_directory **outenum); |
| 29 | | static int xsa_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent); |
| 30 | | static void xsa_image_closeenum(imgtool_directory *enumeration); |
| 31 | | static int xsa_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf); |
| 32 | | |
| 33 | | IMAGEMODULE( |
| 34 | | xsa, |
| 35 | | "XelaSoft Archive", /* human readable name */ |
| 36 | | "xsa", /* file extension */ |
| 37 | | NULL, /* crcfile */ |
| 38 | | NULL, /* crc system name */ |
| 39 | | NULL, /* eoln */ |
| 40 | | 0, /* flags */ |
| 41 | | xsa_image_init, /* init function */ |
| 42 | | xsa_image_exit, /* exit function */ |
| 43 | | NULL, /* info function */ |
| 44 | | xsa_image_beginenum, /* begin enumeration */ |
| 45 | | xsa_image_nextenum, /* enumerate next */ |
| 46 | | xsa_image_closeenum, /* close enumeration */ |
| 47 | | NULL, /* free space on image */ |
| 48 | | xsa_image_readfile, /* read file */ |
| 49 | | NULL, /* write file */ |
| 50 | | NULL, /* delete file */ |
| 51 | | NULL, /* create image */ |
| 52 | | NULL, /* read sector */ |
| 53 | | NULL, /* write sector */ |
| 54 | | NULL, /* file options */ |
| 55 | | NULL /* create options */ |
| 56 | | ) |
| 57 | | |
| 58 | | static int xsa_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 59 | | { |
| 60 | | XSA_IMAGE *image; |
| 61 | | int pos, len, size; |
| 62 | | UINT8 header[4], byt; |
| 63 | | char *file_name, *file_name_new; |
| 64 | | |
| 65 | | size = stream_size (f); |
| 66 | | if (size < 5) return IMGTOOLERR_MODULENOTFOUND; |
| 67 | | if (4 != stream_read (f, header, 4) ) return IMGTOOLERR_READERROR; |
| 68 | | |
| 69 | | if (memcmp (header, "PCK\010", 4) ) |
| 70 | | return IMGTOOLERR_MODULENOTFOUND; |
| 71 | | |
| 72 | | if (4 != stream_read (f, &size, 4) ) return IMGTOOLERR_READERROR; |
| 73 | | size = LITTLE_ENDIANIZE_INT32 (size); |
| 74 | | if (4 != stream_read (f, header, 4) ) return IMGTOOLERR_READERROR; |
| 75 | | |
| 76 | | /* get name from XSA header, can't be longer than 8.3 really */ |
| 77 | | /* but could be, it's zero-terminated */ |
| 78 | | file_name = NULL; |
| 79 | | pos = len = 0; |
| 80 | | while (1) |
| 81 | | { |
| 82 | | if (1 != stream_read (f, &byt, 1) ) |
| 83 | | { |
| 84 | | if (file_name) free (file_name); |
| 85 | | *outimg=NULL; |
| 86 | | return IMGTOOLERR_READERROR; |
| 87 | | } |
| 88 | | if (len <= pos) |
| 89 | | { |
| 90 | | len += 8; /* why 8? */ |
| 91 | | file_name_new = realloc (file_name, len); |
| 92 | | if (!file_name_new) |
| 93 | | { |
| 94 | | if (file_name) free (file_name); |
| 95 | | *outimg=NULL; |
| 96 | | return IMGTOOLERR_OUTOFMEMORY; |
| 97 | | } |
| 98 | | file_name = file_name_new; |
| 99 | | } |
| 100 | | file_name[pos++] = byt; |
| 101 | | if (!byt) break; |
| 102 | | } |
| 103 | | |
| 104 | | image = (XSA_IMAGE*)malloc (sizeof (XSA_IMAGE) ); |
| 105 | | if (!image) return IMGTOOLERR_OUTOFMEMORY; |
| 106 | | *outimg = (imgtool_image*)image; |
| 107 | | |
| 108 | | memset(image, 0, sizeof(XSA_IMAGE)); |
| 109 | | image->base.module = mod; |
| 110 | | image->file_handle = f; |
| 111 | | image->size = size; |
| 112 | | image->file_name = file_name; |
| 113 | | |
| 114 | | return 0; |
| 115 | | } |
| 116 | | |
| 117 | | static void xsa_image_exit(imgtool_image *img) |
| 118 | | { |
| 119 | | XSA_IMAGE *image=(XSA_IMAGE*)img; |
| 120 | | stream_close(image->file_handle); |
| 121 | | free(image->file_name); |
| 122 | | free(image); |
| 123 | | } |
| 124 | | |
| 125 | | static int xsa_image_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 126 | | { |
| 127 | | XSA_IMAGE *image=(XSA_IMAGE*)img; |
| 128 | | XSA_ITERATOR *iter; |
| 129 | | |
| 130 | | iter=*(XSA_ITERATOR**)outenum = (XSA_ITERATOR*) malloc(sizeof(XSA_ITERATOR)); |
| 131 | | if (!iter) return IMGTOOLERR_OUTOFMEMORY; |
| 132 | | |
| 133 | | iter->base.module = img->module; |
| 134 | | |
| 135 | | iter->image=image; |
| 136 | | iter->index = 0; |
| 137 | | return 0; |
| 138 | | } |
| 139 | | |
| 140 | | static int xsa_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 141 | | { |
| 142 | | XSA_ITERATOR *iter=(XSA_ITERATOR*)enumeration; |
| 143 | | |
| 144 | | ent->eof = iter->index; |
| 145 | | if (!ent->eof) |
| 146 | | { |
| 147 | | strcpy (ent->fname, iter->image->file_name); |
| 148 | | |
| 149 | | ent->corrupt=0; |
| 150 | | ent->filesize = iter->image->size; |
| 151 | | |
| 152 | | iter->index++; |
| 153 | | } |
| 154 | | |
| 155 | | return 0; |
| 156 | | } |
| 157 | | |
| 158 | | static void xsa_image_closeenum(imgtool_directory *enumeration) |
| 159 | | { |
| 160 | | free(enumeration); |
| 161 | | } |
| 162 | | |
| 163 | | static int xsa_image_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 164 | | { |
| 165 | | XSA_IMAGE *image=(XSA_IMAGE*)img; |
| 166 | | |
| 167 | | /* check file name */ |
| 168 | | if (core_stricmp (fname, image->file_name) ) |
| 169 | | return IMGTOOLERR_MODULENOTFOUND; |
| 170 | | |
| 171 | | return xsa_extract (image->file_handle, destf); |
| 172 | | } |
| 173 | | |
| 174 | | |
| 175 | | /* |
| 176 | | * .xsa decompression. Code stolen from : |
| 177 | | * |
| 178 | | * http://web.inter.nl.net/users/A.P.Wulms/ |
| 179 | | */ |
| 180 | | |
| 181 | | |
| 182 | | /****************************************************************/ |
| 183 | | /* LZ77 data decompression */ |
| 184 | | /* Copyright (c) 1994 by XelaSoft */ |
| 185 | | /* version history: */ |
| 186 | | /* version 0.9, start date: 11-27-1994 */ |
| 187 | | /****************************************************************/ |
| 188 | | |
| 189 | | #define SHORT unsigned |
| 190 | | |
| 191 | | #define slwinsnrbits (13) |
| 192 | | #define maxstrlen (254) |
| 193 | | |
| 194 | | #define maxhufcnt (127) |
| 195 | | #define logtblsize (4) |
| 196 | | |
| 197 | | |
| 198 | | #define tblsize (1<<logtblsize) |
| 199 | | |
| 200 | | typedef struct huf_node { |
| 201 | | SHORT weight; |
| 202 | | struct huf_node *child1, *child2; |
| 203 | | } huf_node; |
| 204 | | |
| 205 | | /****************************************************************/ |
| 206 | | /* global vars */ |
| 207 | | /****************************************************************/ |
| 208 | | static unsigned updhufcnt; |
| 209 | | static unsigned cpdist[tblsize+1]; |
| 210 | | static unsigned cpdbmask[tblsize]; |
| 211 | | static const unsigned cpdext[] = { /* Extra bits for distance codes */ |
| 212 | | 0, 0, 0, 0, 1, 2, 3, 4, |
| 213 | | 5, 6, 7, 8, 9, 10, 11, 12 |
| 214 | | }; |
| 215 | | |
| 216 | | static SHORT tblsizes[tblsize]; |
| 217 | | static huf_node huftbl[2*tblsize-1]; |
| 218 | | |
| 219 | | /****************************************************************/ |
| 220 | | /* maak de huffman codeer informatie */ |
| 221 | | /****************************************************************/ |
| 222 | | static void mkhuftbl( void ) |
| 223 | | { |
| 224 | | unsigned count; |
| 225 | | huf_node *hufpos; |
| 226 | | huf_node *l1pos, *l2pos; |
| 227 | | SHORT tempw; |
| 228 | | |
| 229 | | /* Initialize the huffman tree */ |
| 230 | | for (count=0, hufpos=huftbl; count != tblsize; count++) { |
| 231 | | (hufpos++)->weight=1+(tblsizes[count] >>= 1); |
| 232 | | } |
| 233 | | for (count=tblsize; count != 2*tblsize-1; count++) |
| 234 | | (hufpos++)->weight=-1; |
| 235 | | |
| 236 | | /* Place the nodes in the correct manner in the tree */ |
| 237 | | while (huftbl[2*tblsize-2].weight == -1) { |
| 238 | | for (hufpos=huftbl; !(hufpos->weight); hufpos++) |
| 239 | | ; |
| 240 | | l1pos = hufpos++; |
| 241 | | while (!(hufpos->weight)) |
| 242 | | hufpos++; |
| 243 | | if (hufpos->weight < l1pos->weight) { |
| 244 | | l2pos = l1pos; |
| 245 | | l1pos = hufpos++; |
| 246 | | } |
| 247 | | else |
| 248 | | l2pos = hufpos++; |
| 249 | | while ((tempw=(hufpos)->weight) != -1) { |
| 250 | | if (tempw) { |
| 251 | | if (tempw < l1pos->weight) { |
| 252 | | l2pos = l1pos; |
| 253 | | l1pos = hufpos; |
| 254 | | } |
| 255 | | else if (tempw < l2pos->weight) |
| 256 | | l2pos = hufpos; |
| 257 | | } |
| 258 | | hufpos++; |
| 259 | | } |
| 260 | | hufpos->weight = l1pos->weight+l2pos->weight; |
| 261 | | (hufpos->child1 = l1pos)->weight = 0; |
| 262 | | (hufpos->child2 = l2pos)->weight = 0; |
| 263 | | } |
| 264 | | updhufcnt = maxhufcnt; |
| 265 | | } |
| 266 | | |
| 267 | | /****************************************************************/ |
| 268 | | /* initialize the huffman info tables */ |
| 269 | | /****************************************************************/ |
| 270 | | static void inithufinfo( void ) |
| 271 | | { |
| 272 | | unsigned offs, count; |
| 273 | | |
| 274 | | for (offs=1, count=0; count != tblsize; count++) { |
| 275 | | cpdist[count] = offs; |
| 276 | | offs += (cpdbmask[count] = 1<<cpdext[count]); |
| 277 | | } |
| 278 | | cpdist[count] = offs; |
| 279 | | |
| 280 | | for (count = 0; count != tblsize; count++) { |
| 281 | | tblsizes[count] = 0; /* reset the table counters */ |
| 282 | | huftbl[count].child1 = 0; /* mark the leave nodes */ |
| 283 | | } |
| 284 | | mkhuftbl(); /* make the huffman table */ |
| 285 | | } |
| 286 | | |
| 287 | | /****************************************************************/ |
| 288 | | /* global vars */ |
| 289 | | /****************************************************************/ |
| 290 | | static imgtool_stream *in_stream, *out_stream; |
| 291 | | |
| 292 | | static UINT8 *outbuf; /* the output buffer */ |
| 293 | | static UINT8 *outbufpos; /* pos in output buffer */ |
| 294 | | static int outbufcnt; /* #elements in the output buffer */ |
| 295 | | static int error_occ; |
| 296 | | |
| 297 | | static UINT8 bitflg; /* flag with the bits */ |
| 298 | | static UINT8 bitcnt; /* #resterende bits */ |
| 299 | | |
| 300 | | #define slwinsize (1 << slwinsnrbits) |
| 301 | | #define outbufsize (slwinsize+4096) |
| 302 | | |
| 303 | | /****************************************************************/ |
| 304 | | /* The function prototypes */ |
| 305 | | /****************************************************************/ |
| 306 | | static void unlz77( void ); /* perform the real decompression */ |
| 307 | | static void charout(UINT8); /* put a character in the output stream */ |
| 308 | | static unsigned rdstrlen( void ); /* read string length */ |
| 309 | | static unsigned rdstrpos( void ); /* read string pos */ |
| 310 | | static void flushoutbuf( void ); |
| 311 | | static UINT8 charin( void ); /* read a char */ |
| 312 | | static UINT8 bitin( void ); /* read a bit */ |
| 313 | | |
| 314 | | /****************************************************************/ |
| 315 | | /* de hoofdlus */ |
| 316 | | /****************************************************************/ |
| 317 | | static int xsa_extract (imgtool_stream *in, imgtool_stream *out) |
| 318 | | { |
| 319 | | UINT8 byt; |
| 320 | | |
| 321 | | /* setup the in-stream */ |
| 322 | | stream_seek (in, 12, SEEK_SET); |
| 323 | | do { |
| 324 | | if (1 != stream_read (in, &byt, 1) ) |
| 325 | | return IMGTOOLERR_READERROR; |
| 326 | | } while (byt); |
| 327 | | in_stream = in; |
| 328 | | bitcnt = 0; /* nog geen bits gelezen */ |
| 329 | | |
| 330 | | /* setup the out buffer */ |
| 331 | | outbuf = malloc (outbufsize); |
| 332 | | if (!outbuf) return IMGTOOLERR_OUTOFMEMORY; |
| 333 | | outbufcnt = 0; |
| 334 | | outbufpos = outbuf; /* dadelijk vooraan in laden */ |
| 335 | | out_stream = out; |
| 336 | | |
| 337 | | /* let's do it .. */ |
| 338 | | inithufinfo(); /* initialize the cpdist tables */ |
| 339 | | |
| 340 | | error_occ = 0; |
| 341 | | unlz77(); /* decrunch de data echt */ |
| 342 | | |
| 343 | | free (outbuf); |
| 344 | | |
| 345 | | return error_occ; |
| 346 | | } |
| 347 | | |
| 348 | | /****************************************************************/ |
| 349 | | /* the actual decompression algorithm itself */ |
| 350 | | /****************************************************************/ |
| 351 | | static void unlz77( void ) |
| 352 | | { |
| 353 | | UINT8 strl = 0; |
| 354 | | unsigned strpos; |
| 355 | | |
| 356 | | do { |
| 357 | | switch (bitin()) { |
| 358 | | case 0 : charout(charin()); break; |
| 359 | | case 1 : strl = rdstrlen(); |
| 360 | | if (strl == (maxstrlen+1)) |
| 361 | | break; |
| 362 | | strpos = rdstrpos(); |
| 363 | | while (strl--) |
| 364 | | charout(*(outbufpos-strpos)); |
| 365 | | strl = 0; |
| 366 | | break; |
| 367 | | } |
| 368 | | } |
| 369 | | while (strl != (maxstrlen+1)&&!error_occ); |
| 370 | | flushoutbuf (); |
| 371 | | } |
| 372 | | |
| 373 | | /****************************************************************/ |
| 374 | | /* Put the next character in the input buffer. */ |
| 375 | | /* Take care that minimal 'slwinsize' chars are before the */ |
| 376 | | /* current position. */ |
| 377 | | /****************************************************************/ |
| 378 | | static void charout(UINT8 ch) |
| 379 | | { |
| 380 | | if (error_occ) return; |
| 381 | | |
| 382 | | if ((outbufcnt++) == outbufsize) { |
| 383 | | if ( (outbufsize-slwinsize) != stream_write (out_stream, outbuf, outbufsize-slwinsize) ) |
| 384 | | error_occ = IMGTOOLERR_WRITEERROR; |
| 385 | | |
| 386 | | memcpy(outbuf, outbuf+outbufsize-slwinsize, slwinsize); |
| 387 | | outbufpos = outbuf+slwinsize; |
| 388 | | outbufcnt = slwinsize+1; |
| 389 | | } |
| 390 | | *(outbufpos++) = ch; |
| 391 | | } |
| 392 | | |
| 393 | | /****************************************************************/ |
| 394 | | /* flush the output buffer */ |
| 395 | | /****************************************************************/ |
| 396 | | static void flushoutbuf( void ) |
| 397 | | { |
| 398 | | if (!error_occ && outbufcnt) { |
| 399 | | if (outbufcnt != stream_write (out_stream, outbuf, outbufcnt) ) |
| 400 | | error_occ = IMGTOOLERR_WRITEERROR; |
| 401 | | |
| 402 | | outbufcnt = 0; |
| 403 | | } |
| 404 | | } |
| 405 | | |
| 406 | | /****************************************************************/ |
| 407 | | /* read string length */ |
| 408 | | /****************************************************************/ |
| 409 | | static unsigned rdstrlen( void ) |
| 410 | | { |
| 411 | | UINT8 len; |
| 412 | | UINT8 nrbits; |
| 413 | | |
| 414 | | if (!bitin()) |
| 415 | | return 2; |
| 416 | | if (!bitin()) |
| 417 | | return 3; |
| 418 | | if (!bitin()) |
| 419 | | return 4; |
| 420 | | |
| 421 | | for (nrbits = 2; (nrbits != 7) && bitin(); nrbits++) |
| 422 | | ; |
| 423 | | |
| 424 | | len = 1; |
| 425 | | while (nrbits--) |
| 426 | | len = (len << 1) | bitin(); |
| 427 | | |
| 428 | | return (unsigned)(len+1); |
| 429 | | } |
| 430 | | |
| 431 | | /****************************************************************/ |
| 432 | | /* read string pos */ |
| 433 | | /****************************************************************/ |
| 434 | | static unsigned rdstrpos( void ) |
| 435 | | { |
| 436 | | UINT8 nrbits; |
| 437 | | UINT8 cpdindex; |
| 438 | | unsigned strpos; |
| 439 | | huf_node *hufpos; |
| 440 | | |
| 441 | | hufpos = huftbl+2*tblsize-2; |
| 442 | | |
| 443 | | while (hufpos->child1) |
| 444 | | if (bitin()) { |
| 445 | | hufpos = hufpos->child2; |
| 446 | | } |
| 447 | | else { |
| 448 | | hufpos = hufpos->child1; |
| 449 | | } |
| 450 | | cpdindex = hufpos-huftbl; |
| 451 | | tblsizes[cpdindex]++; |
| 452 | | |
| 453 | | if (cpdbmask[cpdindex] >= 256) { |
| 454 | | UINT8 strposmsb, strposlsb; |
| 455 | | |
| 456 | | strposlsb = (unsigned)charin(); /* lees LSB van strpos */ |
| 457 | | strposmsb = 0; |
| 458 | | for (nrbits = cpdext[cpdindex]-8; nrbits--; strposmsb |= bitin()) |
| 459 | | strposmsb <<= 1; |
| 460 | | strpos = (unsigned)strposlsb | (((unsigned)strposmsb)<<8); |
| 461 | | } |
| 462 | | else { |
| 463 | | strpos=0; |
| 464 | | for (nrbits = cpdext[cpdindex]; nrbits--; strpos |= bitin()) |
| 465 | | strpos <<= 1; |
| 466 | | } |
| 467 | | if (!(updhufcnt--)) |
| 468 | | mkhuftbl(); /* make the huffman table */ |
| 469 | | |
| 470 | | return strpos+cpdist[cpdindex]; |
| 471 | | } |
| 472 | | |
| 473 | | /****************************************************************/ |
| 474 | | /* read a bit from the input file */ |
| 475 | | /****************************************************************/ |
| 476 | | static UINT8 bitin( void ) |
| 477 | | { |
| 478 | | UINT8 temp; |
| 479 | | |
| 480 | | if (!bitcnt) { |
| 481 | | bitflg = charin(); /* lees de bitflg in */ |
| 482 | | bitcnt = 8; /* nog 8 bits te verwerken */ |
| 483 | | } |
| 484 | | temp = bitflg & 1; |
| 485 | | bitcnt--; /* weer 1 bit verwerkt */ |
| 486 | | bitflg >>= 1; |
| 487 | | |
| 488 | | return temp; |
| 489 | | } |
| 490 | | |
| 491 | | /****************************************************************/ |
| 492 | | /* Get the next character from the input buffer. */ |
| 493 | | /****************************************************************/ |
| 494 | | static UINT8 charin( void ) |
| 495 | | { |
| 496 | | UINT8 byte; |
| 497 | | |
| 498 | | if (error_occ) |
| 499 | | return 0; |
| 500 | | else |
| 501 | | { |
| 502 | | if (1 != stream_read (in_stream, &byte, 1) ) |
| 503 | | { |
| 504 | | error_occ = IMGTOOLERR_READERROR; |
| 505 | | return 0; |
| 506 | | } |
| 507 | | else |
| 508 | | return byte; |
| 509 | | } |
| 510 | | } |