fix declarations
[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 #ifdef _WIN32
35 #include <io.h>
36 #else
37 #include <sys/types.h>
38 #include <unistd.h>
39 #endif
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <time.h>
43 #include "target_manager.h"
44
45 #ifdef SERVER
46 #include <curl/curl.h>
47 #include "fcgi_stdio.h"
48 #define logstream FCGI_stdout
49 #else
50 #define FCGI_stdout stdout
51 #define FCGI_stderr stderr
52 #define logstream stderr
53 #endif /*SERVER*/
54
55 targetlist_param_t * gene_targetlist()
56 {
57   targetlist_param_t *targetlist;
58
59   targetlist = (targetlist_param_t *)malloc( sizeof(targetlist_param_t));
60   
61   targetlist->first = NULL;
62   targetlist->last  = NULL;
63
64   return targetlist;
65 }
66
67
68 /**
69  * open jp2 format image file
70  *
71  * @param[in]  filepath file name (.jp2)
72  * @param[out] tmpfname new file name if filepath is a URL
73  * @return              file descriptor
74  */
75 int open_jp2file( char filepath[], char tmpfname[]);
76
77 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
78 {
79   target_param_t *target;
80   int fd;
81   index_param_t *jp2idx;
82   char tmpfname[MAX_LENOFTID];
83   static int last_csn = 0;
84   
85   if( targetpath[0]=='\0'){
86     fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
87     return NULL;
88   }
89
90   if((fd = open_jp2file( targetpath, tmpfname)) == -1){
91     fprintf( FCGI_stdout, "Status: 404\r\n"); 
92     return NULL;
93   }
94   
95   if( !(jp2idx = parse_jp2file( fd))){
96     fprintf( FCGI_stdout, "Status: 501\r\n");
97     return NULL;
98   }
99
100   target = (target_param_t *)malloc( sizeof(target_param_t));
101   snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
102   target->targetname = strdup( targetpath); 
103   target->fd = fd;
104 #ifdef SERVER
105   if( tmpfname[0])
106     target->tmpfname = strdup( tmpfname);
107   else
108     target->tmpfname = NULL;
109 #endif
110   target->csn = last_csn++;
111   target->codeidx = jp2idx;
112   target->num_of_use = 0; 
113   target->jppstream = true;
114   target->jptstream = isJPTfeasible( *jp2idx);
115   target->next=NULL;
116
117   if( targetlist->first) /* there are one or more entries*/
118     targetlist->last->next = target;
119   else                   /* first entry*/
120     targetlist->first = target;
121   targetlist->last = target;
122
123 #ifndef SERVER
124   fprintf( logstream, "local log: target %s generated\n", targetpath);
125 #endif
126   
127   return target;
128 }
129
130 void refer_target( target_param_t *reftarget, target_param_t **ptr)
131 {
132   *ptr = reftarget;
133   reftarget->num_of_use++;
134 }
135
136 void unrefer_target( target_param_t *target)
137 {
138   target->num_of_use--;
139 }
140
141 void delete_target( target_param_t **target)
142 {
143   close( (*target)->fd);
144
145 #ifdef SERVER
146   if( (*target)->tmpfname){
147     fprintf( FCGI_stderr, "Temporal file %s is deleted\n", (*target)->tmpfname);
148     remove( (*target)->tmpfname);
149   }
150 #endif
151
152   if( (*target)->codeidx)
153     delete_index ( &(*target)->codeidx);
154   
155 #ifndef SERVER
156   fprintf( logstream, "local log: target: %s deleted\n", (*target)->targetname);
157 #endif
158
159   free( (*target)->targetname);
160
161   free(*target);
162 }
163
164 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
165 {
166   target_param_t *ptr;
167
168   if( *target == targetlist->first)
169     targetlist->first = (*target)->next;
170   else{
171     ptr = targetlist->first;
172     while( ptr->next != *target){
173       ptr=ptr->next;
174     }
175     
176     ptr->next = (*target)->next;
177     
178     if( *target == targetlist->last)
179       targetlist->last = ptr;
180   }
181   delete_target( target);
182 }
183
184 void delete_targetlist(targetlist_param_t **targetlist)
185 {
186   target_param_t *targetPtr, *targetNext;
187   
188   targetPtr = (*targetlist)->first;
189   while( targetPtr != NULL){
190     targetNext=targetPtr->next;
191     delete_target( &targetPtr);
192     targetPtr=targetNext;
193   }
194   free( *targetlist);
195 }
196
197 void print_target( target_param_t *target)
198 {
199   fprintf( logstream, "target:\n");
200   fprintf( logstream, "\t tid=%s\n", target->tid);
201   fprintf( logstream, "\t csn=%d\n", target->csn);
202   fprintf( logstream, "\t target=%s\n\n", target->targetname);
203 }
204
205 void print_alltarget( targetlist_param_t *targetlist)
206 {
207   target_param_t *ptr;
208
209   ptr = targetlist->first;
210   while( ptr != NULL){
211     print_target( ptr);
212     ptr=ptr->next;
213   }
214 }
215
216 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
217 {
218   target_param_t *foundtarget;
219
220   foundtarget = targetlist->first;
221   
222   while( foundtarget != NULL){
223     
224     if( strcmp( targetname, foundtarget->targetname) == 0)
225       return foundtarget;
226       
227     foundtarget = foundtarget->next;
228   }
229   return NULL;
230 }
231
232 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
233 {
234   target_param_t *foundtarget;
235   
236   foundtarget = targetlist->first;
237   
238   while( foundtarget != NULL){
239     
240     if( strcmp( tid, foundtarget->tid) == 0)
241       return foundtarget;
242       
243     foundtarget = foundtarget->next;
244   }
245
246   return NULL;
247 }
248
249 int open_remotefile( char filepath[], char tmpfname[]);
250
251 int open_jp2file( char filepath[], char tmpfname[])
252 {
253   int fd;
254   char *data;
255   
256   /* download remote target file to local storage*/
257   if( strncmp( filepath, "http://", 7) == 0){
258     if( (fd = open_remotefile( filepath, tmpfname)) == -1)
259       return -1;
260   }
261   else{
262     tmpfname[0] = 0;
263     if( (fd = open( filepath, O_RDONLY)) == -1){
264       fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filepath);
265       return -1;
266     }
267   }
268   /* Check resource is a JP family file.*/
269   if( lseek( fd, 0, SEEK_SET)==-1){
270     close(fd);
271     fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filepath);
272     return -1;
273   }
274   
275   data = (char *)malloc( 12); /* size of header*/
276
277   if( read( fd, data, 12) != 12){
278     free( data);
279     close(fd);
280     fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filepath);
281     return -1;
282   }
283     
284   if( *data || *(data + 1) || *(data + 2) ||
285       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
286     free( data);
287     close(fd);
288     fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filepath);
289     return -1;
290   } 
291
292   free( data);
293
294   return fd;
295 }
296
297 #ifdef SERVER
298 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
299 #endif
300
301 int open_remotefile( char filepath[], char tmpfname[])
302 {
303 #ifndef SERVER
304   (void)filepath;
305   (void)tmpfname;
306   fprintf( FCGI_stderr, "Remote file can not be opened in local mode\n");
307   return -1;
308
309 #else
310
311   CURL *curl_handle;
312   int fd;
313     
314   curl_handle = curl_easy_init();
315   curl_easy_setopt(curl_handle, CURLOPT_URL, filepath);
316   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
317   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
318   
319   snprintf( tmpfname, MAX_LENOFTID, "%x-%x.jp2", (unsigned int)time(NULL), (unsigned int)rand());
320   fprintf( FCGI_stderr, "%s is downloaded to a temporal new file %s\n", filepath, tmpfname);
321   if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
322     fprintf( FCGI_stdout, "Reason: File open error %s\r\n", tmpfname);
323     curl_easy_cleanup(curl_handle);
324     return -1;
325   }
326   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &fd);
327   curl_easy_perform(curl_handle);
328   curl_easy_cleanup(curl_handle);
329
330   return fd;
331 #endif /*SERVER*/
332 }
333
334 #ifdef SERVER
335 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
336 {
337   int *fd = (int *)stream;
338   int written = write( *fd, ptr, size*nmemb);
339
340   return written;
341 }
342 #endif /*SERVER*/