shithub: dumb

Download patch

ref: beea1d9f5c32609b8b48859f018a4225b1ab29ab
parent: a79a9725a9ed45f80d0c9ecf6fc1697219937dd9
author: Chris Moeller <kode54@gmail.com>
date: Mon Jan 14 15:11:28 EST 2013

More fixes for seeking support

--- a/dumb/src/it/itread.c
+++ b/dumb/src/it/itread.c
@@ -1352,7 +1352,7 @@
         }
 
         mptx_id = dumbfile_igetl( f );
-        while ( dumbfile_pos(f) < dumbfile_get_size(f) )
+        while ( !dumbfile_error(f) && dumbfile_pos(f) < dumbfile_get_size(f) )
         {
             unsigned int size = dumbfile_igetw( f );
             switch (mptx_id)
--- a/dumb/src/it/readam.c
+++ b/dumb/src/it/readam.c
@@ -166,7 +166,7 @@
     start = dumbfile_pos( f );
     end = start + len;
 
-    while ( (row < nrows) && (dumbfile_pos( f ) < end) ) {
+    while ( (row < nrows) && !dumbfile_error( f ) && (dumbfile_pos( f ) < end) ) {
         p = dumbfile_getc( f );
         if ( ! p ) {
 			++ row;
@@ -196,7 +196,7 @@
 
     dumbfile_seek( f, start, DFS_SEEK_SET );
 
-    while ( ( row < nrows ) && ( dumbfile_pos( f ) < end ) )
+    while ( ( row < nrows ) && !dumbfile_error( f ) && ( dumbfile_pos( f ) < end ) )
 	{
         p = dumbfile_getc( f );
 
--- a/dumb/src/it/readdsmf.c
+++ b/dumb/src/it/readdsmf.c
@@ -114,7 +114,7 @@
     start = dumbfile_pos( f );
     end = start + len;
 
-    while ( (row < 64) && (dumbfile_pos( f ) < end) ) {
+    while ( (row < 64) && !dumbfile_error( f ) && (dumbfile_pos( f ) < end) ) {
         p = dumbfile_getc( f );
         if ( ! p ) {
 			++ row;
@@ -143,7 +143,7 @@
 
     if ( dumbfile_seek( f, start, DFS_SEEK_SET ) ) return -1;
 
-    while ( ( row < 64 ) && ( dumbfile_pos( f ) < end ) )
+    while ( ( row < 64 ) && !dumbfile_error( f ) && ( dumbfile_pos( f ) < end ) )
 	{
         p = dumbfile_getc( f );
         if ( ! p )
--- a/dumb/src/it/readmod.c
+++ b/dumb/src/it/readmod.c
@@ -466,7 +466,8 @@
         long total_sample_size;
         long offset = dumbfile_pos(f);
         long remain = dumbfile_get_size(f) - offset;
-        if ( dumbfile_seek(f, 0, SEEK_END) ) {
+        if ( dumbfile_error( f ) ||
+             dumbfile_seek( f, 0, SEEK_END ) ) {
             _dumb_it_unload_sigdata(sigdata);
             return NULL;
         }
--- a/dumb/src/it/readoldpsm.c
+++ b/dumb/src/it/readoldpsm.c
@@ -33,12 +33,13 @@
 	return a - b;
 }
 
-static int it_old_psm_read_samples(IT_SAMPLE ** sample, DUMBFILE * f, int * num, const unsigned char * prebuffer, long data_pos, long data_size)
+static int it_old_psm_read_samples(IT_SAMPLE ** sample, DUMBFILE * f, int * num)
 {
-	int n, o, pos, count = *num, true_num, snum, offset, flags, finetune, delta;
+    int n, o, count = *num, true_num, snum, offset, flags, finetune, delta;
 
-	unsigned char * buffer, * sbuffer = 0;
+    unsigned char * buffer;
 	const unsigned char * sdata;
+    long sample_bytes;
 
 	buffer = malloc(count * 64);
 	if (!buffer) goto error;
@@ -45,8 +46,6 @@
 
 	if (dumbfile_getnc(buffer, count * 64, f) < count * 64) goto error_fb;
 
-	pos = dumbfile_pos(f);
-
 	true_num = 0;
 
 	for (n = 0; n < count; n++) {
@@ -128,22 +127,12 @@
 		s->vibrato_waveform = IT_VIBRATO_SINE;
 		s->max_resampling_quality = -1;
 
-		s->data = malloc(s->length * ((flags & 4) ? 2 : 1));
+        sample_bytes = s->length * ((flags & 4) ? 2 : 1);
+        s->data = malloc(sample_bytes);
 		if (!s->data) goto error_fb;
 
-		if ((offset >= data_pos) &&
-			((offset + s->length * ((flags & 4) ? 2 : 1)) <= (data_pos + data_size))) {
-			sdata = prebuffer + offset - data_pos;
-		} else if (offset >= pos) {
-			if (dumbfile_skip(f, offset - pos)) goto error_fb;
-			pos = offset;
-			offset = s->length * ((flags & 4) ? 2 : 1);
-			sbuffer = malloc(offset);
-			if (!sbuffer) goto error_fb;
-			if (dumbfile_getnc(sbuffer, offset, f) < offset) goto error_fsb;
-			sdata = sbuffer;
-		} else
-			goto error_fb;
+        if (dumbfile_seek(f, offset, DFS_SEEK_SET) || dumbfile_getnc(s->data, sample_bytes, f) < sample_bytes) goto error_fb;
+        sdata = ( const unsigned char * ) s->data;
 
 		if (flags & 0x10) {
 			if (flags & 8) {
@@ -191,11 +180,6 @@
 				}
 			}
 		}
-
-		if (sbuffer) {
-			free(sbuffer);
-			sbuffer = 0;
-		}
 	}
 
 	free(buffer);
@@ -202,8 +186,6 @@
 
 	return 0;
 
-error_fsb:
-	if (sbuffer) free(sbuffer);
 error_fb:
 	free(buffer);
 error:
@@ -519,8 +501,6 @@
 {
 	DUMB_IT_SIGDATA *sigdata;
 
-	unsigned char * ptr = 0;
-
 	PSM_COMPONENT *component;
 	int n_components = 0;
 
@@ -627,34 +607,21 @@
 	for (n = 0; n < n_components; n++)
 	{
 		int o;
-		long data_pos, data_size;
 
-		/* Whee, sample data may be before the sample headers */
+        if ( dumbfile_seek(f, component[n].offset, DFS_SEEK_SET) ) goto error_fc;
 
-		data_pos = dumbfile_pos(f);
-		if (data_pos > component[n].offset) goto error_fc;
-
-		data_size = component[n].offset - data_pos;
-
-		if (data_size) {
-			ptr = malloc(data_size);
-			if (!ptr) goto error_fc;
-
-			if (dumbfile_getnc(ptr, data_size, f) < data_size) goto error_fp;
-		}
-
 		switch (component[n].type) {
 
 			case PSM_COMPONENT_ORDERS:
-				if (dumbfile_getnc(sigdata->order, sigdata->n_orders, f) < sigdata->n_orders) goto error_fp;
+                if (dumbfile_getnc(sigdata->order, sigdata->n_orders, f) < sigdata->n_orders) goto error_fc;
 				if (n_orders > sigdata->n_orders)
 					if (dumbfile_skip(f, n_orders - sigdata->n_orders))
-						goto error_fp;
-				if (dumbfile_igetw(f)) goto error_fp;
+                        goto error_fc;
+                if (dumbfile_igetw(f)) goto error_fc;
 				break;
 
 			case PSM_COMPONENT_PANPOS:
-				if (dumbfile_getnc(sigdata->channel_pan, sigdata->n_pchannels, f) < sigdata->n_pchannels) goto error_fp;
+                if (dumbfile_getnc(sigdata->channel_pan, sigdata->n_pchannels, f) < sigdata->n_pchannels) goto error_fc;
 				for (o = 0; o < sigdata->n_pchannels; o++) {
 					sigdata->channel_pan[o] -= (sigdata->channel_pan[o] & 8) >> 3;
 					sigdata->channel_pan[o] = ((int)sigdata->channel_pan[o] << 5) / 7;
@@ -662,11 +629,11 @@
 				break;
 
 			case PSM_COMPONENT_PATTERNS:
-				if (it_old_psm_read_patterns(sigdata->pattern, f, sigdata->n_patterns, total_pattern_size, sigdata->n_pchannels, flags)) goto error_fp;
+                if (it_old_psm_read_patterns(sigdata->pattern, f, sigdata->n_patterns, total_pattern_size, sigdata->n_pchannels, flags)) goto error_fc;
 				break;
 
 			case PSM_COMPONENT_SAMPLE_HEADERS:
-				if (it_old_psm_read_samples(&sigdata->sample, f, &sigdata->n_samples, ptr, data_pos, data_size)) goto error_fp;
+                if (it_old_psm_read_samples(&sigdata->sample, f, &sigdata->n_samples)) goto error_fc;
 				break;
 
 			case PSM_COMPONENT_COMMENTS:
@@ -674,17 +641,12 @@
 					o = dumbfile_igetw(f);
 					if (o > 0) {
 						sigdata->song_message = malloc(o + 1);
-						if (dumbfile_getnc(sigdata->song_message, o, f) < o) goto error_fp;
+                        if (dumbfile_getnc(sigdata->song_message, o, f) < o) goto error_fc;
 						sigdata->song_message[o] = 0;
 					}
 				}
 				break;
 		}
-
-		if (ptr) {
-			free(ptr);
-			ptr = 0;
-		}
 	}
 
 	_dumb_it_fix_invalid_orders(sigdata);
@@ -693,8 +655,6 @@
 
 	return sigdata;
 
-error_fp:
-	if (ptr) free(ptr);
 error_fc:
 	free(component);
 error_usd:
--- a/dumb/src/it/readptm.c
+++ b/dumb/src/it/readptm.c
@@ -26,23 +26,6 @@
 
 
 
-/** WARNING: this is duplicated in itread.c */
-static int it_seek(DUMBFILE *f, long offset)
-{
-	long pos = dumbfile_pos(f);
-
-	if (pos > offset)
-		return -1;
-
-	if (pos < offset)
-		if (dumbfile_skip(f, offset - pos))
-			return -1;
-
-	return 0;
-}
-
-
-
 static int it_ptm_read_sample_header(IT_SAMPLE *sample, long *offset, DUMBFILE *f)
 {
 	int flags;
@@ -455,7 +438,7 @@
 		return NULL;
 	}
 
-	if (it_seek(f, 352)) {
+    if (dumbfile_seek(f, 352, DFS_SEEK_SET)) {
 		_dumb_it_unload_sigdata(sigdata);
 		return NULL;
 	}
@@ -467,7 +450,7 @@
 		n_components++;
 	}
 
-	if (it_seek(f, 608)) {
+    if (dumbfile_seek(f, 608, DFS_SEEK_SET)) {
 		_dumb_it_unload_sigdata(sigdata);
 		return NULL;
 	}
@@ -510,7 +493,7 @@
 	}
 
 	for (n = 0; n < n_components; n++) {
-		if (it_seek(f, component[n].offset)) {
+        if (dumbfile_seek(f, component[n].offset, DFS_SEEK_SET)) {
 			free(buffer);
 			free(component);
 			_dumb_it_unload_sigdata(sigdata);
--- a/dumb/src/it/readstm.c
+++ b/dumb/src/it/readstm.c
@@ -83,7 +83,7 @@
 	return dumbfile_error(f);
 }
 
-static int it_stm_read_sample_data( IT_SAMPLE *sample, void *data_block, long offset )
+static int it_stm_read_sample_data( IT_SAMPLE *sample, DUMBFILE * f )
 {
 	if ( ! sample->length ) return 0;
 
@@ -91,9 +91,9 @@
 	if (!sample->data)
 		return -1;
 
-	memcpy( sample->data, (unsigned char*)data_block + offset, sample->length );	
+    dumbfile_getnc( sample->data, sample->length, f );
 
-	return 0;
+    return dumbfile_error( f );
 }
 
 static int it_stm_read_pattern( IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer )
@@ -192,10 +192,8 @@
 
 	unsigned short sample_offset[ 31 ];
 
-	void *data_block;
+    int n;
 
-	int n;
-
 	long o, p, q;
 
 	sigdata = malloc(sizeof(*sigdata));
@@ -331,60 +329,21 @@
 		free( buffer );
 	}
 
-	o = LONG_MAX;
-	p = 0;
-
-	for ( n = 0; n < sigdata->n_samples; ++n ) {
-		if ((sigdata->sample[ n ].flags & IT_SAMPLE_EXISTS) && sample_offset[ n ]) {
-			q = ((long)sample_offset[ n ]) * 16;
-			if (q < o) {
-				o = q;
-			}
-			if (q + sigdata->sample[ n ].length > p) {
-				p = q + sigdata->sample[ n ].length;
-			}
-		}
-		else {
-			sigdata->sample[ n ].flags = 0;
-			sigdata->sample[ n ].length = 0;
-		}
-	}
-
-	data_block = malloc( p - o );
-	if ( !data_block ) {
-		_dumb_it_unload_sigdata( sigdata );
-		return NULL;
-	}
-
-	for ( n = 0, q = o / 16; n < sigdata->n_samples; ++n ) {
-		if ( sample_offset[ n ] ) {
-			sample_offset[ n ] -= q;
-		}
-	}
-
-	q = o - dumbfile_pos( f );
-	p -= o;
-	o = 0;
-	if ( q >= 0 ) dumbfile_skip( f, q );
-	else {
-		o = -q;
-		memset ( data_block, 0, o );
-	}
-	if ( dumbfile_getnc( (char*)data_block + o, p - o, f ) != p - o ) {
-		free( data_block );
-		_dumb_it_unload_sigdata( sigdata );
-		return NULL;
-	}
-
-	for ( n = 0; n < sigdata->n_samples; ++n ) {
-		if ( it_stm_read_sample_data( &sigdata->sample[ n ], data_block, ((long)sample_offset[ n ]) * 16 ) ) {
-			free( data_block );
-			_dumb_it_unload_sigdata( sigdata );
-			return NULL;
-		}
-	}
-
-	free( data_block );
+    for ( n = 0; n < sigdata->n_samples; ++n ) {
+        if ( sample_offset[ n ] )
+        {
+            if ( dumbfile_seek( f, sample_offset[ n ] * 16, DFS_SEEK_SET ) ||
+                 it_stm_read_sample_data( &sigdata->sample[ n ], f ) ) {
+                _dumb_it_unload_sigdata( sigdata );
+                return NULL;
+            }
+        }
+        else
+        {
+            sigdata->sample[ n ].flags = 0;
+            sigdata->sample[ n ].length = 0;
+        }
+    }
 
 	_dumb_it_fix_invalid_orders(sigdata);