Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y cmake valgrind help2man
sudo apt-get install -y cmake valgrind help2man clang-tools

- name: Install Dependencies (macOS)
if: runner.os == 'macOS'
run: |
brew install cmake sed help2man
brew install cmake gnu-sed help2man

- name: Set Compiler and Flags
run: |
Expand Down
3 changes: 2 additions & 1 deletion cifti/afni_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ static int64_t loc_strnlen(const char * str, int64_t maxlen)
const char * sptr;
int64_t len;

for( sptr=str, len=0; *sptr && len<maxlen; sptr++, len++)
/* test the length first: str need not be terminated within maxlen */
for( sptr=str, len=0; len<maxlen && *sptr; sptr++, len++)
;

return len; /* max of maxlen */
Expand Down
177 changes: 106 additions & 71 deletions nifti2/nifti2_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -3767,7 +3767,7 @@ char * nifti_findhdrname(const char* fname)
if (nifti_fileexists(gzname)) {
fprintf(stderr,"Image Exception : Multiple possible filenames detected for basename (*.nii, *.nii.gz): %s\n", basename);
free(gzname);
exit(134);
return NULL;
}
free(gzname);
return hdrname;
Expand Down Expand Up @@ -4744,7 +4744,10 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname)
}
if( doswap ) {
if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr);
swap_nifti_header( &nhdr , ni_ver ) ;
/* nhdr is a nifti_1_header, so swap it as one. ni_ver comes from the
magic string, and a magic of "n+2" would otherwise have
swap_nifti_header() treat these 348 bytes as a 540 byte header. */
swap_nifti_header( &nhdr , ni_ver ? 1 : 0 ) ;
}

if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr);
Expand Down Expand Up @@ -4807,8 +4810,14 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname)
nim->nv = nim->dim[6] = nhdr.dim[6];
nim->nw = nim->dim[7] = nhdr.dim[7];

for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ )
/* the product of the dimensions becomes an allocation size, so refuse
the header rather than let it wrap */
for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){
if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){
free(nim); ERREX("dim[] overflows the voxel count");
}
nim->nvox *= nhdr.dim[ii];
}

/**- set the type of data in voxels and how many bytes per voxel */

Expand All @@ -4817,6 +4826,11 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname)
nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ;
if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); }

/* nifti_get_volsize() multiplies these two */
if( nim->nvox > INT64_MAX / nim->nbyper ){
free(nim); ERREX("dim[] and datatype overflow the volume size");
}

/**- set the grid spacings */

nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ;
Expand Down Expand Up @@ -5036,6 +5050,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname)
ERREX("bad datatype") ;
}

/* dim[0] is the number of dimensions and the loops below index dim[]
with it; the NIFTI-1 path gets this check from need_nhdr_swap() */
if( nhdr.dim[0] < 0 || nhdr.dim[0] > 7 )
{
free(nim);
ERREX("bad dim[0]") ;
}

if( nhdr.dim[1] <= 0 )
{
free(nim);
Expand Down Expand Up @@ -5077,8 +5099,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname)
nim->nv = nim->dim[6] = nhdr.dim[6];
nim->nw = nim->dim[7] = nhdr.dim[7];

for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ )
/* the product of the dimensions becomes an allocation size, so refuse
the header rather than let it wrap */
for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){
if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){
free(nim); ERREX("dim[] overflows the voxel count");
}
nim->nvox *= nhdr.dim[ii];
}

/**- set the type of data in voxels and how many bytes per voxel */

Expand All @@ -5087,6 +5115,11 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname)
nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ;
if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); }

/* nifti_get_volsize() multiplies these two */
if( nim->nvox > INT64_MAX / nim->nbyper ){
free(nim); ERREX("dim[] and datatype overflow the volume size");
}

/**- set the grid spacings */

nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ;
Expand Down Expand Up @@ -5367,7 +5400,9 @@ nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check)

if( lswap ) {
if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr);
swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ;
/* only sizeof(nifti_1_header) bytes were read, so swap as that; see
the same guard in nifti_convert_n1hdr2nim() */
swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ? 1 : 0 ) ;
}

if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr);
Expand Down Expand Up @@ -5985,8 +6020,8 @@ nifti_image *nifti_image_read( const char *hname , int read_data )

#ifdef REJECT_COMPLEX
if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) {
fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname);
exit(13);
fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX): use fslcomplex to manipulate: %s\n", hname);
return NULL;
}
#endif

Expand Down Expand Up @@ -7859,68 +7894,68 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts,

#ifdef PIGZ
#ifdef HAVE_ZLIB
int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) {
FILE *pigzPipe;
char command[768];
strcpy(command, "pigz" );
strcat(command, " -n -f > \"");
strcat(command, nim->fname);
strcat(command, "\"");
#ifdef _MSC_VER
if (( pigzPipe = _popen(command, "w")) == NULL)
return -1;
#else
if (( pigzPipe = popen(command, "w")) == NULL)
return -1;
#endif
znzFile fp;
fp = (znzFile) calloc(1,sizeof(struct znzptr));
fp->zfptr = NULL;
fp->withz = 0;
fp->nzfptr = pigzPipe;
fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe);
if( nim->nifti_type != NIFTI_FTYPE_ANALYZE )
nifti_write_extensions(fp,nim);
nifti_write_all_data(fp,nim,NBL);
#ifdef _MSC_VER
_pclose(pigzPipe);
#else
pclose(pigzPipe);
#endif
free(fp);
return 0;
}

int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) {
FILE *pigzPipe;
char command[768];
strcpy(command, "pigz" );
strcat(command, " -n -f > \"");
strcat(command, nim->fname);
strcat(command, "\"");
#ifdef _MSC_VER
if (( pigzPipe = _popen(command, "w")) == NULL)
return -1;
#else
if (( pigzPipe = popen(command, "w")) == NULL)
return -1;
#endif
znzFile fp;
fp = (znzFile) calloc(1,sizeof(struct znzptr));
fp->zfptr = NULL;
fp->withz = 0;
fp->nzfptr = pigzPipe;
fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe);
if( nim->nifti_type != NIFTI_FTYPE_ANALYZE )
nifti_write_extensions(fp,nim);
nifti_write_all_data(fp,nim,NBL);
#ifdef _MSC_VER
_pclose(pigzPipe);
#else
pclose(pigzPipe);
#endif
free(fp);
return 0;
static int doPigz2(nifti_image * nim, const nifti_2_header * nhdr, const nifti_brick_list * NBL) {
FILE *pigzPipe;
char command[4096];
strcpy(command, "pigz" );
strcat(command, " -n -f > \"");
strcat(command, nim->fname);
strcat(command, "\"");
#ifdef _MSC_VER
if (( pigzPipe = _popen(command, "w")) == NULL)
return -1;
#else
if (( pigzPipe = popen(command, "w")) == NULL)
return -1;
#endif
znzFile fp;
fp = (znzFile) calloc(1, sizeof(struct znzptr));
fp->zfptr = NULL;
fp->withz = 0;
fp->nzfptr = pigzPipe;
fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe);
if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE )
nifti_write_extensions(fp, nim);
nifti_write_all_data(fp, nim, NBL);
#ifdef _MSC_VER
_pclose(pigzPipe);
#else
pclose(pigzPipe);
#endif
free(fp);
return 0;
}

static int doPigz(nifti_image * nim, const nifti_1_header * nhdr, const nifti_brick_list * NBL) {
FILE *pigzPipe;
char command[4096];
strcpy(command, "pigz" );
strcat(command, " -n -f > \"");
strcat(command, nim->fname);
strcat(command, "\"");
#ifdef _MSC_VER
if (( pigzPipe = _popen(command, "w")) == NULL)
return -1;
#else
if (( pigzPipe = popen(command, "w")) == NULL)
return -1;
#endif
znzFile fp;
fp = (znzFile) calloc(1, sizeof(struct znzptr));
fp->zfptr = NULL;
fp->withz = 0;
fp->nzfptr = pigzPipe;
fwrite(nhdr, sizeof(*nhdr), 1, pigzPipe);
if ( nim->nifti_type != NIFTI_FTYPE_ANALYZE )
nifti_write_extensions(fp, nim);
nifti_write_all_data(fp, nim, NBL);
#ifdef _MSC_VER
_pclose(pigzPipe);
#else
pclose(pigzPipe);
#endif
free(fp);
return 0;
}
#endif //HAVE_ZLIB
#endif //PIGZ
Expand Down Expand Up @@ -8043,10 +8078,10 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts,
char pigzKey[5] = "PIGZ";
if ((value != NULL) && (strstr(value,pigzKey))) {
if( nver == 2 ) {
if (doPigz2(nim, n2hdr, NBL) == 0)
if (doPigz2(nim, &n2hdr, NBL) == 0)
return 0;
} else {
if (doPigz(nim, n1hdr, NBL) == 0) //success writing with pigz
if (doPigz(nim, &n1hdr, NBL) == 0) //success writing with pigz
return 0;
}
}
Expand Down
18 changes: 14 additions & 4 deletions nifti2/nifti_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -3391,7 +3391,11 @@ int act_mod_hdrs( nt_opts * opts )
nifti_image_free(nim);
}
else if ( swap )
swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr));
/* nhdr is a nifti_1_header, so swap it as one. NIFTI_VERSION()
reads the magic string, and a magic of "n+2" would otherwise
have swap_nifti_header() treat these 348 bytes as a 540 byte
header. ni_ver 0 and 1 are both 348 byte layouts. */
swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0);

/* if all is well, overwrite header in fname dataset */
(void)write_hdr_to_file(nhdr, fname); /* errors printed in function */
Expand Down Expand Up @@ -3512,7 +3516,9 @@ int act_mod_hdr2s( nt_opts * opts )
nifti_image_free(nim);
}
else if ( swap )
swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr));
/* nhdr is a nifti_2_header; use the explicit version rather than
the magic, which could claim "n+1" and swap 540 bytes as 348 */
swap_nifti_header(nhdr, 2);

/* if all is well, overwrite header in fname dataset */
(void)write_hdr2_to_file(nhdr, fname); /* errors printed in function */
Expand Down Expand Up @@ -3613,8 +3619,12 @@ int act_swap_hdrs( nt_opts * opts )
swap_nifti_header(nhdr, 0); /* undo ANALYZE */
swap_nifti_header(nhdr, 1); /* swap NIFTI */
} else if ( opts->swap_old ) {
/* undo whichever was done and apply the old way */
swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr));
/* undo whichever was done and apply the old way. As above,
nhdr is a nifti_1_header, so it must not be swapped as a
540 byte NIFTI-2 header just because its magic says "n+2".
old_swap_nifti_header() takes a nifti_1_header and a
boolean, so it needs no such guard. */
swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0);
old_swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr));
}

Expand Down
Loading
Loading