minor comments
[flac.git] / src / libOggFLAC / file_decoder.c
1 /* libOggFLAC - Free Lossless Audio Codec + Ogg library
2  * Copyright (C) 2002,2003,2004  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h> /* for malloc() */
34 #include <string.h> /* for strcmp() */
35 #include <sys/stat.h> /* for stat() */
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
39 #elif defined __CYGWIN__
40 #include <io.h> /* for setmode(), O_BINARY */
41 #include <fcntl.h> /* for _O_BINARY */
42 #endif
43 #include "FLAC/assert.h"
44 #include "protected/file_decoder.h"
45 #include "protected/seekable_stream_decoder.h"
46
47 /***********************************************************************
48  *
49  * Private class method prototypes
50  *
51  ***********************************************************************/
52
53 static void set_defaults_(OggFLAC__FileDecoder *decoder);
54 static FILE *get_binary_stdin_();
55 static FLAC__SeekableStreamDecoderReadStatus read_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
56 static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
57 static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
58 static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
59 static FLAC__bool eof_callback_(const OggFLAC__SeekableStreamDecoder *decoder, void *client_data);
60 static FLAC__StreamDecoderWriteStatus write_callback_(const OggFLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
61 static void metadata_callback_(const OggFLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
62 static void error_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
63
64 /***********************************************************************
65  *
66  * Private class data
67  *
68  ***********************************************************************/
69
70 typedef struct OggFLAC__FileDecoderPrivate {
71         OggFLAC__FileDecoderWriteCallback write_callback;
72         OggFLAC__FileDecoderMetadataCallback metadata_callback;
73         OggFLAC__FileDecoderErrorCallback error_callback;
74         void *client_data;
75         FILE *file;
76         char *filename; /* == NULL if stdin */
77         OggFLAC__SeekableStreamDecoder *seekable_stream_decoder;
78 } OggFLAC__FileDecoderPrivate;
79
80 /***********************************************************************
81  *
82  * Public static class data
83  *
84  ***********************************************************************/
85
86 OggFLAC_API const char * const OggFLAC__FileDecoderStateString[] = {
87         "OggFLAC__FILE_DECODER_OK",
88         "OggFLAC__FILE_DECODER_END_OF_FILE",
89         "OggFLAC__FILE_DECODER_ERROR_OPENING_FILE",
90         "OggFLAC__FILE_DECODER_SEEK_ERROR",
91         "OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR",
92         "OggFLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
93         "OggFLAC__FILE_DECODER_ALREADY_INITIALIZED",
94         "OggFLAC__FILE_DECODER_INVALID_CALLBACK",
95         "OggFLAC__FILE_DECODER_UNINITIALIZED"
96 };
97
98 /***********************************************************************
99  *
100  * Class constructor/destructor
101  *
102  ***********************************************************************/
103
104 OggFLAC_API OggFLAC__FileDecoder *OggFLAC__file_decoder_new()
105 {
106         OggFLAC__FileDecoder *decoder;
107
108         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
109
110         decoder = (OggFLAC__FileDecoder*)calloc(1, sizeof(OggFLAC__FileDecoder));
111         if(decoder == 0) {
112                 return 0;
113         }
114
115         decoder->protected_ = (OggFLAC__FileDecoderProtected*)calloc(1, sizeof(OggFLAC__FileDecoderProtected));
116         if(decoder->protected_ == 0) {
117                 free(decoder);
118                 return 0;
119         }
120
121         decoder->private_ = (OggFLAC__FileDecoderPrivate*)calloc(1, sizeof(OggFLAC__FileDecoderPrivate));
122         if(decoder->private_ == 0) {
123                 free(decoder->protected_);
124                 free(decoder);
125                 return 0;
126         }
127
128         decoder->private_->seekable_stream_decoder = OggFLAC__seekable_stream_decoder_new();
129         if(0 == decoder->private_->seekable_stream_decoder) {
130                 free(decoder->private_);
131                 free(decoder->protected_);
132                 free(decoder);
133                 return 0;
134         }
135
136         decoder->private_->file = 0;
137
138         set_defaults_(decoder);
139
140         decoder->protected_->state = OggFLAC__FILE_DECODER_UNINITIALIZED;
141
142         return decoder;
143 }
144
145 OggFLAC_API void OggFLAC__file_decoder_delete(OggFLAC__FileDecoder *decoder)
146 {
147         FLAC__ASSERT(0 != decoder);
148         FLAC__ASSERT(0 != decoder->protected_);
149         FLAC__ASSERT(0 != decoder->private_);
150         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
151
152         (void)OggFLAC__file_decoder_finish(decoder);
153
154         OggFLAC__seekable_stream_decoder_delete(decoder->private_->seekable_stream_decoder);
155
156         free(decoder->private_);
157         free(decoder->protected_);
158         free(decoder);
159 }
160
161 /***********************************************************************
162  *
163  * Public class methods
164  *
165  ***********************************************************************/
166
167 OggFLAC_API OggFLAC__FileDecoderState OggFLAC__file_decoder_init(OggFLAC__FileDecoder *decoder)
168 {
169         FLAC__ASSERT(0 != decoder);
170
171         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
172                 return decoder->protected_->state = OggFLAC__FILE_DECODER_ALREADY_INITIALIZED;
173
174         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
175                 return decoder->protected_->state = OggFLAC__FILE_DECODER_INVALID_CALLBACK;
176
177         if(0 == decoder->private_->filename)
178                 decoder->private_->file = get_binary_stdin_();
179         else
180                 decoder->private_->file = fopen(decoder->private_->filename, "rb");
181
182         if(decoder->private_->file == 0)
183                 return decoder->protected_->state = OggFLAC__FILE_DECODER_ERROR_OPENING_FILE;
184
185         OggFLAC__seekable_stream_decoder_set_read_callback(decoder->private_->seekable_stream_decoder, read_callback_);
186         OggFLAC__seekable_stream_decoder_set_seek_callback(decoder->private_->seekable_stream_decoder, seek_callback_);
187         OggFLAC__seekable_stream_decoder_set_tell_callback(decoder->private_->seekable_stream_decoder, tell_callback_);
188         OggFLAC__seekable_stream_decoder_set_length_callback(decoder->private_->seekable_stream_decoder, length_callback_);
189         OggFLAC__seekable_stream_decoder_set_eof_callback(decoder->private_->seekable_stream_decoder, eof_callback_);
190         OggFLAC__seekable_stream_decoder_set_write_callback(decoder->private_->seekable_stream_decoder, write_callback_);
191         OggFLAC__seekable_stream_decoder_set_metadata_callback(decoder->private_->seekable_stream_decoder, metadata_callback_);
192         OggFLAC__seekable_stream_decoder_set_error_callback(decoder->private_->seekable_stream_decoder, error_callback_);
193         OggFLAC__seekable_stream_decoder_set_client_data(decoder->private_->seekable_stream_decoder, decoder);
194
195         if(OggFLAC__seekable_stream_decoder_init(decoder->private_->seekable_stream_decoder) != OggFLAC__SEEKABLE_STREAM_DECODER_OK)
196                 return decoder->protected_->state = OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
197
198         return decoder->protected_->state = OggFLAC__FILE_DECODER_OK;
199 }
200
201 OggFLAC_API FLAC__bool OggFLAC__file_decoder_finish(OggFLAC__FileDecoder *decoder)
202 {
203         FLAC__ASSERT(0 != decoder);
204
205         if(decoder->protected_->state == OggFLAC__FILE_DECODER_UNINITIALIZED)
206                 return true;
207
208         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
209
210         if(0 != decoder->private_->file && decoder->private_->file != stdin) {
211                 fclose(decoder->private_->file);
212                 decoder->private_->file = 0;
213         }
214
215         if(0 != decoder->private_->filename) {
216                 free(decoder->private_->filename);
217                 decoder->private_->filename = 0;
218         }
219
220         set_defaults_(decoder);
221
222         decoder->protected_->state = OggFLAC__FILE_DECODER_UNINITIALIZED;
223
224         return OggFLAC__seekable_stream_decoder_finish(decoder->private_->seekable_stream_decoder);
225 }
226
227 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_md5_checking(OggFLAC__FileDecoder *decoder, FLAC__bool value)
228 {
229         FLAC__ASSERT(0 != decoder);
230         FLAC__ASSERT(0 != decoder->private_);
231         FLAC__ASSERT(0 != decoder->protected_);
232         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
233         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
234                 return false;
235         return OggFLAC__seekable_stream_decoder_set_md5_checking(decoder->private_->seekable_stream_decoder, value);
236 }
237
238 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_filename(OggFLAC__FileDecoder *decoder, const char *value)
239 {
240         FLAC__ASSERT(0 != decoder);
241         FLAC__ASSERT(0 != decoder->private_);
242         FLAC__ASSERT(0 != decoder->protected_);
243         FLAC__ASSERT(0 != value);
244         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
245                 return false;
246         if(0 != decoder->private_->filename) {
247                 free(decoder->private_->filename);
248                 decoder->private_->filename = 0;
249         }
250         if(0 != strcmp(value, "-")) {
251                 if(0 == (decoder->private_->filename = (char*)malloc(strlen(value)+1))) {
252                         decoder->protected_->state = OggFLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
253                         return false;
254                 }
255                 strcpy(decoder->private_->filename, value);
256         }
257         return true;
258 }
259
260 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_write_callback(OggFLAC__FileDecoder *decoder, OggFLAC__FileDecoderWriteCallback value)
261 {
262         FLAC__ASSERT(0 != decoder);
263         FLAC__ASSERT(0 != decoder->private_);
264         FLAC__ASSERT(0 != decoder->protected_);
265         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
266                 return false;
267         decoder->private_->write_callback = value;
268         return true;
269 }
270
271 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_callback(OggFLAC__FileDecoder *decoder, OggFLAC__FileDecoderMetadataCallback value)
272 {
273         FLAC__ASSERT(0 != decoder);
274         FLAC__ASSERT(0 != decoder->private_);
275         FLAC__ASSERT(0 != decoder->protected_);
276         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
277                 return false;
278         decoder->private_->metadata_callback = value;
279         return true;
280 }
281
282 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_error_callback(OggFLAC__FileDecoder *decoder, OggFLAC__FileDecoderErrorCallback value)
283 {
284         FLAC__ASSERT(0 != decoder);
285         FLAC__ASSERT(0 != decoder->private_);
286         FLAC__ASSERT(0 != decoder->protected_);
287         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
288                 return false;
289         decoder->private_->error_callback = value;
290         return true;
291 }
292
293 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_client_data(OggFLAC__FileDecoder *decoder, void *value)
294 {
295         FLAC__ASSERT(0 != decoder);
296         FLAC__ASSERT(0 != decoder->private_);
297         FLAC__ASSERT(0 != decoder->protected_);
298         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
299                 return false;
300         decoder->private_->client_data = value;
301         return true;
302 }
303
304 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_serial_number(OggFLAC__FileDecoder *decoder, long value)
305 {
306         FLAC__ASSERT(0 != decoder);
307         FLAC__ASSERT(0 != decoder->private_);
308         FLAC__ASSERT(0 != decoder->protected_);
309         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
310                 return false;
311         OggFLAC__seekable_stream_decoder_set_serial_number(decoder->private_->seekable_stream_decoder, value);
312         return true;
313 }
314
315 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_respond(OggFLAC__FileDecoder *decoder, FLAC__MetadataType type)
316 {
317         FLAC__ASSERT(0 != decoder);
318         FLAC__ASSERT(0 != decoder->private_);
319         FLAC__ASSERT(0 != decoder->protected_);
320         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
321         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
322                 return false;
323         return OggFLAC__seekable_stream_decoder_set_metadata_respond(decoder->private_->seekable_stream_decoder, type);
324 }
325
326 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_respond_application(OggFLAC__FileDecoder *decoder, const FLAC__byte id[4])
327 {
328         FLAC__ASSERT(0 != decoder);
329         FLAC__ASSERT(0 != decoder->private_);
330         FLAC__ASSERT(0 != decoder->protected_);
331         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
332         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
333                 return false;
334         return OggFLAC__seekable_stream_decoder_set_metadata_respond_application(decoder->private_->seekable_stream_decoder, id);
335 }
336
337 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_respond_all(OggFLAC__FileDecoder *decoder)
338 {
339         FLAC__ASSERT(0 != decoder);
340         FLAC__ASSERT(0 != decoder->private_);
341         FLAC__ASSERT(0 != decoder->protected_);
342         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
343         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
344                 return false;
345         return OggFLAC__seekable_stream_decoder_set_metadata_respond_all(decoder->private_->seekable_stream_decoder);
346 }
347
348 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_ignore(OggFLAC__FileDecoder *decoder, FLAC__MetadataType type)
349 {
350         FLAC__ASSERT(0 != decoder);
351         FLAC__ASSERT(0 != decoder->private_);
352         FLAC__ASSERT(0 != decoder->protected_);
353         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
354         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
355                 return false;
356         return OggFLAC__seekable_stream_decoder_set_metadata_ignore(decoder->private_->seekable_stream_decoder, type);
357 }
358
359 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_ignore_application(OggFLAC__FileDecoder *decoder, const FLAC__byte id[4])
360 {
361         FLAC__ASSERT(0 != decoder);
362         FLAC__ASSERT(0 != decoder->private_);
363         FLAC__ASSERT(0 != decoder->protected_);
364         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
365         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
366                 return false;
367         return OggFLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder->private_->seekable_stream_decoder, id);
368 }
369
370 OggFLAC_API FLAC__bool OggFLAC__file_decoder_set_metadata_ignore_all(OggFLAC__FileDecoder *decoder)
371 {
372         FLAC__ASSERT(0 != decoder);
373         FLAC__ASSERT(0 != decoder->private_);
374         FLAC__ASSERT(0 != decoder->protected_);
375         FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
376         if(decoder->protected_->state != OggFLAC__FILE_DECODER_UNINITIALIZED)
377                 return false;
378         return OggFLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder->private_->seekable_stream_decoder);
379 }
380
381 OggFLAC_API OggFLAC__FileDecoderState OggFLAC__file_decoder_get_state(const OggFLAC__FileDecoder *decoder)
382 {
383         FLAC__ASSERT(0 != decoder);
384         FLAC__ASSERT(0 != decoder->protected_);
385         return decoder->protected_->state;
386 }
387
388 OggFLAC_API OggFLAC__SeekableStreamDecoderState OggFLAC__file_decoder_get_seekable_stream_decoder_state(const OggFLAC__FileDecoder *decoder)
389 {
390         FLAC__ASSERT(0 != decoder);
391         FLAC__ASSERT(0 != decoder->private_);
392         return OggFLAC__seekable_stream_decoder_get_state(decoder->private_->seekable_stream_decoder);
393 }
394
395 OggFLAC_API FLAC__SeekableStreamDecoderState OggFLAC__file_decoder_get_FLAC_seekable_stream_decoder_state(const OggFLAC__FileDecoder *decoder)
396 {
397         FLAC__ASSERT(0 != decoder);
398         FLAC__ASSERT(0 != decoder->private_);
399         return OggFLAC__seekable_stream_decoder_get_FLAC_seekable_stream_decoder_state(decoder->private_->seekable_stream_decoder);
400 }
401
402 OggFLAC_API FLAC__StreamDecoderState OggFLAC__file_decoder_get_FLAC_stream_decoder_state(const OggFLAC__FileDecoder *decoder)
403 {
404         FLAC__ASSERT(0 != decoder);
405         FLAC__ASSERT(0 != decoder->private_);
406         return OggFLAC__seekable_stream_decoder_get_FLAC_stream_decoder_state(decoder->private_->seekable_stream_decoder);
407 }
408
409 OggFLAC_API const char *OggFLAC__file_decoder_get_resolved_state_string(const OggFLAC__FileDecoder *decoder)
410 {
411         if(decoder->protected_->state != OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR)
412                 return OggFLAC__FileDecoderStateString[decoder->protected_->state];
413         else
414                 return OggFLAC__seekable_stream_decoder_get_resolved_state_string(decoder->private_->seekable_stream_decoder);
415 }
416
417 OggFLAC_API FLAC__bool OggFLAC__file_decoder_get_md5_checking(const OggFLAC__FileDecoder *decoder)
418 {
419         FLAC__ASSERT(0 != decoder);
420         FLAC__ASSERT(0 != decoder->private_);
421         return OggFLAC__seekable_stream_decoder_get_md5_checking(decoder->private_->seekable_stream_decoder);
422 }
423
424 OggFLAC_API unsigned OggFLAC__file_decoder_get_channels(const OggFLAC__FileDecoder *decoder)
425 {
426         FLAC__ASSERT(0 != decoder);
427         FLAC__ASSERT(0 != decoder->private_);
428         return OggFLAC__seekable_stream_decoder_get_channels(decoder->private_->seekable_stream_decoder);
429 }
430
431 OggFLAC_API FLAC__ChannelAssignment OggFLAC__file_decoder_get_channel_assignment(const OggFLAC__FileDecoder *decoder)
432 {
433         FLAC__ASSERT(0 != decoder);
434         FLAC__ASSERT(0 != decoder->private_);
435         return OggFLAC__seekable_stream_decoder_get_channel_assignment(decoder->private_->seekable_stream_decoder);
436 }
437
438 OggFLAC_API unsigned OggFLAC__file_decoder_get_bits_per_sample(const OggFLAC__FileDecoder *decoder)
439 {
440         FLAC__ASSERT(0 != decoder);
441         FLAC__ASSERT(0 != decoder->private_);
442         return OggFLAC__seekable_stream_decoder_get_bits_per_sample(decoder->private_->seekable_stream_decoder);
443 }
444
445 OggFLAC_API unsigned OggFLAC__file_decoder_get_sample_rate(const OggFLAC__FileDecoder *decoder)
446 {
447         FLAC__ASSERT(0 != decoder);
448         FLAC__ASSERT(0 != decoder->private_);
449         return OggFLAC__seekable_stream_decoder_get_sample_rate(decoder->private_->seekable_stream_decoder);
450 }
451
452 OggFLAC_API unsigned OggFLAC__file_decoder_get_blocksize(const OggFLAC__FileDecoder *decoder)
453 {
454         FLAC__ASSERT(0 != decoder);
455         FLAC__ASSERT(0 != decoder->private_);
456         return OggFLAC__seekable_stream_decoder_get_blocksize(decoder->private_->seekable_stream_decoder);
457 }
458
459 OggFLAC_API FLAC__bool OggFLAC__file_decoder_get_decode_position(const OggFLAC__FileDecoder *decoder, FLAC__uint64 *position)
460 {
461         FLAC__ASSERT(0 != decoder);
462         FLAC__ASSERT(0 != decoder->private_);
463         return OggFLAC__seekable_stream_decoder_get_decode_position(decoder->private_->seekable_stream_decoder, position);
464 }
465
466 OggFLAC_API FLAC__bool OggFLAC__file_decoder_process_single(OggFLAC__FileDecoder *decoder)
467 {
468         FLAC__bool ret;
469         FLAC__ASSERT(0 != decoder);
470
471         if(decoder->private_->seekable_stream_decoder->protected_->state == OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
472                 decoder->protected_->state = OggFLAC__FILE_DECODER_END_OF_FILE;
473
474         if(decoder->protected_->state == OggFLAC__FILE_DECODER_END_OF_FILE)
475                 return true;
476
477         FLAC__ASSERT(decoder->protected_->state == OggFLAC__FILE_DECODER_OK);
478
479         ret = OggFLAC__seekable_stream_decoder_process_single(decoder->private_->seekable_stream_decoder);
480         if(!ret)
481                 decoder->protected_->state = OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
482
483         return ret;
484 }
485
486 OggFLAC_API FLAC__bool OggFLAC__file_decoder_process_until_end_of_metadata(OggFLAC__FileDecoder *decoder)
487 {
488         FLAC__bool ret;
489         FLAC__ASSERT(0 != decoder);
490
491         if(decoder->private_->seekable_stream_decoder->protected_->state == OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
492                 decoder->protected_->state = OggFLAC__FILE_DECODER_END_OF_FILE;
493
494         if(decoder->protected_->state == OggFLAC__FILE_DECODER_END_OF_FILE)
495                 return true;
496
497         FLAC__ASSERT(decoder->protected_->state == OggFLAC__FILE_DECODER_OK);
498
499         ret = OggFLAC__seekable_stream_decoder_process_until_end_of_metadata(decoder->private_->seekable_stream_decoder);
500         if(!ret)
501                 decoder->protected_->state = OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
502
503         return ret;
504 }
505
506 OggFLAC_API FLAC__bool OggFLAC__file_decoder_process_until_end_of_file(OggFLAC__FileDecoder *decoder)
507 {
508         FLAC__bool ret;
509         FLAC__ASSERT(0 != decoder);
510
511         if(decoder->private_->seekable_stream_decoder->protected_->state == OggFLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
512                 decoder->protected_->state = OggFLAC__FILE_DECODER_END_OF_FILE;
513
514         if(decoder->protected_->state == OggFLAC__FILE_DECODER_END_OF_FILE)
515                 return true;
516
517         FLAC__ASSERT(decoder->protected_->state == OggFLAC__FILE_DECODER_OK);
518
519         ret = OggFLAC__seekable_stream_decoder_process_until_end_of_stream(decoder->private_->seekable_stream_decoder);
520         if(!ret)
521                 decoder->protected_->state = OggFLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
522
523         return ret;
524 }
525
526 OggFLAC_API FLAC__bool OggFLAC__file_decoder_seek_absolute(OggFLAC__FileDecoder *decoder, FLAC__uint64 sample)
527 {
528         FLAC__ASSERT(0 != decoder);
529         FLAC__ASSERT(decoder->protected_->state == OggFLAC__FILE_DECODER_OK || decoder->protected_->state == OggFLAC__FILE_DECODER_END_OF_FILE);
530
531         if(decoder->private_->filename == 0) { /* means the file is stdin... */
532                 decoder->protected_->state = OggFLAC__FILE_DECODER_SEEK_ERROR;
533                 return false;
534         }
535
536         if(!OggFLAC__seekable_stream_decoder_seek_absolute(decoder->private_->seekable_stream_decoder, sample)) {
537                 decoder->protected_->state = OggFLAC__FILE_DECODER_SEEK_ERROR;
538                 return false;
539         }
540         else {
541                 decoder->protected_->state = OggFLAC__FILE_DECODER_OK;
542                 return true;
543         }
544 }
545
546
547 /***********************************************************************
548  *
549  * Private class methods
550  *
551  ***********************************************************************/
552
553 void set_defaults_(OggFLAC__FileDecoder *decoder)
554 {
555         FLAC__ASSERT(0 != decoder);
556         FLAC__ASSERT(0 != decoder->private_);
557
558         decoder->private_->filename = 0;
559         decoder->private_->write_callback = 0;
560         decoder->private_->metadata_callback = 0;
561         decoder->private_->error_callback = 0;
562         decoder->private_->client_data = 0;
563 }
564
565 /*
566  * This will forcibly set stdin to binary mode (for OSes that require it)
567  */
568 FILE *get_binary_stdin_()
569 {
570         /* if something breaks here it is probably due to the presence or
571          * absence of an underscore before the identifiers 'setmode',
572          * 'fileno', and/or 'O_BINARY'; check your system header files.
573          */
574 #if defined _MSC_VER || defined __MINGW32__
575         _setmode(_fileno(stdin), _O_BINARY);
576 #elif defined __CYGWIN__
577         /* almost certainly not needed for any modern Cygwin, but let's be safe... */
578         setmode(_fileno(stdin), _O_BINARY);
579 #endif
580
581         return stdin;
582 }
583
584 FLAC__SeekableStreamDecoderReadStatus read_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
585 {
586         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
587         (void)decoder;
588
589         if(*bytes > 0) {
590                 *bytes = (unsigned)fread(buffer, sizeof(FLAC__byte), *bytes, file_decoder->private_->file);
591                 if(ferror(file_decoder->private_->file)) {
592                         return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
593                 }
594                 else {
595                         return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
596                 }
597         }
598         else
599                 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; /* abort to avoid a deadlock */
600 }
601
602 FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
603 {
604         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
605         (void)decoder;
606
607         if(fseek(file_decoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
608                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
609         else
610                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
611 }
612
613 FLAC__SeekableStreamDecoderTellStatus tell_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
614 {
615         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
616         long pos;
617         (void)decoder;
618
619         if((pos = ftell(file_decoder->private_->file)) < 0)
620                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
621         else {
622                 *absolute_byte_offset = (FLAC__uint64)pos;
623                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
624         }
625 }
626
627 FLAC__SeekableStreamDecoderLengthStatus length_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
628 {
629         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
630         struct stat filestats;
631         (void)decoder;
632
633         if(0 == file_decoder->private_->filename || stat(file_decoder->private_->filename, &filestats) != 0)
634                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
635         else {
636                 *stream_length = (FLAC__uint64)filestats.st_size;
637                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
638         }
639 }
640
641 FLAC__bool eof_callback_(const OggFLAC__SeekableStreamDecoder *decoder, void *client_data)
642 {
643         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
644         (void)decoder;
645
646         return feof(file_decoder->private_->file)? true : false;
647 }
648
649 FLAC__StreamDecoderWriteStatus write_callback_(const OggFLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
650 {
651         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
652         (void)decoder;
653
654         return file_decoder->private_->write_callback(file_decoder, frame, buffer, file_decoder->private_->client_data);
655 }
656
657 void metadata_callback_(const OggFLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
658 {
659         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
660         (void)decoder;
661
662         file_decoder->private_->metadata_callback(file_decoder, metadata, file_decoder->private_->client_data);
663 }
664
665 void error_callback_(const OggFLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
666 {
667         OggFLAC__FileDecoder *file_decoder = (OggFLAC__FileDecoder *)client_data;
668         (void)decoder;
669
670         file_decoder->private_->error_callback(file_decoder, status, file_decoder->private_->client_data);
671 }