| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "c64diskimage.h" |
| 14 |
#include "common.h" |
| 15 |
#include "gameinfo.h" |
| 16 |
#include "graphics.h" |
| 17 |
|
| 18 |
#include "c64detect.h" |
| 19 |
|
| 20 |
#define MAX_LENGTH 300000 |
| 21 |
#define MIN_LENGTH 24 |
| 22 |
|
| 23 |
int issagaimg(const char *name) |
| 24 |
{ |
| 25 |
if (name == NULL) |
| 26 |
return 0; |
| 27 |
size_t len = strlen(name); |
| 28 |
if (len < 4) |
| 29 |
return 0; |
| 30 |
char c = name[0]; |
| 31 |
if (c == 'R' || c == 'B' || c == 'S') { |
| 32 |
for (int i = 1; i < 4; i++) |
| 33 |
if (!isdigit(name[i])) |
| 34 |
return 0; |
| 35 |
return 1; |
| 36 |
} |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static uint8_t *get_file_named(uint8_t *data, size_t length, size_t *newlength, |
| 41 |
const char *name) |
| 42 |
{ |
| 43 |
uint8_t *file = NULL; |
| 44 |
*newlength = 0; |
| 45 |
unsigned char rawname[100]; |
| 46 |
DiskImage *d64 = di_create_from_data(data, (int)length); |
| 47 |
di_rawname_from_name(rawname, name); |
| 48 |
if (d64) { |
| 49 |
ImageFile *c64file = di_open(d64, rawname, 0xc2, "rb"); |
| 50 |
if (c64file) { |
| 51 |
uint8_t buf[0xffff]; |
| 52 |
*newlength = di_read(c64file, buf, 0xffff); |
| 53 |
file = MemAlloc(*newlength); |
| 54 |
memcpy(file, buf, *newlength); |
| 55 |
free(c64file); |
| 56 |
} |
| 57 |
int numfiles; |
| 58 |
char **filenames = get_all_file_names(d64, &numfiles); |
| 59 |
|
| 60 |
if (filenames) { |
| 61 |
int imgindex = 0; |
| 62 |
char *imagefiles[numfiles]; |
| 63 |
for (int i = 0; i < numfiles; i++) { |
| 64 |
if (issagaimg(filenames[i])) { |
| 65 |
imagefiles[imgindex++] = filenames[i]; |
| 66 |
} else { |
| 67 |
free(filenames[i]); |
| 68 |
} |
| 69 |
} |
| 70 |
free(filenames); |
| 71 |
|
| 72 |
Images = MemAlloc((imgindex + 1) * sizeof(struct imgrec)); |
| 73 |
for (int i = 0; i < imgindex; i++) { |
| 74 |
Images[i].Filename = imagefiles[i]; |
| 75 |
di_rawname_from_name(rawname, imagefiles[i]); |
| 76 |
c64file = di_open(d64, rawname, 0xc2, "rb"); |
| 77 |
if (c64file) { |
| 78 |
uint8_t buf[0xffff]; |
| 79 |
Images[i].Size = di_read(c64file, buf, 0xffff); |
| 80 |
Images[i].Data = MemAlloc(Images[i].Size); |
| 81 |
memcpy(Images[i].Data, buf, Images[i].Size); |
| 82 |
free(c64file); |
| 83 |
} |
| 84 |
} |
| 85 |
Images[imgindex].Filename = NULL; |
| 86 |
} |
| 87 |
} |
| 88 |
return file; |
| 89 |
} |
| 90 |
|
| 91 |
int DetectC64(uint8_t **sf, size_t *extent) |
| 92 |
{ |
| 93 |
if (*extent > MAX_LENGTH || *extent < MIN_LENGTH) |
| 94 |
return 0; |
| 95 |
|
| 96 |
size_t newlength; |
| 97 |
uint8_t *datafile = get_file_named(*sf, *extent, &newlength, "DATA"); |
| 98 |
|
| 99 |
if (datafile) { |
| 100 |
free(*sf); |
| 101 |
*sf = datafile; |
| 102 |
*extent = newlength; |
| 103 |
CurrentSys = SYS_C64; |
| 104 |
ImageWidth = 280; |
| 105 |
ImageHeight = 158; |
| 106 |
return 1; |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |