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