diff --git a/applications/jpip/libopenjpip/box_manager.c b/applications/jpip/libopenjpip/box_manager.c index d3332576..8ac1fca9 100644 --- a/applications/jpip/libopenjpip/box_manager.c +++ b/applications/jpip/libopenjpip/box_manager.c @@ -31,9 +31,6 @@ #include #include #include -#include -#include -#include #include #include "box_manager.h" @@ -174,12 +171,8 @@ box_param_t * gene_boxbyType( int fd, Byte8_t offset, Byte8_t length, char TBox[ if( length==0){ // set the max length - struct stat sb; - if( fstat( fd, &sb) == -1){ - fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n"); + if( (length = get_filesize( fd) - offset) <= 0) return NULL; - } - length = (Byte8_t)sb.st_size - offset; } pos = offset; diff --git a/applications/jpip/libopenjpip/byte_manager.c b/applications/jpip/libopenjpip/byte_manager.c index 13685efa..54f27e55 100644 --- a/applications/jpip/libopenjpip/byte_manager.c +++ b/applications/jpip/libopenjpip/byte_manager.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "byte_manager.h" #ifdef SERVER @@ -153,3 +154,15 @@ void modify_4Bytecode( Byte4_t code, Byte_t *stream) *(stream+2) = (Byte_t) ((Byte4_t)(code & 0x0000ff00) >> 8); *(stream+3) = (Byte_t) (code & 0x000000ff); } + +Byte8_t get_filesize( int fd) +{ + struct stat sb; + + if( fstat( fd, &sb) == -1){ + fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n"); + fprintf( FCGI_stderr, "Error: error in get_filesize( %d)\n", fd); + return 0; + } + return (Byte8_t)sb.st_size; +} diff --git a/applications/jpip/libopenjpip/byte_manager.h b/applications/jpip/libopenjpip/byte_manager.h index 00ea22bd..6ce3c607 100644 --- a/applications/jpip/libopenjpip/byte_manager.h +++ b/applications/jpip/libopenjpip/byte_manager.h @@ -124,4 +124,12 @@ Byte8_t big8( Byte_t *buf); */ void modify_4Bytecode( Byte4_t code, Byte_t *stream); +/** + * Get file size + * + * @param[in] fd file discriptor + * @return file size + */ +Byte8_t get_filesize( int fd); + #endif /* !BYTE_MANAGER_H_ */ diff --git a/applications/jpip/libopenjpip/index_manager.c b/applications/jpip/libopenjpip/index_manager.c index 6eafa40b..6cecff05 100644 --- a/applications/jpip/libopenjpip/index_manager.c +++ b/applications/jpip/libopenjpip/index_manager.c @@ -31,9 +31,6 @@ #include #include #include -#include -#include -#include #include #include "bool.h" @@ -79,14 +76,12 @@ index_param_t * parse_jp2file( int fd) box_param_t *cidx; metadatalist_param_t *metadatalist; boxlist_param_t *toplev_boxlist; - struct stat sb; - - if( fstat( fd, &sb) == -1){ - fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n"); - return NULL; - } + Byte8_t filesize; - if( !(toplev_boxlist = get_boxstructure( fd, 0, sb.st_size))){ + if( !(filesize = get_filesize( fd))) + return NULL; + + if( !(toplev_boxlist = get_boxstructure( fd, 0, filesize))){ fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n"); return NULL; } diff --git a/applications/jpip/libopenjpip/metadata_manager.c b/applications/jpip/libopenjpip/metadata_manager.c index c60f437a..1c5f03c5 100644 --- a/applications/jpip/libopenjpip/metadata_manager.c +++ b/applications/jpip/libopenjpip/metadata_manager.c @@ -30,9 +30,6 @@ #include #include -#include -#include -#include #include #include "metadata_manager.h" @@ -67,14 +64,12 @@ metadatalist_param_t * const_metadatalist( int fd) placeholderlist_param_t *phldlist; placeholder_param_t *phld; int idx; - struct stat sb; - - if( fstat( fd, &sb) == -1){ - fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n"); - return NULL; - } + Byte8_t filesize; - if( !(toplev_boxlist = get_boxstructure( fd, 0, sb.st_size))){ + if(!(filesize = get_filesize( fd))) + return NULL; + + if( !(toplev_boxlist = get_boxstructure( fd, 0, filesize))){ fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n"); return NULL; } diff --git a/applications/jpip/libopenjpip/msgqueue_manager.c b/applications/jpip/libopenjpip/msgqueue_manager.c index 295c2360..8244de06 100644 --- a/applications/jpip/libopenjpip/msgqueue_manager.c +++ b/applications/jpip/libopenjpip/msgqueue_manager.c @@ -494,15 +494,8 @@ void emit_body( message_param_t *msg, int fd) { Byte_t *data; - if( lseek( fd, msg->res_offset, SEEK_SET)==-1){ - fprintf( FCGI_stderr, "Error: fseek in emit_body()\n"); - return; - } - - data = (Byte_t *)malloc( msg->length); - if( read( fd, data, msg->length) != msg->length){ - free( data); - fprintf( FCGI_stderr, "Error: fread in emit_body()\n"); + if( !(data = fetch_bytes( fd, msg->res_offset, msg->length))){ + fprintf( FCGI_stderr, "Error: fetch_bytes in emit_body()\n"); return; } diff --git a/applications/jpip/libopenjpip/openjpip.c b/applications/jpip/libopenjpip/openjpip.c index ee8bcfde..00f9a7a3 100644 --- a/applications/jpip/libopenjpip/openjpip.c +++ b/applications/jpip/libopenjpip/openjpip.c @@ -252,19 +252,15 @@ jpip_dec_param_t * init_jpipdecoder( bool jp2) bool fread_jpip( char fname[], jpip_dec_param_t *dec) { int infd; - struct stat sb; if(( infd = open( fname, O_RDONLY)) == -1){ fprintf( stderr, "file %s not exist\n", fname); return false; } - if( fstat( infd, &sb) == -1){ - fprintf( stderr, "input file stream is broken\n"); + if(!(dec->jpiplen = get_filesize(infd))) return false; - } - dec->jpiplen = (Byte8_t)sb.st_size; - + dec->jpipstream = (Byte_t *)malloc( dec->jpiplen); if( read( infd, dec->jpipstream, dec->jpiplen) != dec->jpiplen){