From: Ralph Ronnquist Date: Sun, 17 Jul 2022 02:54:02 +0000 (+1000) Subject: polishes range handling X-Git-Tag: 0.4~4 X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=7059a8375f259a9d739fe6a642d0162ee83c30c8;p=rrq%2Ffusefile.git polishes range handling --- diff --git a/fusefile.c b/fusefile.c index 5a51066..9c4b964 100644 --- a/fusefile.c +++ b/fusefile.c @@ -63,8 +63,8 @@ static void print_source(struct Source *p) { static char *range; static unsigned int c; -static int RANGE(int SCAN,int N ) { - return ( SCAN == N ) && *(range+c) == 0; +static int RANGE(int s,int n ) { + return ( s == n ) && *(range+c) == 0; } static int setup_sources(char **argv,int i,int n) { @@ -102,28 +102,39 @@ static int setup_sources(char **argv,int i,int n) { p->to = filestat.st_size; // Process any range variation if ( range && *(++range) ) { - unsigned int a,b; - if ( RANGE( sscanf( range, "%u-%u%n", &a, &b, &c ), 2 ) ) { - p->from = a; - p->to = b; - } else if ( RANGE( sscanf( range, "%u--%u%n", &a, &b, &c ), 2 ) ) { - p->from = a; - p->to -= b; - } else if ( RANGE( sscanf( range, "%u-%n", &a, &c ), 1 ) ) { - p->from = a; - } else if ( RANGE( sscanf( range, "%u%n", &a, &c ), 1 ) ) { - p->from = a; - } else if ( RANGE( sscanf( range, "-%u%n", &b, &c ), 1 ) ) { - p->to = b; - } else if ( RANGE( sscanf( range, "--%u%n", &b, &c ), 1 ) ) { - p->to -= b; - } else if ( RANGE( sscanf( range, "-%n", &c), 0 ) ) { - // Acceptable as "start-end" range + int a,b; + if ( 0 ) { + } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) { + p->from = ( a < 0 )? ( p->to + a ) : a; + p->to = ( b < 0 )? ( p->to + b ) : b; + } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) { + p->from = ( a < 0 )? ( p->to + a ) : a; + p->to = ( ( b < 0 )? p->to : p->from ) + b; + } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) { + p->from = ( a < 0 )? ( p->to + a ) : a; + } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) { + p->to = ( b < 0 )? ( p->to + b ) : b; + } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) { + p->from = ( a < 0 )? ( p->to + a ) : a; + } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) { + if ( a < 0 ) { + p->from = p->to + a; + } else { + p->to = p->from + a; + } + } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) { + // to end from start } else { fprintf( stderr, "** BAD RANGE: %s\n", argv[i] ); return 1; } } + if ( p->from >= p->to || + p->from >= filestat.st_size || p->to > filestat.st_size ) { + fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n", + argv[i], p->from, p->to ); + return 1; + } p->start = sources.size; // the fusefile position of fragment sources.size += p->to - p->from; #if DEBUG