2
|
1 /*
|
|
2 * SIDInfo - PSID/RSID information displayer
|
|
3 * Written by Matti 'ccr' Hämäläinen
|
|
4 * (C) Copyright 2014 Tecnic Software productions (TNSP)
|
|
5 */
|
0
|
6 #include "th_args.h"
|
|
7 #include "th_endian.h"
|
2
|
8 #include "th_string.h"
|
0
|
9 #include "th_crypto.h"
|
|
10
|
2
|
11 #define PSID_MAGIC_LEN 4
|
|
12 #define PSID_STR_LEN 32
|
1
|
13 #define PSID_BUFFER_SIZE (1024 * 16)
|
|
14
|
0
|
15
|
2
|
16 enum
|
|
17 {
|
|
18 PSF_NONE = 0,
|
|
19
|
|
20 PSF_TYPE = 0x00000001,
|
|
21 PSF_VERSION = 0x00000002,
|
|
22 PSF_DATA_OFFS = 0x00000004,
|
|
23 PSF_LOAD_ADDR = 0x00000008,
|
|
24 PSF_INIT_ADDR = 0x00000010,
|
|
25 PSF_PLAY_ADDR = 0x00000020,
|
|
26 PSF_SONGS = 0x00000040,
|
|
27 PSF_START_SONG = 0x00000080,
|
|
28 PSF_SPEEDS = 0x00000100,
|
|
29 PSF_SID_NAME = 0x00000200,
|
|
30 PSF_SID_AUTHOR = 0x00000400,
|
|
31 PSF_SID_COPYRIGHT = 0x00000800,
|
|
32
|
|
33 PSF_DATA_SIZE = 0x00100000,
|
|
34 PSF_HASH = 0x00200000,
|
|
35
|
|
36 PSF_ALL = 0xffffffff,
|
|
37 };
|
|
38
|
|
39
|
0
|
40 typedef struct
|
|
41 {
|
2
|
42 uint32_t flag;
|
|
43 char *name;
|
|
44 char *lname;
|
|
45 } PSFOption;
|
|
46
|
|
47
|
|
48 const PSFOption optPSFlags[] =
|
|
49 {
|
|
50 { PSF_TYPE , "Type" , NULL },
|
|
51 { PSF_VERSION , "Version" , NULL },
|
|
52 { PSF_DATA_OFFS , "DataOffs" , "Data offset" },
|
|
53 { PSF_DATA_SIZE , "DataSize" , "Data size" },
|
|
54 { PSF_LOAD_ADDR , "LoadAddr" , "Load address" },
|
|
55 { PSF_INIT_ADDR , "InitAddr" , "Init address" },
|
|
56 { PSF_PLAY_ADDR , "PlayAddr" , "Play address" },
|
|
57 { PSF_SONGS , "Songs" , "Songs" },
|
|
58 { PSF_START_SONG , "StartSong" , "Start song" },
|
|
59 { PSF_SID_NAME , "Name" , NULL },
|
|
60 { PSF_SID_AUTHOR , "Author" , NULL },
|
|
61 { PSF_SID_COPYRIGHT , "Copyright" , NULL },
|
|
62 { PSF_HASH , "Hash" , NULL },
|
|
63 { PSF_ALL , "All" , NULL },
|
|
64 };
|
|
65
|
|
66 const int noptPSFlags = sizeof(optPSFlags) / sizeof(optPSFlags[0]);
|
|
67
|
|
68
|
|
69 /* Options
|
|
70 */
|
|
71 char * optInFilename = NULL;
|
|
72 BOOL optParsable = FALSE,
|
|
73 optHexadecimal = FALSE;
|
|
74 uint32_t optFields = PSF_ALL;
|
|
75
|
|
76
|
|
77 /* Arguments
|
|
78 */
|
|
79 static optarg_t optList[] =
|
|
80 {
|
|
81 { 0, '?', "help", "Show this help", OPT_NONE },
|
|
82 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
|
|
83 { 2, 'p', "parsable", "Output in script-parsable format", OPT_NONE },
|
|
84 { 3, 'f', "fields", "Show only specified field(s)", OPT_ARGREQ },
|
|
85 { 4, 'x', "hex", "Use hexadecimal values", OPT_NONE },
|
|
86 };
|
|
87
|
|
88 static const int optListN = (sizeof(optList) / sizeof(optList[0]));
|
|
89
|
|
90
|
|
91 void argShowHelp(void)
|
|
92 {
|
|
93 int index, n;
|
|
94
|
|
95 th_print_banner(stdout, th_prog_name, "[options] <sid filename>");
|
|
96 th_args_help(stdout, optList, optListN);
|
|
97 printf(
|
|
98 "\n"
|
|
99 "Available fields:\n");
|
|
100
|
|
101 for (index = n = 0; index < noptPSFlags; index++)
|
|
102 {
|
|
103 const PSFOption *opt = &optPSFlags[index];
|
|
104 printf("%s%s", opt->name, (index < noptPSFlags - 1) ? ", " : "\n\n");
|
|
105 if (++n > 5)
|
|
106 {
|
|
107 printf("\n");
|
|
108 n = 0;
|
|
109 }
|
|
110 }
|
|
111 }
|
|
112
|
|
113
|
|
114 int argMatchPSField(const char *field)
|
|
115 {
|
|
116 int index, found = -1;
|
|
117 size_t len = strlen(field);
|
|
118 for (index = 0; index < noptPSFlags; index++)
|
|
119 {
|
|
120 const PSFOption *opt = &optPSFlags[index];
|
|
121 if (strncasecmp(opt->name, field, len) == 0)
|
|
122 {
|
|
123 if (found >= 0)
|
|
124 return -2;
|
|
125 found = index;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 return found;
|
|
130 }
|
|
131
|
|
132
|
|
133 BOOL argParsePSField(char *opt, char *end, uint32_t *fields)
|
|
134 {
|
|
135 // Trim whitespace
|
|
136 if (end != NULL)
|
|
137 {
|
|
138 *end = 0;
|
|
139 for (end--; end > opt && *end && th_isspace(*end); end--)
|
|
140 *end = 0;
|
|
141 }
|
|
142 while (*opt && th_isspace(*opt)) opt++;
|
|
143
|
|
144 // Match field name
|
|
145 int found = argMatchPSField(opt);
|
|
146 switch (found)
|
|
147 {
|
|
148 case -1:
|
|
149 THERR("No such flag '%s'.\n", opt);
|
|
150 return FALSE;
|
|
151
|
|
152 case -2:
|
|
153 THERR("Flag '%s' is ambiguous.\n", opt);
|
|
154 return FALSE;
|
|
155
|
|
156 default:
|
|
157 *fields |= optPSFlags[found].flag;
|
|
158 return TRUE;
|
|
159 }
|
|
160 }
|
|
161
|
|
162
|
|
163 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
|
|
164 {
|
|
165 switch (optN)
|
|
166 {
|
|
167 case 0:
|
|
168 argShowHelp();
|
|
169 exit(0);
|
|
170 break;
|
|
171
|
|
172 case 1:
|
|
173 th_verbosityLevel++;
|
|
174 break;
|
|
175
|
|
176 case 2:
|
|
177 optParsable = TRUE;
|
|
178 break;
|
|
179
|
|
180 case 3:
|
|
181 {
|
|
182 char *start = optArg;
|
|
183 optFields = PSF_NONE;
|
|
184
|
|
185 while (*start)
|
|
186 {
|
|
187 char *end = strchr(start, ',');
|
|
188
|
|
189 if (!argParsePSField(start, end, &optFields))
|
|
190 return FALSE;
|
|
191
|
|
192 if (!end)
|
|
193 break;
|
|
194
|
|
195 start = end + 1;
|
|
196 }
|
|
197
|
|
198 //fprintf(stderr, "%08x\n", optFields);
|
|
199 }
|
|
200 break;
|
|
201
|
|
202 case 4:
|
|
203 optHexadecimal = TRUE;
|
|
204 break;
|
|
205
|
|
206 default:
|
|
207 THERR("Unknown option '%s'.\n", currArg);
|
|
208 return FALSE;
|
|
209 }
|
|
210
|
|
211 return TRUE;
|
|
212 }
|
|
213
|
|
214
|
|
215 BOOL argHandleFile(char *currArg)
|
|
216 {
|
|
217 if (!optInFilename)
|
|
218 optInFilename = currArg;
|
|
219 else
|
|
220 {
|
|
221 THERR("Filename already specified on commandline!\n");
|
|
222 return FALSE;
|
|
223 }
|
|
224
|
|
225 return TRUE;
|
|
226 }
|
|
227
|
|
228
|
|
229 typedef struct
|
|
230 {
|
|
231 char magic[PSID_MAGIC_LEN + 1]; // "PSID" / "RSID" magic identifier
|
|
232 uint16_t
|
|
233 version, // Version number
|
0
|
234 dataOffset, // Start of actual c64 data in file
|
|
235 loadAddress, // Loading address
|
|
236 initAddress, // Initialization address
|
|
237 playAddress, // Play one frame
|
|
238 nSongs, // Number of subsongs
|
|
239 startSong; // Default starting song
|
|
240 uint32_t speed; // Speed
|
2
|
241 char sidName[PSID_STR_LEN + 1]; // Descriptive text-fields, ASCIIZ
|
|
242 char sidAuthor[PSID_STR_LEN + 1];
|
|
243 char sidCopyright[PSID_STR_LEN + 1];
|
0
|
244
|
|
245 // PSIDv2 data
|
|
246 uint16_t flags; // Flags
|
|
247 uint8_t startPage, pageLength;
|
|
248 uint16_t reserved;
|
|
249
|
1
|
250 // Extra data
|
0
|
251 BOOL isRSID;
|
1
|
252 size_t dataSize; // Total size of data - header
|
|
253 th_md5hash_t hash; // Songlength database hash
|
0
|
254
|
|
255 } PSIDHeader;
|
|
256
|
|
257
|
2
|
258 static void siAppendHash16(th_md5state_t *state, uint16_t data)
|
|
259 {
|
|
260 uint8_t ib8[2];
|
|
261 ib8[0] = data & 0xff;
|
|
262 ib8[1] = data >> 8;
|
|
263 th_md5_append(state, (uint8_t *) &ib8, sizeof(ib8));
|
|
264 }
|
|
265
|
|
266
|
|
267 int siReadPSIDFile(FILE *inFile, PSIDHeader *psid)
|
0
|
268 {
|
1
|
269 th_md5state_t state;
|
|
270 uint8_t tmp8, *fileData = NULL;
|
|
271 int index, ret = -1;
|
|
272 size_t read;
|
|
273 BOOL first;
|
|
274
|
|
275 memset(psid, 0, sizeof(*psid));
|
|
276
|
|
277 if ((fileData = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL)
|
|
278 {
|
|
279 THERR("Error allocating temporary data buffer of %d bytes.\n", PSID_BUFFER_SIZE);
|
|
280 goto error;
|
|
281 }
|
0
|
282
|
|
283 // Read PSID header in
|
2
|
284 if (!th_fread_str(inFile, (uint8_t *) psid->magic, PSID_MAGIC_LEN) ||
|
1
|
285 !th_fread_be16(inFile, &psid->version) ||
|
|
286 !th_fread_be16(inFile, &psid->dataOffset) ||
|
|
287 !th_fread_be16(inFile, &psid->loadAddress) ||
|
|
288 !th_fread_be16(inFile, &psid->initAddress) ||
|
|
289 !th_fread_be16(inFile, &psid->playAddress) ||
|
|
290 !th_fread_be16(inFile, &psid->nSongs) ||
|
|
291 !th_fread_be16(inFile, &psid->startSong) ||
|
|
292 !th_fread_be32(inFile, &psid->speed))
|
0
|
293 {
|
1
|
294 THERR("Could not read PSID/RSID header.\n");
|
0
|
295 goto error;
|
|
296 }
|
|
297
|
2
|
298 psid->magic[PSID_MAGIC_LEN] = 0;
|
|
299
|
0
|
300 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') ||
|
|
301 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' ||
|
|
302 psid->version < 1 || psid->version > 3)
|
|
303 {
|
1
|
304 THERR("Not a supported PSID or RSID file.\n");
|
0
|
305 goto error;
|
|
306 }
|
|
307
|
1
|
308 psid->isRSID = psid->magic[0] == 'R';
|
0
|
309
|
2
|
310 if (!th_fread_str(inFile, (uint8_t *)psid->sidName, PSID_STR_LEN) ||
|
|
311 !th_fread_str(inFile, (uint8_t *)psid->sidAuthor, PSID_STR_LEN) ||
|
|
312 !th_fread_str(inFile, (uint8_t *)psid->sidCopyright, PSID_STR_LEN))
|
0
|
313 {
|
1
|
314 THERR("Error reading SID file header.\n");
|
0
|
315 goto error;
|
|
316 }
|
|
317
|
2
|
318 psid->sidName[PSID_STR_LEN] = 0;
|
|
319 psid->sidAuthor[PSID_STR_LEN] = 0;
|
|
320 psid->sidCopyright[PSID_STR_LEN] = 0;
|
|
321
|
0
|
322 // Check if we need to load PSIDv2NG header ...
|
|
323 if (psid->version >= 2)
|
|
324 {
|
|
325 // Yes, we need to
|
1
|
326 if (!th_fread_be16(inFile, &psid->flags) ||
|
|
327 !th_fread_byte(inFile, &psid->startPage) ||
|
|
328 !th_fread_byte(inFile, &psid->pageLength) ||
|
|
329 !th_fread_be16(inFile, &psid->reserved))
|
0
|
330 {
|
1
|
331 THERR("Error reading PSID/RSID v2+ extra header data.\n");
|
0
|
332 goto error;
|
|
333 }
|
|
334 }
|
|
335
|
1
|
336 // Initialize MD5-hash calculation
|
|
337 th_md5_init(&state);
|
0
|
338
|
1
|
339 // Process actual data
|
|
340 psid->dataSize = 0;
|
|
341 first = TRUE;
|
2
|
342 do
|
|
343 {
|
1
|
344 read = fread(fileData, sizeof(uint8_t), PSID_BUFFER_SIZE, inFile);
|
|
345 psid->dataSize += read;
|
|
346 if (read < 16)
|
|
347 {
|
|
348 THERR("Error reading song data, unexpectedly small file.\n");
|
|
349 goto error;
|
|
350 }
|
0
|
351
|
1
|
352 if (first && psid->loadAddress == 0)
|
|
353 {
|
|
354 // Strip load address (2 first bytes)
|
|
355 th_md5_append(&state, &fileData[2], read - 2);
|
|
356 first = FALSE;
|
|
357 }
|
|
358 else
|
|
359 {
|
|
360 // Append "as is"
|
|
361 th_md5_append(&state, fileData, read);
|
|
362 }
|
|
363 } while (read > 0 && !feof(inFile));
|
0
|
364
|
1
|
365 // Append header data to hash
|
2
|
366 siAppendHash16(&state, psid->initAddress);
|
|
367 siAppendHash16(&state, psid->playAddress);
|
|
368 siAppendHash16(&state, psid->nSongs);
|
0
|
369
|
|
370 // Append song speed data to hash
|
1
|
371 tmp8 = psid->isRSID ? 60 : 0;
|
0
|
372 for (index = 0; index < psid->nSongs && index < 32; index++)
|
|
373 {
|
1
|
374 if (psid->isRSID)
|
|
375 tmp8 = 60;
|
0
|
376 else
|
1
|
377 tmp8 = (psid->speed & (1 << index)) ? 60 : 0;
|
0
|
378
|
1
|
379 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
380 }
|
|
381
|
|
382 // Rest of songs (more than 32)
|
|
383 for (index = 32; index < psid->nSongs; index++)
|
1
|
384 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
385
|
|
386 // PSIDv2NG specific
|
|
387 if (psid->version >= 2)
|
|
388 {
|
|
389 // REFER TO SIDPLAY HEADERS FOR MORE INFORMATION
|
1
|
390 tmp8 = (psid->flags >> 2) & 3;
|
|
391 if (tmp8 == 2)
|
|
392 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
393 }
|
|
394
|
|
395 // Calculate the hash
|
1
|
396 th_md5_finish(&state, psid->hash);
|
|
397 ret = 0;
|
0
|
398
|
1
|
399 error:
|
0
|
400 // Free buffer
|
1
|
401 th_free(fileData);
|
|
402 return ret;
|
0
|
403 }
|
|
404
|
|
405
|
2
|
406 static void siPrintLinePrefix(FILE *outFile, const BOOL parsable, const char *name)
|
|
407 {
|
|
408 if (parsable)
|
|
409 fprintf(outFile, "%s=", name);
|
|
410 else
|
|
411 fprintf(outFile, "%-20s : ", name);
|
|
412 }
|
|
413
|
|
414
|
|
415 static void siPrintPSIDInfoLine(FILE *outFile, const BOOL parsable, const BOOL hex,
|
|
416 const uint32_t flags, const int xindex, const char *xfmt, const char *xaltfmt, ...)
|
|
417 {
|
|
418 const PSFOption *opt = &optPSFlags[xindex];
|
|
419 if (flags & opt->flag)
|
|
420 {
|
|
421 va_list ap;
|
|
422 const char *fmt = hex ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt;
|
|
423
|
|
424 siPrintLinePrefix(outFile, parsable, (parsable || opt->lname == NULL) ? opt->name : opt->lname);
|
|
425
|
|
426 va_start(ap, xaltfmt);
|
|
427
|
|
428 if (parsable)
|
|
429 vfprintf(outFile, fmt, ap);
|
|
430 else
|
|
431 vfprintf(outFile, fmt, ap);
|
|
432
|
|
433 va_end(ap);
|
|
434
|
|
435 fprintf(outFile, "\n");
|
|
436 }
|
|
437 }
|
|
438
|
|
439 #define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, parsable, hex, flags, xindex, xfmt, xaltfmt, __VA_ARGS__ )
|
|
440
|
|
441
|
|
442 void siPrintPSIDInformation(FILE *outFile, const BOOL parsable, const BOOL hex, const uint32_t flags, const PSIDHeader *psid)
|
|
443 {
|
|
444 PR( 0, "%s", NULL, psid->magic);
|
|
445 PR( 1, "%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8));
|
|
446 PR( 2, "%d", "$%08x", psid->dataOffset);
|
|
447 PR( 3, "%d", "$%08x", psid->dataSize);
|
|
448 PR( 4, "%d", "$%04x", psid->loadAddress);
|
|
449 PR( 5, "%d", "$%04x", psid->initAddress);
|
|
450 PR( 6, "%d", "$%04x", psid->playAddress);
|
|
451 PR( 7, "%d", "$%04x", psid->nSongs);
|
|
452 PR( 8, "%d", "$%04x", psid->startSong);
|
|
453
|
|
454 PR( 9, "%s", NULL, psid->sidName);
|
|
455 PR(10, "%s", NULL, psid->sidAuthor);
|
|
456 PR(11, "%s", NULL, psid->sidCopyright);
|
|
457
|
|
458 if (flags & PSF_HASH)
|
|
459 {
|
|
460 siPrintLinePrefix(outFile, parsable, "Hash");
|
|
461 th_md5_print(outFile, psid->hash);
|
|
462 fprintf(outFile, "\n");
|
|
463 }
|
|
464 }
|
|
465
|
0
|
466
|
|
467 int main(int argc, char *argv[])
|
|
468 {
|
2
|
469 FILE *inFile = NULL;
|
|
470 int ret = -1;
|
|
471 PSIDHeader psid;
|
|
472
|
0
|
473 // Initialize
|
|
474 th_init("SIDInfo", "PSID/RSID information displayer", "0.1", NULL, NULL);
|
|
475 th_verbosityLevel = 0;
|
|
476
|
2
|
477 // Parse command line arguments
|
|
478 if (!th_args_process(argc, argv, optList, optListN,
|
|
479 argHandleOpt, argHandleFile, FALSE))
|
|
480 return -1;
|
|
481
|
|
482 if (optInFilename == NULL)
|
|
483 {
|
|
484 argShowHelp();
|
|
485 THERR("No filename specified.\n");
|
|
486 goto error;
|
|
487 }
|
0
|
488
|
2
|
489 // Try to open the file
|
|
490 if ((inFile = fopen(optInFilename, "rb")) == NULL)
|
|
491 {
|
|
492 THERR("Could not open file '%s'.\n", optInFilename);
|
|
493 goto error;
|
|
494 }
|
1
|
495
|
2
|
496 // Read PSID data
|
|
497 if ((ret = siReadPSIDFile(inFile, &psid)) != 0)
|
|
498 goto error;
|
|
499
|
|
500 // Output
|
|
501 fprintf(stdout, "%s\n", optInFilename);
|
|
502 siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, &psid);
|
|
503
|
|
504 ret = 0;
|
|
505
|
|
506 // Shutdown
|
|
507 error:
|
|
508 if (inFile != NULL)
|
|
509 fclose(inFile);
|
|
510
|
|
511 return ret;
|
0
|
512 }
|