xref: /aosp_15_r20/external/flac/src/metaflac/operations_shorthand_cuesheet.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001-2009  Josh Coalson
3  * Copyright (C) 2011-2023  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 
24 #include <errno.h>
25 #include <string.h>
26 #include "options.h"
27 #include "utils.h"
28 #include "FLAC/assert.h"
29 #include "share/grabbag.h"
30 #include "share/compat.h"
31 #include "operations_shorthand.h"
32 
33 static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
34 static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
35 
do_shorthand_operation__cuesheet(const char * filename,FLAC__Metadata_Chain * chain,const Operation * operation,FLAC__bool * needs_write)36 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
37 {
38 	FLAC__bool ok = true;
39 	FLAC__StreamMetadata *cuesheet = 0;
40 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
41 	FLAC__uint64 lead_out_offset = 0;
42 	FLAC__bool is_cdda = false;
43 	unsigned sample_rate = 0;
44 
45 	if(0 == iterator)
46 		die("out of memory allocating iterator");
47 
48 	FLAC__metadata_iterator_init(iterator, chain);
49 
50 	do {
51 		FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
52 		if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
53 			lead_out_offset = block->data.stream_info.total_samples;
54 			if(lead_out_offset == 0) {
55 				flac_fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
56 				FLAC__metadata_iterator_delete(iterator);
57 				return false;
58 			}
59 			sample_rate = block->data.stream_info.sample_rate;
60 			is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (sample_rate == 44100);
61 		}
62 		else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
63 			cuesheet = block;
64 	} while(FLAC__metadata_iterator_next(iterator));
65 
66 	if(lead_out_offset == 0) {
67 		flac_fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
68 		FLAC__metadata_iterator_delete(iterator);
69 		return false;
70 	}
71 
72 	if(sample_rate == 0) {
73 		flac_fprintf(stderr, "%s: ERROR: cannot parse cuesheet when sample rate is unknown\n", filename);
74 		FLAC__metadata_iterator_delete(iterator);
75 		return false;
76 	}
77 
78 	switch(operation->type) {
79 		case OP__IMPORT_CUESHEET_FROM:
80 			if(0 != cuesheet) {
81 				flac_fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
82 				ok = false;
83 			}
84 			else {
85 				ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, sample_rate, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
86 				if(ok) {
87 					/* append CUESHEET block */
88 					while(FLAC__metadata_iterator_next(iterator))
89 						;
90 					if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
91 						print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
92 						FLAC__metadata_object_delete(cuesheet);
93 						ok = false;
94 					}
95 				}
96 			}
97 			break;
98 		case OP__EXPORT_CUESHEET_TO:
99 			if(0 == cuesheet) {
100 				flac_fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
101 				ok = false;
102 			}
103 			else
104 				ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
105 			break;
106 		default:
107 			ok = false;
108 			FLAC__ASSERT(0);
109 			break;
110 	};
111 
112 	FLAC__metadata_iterator_delete(iterator);
113 	return ok;
114 }
115 
116 /*
117  * local routines
118  */
119 
import_cs_from(const char * filename,FLAC__StreamMetadata ** cuesheet,const char * cs_filename,FLAC__bool * needs_write,FLAC__uint64 lead_out_offset,unsigned sample_rate,FLAC__bool is_cdda,Argument_AddSeekpoint * add_seekpoint_link)120 FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
121 {
122 	FILE *f;
123 	const char *error_message;
124 	char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
125 	unsigned last_line_read;
126 
127 	if(0 == cs_filename || strlen(cs_filename) == 0) {
128 		flac_fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
129 		return false;
130 	}
131 	if(0 == strcmp(cs_filename, "-"))
132 		f = stdin;
133 	else
134 		f = flac_fopen(cs_filename, "r");
135 
136 	if(0 == f) {
137 		flac_fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno));
138 		return false;
139 	}
140 
141 	*cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset);
142 
143 	if(f != stdin)
144 		fclose(f);
145 
146 	if(0 == *cuesheet) {
147 		flac_fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
148 		return false;
149 	}
150 
151 	if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
152 		flac_fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
153 		FLAC__metadata_object_delete(*cuesheet);
154 		return false;
155 	}
156 
157 	/* if we're expecting CDDA, warn about non-compliance */
158 	if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
159 		flac_fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
160 		(*cuesheet)->data.cue_sheet.is_cd = false;
161 	}
162 
163 	/* add seekpoints for each index point if required */
164 	if(0 != seekpoint_specification) {
165 		char spec[128];
166 		unsigned track, indx;
167 		const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
168 		if(0 == *seekpoint_specification)
169 			*seekpoint_specification = local_strdup("");
170 		for(track = 0; track < cs->num_tracks; track++) {
171 			const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
172 			for(indx = 0; indx < tr->num_indices; indx++) {
173 				flac_snprintf(spec, sizeof (spec), "%" PRIu64 ";", (tr->offset + tr->indices[indx].offset));
174 				local_strcat(seekpoint_specification, spec);
175 			}
176 		}
177 	}
178 
179 	*needs_write = true;
180 	return true;
181 }
182 
export_cs_to(const char * filename,const FLAC__StreamMetadata * cuesheet,const char * cs_filename)183 FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
184 {
185 	FILE *f;
186 	char *ref = 0;
187 	size_t reflen;
188 
189 	if(0 == cs_filename || strlen(cs_filename) == 0) {
190 		flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
191 		return false;
192 	}
193 	if(0 == strcmp(cs_filename, "-"))
194 		f = stdout;
195 	else
196 		f = flac_fopen(cs_filename, "w");
197 
198 	if(0 == f) {
199 		flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno));
200 		return false;
201 	}
202 
203 	reflen = strlen(filename) + 7 + 1;
204 	if(0 == (ref = malloc(reflen))) {
205 		flac_fprintf(stderr, "%s: ERROR: allocating memory\n", filename);
206 		if(f != stdout)
207 			fclose(f);
208 		return false;
209 	}
210 
211 	flac_snprintf(ref, reflen, "\"%s\" FLAC", filename);
212 
213 	grabbag__cuesheet_emit(f, cuesheet, ref);
214 
215 	free(ref);
216 
217 	if(f != stdout)
218 		fclose(f);
219 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
220 	/* Delete output file when fuzzing */
221 	if(f != stdout)
222 		flac_unlink(cs_filename);
223 #endif
224 
225 	return true;
226 }
227