[1.5][JPIP] added get_filesize()
authorKaori Hagihara <khagihara@users.noreply.github.com>
Wed, 16 Nov 2011 16:13:11 +0000 (16:13 +0000)
committerKaori Hagihara <khagihara@users.noreply.github.com>
Wed, 16 Nov 2011 16:13:11 +0000 (16:13 +0000)
applications/jpip/libopenjpip/box_manager.c
applications/jpip/libopenjpip/byte_manager.c
applications/jpip/libopenjpip/byte_manager.h
applications/jpip/libopenjpip/index_manager.c
applications/jpip/libopenjpip/metadata_manager.c
applications/jpip/libopenjpip/msgqueue_manager.c
applications/jpip/libopenjpip/openjpip.c

index d3332576c21c0253b08b2fe215af249d2616ebd5..8ac1fca93dfa9d482e90112dfe1e803b5800759d 100644 (file)
@@ -31,9 +31,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <ctype.h>
 #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;
index 13685efa0d6cbf0e9c0e98e062f67bbdbbc3028c..54f27e552d258043ef3258c6b716ea2055461029 100644 (file)
@@ -32,6 +32,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #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;
+}
index 00ea22bd522bcd35caa4b530d245ace395e624f7..6ce3c607c7068120af2a949c06d0ab67f2cee24e 100644 (file)
@@ -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_ */
index 6eafa40b78304c2da26259fe9f3a5601f5612d8d..6cecff050263540498dfe8d9202eeb2309600747 100644 (file)
@@ -31,9 +31,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <string.h>
 
 #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;
   }
index c60f437afff31cfdcb524c7655a8aa2497acd4de..1c5f03c5e8c9a0f6de0a4057f320892d6b1c60d6 100644 (file)
@@ -30,9 +30,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <string.h>
 #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;
   }
index 295c2360cc843c1a5dbacf10e0a99b9b15afc184..8244de06e8b9f5f1c180f9fafaf9a6816a8f626e 100644 (file)
@@ -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;
   }
 
index ee8bcfde2cba51f6eb5367fd46113d2378d3f450..00f9a7a32afc2acaec6a2ea6ae91eae90fcbf7bd 100644 (file)
@@ -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){