File: | libs/libsndfile/src/common.c |
Location: | line 983, column 17 |
Description: | The left operand of '<<' is a garbage value |
1 | /* | |||||
2 | ** Copyright (C) 1999-2013 Erik de Castro Lopo <erikd@mega-nerd.com> | |||||
3 | ** | |||||
4 | ** This program is free software; you can redistribute it and/or modify | |||||
5 | ** it under the terms of the GNU Lesser General Public License as published by | |||||
6 | ** the Free Software Foundation; either version 2.1 of the License, or | |||||
7 | ** (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 Lesser General Public License for more details. | |||||
13 | ** | |||||
14 | ** You should have received a copy of the GNU Lesser 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 | #include <config.h> | |||||
20 | ||||||
21 | #include <stdarg.h> | |||||
22 | #include <string.h> | |||||
23 | ||||||
24 | #ifndef _MSC_VER | |||||
25 | #include <unistd.h> | |||||
26 | #endif | |||||
27 | #include <ctype.h> | |||||
28 | #include <math.h> | |||||
29 | #include <time.h> | |||||
30 | #ifndef _MSC_VER | |||||
31 | #include <sys/time.h> | |||||
32 | #endif | |||||
33 | ||||||
34 | #include "sndfile.h" | |||||
35 | #include "sfendian.h" | |||||
36 | #include "common.h" | |||||
37 | ||||||
38 | /*----------------------------------------------------------------------------------------------- | |||||
39 | ** psf_log_printf allows libsndfile internal functions to print to an internal parselog which | |||||
40 | ** can later be displayed. | |||||
41 | ** The format specifiers are as for printf but without the field width and other modifiers. | |||||
42 | ** Printing is performed to the parselog char array of the SF_PRIVATE struct. | |||||
43 | ** Printing is done in such a way as to guarantee that the log never overflows the end of the | |||||
44 | ** parselog array. | |||||
45 | */ | |||||
46 | ||||||
47 | static inline void | |||||
48 | log_putchar (SF_PRIVATE *psf, char ch) | |||||
49 | { if (psf->parselog.indx < SIGNED_SIZEOF (psf->parselog.buf)((int) sizeof (psf->parselog.buf)) - 1) | |||||
50 | { psf->parselog.buf [psf->parselog.indx++] = ch ; | |||||
51 | psf->parselog.buf [psf->parselog.indx] = 0 ; | |||||
52 | } ; | |||||
53 | return ; | |||||
54 | } /* log_putchar */ | |||||
55 | ||||||
56 | void | |||||
57 | psf_log_printf (SF_PRIVATE *psf, const char *format, ...) | |||||
58 | { va_list ap ; | |||||
59 | unsigned int u ; | |||||
60 | int d, tens, shift, width, width_specifier, left_align ; | |||||
61 | char c, *strptr, istr [5], lead_char, sign_char ; | |||||
62 | ||||||
63 | va_start (ap, format)__builtin_va_start(ap, format) ; | |||||
64 | ||||||
65 | while ((c = *format++)) | |||||
66 | { if (c != '%') | |||||
67 | { log_putchar (psf, c) ; | |||||
68 | continue ; | |||||
69 | } ; | |||||
70 | ||||||
71 | if (format [0] == '%') /* Handle %% */ | |||||
72 | { log_putchar (psf, '%') ; | |||||
73 | format ++ ; | |||||
74 | continue ; | |||||
75 | } ; | |||||
76 | ||||||
77 | sign_char = 0 ; | |||||
78 | left_align = SF_FALSE ; | |||||
79 | while (1) | |||||
80 | { switch (format [0]) | |||||
81 | { case ' ' : | |||||
82 | case '+' : | |||||
83 | sign_char = format [0] ; | |||||
84 | format ++ ; | |||||
85 | continue ; | |||||
86 | ||||||
87 | case '-' : | |||||
88 | left_align = SF_TRUE ; | |||||
89 | format ++ ; | |||||
90 | continue ; | |||||
91 | ||||||
92 | default : break ; | |||||
93 | } ; | |||||
94 | ||||||
95 | break ; | |||||
96 | } ; | |||||
97 | ||||||
98 | if (format [0] == 0) | |||||
99 | break ; | |||||
100 | ||||||
101 | lead_char = ' ' ; | |||||
102 | if (format [0] == '0') | |||||
103 | lead_char = '0' ; | |||||
104 | ||||||
105 | width_specifier = 0 ; | |||||
106 | while ((c = *format++) && isdigit (c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit )) | |||||
107 | width_specifier = width_specifier * 10 + (c - '0') ; | |||||
108 | ||||||
109 | switch (c) | |||||
110 | { case 0 : /* NULL character. */ | |||||
111 | va_end (ap)__builtin_va_end(ap) ; | |||||
112 | return ; | |||||
113 | ||||||
114 | case 's': /* string */ | |||||
115 | strptr = va_arg (ap, char *)__builtin_va_arg(ap, char *) ; | |||||
116 | if (strptr == NULL((void*)0)) | |||||
117 | break ; | |||||
118 | width_specifier -= strlen (strptr) ; | |||||
119 | if (left_align == SF_FALSE) | |||||
120 | while (width_specifier -- > 0) | |||||
121 | log_putchar (psf, ' ') ; | |||||
122 | while (*strptr) | |||||
123 | log_putchar (psf, *strptr++) ; | |||||
124 | while (width_specifier -- > 0) | |||||
125 | log_putchar (psf, ' ') ; | |||||
126 | break ; | |||||
127 | ||||||
128 | case 'd': /* int */ | |||||
129 | d = va_arg (ap, int)__builtin_va_arg(ap, int) ; | |||||
130 | ||||||
131 | if (d < 0) | |||||
132 | { d = -d ; | |||||
133 | sign_char = '-' ; | |||||
134 | if (lead_char != '0' && left_align == SF_FALSE) | |||||
135 | width_specifier -- ; | |||||
136 | } ; | |||||
137 | ||||||
138 | tens = 1 ; | |||||
139 | width = 1 ; | |||||
140 | while (d / tens >= 10) | |||||
141 | { tens *= 10 ; | |||||
142 | width ++ ; | |||||
143 | } ; | |||||
144 | ||||||
145 | width_specifier -= width ; | |||||
146 | ||||||
147 | if (sign_char == ' ') | |||||
148 | { log_putchar (psf, ' ') ; | |||||
149 | width_specifier -- ; | |||||
150 | } ; | |||||
151 | ||||||
152 | if (left_align == SF_FALSE && lead_char != '0') | |||||
153 | { if (sign_char == '+') | |||||
154 | width_specifier -- ; | |||||
155 | ||||||
156 | while (width_specifier -- > 0) | |||||
157 | log_putchar (psf, lead_char) ; | |||||
158 | } ; | |||||
159 | ||||||
160 | if (sign_char == '+' || sign_char == '-') | |||||
161 | { log_putchar (psf, sign_char) ; | |||||
162 | width_specifier -- ; | |||||
163 | } ; | |||||
164 | ||||||
165 | if (left_align == SF_FALSE) | |||||
166 | while (width_specifier -- > 0) | |||||
167 | log_putchar (psf, lead_char) ; | |||||
168 | ||||||
169 | while (tens > 0) | |||||
170 | { log_putchar (psf, '0' + d / tens) ; | |||||
171 | d %= tens ; | |||||
172 | tens /= 10 ; | |||||
173 | } ; | |||||
174 | ||||||
175 | while (width_specifier -- > 0) | |||||
176 | log_putchar (psf, lead_char) ; | |||||
177 | break ; | |||||
178 | ||||||
179 | case 'D': /* sf_count_t */ | |||||
180 | { sf_count_t D, Tens ; | |||||
181 | ||||||
182 | D = va_arg (ap, sf_count_t)__builtin_va_arg(ap, sf_count_t) ; | |||||
183 | ||||||
184 | if (D == 0) | |||||
185 | { while (-- width_specifier > 0) | |||||
186 | log_putchar (psf, lead_char) ; | |||||
187 | log_putchar (psf, '0') ; | |||||
188 | break ; | |||||
189 | } | |||||
190 | if (D < 0) | |||||
191 | { log_putchar (psf, '-') ; | |||||
192 | D = -D ; | |||||
193 | } ; | |||||
194 | Tens = 1 ; | |||||
195 | width = 1 ; | |||||
196 | while (D / Tens >= 10) | |||||
197 | { Tens *= 10 ; | |||||
198 | width ++ ; | |||||
199 | } ; | |||||
200 | ||||||
201 | while (width_specifier > width) | |||||
202 | { log_putchar (psf, lead_char) ; | |||||
203 | width_specifier-- ; | |||||
204 | } ; | |||||
205 | ||||||
206 | while (Tens > 0) | |||||
207 | { log_putchar (psf, '0' + D / Tens) ; | |||||
208 | D %= Tens ; | |||||
209 | Tens /= 10 ; | |||||
210 | } ; | |||||
211 | } ; | |||||
212 | break ; | |||||
213 | ||||||
214 | case 'u': /* unsigned int */ | |||||
215 | u = va_arg (ap, unsigned int)__builtin_va_arg(ap, unsigned int) ; | |||||
216 | ||||||
217 | tens = 1 ; | |||||
218 | width = 1 ; | |||||
219 | while (u / tens >= 10) | |||||
220 | { tens *= 10 ; | |||||
221 | width ++ ; | |||||
222 | } ; | |||||
223 | ||||||
224 | width_specifier -= width ; | |||||
225 | ||||||
226 | if (sign_char == ' ') | |||||
227 | { log_putchar (psf, ' ') ; | |||||
228 | width_specifier -- ; | |||||
229 | } ; | |||||
230 | ||||||
231 | if (left_align == SF_FALSE && lead_char != '0') | |||||
232 | { if (sign_char == '+') | |||||
233 | width_specifier -- ; | |||||
234 | ||||||
235 | while (width_specifier -- > 0) | |||||
236 | log_putchar (psf, lead_char) ; | |||||
237 | } ; | |||||
238 | ||||||
239 | if (sign_char == '+' || sign_char == '-') | |||||
240 | { log_putchar (psf, sign_char) ; | |||||
241 | width_specifier -- ; | |||||
242 | } ; | |||||
243 | ||||||
244 | if (left_align == SF_FALSE) | |||||
245 | while (width_specifier -- > 0) | |||||
246 | log_putchar (psf, lead_char) ; | |||||
247 | ||||||
248 | while (tens > 0) | |||||
249 | { log_putchar (psf, '0' + u / tens) ; | |||||
250 | u %= tens ; | |||||
251 | tens /= 10 ; | |||||
252 | } ; | |||||
253 | ||||||
254 | while (width_specifier -- > 0) | |||||
255 | log_putchar (psf, lead_char) ; | |||||
256 | break ; | |||||
257 | ||||||
258 | case 'c': /* char */ | |||||
259 | c = va_arg (ap, int)__builtin_va_arg(ap, int) & 0xFF ; | |||||
260 | log_putchar (psf, c) ; | |||||
261 | break ; | |||||
262 | ||||||
263 | case 'x': /* hex */ | |||||
264 | case 'X': /* hex */ | |||||
265 | d = va_arg (ap, int)__builtin_va_arg(ap, int) ; | |||||
266 | ||||||
267 | if (d == 0) | |||||
268 | { while (--width_specifier > 0) | |||||
269 | log_putchar (psf, lead_char) ; | |||||
270 | log_putchar (psf, '0') ; | |||||
271 | break ; | |||||
272 | } ; | |||||
273 | shift = 28 ; | |||||
274 | width = (width_specifier < 8) ? 8 : width_specifier ; | |||||
275 | while (! ((0xF << shift) & d)) | |||||
276 | { shift -= 4 ; | |||||
277 | width -- ; | |||||
278 | } ; | |||||
279 | ||||||
280 | while (width > 0 && width_specifier > width) | |||||
281 | { log_putchar (psf, lead_char) ; | |||||
282 | width_specifier-- ; | |||||
283 | } ; | |||||
284 | ||||||
285 | while (shift >= 0) | |||||
286 | { c = (d >> shift) & 0xF ; | |||||
287 | log_putchar (psf, (c > 9) ? c + 'A' - 10 : c + '0') ; | |||||
288 | shift -= 4 ; | |||||
289 | } ; | |||||
290 | break ; | |||||
291 | ||||||
292 | case 'M': /* int2str */ | |||||
293 | d = va_arg (ap, int)__builtin_va_arg(ap, int) ; | |||||
294 | if (CPU_IS_LITTLE_ENDIAN1) | |||||
295 | { istr [0] = d & 0xFF ; | |||||
296 | istr [1] = (d >> 8) & 0xFF ; | |||||
297 | istr [2] = (d >> 16) & 0xFF ; | |||||
298 | istr [3] = (d >> 24) & 0xFF ; | |||||
299 | } | |||||
300 | else | |||||
301 | { istr [3] = d & 0xFF ; | |||||
302 | istr [2] = (d >> 8) & 0xFF ; | |||||
303 | istr [1] = (d >> 16) & 0xFF ; | |||||
304 | istr [0] = (d >> 24) & 0xFF ; | |||||
305 | } ; | |||||
306 | istr [4] = 0 ; | |||||
307 | strptr = istr ; | |||||
308 | while (*strptr) | |||||
309 | { c = *strptr++ ; | |||||
310 | log_putchar (psf, c) ; | |||||
311 | } ; | |||||
312 | break ; | |||||
313 | ||||||
314 | default : | |||||
315 | log_putchar (psf, '*') ; | |||||
316 | log_putchar (psf, c) ; | |||||
317 | log_putchar (psf, '*') ; | |||||
318 | break ; | |||||
319 | } /* switch */ | |||||
320 | } /* while */ | |||||
321 | ||||||
322 | va_end (ap)__builtin_va_end(ap) ; | |||||
323 | return ; | |||||
324 | } /* psf_log_printf */ | |||||
325 | ||||||
326 | /*----------------------------------------------------------------------------------------------- | |||||
327 | ** ASCII header printf functions. | |||||
328 | ** Some formats (ie NIST) use ascii text in their headers. | |||||
329 | ** Format specifiers are the same as the standard printf specifiers (uses vsnprintf). | |||||
330 | ** If this generates a compile error on any system, the author should be notified | |||||
331 | ** so an alternative vsnprintf can be provided. | |||||
332 | */ | |||||
333 | ||||||
334 | void | |||||
335 | psf_asciiheader_printf (SF_PRIVATE *psf, const char *format, ...) | |||||
336 | { va_list argptr ; | |||||
337 | int maxlen ; | |||||
338 | char *start ; | |||||
339 | ||||||
340 | maxlen = strlen ((char*) psf->header) ; | |||||
341 | start = ((char*) psf->header) + maxlen ; | |||||
342 | maxlen = sizeof (psf->header) - maxlen ; | |||||
343 | ||||||
344 | va_start (argptr, format)__builtin_va_start(argptr, format) ; | |||||
345 | vsnprintf (start, maxlen, format, argptr) ; | |||||
346 | va_end (argptr)__builtin_va_end(argptr) ; | |||||
347 | ||||||
348 | /* Make sure the string is properly terminated. */ | |||||
349 | start [maxlen - 1] = 0 ; | |||||
350 | ||||||
351 | psf->headindex = strlen ((char*) psf->header) ; | |||||
352 | ||||||
353 | return ; | |||||
354 | } /* psf_asciiheader_printf */ | |||||
355 | ||||||
356 | /*----------------------------------------------------------------------------------------------- | |||||
357 | ** Binary header writing functions. Returns number of bytes written. | |||||
358 | ** | |||||
359 | ** Format specifiers for psf_binheader_writef are as follows | |||||
360 | ** m - marker - four bytes - no endian manipulation | |||||
361 | ** | |||||
362 | ** e - all following numerical values will be little endian | |||||
363 | ** E - all following numerical values will be big endian | |||||
364 | ** | |||||
365 | ** t - all following O types will be truncated to 4 bytes | |||||
366 | ** T - switch off truncation of all following O types | |||||
367 | ** | |||||
368 | ** 1 - single byte value | |||||
369 | ** 2 - two byte value | |||||
370 | ** 3 - three byte value | |||||
371 | ** 4 - four byte value | |||||
372 | ** 8 - eight byte value (sometimes written as 4 bytes) | |||||
373 | ** | |||||
374 | ** s - string preceded by a four byte length | |||||
375 | ** S - string including null terminator | |||||
376 | ** f - floating point data | |||||
377 | ** d - double precision floating point data | |||||
378 | ** h - 16 binary bytes value | |||||
379 | ** | |||||
380 | ** b - binary data (see below) | |||||
381 | ** z - zero bytes (ses below) | |||||
382 | ** j - jump forwards or backwards | |||||
383 | ** | |||||
384 | ** To write a word followed by an int (both little endian) use: | |||||
385 | ** psf_binheader_writef ("e24", wordval, longval) ; | |||||
386 | ** | |||||
387 | ** To write binary data use: | |||||
388 | ** psf_binheader_writef ("b", &bindata, sizeof (bindata)) ; | |||||
389 | ** | |||||
390 | ** To write N zero bytes use: | |||||
391 | ** NOTE: due to platform issues (ie x86-64) you should cast the | |||||
392 | ** argument to size_t or ensure the variable type is size_t. | |||||
393 | ** psf_binheader_writef ("z", N) ; | |||||
394 | */ | |||||
395 | ||||||
396 | /* These macros may seem a bit messy but do prevent problems with processors which | |||||
397 | ** seg. fault when asked to write an int or short to a non-int/short aligned address. | |||||
398 | */ | |||||
399 | ||||||
400 | static inline void | |||||
401 | header_put_byte (SF_PRIVATE *psf, char x) | |||||
402 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 1) | |||||
403 | psf->header [psf->headindex++] = x ; | |||||
404 | } /* header_put_byte */ | |||||
405 | ||||||
406 | #if (CPU_IS_BIG_ENDIAN0 == 1) | |||||
407 | static inline void | |||||
408 | header_put_marker (SF_PRIVATE *psf, int x) | |||||
409 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 4) | |||||
410 | { psf->header [psf->headindex++] = (x >> 24) ; | |||||
411 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
412 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
413 | psf->header [psf->headindex++] = x ; | |||||
414 | } ; | |||||
415 | } /* header_put_marker */ | |||||
416 | ||||||
417 | #elif (CPU_IS_LITTLE_ENDIAN1 == 1) | |||||
418 | static inline void | |||||
419 | header_put_marker (SF_PRIVATE *psf, int x) | |||||
420 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 4) | |||||
421 | { psf->header [psf->headindex++] = x ; | |||||
422 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
423 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
424 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
425 | } ; | |||||
426 | } /* header_put_marker */ | |||||
427 | ||||||
428 | #else | |||||
429 | # error "Cannot determine endian-ness of processor." | |||||
430 | #endif | |||||
431 | ||||||
432 | ||||||
433 | static inline void | |||||
434 | header_put_be_short (SF_PRIVATE *psf, int x) | |||||
435 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 2) | |||||
436 | { psf->header [psf->headindex++] = (x >> 8) ; | |||||
437 | psf->header [psf->headindex++] = x ; | |||||
438 | } ; | |||||
439 | } /* header_put_be_short */ | |||||
440 | ||||||
441 | static inline void | |||||
442 | header_put_le_short (SF_PRIVATE *psf, int x) | |||||
443 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 2) | |||||
444 | { psf->header [psf->headindex++] = x ; | |||||
445 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
446 | } ; | |||||
447 | } /* header_put_le_short */ | |||||
448 | ||||||
449 | static inline void | |||||
450 | header_put_be_3byte (SF_PRIVATE *psf, int x) | |||||
451 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 3) | |||||
452 | { psf->header [psf->headindex++] = (x >> 16) ; | |||||
453 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
454 | psf->header [psf->headindex++] = x ; | |||||
455 | } ; | |||||
456 | } /* header_put_be_3byte */ | |||||
457 | ||||||
458 | static inline void | |||||
459 | header_put_le_3byte (SF_PRIVATE *psf, int x) | |||||
460 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 3) | |||||
461 | { psf->header [psf->headindex++] = x ; | |||||
462 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
463 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
464 | } ; | |||||
465 | } /* header_put_le_3byte */ | |||||
466 | ||||||
467 | static inline void | |||||
468 | header_put_be_int (SF_PRIVATE *psf, int x) | |||||
469 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 4) | |||||
470 | { psf->header [psf->headindex++] = (x >> 24) ; | |||||
471 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
472 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
473 | psf->header [psf->headindex++] = x ; | |||||
474 | } ; | |||||
475 | } /* header_put_be_int */ | |||||
476 | ||||||
477 | static inline void | |||||
478 | header_put_le_int (SF_PRIVATE *psf, int x) | |||||
479 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 4) | |||||
480 | { psf->header [psf->headindex++] = x ; | |||||
481 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
482 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
483 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
484 | } ; | |||||
485 | } /* header_put_le_int */ | |||||
486 | ||||||
487 | #if (SIZEOF_SF_COUNT_T8 == 4) | |||||
488 | ||||||
489 | static inline void | |||||
490 | header_put_be_8byte (SF_PRIVATE *psf, sf_count_t x) | |||||
491 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 8) | |||||
492 | { psf->header [psf->headindex++] = 0 ; | |||||
493 | psf->header [psf->headindex++] = 0 ; | |||||
494 | psf->header [psf->headindex++] = 0 ; | |||||
495 | psf->header [psf->headindex++] = 0 ; | |||||
496 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
497 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
498 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
499 | psf->header [psf->headindex++] = x ; | |||||
500 | } ; | |||||
501 | } /* header_put_be_8byte */ | |||||
502 | ||||||
503 | static inline void | |||||
504 | header_put_le_8byte (SF_PRIVATE *psf, sf_count_t x) | |||||
505 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 8) | |||||
506 | { psf->header [psf->headindex++] = x ; | |||||
507 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
508 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
509 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
510 | psf->header [psf->headindex++] = 0 ; | |||||
511 | psf->header [psf->headindex++] = 0 ; | |||||
512 | psf->header [psf->headindex++] = 0 ; | |||||
513 | psf->header [psf->headindex++] = 0 ; | |||||
514 | } ; | |||||
515 | } /* header_put_le_8byte */ | |||||
516 | ||||||
517 | #elif (SIZEOF_SF_COUNT_T8 == 8) | |||||
518 | ||||||
519 | static inline void | |||||
520 | header_put_be_8byte (SF_PRIVATE *psf, sf_count_t x) | |||||
521 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 8) | |||||
522 | { psf->header [psf->headindex++] = (x >> 56) ; | |||||
523 | psf->header [psf->headindex++] = (x >> 48) ; | |||||
524 | psf->header [psf->headindex++] = (x >> 40) ; | |||||
525 | psf->header [psf->headindex++] = (x >> 32) ; | |||||
526 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
527 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
528 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
529 | psf->header [psf->headindex++] = x ; | |||||
530 | } ; | |||||
531 | } /* header_put_be_8byte */ | |||||
532 | ||||||
533 | static inline void | |||||
534 | header_put_le_8byte (SF_PRIVATE *psf, sf_count_t x) | |||||
535 | { if (psf->headindex < SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - 8) | |||||
536 | { psf->header [psf->headindex++] = x ; | |||||
537 | psf->header [psf->headindex++] = (x >> 8) ; | |||||
538 | psf->header [psf->headindex++] = (x >> 16) ; | |||||
539 | psf->header [psf->headindex++] = (x >> 24) ; | |||||
540 | psf->header [psf->headindex++] = (x >> 32) ; | |||||
541 | psf->header [psf->headindex++] = (x >> 40) ; | |||||
542 | psf->header [psf->headindex++] = (x >> 48) ; | |||||
543 | psf->header [psf->headindex++] = (x >> 56) ; | |||||
544 | } ; | |||||
545 | } /* header_put_le_8byte */ | |||||
546 | ||||||
547 | #else | |||||
548 | #error "SIZEOF_SF_COUNT_T is not defined." | |||||
549 | #endif | |||||
550 | ||||||
551 | int | |||||
552 | psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...) | |||||
553 | { va_list argptr ; | |||||
554 | sf_count_t countdata ; | |||||
555 | unsigned long longdata ; | |||||
556 | unsigned int data ; | |||||
557 | float floatdata ; | |||||
558 | double doubledata ; | |||||
559 | void *bindata ; | |||||
560 | size_t size ; | |||||
561 | char c, *strptr ; | |||||
562 | int count = 0, trunc_8to4 ; | |||||
563 | ||||||
564 | trunc_8to4 = SF_FALSE ; | |||||
565 | ||||||
566 | va_start (argptr, format)__builtin_va_start(argptr, format) ; | |||||
567 | ||||||
568 | while ((c = *format++)) | |||||
569 | { switch (c) | |||||
570 | { case ' ' : /* Do nothing. Just used to space out format string. */ | |||||
571 | break ; | |||||
572 | ||||||
573 | case 'e' : /* All conversions are now from LE to host. */ | |||||
574 | psf->rwf_endian = SF_ENDIAN_LITTLE ; | |||||
575 | break ; | |||||
576 | ||||||
577 | case 'E' : /* All conversions are now from BE to host. */ | |||||
578 | psf->rwf_endian = SF_ENDIAN_BIG ; | |||||
579 | break ; | |||||
580 | ||||||
581 | case 't' : /* All 8 byte values now get written as 4 bytes. */ | |||||
582 | trunc_8to4 = SF_TRUE ; | |||||
583 | break ; | |||||
584 | ||||||
585 | case 'T' : /* All 8 byte values now get written as 8 bytes. */ | |||||
586 | trunc_8to4 = SF_FALSE ; | |||||
587 | break ; | |||||
588 | ||||||
589 | case 'm' : | |||||
590 | data = va_arg (argptr, unsigned int)__builtin_va_arg(argptr, unsigned int) ; | |||||
591 | header_put_marker (psf, data) ; | |||||
592 | count += 4 ; | |||||
593 | break ; | |||||
594 | ||||||
595 | case '1' : | |||||
596 | data = va_arg (argptr, unsigned int)__builtin_va_arg(argptr, unsigned int) ; | |||||
597 | header_put_byte (psf, data) ; | |||||
598 | count += 1 ; | |||||
599 | break ; | |||||
600 | ||||||
601 | case '2' : | |||||
602 | data = va_arg (argptr, unsigned int)__builtin_va_arg(argptr, unsigned int) ; | |||||
603 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
604 | { header_put_be_short (psf, data) ; | |||||
605 | } | |||||
606 | else | |||||
607 | { header_put_le_short (psf, data) ; | |||||
608 | } ; | |||||
609 | count += 2 ; | |||||
610 | break ; | |||||
611 | ||||||
612 | case '3' : /* tribyte */ | |||||
613 | data = va_arg (argptr, unsigned int)__builtin_va_arg(argptr, unsigned int) ; | |||||
614 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
615 | { header_put_be_3byte (psf, data) ; | |||||
616 | } | |||||
617 | else | |||||
618 | { header_put_le_3byte (psf, data) ; | |||||
619 | } ; | |||||
620 | count += 3 ; | |||||
621 | break ; | |||||
622 | ||||||
623 | case '4' : | |||||
624 | data = va_arg (argptr, unsigned int)__builtin_va_arg(argptr, unsigned int) ; | |||||
625 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
626 | { header_put_be_int (psf, data) ; | |||||
627 | } | |||||
628 | else | |||||
629 | { header_put_le_int (psf, data) ; | |||||
630 | } ; | |||||
631 | count += 4 ; | |||||
632 | break ; | |||||
633 | ||||||
634 | case '8' : | |||||
635 | countdata = va_arg (argptr, sf_count_t)__builtin_va_arg(argptr, sf_count_t) ; | |||||
636 | if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_FALSE) | |||||
637 | { header_put_be_8byte (psf, countdata) ; | |||||
638 | count += 8 ; | |||||
639 | } | |||||
640 | else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_FALSE) | |||||
641 | { header_put_le_8byte (psf, countdata) ; | |||||
642 | count += 8 ; | |||||
643 | } | |||||
644 | else if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_TRUE) | |||||
645 | { longdata = countdata & 0xFFFFFFFF ; | |||||
646 | header_put_be_int (psf, longdata) ; | |||||
647 | count += 4 ; | |||||
648 | } | |||||
649 | else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_TRUE) | |||||
650 | { longdata = countdata & 0xFFFFFFFF ; | |||||
651 | header_put_le_int (psf, longdata) ; | |||||
652 | count += 4 ; | |||||
653 | } | |||||
654 | break ; | |||||
655 | ||||||
656 | case 'f' : | |||||
657 | /* Floats are passed as doubles. Is this always true? */ | |||||
658 | floatdata = (float) va_arg (argptr, double)__builtin_va_arg(argptr, double) ; | |||||
659 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
660 | float32_be_write (floatdata, psf->header + psf->headindex) ; | |||||
661 | else | |||||
662 | float32_le_write (floatdata, psf->header + psf->headindex) ; | |||||
663 | psf->headindex += 4 ; | |||||
664 | count += 4 ; | |||||
665 | break ; | |||||
666 | ||||||
667 | case 'd' : | |||||
668 | doubledata = va_arg (argptr, double)__builtin_va_arg(argptr, double) ; | |||||
669 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
670 | double64_be_write (doubledata, psf->header + psf->headindex) ; | |||||
671 | else | |||||
672 | double64_le_write (doubledata, psf->header + psf->headindex) ; | |||||
673 | psf->headindex += 8 ; | |||||
674 | count += 8 ; | |||||
675 | break ; | |||||
676 | ||||||
677 | case 's' : | |||||
678 | /* Write a C string (guaranteed to have a zero terminator). */ | |||||
679 | strptr = va_arg (argptr, char *)__builtin_va_arg(argptr, char *) ; | |||||
680 | size = strlen (strptr) + 1 ; | |||||
681 | size += (size & 1) ; | |||||
682 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
683 | header_put_be_int (psf, size) ; | |||||
684 | else | |||||
685 | header_put_le_int (psf, size) ; | |||||
686 | memcpy (&(psf->header [psf->headindex]), strptr, size) ; | |||||
687 | psf->headindex += size ; | |||||
688 | psf->header [psf->headindex - 1] = 0 ; | |||||
689 | count += 4 + size ; | |||||
690 | break ; | |||||
691 | ||||||
692 | case 'S' : | |||||
693 | /* | |||||
694 | ** Write an AIFF style string (no zero terminator but possibly | |||||
695 | ** an extra pad byte if the string length is odd). | |||||
696 | */ | |||||
697 | strptr = va_arg (argptr, char *)__builtin_va_arg(argptr, char *) ; | |||||
698 | size = strlen (strptr) ; | |||||
699 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
700 | header_put_be_int (psf, size) ; | |||||
701 | else | |||||
702 | header_put_le_int (psf, size) ; | |||||
703 | memcpy (&(psf->header [psf->headindex]), strptr, size + 1) ; | |||||
704 | size += (size & 1) ; | |||||
705 | psf->headindex += size ; | |||||
706 | psf->header [psf->headindex] = 0 ; | |||||
707 | count += 4 + size ; | |||||
708 | break ; | |||||
709 | ||||||
710 | case 'b' : | |||||
711 | bindata = va_arg (argptr, void *)__builtin_va_arg(argptr, void *) ; | |||||
712 | size = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
713 | memcpy (&(psf->header [psf->headindex]), bindata, size) ; | |||||
714 | psf->headindex += size ; | |||||
715 | count += size ; | |||||
716 | break ; | |||||
717 | ||||||
718 | case 'z' : | |||||
719 | size = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
720 | count += size ; | |||||
721 | while (size) | |||||
722 | { psf->header [psf->headindex] = 0 ; | |||||
723 | psf->headindex ++ ; | |||||
724 | size -- ; | |||||
725 | } ; | |||||
726 | break ; | |||||
727 | ||||||
728 | case 'h' : | |||||
729 | bindata = va_arg (argptr, void *)__builtin_va_arg(argptr, void *) ; | |||||
730 | memcpy (&(psf->header [psf->headindex]), bindata, 16) ; | |||||
731 | psf->headindex += 16 ; | |||||
732 | count += 16 ; | |||||
733 | break ; | |||||
734 | ||||||
735 | case 'j' : | |||||
736 | size = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
737 | psf->headindex += size ; | |||||
738 | count = size ; | |||||
739 | break ; | |||||
740 | ||||||
741 | default : | |||||
742 | psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ; | |||||
743 | psf->error = SFE_INTERNAL ; | |||||
744 | break ; | |||||
745 | } ; | |||||
746 | } ; | |||||
747 | ||||||
748 | va_end (argptr)__builtin_va_end(argptr) ; | |||||
749 | return count ; | |||||
750 | } /* psf_binheader_writef */ | |||||
751 | ||||||
752 | /*----------------------------------------------------------------------------------------------- | |||||
753 | ** Binary header reading functions. Returns number of bytes read. | |||||
754 | ** | |||||
755 | ** Format specifiers are the same as for header write function above with the following | |||||
756 | ** additions: | |||||
757 | ** | |||||
758 | ** p - jump a given number of position from start of file. | |||||
759 | ** | |||||
760 | ** If format is NULL, psf_binheader_readf returns the current offset. | |||||
761 | */ | |||||
762 | ||||||
763 | #if (CPU_IS_BIG_ENDIAN0 == 1) | |||||
764 | #define GET_MARKER(ptr)( ((ptr) [0]) | ((ptr) [1] << 8) | ((ptr) [2] << 16 ) | ((ptr) [3] << 24)) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \ | |||||
765 | ((ptr) [2] << 8) | ((ptr) [3])) | |||||
766 | ||||||
767 | #elif (CPU_IS_LITTLE_ENDIAN1 == 1) | |||||
768 | #define GET_MARKER(ptr)( ((ptr) [0]) | ((ptr) [1] << 8) | ((ptr) [2] << 16 ) | ((ptr) [3] << 24)) ( ((ptr) [0]) | ((ptr) [1] << 8) | \ | |||||
769 | ((ptr) [2] << 16) | ((ptr) [3] << 24)) | |||||
770 | ||||||
771 | #else | |||||
772 | # error "Cannot determine endian-ness of processor." | |||||
773 | #endif | |||||
774 | ||||||
775 | #define GET_LE_SHORT(ptr)(((ptr) [1] << 8) | ((ptr) [0])) (((ptr) [1] << 8) | ((ptr) [0])) | |||||
776 | #define GET_BE_SHORT(ptr)(((ptr) [0] << 8) | ((ptr) [1])) (((ptr) [0] << 8) | ((ptr) [1])) | |||||
777 | ||||||
778 | #define GET_LE_3BYTE(ptr)( ((ptr) [2] << 16) | ((ptr) [1] << 8) | ((ptr) [ 0])) ( ((ptr) [2] << 16) | ((ptr) [1] << 8) | ((ptr) [0])) | |||||
779 | #define GET_BE_3BYTE(ptr)( ((ptr) [0] << 16) | ((ptr) [1] << 8) | ((ptr) [ 2])) ( ((ptr) [0] << 16) | ((ptr) [1] << 8) | ((ptr) [2])) | |||||
780 | ||||||
781 | #define GET_LE_INT(ptr)( ((ptr) [3] << 24) | ((ptr) [2] << 16) | ((ptr) [ 1] << 8) | ((ptr) [0])) ( ((ptr) [3] << 24) | ((ptr) [2] << 16) | \ | |||||
782 | ((ptr) [1] << 8) | ((ptr) [0])) | |||||
783 | ||||||
784 | #define GET_BE_INT(ptr)( ((ptr) [0] << 24) | ((ptr) [1] << 16) | ((ptr) [ 2] << 8) | ((ptr) [3])) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \ | |||||
785 | ((ptr) [2] << 8) | ((ptr) [3])) | |||||
786 | ||||||
787 | #define GET_LE_8BYTE(ptr)( (((sf_count_t) (ptr) [7]) << 56) | (((sf_count_t) (ptr ) [6]) << 48) | (((sf_count_t) (ptr) [5]) << 40) | (((sf_count_t) (ptr) [4]) << 32) | (((sf_count_t) (ptr ) [3]) << 24) | (((sf_count_t) (ptr) [2]) << 16) | (((sf_count_t) (ptr) [1]) << 8) | ((ptr) [0])) ( (((sf_count_t) (ptr) [7]) << 56) | (((sf_count_t) (ptr) [6]) << 48) | \ | |||||
788 | (((sf_count_t) (ptr) [5]) << 40) | (((sf_count_t) (ptr) [4]) << 32) | \ | |||||
789 | (((sf_count_t) (ptr) [3]) << 24) | (((sf_count_t) (ptr) [2]) << 16) | \ | |||||
790 | (((sf_count_t) (ptr) [1]) << 8) | ((ptr) [0])) | |||||
791 | ||||||
792 | #define GET_BE_8BYTE(ptr)( (((sf_count_t) (ptr) [0]) << 56) | (((sf_count_t) (ptr ) [1]) << 48) | (((sf_count_t) (ptr) [2]) << 40) | (((sf_count_t) (ptr) [3]) << 32) | (((sf_count_t) (ptr ) [4]) << 24) | (((sf_count_t) (ptr) [5]) << 16) | (((sf_count_t) (ptr) [6]) << 8) | ((ptr) [7])) ( (((sf_count_t) (ptr) [0]) << 56) | (((sf_count_t) (ptr) [1]) << 48) | \ | |||||
793 | (((sf_count_t) (ptr) [2]) << 40) | (((sf_count_t) (ptr) [3]) << 32) | \ | |||||
794 | (((sf_count_t) (ptr) [4]) << 24) | (((sf_count_t) (ptr) [5]) << 16) | \ | |||||
795 | (((sf_count_t) (ptr) [6]) << 8) | ((ptr) [7])) | |||||
796 | ||||||
797 | ||||||
798 | ||||||
799 | static int | |||||
800 | header_read (SF_PRIVATE *psf, void *ptr, int bytes) | |||||
801 | { int count = 0 ; | |||||
802 | ||||||
803 | if (psf->headindex >= SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header))) | |||||
804 | { memset (ptr, 0, SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - psf->headindex) ; | |||||
805 | ||||||
806 | /* This is the best that we can do. */ | |||||
807 | psf_fseek (psf, bytes, SEEK_CUR1) ; | |||||
808 | return bytes ; | |||||
809 | } ; | |||||
810 | ||||||
811 | if (psf->headindex + bytes > SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header))) | |||||
812 | { int most ; | |||||
813 | ||||||
814 | most = SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header)) - psf->headindex ; | |||||
815 | psf_fread (psf->header + psf->headend, 1, most, psf) ; | |||||
816 | memset ((char *) ptr + most, 0, bytes - most) ; | |||||
817 | ||||||
818 | psf_fseek (psf, bytes - most, SEEK_CUR1) ; | |||||
819 | return bytes ; | |||||
820 | } ; | |||||
821 | ||||||
822 | if (psf->headindex + bytes > psf->headend) | |||||
823 | { count = psf_fread (psf->header + psf->headend, 1, bytes - (psf->headend - psf->headindex), psf) ; | |||||
824 | if (count != bytes - (int) (psf->headend - psf->headindex)) | |||||
825 | { psf_log_printf (psf, "Error : psf_fread returned short count.\n") ; | |||||
826 | return 0 ; | |||||
827 | } ; | |||||
828 | psf->headend += count ; | |||||
829 | } ; | |||||
830 | ||||||
831 | memcpy (ptr, psf->header + psf->headindex, bytes) ; | |||||
832 | psf->headindex += bytes ; | |||||
833 | ||||||
834 | return bytes ; | |||||
835 | } /* header_read */ | |||||
836 | ||||||
837 | static void | |||||
838 | header_seek (SF_PRIVATE *psf, sf_count_t position, int whence) | |||||
839 | { | |||||
840 | ||||||
841 | switch (whence) | |||||
842 | { case SEEK_SET0 : | |||||
843 | if (position > SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header))) | |||||
844 | { /* Too much header to cache so just seek instead. */ | |||||
845 | psf_fseek (psf, position, whence) ; | |||||
846 | return ; | |||||
847 | } ; | |||||
848 | if (position > psf->headend) | |||||
849 | psf->headend += psf_fread (psf->header + psf->headend, 1, position - psf->headend, psf) ; | |||||
850 | psf->headindex = position ; | |||||
851 | break ; | |||||
852 | ||||||
853 | case SEEK_CUR1 : | |||||
854 | if (psf->headindex + position < 0) | |||||
855 | break ; | |||||
856 | ||||||
857 | if (psf->headindex >= SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header))) | |||||
858 | { psf_fseek (psf, position, whence) ; | |||||
859 | return ; | |||||
860 | } ; | |||||
861 | ||||||
862 | if (psf->headindex + position <= psf->headend) | |||||
863 | { psf->headindex += position ; | |||||
864 | break ; | |||||
865 | } ; | |||||
866 | ||||||
867 | if (psf->headindex + position > SIGNED_SIZEOF (psf->header)((int) sizeof (psf->header))) | |||||
868 | { /* Need to jump this without caching it. */ | |||||
869 | psf->headindex = psf->headend ; | |||||
870 | psf_fseek (psf, position, SEEK_CUR1) ; | |||||
871 | break ; | |||||
872 | } ; | |||||
873 | ||||||
874 | psf->headend += psf_fread (psf->header + psf->headend, 1, position - (psf->headend - psf->headindex), psf) ; | |||||
875 | psf->headindex = psf->headend ; | |||||
876 | break ; | |||||
877 | ||||||
878 | case SEEK_END2 : | |||||
879 | default : | |||||
880 | psf_log_printf (psf, "Bad whence param in header_seek().\n") ; | |||||
881 | break ; | |||||
882 | } ; | |||||
883 | ||||||
884 | return ; | |||||
885 | } /* header_seek */ | |||||
886 | ||||||
887 | static int | |||||
888 | header_gets (SF_PRIVATE *psf, char *ptr, int bufsize) | |||||
889 | { | |||||
890 | int k ; | |||||
891 | ||||||
892 | for (k = 0 ; k < bufsize - 1 ; k++) | |||||
893 | { if (psf->headindex < psf->headend) | |||||
894 | { ptr [k] = psf->header [psf->headindex] ; | |||||
895 | psf->headindex ++ ; | |||||
896 | } | |||||
897 | else | |||||
898 | { psf->headend += psf_fread (psf->header + psf->headend, 1, 1, psf) ; | |||||
899 | ptr [k] = psf->header [psf->headindex] ; | |||||
900 | psf->headindex = psf->headend ; | |||||
901 | } ; | |||||
902 | ||||||
903 | if (ptr [k] == '\n') | |||||
904 | break ; | |||||
905 | } ; | |||||
906 | ||||||
907 | ptr [k] = 0 ; | |||||
908 | ||||||
909 | return k ; | |||||
910 | } /* header_gets */ | |||||
911 | ||||||
912 | int | |||||
913 | psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...) | |||||
914 | { va_list argptr ; | |||||
915 | sf_count_t *countptr, countdata ; | |||||
916 | unsigned char *ucptr, sixteen_bytes [16] ; | |||||
917 | unsigned int *intptr, intdata ; | |||||
918 | unsigned short *shortptr ; | |||||
919 | char *charptr ; | |||||
920 | float *floatptr ; | |||||
921 | double *doubleptr ; | |||||
922 | char c ; | |||||
923 | int byte_count = 0, count ; | |||||
924 | ||||||
925 | if (! format) | |||||
| ||||||
926 | return psf_ftell (psf) ; | |||||
927 | ||||||
928 | va_start (argptr, format)__builtin_va_start(argptr, format) ; | |||||
929 | ||||||
930 | while ((c = *format++)) | |||||
931 | { switch (c) | |||||
932 | { case 'e' : /* All conversions are now from LE to host. */ | |||||
933 | psf->rwf_endian = SF_ENDIAN_LITTLE ; | |||||
934 | break ; | |||||
935 | ||||||
936 | case 'E' : /* All conversions are now from BE to host. */ | |||||
937 | psf->rwf_endian = SF_ENDIAN_BIG ; | |||||
938 | break ; | |||||
939 | ||||||
940 | case 'm' : | |||||
941 | intptr = va_arg (argptr, unsigned int*)__builtin_va_arg(argptr, unsigned int*) ; | |||||
942 | ucptr = (unsigned char*) intptr ; | |||||
943 | byte_count += header_read (psf, ucptr, sizeof (int)) ; | |||||
944 | *intptr = GET_MARKER (ucptr)( ((ucptr) [0]) | ((ucptr) [1] << 8) | ((ucptr) [2] << 16) | ((ucptr) [3] << 24)) ; | |||||
945 | break ; | |||||
946 | ||||||
947 | case 'h' : | |||||
948 | intptr = va_arg (argptr, unsigned int*)__builtin_va_arg(argptr, unsigned int*) ; | |||||
949 | ucptr = (unsigned char*) intptr ; | |||||
950 | byte_count += header_read (psf, sixteen_bytes, sizeof (sixteen_bytes)) ; | |||||
951 | { int k ; | |||||
952 | intdata = 0 ; | |||||
953 | for (k = 0 ; k < 16 ; k++) | |||||
954 | intdata ^= sixteen_bytes [k] << k ; | |||||
955 | } | |||||
956 | *intptr = intdata ; | |||||
957 | break ; | |||||
958 | ||||||
959 | case '1' : | |||||
960 | charptr = va_arg (argptr, char*)__builtin_va_arg(argptr, char*) ; | |||||
961 | *charptr = 0 ; | |||||
962 | byte_count += header_read (psf, charptr, sizeof (char)) ; | |||||
963 | break ; | |||||
964 | ||||||
965 | case '2' : | |||||
966 | shortptr = va_arg (argptr, unsigned short*)__builtin_va_arg(argptr, unsigned short*) ; | |||||
967 | *shortptr = 0 ; | |||||
968 | ucptr = (unsigned char*) shortptr ; | |||||
969 | byte_count += header_read (psf, ucptr, sizeof (short)) ; | |||||
970 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
971 | *shortptr = GET_BE_SHORT (ucptr)(((ucptr) [0] << 8) | ((ucptr) [1])) ; | |||||
972 | else | |||||
973 | *shortptr = GET_LE_SHORT (ucptr)(((ucptr) [1] << 8) | ((ucptr) [0])) ; | |||||
974 | break ; | |||||
975 | ||||||
976 | case '3' : | |||||
977 | intptr = va_arg (argptr, unsigned int*)__builtin_va_arg(argptr, unsigned int*) ; | |||||
978 | *intptr = 0 ; | |||||
979 | byte_count += header_read (psf, sixteen_bytes, 3) ; | |||||
980 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
981 | *intptr = GET_BE_3BYTE (sixteen_bytes)( ((sixteen_bytes) [0] << 16) | ((sixteen_bytes) [1] << 8) | ((sixteen_bytes) [2])) ; | |||||
982 | else | |||||
983 | *intptr = GET_LE_3BYTE (sixteen_bytes)( ((sixteen_bytes) [2] << 16) | ((sixteen_bytes) [1] << 8) | ((sixteen_bytes) [0])) ; | |||||
| ||||||
984 | break ; | |||||
985 | ||||||
986 | case '4' : | |||||
987 | intptr = va_arg (argptr, unsigned int*)__builtin_va_arg(argptr, unsigned int*) ; | |||||
988 | *intptr = 0 ; | |||||
989 | ucptr = (unsigned char*) intptr ; | |||||
990 | byte_count += header_read (psf, ucptr, sizeof (int)) ; | |||||
991 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
992 | *intptr = GET_BE_INT (ucptr)( ((ucptr) [0] << 24) | ((ucptr) [1] << 16) | ((ucptr ) [2] << 8) | ((ucptr) [3])) ; | |||||
993 | else | |||||
994 | *intptr = GET_LE_INT (ucptr)( ((ucptr) [3] << 24) | ((ucptr) [2] << 16) | ((ucptr ) [1] << 8) | ((ucptr) [0])) ; | |||||
995 | break ; | |||||
996 | ||||||
997 | case '8' : | |||||
998 | countptr = va_arg (argptr, sf_count_t *)__builtin_va_arg(argptr, sf_count_t *) ; | |||||
999 | *countptr = 0 ; | |||||
1000 | byte_count += header_read (psf, sixteen_bytes, 8) ; | |||||
1001 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
1002 | countdata = GET_BE_8BYTE (sixteen_bytes)( (((sf_count_t) (sixteen_bytes) [0]) << 56) | (((sf_count_t ) (sixteen_bytes) [1]) << 48) | (((sf_count_t) (sixteen_bytes ) [2]) << 40) | (((sf_count_t) (sixteen_bytes) [3]) << 32) | (((sf_count_t) (sixteen_bytes) [4]) << 24) | ((( sf_count_t) (sixteen_bytes) [5]) << 16) | (((sf_count_t ) (sixteen_bytes) [6]) << 8) | ((sixteen_bytes) [7])) ; | |||||
1003 | else | |||||
1004 | countdata = GET_LE_8BYTE (sixteen_bytes)( (((sf_count_t) (sixteen_bytes) [7]) << 56) | (((sf_count_t ) (sixteen_bytes) [6]) << 48) | (((sf_count_t) (sixteen_bytes ) [5]) << 40) | (((sf_count_t) (sixteen_bytes) [4]) << 32) | (((sf_count_t) (sixteen_bytes) [3]) << 24) | ((( sf_count_t) (sixteen_bytes) [2]) << 16) | (((sf_count_t ) (sixteen_bytes) [1]) << 8) | ((sixteen_bytes) [0])) ; | |||||
1005 | *countptr = countdata ; | |||||
1006 | break ; | |||||
1007 | ||||||
1008 | case 'f' : /* Float conversion */ | |||||
1009 | floatptr = va_arg (argptr, float *)__builtin_va_arg(argptr, float *) ; | |||||
1010 | *floatptr = 0.0 ; | |||||
1011 | byte_count += header_read (psf, floatptr, sizeof (float)) ; | |||||
1012 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
1013 | *floatptr = float32_be_read ((unsigned char*) floatptr) ; | |||||
1014 | else | |||||
1015 | *floatptr = float32_le_read ((unsigned char*) floatptr) ; | |||||
1016 | break ; | |||||
1017 | ||||||
1018 | case 'd' : /* double conversion */ | |||||
1019 | doubleptr = va_arg (argptr, double *)__builtin_va_arg(argptr, double *) ; | |||||
1020 | *doubleptr = 0.0 ; | |||||
1021 | byte_count += header_read (psf, doubleptr, sizeof (double)) ; | |||||
1022 | if (psf->rwf_endian == SF_ENDIAN_BIG) | |||||
1023 | *doubleptr = double64_be_read ((unsigned char*) doubleptr) ; | |||||
1024 | else | |||||
1025 | *doubleptr = double64_le_read ((unsigned char*) doubleptr) ; | |||||
1026 | break ; | |||||
1027 | ||||||
1028 | case 's' : | |||||
1029 | psf_log_printf (psf, "Format conversion 's' not implemented yet.\n") ; | |||||
1030 | /* | |||||
1031 | strptr = va_arg (argptr, char *) ; | |||||
1032 | size = strlen (strptr) + 1 ; | |||||
1033 | size += (size & 1) ; | |||||
1034 | longdata = H2LE_32 (size) ; | |||||
1035 | get_int (psf, longdata) ; | |||||
1036 | memcpy (&(psf->header [psf->headindex]), strptr, size) ; | |||||
1037 | psf->headindex += size ; | |||||
1038 | */ | |||||
1039 | break ; | |||||
1040 | ||||||
1041 | case 'b' : | |||||
1042 | charptr = va_arg (argptr, char*)__builtin_va_arg(argptr, char*) ; | |||||
1043 | count = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
1044 | if (count > 0) | |||||
1045 | byte_count += header_read (psf, charptr, count) ; | |||||
1046 | break ; | |||||
1047 | ||||||
1048 | case 'G' : | |||||
1049 | charptr = va_arg (argptr, char*)__builtin_va_arg(argptr, char*) ; | |||||
1050 | count = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
1051 | if (count > 0) | |||||
1052 | byte_count += header_gets (psf, charptr, count) ; | |||||
1053 | break ; | |||||
1054 | ||||||
1055 | case 'z' : | |||||
1056 | psf_log_printf (psf, "Format conversion 'z' not implemented yet.\n") ; | |||||
1057 | /* | |||||
1058 | size = va_arg (argptr, size_t) ; | |||||
1059 | while (size) | |||||
1060 | { psf->header [psf->headindex] = 0 ; | |||||
1061 | psf->headindex ++ ; | |||||
1062 | size -- ; | |||||
1063 | } ; | |||||
1064 | */ | |||||
1065 | break ; | |||||
1066 | ||||||
1067 | case 'p' : | |||||
1068 | /* Get the seek position first. */ | |||||
1069 | count = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
1070 | header_seek (psf, count, SEEK_SET0) ; | |||||
1071 | byte_count = count ; | |||||
1072 | break ; | |||||
1073 | ||||||
1074 | case 'j' : | |||||
1075 | /* Get the seek position first. */ | |||||
1076 | count = va_arg (argptr, size_t)__builtin_va_arg(argptr, size_t) ; | |||||
1077 | header_seek (psf, count, SEEK_CUR1) ; | |||||
1078 | byte_count += count ; | |||||
1079 | break ; | |||||
1080 | ||||||
1081 | default : | |||||
1082 | psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ; | |||||
1083 | psf->error = SFE_INTERNAL ; | |||||
1084 | break ; | |||||
1085 | } ; | |||||
1086 | } ; | |||||
1087 | ||||||
1088 | va_end (argptr)__builtin_va_end(argptr) ; | |||||
1089 | ||||||
1090 | return byte_count ; | |||||
1091 | } /* psf_binheader_readf */ | |||||
1092 | ||||||
1093 | /*----------------------------------------------------------------------------------------------- | |||||
1094 | */ | |||||
1095 | ||||||
1096 | sf_count_t | |||||
1097 | psf_default_seek (SF_PRIVATE *psf, int UNUSED (mode)UNUSED_mode __attribute__ ((unused)), sf_count_t samples_from_start) | |||||
1098 | { sf_count_t position, retval ; | |||||
1099 | ||||||
1100 | if (! (psf->blockwidth && psf->dataoffset >= 0)) | |||||
1101 | { psf->error = SFE_BAD_SEEK ; | |||||
1102 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||||
1103 | } ; | |||||
1104 | ||||||
1105 | if (! psf->sf.seekable) | |||||
1106 | { psf->error = SFE_NOT_SEEKABLE ; | |||||
1107 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||||
1108 | } ; | |||||
1109 | ||||||
1110 | position = psf->dataoffset + psf->blockwidth * samples_from_start ; | |||||
1111 | ||||||
1112 | if ((retval = psf_fseek (psf, position, SEEK_SET0)) != position) | |||||
1113 | { psf->error = SFE_SEEK_FAILED ; | |||||
1114 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||||
1115 | } ; | |||||
1116 | ||||||
1117 | return samples_from_start ; | |||||
1118 | } /* psf_default_seek */ | |||||
1119 | ||||||
1120 | /*----------------------------------------------------------------------------------------------- | |||||
1121 | */ | |||||
1122 | ||||||
1123 | void | |||||
1124 | psf_hexdump (const void *ptr, int len) | |||||
1125 | { const char *data ; | |||||
1126 | char ascii [17] ; | |||||
1127 | int k, m ; | |||||
1128 | ||||||
1129 | if ((data = ptr) == NULL((void*)0)) | |||||
1130 | return ; | |||||
1131 | if (len <= 0) | |||||
1132 | return ; | |||||
1133 | ||||||
1134 | puts ("") ; | |||||
1135 | for (k = 0 ; k < len ; k += 16) | |||||
1136 | { memset (ascii, ' ', sizeof (ascii)) ; | |||||
1137 | ||||||
1138 | printf ("%08X: ", k)__printf_chk (2 - 1, "%08X: ", k) ; | |||||
1139 | for (m = 0 ; m < 16 && k + m < len ; m++) | |||||
1140 | { printf (m == 8 ? " %02X " : "%02X ", data [k + m] & 0xFF)__printf_chk (2 - 1, m == 8 ? " %02X " : "%02X ", data [k + m ] & 0xFF) ; | |||||
1141 | ascii [m] = psf_isprint (data [k + m]) ? data [k + m] : '.' ; | |||||
1142 | } ; | |||||
1143 | ||||||
1144 | if (m <= 8) printf (" ")__printf_chk (2 - 1, " ") ; | |||||
1145 | for ( ; m < 16 ; m++) printf (" ")__printf_chk (2 - 1, " ") ; | |||||
1146 | ||||||
1147 | ascii [16] = 0 ; | |||||
1148 | printf (" %s\n", ascii)__printf_chk (2 - 1, " %s\n", ascii) ; | |||||
1149 | } ; | |||||
1150 | ||||||
1151 | puts ("") ; | |||||
1152 | } /* psf_hexdump */ | |||||
1153 | ||||||
1154 | void | |||||
1155 | psf_log_SF_INFO (SF_PRIVATE *psf) | |||||
1156 | { psf_log_printf (psf, "---------------------------------\n") ; | |||||
1157 | ||||||
1158 | psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ; | |||||
1159 | if (psf->sf.frames == SF_COUNT_MAX0x7FFFFFFFFFFFFFFFLL) | |||||
1160 | psf_log_printf (psf, " Frames : unknown\n") ; | |||||
1161 | else | |||||
1162 | psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ; | |||||
1163 | psf_log_printf (psf, " Channels : %d\n", psf->sf.channels) ; | |||||
1164 | ||||||
1165 | psf_log_printf (psf, " Format : 0x%X\n", psf->sf.format) ; | |||||
1166 | psf_log_printf (psf, " Sections : %d\n", psf->sf.sections) ; | |||||
1167 | psf_log_printf (psf, " Seekable : %s\n", psf->sf.seekable ? "TRUE" : "FALSE") ; | |||||
1168 | ||||||
1169 | psf_log_printf (psf, "---------------------------------\n") ; | |||||
1170 | } /* psf_dump_SFINFO */ | |||||
1171 | ||||||
1172 | /*======================================================================================== | |||||
1173 | */ | |||||
1174 | ||||||
1175 | void* | |||||
1176 | psf_memset (void *s, int c, sf_count_t len) | |||||
1177 | { char *ptr ; | |||||
1178 | int setcount ; | |||||
1179 | ||||||
1180 | ptr = (char *) s ; | |||||
1181 | ||||||
1182 | while (len > 0) | |||||
1183 | { setcount = (len > 0x10000000) ? 0x10000000 : (int) len ; | |||||
1184 | ||||||
1185 | memset (ptr, c, setcount) ; | |||||
1186 | ||||||
1187 | ptr += setcount ; | |||||
1188 | len -= setcount ; | |||||
1189 | } ; | |||||
1190 | ||||||
1191 | return s ; | |||||
1192 | } /* psf_memset */ | |||||
1193 | ||||||
1194 | SF_INSTRUMENT * | |||||
1195 | psf_instrument_alloc (void) | |||||
1196 | { SF_INSTRUMENT *instr ; | |||||
1197 | ||||||
1198 | instr = calloc (1, sizeof (SF_INSTRUMENT)) ; | |||||
1199 | ||||||
1200 | if (instr == NULL((void*)0)) | |||||
1201 | return NULL((void*)0) ; | |||||
1202 | ||||||
1203 | /* Set non-zero default values. */ | |||||
1204 | instr->basenote = -1 ; | |||||
1205 | instr->velocity_lo = -1 ; | |||||
1206 | instr->velocity_hi = -1 ; | |||||
1207 | instr->key_lo = -1 ; | |||||
1208 | instr->key_hi = -1 ; | |||||
1209 | ||||||
1210 | return instr ; | |||||
1211 | } /* psf_instrument_alloc */ | |||||
1212 | ||||||
1213 | void | |||||
1214 | psf_sanitize_string (char * cptr, int len) | |||||
1215 | { | |||||
1216 | do | |||||
1217 | { | |||||
1218 | len -- ; | |||||
1219 | cptr [len] = psf_isprint (cptr [len]) ? cptr [len] : '.' ; | |||||
1220 | } | |||||
1221 | while (len > 0) ; | |||||
1222 | } /* psf_sanitize_string */ | |||||
1223 | ||||||
1224 | void | |||||
1225 | psf_get_date_str (char *str, int maxlen) | |||||
1226 | { time_t current ; | |||||
1227 | struct tm timedata, *tmptr ; | |||||
1228 | ||||||
1229 | time (¤t) ; | |||||
1230 | ||||||
1231 | #if defined (HAVE_GMTIME_R1) | |||||
1232 | /* If the re-entrant version is available, use it. */ | |||||
1233 | tmptr = gmtime_r (¤t, &timedata) ; | |||||
1234 | #elif defined (HAVE_GMTIME1) | |||||
1235 | /* Otherwise use the standard one and copy the data to local storage. */ | |||||
1236 | tmptr = gmtime (¤t) ; | |||||
1237 | memcpy (&timedata, tmptr, sizeof (timedata)) ; | |||||
1238 | #else | |||||
1239 | tmptr = NULL((void*)0) ; | |||||
1240 | #endif | |||||
1241 | ||||||
1242 | if (tmptr) | |||||
1243 | snprintf (str, maxlen, "%4d-%02d-%02d %02d:%02d:%02d UTC",__builtin___snprintf_chk (str, maxlen, 2 - 1, __builtin_object_size (str, 2 > 1), "%4d-%02d-%02d %02d:%02d:%02d UTC", 1900 + timedata .tm_year, timedata.tm_mon, timedata.tm_mday, timedata.tm_hour , timedata.tm_min, timedata.tm_sec) | |||||
1244 | 1900 + timedata.tm_year, timedata.tm_mon, timedata.tm_mday,__builtin___snprintf_chk (str, maxlen, 2 - 1, __builtin_object_size (str, 2 > 1), "%4d-%02d-%02d %02d:%02d:%02d UTC", 1900 + timedata .tm_year, timedata.tm_mon, timedata.tm_mday, timedata.tm_hour , timedata.tm_min, timedata.tm_sec) | |||||
1245 | timedata.tm_hour, timedata.tm_min, timedata.tm_sec)__builtin___snprintf_chk (str, maxlen, 2 - 1, __builtin_object_size (str, 2 > 1), "%4d-%02d-%02d %02d:%02d:%02d UTC", 1900 + timedata .tm_year, timedata.tm_mon, timedata.tm_mday, timedata.tm_hour , timedata.tm_min, timedata.tm_sec) ; | |||||
1246 | else | |||||
1247 | snprintf (str, maxlen, "Unknown date")__builtin___snprintf_chk (str, maxlen, 2 - 1, __builtin_object_size (str, 2 > 1), "Unknown date") ; | |||||
1248 | ||||||
1249 | return ; | |||||
1250 | } /* psf_get_date_str */ | |||||
1251 | ||||||
1252 | int | |||||
1253 | subformat_to_bytewidth (int format) | |||||
1254 | { | |||||
1255 | switch (format) | |||||
1256 | { case SF_FORMAT_PCM_U8 : | |||||
1257 | case SF_FORMAT_PCM_S8 : | |||||
1258 | return 1 ; | |||||
1259 | case SF_FORMAT_PCM_16 : | |||||
1260 | return 2 ; | |||||
1261 | case SF_FORMAT_PCM_24 : | |||||
1262 | return 3 ; | |||||
1263 | case SF_FORMAT_PCM_32 : | |||||
1264 | case SF_FORMAT_FLOAT : | |||||
1265 | return 4 ; | |||||
1266 | case SF_FORMAT_DOUBLE : | |||||
1267 | return 8 ; | |||||
1268 | } ; | |||||
1269 | ||||||
1270 | return 0 ; | |||||
1271 | } /* subformat_to_bytewidth */ | |||||
1272 | ||||||
1273 | int | |||||
1274 | s_bitwidth_to_subformat (int bits) | |||||
1275 | { static int array [] = | |||||
1276 | { SF_FORMAT_PCM_S8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32 | |||||
1277 | } ; | |||||
1278 | ||||||
1279 | if (bits < 8 || bits > 32) | |||||
1280 | return 0 ; | |||||
1281 | ||||||
1282 | return array [((bits + 7) / 8) - 1] ; | |||||
1283 | } /* bitwidth_to_subformat */ | |||||
1284 | ||||||
1285 | int | |||||
1286 | u_bitwidth_to_subformat (int bits) | |||||
1287 | { static int array [] = | |||||
1288 | { SF_FORMAT_PCM_U8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32 | |||||
1289 | } ; | |||||
1290 | ||||||
1291 | if (bits < 8 || bits > 32) | |||||
1292 | return 0 ; | |||||
1293 | ||||||
1294 | return array [((bits + 7) / 8) - 1] ; | |||||
1295 | } /* bitwidth_to_subformat */ | |||||
1296 | ||||||
1297 | /* | |||||
1298 | ** psf_rand_int32 : Not crypto quality, but more than adequate for things | |||||
1299 | ** like stream serial numbers in Ogg files or the unique_id field of the | |||||
1300 | ** SF_PRIVATE struct. | |||||
1301 | */ | |||||
1302 | ||||||
1303 | int32_t | |||||
1304 | psf_rand_int32 (void) | |||||
1305 | { static int32_t value = -1 ; | |||||
1306 | int k, count ; | |||||
1307 | ||||||
1308 | if (value == -1) | |||||
1309 | { | |||||
1310 | #if HAVE_GETTIMEOFDAY1 | |||||
1311 | struct timeval tv ; | |||||
1312 | gettimeofday (&tv, NULL((void*)0)) ; | |||||
1313 | value = tv.tv_sec + tv.tv_usec ; | |||||
1314 | #else | |||||
1315 | value = time (NULL((void*)0)) ; | |||||
1316 | #endif | |||||
1317 | } ; | |||||
1318 | ||||||
1319 | count = 4 + (value & 7) ; | |||||
1320 | for (k = 0 ; k < count ; k++) | |||||
1321 | value = 11117 * value + 211231 ; | |||||
1322 | ||||||
1323 | return value ; | |||||
1324 | } /* psf_rand_int32 */ | |||||
1325 | ||||||
1326 | void | |||||
1327 | append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) | |||||
1328 | { size_t len = strlen (dest) ; | |||||
1329 | ||||||
1330 | if (len < maxlen) | |||||
1331 | { va_list ap ; | |||||
1332 | ||||||
1333 | va_start (ap, fmt)__builtin_va_start(ap, fmt) ; | |||||
1334 | vsnprintf (dest + len, maxlen - len, fmt, ap) ; | |||||
1335 | va_end (ap)__builtin_va_end(ap) ; | |||||
1336 | } ; | |||||
1337 | ||||||
1338 | return ; | |||||
1339 | } /* append_snprintf */ | |||||
1340 | ||||||
1341 | ||||||
1342 | void | |||||
1343 | psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) | |||||
1344 | { /* Must be minus 2 so it can still expand a single trailing '\n' or '\r'. */ | |||||
1345 | char * destend = dest + destmax - 2 ; | |||||
1346 | const char * srcend = src + srcmax ; | |||||
1347 | ||||||
1348 | while (dest < destend && src < srcend) | |||||
1349 | { if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r')) | |||||
1350 | { *dest++ = '\r' ; | |||||
1351 | *dest++ = '\n' ; | |||||
1352 | src += 2 ; | |||||
1353 | continue ; | |||||
1354 | } ; | |||||
1355 | ||||||
1356 | if (src [0] == '\r') | |||||
1357 | { *dest++ = '\r' ; | |||||
1358 | *dest++ = '\n' ; | |||||
1359 | src += 1 ; | |||||
1360 | continue ; | |||||
1361 | } ; | |||||
1362 | ||||||
1363 | if (src [0] == '\n') | |||||
1364 | { *dest++ = '\r' ; | |||||
1365 | *dest++ = '\n' ; | |||||
1366 | src += 1 ; | |||||
1367 | continue ; | |||||
1368 | } ; | |||||
1369 | ||||||
1370 | *dest++ = *src++ ; | |||||
1371 | } ; | |||||
1372 | ||||||
1373 | /* Make sure dest is terminated. */ | |||||
1374 | *dest = 0 ; | |||||
1375 | } /* psf_strlcpy_crlf */ | |||||
1376 | ||||||
1377 | sf_count_t | |||||
1378 | psf_decode_frame_count (SF_PRIVATE *psf) | |||||
1379 | { sf_count_t count, readlen, total = 0 ; | |||||
1380 | BUF_UNION ubuf ; | |||||
1381 | ||||||
1382 | /* If we're reading from a pipe or the file is too long, just return SF_COUNT_MAX. */ | |||||
1383 | if (psf_is_pipe (psf) || psf->datalength > 0x1000000) | |||||
1384 | return SF_COUNT_MAX0x7FFFFFFFFFFFFFFFLL ; | |||||
1385 | ||||||
1386 | psf_fseek (psf, psf->dataoffset, SEEK_SET0) ; | |||||
1387 | ||||||
1388 | readlen = ARRAY_LEN (ubuf.ibuf)((int) (sizeof (ubuf.ibuf) / sizeof ((ubuf.ibuf) [0]))) / psf->sf.channels ; | |||||
1389 | readlen *= psf->sf.channels ; | |||||
1390 | ||||||
1391 | while ((count = psf->read_int (psf, ubuf.ibuf, readlen)) > 0) | |||||
1392 | total += count ; | |||||
1393 | ||||||
1394 | psf_fseek (psf, psf->dataoffset, SEEK_SET0) ; | |||||
1395 | ||||||
1396 | return total / psf->sf.channels ; | |||||
1397 | } /* psf_decode_frame_count */ | |||||
1398 | ||||||
1399 | /*============================================================================== | |||||
1400 | */ | |||||
1401 | ||||||
1402 | #define CASE_NAME(x)case x : return "x" ; break ; case x : return #x ; break ; | |||||
1403 | ||||||
1404 | const char * | |||||
1405 | str_of_major_format (int format) | |||||
1406 | { switch (SF_CONTAINER (format)((format) & SF_FORMAT_TYPEMASK)) | |||||
1407 | { CASE_NAME (SF_FORMAT_WAV)case SF_FORMAT_WAV : return "SF_FORMAT_WAV" ; break ; ; | |||||
1408 | CASE_NAME (SF_FORMAT_AIFF)case SF_FORMAT_AIFF : return "SF_FORMAT_AIFF" ; break ; ; | |||||
1409 | CASE_NAME (SF_FORMAT_AU)case SF_FORMAT_AU : return "SF_FORMAT_AU" ; break ; ; | |||||
1410 | CASE_NAME (SF_FORMAT_RAW)case SF_FORMAT_RAW : return "SF_FORMAT_RAW" ; break ; ; | |||||
1411 | CASE_NAME (SF_FORMAT_PAF)case SF_FORMAT_PAF : return "SF_FORMAT_PAF" ; break ; ; | |||||
1412 | CASE_NAME (SF_FORMAT_SVX)case SF_FORMAT_SVX : return "SF_FORMAT_SVX" ; break ; ; | |||||
1413 | CASE_NAME (SF_FORMAT_NIST)case SF_FORMAT_NIST : return "SF_FORMAT_NIST" ; break ; ; | |||||
1414 | CASE_NAME (SF_FORMAT_VOC)case SF_FORMAT_VOC : return "SF_FORMAT_VOC" ; break ; ; | |||||
1415 | CASE_NAME (SF_FORMAT_IRCAM)case SF_FORMAT_IRCAM : return "SF_FORMAT_IRCAM" ; break ; ; | |||||
1416 | CASE_NAME (SF_FORMAT_W64)case SF_FORMAT_W64 : return "SF_FORMAT_W64" ; break ; ; | |||||
1417 | CASE_NAME (SF_FORMAT_MAT4)case SF_FORMAT_MAT4 : return "SF_FORMAT_MAT4" ; break ; ; | |||||
1418 | CASE_NAME (SF_FORMAT_MAT5)case SF_FORMAT_MAT5 : return "SF_FORMAT_MAT5" ; break ; ; | |||||
1419 | CASE_NAME (SF_FORMAT_PVF)case SF_FORMAT_PVF : return "SF_FORMAT_PVF" ; break ; ; | |||||
1420 | CASE_NAME (SF_FORMAT_XI)case SF_FORMAT_XI : return "SF_FORMAT_XI" ; break ; ; | |||||
1421 | CASE_NAME (SF_FORMAT_HTK)case SF_FORMAT_HTK : return "SF_FORMAT_HTK" ; break ; ; | |||||
1422 | CASE_NAME (SF_FORMAT_SDS)case SF_FORMAT_SDS : return "SF_FORMAT_SDS" ; break ; ; | |||||
1423 | CASE_NAME (SF_FORMAT_AVR)case SF_FORMAT_AVR : return "SF_FORMAT_AVR" ; break ; ; | |||||
1424 | CASE_NAME (SF_FORMAT_WAVEX)case SF_FORMAT_WAVEX : return "SF_FORMAT_WAVEX" ; break ; ; | |||||
1425 | CASE_NAME (SF_FORMAT_SD2)case SF_FORMAT_SD2 : return "SF_FORMAT_SD2" ; break ; ; | |||||
1426 | CASE_NAME (SF_FORMAT_FLAC)case SF_FORMAT_FLAC : return "SF_FORMAT_FLAC" ; break ; ; | |||||
1427 | CASE_NAME (SF_FORMAT_CAF)case SF_FORMAT_CAF : return "SF_FORMAT_CAF" ; break ; ; | |||||
1428 | CASE_NAME (SF_FORMAT_WVE)case SF_FORMAT_WVE : return "SF_FORMAT_WVE" ; break ; ; | |||||
1429 | CASE_NAME (SF_FORMAT_OGG)case SF_FORMAT_OGG : return "SF_FORMAT_OGG" ; break ; ; | |||||
1430 | default : | |||||
1431 | break ; | |||||
1432 | } ; | |||||
1433 | ||||||
1434 | return "BAD_MAJOR_FORMAT" ; | |||||
1435 | } /* str_of_major_format */ | |||||
1436 | ||||||
1437 | const char * | |||||
1438 | str_of_minor_format (int format) | |||||
1439 | { switch (SF_CODEC (format)((format) & SF_FORMAT_SUBMASK)) | |||||
1440 | { CASE_NAME (SF_FORMAT_PCM_S8)case SF_FORMAT_PCM_S8 : return "SF_FORMAT_PCM_S8" ; break ; ; | |||||
1441 | CASE_NAME (SF_FORMAT_PCM_16)case SF_FORMAT_PCM_16 : return "SF_FORMAT_PCM_16" ; break ; ; | |||||
1442 | CASE_NAME (SF_FORMAT_PCM_24)case SF_FORMAT_PCM_24 : return "SF_FORMAT_PCM_24" ; break ; ; | |||||
1443 | CASE_NAME (SF_FORMAT_PCM_32)case SF_FORMAT_PCM_32 : return "SF_FORMAT_PCM_32" ; break ; ; | |||||
1444 | CASE_NAME (SF_FORMAT_PCM_U8)case SF_FORMAT_PCM_U8 : return "SF_FORMAT_PCM_U8" ; break ; ; | |||||
1445 | CASE_NAME (SF_FORMAT_FLOAT)case SF_FORMAT_FLOAT : return "SF_FORMAT_FLOAT" ; break ; ; | |||||
1446 | CASE_NAME (SF_FORMAT_DOUBLE)case SF_FORMAT_DOUBLE : return "SF_FORMAT_DOUBLE" ; break ; ; | |||||
1447 | CASE_NAME (SF_FORMAT_ULAW)case SF_FORMAT_ULAW : return "SF_FORMAT_ULAW" ; break ; ; | |||||
1448 | CASE_NAME (SF_FORMAT_ALAW)case SF_FORMAT_ALAW : return "SF_FORMAT_ALAW" ; break ; ; | |||||
1449 | CASE_NAME (SF_FORMAT_IMA_ADPCM)case SF_FORMAT_IMA_ADPCM : return "SF_FORMAT_IMA_ADPCM" ; break ; ; | |||||
1450 | CASE_NAME (SF_FORMAT_MS_ADPCM)case SF_FORMAT_MS_ADPCM : return "SF_FORMAT_MS_ADPCM" ; break ; ; | |||||
1451 | CASE_NAME (SF_FORMAT_GSM610)case SF_FORMAT_GSM610 : return "SF_FORMAT_GSM610" ; break ; ; | |||||
1452 | CASE_NAME (SF_FORMAT_VOX_ADPCM)case SF_FORMAT_VOX_ADPCM : return "SF_FORMAT_VOX_ADPCM" ; break ; ; | |||||
1453 | CASE_NAME (SF_FORMAT_G721_32)case SF_FORMAT_G721_32 : return "SF_FORMAT_G721_32" ; break ; ; | |||||
1454 | CASE_NAME (SF_FORMAT_G723_24)case SF_FORMAT_G723_24 : return "SF_FORMAT_G723_24" ; break ; ; | |||||
1455 | CASE_NAME (SF_FORMAT_G723_40)case SF_FORMAT_G723_40 : return "SF_FORMAT_G723_40" ; break ; ; | |||||
1456 | CASE_NAME (SF_FORMAT_DWVW_12)case SF_FORMAT_DWVW_12 : return "SF_FORMAT_DWVW_12" ; break ; ; | |||||
1457 | CASE_NAME (SF_FORMAT_DWVW_16)case SF_FORMAT_DWVW_16 : return "SF_FORMAT_DWVW_16" ; break ; ; | |||||
1458 | CASE_NAME (SF_FORMAT_DWVW_24)case SF_FORMAT_DWVW_24 : return "SF_FORMAT_DWVW_24" ; break ; ; | |||||
1459 | CASE_NAME (SF_FORMAT_DWVW_N)case SF_FORMAT_DWVW_N : return "SF_FORMAT_DWVW_N" ; break ; ; | |||||
1460 | CASE_NAME (SF_FORMAT_DPCM_8)case SF_FORMAT_DPCM_8 : return "SF_FORMAT_DPCM_8" ; break ; ; | |||||
1461 | CASE_NAME (SF_FORMAT_DPCM_16)case SF_FORMAT_DPCM_16 : return "SF_FORMAT_DPCM_16" ; break ; ; | |||||
1462 | CASE_NAME (SF_FORMAT_VORBIS)case SF_FORMAT_VORBIS : return "SF_FORMAT_VORBIS" ; break ; ; | |||||
1463 | default : | |||||
1464 | break ; | |||||
1465 | } ; | |||||
1466 | ||||||
1467 | return "BAD_MINOR_FORMAT" ; | |||||
1468 | } /* str_of_minor_format */ | |||||
1469 | ||||||
1470 | const char * | |||||
1471 | str_of_open_mode (int mode) | |||||
1472 | { switch (mode) | |||||
1473 | { CASE_NAME (SFM_READ)case SFM_READ : return "SFM_READ" ; break ; ; | |||||
1474 | CASE_NAME (SFM_WRITE)case SFM_WRITE : return "SFM_WRITE" ; break ; ; | |||||
1475 | CASE_NAME (SFM_RDWR)case SFM_RDWR : return "SFM_RDWR" ; break ; ; | |||||
1476 | ||||||
1477 | default : | |||||
1478 | break ; | |||||
1479 | } ; | |||||
1480 | ||||||
1481 | return "BAD_MODE" ; | |||||
1482 | } /* str_of_open_mode */ | |||||
1483 | ||||||
1484 | const char * | |||||
1485 | str_of_endianness (int end) | |||||
1486 | { switch (end) | |||||
1487 | { CASE_NAME (SF_ENDIAN_BIG)case SF_ENDIAN_BIG : return "SF_ENDIAN_BIG" ; break ; ; | |||||
1488 | CASE_NAME (SF_ENDIAN_LITTLE)case SF_ENDIAN_LITTLE : return "SF_ENDIAN_LITTLE" ; break ; ; | |||||
1489 | CASE_NAME (SF_ENDIAN_CPU)case SF_ENDIAN_CPU : return "SF_ENDIAN_CPU" ; break ; ; | |||||
1490 | default : | |||||
1491 | break ; | |||||
1492 | } ; | |||||
1493 | ||||||
1494 | /* Zero length string for SF_ENDIAN_FILE. */ | |||||
1495 | return "" ; | |||||
1496 | } /* str_of_endianness */ | |||||
1497 | ||||||
1498 | /*============================================================================== | |||||
1499 | */ | |||||
1500 | ||||||
1501 | void | |||||
1502 | psf_f2s_array (const float *src, short *dest, int count, int normalize) | |||||
1503 | { float normfact ; | |||||
1504 | ||||||
1505 | normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; | |||||
1506 | while (--count >= 0) | |||||
1507 | dest [count] = lrintf (src [count] * normfact) ; | |||||
1508 | ||||||
1509 | return ; | |||||
1510 | } /* psf_f2s_array */ | |||||
1511 | ||||||
1512 | void | |||||
1513 | psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) | |||||
1514 | { float normfact, scaled_value ; | |||||
1515 | ||||||
1516 | normfact = normalize ? (1.0 * 0x8000) : 1.0 ; | |||||
1517 | ||||||
1518 | while (--count >= 0) | |||||
1519 | { scaled_value = src [count] * normfact ; | |||||
1520 | if (CPU_CLIPS_POSITIVE0 == 0 && scaled_value >= (1.0 * 0x7FFF)) | |||||
1521 | { dest [count] = 0x7FFF ; | |||||
1522 | continue ; | |||||
1523 | } ; | |||||
1524 | if (CPU_CLIPS_NEGATIVE0 == 0 && scaled_value <= (-8.0 * 0x1000)) | |||||
1525 | { dest [count] = 0x8000 ; | |||||
1526 | continue ; | |||||
1527 | } ; | |||||
1528 | ||||||
1529 | dest [count] = lrintf (scaled_value) ; | |||||
1530 | } ; | |||||
1531 | ||||||
1532 | return ; | |||||
1533 | } /* psf_f2s_clip_array */ | |||||
1534 | ||||||
1535 | void | |||||
1536 | psf_d2s_array (const double *src, short *dest, int count, int normalize) | |||||
1537 | { double normfact ; | |||||
1538 | ||||||
1539 | normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ; | |||||
1540 | while (--count >= 0) | |||||
1541 | dest [count] = lrint (src [count] * normfact) ; | |||||
1542 | ||||||
1543 | return ; | |||||
1544 | } /* psf_f2s_array */ | |||||
1545 | ||||||
1546 | void | |||||
1547 | psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) | |||||
1548 | { double normfact, scaled_value ; | |||||
1549 | ||||||
1550 | normfact = normalize ? (1.0 * 0x8000) : 1.0 ; | |||||
1551 | ||||||
1552 | while (--count >= 0) | |||||
1553 | { scaled_value = src [count] * normfact ; | |||||
1554 | if (CPU_CLIPS_POSITIVE0 == 0 && scaled_value >= (1.0 * 0x7FFF)) | |||||
1555 | { dest [count] = 0x7FFF ; | |||||
1556 | continue ; | |||||
1557 | } ; | |||||
1558 | if (CPU_CLIPS_NEGATIVE0 == 0 && scaled_value <= (-8.0 * 0x1000)) | |||||
1559 | { dest [count] = 0x8000 ; | |||||
1560 | continue ; | |||||
1561 | } ; | |||||
1562 | ||||||
1563 | dest [count] = lrint (scaled_value) ; | |||||
1564 | } ; | |||||
1565 | ||||||
1566 | return ; | |||||
1567 | } /* psf_d2s_clip_array */ | |||||
1568 | ||||||
1569 | ||||||
1570 | void | |||||
1571 | psf_f2i_array (const float *src, int *dest, int count, int normalize) | |||||
1572 | { float normfact ; | |||||
1573 | ||||||
1574 | normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; | |||||
1575 | while (--count >= 0) | |||||
1576 | dest [count] = lrintf (src [count] * normfact) ; | |||||
1577 | ||||||
1578 | return ; | |||||
1579 | } /* psf_f2i_array */ | |||||
1580 | ||||||
1581 | void | |||||
1582 | psf_f2i_clip_array (const float *src, int *dest, int count, int normalize) | |||||
1583 | { float normfact, scaled_value ; | |||||
1584 | ||||||
1585 | normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; | |||||
1586 | ||||||
1587 | while (--count >= 0) | |||||
1588 | { scaled_value = src [count] * normfact ; | |||||
1589 | if (CPU_CLIPS_POSITIVE0 == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) | |||||
1590 | { dest [count] = 0x7FFFFFFF ; | |||||
1591 | continue ; | |||||
1592 | } ; | |||||
1593 | if (CPU_CLIPS_NEGATIVE0 == 0 && scaled_value <= (-8.0 * 0x10000000)) | |||||
1594 | { dest [count] = 0x80000000 ; | |||||
1595 | continue ; | |||||
1596 | } ; | |||||
1597 | ||||||
1598 | dest [count] = lrintf (scaled_value) ; | |||||
1599 | } ; | |||||
1600 | ||||||
1601 | return ; | |||||
1602 | } /* psf_f2i_clip_array */ | |||||
1603 | ||||||
1604 | void | |||||
1605 | psf_d2i_array (const double *src, int *dest, int count, int normalize) | |||||
1606 | { double normfact ; | |||||
1607 | ||||||
1608 | normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ; | |||||
1609 | while (--count >= 0) | |||||
1610 | dest [count] = lrint (src [count] * normfact) ; | |||||
1611 | ||||||
1612 | return ; | |||||
1613 | } /* psf_f2i_array */ | |||||
1614 | ||||||
1615 | void | |||||
1616 | psf_d2i_clip_array (const double *src, int *dest, int count, int normalize) | |||||
1617 | { double normfact, scaled_value ; | |||||
1618 | ||||||
1619 | normfact = normalize ? (8.0 * 0x10000000) : 1.0 ; | |||||
1620 | ||||||
1621 | while (--count >= 0) | |||||
1622 | { scaled_value = src [count] * normfact ; | |||||
1623 | if (CPU_CLIPS_POSITIVE0 == 0 && scaled_value >= (1.0 * 0x7FFFFFFF)) | |||||
1624 | { dest [count] = 0x7FFFFFFF ; | |||||
1625 | continue ; | |||||
1626 | } ; | |||||
1627 | if (CPU_CLIPS_NEGATIVE0 == 0 && scaled_value <= (-8.0 * 0x10000000)) | |||||
1628 | { dest [count] = 0x80000000 ; | |||||
1629 | continue ; | |||||
1630 | } ; | |||||
1631 | ||||||
1632 | dest [count] = lrint (scaled_value) ; | |||||
1633 | } ; | |||||
1634 | ||||||
1635 | return ; | |||||
1636 | } /* psf_d2i_clip_array */ | |||||
1637 | ||||||
1638 | FILE * | |||||
1639 | psf_open_tmpfile (char * fname, size_t fnamelen) | |||||
1640 | { const char * tmpdir ; | |||||
1641 | FILE * file ; | |||||
1642 | ||||||
1643 | if (OS_IS_WIN320) | |||||
1644 | tmpdir = getenv ("TEMP") ; | |||||
1645 | else | |||||
1646 | { tmpdir = getenv ("TMPDIR") ; | |||||
1647 | tmpdir = tmpdir == NULL((void*)0) ? "/tmp" : tmpdir ; | |||||
1648 | } ; | |||||
1649 | ||||||
1650 | // if (tmpdir && access (tmpdir, R_OK | W_OK | X_OK) == 0) | |||||
1651 | { snprintf (fname, fnamelen, "%s/%x%x-alac.tmp", tmpdir, psf_rand_int32 (), psf_rand_int32 ())__builtin___snprintf_chk (fname, fnamelen, 2 - 1, __builtin_object_size (fname, 2 > 1), "%s/%x%x-alac.tmp", tmpdir, psf_rand_int32 (), psf_rand_int32 ()) ; | |||||
1652 | if ((file = fopen (fname, "wb+")) != NULL((void*)0)) | |||||
1653 | return file ; | |||||
1654 | } ; | |||||
1655 | ||||||
1656 | snprintf (fname, fnamelen, "%x%x-alac.tmp", psf_rand_int32 (), psf_rand_int32 ())__builtin___snprintf_chk (fname, fnamelen, 2 - 1, __builtin_object_size (fname, 2 > 1), "%x%x-alac.tmp", psf_rand_int32 (), psf_rand_int32 ()) ; | |||||
1657 | if ((file = fopen (fname, "wb+")) != NULL((void*)0)) | |||||
1658 | return file ; | |||||
1659 | ||||||
1660 | memset (fname, 0, fnamelen) ; | |||||
1661 | return NULL((void*)0) ; | |||||
1662 | } /* psf_open_tmpfile */ |