[1.5][JPIP] enabled the opj_server to reply the first query consisting with len reque...
[openjpeg.git] / applications / jpip / libopenjpip / metadata_manager.c
1 /*
2  * $Id: metadata_manager.c 53 2011-05-09 16:55:39Z 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 "metadata_manager.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #ifdef SERVER
37 #include "fcgi_stdio.h"
38 #define logstream FCGI_stdout
39 #else
40 #define FCGI_stdout stdout
41 #define FCGI_stderr stderr
42 #define logstream stderr
43 #endif /*SERVER*/
44
45
46 metadatalist_param_t * gene_metadatalist()
47 {
48   metadatalist_param_t *list;
49
50   list = (metadatalist_param_t *)malloc( sizeof(metadatalist_param_t));
51   
52   list->first = NULL;
53   list->last  = NULL;
54
55   return list;
56 }
57
58 metadatalist_param_t * const_metadatalist( int fd)
59 {
60   metadatalist_param_t *metadatalist;
61   metadata_param_t *metabin;
62   boxlist_param_t *toplev_boxlist;
63   box_param_t *box, *next;
64   placeholderlist_param_t *phldlist;
65   placeholder_param_t *phld;
66   int idx;
67   Byte8_t filesize;
68
69   if(!(filesize = get_filesize( fd)))
70     return NULL;
71   
72   if( !(toplev_boxlist = get_boxstructure( fd, 0, filesize))){
73     fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n");
74     return NULL;
75   }
76   
77   phldlist = gene_placeholderlist();
78   metadatalist = gene_metadatalist();
79
80   box = toplev_boxlist->first;
81   idx = 0;
82   while( box){
83     next = box->next;
84     if( strncmp( box->type, "jP  ",4)!=0 && strncmp( box->type, "ftyp",4)!=0 && strncmp( box->type, "jp2h",4)!=0){
85       boxlist_param_t *boxlist = NULL;
86       boxcontents_param_t *boxcontents = NULL;
87
88       phld = gene_placeholder( box, ++idx);
89       insert_placeholder_into_list( phld, phldlist);
90
91       boxlist = get_boxstructure( box->fd, get_DBoxoff( box), get_DBoxlen(box));
92       if( !boxlist)
93         boxcontents = gene_boxcontents( get_DBoxoff( box), get_DBoxlen(box));
94       
95       delete_box_in_list( &box, toplev_boxlist);
96       metabin = gene_metadata( idx, boxlist, NULL, boxcontents);
97       insert_metadata_into_list( metabin, metadatalist);
98     }
99     box = next;
100   }
101
102   metabin = gene_metadata( 0, toplev_boxlist, phldlist, NULL);
103   insert_metadata_into_list( metabin, metadatalist);
104
105   return metadatalist;
106 }
107
108 void delete_metadatalist( metadatalist_param_t **list)
109 {
110   metadata_param_t *ptr, *next;
111
112   ptr = (*list)->first;
113
114   while( ptr != NULL){
115     next=ptr->next;
116     delete_metadata( &ptr);
117     ptr=next;
118   }
119   free( *list);
120 }
121
122 metadata_param_t * gene_metadata( int idx, boxlist_param_t *boxlist, placeholderlist_param_t *phldlist, boxcontents_param_t *boxcontents)
123 {
124   metadata_param_t *bin;
125   
126   bin = (metadata_param_t *)malloc( sizeof(metadata_param_t));
127   bin->idx = idx;
128   bin->boxlist = boxlist;
129   bin->placeholderlist = phldlist;
130   bin->boxcontents = boxcontents;
131   bin->next = NULL;
132
133   return bin;
134 }
135
136 void delete_metadata( metadata_param_t **metadata)
137 {
138   delete_boxlist( &((*metadata)->boxlist));
139   delete_placeholderlist( &((*metadata)->placeholderlist));
140   if((*metadata)->boxcontents)
141     free((*metadata)->boxcontents);
142 #ifndef SERVER
143   /*  fprintf( logstream, "local log: Metadata-bin: %d deleted\n", (*metadata)->idx);*/
144 #endif
145   free( *metadata);
146 }
147
148 void insert_metadata_into_list( metadata_param_t *metabin, metadatalist_param_t *metadatalist)
149 {
150   if( metadatalist->first)
151     metadatalist->last->next = metabin;
152   else
153     metadatalist->first = metabin;
154   metadatalist->last = metabin;
155 }
156
157 void print_metadata( metadata_param_t *metadata)
158 {
159   boxcontents_param_t *boxcont;
160   fprintf( logstream, "metadata-bin %d info:\n", metadata->idx);
161   print_allbox( metadata->boxlist);
162   print_allplaceholder( metadata->placeholderlist);
163  
164   boxcont = metadata->boxcontents;
165   if( boxcont)
166       fprintf( logstream, "box contents:\n"
167                "\t offset: %lld %#llx\n" 
168                "\t length: %lld %#llx\n", boxcont->offset, boxcont->offset, boxcont->length, boxcont->length);
169 }
170
171 void print_allmetadata( metadatalist_param_t *list)
172 {
173   metadata_param_t *ptr;
174   
175   fprintf( logstream, "all metadata info: \n");
176   ptr = list->first;
177   while( ptr != NULL){
178     print_metadata( ptr);
179     ptr=ptr->next;
180   }
181 }
182
183 boxcontents_param_t * gene_boxcontents( Byte8_t offset, Byte8_t length)
184 {
185   boxcontents_param_t *contents;
186
187   contents = (boxcontents_param_t *)malloc( sizeof(boxcontents_param_t));
188
189   contents->offset = offset;
190   contents->length = length;
191
192   return contents;
193 }
194
195 metadata_param_t * search_metadata( int idx, metadatalist_param_t *list)
196
197   metadata_param_t *found;
198
199   found = list->first;
200   
201   while( found){
202     
203     if( found->idx == idx)
204       return found;
205       
206     found = found->next;
207   }
208   return NULL;
209 }
210
211 int search_metadataidx( char boxtype[4], metadatalist_param_t *list)
212 {
213   metadata_param_t *ptr;
214   int i;
215
216   for( i=0; i<4; i++)
217     if( boxtype[i] == '_')
218       boxtype[i] = ' ';
219   
220   ptr = list->first;
221   while( ptr){
222     if( ptr->boxlist){
223       box_param_t *box = ptr->boxlist->first;
224       while( box){
225         if( strncmp ( boxtype, box->type, 4) == 0)
226           return ptr->idx;
227         box = box->next;
228       }
229     }
230     ptr = ptr->next;
231   }
232
233   ptr = list->first;
234   while( ptr){
235     if( ptr->placeholderlist){
236       placeholder_param_t *phld = ptr->placeholderlist->first;
237       while( phld){
238         if( strncmp ( boxtype, (char *)phld->OrigBH+4, 4) == 0){
239           return phld->OrigID;
240         }
241         phld = phld->next;
242       }
243     }
244     ptr = ptr->next;
245   }
246   return -1;
247 }