shithub: sox

Download patch

ref: d020c097f327091bd71faa55221c63dde933b66b
parent: 279dd05d06b7f5f15bb251fd3058ad119c828df6
author: cbagwell <cbagwell>
date: Thu Jan 6 00:15:10 EST 2000

Added st_reads() functions and converted files to use them.

--- a/src/8svx.c
+++ b/src/8svx.c
@@ -58,13 +58,13 @@
 	channels = 1;
 
 	/* read FORM chunk */
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "FORM", 4) != 0)
 	{
 		fail("8SVX: header does not begin with magic word 'FORM'");
 		return(ST_EOF);
 	}
 	st_readdw(ft, &totalsize);
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "8SVX", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "8SVX", 4) != 0)
 	{
 		fail("8SVX: 'FORM' chunk does not specify '8SVX' as type");
 		return(ST_EOF);
@@ -71,7 +71,7 @@
 	}
 
 	/* read chunks until 'BODY' (or end) */
-	while (fread(buf,1,4,ft->fp) == 4 && strncmp(buf,"BODY",4) != 0) {
+	while (st_reads(ft, buf, 4) == ST_SUCCESS && strncmp(buf,"BODY",4) != 0) {
 		if (strncmp(buf,"VHDR",4) == 0) {
 			st_readdw(ft, &chunksize);
 			if (chunksize != 20)
--- a/src/aiff.c
+++ b/src/aiff.c
@@ -111,13 +111,13 @@
 	}
 
 	/* FORM chunk */
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "FORM", 4) != 0)
 	{
 		fail("AIFF header does not begin with magic word 'FORM'");
 		return(ST_EOF);
 	}
 	st_readdw(ft, &totalsize);
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "AIFF", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "AIFF", 4) != 0)
 	{
 		fail("AIFF 'FORM' chunk does not specify 'AIFF' as type");
 		return(ST_EOF);
@@ -127,7 +127,7 @@
 	/* Skip everything but the COMM chunk and the SSND chunk */
 	/* The SSND chunk must be the last in the file */
 	while (1) {
-		if (fread(buf, 1, 4, ft->fp) != 4)
+		if (st_reads(ft, buf, 4) == ST_EOF)
 		{
 			if (ssndsize > 0)
 				break;
--- a/src/au.c
+++ b/src/au.c
@@ -260,7 +260,7 @@
 	unsigned char		in_byte;
 
 	if (p->in_bits < p->dec_bits) {
-		if (fread(&in_byte, sizeof (char), 1, ft->fp) != 1) {
+	        if (st_readb(ft, &in_byte) == ST_EOF) {
 			*code = 0;
 			return (-1);
 		}
--- a/src/auto.c
+++ b/src/auto.c
@@ -27,7 +27,7 @@
 		fail("Type AUTO input must be a file, not a pipe");
 		return(ST_EOF);
 	}
-	if (fread(header, 1, sizeof header, ft->fp) != sizeof header)
+	if (fread(header, 1, sizeof(header), ft->fp) != sizeof(header))
 	{
 		fail("Type AUTO detects short file");
 		return(ST_EOF);
--- a/src/avr.c
+++ b/src/avr.c
@@ -29,7 +29,7 @@
 /* Taken from the Audio File Formats FAQ */
 
 typedef struct avrstuff {
-  char magic [4];      /* 2BIT */
+  char magic [5];      /* 2BIT */
   char name [8];       /* null-padded sample name */
   unsigned short mono; /* 0 = mono, 0xffff = stereo */
   unsigned short rez;  /* 8 = 8 bit, 16 = 16 bit */
@@ -83,7 +83,7 @@
   if (rc)
       return rc;
 
-  fread (avr->magic, 1, sizeof (avr->magic), ft->fp);
+  st_reads(ft, avr->magic, 4);
 
   if (strncmp (avr->magic, AVR_MAGIC, 4)) {
     fail ("AVR: unknown header");
@@ -90,7 +90,7 @@
     return(ST_EOF);
   }
 
-  fread (avr->name, 1, sizeof (avr->name), ft->fp);
+  fread(avr->name, 1, sizeof(avr->name), ft->fp);
 
   st_readw (ft, &(avr->mono));
   if (avr->mono) {
@@ -145,9 +145,9 @@
 
   st_readw (ft, &(avr->res3));
 
-  fread (avr->ext, 1, sizeof (avr->ext), ft->fp);
+  fread(avr->ext, 1, sizeof(avr->ext), ft->fp);
 
-  fread (avr->user, 1, sizeof (avr->user), ft->fp);
+  fread(avr->user, 1, sizeof(avr->user), ft->fp);
 
   return(ST_SUCCESS);
 }
@@ -178,10 +178,17 @@
       return rc;
 
   /* magic */
-  fwrite (AVR_MAGIC, 1, sizeof (avr->magic), ft->fp);
+  st_writes(ft, AVR_MAGIC);
 
   /* name */
-  fwrite ("\0\0\0\0\0\0\0\0", 1, sizeof (avr->name), ft->fp);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
+  st_writeb(ft, 0);
 
   /* mono */
   if (ft->info.channels == 1) {
--- a/src/dat.c
+++ b/src/dat.c
@@ -42,7 +42,8 @@
    char sc;
 
    while (ft->info.rate == 0) {
-      fgets(inpstr,82,ft->fp);
+      fgets(inpstr, 82, ft->fp);
+      inpstr[81] = 0;
       sscanf(inpstr," %c",&sc);
       if (sc != ';') 
       {
@@ -69,6 +70,7 @@
 {
    dat_t dat = (dat_t) ft->priv;
    double srate;
+   char s[80];
 
    if (ft->info.channels > 1)
    {
@@ -83,10 +85,11 @@
    srate = ft->info.rate;
    dat->deltat = 1.0 / srate;
 #ifdef __alpha__
-   fprintf(ft->fp,"; Sample Rate %d\015\n", ft->info.rate);
+   sprintf(s,"; Sample Rate %d\015\n", ft->info.rate);
 #else
-   fprintf(ft->fp,"; Sample Rate %ld\015\n",ft->info.rate);
+   sprintf(s,"; Sample Rate %ld\015\n",ft->info.rate);
 #endif
+   st_writes(ft, s);
 
    return (ST_SUCCESS);
 }
@@ -129,11 +132,13 @@
     dat_t dat = (dat_t) ft->priv;
     int done = 0;
     double sampval;
+    char s[80];
 
     while(done < nsamp) {
        sampval = *buf++ ;
        sampval = sampval / 2.147483648e9;  /* normalize to approx 1.0 */
-       fprintf(ft->fp," %15.8g  %15.8g \015\n",dat->timevalue,sampval);
+       sprintf(s," %15.8g  %15.8g \015\n",dat->timevalue,sampval);
+       st_writes(ft, s);
        dat->timevalue += dat->deltat;
        done++;
        }
--- a/src/hcom.c
+++ b/src/hcom.c
@@ -54,7 +54,7 @@
 {
 	struct readpriv *p = (struct readpriv *) ft->priv;
 	int i;
-	char buf[4];
+	char buf[5];
 	ULONG datasize, rsrcsize;
 	ULONG huffcount, checksum, compresstype, divisor;
 	unsigned short dictsize;
@@ -79,7 +79,7 @@
 	    return rc;
 
 	/* Check the file type (bytes 65-68) */
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FSSD", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "FSSD", 4) != 0)
 	{
 		fail("Mac header type is not FSSD");
 		return (ST_EOF);
@@ -100,7 +100,7 @@
 	    return rc;
 
 	/* The data fork must contain a "HCOM" header */
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "HCOM", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "HCOM", 4) != 0)
 	{
 		fail("Mac data fork is not HCOM");
 		return (ST_EOF);
@@ -565,9 +565,9 @@
 	    return 0;
 
 	/* Write the header */
-	(void) fwrite("\000\001A", 1, 3, ft->fp); /* Dummy file name "A" */
+	fwrite("\000\001A", 1, 3, ft->fp); /* Dummy file name "A" */
 	padbytes(ft, 65-3);
-	(void) fwrite("FSSD", 1, 4, ft->fp);
+	st_writes(ft, "FSSD");
 	padbytes(ft, 83-69);
 	st_writedw(ft, (ULONG) compressed_len); /* compressed_data size */
 	st_writedw(ft, (ULONG) 0); /* rsrc size */
--- a/src/maud.c
+++ b/src/maud.c
@@ -75,7 +75,7 @@
 	}
 	
 	/* read FORM chunk */
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "FORM", 4) != 0)
 	{
 		fail("MAUD: header does not begin with magic word 'FORM'");
 		return (ST_EOF);
@@ -83,7 +83,7 @@
 	
 	st_readdw(ft, &trash); /* totalsize */
 	
-	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "MAUD", 4) != 0)
+	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "MAUD", 4) != 0)
 	{
 		fail("MAUD: 'FORM' chunk does not specify 'MAUD' as type");
 		return(ST_EOF);
@@ -91,7 +91,7 @@
 	
 	/* read chunks until 'BODY' (or end) */
 	
-	while (fread(buf,1,4,ft->fp) == 4 && strncmp(buf,"MDAT",4) != 0) {
+	while (st_reads(ft, buf, 4) == ST_SUCCESS && strncmp(buf,"MDAT",4) != 0) {
 		
 		/*
 		buf[4] = 0;
--- a/src/misc.c
+++ b/src/misc.c
@@ -18,7 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
-#include <strings.h>
+#include <string.h>
 
 const char *st_sizes_str[] = {
 	"NONSENSE!",
@@ -51,7 +51,37 @@
  * They all return ST_EOF on error and ST_SUCCESS on success.
  */
 
-/* Write string. */
+/* Read n-char string (and possibly null-terminating). */
+int
+st_reads(ft, c, len)
+ft_t ft;
+char *c;
+int len;
+{
+    char *sc;
+    char in;
+
+    sc = c;
+    do
+    {
+	if (fread(&in, 1, 1, ft->fp) != 1)
+	{
+	    *sc = 0;
+	    return (ST_EOF);
+	}
+	if (in == 0)
+	{
+	    break;
+	}
+
+	*sc = in;
+	sc++;
+    } while (sc - c < len);
+    *sc = 0;
+    return(ST_SUCCESS);
+}
+
+/* Write null-terminated string (without \0). */
 int
 st_writes(ft, c)
 ft_t ft;
--- a/src/sf.c
+++ b/src/sf.c
@@ -87,7 +87,7 @@
 	if (rc)
 	    return rc;
 	
-	if (fread(&sfhead, 1, sizeof(SFHEADER), ft->fp) != sizeof(SFHEADER))
+	if (fread(&sfhead, 1, sizeof(sfhead), ft->fp) != sizeof(sfhead))
 	{
 		fail("unexpected EOF in SF header");
 		return(ST_EOF);
@@ -187,7 +187,7 @@
 	sfcharp = (char *) sfcodep + sizeof(SFCODE);
 	while(sfcharp < (char *) &sfhead + SIZEOF_BSD_HEADER)
 		*sfcharp++ = '\0';
-	(void) fwrite(&sfhead, 1, sizeof(SFHEADER), ft->fp);
+	fwrite(&sfhead, 1, sizeof(SFHEADER), ft->fp);
 
 	return(ST_SUCCESS);
 }
--- a/src/smp.c
+++ b/src/smp.c
@@ -90,7 +90,7 @@
 		ft->loops[8].count = trailer->loops[8].count;
 	}
 	for(i = 0; i < 8; i++) {	/* read the 8 markers */
-		if (fread(trailer->markers[i].name, 1, 10, ft->fp) != 10)
+	        if (fread(trailer->markers[i].name, 1, 10, ft->fp) != 10)
 		{
 		    fail("EOF in SMP");
 		    return(ST_EOF);
@@ -158,7 +158,7 @@
 		st_writew(ft, trailer->loops[i].count);
 	}
 	for(i = 0; i < 8; i++) {	/* write the 8 markers */
-		if (fwrite(trailer->markers[i].name, 1, 10, ft->fp) != 10)
+		if (st_writes(ft, trailer->markers[i].name) == ST_EOF)
 		{
 		    fail("EOF in SMP");
 	 	    return(ST_EOF);
--- a/src/sndrtool.c
+++ b/src/sndrtool.c
@@ -65,7 +65,7 @@
          and second word is between 4000 & 25000 then this is sounder sound */
         /* otherwise, its probably raw, not handled here */
 
-	if (fread(buf, 1, 2, ft->fp) != 2)
+	if (fread(buf, 1, 2, buf) != 2)
 	{
 		fail("SND: unexpected EOF");
 		return(ST_EOF);
@@ -84,7 +84,7 @@
 	else
 	{
 	/* sndtool ? */
-	fread(&buf[2],1,6,ft->fp);
+	fread(&buf[2], 1, 6, ft->fp);
 	if (strncmp(buf,"SOUND",5))
 	{
 		fail ("SND: unrecognized SND format");
@@ -93,7 +93,7 @@
 	fseek(ft->fp,12,SEEK_CUR);
 	st_readw(ft, &rate);
 	fseek(ft->fp,6,SEEK_CUR);
-	if (fread(buf,1,96,ft->fp) != 96)
+	if (st_reads(ft, buf, 96) == ST_EOF)
 	{
 		fail ("SND: unexpected EOF in SND header");
 		return(ST_EOF);
--- a/src/st.h
+++ b/src/st.h
@@ -290,7 +290,8 @@
  * possible byte swapping.
  */
 /* declared in misc.c */
-int	st_writes(P2(ft_t ft, char *uc));
+int	st_reads(P3(ft_t ft, char *c, int len));
+int	st_writes(P2(ft_t ft, char *c));
 int	st_readb(P2(ft_t ft, unsigned char *uc));
 int	st_writeb(P2(ft_t ft, unsigned char uc));
 int	st_readw(P2(ft_t ft, unsigned short *us));
--- a/src/testall.sh
+++ b/src/testall.sh
@@ -15,7 +15,6 @@
 t cdr
 t cvs
 t dat
-t vms
 t hcom -r 22050
 t maud
 t raw -r 8130 -t ub
@@ -24,6 +23,7 @@
 t sndr
 t sndt 
 t txw
+t vms
 t voc
 t wav
 t wve
--- a/src/voc.c
+++ b/src/voc.c
@@ -326,7 +326,7 @@
 	v->samples = 0;
 
 	/* File format name and a ^Z (aborts printing under DOS) */
-	(void) fwrite("Creative Voice File\032\032", 1, 20, ft->fp);
+	st_writes(ft, "Creative Voice File\032");
 	st_writew(ft, 26);			/* size of header */
 	st_writew(ft, 0x10a);              /* major/minor version number */
 	st_writew(ft, 0x1129);		/* checksum of version number */
--- a/src/wav.c
+++ b/src/wav.c
@@ -411,11 +411,11 @@
 
 static ULONG findChunk(ft_t ft, const char *Label)
 {
-    char magic[4];
+    char magic[5];
     ULONG len;
     for (;;)
     {
-	if (fread(magic, 1, 4, ft->fp) != 4)
+	if (st_reads(ft, magic, 4) == ST_EOF)
 	{
 	    fail("WAVE file has missing %s chunk", Label);
 	    return 0;
@@ -440,7 +440,7 @@
 ft_t ft;
 {
     wav_t	wav = (wav_t) ft->priv;
-    char	magic[4];
+    char	magic[5];
     ULONG	len;
     int		littlendian = 1;
     char	*endptr;
@@ -467,7 +467,7 @@
     endptr = (char *) &littlendian;
     if (!*endptr) ft->swap = ft->swap ? 0 : 1;
 
-    if ( fread(magic, 1, 4, ft->fp) != 4 || strncmp("RIFF", magic, 4))
+    if (st_reads(ft, magic, 4) == ST_EOF || strncmp("RIFF", magic, 4))
     {
 	fail("WAVE: RIFF header not found");
 	return ST_EOF;
@@ -475,7 +475,7 @@
 
     st_readdw(ft, &wRiffLength);
 
-    if ( fread(magic, 1, 4, ft->fp) != 4 || strncmp("WAVE", magic, 4))
+    if (st_reads(ft, magic, 4) == ST_EOF || strncmp("WAVE", magic, 4))
     {
 	fail("WAVE header not found");
 	return ST_EOF;
@@ -1420,7 +1420,7 @@
 ft_t ft;
 {
 	wav_t	wav = (wav_t) ft->priv;
-	int	rc;
+	int	rc = ST_SUCCESS;
 
 	/* Call this to flush out any remaining data. */
 	switch (wav->formatTag)
--- a/src/wve.c
+++ b/src/wve.c
@@ -5,6 +5,7 @@
 
 #include "st.h"
 #include "g72x.h"
+#include "string.h"
 
 /* Magic numbers used in Psion audio files */
 #define PSION_MAGIC     "ALawSoundFile**"
@@ -48,9 +49,9 @@
 		ft->swap = ft->swap ? 0 : 1;
 	}
 
-	/* Check the magic word */
-        fread(magic, 16, 1, ft->fp);
-	if (strcmp(magic, PSION_MAGIC)==0) {
+	/* Check the magic word (null-terminated) */
+        st_reads(ft, magic, 16);
+	if (strncmp(magic, PSION_MAGIC, 15)==0) {
 		report("Found Psion magic word");
 	}
 	else
@@ -193,7 +194,9 @@
     version=PSION_VERSION;
     zero=0;
 
-    fwrite(magic, sizeof(magic), 1, ft->fp);
+    st_writes(ft, magic);
+    /* Null terminate string */
+    st_writeb(ft, 0);
 
     st_writew(ft, version);
     st_writedw(ft, p->length);