shithub: sox

Download patch

ref: 5d838971a2a9572bbb4b59ffc3879668164d8f02
parent: 276efc185bdecde3b44164c8a3dfab1ba11b1aa3
author: cbagwell <cbagwell>
date: Tue Jan 4 19:02:59 EST 2000

First pass at abstracting I/O calls.  Still lots more to go though.

--- a/src/8svx.c
+++ b/src/8svx.c
@@ -37,8 +37,8 @@
 	ULONG totalsize;
 	ULONG chunksize;
 
-	int channels;
-	LONG rate;
+	ULONG channels;
+	unsigned short rate;
 	int i;
 	int littlendian = 1;
 	char *endptr;
@@ -63,7 +63,7 @@
 		fail("8SVX: header does not begin with magic word 'FORM'");
 		return(ST_EOF);
 	}
-	totalsize = rlong(ft);
+	st_readdw(ft, &totalsize);
 	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "8SVX", 4) != 0)
 	{
 		fail("8SVX: 'FORM' chunk does not specify '8SVX' as type");
@@ -73,7 +73,7 @@
 	/* read chunks until 'BODY' (or end) */
 	while (fread(buf,1,4,ft->fp) == 4 && strncmp(buf,"BODY",4) != 0) {
 		if (strncmp(buf,"VHDR",4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize != 20)
 			{
 				fail ("8SVX: VHDR chunk has bad size");
@@ -80,7 +80,7 @@
 				return(ST_EOF);
 			}
 			fseek(ft->fp,12,SEEK_CUR);
-			rate = rshort(ft);
+			st_readw(ft, &rate);
 			fseek(ft->fp,1,SEEK_CUR);
 			fread(buf,1,1,ft->fp);
 			if (buf[0] != 0)
@@ -93,7 +93,7 @@
 		}
 
 		if (strncmp(buf,"ANNO",4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize & 1)
 				chunksize++;
 			chunk_buf = (char *) malloc(chunksize + 2);
@@ -116,7 +116,7 @@
 		}
 
 		if (strncmp(buf,"NAME",4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize & 1)
 				chunksize++;
 			chunk_buf = (char *) malloc(chunksize + 1);
@@ -139,13 +139,13 @@
 		}
 
 		if (strncmp(buf,"CHAN",4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize != 4) 
 			{
 				fail("8SVX: Short channel chunk");
 				return(ST_EOF);
 			}
-			channels = rlong(ft);
+			st_readdw(ft, &channels);
 			channels = (channels & 0x01) + 
 					((channels & 0x02) >> 1) +
 				   	((channels & 0x04) >> 2) + 
@@ -155,7 +155,7 @@
 		}
 
 		/* some other kind of chunk */
-		chunksize = rlong(ft);
+		st_readdw(ft, &chunksize);
 		if (chunksize & 1)
 			chunksize++;
 		fseek(ft->fp,chunksize,SEEK_CUR);
@@ -173,7 +173,7 @@
 		fail ("8SVX: BODY chunk not found");
 		return(ST_EOF);
 	}
-	p->nsamples = rlong(ft);
+	st_readdw(ft, &(p->nsamples));
 
 	ft->info.channels = channels;
 	ft->info.rate = rate;
@@ -214,7 +214,7 @@
 ft_t ft;
 LONG *buf, nsamp;
 {
-	ULONG datum;
+	unsigned char datum;
 	int done = 0;
 	int i;
 
@@ -222,7 +222,8 @@
 
 	while (done < nsamp) {
 		for (i = 0; i < ft->info.channels; i++) {
-			datum = getc(p->ch[i]);
+		    	/* FIXME: don't pass FILE pointers! */
+		    	datum = getc(p->ch[i]);
 			if (feof(p->ch[i]))
 				return done;
 			/* scale signed up to long's range */
@@ -301,7 +302,7 @@
 {
 	struct svxpriv *p = (struct svxpriv *) ft->priv;
 
-	LONG datum;
+	unsigned char datum;
 	int done = 0;
 	int i;
 
@@ -310,7 +311,8 @@
 	while(done < len) {
 		for (i = 0; i < ft->info.channels; i++) {
 			datum = RIGHT(*buf++, 24);
-			putc((int)datum, p->ch[i]);
+			/* FIXME: Needs to pass ft struct and not FILE */
+			putc(datum, p->ch[i]);
 		}
 		done += ft->info.channels;
 	}
@@ -347,7 +349,7 @@
 
 	/* add a pad byte if BODY size is odd */
 	if(p->nsamples % 2 != 0)
-		fputc('\0', ft->fp);
+	    st_writeb(ft, '\0');
 
 	/* fixup file sizes in header */
 	if (fseek(ft->fp, 0L, 0) != 0)
@@ -372,29 +374,29 @@
 	/* FORM size must be even */
 	if(formsize % 2 != 0) formsize++;
 
-	fputs ("FORM", ft->fp);
-	wlong(ft, formsize);  /* size of file */
-	fputs("8SVX", ft->fp); /* File type */
+	st_writes(ft, "FORM");
+	st_writedw(ft, formsize);  /* size of file */
+	st_writes(ft, "8SVX"); /* File type */
 
-	fputs ("VHDR", ft->fp);
-	wlong(ft, (LONG) 20); /* number of bytes to follow */
-	wlong(ft, nsamples);  /* samples, 1-shot */
-	wlong(ft, (LONG) 0);  /* samples, repeat */
-	wlong(ft, (LONG) 0);  /* samples per repeat cycle */
-	wshort(ft, (int) ft->info.rate); /* samples per second */
-	fputc(1,ft->fp); /* number of octaves */
-	fputc(0,ft->fp); /* data compression (none) */
-	wshort(ft,1); wshort(ft,0); /* volume */
+	st_writes(ft, "VHDR");
+	st_writedw(ft, (LONG) 20); /* number of bytes to follow */
+	st_writedw(ft, nsamples);  /* samples, 1-shot */
+	st_writedw(ft, (LONG) 0);  /* samples, repeat */
+	st_writedw(ft, (LONG) 0);  /* samples per repeat cycle */
+	st_writew(ft, (int) ft->info.rate); /* samples per second */
+	st_writeb(ft,1); /* number of octabes */
+	st_writeb(ft,0); /* data compression (none) */
+	st_writew(ft,1); st_writew(ft,0); /* volume */
 
-	fputs ("ANNO", ft->fp);
-	wlong(ft, (LONG) 32); /* length of block */
-	fputs ("File created by Sound Exchange  ", ft->fp);
+	st_writes(ft, "ANNO");
+	st_writedw(ft, (LONG) 32); /* length of block */
+	st_writes(ft, "File created by Sound Exchange  ");
 
-	fputs ("CHAN", ft->fp);
-	wlong(ft, (LONG) 4);
-	wlong(ft, (ft->info.channels == 2) ? (LONG) 6 :
+	st_writes(ft, "CHAN");
+	st_writedw(ft, (LONG) 4);
+	st_writedw(ft, (ft->info.channels == 2) ? (LONG) 6 :
 		   (ft->info.channels == 4) ? (LONG) 15 : (LONG) 2);
 
-	fputs ("BODY", ft->fp);
-	wlong(ft, nsamples); /* samples in file */
+	st_writes(ft, "BODY");
+	st_writedw(ft, nsamples); /* samples in file */
 }
--- a/src/aiff.c
+++ b/src/aiff.c
@@ -69,9 +69,9 @@
 	char buf[5];
 	ULONG totalsize;
 	LONG chunksize;
-	int channels = 0;
+	unsigned short channels = 0;
 	ULONG frames;
-	int bits = 0;
+	unsigned short bits = 0;
 	double rate = 0.0;
 	ULONG offset = 0;
 	ULONG blocksize = 0;
@@ -79,18 +79,22 @@
 	char *endptr;
 	int foundcomm = 0, foundmark = 0, foundinstr = 0;
 	struct mark {
-		int id, position;
+		unsigned short id;
+		ULONG position;
 		char name[40]; 
 	} marks[32];
+	unsigned short looptype;
 	int i, j;
-	LONG nmarks = 0;
-	LONG sustainLoopBegin = 0, sustainLoopEnd = 0,
-	     releaseLoopBegin = 0, releaseLoopEnd = 0;
+	unsigned short nmarks = 0;
+	unsigned short sustainLoopBegin = 0, sustainLoopEnd = 0,
+	     	       releaseLoopBegin = 0, releaseLoopEnd = 0;
 	LONG seekto = 0L, ssndsize = 0L;
 	char *author;
 	char *copyright;
 	char *nametext;
 
+	ULONG trash;
+
 	int rc;
 
 	/* Needed because of st_rawread() */
@@ -112,7 +116,7 @@
 		fail("AIFF header does not begin with magic word 'FORM'");
 		return(ST_EOF);
 	}
-	totalsize = rlong(ft);
+	st_readdw(ft, &totalsize);
 	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "AIFF", 4) != 0)
 	{
 		fail("AIFF 'FORM' chunk does not specify 'AIFF' as type");
@@ -135,23 +139,23 @@
 		}
 		if (strncmp(buf, "COMM", 4) == 0) {
 			/* COMM chunk */
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize != 18)
 			{
 				fail("AIFF COMM chunk has bad size");
 				return(ST_EOF);
 			}
-			channels = rshort(ft);
-			frames = rlong(ft);
-			bits = rshort(ft);
+			st_readw(ft, &channels);
+			st_readdw(ft, &frames);
+			st_readw(ft, &bits);
 			rate = read_ieee_extended(ft);
 			foundcomm = 1;
 		}
 		else if (strncmp(buf, "SSND", 4) == 0) {
 			/* SSND chunk */
-			chunksize = rlong(ft);
-			offset = rlong(ft);
-			blocksize = rlong(ft);
+			st_readdw(ft, &chunksize);
+			st_readdw(ft, &offset);
+			st_readdw(ft, &blocksize);
 			chunksize -= 8;
 			ssndsize = chunksize;
 			/* if can't seek, just do sound now */
@@ -163,8 +167,8 @@
 		}
 		else if (strncmp(buf, "MARK", 4) == 0) {
 			/* MARK chunk */
-			chunksize = rlong(ft);
-			nmarks = rshort(ft);
+			st_readdw(ft, &chunksize);
+			st_readw(ft, &nmarks);
 
 			/* Some programs like to always have a MARK chunk
 			 * but will set number of marks to 0 and force
@@ -177,19 +181,19 @@
 
 			chunksize -= 2;
 			for(i = 0; i < nmarks; i++) {
-				int len;
+				unsigned char len;
 
-				marks[i].id = rshort(ft);
-				marks[i].position = rlong(ft);
+				st_readw(ft, &(marks[i].id));
+				st_readdw(ft, &(marks[i].position));
 				chunksize -= 6;
-				len = getc(ft->fp);
+				st_readb(ft, &len);
 				chunksize -= len + 1;
 				for(j = 0; j < len ; j++) 
-					marks[i].name[j] = getc(ft->fp);
+				    st_readb(ft, &(marks[i].name[j]));
 				marks[i].name[j] = 0;
 				if ((len & 1) == 0) {
 					chunksize--;
-					getc(ft->fp);
+					st_readb(ft, (unsigned char *)&trash);
 				}
 			}
 			/* HA HA!  Sound Designer (and others) makes */
@@ -196,24 +200,28 @@
 			/* bogus files. It spits out bogus chunksize */
 			/* for MARK field */
 			while(chunksize-- > 0)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 		}
 		else if (strncmp(buf, "INST", 4) == 0) {
 			/* INST chunk */
-			chunksize = rlong(ft);
-			ft->instr.MIDInote = getc(ft->fp);
-			getc(ft->fp);				/* detune */
-			ft->instr.MIDIlow = getc(ft->fp);
-			ft->instr.MIDIhi = getc(ft->fp);
-			getc(ft->fp);			/* low velocity */
-			getc(ft->fp);			/* hi  velocity */
-			rshort(ft);				/* gain */
-			ft->loops[0].type = rshort(ft); /* sustain loop */
-			sustainLoopBegin = rshort(ft);	 /* begin marker */
-			sustainLoopEnd = rshort(ft);    /* end marker */
-			ft->loops[1].type = rshort(ft); /* release loop */
-			releaseLoopBegin = rshort(ft);  /* begin marker */
-			releaseLoopEnd = rshort(ft);    /* end marker */
+			st_readdw(ft, &chunksize);
+			st_readb(ft, &(ft->instr.MIDInote));
+			st_readb(ft, (unsigned char *)&trash);
+			st_readb(ft, &(ft->instr.MIDIlow));
+			st_readb(ft, &(ft->instr.MIDIhi));
+			/* Low  velocity */
+			st_readb(ft, (unsigned char *)&trash);
+			/* Hi  velocity */
+			st_readb(ft, (unsigned char *)&trash);
+			st_readw(ft, (unsigned short *)&trash);	/* gain */
+			st_readw(ft, &looptype); /* sustain loop */
+			ft->loops[0].type = looptype;
+			st_readw(ft, &sustainLoopBegin); /* begin marker */
+			st_readw(ft, &sustainLoopEnd);    /* end marker */
+			st_readw(ft, &looptype); /* release loop */
+			ft->loops[1].type = looptype;
+			st_readw(ft, &releaseLoopBegin);  /* begin marker */
+			st_readw(ft, &releaseLoopEnd);    /* end marker */
 
 			/* At least one known program generates an INST */
 			/* block with everything zeroed out (meaning    */
@@ -225,21 +233,21 @@
 				foundinstr = 1;
 		}
 		else if (strncmp(buf, "APPL", 4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			while(chunksize-- > 0)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 		}
 		else if (strncmp(buf, "ALCH", 4) == 0) {
 			/* I think this is bogus and gets grabbed by APPL */
 			/* INST chunk */
-			rlong(ft);		/* ENVS - jeez! */
-			chunksize = rlong(ft);
+			st_readdw(ft, &trash);		/* ENVS - jeez! */
+			st_readdw(ft, &chunksize);
 			while(chunksize-- > 0)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 		}
 		else if (strncmp(buf, "ANNO", 4) == 0) {
 			/* Old form of comment chunk */
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			/* allocate enough memory to hold the comment */
 			ft->comment = (char *) malloc((size_t) chunksize);
 			if (ft->comment == NULL)
@@ -294,13 +302,13 @@
 			if (feof(ft->fp))
 				break;
 			report("AIFFstartread: ignoring '%s' chunk\n", buf);
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (feof(ft->fp))
 				break;
-			/* Skip the chunk using getc() so we may read
+			/* Skip the chunk using st_readb() so we may read
 			   from a pipe */
 			while (chunksize-- > 0) {
-				if (getc(ft->fp) == EOF)
+			    if (st_readb(ft, (unsigned char *)&trash) == ST_EOF)
 					break;
 			}
 		}
@@ -329,7 +337,7 @@
 		return(ST_EOF);
 	}
 	while ((LONG) (--offset) >= 0) {
-		if (getc(ft->fp) == EOF)
+		if (st_readb(ft, (unsigned char *)&trash) == ST_EOF)
 		{
 			fail("unexpected EOF while skipping AIFF offset");
 			return(ST_EOF);
@@ -453,7 +461,8 @@
 char *chunkDescription;
 ft_t ft;
 {
-  LONG chunksize = rlong(ft);
+  LONG chunksize;
+  st_readdw(ft, &chunksize);
   /* allocate enough memory to hold the text including a terminating \0 */
   *text = (char *) malloc((size_t) chunksize + 1);
   if (*text == NULL)
@@ -492,6 +501,7 @@
 {
 	char buf[5];
 	ULONG chunksize;
+	ULONG trash;
 
 	if (!ft->seekable)
 	{
@@ -500,7 +510,7 @@
 		if (fread(buf, 1, 4, ft->fp) != 4)
 			break;
 
-		chunksize = rlong(ft);
+		st_readdw(ft, &chunksize);
 		if (feof(ft->fp))
 			break;
 		buf[4] = '\0';
@@ -510,7 +520,7 @@
 			warn("	You're stripping MIDI/loop info!\n");
 		while ((LONG) (--chunksize) >= 0) 
 		{
-			if (getc(ft->fp) == EOF)
+			if (st_readb(ft, (unsigned char *)&trash) == ST_EOF)
 				break;
 		}
 	    }
@@ -632,9 +642,9 @@
 		return(ST_EOF);
 	}
 
-	fputs("FORM", ft->fp); /* IFF header */
-	wlong(ft, hsize + nframes * ft->info.size * ft->info.channels); /* file size */
-	fputs("AIFF", ft->fp); /* File type */
+	st_writes(ft, "FORM"); /* IFF header */
+	st_writedw(ft, hsize + nframes * ft->info.size * ft->info.channels); /* file size */
+	st_writes(ft, "AIFF"); /* File type */
 
 	/* ANNO chunk -- holds comments text, however this is */
 	/* discouraged by Apple in preference to a COMT comments */
@@ -641,77 +651,77 @@
 	/* chunk, which holds a timestamp and marker id */
 	if (ft->comment)
 	{
-	  fputs("ANNO", ft->fp);
+	  st_writes(ft, "ANNO");
 	  /* Must put an even number of characters out.  True 68k processors
 	   * OS's seem to require this 
 	   */
 	  comment_size = strlen(ft->comment);
-	  wlong(ft, (LONG)(((comment_size % 2) == 0) ? comment_size : comment_size + 1)); /* ANNO chunk size, the No of chars */
-	  fputs(ft->comment, ft->fp);
+	  st_writedw(ft, (LONG)(((comment_size % 2) == 0) ? comment_size : comment_size + 1)); /* ANNO chunk size, the No of chars */
+	  st_writes(ft, ft->comment);
 	  if (comment_size % 2 == 1)
-		fputs(" ", ft->fp);
+		st_writes(ft, " ");
 	}
 
 	/* COMM chunk -- describes encoding (and #frames) */
-	fputs("COMM", ft->fp);
-	wlong(ft, (LONG) 18); /* COMM chunk size */
-	wshort(ft, ft->info.channels); /* nchannels */
-	wlong(ft, nframes); /* number of frames */
-	wshort(ft, bits); /* sample width, in bits */
+	st_writes(ft, "COMM");
+	st_writedw(ft, (LONG) 18); /* COMM chunk size */
+	st_writew(ft, ft->info.channels); /* nchannels */
+	st_writedw(ft, nframes); /* number of frames */
+	st_writew(ft, bits); /* sample width, in bits */
 	write_ieee_extended(ft, (double)ft->info.rate);
 
 	/* MARK chunk -- set markers */
 	if (ft->instr.nloops) {
-		fputs("MARK", ft->fp);
+		st_writes(ft, "MARK");
 		if (ft->instr.nloops > 2)
 			ft->instr.nloops = 2;
-		wlong(ft, 2 + 16*ft->instr.nloops);
-		wshort(ft, ft->instr.nloops);
+		st_writedw(ft, 2 + 16*ft->instr.nloops);
+		st_writew(ft, ft->instr.nloops);
 
 		for(i = 0; i < ft->instr.nloops; i++) {
-			wshort(ft, i + 1);
-			wlong(ft, ft->loops[i].start);
-			fputc(0, ft->fp);
-			fputc(0, ft->fp);
-			wshort(ft, i*2 + 1);
-			wlong(ft, ft->loops[i].start + ft->loops[i].length);
-			fputc(0, ft->fp);
-			fputc(0, ft->fp);
+			st_writew(ft, i + 1);
+			st_writedw(ft, ft->loops[i].start);
+			st_writeb(ft, 0);
+			st_writeb(ft, 0);
+			st_writew(ft, i*2 + 1);
+			st_writedw(ft, ft->loops[i].start + ft->loops[i].length);
+			st_writeb(ft, 0);
+			st_writeb(ft, 0);
 			}
 
-		fputs("INST", ft->fp);
-		wlong(ft, 20);
+		st_writes(ft, "INST");
+		st_writedw(ft, 20);
 		/* random MIDI shit that we default on */
-		fputc(ft->instr.MIDInote, ft->fp);
-		fputc(0, ft->fp);			/* detune */
-		fputc(ft->instr.MIDIlow, ft->fp);
-		fputc(ft->instr.MIDIhi, ft->fp);
-		fputc(1, ft->fp);			/* low velocity */
-		fputc(127, ft->fp);			/* hi  velocity */
-		wshort(ft, 0);				/* gain */
+		st_writeb(ft, ft->instr.MIDInote);
+		st_writeb(ft, 0);			/* detune */
+		st_writeb(ft, ft->instr.MIDIlow);
+		st_writeb(ft, ft->instr.MIDIhi);
+		st_writeb(ft, 1);			/* low velocity */
+		st_writeb(ft, 127);			/* hi  velocity */
+		st_writew(ft, 0);				/* gain */
 
 		/* sustain loop */
-		wshort(ft, ft->loops[0].type);
-		wshort(ft, 1);				/* marker 1 */
-		wshort(ft, 3);				/* marker 3 */
+		st_writew(ft, ft->loops[0].type);
+		st_writew(ft, 1);				/* marker 1 */
+		st_writew(ft, 3);				/* marker 3 */
 		/* release loop, if there */
 		if (ft->instr.nloops == 2) {
-			wshort(ft, ft->loops[1].type);
-			wshort(ft, 2);			/* marker 2 */
-			wshort(ft, 4);			/* marker 4 */
+			st_writew(ft, ft->loops[1].type);
+			st_writew(ft, 2);			/* marker 2 */
+			st_writew(ft, 4);			/* marker 4 */
 		} else {
-			wshort(ft, 0);			/* no release loop */
-			wshort(ft, 0);
-			wshort(ft, 0);
+			st_writew(ft, 0);			/* no release loop */
+			st_writew(ft, 0);
+			st_writew(ft, 0);
 		}
 	}
 
 	/* SSND chunk -- describes data */
-	fputs("SSND", ft->fp);
+	st_writes(ft, "SSND");
 	/* chunk size */
-	wlong(ft, 8 + nframes * ft->info.channels * ft->info.size); 
-	wlong(ft, (LONG) 0); /* offset */
-	wlong(ft, (LONG) 0); /* block size */
+	st_writedw(ft, 8 + nframes * ft->info.channels * ft->info.size); 
+	st_writedw(ft, (LONG) 0); /* offset */
+	st_writedw(ft, (LONG) 0); /* block size */
 	return(ST_SUCCESS);
 }
 
--- a/src/au.c
+++ b/src/au.c
@@ -95,7 +95,7 @@
 	}
 
 	/* Check the magic word */
-	magic = rlong(ft);
+	st_readdw(ft, &magic);
 	if (magic == DEC_INV_MAGIC) {
 		/* Inverted headers are not standard.  Code was probably
 		 * left over from pre-standardize period of testing for
@@ -121,7 +121,7 @@
 	}
 
 	/* Read the header size */
-	hdr_size = rlong(ft);
+	st_readdw(ft, &hdr_size);
 	if (hdr_size < SUN_HDRSIZE)
 	{
 		fail("Sun/NeXT header size too small.");
@@ -129,10 +129,10 @@
 	}
 
 	/* Read the data size; may be ~0 meaning unspecified */
-	data_size = rlong(ft);
+	st_readdw(ft, &data_size);
 
 	/* Read the encoding; there are some more possibilities */
-	encoding = rlong(ft);
+	st_readdw(ft, &encoding);
 
 
 	/* Translate the encoding into style and size parameters */
@@ -184,11 +184,11 @@
 	}
 
 	/* Read the sampling rate */
-	sample_rate = rlong(ft);
+	st_readdw(ft, &sample_rate);
 	ft->info.rate = sample_rate;
 
 	/* Read the number of channels */
-	channels = rlong(ft);
+	st_readdw(ft, &channels);
 	ft->info.channels = (int) channels;
 
 	/* Skip the info string in header; print it if verbose */
@@ -196,7 +196,7 @@
 	if (hdr_size > 0) {
 		buf = (char *) malloc(hdr_size + 1);
 		for(i = 0; i < hdr_size; i++) {
-			buf[i] = (char) getc(ft->fp);
+		    	st_readb(ft, &(buf[i]));
 			if (feof(ft->fp))
 			{
 				fail("Unexpected EOF in Sun/NeXT header info.");
@@ -361,23 +361,23 @@
 	}
 
 	magic = SUN_MAGIC;
-	wlong(ft, magic);
+	st_writedw(ft, magic);
 
 	if (ft->comment == NULL)
 		ft->comment = "";
 	hdr_size = SUN_HDRSIZE + strlen(ft->comment);
-	wlong(ft, hdr_size);
+	st_writedw(ft, hdr_size);
 
-	wlong(ft, data_size);
+	st_writedw(ft, data_size);
 
-	wlong(ft, encoding);
+	st_writedw(ft, encoding);
 
 	sample_rate = ft->info.rate;
-	wlong(ft, sample_rate);
+	st_writedw(ft, sample_rate);
 
 	channels = ft->info.channels;
-	wlong(ft, channels);
+	st_writedw(ft, channels);
 
-	fputs(ft->comment, ft->fp);
+	st_writes(ft, ft->comment);
 }
 
--- a/src/avr.c
+++ b/src/avr.c
@@ -92,7 +92,7 @@
 
   fread (avr->name, 1, sizeof (avr->name), ft->fp);
 
-  avr->mono = rshort (ft);
+  st_readw (ft, &(avr->mono));
   if (avr->mono) {
     ft->info.channels = 2;
   }
@@ -100,7 +100,7 @@
     ft->info.channels = 1;
   }
 
-  avr->rez = rshort (ft);
+  st_readw (ft, &(avr->rez));
   if (avr->rez == 8) {
     ft->info.size = ST_SIZE_BYTE;
   }
@@ -112,7 +112,7 @@
     return(0);
   }
 
-  avr->sign = rshort (ft);
+  st_readw (ft, &(avr->sign));
   if (avr->sign) {
     ft->info.style = ST_ENCODING_SIGN2;
   }
@@ -120,11 +120,11 @@
     ft->info.style = ST_ENCODING_UNSIGNED;
   }
 
-  avr->loop = rshort (ft);
+  st_readw (ft, &(avr->loop));
 
-  avr->midi = rshort (ft);
+  st_readw (ft, &(avr->midi));
 
-  avr->rate = rlong (ft);
+  st_readdw (ft, &(avr->rate));
   /*
    * No support for AVRs created by ST-Replay,
    * Replay Proffesional and PRO-Series 12.
@@ -133,17 +133,17 @@
    */
   ft->info.rate = (avr->rate & 0x00ffffff);
 
-  avr->size = rlong (ft);
+  st_readdw (ft, &(avr->size));
 
-  avr->lbeg = rlong (ft);
+  st_readdw (ft, &(avr->lbeg));
 
-  avr->lend = rlong (ft);
+  st_readdw (ft, &(avr->lend));
 
-  avr->res1 = rshort (ft);
+  st_readw (ft, &(avr->res1));
 
-  avr->res2 = rshort (ft);
+  st_readw (ft, &(avr->res2));
 
-  avr->res3 = rshort (ft);
+  st_readw (ft, &(avr->res3));
 
   fread (avr->ext, 1, sizeof (avr->ext), ft->fp);
 
@@ -185,10 +185,10 @@
 
   /* mono */
   if (ft->info.channels == 1) {
-    wshort (ft, 0);
+    st_writew (ft, 0);
   }
   else if (ft->info.channels == 2) {
-    wshort (ft, 0xffff);
+    st_writew (ft, 0xffff);
   }
   else {
     fail ("AVR: number of channels not supported");
@@ -197,10 +197,10 @@
 
   /* rez */
   if (ft->info.size == ST_SIZE_BYTE) {
-    wshort (ft, 8);
+    st_writew (ft, 8);
   }
   else if (ft->info.size == ST_SIZE_WORD) {
-    wshort (ft, 16);
+    st_writew (ft, 16);
   }
   else {
     fail ("AVR: unsupported sample resolution");
@@ -209,10 +209,10 @@
 
   /* sign */
   if (ft->info.style == ST_ENCODING_SIGN2) {
-    wshort (ft, 0xffff);
+    st_writew (ft, 0xffff);
   }
   else if (ft->info.style == ST_ENCODING_UNSIGNED) {
-    wshort (ft, 0);
+    st_writew (ft, 0);
   }
   else {
     fail ("AVR: unsupported style");
@@ -220,33 +220,33 @@
   }
 
   /* loop */
-  wshort (ft, 0xffff);
+  st_writew (ft, 0xffff);
 
   /* midi */
-  wshort (ft, 0xffff);
+  st_writew (ft, 0xffff);
 
   /* rate */
-  wlong (ft, ft->info.rate);
+  st_writedw (ft, ft->info.rate);
 
   /* size */
   /* Don't know the size yet. */
-  wlong (ft, 0);
+  st_writedw (ft, 0);
 
   /* lbeg */
-  wlong (ft, 0);
+  st_writedw (ft, 0);
 
   /* lend */
   /* Don't know the size yet, so we can't set lend, either. */
-  wlong (ft, 0);
+  st_writedw (ft, 0);
 
   /* res1 */
-  wshort (ft, 0);
+  st_writew (ft, 0);
 
   /* res2 */
-  wshort (ft, 0);
+  st_writew (ft, 0);
 
   /* res3 */
-  wshort (ft, 0);
+  st_writew (ft, 0);
 
   /* ext */
   fwrite ("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 1, sizeof (avr->ext),
@@ -288,11 +288,11 @@
 
   /* Fix size */
   fseek (ft->fp, 26L, SEEK_SET);
-  wlong (ft, size);
+  st_writedw (ft, size);
 
   /* Fix lend */
   fseek (ft->fp, 34L, SEEK_SET);
-  wlong (ft, size);
+  st_writedw (ft, size);
 
   return(ST_SUCCESS);
 }
--- a/src/cdr.c
+++ b/src/cdr.c
@@ -162,7 +162,7 @@
 	if (padsamps != SECTORSIZE) 
 	{
 		while (padsamps > 0) {
-			wshort(ft, zero);
+			st_writew(ft, zero);
 			padsamps--;
 		}
 	}
--- a/src/cvsd.c
+++ b/src/cvsd.c
@@ -84,7 +84,7 @@
 		struct cvsd_encode_state enc;
 	} c;
 	struct {
-		unsigned shreg;
+		unsigned char shreg;
 		unsigned mask;
 		unsigned cnt;
 	} bit;
@@ -217,7 +217,7 @@
 	struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
 
 	if (p->bit.cnt) {
-		putc(p->bit.shreg, ft->fp);
+		st_writeb(ft, p->bit.shreg);
 		p->bytes_written++;
 	}
 	report("cvsd: min slope %f, max slope %f\n", 
@@ -279,8 +279,7 @@
 #endif
 	while (done < nsamp) {
 		if (!p->bit.cnt) {
-			p->bit.shreg = getc(ft->fp);
-			if (feof(ft->fp))
+		    	if (st_readb(ft, &(p->bit.shreg)) == ST_EOF)
 				return done;
 			p->bit.cnt = 8;
 			p->bit.mask = p->swapbits ? 0x80 : 1;
@@ -398,7 +397,7 @@
 		} else
 			p->c.enc.recon_int -= p->com.mla_int;
 		if ((++(p->bit.cnt)) >= 8) {
-			putc(p->bit.shreg, ft->fp);
+		        st_writeb(ft, p->bit.shreg);
 			p->bytes_written++;
 			p->bit.shreg = p->bit.cnt = 0;
 			p->bit.mask = p->swapbits ? 0x80 : 1;
--- a/src/hcom.c
+++ b/src/hcom.c
@@ -91,8 +91,8 @@
 	    return rc;
 
 	/* Get essential numbers from the header */
-	datasize = rlong(ft); /* bytes 83-86 */
-	rsrcsize = rlong(ft); /* bytes 87-90 */
+	st_readdw(ft, &datasize); /* bytes 83-86 */
+	st_readdw(ft, &rsrcsize); /* bytes 87-90 */
 
 	/* Skip the rest of the header (total 128 bytes) */
 	rc = skipbytes(ft, 128-91);
@@ -107,21 +107,21 @@
 	}
 
 	/* Then follow various parameters */
-	huffcount = rlong(ft);
-	checksum = rlong(ft);
-	compresstype = rlong(ft);
+	st_readdw(ft, &huffcount);
+	st_readdw(ft, &checksum);
+	st_readdw(ft, &compresstype);
 	if (compresstype > 1)
 	{
 		fail("Bad compression type in HCOM header");
 		return (ST_EOF);
 	}
-	divisor = rlong(ft);
+	st_readdw(ft, &divisor);
 	if (divisor == 0 || divisor > 4)
 	{
 		fail("Bad sampling rate divisor in HCOM header");
 		return (ST_EOF);
 	}
-	dictsize = rshort(ft);
+	st_readw(ft, &dictsize);
 
 	/* Translate to sox parameters */
 	ft->info.style = ST_ENCODING_UNSIGNED;
@@ -139,8 +139,8 @@
 
 	/* Read dictionary */
 	for(i = 0; i < dictsize; i++) {
-		p->dictionary[i].dict_leftson = rshort(ft);
-		p->dictionary[i].dict_rightson = rshort(ft);
+		st_readw(ft, &(p->dictionary[i].dict_leftson));
+		st_readw(ft, &(p->dictionary[i].dict_rightson));
 		/*
 		report("%d %d",
 		       p->dictionary[i].dict_leftson,
@@ -169,8 +169,9 @@
 ft_t ft;
 int n;
 {
+    unsigned char trash;
 	while (--n >= 0) {
-		if (getc(ft->fp) == EOF)
+	    	if (st_readb(ft, &trash) == ST_EOF)
 		{
 			fail("unexpected EOF in Mac header");
 			return(ST_EOF);
@@ -185,17 +186,18 @@
 {
 	register struct readpriv *p = (struct readpriv *) ft->priv;
 	int done = 0;
+	unsigned char sample_rate;
 
 	if (p->nrbits < 0) {
 		/* The first byte is special */
 		if (p->huffcount == 0)
 			return 0; /* Don't know if this can happen... */
-		p->sample = getc(ft->fp);
-		if (p->sample == EOF)
+		if (st_readb(ft, &sample_rate) == ST_EOF)
 		{
 			fail("unexpected EOF at start of HCOM data");
 			return (0);
 		}
+		p->sample = sample_rate;
 		*buf++ = (p->sample - 128) * 0x1000000L;
 		p->huffcount--;
 		p->nrbits = 0;
@@ -207,7 +209,7 @@
 
 	while (p->huffcount > 0) {
 		if(p->nrbits == 0) {
-			p->current = rlong(ft);
+			st_readdw(ft, &(p->current));
 			if (feof(ft->fp))
 			{
 				fail("unexpected EOF in HCOM data");
@@ -541,7 +543,7 @@
 int n;
 {
 	while (--n >= 0)
-		putc('\0', ft->fp);
+	    st_writeb(ft, '\0');
 }
 
 
@@ -567,8 +569,8 @@
 	padbytes(ft, 65-3);
 	(void) fwrite("FSSD", 1, 4, ft->fp);
 	padbytes(ft, 83-69);
-	wlong(ft, (ULONG) compressed_len); /* compressed_data size */
-	wlong(ft, (ULONG) 0); /* rsrc size */
+	st_writedw(ft, (ULONG) compressed_len); /* compressed_data size */
+	st_writedw(ft, (ULONG) 0); /* rsrc size */
 	padbytes(ft, 128 - 91);
 	if (ferror(ft->fp))
 	{
--- a/src/maud.c
+++ b/src/maud.c
@@ -54,6 +54,7 @@
 	unsigned short chaninf;
 	
 	ULONG chunksize;
+	ULONG trash;
 
 	int littlendian = 1;
 	char *endptr;
@@ -80,7 +81,7 @@
 		return (ST_EOF);
 	}
 	
-	rlong(ft); /* totalsize */
+	st_readdw(ft, &trash); /* totalsize */
 	
 	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "MAUD", 4) != 0)
 	{
@@ -99,7 +100,7 @@
 		
 		if (strncmp(buf,"MHDR",4) == 0) {
 			
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize != 8*4) 
 			{
 			    fail ("MAUD: MHDR chunk has bad size");
@@ -107,12 +108,18 @@
 			}
 			
 			/* fseek(ft->fp,12,SEEK_CUR); */
-			
-			p->nsamples = rlong(ft); /* number of samples stored in MDAT */
-			bitpersam = rshort(ft);  /* number of bits per sample as stored in MDAT */
-			rshort(ft);              /* number of bits per sample after decompression */
-			nom = rlong(ft);         /* clock source frequency */
-			denom = rshort(ft);       /* clock devide           */
+
+			/* number of samples stored in MDAT */
+			st_readdw(ft, &(p->nsamples));
+
+			/* number of bits per sample as stored in MDAT */
+			st_readw(ft, &bitpersam);
+
+			/* number of bits per sample after decompression */
+			st_readw(ft, (unsigned short *)&trash);
+
+			st_readdw(ft, &nom);         /* clock source frequency */
+			st_readw(ft, &denom);       /* clock devide           */
 			if (denom == 0) 
 			{
 			    fail("MAUD: frequency denominator == 0, failed");
@@ -121,7 +128,7 @@
 			
 			ft->info.rate = nom / denom;
 			
-			chaninf = rshort(ft); /* channel information */
+			st_readw(ft, &chaninf); /* channel information */
 			switch (chaninf) {
 			case 0:
 				ft->info.channels = 1;
@@ -134,7 +141,7 @@
 				return (ST_EOF);
 			}
 			
-			chaninf = rshort(ft); /* number of channels (mono: 1, stereo: 2, ...) */
+			st_readw(ft, &chaninf); /* number of channels (mono: 1, stereo: 2, ...) */
 			if (chaninf != ft->info.channels) 
 			{
 			    fail("MAUD: unsupported number of channels in file");
@@ -141,11 +148,11 @@
 			    return(ST_EOF);
 			}
 			
-			chaninf = rshort(ft); /* compression type */
+			st_readw(ft, &chaninf); /* compression type */
 			
-			rlong(ft); /* rest of chunk, unused yet */
-			rlong(ft);
-			rlong(ft);
+			st_readdw(ft, &trash); /* rest of chunk, unused yet */
+			st_readdw(ft, &trash);
+			st_readdw(ft, &trash);
 			
 			if (bitpersam == 8 && chaninf == 0) {
 				ft->info.size = ST_SIZE_BYTE;
@@ -175,7 +182,7 @@
 		}
 		
 		if (strncmp(buf,"ANNO",4) == 0) {
-			chunksize = rlong(ft);
+			st_readdw(ft, &chunksize);
 			if (chunksize & 1)
 				chunksize++;
 			chunk_buf = (char *) malloc(chunksize + 1);
@@ -198,7 +205,7 @@
 		}
 		
 		/* some other kind of chunk */
-		chunksize = rlong(ft);
+		st_readdw(ft, &chunksize);
 		if (chunksize & 1)
 			chunksize++;
 		fseek(ft->fp,chunksize,SEEK_CUR);
@@ -211,7 +218,7 @@
 	    fail("MAUD: MDAT chunk not found");
 	    return(ST_EOF);
 	}
-	p->nsamples = rlong(ft);
+	st_readdw(ft, &(p->nsamples));
 	return(ST_SUCCESS);
 }
 
@@ -326,44 +333,44 @@
 {
 	struct maudstuff * p = (struct maudstuff *) ft->priv;
 	
-	fputs ("FORM", ft->fp);
-	wlong(ft, (p->nsamples*ft->info.size) + MAUDHEADERSIZE);  /* size of file */
-	fputs("MAUD", ft->fp); /* File type */
+	st_writes(ft, "FORM");
+	st_writedw(ft, (p->nsamples*ft->info.size) + MAUDHEADERSIZE);  /* size of file */
+	st_writes(ft, "MAUD"); /* File type */
 	
-	fputs ("MHDR", ft->fp);
-	wlong(ft, (LONG) 8*4); /* number of bytes to follow */
-	wlong(ft, (LONG) (p->nsamples ));  /* number of samples stored in MDAT */
+	st_writes(ft, "MHDR");
+	st_writedw(ft, (LONG) 8*4); /* number of bytes to follow */
+	st_writedw(ft, (LONG) (p->nsamples ));  /* number of samples stored in MDAT */
 	
 	switch (ft->info.style) {
 		
 	case ST_ENCODING_UNSIGNED:
-		wshort(ft, (int) 8); /* number of bits per sample as stored in MDAT */
-		wshort(ft, (int) 8); /* number of bits per sample after decompression */
+		st_writew(ft, (int) 8); /* number of bits per sample as stored in MDAT */
+		st_writew(ft, (int) 8); /* number of bits per sample after decompression */
 		break;
 		
 	case ST_ENCODING_SIGN2:
-		wshort(ft, (int) 16); /* number of bits per sample as stored in MDAT */
-		wshort(ft, (int) 16); /* number of bits per sample after decompression */
+		st_writew(ft, (int) 16); /* number of bits per sample as stored in MDAT */
+		st_writew(ft, (int) 16); /* number of bits per sample after decompression */
 		break;
 		
 	case ST_ENCODING_ALAW:
 	case ST_ENCODING_ULAW:
-		wshort(ft, (int) 8); /* number of bits per sample as stored in MDAT */
-		wshort(ft, (int) 16); /* number of bits per sample after decompression */
+		st_writew(ft, (int) 8); /* number of bits per sample as stored in MDAT */
+		st_writew(ft, (int) 16); /* number of bits per sample after decompression */
 		break;
 		
 	}
 	
-	wlong(ft, (LONG) ft->info.rate); /* clock source frequency */
-	wshort(ft, (int) 1); /* clock devide */
+	st_writedw(ft, (LONG) ft->info.rate); /* clock source frequency */
+	st_writew(ft, (int) 1); /* clock devide */
 	
 	if (ft->info.channels == 1) {
-		wshort(ft, (int) 0); /* channel information */
-		wshort(ft, (int) 1); /* number of channels (mono: 1, stereo: 2, ...) */
+		st_writew(ft, (int) 0); /* channel information */
+		st_writew(ft, (int) 1); /* number of channels (mono: 1, stereo: 2, ...) */
 	}
 	else {
-		wshort(ft, (int) 1);
-		wshort(ft, (int) 2);
+		st_writew(ft, (int) 1);
+		st_writew(ft, (int) 2);
 	}
 	
 	switch (ft->info.style) {
@@ -370,27 +377,27 @@
 		
 	case ST_ENCODING_UNSIGNED:
 	case ST_ENCODING_SIGN2:
-		wshort(ft, (int) 0); /* no compression */
+		st_writew(ft, (int) 0); /* no compression */
 		break;
 		
 	case ST_ENCODING_ULAW:
-		wshort(ft, (int) 3);
+		st_writew(ft, (int) 3);
 		break;
 		
 	case ST_ENCODING_ALAW:
-		wshort(ft, (int) 2);
+		st_writew(ft, (int) 2);
 		break;
 		
 	}
 	
-	wlong(ft, (LONG) 0); /* reserved */
-	wlong(ft, (LONG) 0); /* reserved */
-	wlong(ft, (LONG) 0); /* reserved */
+	st_writedw(ft, (LONG) 0); /* reserved */
+	st_writedw(ft, (LONG) 0); /* reserved */
+	st_writedw(ft, (LONG) 0); /* reserved */
 	
-	fputs ("ANNO", ft->fp);
-	wlong(ft, (LONG) 32); /* length of block */
-	fputs ("file written by SOX MAUD-export ", ft->fp);
+	st_writes(ft, "ANNO");
+	st_writedw(ft, (LONG) 30); /* length of block */
+	st_writes(ft, "file create by Sound eXchange ");
 	
-	fputs ("MDAT", ft->fp);
-	wlong(ft, p->nsamples * ft->info.size ); /* samples in file */
+	st_writes(ft, "MDAT");
+	st_writedw(ft, p->nsamples * ft->info.size ); /* samples in file */
 }
--- a/src/misc.c
+++ b/src/misc.c
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <strings.h>
 
 const char *st_sizes_str[] = {
 	"NONSENSE!",
@@ -46,24 +47,69 @@
 
 /* Utilities */
 
-/* Read and write words and longs in "machine format".  Swap if indicated. */
+/* Read and write known datatypes in "machine format".  Swap if indicated.
+ * They all return ST_EOF on error and ST_SUCCESS on success.
+ */
 
-/* Read short. */
-unsigned short
-rshort(ft)
+/* Write string. */
+int
+st_writes(ft, c)
 ft_t ft;
+char *c;
 {
-	unsigned short us;
+	if (fwrite(c, 1, strlen(c), ft->fp) != strlen(c))
+	{
+		fail(writerr);
+		return(ST_EOF);
+	}
+	return(ST_SUCCESS);
+}
 
-	fread(&us, 2, 1, ft->fp);
+/* Read word. */
+int
+st_readb(ft, uc)
+ft_t ft;
+unsigned char *uc;
+{
+	if (fread(uc, 1, 1, ft->fp) != 1)
+	{
+	    return(ST_EOF);
+	}
+	return ST_SUCCESS;
+}
+
+/* Write word. */
+int
+st_writeb(ft, uc)
+ft_t ft;
+unsigned char uc;
+{
+	if (fwrite(&uc, 1, 1, ft->fp) != 1)
+	{
+		fail(writerr);
+		return(ST_EOF);
+	}
+	return(ST_SUCCESS);
+}
+
+/* Read word. */
+int
+st_readw(ft, us)
+ft_t ft;
+unsigned short *us;
+{
+	if (fread(us, 2, 1, ft->fp) != 1)
+	{
+	    return (ST_EOF);
+	}
 	if (ft->swap)
-		us = st_swapw(us);
-	return us;
+		*us = st_swapw(*us);
+	return ST_SUCCESS;
 }
 
-/* Write short. */
-unsigned short
-wshort(ft, us)
+/* Write word. */
+int
+st_writew(ft, us)
 ft_t ft;
 unsigned short us;
 {
@@ -70,26 +116,31 @@
 	if (ft->swap)
 		us = st_swapw(us);
 	if (fwrite(&us, 2, 1, ft->fp) != 1)
+	{
 		fail(writerr);
-	return(0);
+		return (ST_EOF);
+	}
+	return(ST_SUCCESS);
 }
 
-/* Read long. */
-ULONG
-rlong(ft)
+/* Read double word. */
+int
+st_readdw(ft, ul)
 ft_t ft;
+ULONG *ul;
 {
-	ULONG ul;
-
-	fread(&ul, 4, 1, ft->fp);
+	if (fread(ul, 4, 1, ft->fp) != 1)
+	{
+	    return (ST_EOF);
+	}
 	if (ft->swap)
-		ul = st_swapl(ul);
-	return ul;
+		*ul = st_swapl(*ul);
+	return ST_SUCCESS;
 }
 
-/* Write long. */
-ULONG
-wlong(ft, ul)
+/* Write double word. */
+int
+st_writedw(ft, ul)
 ft_t ft;
 ULONG ul;
 {
@@ -96,25 +147,31 @@
 	if (ft->swap)
 		ul = st_swapl(ul);
 	if (fwrite(&ul, 4, 1, ft->fp) != 1)
+	{
 		fail(writerr);
-	return(0);
+		return (ST_EOF);
+	}
+	return(ST_SUCCESS);
 }
 
 /* Read float. */
-float
-rfloat(ft)
+int
+st_readf(ft, f)
 ft_t ft;
+float *f;
 {
-	float f;
-
-	fread(&f, sizeof(float), 1, ft->fp);
+	if (fread(f, sizeof(float), 1, ft->fp) != 1)
+	{
+	    return(ST_EOF);
+	}
 	if (ft->swap)
-		f = st_swapf(f);
-	return f;
+		*f = st_swapf(*f);
+	return ST_SUCCESS;
 }
 
-void
-wfloat(ft, f)
+/* Write float. */
+int
+st_writef(ft, f)
 ft_t ft;
 float f;
 {
@@ -123,25 +180,31 @@
 	if (ft->swap)
 		t = st_swapf(t);
 	if (fwrite(&t, sizeof(float), 1, ft->fp) != 1)
+	{
 		fail(writerr);
+		return (ST_EOF);
+	}
+	return (ST_SUCCESS);
 }
 
 /* Read double. */
-double
-rdouble(ft)
+int
+st_readdf(ft, d)
 ft_t ft;
+double *d;
 {
-	double d;
-
-	fread(&d, sizeof(double), 1, ft->fp);
+	if (fread(d, sizeof(double), 1, ft->fp) != 1)
+	{
+	    return(ST_EOF);
+	}
 	if (ft->swap)
-		d = st_swapd(d);
-	return d;
+		*d = st_swapd(*d);
+	return ST_SUCCESS;
 }
 
 /* Write double. */
-void
-wdouble(ft, d)
+int
+st_writedf(ft, d)
 ft_t ft;
 double d;
 {
@@ -148,10 +211,14 @@
 	if (ft->swap)
 		d = st_swapd(d);
 	if (fwrite(&d, sizeof(double), 1, ft->fp) != 1)
+	{
 		fail(writerr);
+		return (ST_EOF);
+	}
+	return (ST_SUCCESS);
 }
 
-/* generic swap routine */
+/* generic swap routine. Swap l and place in to f (datatype length = n) */
 static void
 st_swapb(l, f, n)
 char *l, *f;
--- a/src/skel.c
+++ b/src/skel.c
@@ -128,7 +128,7 @@
 	int done = 0;
 
 	while(len--)
-		putc((*buf++ >> 24) ^ 0x80, ft->fp);
+	    st_writeb(ft, (*buf++ >> 24) ^ 0x80);
 	/* If you cannot write out all of the supplied samples, */
 	/*	fail("SKEL: Can't write all samples to %s", ft->filename); */
 	/*      return (ST_EOF); */
--- a/src/smp.c
+++ b/src/smp.c
@@ -36,7 +36,7 @@
 struct loop {
 	ULONG start; /* Sample count into sample data, not byte count */
 	ULONG end;   /* end point */
-	char type;	     /* 0 = loop off, 1 = forward, 2 = forw/back */
+	char type;   /* 0 = loop off, 1 = forward, 2 = forw/back */
 	short count;	     /* No of times to loop */
 };
 
@@ -75,17 +75,18 @@
 struct smptrailer *trailer;
 {
 	int i;
+	ULONG trash;
 
-	rshort(ft);			/* read reserved word */
+	st_readw(ft, (unsigned short *)&trash);	/* read reserved word */
 	for(i = 0; i < 8; i++) {	/* read the 8 loops */
-		trailer->loops[i].start = rlong(ft);
+		st_readdw(ft, &(trailer->loops[i].start));
 		ft->loops[i].start = trailer->loops[i].start;
-		trailer->loops[i].end = rlong(ft);
+		st_readdw(ft, &(trailer->loops[i].end));
 		ft->loops[i].length = 
 			trailer->loops[i].end - trailer->loops[i].start;
-		trailer->loops[i].type = getc(ft->fp);
+		st_readb(ft, &(trailer->loops[i].type));
 		ft->loops[i].type = trailer->loops[8].type;
-		trailer->loops[i].count = rshort(ft);
+		st_readw(ft, &(trailer->loops[i].count));
 		ft->loops[8].count = trailer->loops[8].count;
 	}
 	for(i = 0; i < 8; i++) {	/* read the 8 markers */
@@ -94,12 +95,12 @@
 		    fail("EOF in SMP");
 		    return(ST_EOF);
 		}
-		trailer->markers[i].position = rlong(ft);
+		st_readdw(ft, &(trailer->markers[i].position));
 	}
-	trailer->MIDInote = getc(ft->fp);
-	trailer->rate = rlong(ft);
-	trailer->SMPTEoffset = rlong(ft);
-	trailer->CycleSize = rlong(ft);
+	st_readb(ft, &(trailer->MIDInote));
+	st_readdw(ft, &(trailer->rate));
+	st_readdw(ft, &(trailer->SMPTEoffset));
+	st_readdw(ft, &(trailer->CycleSize));
 	return(ST_SUCCESS);
 }
 
@@ -149,12 +150,12 @@
 {
 	int i;
 
-	wshort(ft, 0);			/* write the reserved word */
+	st_writew(ft, 0);			/* write the reserved word */
 	for(i = 0; i < 8; i++) {	/* write the 8 loops */
-		wlong(ft, trailer->loops[i].start);
-		wlong(ft, trailer->loops[i].end);
-		putc(trailer->loops[i].type, ft->fp);
-		wshort(ft, trailer->loops[i].count);
+		st_writedw(ft, trailer->loops[i].start);
+		st_writedw(ft, trailer->loops[i].end);
+		st_writeb(ft, trailer->loops[i].type);
+		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)
@@ -162,12 +163,12 @@
 		    fail("EOF in SMP");
 	 	    return(ST_EOF);
 		}
-		wlong(ft, trailer->markers[i].position);
+		st_writedw(ft, trailer->markers[i].position);
 	}
-	putc(trailer->MIDInote, ft->fp);
-	wlong(ft, trailer->rate);
-	wlong(ft, trailer->SMPTEoffset);
-	wlong(ft, trailer->CycleSize);
+	st_writeb(ft, trailer->MIDInote);
+	st_writedw(ft, trailer->rate);
+	st_writedw(ft, trailer->SMPTEoffset);
+	st_writedw(ft, trailer->CycleSize);
 	return(ST_SUCCESS);
 }
 
@@ -237,7 +238,7 @@
 
 	report("SampleVision file name and comments: %s", ft->comment);
 	/* Extract out the sample size (always intel format) */
-	smp->NoOfSamps = rlong(ft);
+	st_readdw(ft, &(smp->NoOfSamps));
 	/* mark the start of the sample data */
 	samplestart = ftell(ft->fp);
 
@@ -316,11 +317,11 @@
 LONG *buf, len;
 {
 	smp_t smp = (smp_t) ft->priv;
-	LONG datum;
+	unsigned short datum;
 	int done = 0;
 	
 	for(; done < len && smp->NoOfSamps; done++, smp->NoOfSamps--) {
-		datum = rshort(ft);
+		st_readw(ft, &datum);
 		/* scale signed up to long's range */
 		*buf++ = LEFT(datum, 16);
 	}
@@ -377,7 +378,7 @@
 	    fail("SMP: Can't write header completely");
 	    return(ST_EOF);
 	}
-	wlong(ft, 0);	/* write as zero length for now, update later */
+	st_writedw(ft, 0);	/* write as zero length for now, update later */
 	smp->NoOfSamps = 0;
 
 	return(ST_SUCCESS);
@@ -393,7 +394,7 @@
 
 	while(done < len) {
 		datum = (int) RIGHT(*buf++, 16);
-		wshort(ft, datum);
+		st_writew(ft, datum);
 		smp->NoOfSamps++;
 		done++;
 	}
@@ -415,7 +416,7 @@
 		fail("SMP unable to seek back to save size");
 		return(ST_EOF);
 	}
-	wlong(ft, smp->NoOfSamps);
+	st_writedw(ft, smp->NoOfSamps);
 
 	return(ST_SUCCESS);
 }
--- a/src/sndrtool.c
+++ b/src/sndrtool.c
@@ -37,7 +37,7 @@
 {
         char buf[97];
 
-        LONG rate;
+        unsigned short rate;
 
 	int littlendian = 1;
 	char *endptr;
@@ -73,7 +73,7 @@
 	if (strncmp(buf,"\0\0",2) == 0)
 	{
 	/* sounder */
-	rate = rshort(ft);
+	st_readw(ft, &rate);
 	if (rate < 4000 || rate > 25000 )
 	{
 		fail ("SND: sample rate out of range");
@@ -91,7 +91,7 @@
 		return(ST_EOF);
 	}
 	fseek(ft->fp,12,SEEK_CUR);
-	rate = rshort(ft);
+	st_readw(ft, &rate);
 	fseek(ft->fp,6,SEEK_CUR);
 	if (fread(buf,1,96,ft->fp) != 96)
 	{
@@ -174,10 +174,10 @@
 ft->info.size = ST_SIZE_BYTE;
 
 /* sounder header */
-wshort (ft,0); /* sample size code */
-wshort (ft,(int) ft->info.rate);     /* sample rate */
-wshort (ft,10);        /* volume */
-wshort (ft,4); /* shift */
+st_writew (ft,0); /* sample size code */
+st_writew (ft,(int) ft->info.rate);     /* sample rate */
+st_writew (ft,10);        /* volume */
+st_writew (ft,4); /* shift */
 
 return(ST_SUCCESS);
 }
@@ -228,16 +228,16 @@
 char name_buf[97];
 
 /* sndtool header */
-fputs ("SOUND",ft->fp); /* magic */
-fputc (0x1a,ft->fp);
-wshort (ft,(LONG)0);  /* hGSound */
-wlong (ft,nsamples);
-wlong (ft,(LONG)0);
-wlong (ft,nsamples);
-wshort (ft,(int) ft->info.rate);
-wshort (ft,0);
-wshort (ft,10);
-wshort (ft,4);
+st_writes(ft, "SOUND"); /* magic */
+st_writeb(ft, 0x1a);
+st_writew (ft,(LONG)0);  /* hGSound */
+st_writedw (ft,nsamples);
+st_writedw (ft,(LONG)0);
+st_writedw (ft,nsamples);
+st_writew (ft,(int) ft->info.rate);
+st_writew (ft,0);
+st_writew (ft,10);
+st_writew (ft,4);
 memset (name_buf, 0, 96);
 sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
 fwrite (name_buf, 1, 96, ft->fp);
--- a/src/st.h
+++ b/src/st.h
@@ -290,14 +290,17 @@
  * possible byte swapping.
  */
 /* declared in misc.c */
-unsigned short rshort(P1(ft_t ft));			
-unsigned short wshort(P2(ft_t ft, unsigned short us));
-ULONG          rlong(P1(ft_t ft));		
-ULONG          wlong(P2(ft_t ft, ULONG ul));
-float          rfloat(P1(ft_t ft));
-void           wfloat(P2(ft_t ft, double f));
-double         rdouble(P1(ft_t ft));
-void           wdouble(P2(ft_t ft, double d));
+int	st_writes(P2(ft_t ft, char *uc));
+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));
+int	st_writew(P2(ft_t ft, unsigned short us));
+int	st_readdw(P2(ft_t ft, ULONG *ul));		
+int	st_writedw(P2(ft_t ft, ULONG ul));
+int	st_readf(P2(ft_t ft, float *f));
+int	st_writef(P2(ft_t ft, double f));
+int	st_readdf(P2(ft_t ft, double *d));
+int	st_writedf(P2(ft_t ft, double d));
 
 /* FIXME: raw routines are used by so many formats their prototypes are defined
  * here for convience.  This wont last for long so application software
--- a/src/tx16w.c
+++ b/src/tx16w.c
@@ -82,9 +82,9 @@
   char format;
   char sample_rate;
   LONG num_samp_bytes = 0;
-  char dummy;
   char gunk[8];
   int blewIt;
+  ULONG trash;
 
   txw_t sk = (txw_t) ft->priv;
   /* If you need to seek around the input file. */
@@ -95,7 +95,7 @@
   }
 
   /* This is dumb but portable, just count the bytes til EOF */
-  while ( getc(ft->fp) != EOF ) 
+  while (st_readb(ft, (unsigned char *)&trash) != ST_EOF)
     num_samp_bytes++; 
   num_samp_bytes -= 32;		/* calculate num samples by sub header size */
   fseek(ft->fp,0L,0);		/* rewind file */
@@ -102,23 +102,23 @@
   sk->rest = num_samp_bytes;	/* set how many sample bytes to read */
 
   /* first 6 bytes are file type ID LM8953 */
-  filetype[0] = getc(ft->fp);
-  filetype[1] = getc(ft->fp);
-  filetype[2] = getc(ft->fp);
-  filetype[3] = getc(ft->fp);
-  filetype[4] = getc(ft->fp);
-  filetype[5] = getc(ft->fp);
+  st_readb(ft, &filetype[0]);
+  st_readb(ft, &filetype[1]);
+  st_readb(ft, &filetype[2]);
+  st_readb(ft, &filetype[3]);
+  st_readb(ft, &filetype[4]);
+  st_readb(ft, &filetype[5]);
   filetype[6] = '\0';
   for( c = 16; c > 0 ; c-- )	/* Discard next 16 bytes */
-    dummy = getc(ft->fp);	/* they have no meaning here */
-  format = getc(ft->fp);
-  sample_rate = getc(ft->fp);
+      st_readb(ft, (unsigned char *)&trash);
+  st_readb(ft, &format);
+  st_readb(ft, &sample_rate);
   /*
    * save next 8 bytes - if sample rate is 0, then we need
    *  to look at gunk[2] and gunk[5] to get real rate
    */
   for( c = 0; c < 8; c++ )
-    gunk[c] = getc(ft->fp);
+      st_readb(ft, &(gunk[c]));
   /*
    * We should now be pointing at start of raw sample data in file 
    */
@@ -222,9 +222,9 @@
          */
         for(done = 0; done < len; ) {
 	    if(sk->rest <= 0) break; /* Finished reading from file? */
-	    uc1 = (unsigned char)getc(ft->fp); /* read the three bytes */
-	    uc2 = (unsigned char)getc(ft->fp);
-	    uc3 = (unsigned char)getc(ft->fp);
+	    st_readb(ft, &uc1);
+	    st_readb(ft, &uc2);
+	    st_readb(ft, &uc3);
 	    sk->rest -= 3; /* adjust remaining for bytes we just read */
 	    s1 = (unsigned short) (uc1 << 4) | (((uc2 >> 4) & 017));
 	    s2 = (unsigned short) (uc3 << 4) | (( uc2 & 017 ));
@@ -296,9 +296,9 @@
             else {
                 w2 =  *buf++ >> 20;
             }
-            putc((w1 >> 4) & 0xFF,ft->fp);
-            putc((((w1 & 0x0F) << 4) | (w2 & 0x0F)) & 0xFF,ft->fp);
-            putc((w2 >> 4) & 0xFF,ft->fp);
+            st_writeb(ft, (w1 >> 4) & 0xFF);
+            st_writeb(ft, (((w1 & 0x0F) << 4) | (w2 & 0x0F)) & 0xFF);
+            st_writeb(ft, (w2 >> 4) & 0xFF);
             writedone += 3;
         }
 	return(len);
@@ -351,9 +351,9 @@
         AttackLength                       = 0x40;
         LoopLength                         = 0x40;
         for(i=tx16w_len;i<0x80;i++) {
-            putc(0,ft->fp);
-            putc(0,ft->fp);
-            putc(0,ft->fp);
+            st_writeb(ft, 0);
+            st_writeb(ft, 0);
+            st_writeb(ft, 0);
             writedone += 3;
         }
     }
@@ -361,7 +361,7 @@
     /* Fill up to 256 byte blocks; the TX16W seems to like that */
 
     while ((writedone % 0x100) != 0) {
-        putc(0,ft->fp);
+        st_writeb(ft, 0);
         writedone++;
     }
 
--- a/src/voc.c
+++ b/src/voc.c
@@ -145,12 +145,12 @@
 typedef struct vocstuff {
 	LONG	rest;			/* bytes remaining in current block */
 	LONG	rate;			/* rate code (byte) of this chunk */
-	int		silent;			/* sound or silence? */
+	int		silent;		/* sound or silence? */
 	LONG	srate;			/* rate code (byte) of silence */
 	LONG	blockseek;		/* start of current output block */
 	LONG	samples;		/* number of samples output */
-	int		size;			/* word length of data */
-	int		channels;		/* number of sound channels */
+	int		size;		/* word length of data */
+	unsigned char	channels;	/* number of sound channels */
 	int     extended;       /* Has an extended block been read? */
 } *vs_t;
 
@@ -176,7 +176,7 @@
 {
 	char header[20];
 	vs_t v = (vs_t) ft->priv;
-	int sbseek;
+	unsigned short sbseek;
 	int littlendian = 1;
 	char *endptr;
 	int rc;
@@ -205,7 +205,7 @@
 		return(ST_EOF);
 	}
 
-	sbseek = rshort(ft);
+	st_readw(ft, &sbseek);
 	fseek(ft->fp, sbseek, 0);
 
 	v->rate = -1;
@@ -237,6 +237,8 @@
 	vs_t v = (vs_t) ft->priv;
 	int done = 0;
 	int rc;
+	unsigned short us;
+	unsigned char uc;
 	
 	if (v->rest == 0)
 	{
@@ -254,20 +256,19 @@
 			*buf++ = 0x80000000L;
 	} else {
 		for(;v->rest && (done < len); v->rest--, done++) {
-			LONG datum;
 			switch(v->size)
 			{
 			    case ST_SIZE_BYTE:
-				if ((datum = getc(ft->fp)) == EOF) {
+				if (st_readb(ft, &uc) == ST_EOF) {
 				    warn("VOC input: short file");
 				    v->rest = 0;
 				    return done;
 				}
-				datum ^= 0x80;	/* convert to signed */
-				*buf++ = LEFT(datum, 24);
+				uc ^= 0x80;	/* convert to signed */
+				*buf++ = LEFT(uc, 24);
 				break;
 			    case ST_SIZE_WORD:
-				datum = rshort(ft);
+				st_readw(ft, &us);
 				if (feof(ft->fp))
 				{
 				    warn("VOC input: short file");
@@ -274,7 +275,7 @@
 				    v->rest = 0;
 				    return done;
 				}
-				*buf++ = LEFT(datum, 16);
+				*buf++ = LEFT(us, 16);
 				v->rest--; /* Processed 2 bytes so update */
 				break;
 			}	
@@ -326,9 +327,9 @@
 
 	/* File format name and a ^Z (aborts printing under DOS) */
 	(void) fwrite("Creative Voice File\032\032", 1, 20, ft->fp);
-	wshort(ft, 26);			/* size of header */
-	wshort(ft, 0x10a);              /* major/minor version number */
-	wshort(ft, 0x1129);		/* checksum of version number */
+	st_writew(ft, 26);			/* size of header */
+	st_writew(ft, 0x10a);              /* major/minor version number */
+	st_writew(ft, 0x1129);		/* checksum of version number */
 
 	if (ft->info.size == ST_SIZE_BYTE)
 	  ft->info.style = ST_ENCODING_UNSIGNED;
@@ -359,10 +360,10 @@
 	  if (ft->info.size == ST_SIZE_BYTE) {
 	    uc = RIGHT(*buf++, 24);
 	    uc ^= 0x80;
-	    putc(uc, ft->fp);
+	    st_writeb(ft, uc);
 	  } else {
 		sw = (int) RIGHT(*buf++, 16);
-	    wshort(ft,sw);
+	    st_writew(ft,sw);
           }
 	  done++;
 	}
@@ -386,14 +387,16 @@
 	vs_t v = (vs_t) ft->priv;
 	unsigned char uc, block;
 	ULONG sblen;
-	LONG new_rate;
+	unsigned short new_rate_short;
+	ULONG new_rate_long;
 	int i;
+	ULONG trash;
 
 	v->silent = 0;
 	while (v->rest == 0) {
 		if (feof(ft->fp))
 			return ST_SUCCESS;
-		block = getc(ft->fp);
+		st_readb(ft, &block);
 		if (block == VOC_TERM)
 			return ST_SUCCESS;
 		if (feof(ft->fp))
@@ -403,15 +406,15 @@
 		 * func to read this so do it this cross-platform way
 		 *
 		 */
-		uc = getc(ft->fp);
+		st_readb(ft, &uc);
 		sblen = uc;
-		uc = getc(ft->fp);
+		st_readb(ft, &uc);
 		sblen |= ((LONG) uc) << 8;
-		uc = getc(ft->fp);
+		st_readb(ft, &uc);
 		sblen |= ((LONG) uc) << 16;
 		switch(block) {
 		case VOC_DATA: 
-		        uc = getc(ft->fp);
+			st_readb(ft, &uc);
 			/* When DATA block preceeded by an EXTENDED     */
 			/* block, the DATA blocks rate value is invalid */
 		        if (!v->extended) {
@@ -430,7 +433,7 @@
 			  ft->info.rate = 1000000.0/(256 - v->rate);
 			  v->channels = 1;
 			}
-			uc = getc(ft->fp);
+			st_readb(ft, &uc);
 			if (uc != 0)
 			{
 			  fail("File %s: only interpret 8-bit data!",
@@ -442,21 +445,21 @@
 			v->size = ST_SIZE_BYTE;
 			return (ST_SUCCESS);
 		case VOC_DATA_16:
-			new_rate = rlong(ft);
-			if (new_rate == 0)
+			st_readdw(ft, &new_rate_long);
+			if (new_rate_long == 0)
 			{
 			    fail("File %s: Sample rate is zero?",ft->filename);
 			    return(ST_EOF);
 			}
-			if ((v->rate != -1) && (new_rate != v->rate))
+			if ((v->rate != -1) && (new_rate_long != v->rate))
 			{
 			    fail("File %s: sample rate codes differ: %d != %d",
-				ft->filename, v->rate, new_rate);
+				ft->filename, v->rate, new_rate_long);
 			    return(ST_EOF);
 			}
-			v->rate = new_rate;
-			ft->info.rate = new_rate;
-			uc = getc(ft->fp);
+			v->rate = new_rate_long;
+			ft->info.rate = new_rate_long;
+			st_readb(ft, &uc);
 			switch (uc)
 			{
 			    case 8:	v->size = ST_SIZE_BYTE; break;
@@ -465,13 +468,13 @@
 					fail("Don't understand size %d", uc);
 					return(ST_EOF);
 			}
-			v->channels = getc(ft->fp);
-			getc(ft->fp);	/* unknown */
-			getc(ft->fp);	/* notused */
-			getc(ft->fp);	/* notused */
-			getc(ft->fp);	/* notused */
-			getc(ft->fp);	/* notused */
-			getc(ft->fp);	/* notused */
+			st_readb(ft, &(v->channels));
+			st_readb(ft, (unsigned char *)&trash); /* unknown */
+			st_readb(ft, (unsigned char *)&trash); /* notused */
+			st_readb(ft, (unsigned char *)&trash); /* notused */
+			st_readb(ft, (unsigned char *)&trash); /* notused */
+			st_readb(ft, (unsigned char *)&trash); /* notused */
+			st_readb(ft, (unsigned char *)&trash); /* notused */
 			v->rest = sblen - 12;
 			return (ST_SUCCESS);
 		case VOC_CONT: 
@@ -481,8 +484,8 @@
 			{
 			unsigned short period;
 
-			period = rshort(ft);
-			uc = getc(ft->fp);
+			st_readw(ft, &period);
+			st_readb(ft, &uc);
 			if (uc == 0)
 			{
 				fail("File %s: Silence sample rate is zero");
@@ -502,8 +505,8 @@
 			return (ST_SUCCESS);
 			}
 		case VOC_MARKER:
-			uc = getc(ft->fp);
-			uc = getc(ft->fp);
+			st_readb(ft, &uc);
+			st_readb(ft, &uc);
 			/* Falling! Falling! */
 		case VOC_TEXT:
 			{
@@ -510,7 +513,7 @@
 			int i;
 			/* Could add to comment in SF? */
 			for(i = 0; i < sblen; i++)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 			}
 			continue;	/* get next block */
 		case VOC_LOOP:
@@ -517,7 +520,7 @@
 		case VOC_LOOPEND:
 			report("File %s: skipping repeat loop");
 			for(i = 0; i < sblen; i++)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 			break;
 		case VOC_EXTENDED:
 			/* An Extended block is followed by a data block */
@@ -525,20 +528,20 @@
 			/* value from the extended block and not the     */
 			/* data block.					 */
 			v->extended = 1;
-			new_rate = rshort(ft);
-			if (new_rate == 0)
+			st_readw(ft, &new_rate_short);
+			if (new_rate_short == 0)
 			{
 			   fail("File %s: Sample rate is zero?");
 			   return(ST_EOF);
 			}
-			if ((v->rate != -1) && (new_rate != v->rate))
+			if ((v->rate != -1) && (new_rate_short != v->rate))
 			{
 			   fail("File %s: sample rate codes differ: %d != %d",
-					ft->filename, v->rate, new_rate);
+					ft->filename, v->rate, new_rate_short);
 			   return(ST_EOF);
 			}
-			v->rate = new_rate;
-			uc = getc(ft->fp);
+			v->rate = new_rate_short;
+			st_readb(ft, &uc);
 			if (uc != 0)
 			{
 				fail("File %s: only interpret 8-bit data!",
@@ -545,7 +548,7 @@
 					ft->filename);
 				return(ST_EOF);
 			}
-			uc = getc(ft->fp);
+			st_readb(ft, &uc);
 			if (uc)
 				ft->info.channels = 2;  /* Stereo */
 			/* Needed number of channels before finishing
@@ -559,7 +562,7 @@
 			report("File %s: skipping unknown block code %d",
 				ft->filename, block);
 			for(i = 0; i < sblen; i++)
-				getc(ft->fp);
+			    st_readb(ft, (unsigned char *)&trash);
 		}
 	}
 	return ST_SUCCESS;
@@ -573,10 +576,10 @@
 
 	v->blockseek = ftell(ft->fp);
 	if (v->silent) {
-		putc(VOC_SILENCE, ft->fp);	/* Silence block code */
-		putc(0, ft->fp);		/* Period length */
-		putc(0, ft->fp);		/* Period length */
-		putc((int) v->rate, ft->fp);		/* Rate code */
+		st_writeb(ft, VOC_SILENCE);	/* Silence block code */
+		st_writeb(ft, 0);		/* Period length */
+		st_writeb(ft, 0);		/* Period length */
+		st_writeb(ft, v->rate);		/* Rate code */
 	} else {
 	  if (ft->info.size == ST_SIZE_BYTE) {
 	    /* 8-bit sample section.  By always setting the correct     */
@@ -586,37 +589,37 @@
 	    /* Prehaps the rate should be doubled though to make up for */
 	    /* double amount of samples for a given time????            */
 	    if (ft->info.channels > 1) {
-	      putc(VOC_EXTENDED, ft->fp);	/* Voice Extended block code */
-	      putc(4, ft->fp);                /* block length = 4 */
-	      putc(0, ft->fp);                /* block length = 4 */
-	      putc(0, ft->fp);                /* block length = 4 */
+	      st_writeb(ft, VOC_EXTENDED);	/* Voice Extended block code */
+	      st_writeb(ft, 4);                /* block length = 4 */
+	      st_writeb(ft, 0);                /* block length = 4 */
+	      st_writeb(ft, 0);                /* block length = 4 */
 		  v->rate = 65536L - (256000000.0/(2*(float)ft->info.rate));
-	      wshort(ft,v->rate);	/* Rate code */
-	      putc(0, ft->fp);                /* File is not packed */
-	      putc(1, ft->fp);                /* samples are in stereo */
+	      st_writew(ft,v->rate);	/* Rate code */
+	      st_writeb(ft, 0);         /* File is not packed */
+	      st_writeb(ft, 1);         /* samples are in stereo */
 	    }
-	    putc(VOC_DATA, ft->fp);		/* Voice Data block code */
-	    putc(0, ft->fp);		/* block length (for now) */
-	    putc(0, ft->fp);		/* block length (for now) */
-	    putc(0, ft->fp);		/* block length (for now) */
+	    st_writeb(ft, VOC_DATA);	/* Voice Data block code */
+	    st_writeb(ft, 0);		/* block length (for now) */
+	    st_writeb(ft, 0);		/* block length (for now) */
+	    st_writeb(ft, 0);		/* block length (for now) */
 	    v->rate = 256 - (1000000.0/(float)ft->info.rate);
-	    putc((int) v->rate, ft->fp);/* Rate code */
-	    putc(0, ft->fp);		/* 8-bit raw data */
+	    st_writeb(ft, (int) v->rate);/* Rate code */
+	    st_writeb(ft, 0);		/* 8-bit raw data */
 	} else {
-	    putc(VOC_DATA_16, ft->fp);		/* Voice Data block code */
-	    putc(0, ft->fp);		/* block length (for now) */
-	    putc(0, ft->fp);		/* block length (for now) */
-	    putc(0, ft->fp);		/* block length (for now) */
+	    st_writeb(ft, VOC_DATA_16); /* Voice Data block code */
+	    st_writeb(ft, 0);		/* block length (for now) */
+	    st_writeb(ft, 0);		/* block length (for now) */
+	    st_writeb(ft, 0);		/* block length (for now) */
 	    v->rate = ft->info.rate;
-	    wlong(ft, v->rate);	/* Rate code */
-	    putc(16, ft->fp);		/* Sample Size */
-	    putc(ft->info.channels, ft->fp);	/* Sample Size */
-	    putc(0, ft->fp);		/* Unknown */
-	    putc(0, ft->fp);		/* Unused */
-	    putc(0, ft->fp);		/* Unused */
-	    putc(0, ft->fp);		/* Unused */
-	    putc(0, ft->fp);		/* Unused */
-	    putc(0, ft->fp);		/* Unused */
+	    st_writedw(ft, v->rate);	/* Rate code */
+	    st_writeb(ft, 16);		/* Sample Size */
+	    st_writeb(ft, ft->info.channels);	/* Sample Size */
+	    st_writeb(ft, 0);		/* Unknown */
+	    st_writeb(ft, 0);		/* Unused */
+	    st_writeb(ft, 0);		/* Unused */
+	    st_writeb(ft, 0);		/* Unused */
+	    st_writeb(ft, 0);		/* Unused */
+	    st_writeb(ft, 0);		/* Unused */
 	  }
 	}
 }
@@ -628,11 +631,11 @@
 	vs_t v = (vs_t) ft->priv;
 	LONG datum;
 
-	putc(0, ft->fp);			/* End of file block code */
+	st_writeb(ft, 0);			/* End of file block code */
 	fseek(ft->fp, v->blockseek, 0);		/* seek back to block length */
 	fseek(ft->fp, 1, 1);			/* seek forward one */
 	if (v->silent) {
-		wshort(ft, v->samples);
+		st_writew(ft, v->samples);
 	} else {
 	  if (ft->info.size == ST_SIZE_BYTE) {
 	    if (ft->info.channels > 1) {
@@ -641,11 +644,11 @@
 	  }
 	        v->samples += 2;		/* adjustment: SBDK pp. 3-5 */
 		datum = (v->samples) & 0xff;
-		putc((int)datum, ft->fp);       /* low byte of length */
+		st_writeb(ft, (int)datum);       /* low byte of length */
 		datum = (v->samples >> 8) & 0xff;
-		putc((int)datum, ft->fp);  /* middle byte of length */
+		st_writeb(ft, (int)datum);  /* middle byte of length */
 		datum = (v->samples >> 16) & 0xff;
-		putc((int)datum, ft->fp); /* high byte of length */
+		st_writeb(ft, (int)datum); /* high byte of length */
 	}
 }
 
--- a/src/wav.c
+++ b/src/wav.c
@@ -351,7 +351,7 @@
 
     if (pad & wav->gsmbytecount){
 	/* pad output to an even number of bytes */
-	if(fputc(0,ft->fp))
+	if(st_writeb(ft, 0))
 	{
 	    fail("write error");
 	    return (ST_EOF);
@@ -420,7 +420,7 @@
 	    fail("WAVE file has missing %s chunk", Label);
 	    return 0;
 	}
-	len = rlong(ft);
+	st_readdw(ft, &len);
 	if (strncmp(Label, magic, 4) == 0)
 	    break;		/* Found the data chunk */
 	
@@ -459,7 +459,7 @@
     ULONG    bytesPerBlock = 0;
     ULONG    bytespersample;	    /* bytes per sample (per channel */
 
-    /* This is needed for rawread(), rshort, etc */
+    /* This is needed for rawread(), st_readw, etc */
     rc = st_rawstartread(ft);
     if (rc)
 	return rc;
@@ -473,7 +473,7 @@
 	return ST_EOF;
     }
 
-    wRiffLength = rlong(ft);
+    st_readdw(ft, &wRiffLength);
 
     if ( fread(magic, 1, 4, ft->fp) != 4 || strncmp("WAVE", magic, 4))
     {
@@ -491,12 +491,12 @@
 	return ST_EOF;
     }
 
-    wav->formatTag = rshort(ft);
-    wChannels = rshort(ft);
-    wSamplesPerSecond = rlong(ft);
-    wAvgBytesPerSec = rlong(ft);	/* Average bytes/second */
-    wav->blockAlign = rshort(ft);	/* Block align */
-    wBitsPerSample =  rshort(ft);	/* bits per sample per channel */
+    st_readw(ft, &(wav->formatTag));
+    st_readw(ft, &wChannels);
+    st_readdw(ft, &wSamplesPerSecond);
+    st_readdw(ft, &wAvgBytesPerSec);	/* Average bytes/second */
+    st_readw(ft, &(wav->blockAlign));	/* Block align */
+    st_readw(ft, &wBitsPerSample);	/* bits per sample per channel */
     len -= 16;
 
     switch (wav->formatTag)
@@ -614,7 +614,7 @@
     /* non-PCM formats have extended fmt chunk.  Check for those cases. */
     if (wav->formatTag != WAVE_FORMAT_PCM) {
 	if (len >= 2) {
-	    wExtSize = rshort(ft);
+	    st_readw(ft, &wExtSize);
 	    len -= 2;
 	} else {
 	    warn("wave header missing FmtExt chunk");
@@ -644,7 +644,7 @@
 	    return ST_EOF;
 	}
 
-	wav->samplesPerBlock = rshort(ft);
+	st_readw(ft, &(wav->samplesPerBlock));
 	bytesPerBlock = AdpcmBytesPerBlock(ft->info.channels, wav->samplesPerBlock);
 	if (bytesPerBlock > wav->blockAlign)
 	{
@@ -653,7 +653,7 @@
 	    return ST_EOF;
 	}
 
-	wav->nCoefs = rshort(ft);
+	st_readw(ft, &(wav->nCoefs));
 	if (wav->nCoefs < 7 || wav->nCoefs > 0x100) {
 	    fail("ADPCM file nCoefs (%.4hx) makes no sense\n", wav->nCoefs);
 	    return ST_EOF;
@@ -690,7 +690,7 @@
 	{
 	    int i, errct=0;
 	    for (i=0; len>=2 && i < 2*wav->nCoefs; i++) {
-		wav->iCoefs[i] = rshort(ft);
+		st_readw(ft, &(wav->iCoefs[i]));
 		len -= 2;
 		if (i<14) errct += (wav->iCoefs[i] != iCoef[i/2][i%2]);
 		/* fprintf(stderr,"iCoefs[%2d] %4d\n",i,wav->iCoefs[i]); */
@@ -715,7 +715,7 @@
 	    return ST_EOF;
 	}
 
-	wav->samplesPerBlock = rshort(ft);
+	st_readw(ft, &(wav->samplesPerBlock));
 	bytesPerBlock = ImaBytesPerBlock(ft->info.channels, wav->samplesPerBlock);
 	if (bytesPerBlock > wav->blockAlign || wav->samplesPerBlock%8 != 1)
 	{
@@ -751,7 +751,7 @@
 		    wav_format_str(wav->formatTag), 2);
 	    return ST_EOF;
 	}
-	wav->samplesPerBlock = rshort(ft);
+	st_readw(ft, &wav->samplesPerBlock);
 	bytesPerBlock = 65;
 	if (wav->blockAlign != 65)
 	{
@@ -1305,39 +1305,39 @@
 	wAvgBytesPerSec = (double)wBlockAlign*ft->info.rate / (double)wSamplesPerBlock + 0.5;
 
 	/* figured out header info, so write it */
-	fputs("RIFF", ft->fp);
-	wlong(ft, wRiffLength);
-	fputs("WAVE", ft->fp);
-	fputs("fmt ", ft->fp);
-	wlong(ft, wFmtSize);
-	wshort(ft, wFormatTag);
-	wshort(ft, wChannels);
-	wlong(ft, wSamplesPerSecond);
-	wlong(ft, wAvgBytesPerSec);
-	wshort(ft, wBlockAlign);
-	wshort(ft, wBitsPerSample); /* end info common to all fmts */
+	st_writes(ft, "RIFF");
+	st_writedw(ft, wRiffLength);
+	st_writes(ft, "WAVE");
+	st_writes(ft, "fmt ");
+	st_writedw(ft, wFmtSize);
+	st_writew(ft, wFormatTag);
+	st_writew(ft, wChannels);
+	st_writedw(ft, wSamplesPerSecond);
+	st_writedw(ft, wAvgBytesPerSec);
+	st_writew(ft, wBlockAlign);
+	st_writew(ft, wBitsPerSample); /* end info common to all fmts */
 
 	/* if not PCM, we need to write out wExtSize even if wExtSize=0 */
 	if (wFormatTag != WAVE_FORMAT_PCM)
-	    wshort(ft,wExtSize);
+	    st_writew(ft,wExtSize);
 
 	switch (wFormatTag)
 	{
 	int i;
 	case WAVE_FORMAT_IMA_ADPCM:
-	    wshort(ft, wSamplesPerBlock);
+	    st_writew(ft, wSamplesPerBlock);
 	    break;
 	case WAVE_FORMAT_ADPCM:
-	    wshort(ft, wSamplesPerBlock);
-	    wshort(ft, 7); /* nCoefs */
+	    st_writew(ft, wSamplesPerBlock);
+	    st_writew(ft, 7); /* nCoefs */
 	    for (i=0; i<7; i++) {
-	      wshort(ft, iCoef[i][0]);
-	      wshort(ft, iCoef[i][1]);
+	      st_writew(ft, iCoef[i][0]);
+	      st_writew(ft, iCoef[i][1]);
 	    }
 	    break;
 #ifdef HAVE_LIBGSM
 	case WAVE_FORMAT_GSM610:
-	    wshort(ft, wSamplesPerBlock);
+	    st_writew(ft, wSamplesPerBlock);
 	    break;
 #endif
 	default:
@@ -1345,13 +1345,13 @@
 
 	/* if not PCM, write the 'fact' chunk */
 	if (wFormatTag != WAVE_FORMAT_PCM){
-	    fputs("fact", ft->fp);
-	    wlong(ft,wFactSize); 
-	    wlong(ft,wSamplesWritten);
+	    st_writes(ft, "fact");
+	    st_writedw(ft,wFactSize); 
+	    st_writedw(ft,wSamplesWritten);
 	}
 
-	fputs("data", ft->fp);
-	wlong(ft, wDataLength);		/* data chunk size */
+	st_writes(ft, "data");
+	st_writedw(ft, wDataLength);		/* data chunk size */
 
 	if (!second_header) {
 		report("Writing Wave file: %s format, %d channel%s, %d samp/sec",
--- a/src/wve.c
+++ b/src/wve.c
@@ -1,4 +1,4 @@
-/*
+/*;
  * Psion wve format, based on the au format file. Hacked by
  * Richard Caley (R.Caley@ed.ac.uk)
  */
@@ -14,7 +14,7 @@
 
 struct wvepriv
     {
-    unsigned int length;
+    ULONG length;
     short padding;
     short repeats;
     };
@@ -32,6 +32,8 @@
 	char *endptr;
 	int rc;
 
+	ULONG trash;
+
 	/* Needed for rawread() */
 	rc = st_rawstartread(ft);
 	if (rc)
@@ -57,7 +59,7 @@
 		return (ST_EOF);
 	}
 
-        version=rshort(ft);
+        st_readw(ft, &version);
 
 	/* Check for what type endian machine its read on */
 	if (version == PSION_INV_VERSION)
@@ -79,15 +81,15 @@
 	    return(ST_EOF);
 	}
 
-     	p->length=rlong(ft);
+     	st_readdw(ft, &(p->length));
 
-	p->padding=rshort(ft);
+	st_readw(ft, &(p->padding));
 
-	p->repeats=rshort(ft);
+	st_readw(ft, &(p->repeats));
 
- 	(void)rshort(ft);
- 	(void)rshort(ft);
- 	(void)rshort(ft);
+ 	(void)st_readw(ft, (unsigned short *)&trash);
+  	(void)st_readw(ft, (unsigned short *)&trash);
+	(void)st_readw(ft, (unsigned short *)&trash);
     
 	ft->info.style = ST_ENCODING_ALAW;
 	ft->info.size = ST_SIZE_BYTE;
@@ -193,13 +195,13 @@
 
     fwrite(magic, sizeof(magic), 1, ft->fp);
 
-    wshort(ft, version);
-    wlong(ft, p->length);
-    wshort(ft, p->padding);
-    wshort(ft, p->repeats);
+    st_writew(ft, version);
+    st_writedw(ft, p->length);
+    st_writew(ft, p->padding);
+    st_writew(ft, p->repeats);
 
-    wshort(ft, zero);
-    wshort(ft, zero);
-    wshort(ft, zero);
+    st_writew(ft, zero);
+    st_writew(ft, zero);
+    st_writew(ft, zero);
 }