[1.5][JPIP] new feature to target JP2 files from www (libcurl required)
[openjpeg.git] / applications / jpip / libopenjpip / target_manager.c
1 /*
2  * $Id: target_manager.c 44 2011-02-15 12:32:29Z kaori $
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <time.h>
39 #include <curl/curl.h>
40 #include "target_manager.h"
41
42 #ifdef SERVER
43 #include "fcgi_stdio.h"
44 #define logstream FCGI_stdout
45 #else
46 #define FCGI_stdout stdout
47 #define FCGI_stderr stderr
48 #define logstream stderr
49 #endif //SERVER
50
51
52 targetlist_param_t * gene_targetlist()
53 {
54   targetlist_param_t *targetlist;
55
56   targetlist = (targetlist_param_t *)malloc( sizeof(targetlist_param_t));
57   
58   targetlist->first = NULL;
59   targetlist->last  = NULL;
60
61   return targetlist;
62 }
63
64
65 /**
66  * open jp2 format image file
67  *
68  * @param[in]  filepath file name (.jp2)
69  * @param[out] tmpfname new file name if filepath is a URL
70  * @return              file descriptor
71  */
72 int open_jp2file( char filepath[], char tmpfname[]);
73
74 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
75 {
76   target_param_t *target;
77   int fd;
78   index_param_t *jp2idx;
79   char tmpfname[MAX_LENOFTID];
80   static int last_csn = 0;
81   
82   if( targetpath[0]=='\0'){
83     fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
84     return NULL;
85   }
86
87   if((fd = open_jp2file( targetpath, tmpfname)) == -1){
88     fprintf( FCGI_stdout, "Status: 404\r\n"); 
89     return NULL;
90   }
91   
92   if( !(jp2idx = parse_jp2file( fd))){
93     fprintf( FCGI_stdout, "Status: 501\r\n");
94     return NULL;
95   }
96
97   target = (target_param_t *)malloc( sizeof(target_param_t));
98   snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
99   target->targetname = strdup( targetpath); 
100   target->fd = fd;
101   if( tmpfname[0])
102     target->tmpfname = strdup( tmpfname);
103   else
104     target->tmpfname = NULL;
105   target->csn = last_csn++;
106   target->codeidx = jp2idx;
107   target->num_of_use = 0; 
108   target->jppstream = true;
109   target->jptstream = isJPTfeasible( *jp2idx);
110   target->next=NULL;
111
112   if( targetlist->first) // there are one or more entries
113     targetlist->last->next = target;
114   else                   // first entry
115     targetlist->first = target;
116   targetlist->last = target;
117
118 #ifndef SERVER
119   fprintf( logstream, "local log: target %s generated\n", targetpath);
120 #endif
121   
122   return target;
123 }
124
125 void refer_target( target_param_t *reftarget, target_param_t **ptr)
126 {
127   *ptr = reftarget;
128   reftarget->num_of_use++;
129 }
130
131 void unrefer_target( target_param_t *target)
132 {
133   target->num_of_use--;
134 }
135
136 void delete_target( target_param_t **target)
137 {
138   close( (*target)->fd);
139
140   if( (*target)->tmpfname){
141     fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
142     remove( (*target)->tmpfname);
143   }
144
145   if( (*target)->codeidx)
146     delete_index ( &(*target)->codeidx);
147   
148 #ifndef SERVER
149   fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
150 #endif
151
152   free( (*target)->targetname);
153
154   free(*target);
155 }
156
157 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
158 {
159   target_param_t *ptr;
160
161   if( *target == targetlist->first)
162     targetlist->first = (*target)->next;
163   else{
164     ptr = targetlist->first;
165     while( ptr->next != *target){
166       ptr=ptr->next;
167     }
168     
169     ptr->next = (*target)->next;
170     
171     if( *target == targetlist->last)
172       targetlist->last = ptr;
173   }
174   delete_target( target);
175 }
176
177 void delete_targetlist(targetlist_param_t **targetlist)
178 {
179   target_param_t *targetPtr, *targetNext;
180   
181   targetPtr = (*targetlist)->first;
182   while( targetPtr != NULL){
183     targetNext=targetPtr->next;
184     delete_target( &targetPtr);
185     targetPtr=targetNext;
186   }
187   free( *targetlist);
188 }
189
190 void print_target( target_param_t *target)
191 {
192   fprintf( logstream, "target:\n");
193   fprintf( logstream, "\t tid=%s\n", target->tid);
194   fprintf( logstream, "\t csn=%d\n", target->csn);
195   fprintf( logstream, "\t target=%s\n\n", target->targetname);
196 }
197
198 void print_alltarget( targetlist_param_t *targetlist)
199 {
200   target_param_t *ptr;
201
202   ptr = targetlist->first;
203   while( ptr != NULL){
204     print_target( ptr);
205     ptr=ptr->next;
206   }
207 }
208
209 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
210 {
211   target_param_t *foundtarget;
212
213   foundtarget = targetlist->first;
214   
215   while( foundtarget != NULL){
216     
217     if( strcmp( targetname, foundtarget->targetname) == 0)
218       return foundtarget;
219       
220     foundtarget = foundtarget->next;
221   }
222   return NULL;
223 }
224
225 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
226 {
227   target_param_t *foundtarget;
228   
229   foundtarget = targetlist->first;
230   
231   while( foundtarget != NULL){
232     
233     if( strcmp( tid, foundtarget->tid) == 0)
234       return foundtarget;
235       
236     foundtarget = foundtarget->next;
237   }
238
239   return NULL;
240 }
241
242 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
243
244 int open_jp2file( char filepath[], char tmpfname[])
245 {
246   int fd;
247   char *data;
248   CURL *curl_handle;
249   
250   // download remote target file to local storage
251   if( strncmp( filepath, "http://", 7) == 0){
252     curl_handle = curl_easy_init();
253     curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
254     curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
255     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
256     
257     snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
258     fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
259     if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
260       fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
261       curl_easy_cleanup(curl_handle);
262       return -1;
263     }
264     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
265     curl_easy_perform(curl_handle);
266     curl_easy_cleanup(curl_handle);
267   }
268   else{
269     tmpfname[0] = 0;
270     if( (fd = open( filepath, O_RDONLY)) == -1){
271       fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
272       return -1;
273     }
274   }
275   // Check resource is a JP family file.
276   if( lseek( fd, 0, SEEK_SET)==-1){
277     close(fd);
278     fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
279     return -1;
280   }
281   
282   data = (char *)malloc( 12); // size of header
283
284   if( read( fd, data, 12) != 12){
285     free( data);
286     close(fd);
287     fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
288     return -1;
289   }
290     
291   if( *data || *(data + 1) || *(data + 2) ||
292       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
293     free( data);
294     close(fd);
295     fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
296     return -1;
297   } 
298
299   free( data);
300
301   return fd;
302 }
303
304 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
305 {
306   int *fd = (int *)stream;
307   int written = write( *fd, ptr, size*nmemb);
308
309   return written;
310 }