src/(cart|wav).c : Minor cleanups.

This commit is contained in:
Erik de Castro Lopo 2013-02-11 19:22:29 +11:00
parent 1640ab2eef
commit e866561923
2 changed files with 32 additions and 28 deletions

View File

@ -1,6 +1,6 @@
/*
** Copyright (C) 2012 Chris Roberts <c.roberts@csrfm.com>
** Copyright (C) 2006-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2006-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2006 Paul Davis <paul@linuxaudiosystems.com>
**
** This program is free software; you can redistribute it and/or modify
@ -34,7 +34,7 @@ cart_min_size (const SF_CART_INFO* info)
return 0 ;
return offsetof (SF_CART_INFO, tag_text) + info->tag_text_size ;
} /* bc_min_size */
} /* cart_min_size */
SF_CART_INFO_16K*
cart_var_alloc (void)
@ -46,33 +46,41 @@ cart_var_alloc (void)
int
cart_var_set (SF_PRIVATE *psf, const SF_CART_INFO * info, size_t datasize)
{ size_t len ;
if (info == NULL)
return SF_FALSE ;
if (cart_min_size (info) > datasize)
{ psf->error = SFE_BAD_CART_INFO_SIZE ;
return SF_FALSE ;
} ;
if (datasize >= sizeof (SF_CART_INFO_16K))
{ psf->error = SFE_BAD_CART_INFO_TOO_BIG ;
return SF_FALSE ;
} ;
if (psf->cart_16k == NULL)
{
if ((psf->cart_16k = cart_var_alloc ()) == NULL)
{
psf->error = SFE_MALLOC_FAILED ;
{ if ((psf->cart_16k = cart_var_alloc ()) == NULL)
{ psf->error = SFE_MALLOC_FAILED ;
return SF_FALSE ;
}
}
} ;
} ;
memcpy (psf->cart_16k, info, offsetof (SF_CART_INFO, tag_text)) ;
psf_strlcpy_crlf (psf->cart_16k->tag_text, info->tag_text, sizeof (psf->cart_16k->tag_text), datasize - offsetof (SF_CART_INFO, tag_text)) ;
len = strlen (psf->cart_16k->tag_text) ;
if (len > 0 && psf->cart_16k->tag_text [len - 1] != '\n')
psf_strlcat (psf->cart_16k->tag_text, sizeof (psf->cart_16k->tag_text), "\r\n") ;
/* Force tag_text_size to be even. */
len = strlen (psf->cart_16k->tag_text) ;
len += (len & 1) ? 1 : 2 ;
psf->cart_16k->tag_text_size = len ;
return SF_TRUE ;
} /* cart_var_set */

View File

@ -1784,7 +1784,7 @@ int
wav_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
{ SF_CART_INFO_16K *c ;
uint32_t bytes = 0 ;
SF_CART_TIMER *timer ;
int k ;
if (chunksize < WAV_CART_MIN_CHUNK_SIZE)
{ psf_log_printf (psf, "cart : %u (should be >= %d)\n", chunksize, WAV_CART_MIN_CHUNK_SIZE) ;
@ -1826,15 +1826,12 @@ wav_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
bytes += psf_binheader_readf (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
bytes += psf_binheader_readf (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
bytes += psf_binheader_readf (psf, "b", c->user_def, sizeof (c->user_def)) ;
bytes += psf_binheader_readf (psf, "4", &c->level_reference, sizeof (c->level_reference)) ;
timer = c->post_timers ; // point at the first timer
for (int f = 0 ; f < 8 ; f++) //TODO: make this less magic - 8 is the maximum number of timers, should make this a constant
{
bytes += psf_binheader_readf (psf, "b", &timer->usage, make_size_t (4)) ;
bytes += psf_binheader_readf (psf, "4", &timer->value) ;
timer++ ;
}
bytes += psf_binheader_readf (psf, "j", sizeof (c->reserved)) ; // discard the reserved data ... ?
bytes += psf_binheader_readf (psf, "e4", &c->level_reference, sizeof (c->level_reference)) ;
for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
bytes += psf_binheader_readf (psf, "b4", &c->post_timers [k].usage, make_size_t (4), &c->post_timers [k].value) ;
bytes += psf_binheader_readf (psf, "b", c->reserved, sizeof (c->reserved)) ;
bytes += psf_binheader_readf (psf, "b", c->url, sizeof (c->url)) ;
if (chunksize > WAV_CART_MIN_CHUNK_SIZE)
@ -1842,14 +1839,15 @@ wav_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
c->tag_text_size = chunksize - WAV_CART_MIN_CHUNK_SIZE ;
bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
} ;
return 0 ;
return 0 ;
} /* wav_read_cart_chunk */
int
wav_write_cart_chunk (SF_PRIVATE *psf)
{ SF_CART_INFO_16K *c ;
SF_CART_TIMER *timer ;
int k ;
if (psf->cart_16k == NULL)
return -1 ;
@ -1875,18 +1873,16 @@ wav_write_cart_chunk (SF_PRIVATE *psf)
psf_binheader_writef (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
psf_binheader_writef (psf, "b", c->user_def, sizeof (c->user_def)) ;
psf_binheader_writef (psf, "4", c->level_reference, sizeof (c->level_reference)) ;
// TODO : sort out them pesky timers
timer = (SF_CART_TIMER *) c->post_timers ;
for (int f = 0 ; f < 8 ; f++) //TODO: make this less magic - 8 is the maximum number of timers, should make this a constant
{
psf_binheader_writef (psf, "b", (c->post_timers + f)->usage, make_size_t (4)) ;
psf_binheader_writef (psf, "4", (c->post_timers + f)->value) ;
timer++ ;
}
for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
psf_binheader_writef (psf, "b4", c->post_timers [k].usage, make_size_t (4), c->post_timers [k].value) ;
psf_binheader_writef (psf, "z", sizeof (c->reserved)) ; // just write zeros, we don't have any other use for it
psf_binheader_writef (psf, "b", c->url, sizeof (c->url)) ;
if (c->tag_text_size > 0)
psf_binheader_writef (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
return 0 ;
} /* wav_write_cart_chunk */