fixes for windows builds (SF#1676822: https://sourceforge.net/tracker2/?func=detail...
[flac.git] / src / flac / utils.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2002,2003,2004,2005,2006,2007,2008  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "utils.h"
24 #include "FLAC/assert.h"
25 #include "FLAC/metadata.h"
26 #include <math.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
33
34 int flac__utils_verbosity_ = 2;
35
36 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
37 {
38         FLAC__uint64 ret = 0;
39         char c;
40
41         if(*s == '\0')
42                 return false;
43
44         while('\0' != (c = *s++))
45                 if(c >= '0' && c <= '9')
46                         ret = ret * 10 + (c - '0');
47                 else
48                         return false;
49
50         *value = ret;
51         return true;
52 }
53
54 static FLAC__bool local__parse_timecode_(const char *s, double *value)
55 {
56         double ret;
57         unsigned i;
58         char c;
59
60         /* parse [0-9][0-9]*: */
61         c = *s++;
62         if(c >= '0' && c <= '9')
63                 i = (c - '0');
64         else
65                 return false;
66         while(':' != (c = *s++)) {
67                 if(c >= '0' && c <= '9')
68                         i = i * 10 + (c - '0');
69                 else
70                         return false;
71         }
72         ret = (double)i * 60.;
73
74         /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */
75         if(strspn(s, "1234567890.,") != strlen(s))
76                 return false;
77         {
78                 const char *p = strpbrk(s, ".,");
79                 if(p && 0 != strpbrk(++p, ".,"))
80                         return false;
81         }
82         ret += atof(s);
83
84         *value = ret;
85         return true;
86 }
87
88 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index)
89 {
90         FLAC__bool got_track = false, got_index = false;
91         unsigned t = 0, i = 0;
92         char c;
93
94         while(end? s < end : *s != '\0') {
95                 c = *s++;
96                 if(c >= '0' && c <= '9') {
97                         t = t * 10 + (c - '0');
98                         got_track = true;
99                 }
100                 else if(c == '.')
101                         break;
102                 else
103                         return false;
104         }
105         while(end? s < end : *s != '\0') {
106                 c = *s++;
107                 if(c >= '0' && c <= '9') {
108                         i = i * 10 + (c - '0');
109                         got_index = true;
110                 }
111                 else
112                         return false;
113         }
114         *track = t;
115         *index = i;
116         return got_track && got_index;
117 }
118
119 /*
120  * this only works with sorted cuesheets (the spec strongly recommends but
121  * does not require sorted cuesheets).  but if it's not sorted, picking a
122  * nearest cue point has no significance.
123  */
124 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned index, FLAC__uint64 total_samples, FLAC__bool look_forward)
125 {
126         int t, i;
127         if(look_forward) {
128                 for(t = 0; t < (int)cuesheet->num_tracks; t++)
129                         for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
130                                 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= index))
131                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
132                 return total_samples;
133         }
134         else {
135                 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
136                         for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
137                                 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= index))
138                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
139                 return 0;
140         }
141 }
142
143 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
144 {
145         if(flac__utils_verbosity_ >= level) {
146                 va_list args;
147
148                 FLAC__ASSERT(0 != format);
149
150                 va_start(args, format);
151
152                 (void) vfprintf(stream, format, args);
153
154                 va_end(args);
155
156 #ifdef _MSC_VER
157                 if(stream == stderr)
158                         fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
159 #endif
160         }
161 }
162
163 #ifdef FLAC__VALGRIND_TESTING
164 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
165 {
166         size_t ret = fwrite(ptr, size, nmemb, stream);
167         if(!ferror(stream))
168                 fflush(stream);
169         return ret;
170 }
171 #endif
172
173 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
174 {
175         FLAC__uint64 val;
176         FLAC__bool is_negative = false;
177
178         FLAC__ASSERT(0 != spec);
179
180         spec->is_relative = false;
181         spec->value_is_samples = true;
182         spec->value.samples = 0;
183
184         if(0 != s) {
185                 if(s[0] == '-') {
186                         is_negative = true;
187                         spec->is_relative = true;
188                         s++;
189                 }
190                 else if(s[0] == '+') {
191                         spec->is_relative = true;
192                         s++;
193                 }
194
195                 if(local__parse_uint64_(s, &val)) {
196                         spec->value_is_samples = true;
197                         spec->value.samples = (FLAC__int64)val;
198                         if(is_negative)
199                                 spec->value.samples = -(spec->value.samples);
200                 }
201                 else {
202                         double d;
203                         if(!local__parse_timecode_(s, &d))
204                                 return false;
205                         spec->value_is_samples = false;
206                         spec->value.seconds = d;
207                         if(is_negative)
208                                 spec->value.seconds = -(spec->value.seconds);
209                 }
210         }
211
212         return true;
213 }
214
215 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
216 {
217         FLAC__ASSERT(0 != spec);
218         if(!spec->value_is_samples) {
219                 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
220                 spec->value_is_samples = true;
221         }
222 }
223
224 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
225 {
226         const char *start = s, *end = 0;
227
228         FLAC__ASSERT(0 != spec);
229
230         spec->has_start_point = spec->has_end_point = false;
231
232         s = strchr(s, '-');
233
234         if(0 != s) {
235                 if(s == start)
236                         start = 0;
237                 end = s+1;
238                 if(*end == '\0')
239                         end = 0;
240         }
241
242         if(start) {
243                 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
244                         return false;
245                 spec->has_start_point = true;
246         }
247
248         if(end) {
249                 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
250                         return false;
251                 spec->has_end_point = true;
252         }
253
254         return true;
255 }
256
257 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)
258 {
259         FLAC__ASSERT(0 != cue_spec);
260         FLAC__ASSERT(0 != cuesheet);
261         FLAC__ASSERT(0 != total_samples);
262         FLAC__ASSERT(0 != skip_spec);
263         FLAC__ASSERT(0 != until_spec);
264
265         skip_spec->is_relative = false;
266         skip_spec->value_is_samples = true;
267
268         until_spec->is_relative = false;
269         until_spec->value_is_samples = true;
270
271         if(cue_spec->has_start_point)
272                 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
273         else
274                 skip_spec->value.samples = 0;
275
276         if(cue_spec->has_end_point)
277                 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
278         else
279                 until_spec->value.samples = total_samples;
280 }
281
282 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
283 {
284         FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
285         char tag[128];
286
287         FLAC__ASSERT(object);
288         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
289         FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */
290         entry.entry = (FLAC__byte*)tag;
291 #if defined _MSC_VER || defined __MINGW32__
292         if((entry.length = _snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag))
293 #else
294         if((entry.length = snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag))
295 #endif
296                 return false;
297         if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
298                 return false;
299         return true;
300 }
301
302 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
303 {
304         int offset;
305         unsigned val;
306         char *p;
307         FLAC__ASSERT(object);
308         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
309         if(0 > (offset = FLAC__metadata_object_vorbiscomment_find_entry_from(object, /*offset=*/0, CHANNEL_MASK_TAG)))
310                 return false;
311         if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
312                 return false;
313         if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
314                 return false;
315         if(strncmp(p, "=0x", 3))
316                 return false;
317         if(sscanf(p+3, "%x", &val) != 1)
318                 return false;
319         *channel_mask = val;
320         return true;
321 }