1 | /* |
---|
2 | Audio File Library |
---|
3 | Copyright (C) 1998, Michael Pruett <michael@68k.org> |
---|
4 | |
---|
5 | This library is free software; you can redistribute it and/or |
---|
6 | modify it under the terms of the GNU Library General Public |
---|
7 | License as published by the Free Software Foundation; either |
---|
8 | version 2 of the License, or (at your option) any later version. |
---|
9 | |
---|
10 | This library 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 GNU |
---|
13 | Library General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU Library General Public |
---|
16 | License along with this library; if not, write to the |
---|
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | Boston, MA 02111-1307 USA. |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | misc.c |
---|
23 | |
---|
24 | This file contains routines for dealing with the Audio File |
---|
25 | Library's internal miscellaneous data types. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/types.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include "audiofile.h" |
---|
33 | #include "afinternal.h" |
---|
34 | #include "util.h" |
---|
35 | |
---|
36 | _Miscellaneous *find_misc_by_id (AFfilehandle file, int id) |
---|
37 | { |
---|
38 | int i; |
---|
39 | |
---|
40 | for (i=0; i<file->miscellaneousCount; i++) |
---|
41 | { |
---|
42 | if (file->miscellaneous[i].id == id) |
---|
43 | return &file->miscellaneous[i]; |
---|
44 | } |
---|
45 | |
---|
46 | _af_error(AF_BAD_MISCID, "bad miscellaneous id %d", id); |
---|
47 | |
---|
48 | return NULL; |
---|
49 | } |
---|
50 | |
---|
51 | _MiscellaneousSetup *find_miscsetup_by_id (AFfilesetup setup, int id) |
---|
52 | { |
---|
53 | int i; |
---|
54 | |
---|
55 | for (i=0; i<setup->miscellaneousCount; i++) |
---|
56 | { |
---|
57 | if (setup->miscellaneous[i].id == id) |
---|
58 | return &setup->miscellaneous[i]; |
---|
59 | } |
---|
60 | |
---|
61 | _af_error(AF_BAD_MISCID, "bad miscellaneous id %d", id); |
---|
62 | |
---|
63 | return NULL; |
---|
64 | } |
---|
65 | |
---|
66 | void afInitMiscIDs (AFfilesetup setup, int *ids, int nids) |
---|
67 | { |
---|
68 | int i; |
---|
69 | |
---|
70 | if (!_af_filesetup_ok(setup)) |
---|
71 | return; |
---|
72 | |
---|
73 | if (setup->miscellaneous != NULL) |
---|
74 | { |
---|
75 | free(setup->miscellaneous); |
---|
76 | } |
---|
77 | |
---|
78 | setup->miscellaneousCount = nids; |
---|
79 | |
---|
80 | if (nids == 0) |
---|
81 | setup->miscellaneous = NULL; |
---|
82 | else |
---|
83 | { |
---|
84 | setup->miscellaneous = _af_calloc(nids, |
---|
85 | sizeof (_Miscellaneous)); |
---|
86 | |
---|
87 | if (setup->miscellaneous == NULL) |
---|
88 | return; |
---|
89 | |
---|
90 | for (i=0; i<nids; i++) |
---|
91 | { |
---|
92 | setup->miscellaneous[i].id = ids[i]; |
---|
93 | setup->miscellaneous[i].type = 0; |
---|
94 | setup->miscellaneous[i].size = 0; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | setup->miscellaneousSet = AF_TRUE; |
---|
99 | } |
---|
100 | |
---|
101 | int afGetMiscIDs (AFfilehandle file, int *ids) |
---|
102 | { |
---|
103 | int i; |
---|
104 | |
---|
105 | if (!_af_filehandle_ok(file)) |
---|
106 | return -1; |
---|
107 | |
---|
108 | if (ids != NULL) |
---|
109 | { |
---|
110 | for (i=0; i<file->miscellaneousCount; i++) |
---|
111 | { |
---|
112 | ids[i] = file->miscellaneous[i].id; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | return file->miscellaneousCount; |
---|
117 | } |
---|
118 | |
---|
119 | void afInitMiscType (AFfilesetup setup, int miscellaneousid, int type) |
---|
120 | { |
---|
121 | _MiscellaneousSetup *miscellaneous; |
---|
122 | |
---|
123 | if (!_af_filesetup_ok(setup)) |
---|
124 | return; |
---|
125 | |
---|
126 | miscellaneous = find_miscsetup_by_id(setup, miscellaneousid); |
---|
127 | |
---|
128 | if (miscellaneous) |
---|
129 | miscellaneous->type = type; |
---|
130 | else |
---|
131 | _af_error(AF_BAD_MISCID, "bad miscellaneous id"); |
---|
132 | } |
---|
133 | |
---|
134 | int afGetMiscType (AFfilehandle file, int miscellaneousid) |
---|
135 | { |
---|
136 | _Miscellaneous *miscellaneous; |
---|
137 | |
---|
138 | if (!_af_filehandle_ok(file)) |
---|
139 | return -1; |
---|
140 | |
---|
141 | miscellaneous = find_misc_by_id(file, miscellaneousid); |
---|
142 | |
---|
143 | if (miscellaneous) |
---|
144 | { |
---|
145 | return miscellaneous->type; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | _af_error(AF_BAD_MISCID, "bad miscellaneous id"); |
---|
150 | return -1; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | void afInitMiscSize (AFfilesetup setup, int miscellaneousid, int size) |
---|
155 | { |
---|
156 | _MiscellaneousSetup *miscellaneous; |
---|
157 | |
---|
158 | if (!_af_filesetup_ok(setup)) |
---|
159 | return; |
---|
160 | |
---|
161 | miscellaneous = find_miscsetup_by_id(setup, miscellaneousid); |
---|
162 | |
---|
163 | if (miscellaneous) |
---|
164 | { |
---|
165 | miscellaneous->size = size; |
---|
166 | } |
---|
167 | else |
---|
168 | _af_error(AF_BAD_MISCID, "bad miscellaneous id"); |
---|
169 | } |
---|
170 | |
---|
171 | int afGetMiscSize (AFfilehandle file, int miscellaneousid) |
---|
172 | { |
---|
173 | _Miscellaneous *miscellaneous; |
---|
174 | |
---|
175 | if (!_af_filehandle_ok(file)) |
---|
176 | return -1; |
---|
177 | |
---|
178 | miscellaneous = find_misc_by_id(file, miscellaneousid); |
---|
179 | |
---|
180 | if (miscellaneous) |
---|
181 | { |
---|
182 | return miscellaneous->size; |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | _af_error(AF_BAD_MISCID, "bad miscellaneous id"); |
---|
187 | return -1; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | int afWriteMisc (AFfilehandle file, int miscellaneousid, void *buf, int bytes) |
---|
192 | { |
---|
193 | _Miscellaneous *miscellaneous; |
---|
194 | int localsize; |
---|
195 | |
---|
196 | if (!_af_filehandle_ok(file)) |
---|
197 | return -1; |
---|
198 | |
---|
199 | if (!_af_filehandle_can_write(file)) |
---|
200 | return -1; |
---|
201 | |
---|
202 | if ((miscellaneous = find_misc_by_id(file, miscellaneousid)) == NULL) |
---|
203 | return -1; |
---|
204 | |
---|
205 | if (bytes <= 0) |
---|
206 | { |
---|
207 | _af_error(AF_BAD_MISCSIZE, "invalid size (%d) for miscellaneous chunk", bytes); |
---|
208 | } |
---|
209 | |
---|
210 | if (miscellaneous->buffer == NULL && miscellaneous->size != 0) |
---|
211 | { |
---|
212 | miscellaneous->buffer = _af_malloc(miscellaneous->size); |
---|
213 | memset(miscellaneous->buffer, 0, miscellaneous->size); |
---|
214 | if (miscellaneous->buffer == NULL) |
---|
215 | return -1; |
---|
216 | } |
---|
217 | |
---|
218 | if (bytes + miscellaneous->position > miscellaneous->size) |
---|
219 | localsize = miscellaneous->size - miscellaneous->position; |
---|
220 | else |
---|
221 | localsize = bytes; |
---|
222 | |
---|
223 | memcpy((char *) miscellaneous->buffer + miscellaneous->position, |
---|
224 | buf, localsize); |
---|
225 | miscellaneous->position += localsize; |
---|
226 | return localsize; |
---|
227 | } |
---|
228 | |
---|
229 | int afReadMisc (AFfilehandle file, int miscellaneousid, void *buf, int bytes) |
---|
230 | { |
---|
231 | int localsize; |
---|
232 | _Miscellaneous *miscellaneous; |
---|
233 | |
---|
234 | if (!_af_filehandle_ok(file)) |
---|
235 | return -1; |
---|
236 | |
---|
237 | if (!_af_filehandle_can_read(file)) |
---|
238 | return -1; |
---|
239 | |
---|
240 | if ((miscellaneous = find_misc_by_id(file, miscellaneousid)) == NULL) |
---|
241 | return -1; |
---|
242 | |
---|
243 | if (bytes <= 0) |
---|
244 | { |
---|
245 | _af_error(AF_BAD_MISCSIZE, "invalid size (%d) for miscellaneous chunk", bytes); |
---|
246 | return -1; |
---|
247 | } |
---|
248 | |
---|
249 | if (bytes + miscellaneous->position > miscellaneous->size) |
---|
250 | localsize = miscellaneous->size - miscellaneous->position; |
---|
251 | else |
---|
252 | localsize = bytes; |
---|
253 | |
---|
254 | memcpy(buf, (char *) miscellaneous->buffer + miscellaneous->position, |
---|
255 | localsize); |
---|
256 | miscellaneous->position += localsize; |
---|
257 | return localsize; |
---|
258 | } |
---|
259 | |
---|
260 | int afSeekMisc (AFfilehandle file, int miscellaneousid, int offset) |
---|
261 | { |
---|
262 | _Miscellaneous *miscellaneous; |
---|
263 | |
---|
264 | if (!_af_filehandle_ok(file)) |
---|
265 | return -1; |
---|
266 | |
---|
267 | if ((miscellaneous = find_misc_by_id(file, miscellaneousid)) == NULL) |
---|
268 | return -1; |
---|
269 | |
---|
270 | if (offset >= miscellaneous->size) |
---|
271 | { |
---|
272 | _af_error(AF_BAD_MISCSEEK, |
---|
273 | "offset %d too big for miscellaneous chunk %d " |
---|
274 | "(%d data bytes)", |
---|
275 | offset, miscellaneousid, miscellaneous->size); |
---|
276 | return -1; |
---|
277 | } |
---|
278 | |
---|
279 | miscellaneous->position = offset; |
---|
280 | |
---|
281 | return offset; |
---|
282 | } |
---|