[1.5][JPIP] backport r1037 to branch 1.5
authorAntonin Descampe <antonin@gmail.com>
Sun, 6 Nov 2011 22:03:12 +0000 (22:03 +0000)
committerAntonin Descampe <antonin@gmail.com>
Sun, 6 Nov 2011 22:03:12 +0000 (22:03 +0000)
13 files changed:
applications/jpip/CHANGES
applications/jpip/libopenjpip/boxheader_manager.c
applications/jpip/libopenjpip/cache_manager.c
applications/jpip/libopenjpip/cache_manager.h
applications/jpip/libopenjpip/cachemodel_manager.c
applications/jpip/libopenjpip/dec_clientmsg_handler.c
applications/jpip/libopenjpip/imgsock_manager.c
applications/jpip/libopenjpip/imgsock_manager.h
applications/jpip/libopenjpip/index_manager.c
applications/jpip/libopenjpip/manfbox_manager.c
applications/jpip/libopenjpip/target_manager.c
applications/jpip/libopenjpip/target_manager.h
applications/jpip/util/jpip_to_j2k.c

index 545b4e9831c2421f9ca6565c758cae1b124a9f7e..2bdbdc0d2fe2c0eb9a793d00bbe0e48831370cd6 100644 (file)
@@ -5,6 +5,10 @@ What's New for OpenJPIP
 ! : changed
 + : added
 
+November 3, 2011
+* [kaori] solved memory leak of opj_server
+! [kaori] removed redundant defines
+
 November 2, 2011
 * [antonin] additional patches for autotools and cmake
 
index 2f465a3b8a560911508eeeced6170ff32c2eef90..0a8b2215fc783134dfb262f68edf61dc104cc6cc 100644 (file)
@@ -59,6 +59,7 @@ boxheader_param_t * gene_boxheader( int fd, Byte8_t offset)
     boxlen = fetch_8bytebigendian( fd, offset+8);
     headlen = 16;
   }
+
   boxheader = (boxheader_param_t *)malloc( sizeof( boxheader_param_t));
   boxheader->headlen = headlen;
   boxheader->length = boxlen;
index 597a589633b9a559e1c279ed41744bbe0484c5a8..adc0ba87f732a706ad3e2c8b6e13b03a97860baa 100644 (file)
@@ -33,9 +33,6 @@
 #include <string.h>
 #include "cache_manager.h"
 
-//! maximum length of channel identifier
-#define MAX_LENOFCID 30
-
 cachelist_param_t * gene_cachelist()
 {
   cachelist_param_t *cachelist;
@@ -66,12 +63,11 @@ cache_param_t * gene_cache( char *targetname, int csn, char *tid, char *cid)
   cache_param_t *cache;
   
   cache = (cache_param_t *)malloc( sizeof(cache_param_t));
-  strcpy( cache->filename, targetname);
-  strcpy( cache->tid, tid);
+  cache->filename = strdup( targetname);
+  cache->tid = strdup( tid);
   cache->csn = csn;
   cache->cid = (char **)malloc( sizeof(char *));
-  *cache->cid = (char *)malloc( MAX_LENOFCID);
-  strcpy( *cache->cid, cid);
+  *cache->cid = strdup( cid);
   cache->numOfcid = 1;
 #if 1
   cache->metadatalist = NULL;
@@ -87,6 +83,9 @@ cache_param_t * gene_cache( char *targetname, int csn, char *tid, char *cid)
 void delete_cache( cache_param_t **cache)
 {
   int i;
+  
+  free( (*cache)->filename);
+  free( (*cache)->tid);
 
   delete_metadatalist( &(*cache)->metadatalist);
 
@@ -111,6 +110,9 @@ cache_param_t * search_cache( char targetname[], cachelist_param_t *cachelist)
 {
   cache_param_t *foundcache;
 
+  if( !targetname)
+    return NULL;
+
   foundcache = cachelist->first;
   
   while( foundcache != NULL){
@@ -143,6 +145,9 @@ cache_param_t * search_cacheBycid( char cid[], cachelist_param_t *cachelist)
   cache_param_t *foundcache;
   int i;
 
+  if( !cid)
+    return NULL;
+
   foundcache = cachelist->first;
   
   while( foundcache != NULL){
@@ -158,6 +163,9 @@ cache_param_t * search_cacheBytid( char tid[], cachelist_param_t *cachelist)
 {
   cache_param_t *foundcache;
 
+  if( !tid)
+    return NULL;
+
   foundcache = cachelist->first;
   
   while( foundcache != NULL){
@@ -170,31 +178,28 @@ cache_param_t * search_cacheBytid( char tid[], cachelist_param_t *cachelist)
 
 void add_cachecid( char *cid, cache_param_t *cache)
 {
-  char **tmp;
-  int i;
+  if( !cid)
+    return;
 
-  tmp = cache->cid;
-  
-  cache->cid = (char **)malloc( (cache->numOfcid+1)*sizeof(char *));
-
-  for( i=0; i<cache->numOfcid; i++){
-    cache->cid[i] = (char *)malloc( MAX_LENOFCID);
-    strcpy( cache->cid[i], tmp[i]);
-    free( tmp[i]);
+  if( realloc( cache->cid, (cache->numOfcid+1)*sizeof(char *)) == NULL){
+    fprintf( stderr, "failed to add new cid to cache table in add_cachecid()\n");
+    return;
   }
-  free( tmp);
-
-  cache->cid[ cache->numOfcid] = (char *)malloc( MAX_LENOFCID);
-  strcpy( cache->cid[ cache->numOfcid], cid);
+  
+  cache->cid[ cache->numOfcid] = strdup( cid);
 
   cache->numOfcid ++;
 }
 
 void update_cachetid( char *tid, cache_param_t *cache)
 {
+  if( !tid)
+    return;
+
   if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){
     fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename);
-    strcpy( cache->tid, tid);
+    free( cache->tid);
+    cache->tid = strdup( tid);
   }
 }
 
@@ -231,8 +236,7 @@ void remove_cidInCache( char *cid, cache_param_t *cache)
   
   for( i=0, j=0; i<cache->numOfcid; i++){
     if( i != idx){
-      cache->cid[j] = (char *)malloc( MAX_LENOFCID);
-      strcpy( cache->cid[j], tmp[i]);
+      cache->cid[j] = strdup( tmp[i]);
       j++;
     }
     free( tmp[i]);
index 61c1381569ee982e826bfc80ca086f2a493ee2fe..e2e2bafb863995afabe9615ac9a20a48e66b792b 100644 (file)
 #include "metadata_manager.h"
 #include "ihdrbox_manager.h"
 
-//! maximum length of target name
-#define MAX_LENOFTARGET 128
-
-//! maximum length of target identifier
-#define MAX_LENOFTID 30
-
 //! cache parameters
 typedef struct cache_param{
-  char filename[MAX_LENOFTARGET];     //!< file name
-  char tid[MAX_LENOFTID];             //!< taregt identifier
+  char *filename;                     //!< file name
+  char *tid;                          //!< taregt identifier
   int csn;                            //!< codestream number
   char **cid;                         //!< dynamic array of channel identifiers
   int numOfcid;                       //!< number of cids
index 7a97ee34e13b167be16d8a1351fa21796f4c94ec..ba5a3ec1ff7bff200bc1315a3c4e3d9596d16351 100644 (file)
@@ -184,9 +184,8 @@ void delete_cachemodel( cachemodel_param_t **cachemodel)
   free( (*cachemodel)->tp_model);
   free( (*cachemodel)->th_model);
   
-  if( (*cachemodel)->target->codeidx->SIZ.Csiz > 1)
-    for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++)
-      free( (*cachemodel)->pp_model[i]);
+  for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++)
+    free( (*cachemodel)->pp_model[i]);
   free( (*cachemodel)->pp_model);
 
 #ifndef SERVER
index 875e297df6c471ade6c0587a31b8c68a194dccb0..294285af23eaa93c0348141903ff4f90e7bc514c 100644 (file)
 #include "jpipstream_manager.h"
 #include "jp2k_encoder.h"
 
-
-//! maximum length of channel identifier
-#define MAX_LENOFCID 30
-
 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
                           Byte_t **jpipstream, int *streamlen, msgqueue_param_t *msgqueue)
 {
   Byte_t *newjpipstream;
   int newstreamlen = 0;
   cache_param_t *cache;
-  char target[MAX_LENOFTARGET], tid[MAX_LENOFTID], cid[MAX_LENOFCID];
+  char *target, *tid, *cid;
   metadatalist_param_t *metadatalist;
   
-  newjpipstream = receive_JPIPstream( connected_socket, target, tid, cid, &newstreamlen);
+  newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen);
 
   parse_JPIPstream( newjpipstream, newstreamlen, *streamlen, msgqueue);
 
@@ -61,11 +57,11 @@ void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist
   parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist);
 
   // cid registration
-  if( target[0] != 0){
+  if( target != NULL){
     if((cache = search_cache( target, cachelist))){
-      if( tid[0] != 0)
+      if( tid != NULL)
        update_cachetid( tid, cache);
-      if( cid[0] != 0)
+      if( cid != NULL)
        add_cachecid( cid, cache);
     }
     else{
@@ -80,6 +76,10 @@ void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist
     delete_metadatalist( &cache->metadatalist);
   cache->metadatalist = metadatalist;
 
+  if( target)    free( target);
+  if( tid)    free( tid);
+  if( cid)    free( cid);
+
   response_signal( connected_socket, true);
 }
 
@@ -87,14 +87,19 @@ void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par
 {
   Byte_t *pnmstream;
   ihdrbox_param_t *ihdrbox;
-  char cid[MAX_LENOFCID], tmp[10];
+  char *CIDorTID, tmp[10];
   cache_param_t *cache;
   int fw, fh;
 
-  receive_line( connected_socket, cid);
-  if(!(cache = search_cacheBycid( cid, cachelist)))
-    if(!(cache = search_cacheBytid( cid, cachelist)))
+  CIDorTID = receive_string( connected_socket);
+  
+  if(!(cache = search_cacheBycid( CIDorTID, cachelist)))
+    if(!(cache = search_cacheBytid( CIDorTID, cachelist))){
+      free( CIDorTID);
       return;
+    }
+  
+  free( CIDorTID);
 
   receive_line( connected_socket, tmp);
   fw = atoi( tmp);
@@ -112,12 +117,17 @@ void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par
 
 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
 {
-  char cid[MAX_LENOFCID];
+  char *cid;
   cache_param_t *cache;
 
-  receive_line( connected_socket, cid);
-  if(!(cache = search_cacheBycid( cid, cachelist)))
+  cid = receive_string( connected_socket);
+
+  if(!(cache = search_cacheBycid( cid, cachelist))){
+    free( cid);
     return;
+  }
+
+  free( cid);
   
   boxcontents_param_t *boxcontents = cache->metadatalist->last->boxcontents;
   Byte_t *xmlstream = (Byte_t *)malloc( boxcontents->length);
@@ -128,12 +138,14 @@ void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_pa
 
 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
 {
-  char target[MAX_LENOFTARGET], *tid = NULL;
+  char *target, *tid = NULL;
   cache_param_t *cache;
   int tidlen = 0;
 
-  receive_line( connected_socket, target);
+  target = receive_string( connected_socket);
   cache = search_cache( target, cachelist);
+
+  free( target);
   
   if( cache){
     tid = cache->tid;
@@ -144,13 +156,15 @@ void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
 
 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
 {
-  char target[MAX_LENOFTARGET], *cid = NULL;
+  char *target, *cid = NULL;
   cache_param_t *cache;
   int cidlen = 0;
 
-  receive_line( connected_socket, target);
+  target = receive_string( connected_socket);
   cache = search_cache( target, cachelist);
   
+  free( target);
+
   if( cache){
     if( cache->numOfcid > 0){
       cid = cache->cid[ cache->numOfcid-1];
@@ -162,23 +176,29 @@ void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
 
 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
 {
-  char cid[MAX_LENOFCID];
+  char *cid;
 
-  receive_line( connected_socket, cid);
+  cid = receive_string( connected_socket);
   remove_cachecid( cid, cachelist);
   response_signal( connected_socket, true);
+  
+  free( cid);
 }
 
 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
 {
-  char cid[MAX_LENOFCID];
+  char *cid;
   cache_param_t *cache;
   Byte_t *jp2stream;
   Byte8_t jp2len;
 
-  receive_line( connected_socket, cid);
-  if(!(cache = search_cacheBycid( cid, cachelist)))
+  cid = receive_string( connected_socket);
+  if(!(cache = search_cacheBycid( cid, cachelist))){
+    free( cid);
     return;
+  }
+  
+  free( cid);
   
   jp2stream = recons_jp2( msgqueue, jpipstream, cache->csn, &jp2len);
 
index ccc2b04c8976b68032125c63b4ac16ad2d73ab32..79149f5fe4d5c704c9c236249d8bad12c1c71546 100644 (file)
@@ -123,15 +123,13 @@ msgtype_t identify_clientmsg( SOCKET connected_socket)
   return MSGERROR;
 }
 
-Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, char *cid, int *streamlen)
+Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen)
 {
   Byte_t *jpipstream=NULL, *ptr;
   char buf[BUF_LEN], versionstring[] = "version 1.2";
   int linelen, redlen, remlen;
   
-  target[0] = 0;
-  cid[0] = 0;
-  tid[0] = 0;
+  *target = *cid = *tid = NULL;
   
   if((linelen = receive_line( connected_socket, buf)) == 0)
     return NULL;
@@ -145,17 +143,17 @@ Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, c
 
   if( strstr( buf, "jp2")){ 
     // register cid option
-    strcpy( target, buf);
+    *target = strdup( buf);
     
     if((linelen = receive_line( connected_socket, buf)) == 0)
       return NULL;
     if( strcmp( buf, "0") != 0)
-      strcpy( tid, buf);
+      *tid = strdup( buf);
 
     if((linelen = receive_line( connected_socket, buf)) == 0)
       return NULL;
     if( strcmp( buf, "0") != 0)
-      strcpy( cid, buf);
+      *cid = strdup( buf);
     
     if((linelen = receive_line( connected_socket, buf)) == 0)
       return NULL;
@@ -281,6 +279,15 @@ int receive_line(SOCKET connected_socket, char *p)
   return len;
 }
 
+char * receive_string( SOCKET connected_socket)
+{
+  char buf[BUF_LEN];
+  
+  receive_line( connected_socket, buf);
+    
+  return strdup(buf);
+}
+
 void response_signal( SOCKET connected_socket, bool succeed)
 {
   Byte_t code;
index bb8e1bcf682fec9e004595b3bc965048587f96da..5961a58109b7940c5670addbab4b03d45625cf43 100644 (file)
@@ -71,13 +71,13 @@ msgtype_t identify_clientmsg( SOCKET connected_socket);
  * receive a JPT- JPP- stream from client
  *
  * @param [in]  connected_socket file descriptor of the connected socket
- * @param [out] target           received target file name (if not received, null string)
- * @param [out] tid              received target identifier (if not received, null string)
- * @param [out] cid              received channel identifier (if not received, null string)
+ * @param [out] target           address of received target file name string pointer ( malloced, if not received, NULL)
+ * @param [out] tid              address of received target identifier string pointer ( malloced, if not received, null string)
+ * @param [out] cid              address of received channel identifier string pointer ( malloced, if not received, null string)
  * @param [out] streamlen        length of the received codestream
  * @return                       JPT- JPP- codestream
  */
-Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, char *cid, int *streamlen);
+Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen);
 
 /**
  * send PGM/PPM image stream to the client
@@ -135,6 +135,14 @@ void response_signal( SOCKET connected_socket, bool succeed);
  */
 int receive_line(SOCKET connected_socket, char *buf);
 
+/**
+ * receive a string line (ending with '\n') from client, return malloc string
+ *
+ * @param [in]  connected_socket file descriptor of the connected socket
+ * @return                       pointer to the string (memory allocated)
+ */
+char * receive_string( SOCKET connected_socket);
+
 /**
  * close socket
  *
index 2f8268a578e78be7ed205a66b7759c0fce0ddf8d..83e2d3b589cc6691b0a921f064c4e3eca24a1634 100644 (file)
@@ -564,7 +564,7 @@ bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx)
     free( faix_box);   
   }
   
-  free(manf);
+  delete_manfbox( &manf);
 
   return true;
 }
index 6b1fccef8bbdd21771fc4b4f1a88a56bfa32f99c..d2b4153625124ca21f8ab20a833a8c721c43ab7e 100644 (file)
@@ -75,12 +75,12 @@ void delete_manfbox( manfbox_param_t **manf)
   
   bhPtr = (*manf)->first;
   while( bhPtr != NULL){
-    bhNext=bhPtr->next;
+    bhNext = bhPtr->next;
 #ifndef SERVER
     //      fprintf( logstream, "local log: boxheader %.4s deleted!\n", bhPtr->type);
 #endif
       free(bhPtr);
-      bhPtr=bhNext;
+      bhPtr = bhNext;
   }
   free( *manf);
 }
index 692dd59f4cfec60910c333bdea88f49385301cb0..813c1a99013c4b7828c830607ba31aef981cccfc 100644 (file)
@@ -69,19 +69,19 @@ targetlist_param_t * gene_targetlist()
  */
 int open_jp2file( char filename[]);
 
-target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname)
+target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
 {
   target_param_t *target;
   int fd;
   index_param_t *jp2idx;
   static int last_csn = 0;
 
-  if( targetname[0]=='\0'){
-    fprintf( FCGI_stderr, "Error: exception, no targetname in gene_target()\n");
+  if( targetpath[0]=='\0'){
+    fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
     return NULL;
   }
 
-  if((fd = open_jp2file( targetname)) == -1){
+  if((fd = open_jp2file( targetpath)) == -1){
     fprintf( FCGI_stdout, "Status: 404\r\n"); 
     return NULL;
   }
@@ -93,7 +93,7 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname)
 
   target = (target_param_t *)malloc( sizeof(target_param_t));
   snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
-  strcpy( target->filename, targetname); 
+  target->filename = strdup( targetpath); 
   target->fd = fd;
   target->csn = last_csn++;
   target->codeidx = jp2idx;
@@ -109,7 +109,7 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname)
   targetlist->last = target;
 
 #ifndef SERVER
-  fprintf( logstream, "local log: target %s generated\n", targetname);
+  fprintf( logstream, "local log: target %s generated\n", targetpath);
 #endif
   
   return target;
@@ -129,11 +129,16 @@ void unrefer_target( target_param_t *target)
 void delete_target( target_param_t **target)
 {
   close( (*target)->fd);
-  delete_index ( &(*target)->codeidx);
+  
+  if( (*target)->codeidx)
+    delete_index ( &(*target)->codeidx);
 
 #ifndef SERVER
   fprintf( logstream, "local log: target: %s deleted\n", (*target)->filename);
 #endif
+
+  free( (*target)->filename);
+
   free(*target);
 }
 
index 027ac2432f9e0dd0203672aaf41486ac82e5072a..a5469e798be10ec5142b646db56a763daa41f5d2 100644 (file)
 //! maximum length of target identifier
 #define MAX_LENOFTID 30
 
-//! maximum length of target name
-#define MAX_LENOFTARGET 128
-
 //! target parameters
 typedef struct target_param{
   char tid[MAX_LENOFTID];         //!< taregt identifier
-  char filename[MAX_LENOFTARGET]; //!< file name
+  char *filename;                 //!< file name
   int fd;                         //!< file descriptor
   int csn;                        //!< codestream number
   index_param_t *codeidx;         //!< index information of codestream
@@ -74,10 +71,10 @@ targetlist_param_t * gene_targetlist();
  * generate a target
  *
  * @param[in] targetlist target list to insert the generated target
- * @param[in] targetname target file name
+ * @param[in] targetpath file path or URL of the target
  * @return               pointer to the generated target
  */
-target_param_t * gene_target( targetlist_param_t *targetlist, char *targetname);
+target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath);
 
 
 /**
index 15dfc31012a1cba434fe638a21c3ea56ad7b3ce3..28876377328922109d2dbaa601e0dd1deeb516cd 100644 (file)
@@ -64,7 +64,7 @@ int main(int argc,char *argv[])
   if(!( fwrite_jp2k( argv[2], dec)))
     return -1;
   
-  output_log( true, false, false, dec);
+  //  output_log( true, false, false, dec);
   
   destroy_jpipdecoder( &dec);