• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 1999-2020 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004-2005 David Viens <davidv@plogue.com>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation; either version 2.1 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program 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
13 ** GNU Lesser General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 
20 #include	"sfconfig.h"
21 
22 #include	<stdio.h>
23 #include	<string.h>
24 #include	<ctype.h>
25 #include	<time.h>
26 
27 #include	"sndfile.h"
28 #include	"sfendian.h"
29 #include	"common.h"
30 #include	"wavlike.h"
31 
32 
33 #define WAV_BEXT_MIN_CHUNK_SIZE		602
34 #define WAV_BEXT_MAX_CHUNK_SIZE		(10 * 1024)
35 
36 #define WAV_CART_MIN_CHUNK_SIZE		2048
37 #define WAV_CART_MAX_CHUNK_SIZE		0xffffffff
38 
39 
40 static int 	exif_subchunk_parse	(SF_PRIVATE *psf, uint32_t length) ;
41 
42 
43 /*  Known WAVEFORMATEXTENSIBLE GUIDS.  */
44 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
45 {	0x00000001, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
46 } ;
47 
48 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
49 {	0x00000002, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
50 } ;
51 
52 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
53 {	0x00000003, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
54 } ;
55 
56 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
57 {	0x00000006, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
58 } ;
59 
60 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
61 {	0x00000007, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
62 } ;
63 
64 /*
65 ** the next two are from
66 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
67 */
68 
69 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
70 {	0x00000001, 0x0721, 0x11d3, {	0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00 }
71 } ;
72 
73 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
74 {	0x00000003, 0x0721, 0x11d3, {	0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00 }
75 } ;
76 
77 
78 #if 0
79 /* maybe interesting one day to read the following through sf_read_raw */
80 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
81 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
82 {	0x8312b9c2, 0x2e6e, 0x11d4, {	0xa8, 0x24, 0xde, 0x5b, 0x96, 0xc3, 0xab, 0x21 }
83 } ;
84 #endif
85 
86 /* This stores which bit in dwChannelMask maps to which channel */
87 static const struct chanmap_s
88 {	int id ;
89 	const char * name ;
90 } channel_mask_bits [] =
91 {	/* WAVEFORMATEXTENSIBLE doesn't distuingish FRONT_LEFT from LEFT */
92 	{	SF_CHANNEL_MAP_LEFT, "L" },
93 	{	SF_CHANNEL_MAP_RIGHT, "R" },
94 	{	SF_CHANNEL_MAP_CENTER, "C" },
95 	{	SF_CHANNEL_MAP_LFE, "LFE" },
96 	{	SF_CHANNEL_MAP_REAR_LEFT, "Ls" },
97 	{	SF_CHANNEL_MAP_REAR_RIGHT, "Rs" },
98 	{	SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER, "Lc" },
99 	{	SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER, "Rc" },
100 	{	SF_CHANNEL_MAP_REAR_CENTER, "Cs" },
101 	{	SF_CHANNEL_MAP_SIDE_LEFT, "Sl" },
102 	{	SF_CHANNEL_MAP_SIDE_RIGHT, "Sr" },
103 	{	SF_CHANNEL_MAP_TOP_CENTER, "Tc" },
104 	{	SF_CHANNEL_MAP_TOP_FRONT_LEFT, "Tfl" },
105 	{	SF_CHANNEL_MAP_TOP_FRONT_CENTER, "Tfc" },
106 	{	SF_CHANNEL_MAP_TOP_FRONT_RIGHT, "Tfr" },
107 	{	SF_CHANNEL_MAP_TOP_REAR_LEFT, "Trl" },
108 	{	SF_CHANNEL_MAP_TOP_REAR_CENTER, "Trc" },
109 	{	SF_CHANNEL_MAP_TOP_REAR_RIGHT, "Trr" },
110 } ;
111 
112 /*------------------------------------------------------------------------------
113  * Private static functions.
114  */
115 
116 static int
wavex_guid_equal(const EXT_SUBFORMAT * first,const EXT_SUBFORMAT * second)117 wavex_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second)
118 {	return !memcmp (first, second, sizeof (EXT_SUBFORMAT)) ;
119 } /* wavex_guid_equal */
120 
121 
122 
123 int
wavlike_read_fmt_chunk(SF_PRIVATE * psf,int fmtsize)124 wavlike_read_fmt_chunk (SF_PRIVATE *psf, int fmtsize)
125 {	WAVLIKE_PRIVATE * wpriv ;
126 	WAV_FMT *wav_fmt ;
127 	int	bytesread, k, bytespersec = 0 ;
128 
129 	if ((wpriv = psf->container_data) == NULL)
130 		return SFE_INTERNAL ;
131 	wav_fmt = &wpriv->wav_fmt ;
132 
133 	memset (wav_fmt, 0, sizeof (WAV_FMT)) ;
134 
135 	if (fmtsize < 16)
136 		return SFE_WAV_FMT_SHORT ;
137 
138 	/* assume psf->rwf_endian is already properly set */
139 
140 	/* Read the minimal WAV file header here. */
141 	bytesread = psf_binheader_readf (psf, "224422",
142 					&(wav_fmt->format), &(wav_fmt->min.channels),
143 					&(wav_fmt->min.samplerate), &(wav_fmt->min.bytespersec),
144 					&(wav_fmt->min.blockalign), &(wav_fmt->min.bitwidth)) ;
145 
146 	psf_log_printf (psf, "  Format        : 0x%X => %s\n", wav_fmt->format, wavlike_format_str (wav_fmt->format)) ;
147 	psf_log_printf (psf, "  Channels      : %d\n", wav_fmt->min.channels) ;
148 	psf_log_printf (psf, "  Sample Rate   : %d\n", wav_fmt->min.samplerate) ;
149 
150 	if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.blockalign == 0
151 		&& wav_fmt->min.bitwidth > 0 && wav_fmt->min.channels > 0)
152 	{	wav_fmt->min.blockalign = wav_fmt->min.bitwidth / 8 + (wav_fmt->min.bitwidth % 8 > 0 ? 1 : 0) ;
153 		wav_fmt->min.blockalign *= wav_fmt->min.channels ;
154 		psf_log_printf (psf, "  Block Align   : 0 (should be %d)\n", wav_fmt->min.blockalign) ;
155 		}
156 	else
157 		psf_log_printf (psf, "  Block Align   : %d\n", wav_fmt->min.blockalign) ;
158 
159 	if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.bitwidth == 24 &&
160 			wav_fmt->min.blockalign == 4 * wav_fmt->min.channels)
161 	{	psf_log_printf (psf, "  Bit Width     : 24\n") ;
162 
163 		psf_log_printf (psf, "\n"
164 			"  Ambiguous information in 'fmt ' chunk. Possibile file types:\n"
165 			"    0) Invalid IEEE float file generated by Syntrillium's Cooledit!\n"
166 			"    1) File generated by ALSA's arecord containing 24 bit samples in 32 bit containers.\n"
167 			"    2) 24 bit file with incorrect Block Align value.\n"
168 			"\n") ;
169 
170 		wpriv->fmt_is_broken = 1 ;
171 		}
172 	else if (wav_fmt->min.bitwidth == 0)
173 	{	switch (wav_fmt->format)
174 		{	case WAVE_FORMAT_GSM610 :
175 			case WAVE_FORMAT_IPP_ITU_G_723_1 :
176 					psf_log_printf (psf, "  Bit Width     : %d\n", wav_fmt->min.bitwidth) ;
177 					break ;
178 			default :
179 					psf_log_printf (psf, "  Bit Width     : %d (should not be 0)\n", wav_fmt->min.bitwidth) ;
180 			}
181 		}
182 	else
183 	{	switch (wav_fmt->format)
184 		{	case WAVE_FORMAT_GSM610 :
185 			case WAVE_FORMAT_IPP_ITU_G_723_1 :
186 		psf_log_printf (psf, "  Bit Width     : %d (should be 0)\n", wav_fmt->min.bitwidth) ;
187 					break ;
188 			default :
189 					psf_log_printf (psf, "  Bit Width     : %d\n", wav_fmt->min.bitwidth) ;
190 			}
191 		} ;
192 
193 	psf->sf.samplerate	= wav_fmt->min.samplerate ;
194 	psf->sf.frames 		= 0 ;					/* Correct this when reading data chunk. */
195 	psf->sf.channels	= wav_fmt->min.channels ;
196 
197 	switch (wav_fmt->format)
198 	{	case WAVE_FORMAT_PCM :
199 		case WAVE_FORMAT_IEEE_FLOAT :
200 				bytespersec = wav_fmt->min.samplerate * wav_fmt->min.blockalign ;
201 				if (wav_fmt->min.bytespersec != (unsigned) bytespersec)
202 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
203 				else
204 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
205 
206 				psf->bytewidth = BITWIDTH2BYTES (wav_fmt->min.bitwidth) ;
207 				break ;
208 
209 		case WAVE_FORMAT_ALAW :
210 		case WAVE_FORMAT_MULAW :
211 				if (wav_fmt->min.bytespersec != wav_fmt->min.samplerate * wav_fmt->min.blockalign)
212 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, wav_fmt->min.samplerate * wav_fmt->min.blockalign) ;
213 				else
214 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
215 
216 				psf->bytewidth = 1 ;
217 				if (fmtsize >= 18)
218 				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->size20.extrabytes)) ;
219 					psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->size20.extrabytes) ;
220 					} ;
221 				break ;
222 
223 		case WAVE_FORMAT_IMA_ADPCM :
224 				if (wav_fmt->min.bitwidth != 4)
225 					return SFE_WAV_ADPCM_NOT4BIT ;
226 				if (wav_fmt->min.channels < 1 || wav_fmt->min.channels > 2)
227 					return SFE_WAV_ADPCM_CHANNELS ;
228 
229 				bytesread += psf_binheader_readf (psf, "22", &(wav_fmt->ima.extrabytes), &(wav_fmt->ima.samplesperblock)) ;
230 				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->ima.extrabytes) ;
231 				if (wav_fmt->ima.samplesperblock < 1)
232 				{	psf_log_printf (psf, "  Samples/Block : %d (should be > 0)\n", wav_fmt->ima.samplesperblock) ;
233 					return SFE_WAV_ADPCM_SAMPLES ;
234 					}
235 				else
236 					psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
237 
238 				bytespersec = (wav_fmt->ima.samplerate * wav_fmt->ima.blockalign) / wav_fmt->ima.samplesperblock ;
239 				if (wav_fmt->ima.bytespersec != (unsigned) bytespersec)
240 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->ima.bytespersec, bytespersec) ;
241 				else
242 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->ima.bytespersec) ;
243 
244 				break ;
245 
246 		case WAVE_FORMAT_MS_ADPCM :
247 				if (wav_fmt->msadpcm.bitwidth != 4)
248 					return SFE_WAV_ADPCM_NOT4BIT ;
249 				if (wav_fmt->msadpcm.channels < 1 || wav_fmt->msadpcm.channels > 2)
250 					return SFE_WAV_ADPCM_CHANNELS ;
251 
252 				bytesread += psf_binheader_readf (psf, "222", &(wav_fmt->msadpcm.extrabytes),
253 								&(wav_fmt->msadpcm.samplesperblock), &(wav_fmt->msadpcm.numcoeffs)) ;
254 
255 				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->msadpcm.extrabytes) ;
256 				if (wav_fmt->ima.samplesperblock < 1)
257 				{	psf_log_printf (psf, "  Samples/Block : %d (should be > 0)\n", wav_fmt->ima.samplesperblock) ;
258 					return SFE_WAV_ADPCM_SAMPLES ;
259 					}
260 				else
261 					psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
262 
263 				bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / wav_fmt->msadpcm.samplesperblock ;
264 				if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
265 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
266 				else if (wav_fmt->min.bytespersec == (wav_fmt->min.samplerate / wav_fmt->msadpcm.samplesperblock) * wav_fmt->min.blockalign)
267 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d (MS BUG!))\n", wav_fmt->min.bytespersec, bytespersec) ;
268 				else
269 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
270 
271 				if (wav_fmt->msadpcm.numcoeffs > ARRAY_LEN (wav_fmt->msadpcm.coeffs))
272 				{	psf_log_printf (psf, "  No. of Coeffs : %d (should be <= %d)\n", wav_fmt->msadpcm.numcoeffs, ARRAY_LEN (wav_fmt->msadpcm.coeffs)) ;
273 					wav_fmt->msadpcm.numcoeffs = ARRAY_LEN (wav_fmt->msadpcm.coeffs) ;
274 					}
275 				else
276 					psf_log_printf (psf, "  No. of Coeffs : %d\n", wav_fmt->msadpcm.numcoeffs) ;
277 
278 				psf_log_printf (psf, "    Index   Coeffs1   Coeffs2\n") ;
279 				for (k = 0 ; k < wav_fmt->msadpcm.numcoeffs ; k++)
280 				{	char buffer [128] ;
281 
282 					bytesread +=
283 						psf_binheader_readf (psf, "22", &(wav_fmt->msadpcm.coeffs [k].coeff1), &(wav_fmt->msadpcm.coeffs [k].coeff2)) ;
284 					snprintf (buffer, sizeof (buffer), "     %2d     %7d   %7d\n", k, wav_fmt->msadpcm.coeffs [k].coeff1, wav_fmt->msadpcm.coeffs [k].coeff2) ;
285 					psf_log_printf (psf, buffer) ;
286 					} ;
287 				break ;
288 
289 		case WAVE_FORMAT_GSM610 :
290 				if (wav_fmt->gsm610.channels != 1 || wav_fmt->gsm610.blockalign != 65)
291 					return SFE_WAV_GSM610_FORMAT ;
292 
293 				bytesread +=
294 				psf_binheader_readf (psf, "22", &(wav_fmt->gsm610.extrabytes), &(wav_fmt->gsm610.samplesperblock)) ;
295 
296 				if (wav_fmt->gsm610.samplesperblock != 320)
297 					return SFE_WAV_GSM610_FORMAT ;
298 
299 				bytespersec = (wav_fmt->gsm610.samplerate * wav_fmt->gsm610.blockalign) / wav_fmt->gsm610.samplesperblock ;
300 				if (wav_fmt->gsm610.bytespersec != (unsigned) bytespersec)
301 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->gsm610.bytespersec, bytespersec) ;
302 				else
303 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->gsm610.bytespersec) ;
304 
305 				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->gsm610.extrabytes) ;
306 				psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->gsm610.samplesperblock) ;
307 				break ;
308 
309 		case WAVE_FORMAT_EXTENSIBLE :
310 				if (wav_fmt->ext.bytespersec != wav_fmt->ext.samplerate * wav_fmt->ext.blockalign)
311 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->ext.bytespersec, wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) ;
312 				else
313 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->ext.bytespersec) ;
314 
315 				bytesread +=
316 				psf_binheader_readf (psf, "224", &(wav_fmt->ext.extrabytes), &(wav_fmt->ext.validbits),
317 						&(wav_fmt->ext.channelmask)) ;
318 
319 				psf_log_printf (psf, "  Valid Bits    : %d\n", wav_fmt->ext.validbits) ;
320 
321 				if (wav_fmt->ext.channelmask == 0)
322 					psf_log_printf (psf, "  Channel Mask  : 0x0 (should not be zero)\n") ;
323 				else
324 				{	char buffer [512] ;
325 					unsigned bit ;
326 
327 					wpriv->wavex_channelmask = wav_fmt->ext.channelmask ;
328 
329 					/* It's probably wise to ignore the channel mask if it is all zero */
330 					free (psf->channel_map) ;
331 
332 					if ((psf->channel_map = calloc (psf->sf.channels, sizeof (psf->channel_map [0]))) == NULL)
333 						return SFE_MALLOC_FAILED ;
334 
335 					/* Terminate the buffer we're going to append_snprintf into. */
336 					buffer [0] = 0 ;
337 
338 					for (bit = k = 0 ; bit < ARRAY_LEN (channel_mask_bits) && k < psf->sf.channels ; bit++)
339 					{
340 						if (wav_fmt->ext.channelmask & (1 << bit))
341 						{	if (k > psf->sf.channels)
342 							{	psf_log_printf (psf, "*** More channel map bits than there are channels.\n") ;
343 								break ;
344 								} ;
345 
346 							psf->channel_map [k++] = channel_mask_bits [bit].id ;
347 							append_snprintf (buffer, sizeof (buffer), "%s, ", channel_mask_bits [bit].name) ;
348 							} ;
349 						} ;
350 
351 					/* Remove trailing ", ". */
352 					bit = strlen (buffer) ;
353 					if (bit >= 2)
354 					{	buffer [--bit] = 0 ;
355 						buffer [--bit] = 0 ;
356 						} ;
357 
358 					if (k != psf->sf.channels)
359 					{	psf_log_printf (psf, "  Channel Mask  : 0x%X\n", wav_fmt->ext.channelmask) ;
360 						psf_log_printf (psf, "*** Less channel map bits than there are channels.\n") ;
361 						}
362 					else
363 						psf_log_printf (psf, "  Channel Mask  : 0x%X (%s)\n", wav_fmt->ext.channelmask, buffer) ;
364 					} ;
365 
366 				bytesread += psf_binheader_readf (psf, "422", &(wav_fmt->ext.esf.esf_field1), &(wav_fmt->ext.esf.esf_field2), &(wav_fmt->ext.esf.esf_field3)) ;
367 
368 				/* compare the esf_fields with each known GUID? and print? */
369 				psf_log_printf (psf, "  Subformat\n") ;
370 				psf_log_printf (psf, "    esf_field1 : 0x%X\n", wav_fmt->ext.esf.esf_field1) ;
371 				psf_log_printf (psf, "    esf_field2 : 0x%X\n", wav_fmt->ext.esf.esf_field2) ;
372 				psf_log_printf (psf, "    esf_field3 : 0x%X\n", wav_fmt->ext.esf.esf_field3) ;
373 				psf_log_printf (psf, "    esf_field4 : ") ;
374 				for (k = 0 ; k < 8 ; k++)
375 				{	bytesread += psf_binheader_readf (psf, "1", &(wav_fmt->ext.esf.esf_field4 [k])) ;
376 					psf_log_printf (psf, "0x%X ", wav_fmt->ext.esf.esf_field4 [k] & 0xFF) ;
377 					} ;
378 				psf_log_printf (psf, "\n") ;
379 				psf->bytewidth = BITWIDTH2BYTES (wav_fmt->ext.bitwidth) ;
380 
381 				/* Compare GUIDs for known ones. */
382 				if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_PCM))
383 				{	psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
384 					psf_log_printf (psf, "    format : pcm\n") ;
385 					}
386 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_MS_ADPCM))
387 				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM) ;
388 					psf_log_printf (psf, "    format : ms adpcm\n") ;
389 					}
390 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_IEEE_FLOAT))
391 				{	psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
392 					psf_log_printf (psf, "    format : IEEE float\n") ;
393 					}
394 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_ALAW))
395 				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ALAW) ;
396 					psf_log_printf (psf, "    format : A-law\n") ;
397 					}
398 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_MULAW))
399 				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ULAW) ;
400 					psf_log_printf (psf, "    format : u-law\n") ;
401 					}
402 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM))
403 				{	psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
404 					psf_log_printf (psf, "    format : pcm (Ambisonic B)\n") ;
405 					wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
406 					}
407 				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT))
408 				{	psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
409 					psf_log_printf (psf, "    format : IEEE float (Ambisonic B)\n") ;
410 					wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
411 					}
412 				else
413 					return SFE_UNIMPLEMENTED ;
414 
415 				break ;
416 
417 		case WAVE_FORMAT_G721_ADPCM :
418 				psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->g72x.bytespersec) ;
419 				if (fmtsize >= 20)
420 				{	bytesread += psf_binheader_readf (psf, "22", &(wav_fmt->g72x.extrabytes), &(wav_fmt->g72x.auxblocksize)) ;
421 					if (wav_fmt->g72x.extrabytes == 0)
422 						psf_log_printf (psf, "  Extra Bytes   : %d (should be 2)\n", wav_fmt->g72x.extrabytes) ;
423 					else
424 						psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->g72x.extrabytes) ;
425 					psf_log_printf (psf, "  Aux Blk Size  : %d\n", wav_fmt->g72x.auxblocksize) ;
426 					}
427 				else if (fmtsize == 18)
428 				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->g72x.extrabytes)) ;
429 					psf_log_printf (psf, "  Extra Bytes   : %d%s\n", wav_fmt->g72x.extrabytes, wav_fmt->g72x.extrabytes != 0 ? " (should be 0)" : "") ;
430 					}
431 				else
432 					psf_log_printf (psf, "*** 'fmt ' chunk should be bigger than this!\n") ;
433 				break ;
434 
435 		case WAVE_FORMAT_NMS_VBXADPCM :
436 				if (wav_fmt->min.channels != 1 || wav_fmt->min.bitwidth < 2 || wav_fmt->min.bitwidth * 20 + 2 != wav_fmt->min.blockalign)
437 					return SFE_WAV_NMS_FORMAT ;
438 
439 				bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / 160 ;
440 				if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
441 					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
442 				else
443 					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
444 				if (fmtsize >= 18)
445 				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->size20.extrabytes)) ;
446 					psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->size20.extrabytes) ;
447 					} ;
448 				break ;
449 
450 		default :
451 				psf_log_printf (psf, "*** No 'fmt ' chunk dumper for this format!\n") ;
452 				return SFE_WAV_BAD_FMT ;
453 		} ;
454 
455 	if (bytesread > fmtsize)
456 	{	psf_log_printf (psf, "*** wavlike_read_fmt_chunk (bytesread > fmtsize)\n") ;
457 		return SFE_WAV_BAD_FMT ;
458 		}
459 	else
460 		psf_binheader_readf (psf, "j", fmtsize - bytesread) ;
461 
462 	psf->blockwidth = wav_fmt->min.channels * psf->bytewidth ;
463 
464 	return 0 ;
465 } /* wavlike_read_fmt_chunk */
466 
467 void
wavlike_write_guid(SF_PRIVATE * psf,const EXT_SUBFORMAT * subformat)468 wavlike_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat)
469 {
470 	psf_binheader_writef (psf, "422b", BHW4 (subformat->esf_field1),
471 					BHW2 (subformat->esf_field2), BHW2 (subformat->esf_field3),
472 					BHWv (subformat->esf_field4), BHWz (8)) ;
473 } /* wavlike_write_guid */
474 
475 
476 int
wavlike_gen_channel_mask(const int * chan_map,int channels)477 wavlike_gen_channel_mask (const int *chan_map, int channels)
478 {	int chan, mask = 0, bit = -1, last_bit = -1 ;
479 
480 	if (chan_map == NULL)
481 		return 0 ;
482 
483 	for (chan = 0 ; chan < channels ; chan ++)
484 	{	int k ;
485 
486 		for (k = bit + 1 ; k < ARRAY_LEN (channel_mask_bits) ; k++)
487 			if (chan_map [chan] == channel_mask_bits [k].id)
488 			{	bit = k ;
489 				break ;
490 				} ;
491 
492 		/* Check for bad sequence. */
493 		if (bit <= last_bit)
494 			return 0 ;
495 
496 		mask += 1 << bit ;
497 		last_bit = bit ;
498 		} ;
499 
500 	return mask ;
501 } /* wavlike_gen_channel_mask */
502 
503 void
wavlike_analyze(SF_PRIVATE * psf)504 wavlike_analyze (SF_PRIVATE *psf)
505 {	unsigned char buffer [4096] ;
506 	AUDIO_DETECT ad ;
507 	int format = 0 ;
508 
509 	if (psf->is_pipe)
510 	{	psf_log_printf (psf, "*** Error : Reading from a pipe. Can't analyze data section to figure out real data format.\n\n") ;
511 		return ;
512 		} ;
513 
514 	psf_log_printf (psf, "---------------------------------------------------\n"
515 						"Format is known to be broken. Using detection code.\n") ;
516 
517 	/* Code goes here. */
518 	ad.endianness = SF_ENDIAN_LITTLE ;
519 	ad.channels = psf->sf.channels ;
520 
521 	psf_fseek (psf, 3 * 4 * 50, SEEK_SET) ;
522 
523 	while (psf_fread (buffer, 1, sizeof (buffer), psf) == sizeof (buffer))
524 	{	format = audio_detect (psf, &ad, buffer, sizeof (buffer)) ;
525 		if (format != 0)
526 			break ;
527 		} ;
528 
529 	/* Seek to start of DATA section. */
530 	psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
531 
532 	if (format == 0)
533 	{	psf_log_printf (psf, "wavlike_analyze : detection failed.\n") ;
534 		return ;
535 		} ;
536 
537 	switch (format)
538 	{	case SF_FORMAT_PCM_32 :
539 		case SF_FORMAT_FLOAT :
540 			psf_log_printf (psf, "wavlike_analyze : found format : 0x%X\n", format) ;
541 			psf->sf.format = (psf->sf.format & ~SF_FORMAT_SUBMASK) + format ;
542 			psf->bytewidth = 4 ;
543 			psf->blockwidth = psf->sf.channels * psf->bytewidth ;
544 			break ;
545 
546 		case SF_FORMAT_PCM_24 :
547 			psf_log_printf (psf, "wavlike_analyze : found format : 0x%X\n", format) ;
548 			psf->sf.format = (psf->sf.format & ~SF_FORMAT_SUBMASK) + format ;
549 			psf->bytewidth = 3 ;
550 			psf->blockwidth = psf->sf.channels * psf->bytewidth ;
551 			break ;
552 
553 		default :
554 			psf_log_printf (psf, "wavlike_analyze : unhandled format : 0x%X\n", format) ;
555 			break ;
556 		} ;
557 
558 	return ;
559 } /* wavlike_analyze */
560 
561 /*==============================================================================
562 */
563 
564 typedef struct
565 {	int			ID ;
566 	const char	*name ;
567 } WAV_FORMAT_DESC ;
568 
569 #define STR(x)			#x
570 #define FORMAT_TYPE(x)	{ x, STR (x) }
571 
572 static WAV_FORMAT_DESC wave_descs [] =
573 {	FORMAT_TYPE	(WAVE_FORMAT_PCM),
574 	FORMAT_TYPE (WAVE_FORMAT_MS_ADPCM),
575 	FORMAT_TYPE (WAVE_FORMAT_IEEE_FLOAT),
576 	FORMAT_TYPE (WAVE_FORMAT_VSELP),
577 	FORMAT_TYPE (WAVE_FORMAT_IBM_CVSD),
578 	FORMAT_TYPE (WAVE_FORMAT_ALAW),
579 	FORMAT_TYPE (WAVE_FORMAT_MULAW),
580 	FORMAT_TYPE (WAVE_FORMAT_OKI_ADPCM),
581 	FORMAT_TYPE (WAVE_FORMAT_IMA_ADPCM),
582 	FORMAT_TYPE (WAVE_FORMAT_MEDIASPACE_ADPCM),
583 	FORMAT_TYPE (WAVE_FORMAT_SIERRA_ADPCM),
584 	FORMAT_TYPE (WAVE_FORMAT_G723_ADPCM),
585 	FORMAT_TYPE (WAVE_FORMAT_DIGISTD),
586 	FORMAT_TYPE (WAVE_FORMAT_DIGIFIX),
587 	FORMAT_TYPE (WAVE_FORMAT_DIALOGIC_OKI_ADPCM),
588 	FORMAT_TYPE (WAVE_FORMAT_MEDIAVISION_ADPCM),
589 	FORMAT_TYPE (WAVE_FORMAT_CU_CODEC),
590 	FORMAT_TYPE (WAVE_FORMAT_YAMAHA_ADPCM),
591 	FORMAT_TYPE (WAVE_FORMAT_SONARC),
592 	FORMAT_TYPE (WAVE_FORMAT_DSPGROUP_TRUESPEECH),
593 	FORMAT_TYPE (WAVE_FORMAT_ECHOSC1),
594 	FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF36),
595 	FORMAT_TYPE (WAVE_FORMAT_APTX),
596 	FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF10),
597 	FORMAT_TYPE (WAVE_FORMAT_PROSODY_1612),
598 	FORMAT_TYPE (WAVE_FORMAT_LRC),
599 	FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC2),
600 	FORMAT_TYPE (WAVE_FORMAT_GSM610),
601 	FORMAT_TYPE (WAVE_FORMAT_MSNAUDIO),
602 	FORMAT_TYPE (WAVE_FORMAT_ANTEX_ADPCME),
603 	FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_VQLPC),
604 	FORMAT_TYPE (WAVE_FORMAT_DIGIREAL),
605 	FORMAT_TYPE (WAVE_FORMAT_DIGIADPCM),
606 	FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_CR10),
607 	FORMAT_TYPE (WAVE_FORMAT_NMS_VBXADPCM),
608 	FORMAT_TYPE (WAVE_FORMAT_ROLAND_RDAC),
609 	FORMAT_TYPE (WAVE_FORMAT_ECHOSC3),
610 	FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_ADPCM),
611 	FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_DIGITALK),
612 	FORMAT_TYPE (WAVE_FORMAT_XEBEC),
613 	FORMAT_TYPE (WAVE_FORMAT_G721_ADPCM),
614 	FORMAT_TYPE (WAVE_FORMAT_G728_CELP),
615 	FORMAT_TYPE (WAVE_FORMAT_MSG723),
616 	FORMAT_TYPE (WAVE_FORMAT_MPEG),
617 	FORMAT_TYPE (WAVE_FORMAT_RT24),
618 	FORMAT_TYPE (WAVE_FORMAT_PAC),
619 	FORMAT_TYPE (WAVE_FORMAT_MPEGLAYER3),
620 	FORMAT_TYPE (WAVE_FORMAT_LUCENT_G723),
621 	FORMAT_TYPE (WAVE_FORMAT_CIRRUS),
622 	FORMAT_TYPE (WAVE_FORMAT_ESPCM),
623 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE),
624 	FORMAT_TYPE (WAVE_FORMAT_CANOPUS_ATRAC),
625 	FORMAT_TYPE (WAVE_FORMAT_G726_ADPCM),
626 	FORMAT_TYPE (WAVE_FORMAT_G722_ADPCM),
627 	FORMAT_TYPE (WAVE_FORMAT_DSAT),
628 	FORMAT_TYPE (WAVE_FORMAT_DSAT_DISPLAY),
629 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_BYTE_ALIGNED),
630 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC8),
631 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC10),
632 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC16),
633 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC20),
634 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT24),
635 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29),
636 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29HW),
637 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR12),
638 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR18),
639 	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_TQ40),
640 	FORMAT_TYPE (WAVE_FORMAT_SOFTSOUND),
641 	FORMAT_TYPE (WAVE_FORMAT_VOXARE_TQ60),
642 	FORMAT_TYPE (WAVE_FORMAT_MSRT24),
643 	FORMAT_TYPE (WAVE_FORMAT_G729A),
644 	FORMAT_TYPE (WAVE_FORMAT_MVI_MV12),
645 	FORMAT_TYPE (WAVE_FORMAT_DF_G726),
646 	FORMAT_TYPE (WAVE_FORMAT_DF_GSM610),
647 	FORMAT_TYPE (WAVE_FORMAT_ONLIVE),
648 	FORMAT_TYPE (WAVE_FORMAT_SBC24),
649 	FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC3_SPDIF),
650 	FORMAT_TYPE (WAVE_FORMAT_ZYXEL_ADPCM),
651 	FORMAT_TYPE (WAVE_FORMAT_PHILIPS_LPCBB),
652 	FORMAT_TYPE (WAVE_FORMAT_PACKED),
653 	FORMAT_TYPE (WAVE_FORMAT_RHETOREX_ADPCM),
654 	FORMAT_TYPE (IBM_FORMAT_MULAW),
655 	FORMAT_TYPE (IBM_FORMAT_ALAW),
656 	FORMAT_TYPE (IBM_FORMAT_ADPCM),
657 	FORMAT_TYPE (WAVE_FORMAT_VIVO_G723),
658 	FORMAT_TYPE (WAVE_FORMAT_VIVO_SIREN),
659 	FORMAT_TYPE (WAVE_FORMAT_DIGITAL_G723),
660 	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_ADPCM),
661 	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH8),
662 	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH10),
663 	FORMAT_TYPE (WAVE_FORMAT_QUARTERDECK),
664 	FORMAT_TYPE (WAVE_FORMAT_FM_TOWNS_SND),
665 	FORMAT_TYPE (WAVE_FORMAT_BZV_DIGITAL),
666 	FORMAT_TYPE (WAVE_FORMAT_VME_VMPCM),
667 	FORMAT_TYPE (WAVE_FORMAT_OLIGSM),
668 	FORMAT_TYPE (WAVE_FORMAT_OLIADPCM),
669 	FORMAT_TYPE (WAVE_FORMAT_OLICELP),
670 	FORMAT_TYPE (WAVE_FORMAT_OLISBC),
671 	FORMAT_TYPE (WAVE_FORMAT_OLIOPR),
672 	FORMAT_TYPE (WAVE_FORMAT_LH_CODEC),
673 	FORMAT_TYPE (WAVE_FORMAT_NORRIS),
674 	FORMAT_TYPE (WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS),
675 	FORMAT_TYPE (WAVE_FORMAT_DVM),
676 	FORMAT_TYPE (WAVE_FORMAT_INTERWAV_VSC112),
677 	FORMAT_TYPE (WAVE_FORMAT_IPP_ITU_G_723_1),
678 	FORMAT_TYPE (WAVE_FORMAT_EXTENSIBLE),
679 } ;
680 
681 char const*
wavlike_format_str(int k)682 wavlike_format_str (int k)
683 {	int lower, upper, mid ;
684 
685 	lower = -1 ;
686 	upper = sizeof (wave_descs) / sizeof (WAV_FORMAT_DESC) ;
687 
688 	/* binary search */
689 	if ((wave_descs [0].ID <= k) && (k <= wave_descs [upper - 1].ID))
690 	{
691 		while (lower + 1 < upper)
692 		{	mid = (upper + lower) / 2 ;
693 
694 			if (k == wave_descs [mid].ID)
695 				return wave_descs [mid].name ;
696 			if (k < wave_descs [mid].ID)
697 				upper = mid ;
698 			else
699 				lower = mid ;
700 			} ;
701 		} ;
702 
703 	return "Unknown format" ;
704 } /* wavlike_format_str */
705 
706 int
wavlike_srate2blocksize(int srate_chan_product)707 wavlike_srate2blocksize (int srate_chan_product)
708 {	if (srate_chan_product < 12000)
709 		return 256 ;
710 	if (srate_chan_product < 23000)
711 		return 512 ;
712 	if (srate_chan_product < 44000)
713 		return 1024 ;
714 	return 2048 ;
715 } /* srate2blocksize */
716 
717 int
wavlike_read_bext_chunk(SF_PRIVATE * psf,uint32_t chunksize)718 wavlike_read_bext_chunk (SF_PRIVATE *psf, uint32_t chunksize)
719 {
720 	SF_BROADCAST_INFO_16K * b ;
721 	uint32_t bytes = 0 ;
722 
723 	if (chunksize < WAV_BEXT_MIN_CHUNK_SIZE)
724 	{	psf_log_printf (psf, "bext : %u (should be >= %d)\n", chunksize, WAV_BEXT_MIN_CHUNK_SIZE) ;
725 		psf_binheader_readf (psf, "j", chunksize) ;
726 		return 0 ;
727 		} ;
728 
729 	if (chunksize > WAV_BEXT_MAX_CHUNK_SIZE)
730 	{	psf_log_printf (psf, "bext : %u (should be < %d)\n", chunksize, WAV_BEXT_MAX_CHUNK_SIZE) ;
731 		psf_binheader_readf (psf, "j", chunksize) ;
732 		return 0 ;
733 		} ;
734 
735 	if (chunksize >= sizeof (SF_BROADCAST_INFO_16K))
736 	{	psf_log_printf (psf, "bext : %u too big to be handled\n", chunksize) ;
737 		psf_binheader_readf (psf, "j", chunksize) ;
738 		return 0 ;
739 		} ;
740 
741 	psf_log_printf (psf, "bext : %u\n", chunksize) ;
742 
743 	if (!psf->broadcast_16k)
744 	{	psf->broadcast_16k = broadcast_var_alloc () ;
745 		if (!psf->broadcast_16k)
746 		{	psf->error = SFE_MALLOC_FAILED ;
747 			return psf->error ;
748 			}
749 		}
750 	else
751 	{	psf_log_printf (psf, "bext : found more than one bext chunk, using last one.\n") ;
752 		memset (psf->broadcast_16k, 0, sizeof (SF_BROADCAST_INFO_16K)) ;
753 		}
754 
755 	b = psf->broadcast_16k ;
756 
757 	bytes += psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
758 	bytes += psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
759 	bytes += psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
760 	bytes += psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
761 	bytes += psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
762 	bytes += psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
763 	bytes += psf_binheader_readf (psf, "b", &b->umid, sizeof (b->umid)) ;
764 	bytes += psf_binheader_readf (psf, "22", &b->loudness_value, &b->loudness_range) ;
765 	bytes += psf_binheader_readf (psf, "222", &b->max_true_peak_level, &b->max_momentary_loudness, &b->max_shortterm_loudness) ;
766 	bytes += psf_binheader_readf (psf, "j", 180) ;
767 
768 	if (chunksize > WAV_BEXT_MIN_CHUNK_SIZE)
769 	{	/* File has coding history data. */
770 
771 		b->coding_history_size = chunksize - WAV_BEXT_MIN_CHUNK_SIZE ;
772 
773 		/* We do not parse the coding history */
774 		bytes += psf_binheader_readf (psf, "b", BHWv (b->coding_history), BHWz (b->coding_history_size)) ;
775 		} ;
776 
777 	if (bytes < chunksize)
778 		psf_binheader_readf (psf, "j", BHWj (chunksize - bytes)) ;
779 
780 	return 0 ;
781 } /* wavlike_read_bext_chunk */
782 
783 int
wavlike_write_bext_chunk(SF_PRIVATE * psf)784 wavlike_write_bext_chunk (SF_PRIVATE *psf)
785 {	SF_BROADCAST_INFO_16K *b ;
786 
787 	if (psf->broadcast_16k == NULL)
788 		return -1 ;
789 
790 	b = psf->broadcast_16k ;
791 
792 	psf_binheader_writef (psf, "m4", BHWm (bext_MARKER), BHW4 (WAV_BEXT_MIN_CHUNK_SIZE + b->coding_history_size)) ;
793 
794 	/*
795 	**	Note that it is very important that the field widths of the SF_BROADCAST_INFO
796 	**	struct match those of the bext chunk fields.
797 	*/
798 
799 	psf_binheader_writef (psf, "b", BHWv (b->description), BHWz (sizeof (b->description))) ;
800 	psf_binheader_writef (psf, "b", BHWv (b->originator), BHWz (sizeof (b->originator))) ;
801 	psf_binheader_writef (psf, "b", BHWv (b->originator_reference), BHWz (sizeof (b->originator_reference))) ;
802 	psf_binheader_writef (psf, "b", BHWv (b->origination_date), BHWz (sizeof (b->origination_date))) ;
803 	psf_binheader_writef (psf, "b", BHWv (b->origination_time), BHWz (sizeof (b->origination_time))) ;
804 	psf_binheader_writef (psf, "442", BHW4 (b->time_reference_low), BHW4 (b->time_reference_high), BHW2 (b->version)) ;
805 	psf_binheader_writef (psf, "b", BHWv (b->umid), BHWz (sizeof (b->umid))) ;
806 	psf_binheader_writef (psf, "22", BHW2 (b->loudness_value), BHW2 (b->loudness_range)) ;
807 	psf_binheader_writef (psf, "222", BHW2 (b->max_true_peak_level), BHW2 (b->max_momentary_loudness), BHW2 (b->max_shortterm_loudness)) ;
808 	psf_binheader_writef (psf, "z", BHWz (180)) ;
809 
810 	if (b->coding_history_size > 0)
811 		psf_binheader_writef (psf, "b", BHWv (b->coding_history), BHWz (b->coding_history_size)) ;
812 
813 	return 0 ;
814 } /* wavlike_write_bext_chunk */
815 
816 int
wavlike_read_cart_chunk(SF_PRIVATE * psf,uint32_t chunksize)817 wavlike_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
818 {	SF_CART_INFO_16K *c ;
819 	uint32_t bytes = 0 ;
820 	int k ;
821 
822 	if (chunksize < WAV_CART_MIN_CHUNK_SIZE)
823 	{	psf_log_printf (psf, "cart : %u (should be >= %d)\n", chunksize, WAV_CART_MIN_CHUNK_SIZE) ;
824 		psf_binheader_readf (psf, "j", chunksize) ;
825 		return 0 ;
826 		} ;
827 	if (chunksize > WAV_CART_MAX_CHUNK_SIZE)
828 	{	psf_log_printf (psf, "cart : %u (should be < %d)\n", chunksize, WAV_CART_MAX_CHUNK_SIZE) ;
829 		psf_binheader_readf (psf, "j", chunksize) ;
830 		return 0 ;
831 		} ;
832 
833 	if (chunksize >= sizeof (SF_CART_INFO_16K))
834 	{	psf_log_printf (psf, "cart : %u too big to be handled\n", chunksize) ;
835 		psf_binheader_readf (psf, "j", chunksize) ;
836 		return 0 ;
837 		} ;
838 
839 	psf_log_printf (psf, "cart : %u\n", chunksize) ;
840 
841 	if (psf->cart_16k)
842 	{	psf_log_printf (psf, "  Found more than one cart chunk, using last one.\n") ;
843 		free (psf->cart_16k) ;
844 		psf->cart_16k = NULL ;
845 		} ;
846 
847 	if ((psf->cart_16k = cart_var_alloc ()) == NULL)
848 	{	psf->error = SFE_MALLOC_FAILED ;
849 		return psf->error ;
850 		} ;
851 
852 	c = psf->cart_16k ;
853 	bytes += psf_binheader_readf (psf, "b", c->version, sizeof (c->version)) ;
854 	bytes += psf_binheader_readf (psf, "b", c->title, sizeof (c->title)) ;
855 	bytes += psf_binheader_readf (psf, "b", c->artist, sizeof (c->artist)) ;
856 	bytes += psf_binheader_readf (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
857 	bytes += psf_binheader_readf (psf, "b", c->client_id, sizeof (c->client_id)) ;
858 	bytes += psf_binheader_readf (psf, "b", c->category, sizeof (c->category)) ;
859 	bytes += psf_binheader_readf (psf, "b", c->classification, sizeof (c->classification)) ;
860 	bytes += psf_binheader_readf (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
861 	bytes += psf_binheader_readf (psf, "b", c->start_date, sizeof (c->start_date)) ;
862 	bytes += psf_binheader_readf (psf, "b", c->start_time, sizeof (c->start_time)) ;
863 	bytes += psf_binheader_readf (psf, "b", c->end_date, sizeof (c->end_date)) ;
864 	bytes += psf_binheader_readf (psf, "b", c->end_time, sizeof (c->end_time)) ;
865 	bytes += psf_binheader_readf (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
866 	bytes += psf_binheader_readf (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
867 	bytes += psf_binheader_readf (psf, "b", c->user_def, sizeof (c->user_def)) ;
868 	bytes += psf_binheader_readf (psf, "e4", &c->level_reference, sizeof (c->level_reference)) ;
869 
870 	for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
871 		bytes += psf_binheader_readf (psf, "b4", &c->post_timers [k].usage, make_size_t (4), &c->post_timers [k].value) ;
872 
873 	bytes += psf_binheader_readf (psf, "b", c->reserved, sizeof (c->reserved)) ;
874 	bytes += psf_binheader_readf (psf, "b", c->url, sizeof (c->url)) ;
875 
876 	if (chunksize > WAV_CART_MIN_CHUNK_SIZE)
877 	{	/* File has tag text. */
878 		c->tag_text_size = chunksize - WAV_CART_MIN_CHUNK_SIZE ;
879 		bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
880 		} ;
881 
882 	return 0 ;
883 } /* wavlike_read_cart_chunk */
884 
885 int
wavlike_write_cart_chunk(SF_PRIVATE * psf)886 wavlike_write_cart_chunk (SF_PRIVATE *psf)
887 {	SF_CART_INFO_16K *c ;
888 	int k ;
889 
890 	if (psf->cart_16k == NULL)
891 		return -1 ;
892 
893 	c = psf->cart_16k ;
894 	psf_binheader_writef (psf, "m4", BHWm (cart_MARKER), BHW4 (WAV_CART_MIN_CHUNK_SIZE + c->tag_text_size)) ;
895 	/*
896 	**	Note that it is very important that the field widths of the SF_CART_INFO
897 	**	struct match those of the cart chunk fields.
898 	*/
899 	psf_binheader_writef (psf, "b", BHWv (c->version), BHWz (sizeof (c->version))) ;
900 	psf_binheader_writef (psf, "b", BHWv (c->title), BHWz (sizeof (c->title))) ;
901 	psf_binheader_writef (psf, "b", BHWv (c->artist), BHWz (sizeof (c->artist))) ;
902 	psf_binheader_writef (psf, "b", BHWv (c->cut_id), BHWz (sizeof (c->cut_id))) ;
903 	psf_binheader_writef (psf, "b", BHWv (c->client_id), BHWz (sizeof (c->client_id))) ;
904 	psf_binheader_writef (psf, "b", BHWv (c->category), BHWz (sizeof (c->category))) ;
905 	psf_binheader_writef (psf, "b", BHWv (c->classification), BHWz (sizeof (c->classification))) ;
906 	psf_binheader_writef (psf, "b", BHWv (c->out_cue), BHWz (sizeof (c->out_cue))) ;
907 	psf_binheader_writef (psf, "b", BHWv (c->start_date), BHWz (sizeof (c->start_date))) ;
908 	psf_binheader_writef (psf, "b", BHWv (c->start_time), BHWz (sizeof (c->start_time))) ;
909 	psf_binheader_writef (psf, "b", BHWv (c->end_date), BHWz (sizeof (c->end_date))) ;
910 	psf_binheader_writef (psf, "b", BHWv (c->end_time), BHWz (sizeof (c->end_time))) ;
911 	psf_binheader_writef (psf, "b", BHWv (c->producer_app_id), BHWz (sizeof (c->producer_app_id))) ;
912 	psf_binheader_writef (psf, "b", BHWv (c->producer_app_version), BHWz (sizeof (c->producer_app_version))) ;
913 	psf_binheader_writef (psf, "b", BHWv (c->user_def), BHWz (sizeof (c->user_def))) ;
914 	psf_binheader_writef (psf, "e4", BHW4 (c->level_reference)) ;
915 
916 	for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
917 		psf_binheader_writef (psf, "b4", BHWv (c->post_timers [k].usage), BHWz (4), BHW4 (c->post_timers [k].value)) ;
918 
919 	psf_binheader_writef (psf, "z", BHWz (sizeof (c->reserved))) ;	// just write zeros, we don't have any other use for it
920 	psf_binheader_writef (psf, "b", BHWv (c->url), BHWz (sizeof (c->url))) ;
921 
922 	if (c->tag_text_size > 0)
923 		psf_binheader_writef (psf, "b", BHWv (c->tag_text), BHWz (c->tag_text_size)) ;
924 
925 	return 0 ;
926 } /* wavlike_write_cart_chunk */
927 
928 int
wavlike_subchunk_parse(SF_PRIVATE * psf,int chunk,uint32_t chunk_length)929 wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length)
930 {	sf_count_t	current_pos ;
931 	char		buffer [2048] ;
932 	uint32_t 	chunk_size, bytesread = 0 ;
933 
934 	current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
935 
936 	if (chunk_length <= 8)
937 	{	/* This case is for broken files generated by PEAK. */
938 		psf_log_printf (psf, "%M : %u (weird length)\n", chunk, chunk_length) ;
939 		psf_binheader_readf (psf, "mj", &chunk, chunk_length - 4) ;
940 		psf_log_printf (psf, "  %M\n", chunk) ;
941 		return 0 ;
942 		} ;
943 
944 	if (current_pos + chunk_length > psf->filelength)
945 	{	psf_log_printf (psf, "%M : %u (should be %d)\n", chunk, chunk_length, (int) (psf->filelength - current_pos)) ;
946 		chunk_length = psf->filelength - current_pos ;
947 		}
948 	else
949 		psf_log_printf (psf, "%M : %u\n", chunk, chunk_length) ;
950 
951 	while (bytesread < chunk_length)
952 	{	uint32_t thisread ;
953 
954 		if ((thisread = psf_binheader_readf (psf, "m", &chunk)) == 0)
955 			break ;
956 		bytesread += thisread ;
957 
958 		switch (chunk)
959 		{	case adtl_MARKER :
960 			case INFO_MARKER :
961 					/* These markers don't contain anything, not even a chunk lebgth. */
962 					psf_log_printf (psf, "  %M\n", chunk) ;
963 					continue ;
964 
965 			case exif_MARKER :
966 					psf_log_printf (psf, "  %M\n", chunk) ;
967 					if (chunk_length > bytesread)
968 						bytesread += exif_subchunk_parse (psf, chunk_length - bytesread) ;
969 					continue ;
970 
971 			case data_MARKER :
972 					psf_log_printf (psf, "  %M inside a LIST block??? Backing out.\n", chunk) ;
973 					/* Jump back four bytes and return to caller. */
974 					psf_binheader_readf (psf, "j", -4) ;
975 					return 0 ;
976 
977 			case 0 :
978 					/*
979 					**	Four zero bytes where a marker was expected. Assume this means
980 					**	the rest of the chunk is garbage.
981 					*/
982 					psf_log_printf (psf, "    *** Found weird-ass zero marker. Jumping to end of chunk.\n") ;
983 					if (bytesread < chunk_length)
984 						bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ;
985 					psf_log_printf (psf, "    *** Offset is now : 0x%X\n", psf_fseek (psf, 0, SEEK_CUR)) ;
986 					return 0 ;
987 
988 			default :
989 					break ;
990 			} ;
991 
992 		switch (chunk)
993 		{	case ISFT_MARKER :
994 			case ICOP_MARKER :
995 			case IARL_MARKER :
996 			case IART_MARKER :
997 			case ICMT_MARKER :
998 			case ICRD_MARKER :
999 			case IENG_MARKER :
1000 			case IGNR_MARKER :
1001 			case INAM_MARKER :
1002 			case IPRD_MARKER :
1003 			case ISBJ_MARKER :
1004 			case ISRC_MARKER :
1005 			case IAUT_MARKER :
1006 			case ITRK_MARKER :
1007 					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1008 					chunk_size += (chunk_size & 1) ;
1009 					if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1010 					{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1011 						goto cleanup_subchunk_parse ;
1012 						} ;
1013 
1014 					bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1015 					buffer [chunk_size] = 0 ;
1016 					psf_log_printf (psf, "    %M : %s\n", chunk, buffer) ;
1017 					break ;
1018 
1019 			case labl_MARKER :
1020 					{	int mark_id ;
1021 
1022 						bytesread += psf_binheader_readf (psf, "44", &chunk_size, &mark_id) ;
1023 						chunk_size -= 4 ;
1024 						chunk_size += (chunk_size & 1) ;
1025 						if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1026 						{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1027 							goto cleanup_subchunk_parse ;
1028 							} ;
1029 
1030 						bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1031 						buffer [chunk_size] = 0 ;
1032 
1033 						if (mark_id < 10) /* avoid swamping log buffer with labels */
1034 							psf_log_printf (psf, "    %M : %u : %s\n", chunk, mark_id, buffer) ;
1035 						else if (mark_id == 10)
1036 							psf_log_printf (psf, "    (Skipping)\n") ;
1037 
1038 						if (psf->cues)
1039  						{	unsigned int i = 0 ;
1040 
1041 							/* find id to store label */
1042 							while (i < psf->cues->cue_count && psf->cues->cue_points [i].indx != mark_id)
1043 								i++ ;
1044 
1045 							if (i < psf->cues->cue_count)
1046 								memcpy (psf->cues->cue_points [i].name, buffer, sizeof (psf->cues->cue_points [i].name)) ;
1047 							} ;
1048 						} ;
1049 					break ;
1050 
1051 			case DISP_MARKER :
1052 			case ltxt_MARKER :
1053 			case note_MARKER :
1054 					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1055 					chunk_size += (chunk_size & 1) ;
1056 					if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1057 					{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1058 						goto cleanup_subchunk_parse ;
1059 						} ;
1060 
1061 					psf_log_printf (psf, "    %M : %u\n", chunk, chunk_size) ;
1062 					goto cleanup_subchunk_parse ;
1063 
1064 			default :
1065 					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1066 					chunk_size += (chunk_size & 1) ;
1067 					psf_log_printf (psf, "    *** %M : %u\n", chunk, chunk_size) ;
1068 					if (bytesread + chunk_size > chunk_length)
1069 					{	bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ;
1070 						continue ;
1071 						}
1072 					else
1073 						bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
1074 
1075 					if (chunk_size >= chunk_length)
1076 						return 0 ;
1077 					break ;
1078 			} ;
1079 
1080 		switch (chunk)
1081 		{	case ISFT_MARKER :
1082 					psf_store_string (psf, SF_STR_SOFTWARE, buffer) ;
1083 					break ;
1084 			case ICOP_MARKER :
1085 					psf_store_string (psf, SF_STR_COPYRIGHT, buffer) ;
1086 					break ;
1087 			case INAM_MARKER :
1088 					psf_store_string (psf, SF_STR_TITLE, buffer) ;
1089 					break ;
1090 			case IART_MARKER :
1091 					psf_store_string (psf, SF_STR_ARTIST, buffer) ;
1092 					break ;
1093 			case ICMT_MARKER :
1094 					psf_store_string (psf, SF_STR_COMMENT, buffer) ;
1095 					break ;
1096 			case ICRD_MARKER :
1097 					psf_store_string (psf, SF_STR_DATE, buffer) ;
1098 					break ;
1099 			case IGNR_MARKER :
1100 					psf_store_string (psf, SF_STR_GENRE, buffer) ;
1101 					break ;
1102 			case IPRD_MARKER :
1103 					psf_store_string (psf, SF_STR_ALBUM, buffer) ;
1104 					break ;
1105 			case ITRK_MARKER :
1106 					psf_store_string (psf, SF_STR_TRACKNUMBER, buffer) ;
1107 					break ;
1108 			} ;
1109 		} ;
1110 
1111 cleanup_subchunk_parse :
1112 
1113 	if (chunk_length > bytesread)
1114 		bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ;
1115 
1116 	return 0 ;
1117 } /* wavlike_subchunk_parse */
1118 
1119 void
wavlike_write_strings(SF_PRIVATE * psf,int location)1120 wavlike_write_strings (SF_PRIVATE *psf, int location)
1121 {	int	k, prev_head_index, saved_head_index ;
1122 
1123 	if (psf_location_string_count (psf, location) == 0)
1124 		return ;
1125 
1126 	prev_head_index = psf->header.indx + 4 ;
1127 
1128 	psf_binheader_writef (psf, "m4m", BHWm (LIST_MARKER), BHW4 (0xBADBAD), BHWm (INFO_MARKER)) ;
1129 
1130 	for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1131 	{	if (psf->strings.data [k].type == 0)
1132 			break ;
1133 		if (psf->strings.data [k].type < 0 || psf->strings.data [k].flags != location)
1134 			continue ;
1135 
1136 		switch (psf->strings.data [k].type)
1137 		{	case SF_STR_SOFTWARE :
1138 				psf_binheader_writef (psf, "ms", BHWm (ISFT_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1139 				break ;
1140 
1141 			case SF_STR_TITLE :
1142 				psf_binheader_writef (psf, "ms", BHWm (INAM_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1143 				break ;
1144 
1145 			case SF_STR_COPYRIGHT :
1146 				psf_binheader_writef (psf, "ms", BHWm (ICOP_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1147 				break ;
1148 
1149 			case SF_STR_ARTIST :
1150 				psf_binheader_writef (psf, "ms", BHWm (IART_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1151 				break ;
1152 
1153 			case SF_STR_COMMENT :
1154 				psf_binheader_writef (psf, "ms", BHWm (ICMT_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1155 				break ;
1156 
1157 			case SF_STR_DATE :
1158 				psf_binheader_writef (psf, "ms", BHWm (ICRD_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1159 				break ;
1160 
1161 			case SF_STR_GENRE :
1162 				psf_binheader_writef (psf, "ms", BHWm (IGNR_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1163 				break ;
1164 
1165 			case SF_STR_ALBUM :
1166 				psf_binheader_writef (psf, "ms", BHWm (IPRD_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1167 				break ;
1168 
1169 			case SF_STR_TRACKNUMBER :
1170 				psf_binheader_writef (psf, "ms", BHWm (ITRK_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1171 				break ;
1172 
1173 			default :
1174 				break ;
1175 			} ;
1176 		} ;
1177 
1178 	saved_head_index = psf->header.indx ;
1179 	psf->header.indx = prev_head_index ;
1180 	psf_binheader_writef (psf, "4", BHW4 (saved_head_index - prev_head_index - 4)) ;
1181 	psf->header.indx = saved_head_index ;
1182 
1183 } /* wavlike_write_strings */
1184 
1185 int
wavlike_read_peak_chunk(SF_PRIVATE * psf,size_t chunk_size)1186 wavlike_read_peak_chunk (SF_PRIVATE * psf, size_t chunk_size)
1187 {	char		buffer [256] ;
1188 	uint32_t uk ;
1189 
1190 	if (chunk_size != WAVLIKE_PEAK_CHUNK_SIZE (psf->sf.channels))
1191 	{	psf_binheader_readf (psf, "j", chunk_size) ;
1192 		psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
1193 		return SFE_WAV_BAD_PEAK ;
1194 		} ;
1195 
1196 	if (psf->peak_info)
1197 	{	psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
1198 		free (psf->peak_info) ;
1199 		psf->peak_info = NULL ;
1200 		} ;
1201 	if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
1202 		return SFE_MALLOC_FAILED ;
1203 
1204 	/* read in rest of PEAK chunk. */
1205 	psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
1206 
1207 	if (psf->peak_info->version != 1)
1208 		psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
1209 	else
1210 		psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
1211 
1212 	psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
1213 	psf_log_printf (psf, "    Ch   Position       Value\n") ;
1214 
1215 	for (uk = 0 ; uk < (uint32_t) psf->sf.channels ; uk++)
1216 	{	float value ;
1217 		uint32_t position ;
1218 
1219 		psf_binheader_readf (psf, "f4", &value, &position) ;
1220 		psf->peak_info->peaks [uk].value = value ;
1221 		psf->peak_info->peaks [uk].position = position ;
1222 
1223 		snprintf (buffer, sizeof (buffer), "    %2d   %-12" PRId64 "   %g\n",
1224 				uk, psf->peak_info->peaks [uk].position, psf->peak_info->peaks [uk].value) ;
1225 		buffer [sizeof (buffer) - 1] = 0 ;
1226 		psf_log_printf (psf, "%s", buffer) ;
1227 		} ;
1228 
1229 	return 0 ;
1230 } /* wavlike_read_peak_chunk */
1231 
1232 void
wavlike_write_peak_chunk(SF_PRIVATE * psf)1233 wavlike_write_peak_chunk (SF_PRIVATE * psf)
1234 {	int k ;
1235 
1236 	if (psf->peak_info == NULL)
1237 		return ;
1238 
1239 	psf_binheader_writef (psf, "m4", BHWm (PEAK_MARKER), BHW4 (WAVLIKE_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1240 	psf_binheader_writef (psf, "44", BHW4 (1), BHW4 (time (NULL))) ;
1241 	for (k = 0 ; k < psf->sf.channels ; k++)
1242 		psf_binheader_writef (psf, "ft8", BHWf (psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1243 } /* wavlike_write_peak_chunk */
1244 
1245 /*==============================================================================
1246 */
1247 
1248 static int
exif_fill_and_sink(SF_PRIVATE * psf,char * buf,size_t bufsz,size_t toread)1249 exif_fill_and_sink (SF_PRIVATE *psf, char* buf, size_t bufsz, size_t toread)
1250 {
1251 	size_t bytesread = 0 ;
1252 
1253 	buf [0] = 0 ;
1254 	bufsz -= 1 ;
1255 	if (toread < bufsz)
1256 		bufsz = toread ;
1257 	bytesread = psf_binheader_readf (psf, "b", buf, bufsz) ;
1258 	buf [bufsz] = 0 ;
1259 
1260 	if (bytesread == bufsz && toread > bufsz)
1261 		bytesread += psf_binheader_readf (psf, "j", toread - bufsz) ;
1262 
1263 	return bytesread ;
1264 } /* exif_fill_and_sink */
1265 
1266 /*
1267 ** Exif specification for audio files, at JEITA CP-3451 Exif 2.2 section 5
1268 ** (Exif Audio File Specification) http://www.exif.org/Exif2-2.PDF
1269 */
1270 static int
exif_subchunk_parse(SF_PRIVATE * psf,uint32_t length)1271 exif_subchunk_parse (SF_PRIVATE *psf, uint32_t length)
1272 {	uint32_t marker, dword = 0, vmajor = -1, vminor = -1, bytesread = 0 ;
1273 	char buf [4096] ;
1274 	int thisread ;
1275 
1276 	while (bytesread < length)
1277 	{
1278 		if ((thisread = psf_binheader_readf (psf, "m", &marker)) == 0)
1279 			break ;
1280 		bytesread += thisread ;
1281 
1282 		switch (marker)
1283 		{
1284 			case 0 : /* camera padding? */
1285 				break ;
1286 
1287 			case ever_MARKER :
1288 				bytesread += psf_binheader_readf (psf, "j4", 4, &dword) ;
1289 				vmajor = 10 * (((dword >> 24) & 0xff) - '0') + (((dword >> 16) & 0xff) - '0') ;
1290 				vminor = 10 * (((dword >> 8) & 0xff) - '0') + ((dword & 0xff) - '0') ;
1291 				psf_log_printf (psf, "    EXIF Version : %u.%02u\n", vmajor, vminor) ;
1292 				break ;
1293 
1294 			case olym_MARKER :
1295 				bytesread += psf_binheader_readf (psf, "4", &dword) ;
1296 				psf_log_printf (psf, "%M : %u\n", marker, dword) ;
1297 				if (dword > length || bytesread + dword > length)
1298 					break ;
1299 				dword += (dword & 1) ;
1300 				bytesread += psf_binheader_readf (psf, "j", dword) ;
1301 				break ;
1302 
1303 			case emnt_MARKER : /* design information: null-terminated string */
1304 			case emdl_MARKER : /* model name ; null-terminated string */
1305 			case ecor_MARKER : /* manufacturer: null-terminated string */
1306 			case etim_MARKER : /* creation time: null-terminated string in the format "hour:minute:second.subsecond" */
1307 			case erel_MARKER : /* relation info: null-terminated string (filename) */
1308 			case eucm_MARKER : /* user comment: 4-byte size follows, then possibly unicode data */
1309 				bytesread += psf_binheader_readf (psf, "4", &dword) ;
1310 				bytesread += sizeof (dword) ;
1311 				dword += (dword & 1) ;
1312 
1313 				if (dword >= sizeof (buf))
1314 				{	psf_log_printf (psf, "*** Marker '%M' is too big %u\n\n", marker, dword) ;
1315 					return bytesread ;
1316 					} ;
1317 
1318 				bytesread += exif_fill_and_sink (psf, buf, sizeof (buf), dword) ;
1319 
1320 				/* BAD - don't know what's going on here -- maybe a bug in the camera */
1321 				/* field should be NULL-terminated but there's no room for it with the reported number */
1322 				/*  example output:     emdl : 8 (EX-Z1050) */
1323 				if (marker == emdl_MARKER && dword == strlen (buf) /* should be >= strlen+1*/)
1324 				{	psf_log_printf (psf, "    *** field size too small for string (sinking 2 bytes)\n") ;
1325 					bytesread += psf_binheader_readf (psf, "j", 2) ;
1326 					} ;
1327 
1328 				psf_log_printf (psf, "    %M : %u (%s)\n", marker, dword, buf) ;
1329 				if (dword > length)
1330 					return bytesread ;
1331 				break ;
1332 
1333 			default :
1334 				psf_log_printf (psf, "    *** %M (%u): -- ignored --\n", marker, marker) ;
1335 				break ;
1336 			} ;
1337 		} ;
1338 
1339 	return bytesread ;
1340 } /* exif_subchunk_parse */
1341 
1342 void
wavlike_write_custom_chunks(SF_PRIVATE * psf)1343 wavlike_write_custom_chunks (SF_PRIVATE * psf)
1344 {	uint32_t k ;
1345 
1346 	for (k = 0 ; k < psf->wchunks.used ; k++)
1347 		psf_binheader_writef (psf, "m4b", BHWm (psf->wchunks.chunks [k].mark32), BHW4 (psf->wchunks.chunks [k].len),
1348 							BHWv (psf->wchunks.chunks [k].data), BHWz (psf->wchunks.chunks [k].len)) ;
1349 
1350 } /* wavlike_write_custom_chunks */
1351