• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2022  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 <math.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "utils.h"
30 #include "FLAC/assert.h"
31 #include "FLAC/metadata.h"
32 #include "share/compat.h"
33 #ifndef _WIN32
34 #ifndef _XOPEN_SOURCE
35 #define _XOPEN_SOURCE
36 #endif
37 #include <wchar.h>
38 #ifdef HAVE_TERMIOS_H
39 # include <termios.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
43 #endif
44 #endif
45 
46 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
47 
48 int flac__utils_verbosity_ = 2;
49 
local__parse_uint64_(const char * s,FLAC__uint64 * value)50 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
51 {
52 	FLAC__uint64 ret = 0;
53 	char c;
54 
55 	if(*s == '\0')
56 		return false;
57 
58 	while('\0' != (c = *s++))
59 		if(c >= '0' && c <= '9')
60 			ret = ret * 10 + (c - '0');
61 		else
62 			return false;
63 
64 	*value = ret;
65 	return true;
66 }
67 
local__parse_timecode_(const char * s,double * value)68 static FLAC__bool local__parse_timecode_(const char *s, double *value)
69 {
70 	double ret;
71 	uint32_t i;
72 	char c, *endptr;
73 
74 	/* parse [0-9][0-9]*: */
75 	c = *s++;
76 	if(c >= '0' && c <= '9')
77 		i = (c - '0');
78 	else
79 		return false;
80 	while(':' != (c = *s++)) {
81 		if(c >= '0' && c <= '9')
82 			i = i * 10 + (c - '0');
83 		else
84 			return false;
85 	}
86 	ret = (double)i * 60.;
87 
88 	/* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */
89 	if(strspn(s, "1234567890.,") != strlen(s))
90 		return false;
91 	ret += strtod(s, &endptr);
92 	if (endptr == s || *endptr)
93 		return false;
94 
95 	*value = ret;
96 	return true;
97 }
98 
local__parse_cue_(const char * s,const char * end,uint32_t * track,uint32_t * indx)99 static FLAC__bool local__parse_cue_(const char *s, const char *end, uint32_t *track, uint32_t *indx)
100 {
101 	FLAC__bool got_track = false, got_index = false;
102 	uint32_t t = 0, i = 0;
103 	char c;
104 
105 	while(end? s < end : *s != '\0') {
106 		c = *s++;
107 		if(c >= '0' && c <= '9') {
108 			t = t * 10 + (c - '0');
109 			got_track = true;
110 		}
111 		else if(c == '.')
112 			break;
113 		else
114 			return false;
115 	}
116 	while(end? s < end : *s != '\0') {
117 		c = *s++;
118 		if(c >= '0' && c <= '9') {
119 			i = i * 10 + (c - '0');
120 			got_index = true;
121 		}
122 		else
123 			return false;
124 	}
125 	*track = t;
126 	*indx = i;
127 	return got_track && got_index;
128 }
129 
130 /*
131  * this only works with sorted cuesheets (the spec strongly recommends but
132  * does not require sorted cuesheets).  but if it's not sorted, picking a
133  * nearest cue point has no significance.
134  */
local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet * cuesheet,uint32_t track,uint32_t indx,FLAC__uint64 total_samples,FLAC__bool look_forward)135 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, uint32_t track, uint32_t indx, FLAC__uint64 total_samples, FLAC__bool look_forward)
136 {
137 	int t, i;
138 	if(look_forward) {
139 		for(t = 0; t < (int)cuesheet->num_tracks; t++)
140 			for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
141 				if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= indx))
142 					return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
143 		return total_samples;
144 	}
145 	else {
146 		for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
147 			for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
148 				if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= indx))
149 					return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
150 		return 0;
151 	}
152 }
153 
flac__utils_printf(FILE * stream,int level,const char * format,...)154 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
155 {
156 	if(flac__utils_verbosity_ >= level) {
157 		va_list args;
158 
159 		FLAC__ASSERT(0 != format);
160 
161 		va_start(args, format);
162 
163 		(void) flac_vfprintf(stream, format, args);
164 
165 		va_end(args);
166 
167 #ifdef _MSC_VER
168 		if(stream == stderr)
169 			fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
170 #endif
171 	}
172 }
173 
174 /* variables and functions for console status output */
175 static FLAC__bool is_name_printed;
176 static int stats_char_count = 0;
177 static int console_width;
178 static int console_chars_left;
179 
get_console_width(void)180 int get_console_width(void)
181 {
182 	int width = 0;
183 #if defined _WIN32
184 	width = win_get_console_width();
185 #elif defined __EMX__
186 	int s[2];
187 	_scrsize (s);
188 	width = s[0];
189 #elif defined TIOCGWINSZ
190 	struct winsize w;
191 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
192 		width = w.ws_col;
193 #endif
194 	if (width <= 0)
195 		width = 80;
196 	return width;
197 }
198 
strlen_console(const char * text)199 size_t strlen_console(const char *text)
200 {
201 #ifdef _WIN32
202 	return strlen_utf8(text);
203 #elif defined(__DJGPP__) /* workaround for DJGPP missing wcswidth() */
204 	return strlen(text);
205 #else
206 	size_t len;
207 	wchar_t *wtmp;
208 
209 	len = strlen(text)+1;
210 	wtmp = (wchar_t *)malloc(len*sizeof(wchar_t));
211 	if (wtmp == NULL) return len-1;
212 	mbstowcs(wtmp, text, len);
213 	len = wcswidth(wtmp, len);
214 	free(wtmp);
215 
216 	return len;
217 #endif
218 }
219 
stats_new_file(void)220 void stats_new_file(void)
221 {
222 	is_name_printed = false;
223 }
224 
stats_clear(void)225 void stats_clear(void)
226 {
227 	while (stats_char_count > 0 && stats_char_count--)
228 		fprintf(stderr, "\b");
229 }
230 
stats_print_name(int level,const char * name)231 void stats_print_name(int level, const char *name)
232 {
233 	int len;
234 
235 	if (flac__utils_verbosity_ >= level) {
236 		stats_clear();
237 		if(is_name_printed) return;
238 
239 		console_width = get_console_width();
240 		len = strlen_console(name)+2;
241 		console_chars_left = console_width  - (len % console_width);
242 		flac_fprintf(stderr, "%s: ", name);
243 		is_name_printed = true;
244 	}
245 }
246 
stats_print_info(int level,const char * format,...)247 void stats_print_info(int level, const char *format, ...)
248 {
249 	char tmp[80];
250 	int len, clear_len;
251 
252 	if (flac__utils_verbosity_ >= level) {
253 		va_list args;
254 		va_start(args, format);
255 		len = flac_vsnprintf(tmp, sizeof(tmp), format, args);
256 		va_end(args);
257 		stats_clear();
258 		if (len >= console_chars_left) {
259 			clear_len = console_chars_left;
260 			while (clear_len > 0 && clear_len--) fprintf(stderr, " ");
261 			fprintf(stderr, "\n");
262 			console_chars_left = console_width;
263 		}
264 		stats_char_count = fprintf(stderr, "%s", tmp);
265 		fflush(stderr);
266 	}
267 }
268 
269 #ifdef FLAC__VALGRIND_TESTING
flac__utils_fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream)270 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
271 {
272 	size_t ret = fwrite(ptr, size, nmemb, stream);
273 	if(!ferror(stream))
274 		fflush(stream);
275 	return ret;
276 }
277 #endif
278 
flac__utils_parse_skip_until_specification(const char * s,utils__SkipUntilSpecification * spec)279 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
280 {
281 	FLAC__uint64 val;
282 	FLAC__bool is_negative = false;
283 
284 	FLAC__ASSERT(0 != spec);
285 
286 	spec->is_relative = false;
287 	spec->value_is_samples = true;
288 	spec->value.samples = 0;
289 
290 	if(0 != s) {
291 		if(s[0] == '-') {
292 			is_negative = true;
293 			spec->is_relative = true;
294 			s++;
295 		}
296 		else if(s[0] == '+') {
297 			spec->is_relative = true;
298 			s++;
299 		}
300 
301 		if(local__parse_uint64_(s, &val)) {
302 			spec->value_is_samples = true;
303 			spec->value.samples = (FLAC__int64)val;
304 			if(is_negative)
305 				spec->value.samples = -(spec->value.samples);
306 		}
307 		else {
308 			double d;
309 			if(!local__parse_timecode_(s, &d))
310 				return false;
311 			spec->value_is_samples = false;
312 			spec->value.seconds = d;
313 			if(is_negative)
314 				spec->value.seconds = -(spec->value.seconds);
315 		}
316 	}
317 
318 	return true;
319 }
320 
flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification * spec,uint32_t sample_rate)321 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, uint32_t sample_rate)
322 {
323 	FLAC__ASSERT(0 != spec);
324 	if(!spec->value_is_samples) {
325 		spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
326 		spec->value_is_samples = true;
327 	}
328 }
329 
flac__utils_parse_cue_specification(const char * s,utils__CueSpecification * spec)330 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
331 {
332 	const char *start = s, *end = 0;
333 
334 	FLAC__ASSERT(0 != spec);
335 
336 	spec->has_start_point = spec->has_end_point = false;
337 
338 	s = strchr(s, '-');
339 
340 	if(0 != s) {
341 		if(s == start)
342 			start = 0;
343 		end = s+1;
344 		if(*end == '\0')
345 			end = 0;
346 	}
347 
348 	if(start) {
349 		if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
350 			return false;
351 		spec->has_start_point = true;
352 	}
353 
354 	if(end) {
355 		if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
356 			return false;
357 		spec->has_end_point = true;
358 	}
359 
360 	return true;
361 }
362 
flac__utils_canonicalize_cue_specification(const utils__CueSpecification * cue_spec,const FLAC__StreamMetadata_CueSheet * cuesheet,FLAC__uint64 total_samples,utils__SkipUntilSpecification * skip_spec,utils__SkipUntilSpecification * until_spec)363 void flac__utils_canonicalize_cue_specification(const utils__CueSpecification *cue_spec, const FLAC__StreamMetadata_CueSheet *cuesheet, FLAC__uint64 total_samples, utils__SkipUntilSpecification *skip_spec, utils__SkipUntilSpecification *until_spec)
364 {
365 	FLAC__ASSERT(0 != cue_spec);
366 	FLAC__ASSERT(0 != cuesheet);
367 	FLAC__ASSERT(0 != total_samples);
368 	FLAC__ASSERT(0 != skip_spec);
369 	FLAC__ASSERT(0 != until_spec);
370 
371 	skip_spec->is_relative = false;
372 	skip_spec->value_is_samples = true;
373 
374 	until_spec->is_relative = false;
375 	until_spec->value_is_samples = true;
376 
377 	if(cue_spec->has_start_point)
378 		skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
379 	else
380 		skip_spec->value.samples = 0;
381 
382 	if(cue_spec->has_end_point)
383 		until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
384 	else
385 		until_spec->value.samples = total_samples;
386 }
387 
flac__utils_set_channel_mask_tag(FLAC__StreamMetadata * object,FLAC__uint32 channel_mask)388 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
389 {
390 	FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
391 	char tag[128];
392 
393 	FLAC__ASSERT(object);
394 	FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
395 	FLAC__ASSERT(strlen(CHANNEL_MASK_TAG)+1+2+16+1 <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */
396 	entry.entry = (FLAC__byte*)tag;
397 	if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (uint32_t)channel_mask)) >= sizeof(tag))
398 		return false;
399 	if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
400 		return false;
401 	return true;
402 }
403 
flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata * object,FLAC__uint32 * channel_mask)404 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
405 {
406 	int offset;
407 	uint32_t val;
408 	char *p;
409 	FLAC__ASSERT(object);
410 	FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
411 	if(0 > (offset = FLAC__metadata_object_vorbiscomment_find_entry_from(object, /*offset=*/0, CHANNEL_MASK_TAG)))
412 		return false;
413 	if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
414 		return false;
415 	if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
416 		return false;
417 	if(FLAC__STRNCASECMP(p, "=0x", 3))
418 		return false;
419 	if(sscanf(p+3, "%x", &val) != 1)
420 		return false;
421 	*channel_mask = val;
422 	return true;
423 }
424