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