From 8a04aa20705b86fb682447e874d6acb571a5ab53 Mon Sep 17 00:00:00 2001 From: NIFTI Upstream Date: Mon, 22 May 2023 10:58:55 -0500 Subject: [PATCH] nifti 2023-05-22 (c135c3d4) Code extracted from: https://github.com/NIFTI-Imaging/nifti_clib.git at commit c135c3d474c2db97c071e184d91460ce8b0027da (master). --- niftilib/nifti1_io.c | 35 +++++++++--------- niftilib/nifti1_test.c | 7 ++-- niftilib/nifti1_tool.c | 81 +++++++++++++++++++++--------------------- 3 files changed, 64 insertions(+), 59 deletions(-) diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index cc3382b8a21..95e6c511bd2 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -4396,6 +4396,7 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain ) nifti1_extender extdr; /* defines extension existence */ nifti1_extension extn; /* single extension to process */ nifti1_extension * Elist; /* list of processed extensions */ + znz_off_t posn; int count; if( !nim || znz_isnull(fp) ) { @@ -4405,16 +4406,17 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain ) return -1; } - znz_off_t posn = znztell(fp); + posn = znztell(fp); if( (posn != sizeof(nifti_1_header)) && (nim->nifti_type != NIFTI_FTYPE_ASCII) ) fprintf(stderr,"** WARNING: posn not header size (%lld, %lu)\n", - posn, sizeof(nifti_1_header)); + (long long)posn, sizeof(nifti_1_header)); if( g_opts.debug > 2 ) - fprintf(stderr,"-d nre: posn = %lld, offset = %d, type = %d, remain = %d\n", - posn, nim->iname_offset, nim->nifti_type, remain); + fprintf(stderr,"-d nre: posn = %lld, offset = %d, type = %d," + " remain = %d\n", + (long long)posn, nim->iname_offset, nim->nifti_type, remain); if( remain < 16 ){ if( g_opts.debug > 2 ){ @@ -6928,7 +6930,7 @@ int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], ** stride array. */ static void -compute_strides(int *strides,const int *size,int nbyper) +compute_strides(int64_t *strides,const int *size,int nbyper) { int i; strides[0] = nbyper; @@ -6977,7 +6979,7 @@ int nifti_read_subregion_image( nifti_image * nim, long int bytes = 0; /* total # bytes read */ size_t total_alloc_size; /* size of buffer allocation */ char *readptr; /* where in *data to read next */ - int strides[7]; /* strides between dimensions */ + int64_t strides[7]; /* strides between dimensions */ int collapsed_dims[8]; /* for read_collapsed_image */ int *image_size; /* pointer to dimensions in header */ long int initial_offset; @@ -7343,10 +7345,11 @@ static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[], *//*-------------------------------------------------------------------*/ int * nifti_get_intlist( int nvals , const char * str ) { - int *subv = NULL ; - int *subv_realloc = NULL; - int ii , ipos , nout , slen ; - int ibot,itop,istep ; + int *subv = NULL ; + int *subv_realloc = NULL; + int ii , ipos , nout , slen ; + int ibot,itop,istep ; + long nused, temp ; char *cpt ; /* Meaningless input? */ @@ -7383,7 +7386,7 @@ int * nifti_get_intlist( int nvals , const char * str ) ibot = nvals-1 ; ipos++ ; } else { /* decode an integer */ errno = 0; - long temp = strtol( str+ipos , &cpt , 10 ) ; + temp = strtol( str+ipos , &cpt , 10 ) ; if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){ fprintf(stderr,"** ERROR: list index does not fit in int\n") ; free(subv) ; return NULL ; @@ -7399,7 +7402,7 @@ int * nifti_get_intlist( int nvals , const char * str ) ibot,nvals-1) ; free(subv) ; return NULL ; } - long nused = (cpt-(str+ipos)) ; + nused = (cpt-(str+ipos)) ; if( ibot == 0 && nused == 0 ){ fprintf(stderr,"** ERROR: list syntax error '%s'\n",str+ipos) ; free(subv) ; return NULL ; @@ -7446,7 +7449,7 @@ int * nifti_get_intlist( int nvals , const char * str ) itop = nvals-1 ; ipos++ ; } else { /* decode an integer */ errno = 0; - long temp = strtol( str+ipos , &cpt , 10 ) ; + temp = strtol( str+ipos , &cpt , 10 ) ; if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){ fprintf(stderr,"** ERROR: list index does not fit in int\n") ; free(subv) ; return NULL ; @@ -7462,7 +7465,7 @@ int * nifti_get_intlist( int nvals , const char * str ) itop,nvals-1) ; free(subv) ; return NULL ; } - long nused = (cpt-(str+ipos)) ; + nused = (cpt-(str+ipos)) ; if( itop == 0 && nused == 0 ){ fprintf(stderr,"** ERROR: index list syntax error '%s'\n",str+ipos) ; free(subv) ; return NULL ; @@ -7481,7 +7484,7 @@ int * nifti_get_intlist( int nvals , const char * str ) if( str[ipos] == '(' ){ /* decode an integer */ ipos++ ; errno = 0; - long temp = strtol( str+ipos , &cpt , 10 ) ; + temp = strtol( str+ipos , &cpt , 10 ) ; if( (temp == 0 && errno != 0) || temp <= INT_MIN || temp >= INT_MAX){ fprintf(stderr,"** ERROR: list index does not fit in int\n") ; free(subv) ; return NULL ; @@ -7491,7 +7494,7 @@ int * nifti_get_intlist( int nvals , const char * str ) fprintf(stderr,"** ERROR: index loop step is 0!\n") ; free(subv) ; return NULL ; } - long nused = (cpt-(str+ipos)) ; + nused = (cpt-(str+ipos)) ; ipos += nused ; if( str[ipos] == ')' ) ipos++ ; if( (ibot-itop)*istep > 0 ){ diff --git a/niftilib/nifti1_test.c b/niftilib/nifti1_test.c index 96b29156e4c..e30e791a67e 100644 --- a/niftilib/nifti1_test.c +++ b/niftilib/nifti1_test.c @@ -17,8 +17,9 @@ static int local_fileexists(const char* fname) int main( int argc , char *argv[] ) { nifti_image *nim ; - int iarg=1 , outmode=1 , argn=1, usegzip=0; - char *tmpstr; + int iarg=1 , outmode=1 , argn=1, usegzip=0; + char *tmpstr; + size_t ll; if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ printf("Usage: nifti1_test [-n2|-n1|-na|-a2] infile [prefix]\n" @@ -78,7 +79,7 @@ int main( int argc , char *argv[] ) if( nim->fname != NULL ) free(nim->fname) ; if( nim->iname != NULL ) free(nim->iname) ; - size_t ll = strlen(argv[iarg]) ; + ll = strlen(argv[iarg]) ; tmpstr = nifti_makebasename(argv[iarg]); nim->fname = (char *)calloc(1,ll+8) ; strcpy(nim->fname,tmpstr) ; nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ; diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index b00aced2650..32fb439c5e8 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -172,7 +172,7 @@ static int g_debug = 1; /* local prototypes */ static int free_opts_mem(nt_opts * nopt); static int num_volumes(nifti_image * nim); -static char * read_file_text(const char * filename, int64_t * length); +static char * read_file_text(const char * filename, int * length); #define NTL_FERR(func,msg,file) \ fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file) @@ -689,13 +689,14 @@ int verify_opts( nt_opts * opts, char * prog ) int fill_cmd_string( nt_opts * opts, int argc, char * argv[]) { char * cp; - size_t remain = sizeof(opts->command); /* max command len */ + int len, remain = (int)sizeof(opts->command); /* max command len */ int c, ac; int has_space; /* arguments containing space must be quoted */ int skip = 0; /* counter to skip some of the arguments */ /* get the first argument separately */ - int len = snprintf( opts->command, sizeof(opts->command), "\n command: %s", argv[0] ); + len = snprintf( opts->command, sizeof(opts->command), + "\n command: %s", argv[0] ); if( len < 0 || len >= (int)sizeof(opts->command) ) { fprintf(stderr,"FCS: no space remaining for command, continuing...\n"); return 1; @@ -708,7 +709,7 @@ int fill_cmd_string( nt_opts * opts, int argc, char * argv[]) { if( skip ){ skip--; continue; } /* then skip these arguments */ - size_t len = strlen(argv[ac]); + len = (int)strlen(argv[ac]); if( len + 3 >= remain ) { /* extra 3 for space and possible '' */ fprintf(stderr,"FCS: no space remaining for command, continuing...\n"); return 1; @@ -1824,8 +1825,7 @@ int act_add_exts( nt_opts * opts ) nifti_image * nim; const char * ext; char * edata = NULL; - int fc, ec; - int64_t elen; + int fc, ec, elen; if( g_debug > 2 ){ fprintf(stderr,"+d adding %d extensions to %d files...\n" @@ -1849,10 +1849,10 @@ int act_add_exts( nt_opts * opts ) for( ec = 0; ec < opts->elist.len; ec++ ){ ext = opts->elist.list[ec]; - elen = strlen(ext); + elen = (int)strlen(ext); if( !strncmp(ext,"file:",5) ){ edata = read_file_text(ext+5, &elen); - if( !edata || elen <= 0 || elen > INT_MAX) { + if( !edata || elen <= 0 ) { fprintf(stderr,"** failed to read extension data from '%s'\n", ext+5); continue; @@ -1860,7 +1860,7 @@ int act_add_exts( nt_opts * opts ) ext = edata; } - if( nifti_add_extension(nim, ext, (int)elen, opts->etypes.list[ec]) ){ + if( nifti_add_extension(nim, ext, elen, opts->etypes.list[ec]) ){ nifti_image_free(nim); return 1; } @@ -1904,20 +1904,24 @@ int act_add_exts( nt_opts * opts ) /*---------------------------------------------------------------------- * Return the allocated file contents. *----------------------------------------------------------------------*/ -static char * read_file_text(const char * filename, int64_t * length) +static char * read_file_text(const char * filename, int * length) { + FILE * fp; + char * text; + int len, bytes; + if( !filename || !length ) { fprintf(stderr,"** bad params to read_file_text\n"); return NULL; } - int64_t len = nifti_get_filesize(filename); + len = nifti_get_filesize(filename); if( len <= 0 ) { fprintf(stderr,"** RFT: file '%s' appears empty\n", filename); return NULL; } - FILE * fp = fopen(filename, "r"); + fp = fopen(filename, "r"); if( !fp ) { fprintf(stderr,"** RFT: failed to open '%s' for reading\n", filename); return NULL; @@ -1925,18 +1929,18 @@ static char * read_file_text(const char * filename, int64_t * length) /* allocate the bytes, and fill them with the file contents */ - char * text = (char *)malloc(len * sizeof(char)); + text = (char *)malloc(len * sizeof(char)); if( !text ) { - fprintf(stderr,"** RFT: failed to allocate %lld bytes\n", len); + fprintf(stderr,"** RFT: failed to allocate %d bytes\n", len); fclose(fp); return NULL; } - size_t bytes = fread(text, sizeof(char), len, fp); + bytes = (int)fread(text, sizeof(char), len, fp); fclose(fp); /* in any case */ if( bytes != len ) { - fprintf(stderr,"** RFT: read only %zu of %lld bytes from %s\n", + fprintf(stderr,"** RFT: read only %d of %d bytes from %s\n", bytes, len, filename); free(text); return NULL; @@ -1945,7 +1949,7 @@ static char * read_file_text(const char * filename, int64_t * length) /* success */ if( g_debug > 1 ) { - fprintf(stderr,"++ found extension of length %lld in file %s\n", + fprintf(stderr,"++ found extension of length %d in file %s\n", len, filename); if( g_debug > 2 ) fprintf(stderr,"++ text is:\n%s\n", text); @@ -2888,14 +2892,14 @@ int modify_all_fields( void * basep, nt_opts * opts, field_s * fields, int flen) *----------------------------------------------------------------------*/ int modify_field(void * basep, field_s * field, const char * data) { + float fval; const char * posn = data; int val, max, fc, nchars; if( g_debug > 1 ) fprintf(stderr,"+d modifying field '%s' with '%s'\n", field->name, data); - size_t dataLength = strlen(data); - if( !data || dataLength == 0 ) + if( !data || strlen(data) == 0 ) { fprintf(stderr,"** no data for '%s' field modification\n",field->name); return 1; @@ -2914,7 +2918,7 @@ int modify_field(void * basep, field_s * field, const char * data) case DT_INT8: { - max = INT8_MAX; + max = 127; for( fc = 0; fc < field->len; fc++ ) { if( sscanf(posn, " %d%n", &val, &nchars) != 1 ) @@ -2942,7 +2946,7 @@ int modify_field(void * basep, field_s * field, const char * data) case DT_INT16: { - max = INT16_MAX; + max = 32767; for( fc = 0; fc < field->len; fc++ ) { if( sscanf(posn, " %d%n", &val, &nchars) != 1 ) @@ -2959,9 +2963,7 @@ int modify_field(void * basep, field_s * field, const char * data) return 1; } /* otherwise, we're good */ - char * offsetp = &(((char *)basep + field->offset)[fc]); - int16_t sval = (int16_t)val; - memcpy(offsetp, &sval, sizeof(sval)); + ((short *)((char *)basep + field->offset))[fc] = (short)val; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -2980,9 +2982,7 @@ int modify_field(void * basep, field_s * field, const char * data) fc,field->len); return 1; } - char * offsetp = &(((char *)basep + field->offset)[fc]); - int32_t ival = (int32_t)val; - memcpy(offsetp, &ival, sizeof(ival)); + ((int *)((char *)basep + field->offset))[fc] = val; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %d\n", fc, field->name, val); @@ -2993,21 +2993,19 @@ int modify_field(void * basep, field_s * field, const char * data) case DT_FLOAT32: { - float f32; for( fc = 0; fc < field->len; fc++ ) { - if( sscanf(posn, " %f%n", &f32, &nchars) != 1 ) + if( sscanf(posn, " %f%n", &fval, &nchars) != 1 ) { fprintf(stderr,"** found %d of %d modify values\n", fc,field->len); return 1; } /* otherwise, we're good */ - char * offsetp = &(((char *)basep + field->offset)[fc]); - memcpy(offsetp, &f32, sizeof(f32)); + ((float *)((char *)basep + field->offset))[fc] = fval; if( g_debug > 1 ) fprintf(stderr,"+d setting posn %d of '%s' to %f\n", - fc, field->name, f32); + fc, field->name, fval); posn += nchars; } } @@ -3016,9 +3014,10 @@ int modify_field(void * basep, field_s * field, const char * data) case NT_DT_STRING: { char * dest = (char *)basep + field->offset; + nchars = strlen(data); strncpy(dest, data, field->len); - if( dataLength < field->len ) /* clear the rest */ - memset(dest+dataLength, '\0', field->len - dataLength); + if( nchars < field->len ) /* clear the rest */ + memset(dest+nchars, '\0', field->len-nchars); } break; } @@ -3832,10 +3831,10 @@ int act_disp_ci( nt_opts * opts ) return 0; } - +#define NT_LOC_MAX_FLOAT_BUF 32 int disp_raw_data( void * data, int type, int nvals, char space, int newline ) { - char * dp, fbuf[32]; + char * dp, fbuf[NT_LOC_MAX_FLOAT_BUF]; int c, size; nifti_datatype_sizes( type, &size, NULL ); /* get nbyper */ @@ -3886,7 +3885,7 @@ int disp_raw_data( void * data, int type, int nvals, char space, int newline ) { float temp; memcpy(&temp, dp, sizeof(temp)); - snprintf(fbuf,sizeof(fbuf),"%f", temp); + snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); clear_float_zeros(fbuf); printf("%s", fbuf); break; @@ -3895,7 +3894,7 @@ int disp_raw_data( void * data, int type, int nvals, char space, int newline ) { double temp; memcpy(&temp, dp, sizeof(temp)); - snprintf(fbuf,sizeof(fbuf),"%f", temp); + snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); clear_float_zeros(fbuf); printf("%s", fbuf); break; @@ -3914,16 +3913,18 @@ int disp_raw_data( void * data, int type, int nvals, char space, int newline ) /*---------------------------------------------------------------------- * remove trailing zeros from string of printed float + * (of a max NT_LOC_MAX_FLOAT_BUF (32) string) * return 1 if something was cleared * 0 if not *----------------------------------------------------------------------*/ int clear_float_zeros( char * str ) { - char * dp = strchr(str, '.'), * valp; + char * dp = strchr(str, '.'), * valp; + size_t len; if( !dp ) return 0; /* nothing to clear */ - size_t len = strlen(dp); + len = strlen(dp); /* never clear what is just to the right of '.' */ for( valp = dp+len-1; (valp > dp+1) && (*valp==' ' || *valp=='0'); valp-- )