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");
|
3
|
100
|
2
|
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 }
|
5
|
111
|
|
112 printf("Example: %s -x -p -f hash,copyright somesidfile.sid\n", th_prog_name);
|
2
|
113 }
|
|
114
|
|
115
|
|
116 int argMatchPSField(const char *field)
|
|
117 {
|
|
118 int index, found = -1;
|
|
119 size_t len = strlen(field);
|
|
120 for (index = 0; index < noptPSFlags; index++)
|
|
121 {
|
|
122 const PSFOption *opt = &optPSFlags[index];
|
|
123 if (strncasecmp(opt->name, field, len) == 0)
|
|
124 {
|
|
125 if (found >= 0)
|
|
126 return -2;
|
|
127 found = index;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 return found;
|
|
132 }
|
|
133
|
|
134
|
|
135 BOOL argParsePSField(char *opt, char *end, uint32_t *fields)
|
|
136 {
|
|
137 // Trim whitespace
|
|
138 if (end != NULL)
|
|
139 {
|
|
140 *end = 0;
|
|
141 for (end--; end > opt && *end && th_isspace(*end); end--)
|
|
142 *end = 0;
|
|
143 }
|
|
144 while (*opt && th_isspace(*opt)) opt++;
|
|
145
|
|
146 // Match field name
|
|
147 int found = argMatchPSField(opt);
|
|
148 switch (found)
|
|
149 {
|
|
150 case -1:
|
|
151 THERR("No such flag '%s'.\n", opt);
|
|
152 return FALSE;
|
|
153
|
|
154 case -2:
|
|
155 THERR("Flag '%s' is ambiguous.\n", opt);
|
|
156 return FALSE;
|
3
|
157
|
2
|
158 default:
|
|
159 *fields |= optPSFlags[found].flag;
|
|
160 return TRUE;
|
|
161 }
|
|
162 }
|
|
163
|
|
164
|
|
165 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
|
|
166 {
|
|
167 switch (optN)
|
|
168 {
|
|
169 case 0:
|
|
170 argShowHelp();
|
|
171 exit(0);
|
|
172 break;
|
|
173
|
|
174 case 1:
|
|
175 th_verbosityLevel++;
|
|
176 break;
|
|
177
|
|
178 case 2:
|
|
179 optParsable = TRUE;
|
|
180 break;
|
|
181
|
|
182 case 3:
|
|
183 {
|
|
184 char *start = optArg;
|
|
185 optFields = PSF_NONE;
|
|
186
|
|
187 while (*start)
|
|
188 {
|
|
189 char *end = strchr(start, ',');
|
|
190
|
|
191 if (!argParsePSField(start, end, &optFields))
|
|
192 return FALSE;
|
|
193
|
|
194 if (!end)
|
|
195 break;
|
|
196
|
|
197 start = end + 1;
|
|
198 }
|
3
|
199
|
2
|
200 //fprintf(stderr, "%08x\n", optFields);
|
|
201 }
|
|
202 break;
|
|
203
|
|
204 case 4:
|
|
205 optHexadecimal = TRUE;
|
|
206 break;
|
|
207
|
|
208 default:
|
|
209 THERR("Unknown option '%s'.\n", currArg);
|
|
210 return FALSE;
|
|
211 }
|
|
212
|
|
213 return TRUE;
|
|
214 }
|
|
215
|
|
216
|
|
217 BOOL argHandleFile(char *currArg)
|
|
218 {
|
|
219 if (!optInFilename)
|
|
220 optInFilename = currArg;
|
|
221 else
|
|
222 {
|
|
223 THERR("Filename already specified on commandline!\n");
|
|
224 return FALSE;
|
|
225 }
|
|
226
|
|
227 return TRUE;
|
|
228 }
|
|
229
|
|
230
|
|
231 typedef struct
|
|
232 {
|
|
233 char magic[PSID_MAGIC_LEN + 1]; // "PSID" / "RSID" magic identifier
|
|
234 uint16_t
|
|
235 version, // Version number
|
0
|
236 dataOffset, // Start of actual c64 data in file
|
|
237 loadAddress, // Loading address
|
|
238 initAddress, // Initialization address
|
|
239 playAddress, // Play one frame
|
|
240 nSongs, // Number of subsongs
|
|
241 startSong; // Default starting song
|
|
242 uint32_t speed; // Speed
|
2
|
243 char sidName[PSID_STR_LEN + 1]; // Descriptive text-fields, ASCIIZ
|
|
244 char sidAuthor[PSID_STR_LEN + 1];
|
|
245 char sidCopyright[PSID_STR_LEN + 1];
|
0
|
246
|
|
247 // PSIDv2 data
|
|
248 uint16_t flags; // Flags
|
|
249 uint8_t startPage, pageLength;
|
|
250 uint16_t reserved;
|
|
251
|
1
|
252 // Extra data
|
0
|
253 BOOL isRSID;
|
1
|
254 size_t dataSize; // Total size of data - header
|
|
255 th_md5hash_t hash; // Songlength database hash
|
0
|
256
|
|
257 } PSIDHeader;
|
|
258
|
|
259
|
2
|
260 static void siAppendHash16(th_md5state_t *state, uint16_t data)
|
|
261 {
|
|
262 uint8_t ib8[2];
|
|
263 ib8[0] = data & 0xff;
|
|
264 ib8[1] = data >> 8;
|
|
265 th_md5_append(state, (uint8_t *) &ib8, sizeof(ib8));
|
|
266 }
|
|
267
|
|
268
|
|
269 int siReadPSIDFile(FILE *inFile, PSIDHeader *psid)
|
0
|
270 {
|
1
|
271 th_md5state_t state;
|
|
272 uint8_t tmp8, *fileData = NULL;
|
|
273 int index, ret = -1;
|
|
274 size_t read;
|
|
275 BOOL first;
|
|
276
|
|
277 memset(psid, 0, sizeof(*psid));
|
|
278
|
|
279 if ((fileData = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL)
|
|
280 {
|
|
281 THERR("Error allocating temporary data buffer of %d bytes.\n", PSID_BUFFER_SIZE);
|
|
282 goto error;
|
|
283 }
|
3
|
284
|
0
|
285 // Read PSID header in
|
2
|
286 if (!th_fread_str(inFile, (uint8_t *) psid->magic, PSID_MAGIC_LEN) ||
|
1
|
287 !th_fread_be16(inFile, &psid->version) ||
|
|
288 !th_fread_be16(inFile, &psid->dataOffset) ||
|
|
289 !th_fread_be16(inFile, &psid->loadAddress) ||
|
|
290 !th_fread_be16(inFile, &psid->initAddress) ||
|
|
291 !th_fread_be16(inFile, &psid->playAddress) ||
|
|
292 !th_fread_be16(inFile, &psid->nSongs) ||
|
|
293 !th_fread_be16(inFile, &psid->startSong) ||
|
|
294 !th_fread_be32(inFile, &psid->speed))
|
0
|
295 {
|
1
|
296 THERR("Could not read PSID/RSID header.\n");
|
0
|
297 goto error;
|
|
298 }
|
|
299
|
2
|
300 psid->magic[PSID_MAGIC_LEN] = 0;
|
|
301
|
0
|
302 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') ||
|
|
303 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' ||
|
|
304 psid->version < 1 || psid->version > 3)
|
|
305 {
|
1
|
306 THERR("Not a supported PSID or RSID file.\n");
|
0
|
307 goto error;
|
|
308 }
|
|
309
|
1
|
310 psid->isRSID = psid->magic[0] == 'R';
|
0
|
311
|
2
|
312 if (!th_fread_str(inFile, (uint8_t *)psid->sidName, PSID_STR_LEN) ||
|
|
313 !th_fread_str(inFile, (uint8_t *)psid->sidAuthor, PSID_STR_LEN) ||
|
|
314 !th_fread_str(inFile, (uint8_t *)psid->sidCopyright, PSID_STR_LEN))
|
0
|
315 {
|
1
|
316 THERR("Error reading SID file header.\n");
|
0
|
317 goto error;
|
|
318 }
|
|
319
|
2
|
320 psid->sidName[PSID_STR_LEN] = 0;
|
|
321 psid->sidAuthor[PSID_STR_LEN] = 0;
|
|
322 psid->sidCopyright[PSID_STR_LEN] = 0;
|
|
323
|
0
|
324 // Check if we need to load PSIDv2NG header ...
|
|
325 if (psid->version >= 2)
|
|
326 {
|
|
327 // Yes, we need to
|
1
|
328 if (!th_fread_be16(inFile, &psid->flags) ||
|
|
329 !th_fread_byte(inFile, &psid->startPage) ||
|
|
330 !th_fread_byte(inFile, &psid->pageLength) ||
|
|
331 !th_fread_be16(inFile, &psid->reserved))
|
0
|
332 {
|
1
|
333 THERR("Error reading PSID/RSID v2+ extra header data.\n");
|
0
|
334 goto error;
|
|
335 }
|
|
336 }
|
|
337
|
1
|
338 // Initialize MD5-hash calculation
|
|
339 th_md5_init(&state);
|
0
|
340
|
1
|
341 // Process actual data
|
|
342 psid->dataSize = 0;
|
|
343 first = TRUE;
|
2
|
344 do
|
|
345 {
|
1
|
346 read = fread(fileData, sizeof(uint8_t), PSID_BUFFER_SIZE, inFile);
|
|
347 psid->dataSize += read;
|
|
348 if (read < 16)
|
|
349 {
|
|
350 THERR("Error reading song data, unexpectedly small file.\n");
|
|
351 goto error;
|
|
352 }
|
0
|
353
|
1
|
354 if (first && psid->loadAddress == 0)
|
|
355 {
|
|
356 // Strip load address (2 first bytes)
|
|
357 th_md5_append(&state, &fileData[2], read - 2);
|
|
358 first = FALSE;
|
|
359 }
|
|
360 else
|
|
361 {
|
|
362 // Append "as is"
|
|
363 th_md5_append(&state, fileData, read);
|
|
364 }
|
|
365 } while (read > 0 && !feof(inFile));
|
0
|
366
|
1
|
367 // Append header data to hash
|
2
|
368 siAppendHash16(&state, psid->initAddress);
|
|
369 siAppendHash16(&state, psid->playAddress);
|
|
370 siAppendHash16(&state, psid->nSongs);
|
0
|
371
|
|
372 // Append song speed data to hash
|
1
|
373 tmp8 = psid->isRSID ? 60 : 0;
|
0
|
374 for (index = 0; index < psid->nSongs && index < 32; index++)
|
|
375 {
|
1
|
376 if (psid->isRSID)
|
|
377 tmp8 = 60;
|
0
|
378 else
|
1
|
379 tmp8 = (psid->speed & (1 << index)) ? 60 : 0;
|
0
|
380
|
1
|
381 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
382 }
|
|
383
|
|
384 // Rest of songs (more than 32)
|
|
385 for (index = 32; index < psid->nSongs; index++)
|
1
|
386 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
387
|
|
388 // PSIDv2NG specific
|
|
389 if (psid->version >= 2)
|
|
390 {
|
|
391 // REFER TO SIDPLAY HEADERS FOR MORE INFORMATION
|
1
|
392 tmp8 = (psid->flags >> 2) & 3;
|
|
393 if (tmp8 == 2)
|
|
394 th_md5_append(&state, &tmp8, sizeof(tmp8));
|
0
|
395 }
|
|
396
|
|
397 // Calculate the hash
|
1
|
398 th_md5_finish(&state, psid->hash);
|
|
399 ret = 0;
|
0
|
400
|
1
|
401 error:
|
0
|
402 // Free buffer
|
1
|
403 th_free(fileData);
|
|
404 return ret;
|
0
|
405 }
|
|
406
|
|
407
|
2
|
408 static void siPrintLinePrefix(FILE *outFile, const BOOL parsable, const char *name)
|
|
409 {
|
|
410 if (parsable)
|
|
411 fprintf(outFile, "%s=", name);
|
|
412 else
|
|
413 fprintf(outFile, "%-20s : ", name);
|
|
414 }
|
|
415
|
|
416
|
|
417 static void siPrintPSIDInfoLine(FILE *outFile, const BOOL parsable, const BOOL hex,
|
|
418 const uint32_t flags, const int xindex, const char *xfmt, const char *xaltfmt, ...)
|
|
419 {
|
|
420 const PSFOption *opt = &optPSFlags[xindex];
|
|
421 if (flags & opt->flag)
|
|
422 {
|
|
423 va_list ap;
|
|
424 const char *fmt = hex ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt;
|
|
425
|
|
426 siPrintLinePrefix(outFile, parsable, (parsable || opt->lname == NULL) ? opt->name : opt->lname);
|
|
427
|
|
428 va_start(ap, xaltfmt);
|
|
429
|
|
430 if (parsable)
|
|
431 vfprintf(outFile, fmt, ap);
|
|
432 else
|
|
433 vfprintf(outFile, fmt, ap);
|
|
434
|
|
435 va_end(ap);
|
3
|
436
|
2
|
437 fprintf(outFile, "\n");
|
|
438 }
|
|
439 }
|
|
440
|
|
441 #define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, parsable, hex, flags, xindex, xfmt, xaltfmt, __VA_ARGS__ )
|
|
442
|
|
443
|
|
444 void siPrintPSIDInformation(FILE *outFile, const BOOL parsable, const BOOL hex, const uint32_t flags, const PSIDHeader *psid)
|
|
445 {
|
|
446 PR( 0, "%s", NULL, psid->magic);
|
|
447 PR( 1, "%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8));
|
|
448 PR( 2, "%d", "$%08x", psid->dataOffset);
|
|
449 PR( 3, "%d", "$%08x", psid->dataSize);
|
|
450 PR( 4, "%d", "$%04x", psid->loadAddress);
|
|
451 PR( 5, "%d", "$%04x", psid->initAddress);
|
|
452 PR( 6, "%d", "$%04x", psid->playAddress);
|
|
453 PR( 7, "%d", "$%04x", psid->nSongs);
|
|
454 PR( 8, "%d", "$%04x", psid->startSong);
|
|
455
|
|
456 PR( 9, "%s", NULL, psid->sidName);
|
|
457 PR(10, "%s", NULL, psid->sidAuthor);
|
3
|
458 PR(11, "%s", NULL, psid->sidCopyright);
|
2
|
459
|
|
460 if (flags & PSF_HASH)
|
|
461 {
|
|
462 siPrintLinePrefix(outFile, parsable, "Hash");
|
|
463 th_md5_print(outFile, psid->hash);
|
|
464 fprintf(outFile, "\n");
|
|
465 }
|
|
466 }
|
|
467
|
0
|
468
|
|
469 int main(int argc, char *argv[])
|
|
470 {
|
2
|
471 FILE *inFile = NULL;
|
|
472 int ret = -1;
|
|
473 PSIDHeader psid;
|
|
474
|
0
|
475 // Initialize
|
|
476 th_init("SIDInfo", "PSID/RSID information displayer", "0.1", NULL, NULL);
|
|
477 th_verbosityLevel = 0;
|
|
478
|
2
|
479 // Parse command line arguments
|
|
480 if (!th_args_process(argc, argv, optList, optListN,
|
|
481 argHandleOpt, argHandleFile, FALSE))
|
|
482 return -1;
|
|
483
|
|
484 if (optInFilename == NULL)
|
|
485 {
|
|
486 argShowHelp();
|
|
487 THERR("No filename specified.\n");
|
|
488 goto error;
|
|
489 }
|
0
|
490
|
2
|
491 // Try to open the file
|
|
492 if ((inFile = fopen(optInFilename, "rb")) == NULL)
|
|
493 {
|
|
494 THERR("Could not open file '%s'.\n", optInFilename);
|
|
495 goto error;
|
|
496 }
|
1
|
497
|
2
|
498 // Read PSID data
|
|
499 if ((ret = siReadPSIDFile(inFile, &psid)) != 0)
|
|
500 goto error;
|
|
501
|
|
502 // Output
|
|
503 fprintf(stdout, "%s\n", optInFilename);
|
|
504 siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, &psid);
|
|
505
|
|
506 ret = 0;
|
|
507
|
|
508 // Shutdown
|
|
509 error:
|
|
510 if (inFile != NULL)
|
|
511 fclose(inFile);
|
|
512
|
|
513 return ret;
|
0
|
514 }
|