ref: 8bc85b09e935c41171ef74be94a1da0a78780f41
parent: a8e09ce5a8a2818a11f926962f1cbd71d469d0bf
author: rrt <rrt>
date: Mon Dec 11 19:20:07 EST 2006
Move some more generic functions into misc.c.
--- a/src/hcom.c
+++ b/src/hcom.c
@@ -48,8 +48,6 @@
short sample;
};
-static int skipbytes(ft_t, int);
-
static int st_hcomstartread(ft_t ft)
{
struct readpriv *p = (struct readpriv *) ft->priv;
@@ -70,7 +68,7 @@
}
/* Skip first 65 bytes of header */
- rc = skipbytes(ft, 65);
+ rc = st_skipbytes(ft, 65);
if (rc)
return rc;
@@ -82,7 +80,7 @@
}
/* Skip to byte 83 */
- rc = skipbytes(ft, 83-69);
+ rc = st_skipbytes(ft, 83-69);
if (rc)
return rc;
@@ -91,7 +89,7 @@
st_readdw(ft, &rsrcsize); /* bytes 87-90 */
/* Skip the rest of the header (total 128 bytes) */
- rc = skipbytes(ft, 128-91);
+ rc = st_skipbytes(ft, 128-91);
if (rc != 0)
return rc;
@@ -143,7 +141,7 @@
p->dictionary[i].dict_rightson);
*/
}
- rc = skipbytes(ft, 1); /* skip pad byte */
+ rc = st_skipbytes(ft, 1); /* skip pad byte */
if (rc)
return rc;
@@ -160,20 +158,6 @@
return (ST_SUCCESS);
}
-/* FIXME: Move to misc.c */
-static int skipbytes(ft_t ft, int n)
-{
- unsigned char trash;
- while (--n >= 0) {
- if (st_readb(ft, &trash) == ST_EOF)
- {
- st_fail_errno(ft,ST_EOF,"unexpected EOF in Mac header");
- return(ST_EOF);
- }
- }
- return(ST_SUCCESS);
-}
-
static st_size_t st_hcomread(ft_t ft, st_sample_t *buf, st_size_t len)
{
register struct readpriv *p = (struct readpriv *) ft->priv;
@@ -359,22 +343,7 @@
static int nbits;
static int32_t curword;
-/* FIXME: Place in misc.c */
-static void putlong(unsigned char *c, int32_t v)
-{
- *c++ = (v >> 24) & 0xff;
- *c++ = (v >> 16) & 0xff;
- *c++ = (v >> 8) & 0xff;
- *c++ = v & 0xff;
-}
-static void putshort(unsigned char *c, short v)
-{
- *c++ = (v >> 8) & 0xff;
- *c++ = v & 0xff;
-}
-
-
static void putcode(unsigned char c, unsigned char **df)
{
long code, size;
@@ -387,9 +356,8 @@
if(code & 1) curword += 1;
nbits++;
if(nbits == 32) {
- putlong(*df, curword);
+ put32_be(df, curword);
checksum += curword;
- (*df) += 4;
nbits = 0;
curword = 0;
}
@@ -401,7 +369,7 @@
{
int32_t samplerate;
unsigned char *datafork = *df;
- unsigned char *ddf;
+ unsigned char *ddf, *p;
short dictsize;
int frequtable[256];
int i, sample, j, k, d, l, frequcount;
@@ -475,10 +443,8 @@
}
ddf = datafork + 22;
for(i = 0; i < dictsize; i++) {
- putshort(ddf, dictionary[i].dict_leftson);
- ddf += 2;
- putshort(ddf, dictionary[i].dict_rightson);
- ddf += 2;
+ put16_be(&ddf, dictionary[i].dict_leftson);
+ put16_be(&ddf, dictionary[i].dict_rightson);
}
*ddf++ = 0;
*ddf++ = *(*df)++;
@@ -492,12 +458,13 @@
putcode(0, &ddf);
}
strncpy((char *) datafork, "HCOM", 4);
- putlong(datafork + 4, *dl);
- putlong(datafork + 8, checksum);
- putlong(datafork + 12, 1L);
+ p = datafork + 4;
+ put32_be(&p, *dl);
+ put32_be(&p, checksum);
+ put32_be(&p, 1L);
samplerate = 22050 / (int32_t)fr;
- putlong(datafork + 16, samplerate);
- putshort(datafork + 20, dictsize);
+ put32_be(&p, samplerate);
+ put16_be(&p, dictsize);
*df = datafork; /* reassign passed pointer to new datafork */
*dl = l; /* and its compressed length */
@@ -504,14 +471,6 @@
return (ST_SUCCESS);
}
-/* FIXME: Place in misc.c */
-static void padbytes(ft_t ft, int n)
-{
- while (--n >= 0)
- st_writeb(ft, '\0');
-}
-
-
/* End of hcom utility routines */
static int st_hcomstopwrite(ft_t ft)
@@ -532,12 +491,12 @@
/* Write the header */
st_writebuf(ft, (void *)"\000\001A", 1, 3); /* Dummy file name "A" */
- padbytes(ft, 65-3);
+ st_padbytes(ft, 65-3);
st_writes(ft, "FSSD");
- padbytes(ft, 83-69);
+ st_padbytes(ft, 83-69);
st_writedw(ft, (uint32_t) compressed_len); /* compressed_data size */
st_writedw(ft, (uint32_t) 0); /* rsrc size */
- padbytes(ft, 128 - 91);
+ st_padbytes(ft, 128 - 91);
if (st_error(ft))
{
st_fail_errno(ft,errno,"write error in HCOM header");
@@ -558,7 +517,7 @@
return rc;
/* Pad the compressed_data fork to a multiple of 128 bytes */
- padbytes(ft, 128 - (int) (compressed_len%128));
+ st_padbytes(ft, 128 - (int) (compressed_len%128));
return (ST_SUCCESS);
}
--- a/src/misc.c
+++ b/src/misc.c
@@ -83,12 +83,33 @@
/* Read in a buffer of data of length len and each element is size bytes.
* Returns number of elements read, not bytes read.
*/
-
size_t st_readbuf(ft_t ft, void *buf, size_t size, st_size_t len)
{
return fread(buf, size, len, ft->fp);
}
+/* Skip input without seeking. */
+int st_skipbytes(ft_t ft, st_size_t n)
+{
+ unsigned char trash;
+
+ while (n--)
+ if (st_readb(ft, &trash) == ST_EOF)
+ return (ST_EOF);
+
+ return (ST_SUCCESS);
+}
+
+/* Pad output. */
+int st_padbytes(ft_t ft, st_size_t n)
+{
+ while (n--)
+ if (st_writeb(ft, '\0') == ST_EOF)
+ return (ST_EOF);
+
+ return (ST_SUCCESS);
+}
+
/* Write a buffer of data of length len and each element is size bytes.
* Returns number of elements writen, not bytes written.
*/
@@ -362,6 +383,20 @@
*(*p)++ = (val >> 8) & 0xff;
}
+void put32_be(unsigned char **p, int32_t val)
+{
+ *(*p)++ = (val >> 24) & 0xff;
+ *(*p)++ = (val >> 16) & 0xff;
+ *(*p)++ = (val >> 8) & 0xff;
+ *(*p)++ = val & 0xff;
+}
+
+void put16_be(unsigned char **p, short val)
+{
+ *(*p)++ = (val >> 8) & 0xff;
+ *(*p)++ = val & 0xff;
+}
+
/* generic swap routine. Swap l and place in to f (datatype length = n) */
static void st_swapb(char *l, char *f, int n)
{
@@ -390,7 +425,6 @@
return u.f;
}
-
uint32_t st_swap24(uint24_t udw)
{
return ((udw >> 16) & 0xff) | (udw & 0xff00) | ((udw << 16) & 0xff0000L);
@@ -567,7 +601,7 @@
return PACKAGE_VERSION;
}
-/* Implements traditional fseeko() behavior. Meant to abstract out
+/* Implements traditional fseek() behavior. Meant to abstract out
* file operations so that they could one day also work on memory
* buffers.
*
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -61,6 +61,8 @@
*/
/* declared in misc.c */
size_t st_readbuf(ft_t ft, void *buf, size_t size, st_size_t len);
+int st_skipbytes(ft_t ft, st_size_t n);
+int st_padbytes(ft_t ft, st_size_t n);
size_t st_writebuf(ft_t ft, void const *buf, size_t size, st_size_t len);
int st_reads(ft_t ft, char *c, st_size_t len);
int st_writes(ft_t ft, char *c);
@@ -90,6 +92,8 @@
uint16_t get16_le(unsigned char **p);
void put32_le(unsigned char **p, uint32_t val);
void put16_le(unsigned char **p, int16_t val);
+void put32_be(unsigned char **p, int32_t val);
+void put16_be(unsigned char **p, short val);
/* Utilities to byte-swap values, use libc optimized macros if possible */
#ifdef HAVE_BYTESWAP_H