shithub: cuefs

Download patch

ref: 11b150a2bb01fbf09ae7cfee553dda0e8af5334d
parent: adb84ba1604eca9fbacc16b1eede182a107562ca
author: Tevo <estevan.cps@gmail.com>
date: Sat Dec 26 13:51:28 EST 2020

Clean things up, accidentaly fixed crash on unmount

--- a/BUGS	Wed Dec 23 19:04:34 2020
+++ b/BUGS	Sat Dec 26 13:51:28 2020
@@ -1,4 +1,5 @@
 • unsure if seeking on a file works correctly
-• crashes on unmount (buffer overflow caught by free())
 • current treatment of lossy sources is less than ideal
 • can only serve .wav and raw pcm files (no flac encoder)
+• everything will get resampled to 44100kHz/16bit/2channel
+• the codebase is a mess
--- a/cue.c	Wed Dec 23 19:04:34 2020
+++ b/cue.c	Sat Dec 26 13:51:28 2020
@@ -4,29 +4,34 @@
 #include "cuefs.h"
 #include "y.tab.h"
 
+/*
+ * minute:second:frames → Timestamp
+ * 1 frame = 1/75 second
+ */
 Timestamp
 parsetime(int min, int sec, int frames)
 {
-	debug("parsing %d:%d:%d into ", min, sec, frames);
 	sec += min*60;
 	frames += sec*75;
-	debug("%d frames\n", frames);
 	return (Timestamp){frames};
 }
 
 double
 t2sec(Timestamp t)
 {
-	debug("returning %fs for %ud frames\n", t.frames/75.0, t.frames);
 	return t.frames/75.0;
 }
 
+/*
+ * offset (in bytes) → sec, with 
+ * rate = sample rate, size = sample size (bits), chans = channels
+ */
 double
 of2sec(uint rate, uint size, uint chans, vlong offset)
 {
 	int bs;
 	bs = (size/8)*chans;
-	return (double)offset/(double)bs/(double)rate;
+	return (double)offset/bs/rate;
 }
 
 Cuesheet*
@@ -36,44 +41,46 @@
 }
 
 void
-maybefree(void *s, void *p)
+freesheet(Cuesheet *s)
 {
-	if(p != nil && p != s)
-		free(p);
-}
+	Entry *e;
+	AFile *f;
 
-void
-recfreeentries(Cuesheet *s, Entry *cur)
-{
-	if(cur == nil)
-		return;
-	recfreeentries(s, cur->next);
-	for(Start *s = cur->starts; s != nil; s = cur->starts)
+	/* free entries */
+	while((e = s->entries) != nil)
 	{
-		cur->starts = s->next;
-		free(s);
+		s->entries = e->next;
+
+		/* free starts */
+		for(Start *st = e->starts; st != nil; st = e->starts)
+		{
+			e->starts = st->next;
+			free(st);
+		}
+		free(e->title);
+		/*
+		 * TODO maybe we should keep track of every performer in
+		 * the Cuesheet, even if they're "song only" performers?
+		 */
+		if(e->performer != s->performer)
+			free(e->title);
+
+		free(e);
 	}
-	/* file should get freed by the cuesheet */
-	maybefree(nil, cur->title);
-	maybefree(s->performer, cur->performer);
-}
 
-void
-recfreefiles(Cuesheet *s, AFile *cur)
-{
-	if(cur == nil)
-		return;
-	recfreefiles(s, cur->next);
-	maybefree(nil, cur->name);
-}
+	/* free files */
+	while((f = s->files) != nil)
+	{
+		s->files = f->next;
 
-void
-freesheet(Cuesheet *s)
-{
-	recfreeentries(s, s->entries);
-	recfreefiles(s, s->files);
-	maybefree(nil, s->title);
-	maybefree(nil, s->performer);
+		free(f->name);
+		free(f);
+	}
+
+	free(s->title);
+	free(s->performer);
+
+	free(s);
 }
 
 AFile*
@@ -111,9 +118,16 @@
 {
 	artist = strdup(artist);
 	if(c->curentry == nil)
-		setstr(nil, &c->performer, artist);
+	{
+		free(c->performer);
+		c->performer = artist;
+	}
 	else
-		setstr(c->performer, &c->curentry->performer, artist);
+	{
+		if(c->curentry->performer != c->performer)
+			free(c->curentry->performer);
+		c->curentry->performer = artist;
+	}
 }
 
 void
@@ -121,9 +135,15 @@
 {
 	title = strdup(title);
 	if(c->curentry == nil)
-		setstr(nil, &c->title, title);
+	{
+		free(c->title);
+		c->title = title;
+	}
 	else
-		setstr(nil, &c->curentry->title, title);
+	{
+		free(c->curentry->title);
+		c->curentry->title = title;
+	}
 }
 
 void
@@ -131,8 +151,6 @@
 {
 	AFile *new;
 
-	lastfile(c);
-
 	new = emalloc(sizeof(*new));
 	new->name	= strdup(name);
 	new->type	= format;
@@ -142,7 +160,7 @@
 	if(c->files == nil)
 		c->files = new;
 	else
-		c->curfile->next = new;
+		lastfile(c)->next = new;
 
 	c->curfile = new;
 }
@@ -152,8 +170,6 @@
 {
 	Entry *new;
 
-	lastentry(c);
-
 	new = emallocz(sizeof(*new), 1);
 	new->file = c->curfile;
 	new->performer = c->performer;
@@ -162,7 +178,7 @@
 	if(c->entries == nil)
 		c->entries = new;
 	else
-		c->curentry->next = new;
+		lastentry(c)->next = new;
 
 	c->curentry = new;
 }
@@ -197,11 +213,11 @@
 char*
 extension(char *f)
 {
-	char *ext = "";
+	char *ext;
 
-	for(char *c = f; *c != 0; c++)
-		if(*c == '.')
-			ext = c+1;
+	for(; *f != 0; f++)
+		if(*f == '.')
+			ext = f+1;
 
 	return ext;
 }