1 /*
2   Copyright (c) 2005-2009, The Musepack Development Team
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8 
9   * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12   * Redistributions in binary form must reproduce the above
13   copyright notice, this list of conditions and the following
14   disclaimer in the documentation and/or other materials provided
15   with the distribution.
16 
17   * Neither the name of the The Musepack Development Team nor the
18   names of its contributors may be used to endorse or promote
19   products derived from this software without specific prior
20   written permission.
21 
22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /// \file mpc_reader.c
35 /// Contains implementations for simple file-based mpc_reader
36 #include "reader.h"
37 #include "internal.h"
38 #include <stdio.h>
39 
40 #define STDIO_MAGIC 0xF34B963C ///< Just a random safe-check value...
41 typedef struct mpc_reader_stdio_t {
42     FILE       *p_file;
43     int         file_size;
44     mpc_bool_t  is_seekable;
45     mpc_int32_t magic;
46 } mpc_reader_stdio;
47 
48 /// mpc_reader callback implementations
49 static mpc_int32_t
read_stdio(mpc_reader * p_reader,void * ptr,mpc_int32_t size)50 read_stdio(mpc_reader *p_reader, void *ptr, mpc_int32_t size)
51 {
52     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
53     if(p_stdio->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
54     return (mpc_int32_t) fread(ptr, 1, size, p_stdio->p_file);
55 }
56 
57 static mpc_bool_t
seek_stdio(mpc_reader * p_reader,mpc_int32_t offset)58 seek_stdio(mpc_reader *p_reader, mpc_int32_t offset)
59 {
60     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
61     if(p_stdio->magic != STDIO_MAGIC) return MPC_FALSE;
62     return p_stdio->is_seekable ? fseek(p_stdio->p_file, offset, SEEK_SET) == 0 : MPC_FALSE;
63 }
64 
65 static mpc_int32_t
tell_stdio(mpc_reader * p_reader)66 tell_stdio(mpc_reader *p_reader)
67 {
68     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
69     if(p_stdio->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
70     return ftell(p_stdio->p_file);
71 }
72 
73 static mpc_int32_t
get_size_stdio(mpc_reader * p_reader)74 get_size_stdio(mpc_reader *p_reader)
75 {
76     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
77     if(p_stdio->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
78     return p_stdio->file_size;
79 }
80 
81 static mpc_bool_t
canseek_stdio(mpc_reader * p_reader)82 canseek_stdio(mpc_reader *p_reader)
83 {
84     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
85     if(p_stdio->magic != STDIO_MAGIC) return MPC_FALSE;
86     return p_stdio->is_seekable;
87 }
88 
89 mpc_status
mpc_reader_init_stdio_stream(mpc_reader * p_reader,FILE * p_file)90 mpc_reader_init_stdio_stream(mpc_reader * p_reader, FILE * p_file)
91 {
92     mpc_reader tmp_reader; mpc_reader_stdio *p_stdio; int err;
93 
94     p_stdio = NULL;
95     memset(&tmp_reader, 0, sizeof tmp_reader);
96     p_stdio = malloc(sizeof *p_stdio);
97     if(!p_stdio) return MPC_STATUS_FAIL;
98     memset(p_stdio, 0, sizeof *p_stdio);
99 
100     p_stdio->magic  = STDIO_MAGIC;
101     p_stdio->p_file = p_file;
102     p_stdio->is_seekable = MPC_TRUE;
103     err = fseek(p_stdio->p_file, 0, SEEK_END);
104     if(err < 0) goto clean;
105     err = ftell(p_stdio->p_file);
106     if(err < 0) goto clean;
107     p_stdio->file_size = err;
108     err = fseek(p_stdio->p_file, 0, SEEK_SET);
109     if(err < 0) goto clean;
110 
111     tmp_reader.data     = p_stdio;
112     tmp_reader.canseek  = canseek_stdio;
113     tmp_reader.get_size = get_size_stdio;
114     tmp_reader.read     = read_stdio;
115     tmp_reader.seek     = seek_stdio;
116     tmp_reader.tell     = tell_stdio;
117 
118     *p_reader = tmp_reader;
119     return MPC_STATUS_OK;
120 clean:
121     if(p_stdio && p_stdio->p_file)
122         fclose(p_stdio->p_file);
123     free(p_stdio);
124     return MPC_STATUS_FAIL;
125 }
126 
127 mpc_status
mpc_reader_init_stdio(mpc_reader * p_reader,const char * filename)128 mpc_reader_init_stdio(mpc_reader *p_reader, const char *filename)
129 {
130 	FILE * stream = fopen(filename, "rb");
131 	if (stream == NULL) return MPC_STATUS_FAIL;
132 	return mpc_reader_init_stdio_stream(p_reader,stream);
133 }
134 
135 void
mpc_reader_exit_stdio(mpc_reader * p_reader)136 mpc_reader_exit_stdio(mpc_reader *p_reader)
137 {
138     mpc_reader_stdio *p_stdio = (mpc_reader_stdio*) p_reader->data;
139     if(p_stdio->magic != STDIO_MAGIC) return;
140     fclose(p_stdio->p_file);
141     free(p_stdio);
142     p_reader->data = NULL;
143 }
144 
145