1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/* grabbag - Convenience lib for various routines common to several tools
* Copyright (C) 2002-2009 Josh Coalson
* Copyright (C) 2011-2016 Xiph.Org Foundation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FLAC/assert.h"
#include "share/compat.h"
#include "share/grabbag.h"
#include "share/safe_str.h"
unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
{
return ((minutes * 60) + seconds) * 75 + frames;
}
void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
{
*frames = frame % 75;
frame /= 75;
*seconds = frame % 60;
frame /= 60;
*minutes = frame;
}
/* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
static int local__parse_int_(const char *s)
{
int ret = 0;
char c;
if(*s == '\0')
return -1;
while('\0' != (c = *s++))
if(c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
else
return -1;
return ret;
}
/* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
static FLAC__int64 local__parse_int64_(const char *s)
{
FLAC__int64 ret = 0;
char c;
if(*s == '\0')
return -1;
while('\0' != (c = *s++))
if(c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
else
return -1;
return ret;
}
/* accept minute:second:frame syntax of '[0-9]+:[0-9][0-9]?:[0-9][0-9]?', but max second of 59 and max frame of 74, e.g. 0:0:0, 123:45:67
* return sample number or <0 for error
* WATCHOUT: if sample rate is not evenly divisible by 75, the resulting sample number will be approximate
*/
static FLAC__int64 local__parse_msf_(const char *s, unsigned sample_rate)
{
FLAC__int64 ret, field;
char c;
c = *s++;
if(c >= '0' && c <= '9')
field = (c - '0');
else
return -1;
while(':' != (c = *s++)) {
if(c >= '0' && c <= '9')
field = field * 10 + (c - '0');
else
return -1;
}
ret = field * 60 * sample_rate;
c = *s++;
if(c >= '0' && c <= '9')
field = (c - '0');
else
return -1;
if(':' != (c = *s++)) {
if(c >= '0' && c <= '9') {
field = field * 10 + (c - '0');
c = *s++;
if(c != ':')
return -1;
}
else
return -1;
}
if(field >= 60)
return -1;
ret += field * sample_rate;
c = *s++;
if(c >= '0' && c <= '9')
field = (c - '0');
else
return -1;
if('\0' != (c = *s++)) {
if(c >= '0' && c <= '9') {
field = field * 10 + (c - '0');
c = *s++;
}
else
return -1;
}
if(c != '\0')
return -1;
if(field >= 75)
return -1;
ret += field * (sample_rate / 75);
return ret;
}
/* accept minute:second syntax of '[0-9]+:[0-9][0-9]?{,.[0-9]+}', but second < 60, e.g. 0:0.0, 3:5, 15:31.731
* return sample number or <0 for error
* WATCHOUT: depending on the sample rate, the resulting sample number may be approximate with fractional seconds
*/
static FLAC__int64 local__parse_ms_(const char *s, unsigned sample_rate)
{
FLAC__int64 ret, field;
double x;
char c, *end;
c = *s++;
if(c >= '0' && c <= '9')
field = (c - '0');
else
return -1;
while(':' != (c = *s++)) {
if(c >= '0' && c <= '9')
field = field * 10 + (c - '0');
else
return -1;
}
ret = field * 60 * sample_rate;
s++; /* skip the ':' */
if(strspn(s, "0123456789.") != strlen(s))
return -1;
x = strtod(s, &end);
if(*end || end == s)
return -1;
if(x < 0.0 || x >= 60.0)
return -1;
ret += (FLAC__int64)(x * sample_rate);
return ret;
}
static char *local__get_field_(char **s, FLAC__bool allow_quotes)
{
FLAC__bool has_quote = false;
char *p;
FLAC__ASSERT(0 != s);
if(0 == *s)
return 0;
/* skip leading whitespace */
while(**s && 0 != strchr(" \t\r\n", **s))
(*s)++;
if(**s == 0) {
*s = 0;
return 0;
}
if(allow_quotes && (**s == '"')) {
has_quote = true;
(*s)++;
if(**s == 0) {
*s = 0;
return 0;
}
}
p = *s;
if(has_quote) {
*s = strchr(*s, '\"');
/* if there is no matching end quote, it's an error */
if(0 == *s)
p = *s = 0;
else {
**s = '\0';
(*s)++;
}
}
else {
while(**s && 0 == strchr(" \t\r\n", **s))
(*s)++;
if(**s) {
**s = '\0';
(*s)++;
}
else
*s = 0;
}
return p;
}
static FLAC__bool local__cuesheet_parse_(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__StreamMetadata *cuesheet, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
{
char buffer[4096], *line, *field;
unsigned forced_leadout_track_num = 0;
FLAC__uint64 forced_leadout_track_offset = 0;
int in_track_num = -1, in_index_num = -1;
FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
FLAC__ASSERT(!is_cdda || sample_rate == 44100);
/* double protection */
if(is_cdda && sample_rate != 44100) {
*error_message = "CD-DA cuesheet only allowed with 44.1kHz sample rate";
return false;
}
cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
cs->is_cd = is_cdda;
while(0 != fgets(buffer, sizeof(buffer), file)) {
(*last_line_read)++;
line = buffer;
{
size_t linelen = strlen(line);
if((linelen == sizeof(buffer)-1) && line[linelen-1] != '\n') {
*error_message = "line too long";
return false;
}
}
if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
if(disc_has_catalog) {
*error_message = "found multiple CATALOG commands";
return false;
}
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
*error_message = "CATALOG is missing catalog number";
return false;
}
if(strlen(field) >= sizeof(cs->media_catalog_number)) {
*error_message = "CATALOG number is too long";
return false;
}
if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
*error_message = "CD-DA CATALOG number must be 13 decimal digits";
return false;
}
safe_strncpy(cs->media_catalog_number, field, sizeof(cs->media_catalog_number));
disc_has_catalog = true;
}
else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
if(track_has_flags) {
*error_message = "found multiple FLAGS commands";
return false;
}
if(in_track_num < 0 || in_index_num >= 0) {
*error_message = "FLAGS command must come after TRACK but before INDEX";
return false;
}
while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
if(0 == FLAC__STRCASECMP(field, "PRE"))
cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
}
track_has_flags = true;
}
else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
FLAC__int64 xx;
FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
if(in_track_num < 0) {
*error_message = "found INDEX before any TRACK";
return false;
}
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "INDEX is missing index number";
return false;
}
in_index_num = local__parse_int_(field);
if(in_index_num < 0) {
*error_message = "INDEX has invalid index number";
return false;
}
FLAC__ASSERT(cs->num_tracks > 0);
if(track->num_indices == 0) {
/* it's the first index point of the track */
if(in_index_num > 1) {
*error_message = "first INDEX number of a TRACK must be 0 or 1";
return false;
}
}
else {
if(in_index_num != track->indices[track->num_indices-1].number + 1) {
*error_message = "INDEX numbers must be sequential";
return false;
}
}
if(is_cdda && in_index_num > 99) {
*error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
return false;
}
/*@@@ search for duplicate track number? */
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "INDEX is missing an offset after the index number";
return false;
}
/* first parse as minute:second:frame format */
xx = local__parse_msf_(field, sample_rate);
if(xx < 0) {
/* CD-DA must use only MM:SS:FF format */
if(is_cdda) {
*error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
return false;
}
/* as an extension for non-CD-DA we allow MM:SS.SS or raw sample number */
xx = local__parse_ms_(field, sample_rate);
if(xx < 0) {
xx = local__parse_int64_(field);
if(xx < 0) {
*error_message = "illegal INDEX offset";
return false;
}
}
}
else if(sample_rate % 75 && xx) {
/* only sample zero is exact */
*error_message = "illegal INDEX offset (MM:SS:FF form not allowed if sample rate is not a multiple of 75)";
return false;
}
if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
*error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
return false;
}
if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
*error_message = "CD-DA INDEX offsets must increase in time";
return false;
}
/* fill in track offset if it's the first index of the track */
if(track->num_indices == 0)
track->offset = (FLAC__uint64)xx;
if(is_cdda && cs->num_tracks > 1) {
const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
*error_message = "CD-DA INDEX offsets must increase in time";
return false;
}
}
if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
*error_message = "memory allocation error";
return false;
}
track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
track->indices[track->num_indices-1].number = in_index_num;
}
else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
char *l, *r;
if(track_has_isrc) {
*error_message = "found multiple ISRC commands";
return false;
}
if(in_track_num < 0 || in_index_num >= 0) {
*error_message = "ISRC command must come after TRACK but before INDEX";
return false;
}
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
*error_message = "ISRC is missing ISRC number";
return false;
}
/* strip out dashes */
for(l = r = field; *r; r++) {
if(*r != '-')
*l++ = *r;
}
*l = '\0';
if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
*error_message = "invalid ISRC number";
return false;
}
safe_strncpy(cs->tracks[cs->num_tracks-1].isrc, field, sizeof(cs->tracks[cs->num_tracks-1].isrc));
track_has_isrc = true;
}
else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
if(cs->num_tracks > 0) {
const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
if(
prev->num_indices == 0 ||
(
is_cdda &&
(
(prev->num_indices == 1 && prev->indices[0].number != 1) ||
(prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
)
)
) {
*error_message = is_cdda?
"previous TRACK must specify at least one INDEX 01" :
"previous TRACK must specify at least one INDEX";
return false;
}
}
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "TRACK is missing track number";
return false;
}
in_track_num = local__parse_int_(field);
if(in_track_num < 0) {
*error_message = "TRACK has invalid track number";
return false;
}
if(in_track_num == 0) {
*error_message = "TRACK number must be greater than 0";
return false;
}
if(is_cdda) {
if(in_track_num > 99) {
*error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
return false;
}
}
else {
if(in_track_num == 255) {
*error_message = "TRACK number 255 is reserved for the lead-out";
return false;
}
else if(in_track_num > 255) {
*error_message = "TRACK number must be between 1 and 254, inclusive";
return false;
}
}
if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
*error_message = "CD-DA TRACK numbers must be sequential";
return false;
}
/*@@@ search for duplicate track number? */
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "TRACK is missing a track type after the track number";
return false;
}
if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
*error_message = "memory allocation error";
return false;
}
cs->tracks[cs->num_tracks-1].number = in_track_num;
cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
in_index_num = -1;
track_has_flags = false;
track_has_isrc = false;
}
else if(0 == FLAC__STRCASECMP(field, "REM")) {
if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
if(0 == strcmp(field, "FLAC__lead-in")) {
FLAC__int64 xx;
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "FLAC__lead-in is missing offset";
return false;
}
xx = local__parse_int64_(field);
if(xx < 0) {
*error_message = "illegal FLAC__lead-in offset";
return false;
}
if(is_cdda && xx % 588 != 0) {
*error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
return false;
}
cs->lead_in = (FLAC__uint64)xx;
}
else if(0 == strcmp(field, "FLAC__lead-out")) {
int track_num;
FLAC__int64 offset;
if(has_forced_leadout) {
*error_message = "multiple FLAC__lead-out commands";
return false;
}
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "FLAC__lead-out is missing track number";
return false;
}
track_num = local__parse_int_(field);
if(track_num < 0) {
*error_message = "illegal FLAC__lead-out track number";
return false;
}
forced_leadout_track_num = (unsigned)track_num;
/*@@@ search for duplicate track number? */
if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
*error_message = "FLAC__lead-out is missing offset";
return false;
}
offset = local__parse_int64_(field);
if(offset < 0) {
*error_message = "illegal FLAC__lead-out offset";
return false;
}
forced_leadout_track_offset = (FLAC__uint64)offset;
if(forced_leadout_track_offset != lead_out_offset) {
*error_message = "FLAC__lead-out offset does not match end-of-stream offset";
return false;
}
has_forced_leadout = true;
}
}
}
}
}
if(cs->num_tracks == 0) {
*error_message = "there must be at least one TRACK command";
return false;
}
else {
const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
if(
prev->num_indices == 0 ||
(
is_cdda &&
(
(prev->num_indices == 1 && prev->indices[0].number != 1) ||
(prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
)
)
) {
*error_message = is_cdda?
"previous TRACK must specify at least one INDEX 01" :
"previous TRACK must specify at least one INDEX";
return false;
}
}
if(!has_forced_leadout) {
forced_leadout_track_num = is_cdda? 170 : 255;
forced_leadout_track_offset = lead_out_offset;
}
if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
*error_message = "memory allocation error";
return false;
}
cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
if(!feof(file)) {
*error_message = "read error";
return false;
}
return true;
}
FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
{
FLAC__StreamMetadata *cuesheet;
FLAC__ASSERT(0 != file);
FLAC__ASSERT(0 != error_message);
FLAC__ASSERT(0 != last_line_read);
*last_line_read = 0;
cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
if(0 == cuesheet) {
*error_message = "memory allocation error";
return 0;
}
if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, sample_rate, is_cdda, lead_out_offset)) {
FLAC__metadata_object_delete(cuesheet);
return 0;
}
return cuesheet;
}
void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
{
const FLAC__StreamMetadata_CueSheet *cs;
unsigned track_num, index_num;
FLAC__ASSERT(0 != file);
FLAC__ASSERT(0 != cuesheet);
FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
cs = &cuesheet->data.cue_sheet;
if(*(cs->media_catalog_number))
fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
fprintf(file, "FILE %s\n", file_reference);
for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
fprintf(file, " TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
if(track->pre_emphasis)
fprintf(file, " FLAGS PRE\n");
if(*(track->isrc))
fprintf(file, " ISRC %s\n", track->isrc);
for(index_num = 0; index_num < track->num_indices; index_num++) {
const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + index_num;
fprintf(file, " INDEX %02u ", (unsigned)indx->number);
if(cs->is_cd) {
const unsigned logical_frame = (unsigned)((track->offset + indx->offset) / (44100 / 75));
unsigned m, s, f;
grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
fprintf(file, "%02u:%02u:%02u\n", m, s, f);
}
else
fprintf(file, "%" PRIu64 "\n", (track->offset + indx->offset));
}
}
fprintf(file, "REM FLAC__lead-in %" PRIu64 "\n", cs->lead_in);
fprintf(file, "REM FLAC__lead-out %u %" PRIu64 "\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);
}
|