[1.5][JPIP] added get_filesize()
[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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "metadata_manager.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   delete_box_in_list_by_type( "iptr", toplev_boxlist);
81   delete_box_in_list_by_type( "cidx", toplev_boxlist);
82   delete_box_in_list_by_type( "fidx", toplev_boxlist);
83   
84   box = toplev_boxlist->first;
85   idx = 0;
86   while( box){
87     next = box->next;
88     if( strncmp( box->type, "jP  ",4)!=0 && strncmp( box->type, "ftyp",4)!=0 && strncmp( box->type, "jp2h",4)!=0){
89       boxlist_param_t *boxlist = NULL;
90       boxcontents_param_t *boxcontents = NULL;
91
92       phld = gene_placeholder( box, ++idx);
93       insert_placeholder_into_list( phld, phldlist);
94
95       boxlist = get_boxstructure( box->fd, get_DBoxoff( box), get_DBoxlen(box));
96       if( !boxlist)
97         boxcontents = gene_boxcontents( get_DBoxoff( box), get_DBoxlen(box));
98       
99       delete_box_in_list( &box, toplev_boxlist);
100       metabin = gene_metadata( idx, boxlist, NULL, boxcontents);
101       insert_metadata_into_list( metabin, metadatalist);
102     }
103     box = next;
104   }
105
106   metabin = gene_metadata( 0, toplev_boxlist, phldlist, NULL);
107   insert_metadata_into_list( metabin, metadatalist);
108
109   return metadatalist;
110 }
111
112 void delete_metadatalist( metadatalist_param_t **list)
113 {
114   metadata_param_t *ptr, *next;
115
116   ptr = (*list)->first;
117
118   while( ptr != NULL){
119     next=ptr->next;
120     delete_metadata( &ptr);
121     ptr=next;
122   }
123   free( *list);
124 }
125
126 metadata_param_t * gene_metadata( int idx, boxlist_param_t *boxlist, placeholderlist_param_t *phldlist, boxcontents_param_t *boxcontents)
127 {
128   metadata_param_t *bin;
129   
130   bin = (metadata_param_t *)malloc( sizeof(metadata_param_t));
131   bin->idx = idx;
132   bin->boxlist = boxlist;
133   bin->placeholderlist = phldlist;
134   bin->boxcontents = boxcontents;
135   bin->next = NULL;
136
137   return bin;
138 }
139
140 void delete_metadata( metadata_param_t **metadata)
141 {
142   delete_boxlist( &((*metadata)->boxlist));
143   delete_placeholderlist( &((*metadata)->placeholderlist));
144   if((*metadata)->boxcontents)
145     free((*metadata)->boxcontents);
146 #ifndef SERVER
147   //  fprintf( logstream, "local log: Metadata-bin: %d deleted\n", (*metadata)->idx);
148 #endif
149   free( *metadata);
150 }
151
152 void insert_metadata_into_list( metadata_param_t *metabin, metadatalist_param_t *metadatalist)
153 {
154   if( metadatalist->first)
155     metadatalist->last->next = metabin;
156   else
157     metadatalist->first = metabin;
158   metadatalist->last = metabin;
159 }
160
161 void print_metadata( metadata_param_t *metadata)
162 {
163   fprintf( logstream, "metadata-bin %d info:\n", metadata->idx);
164   print_allbox( metadata->boxlist);
165   print_allplaceholder( metadata->placeholderlist);
166  
167   boxcontents_param_t *boxcont = metadata->boxcontents;
168   if( boxcont)
169       fprintf( logstream, "box contents:\n"
170                "\t offset: %lld %#llx\n" 
171                "\t length: %lld %#llx\n", boxcont->offset, boxcont->offset, boxcont->length, boxcont->length);
172 }
173
174 void print_allmetadata( metadatalist_param_t *list)
175 {
176   metadata_param_t *ptr;
177   
178   fprintf( logstream, "all metadata info: \n");
179   ptr = list->first;
180   while( ptr != NULL){
181     print_metadata( ptr);
182     ptr=ptr->next;
183   }
184 }
185
186 boxcontents_param_t * gene_boxcontents( Byte8_t offset, Byte8_t length)
187 {
188   boxcontents_param_t *contents;
189
190   contents = (boxcontents_param_t *)malloc( sizeof(boxcontents_param_t));
191
192   contents->offset = offset;
193   contents->length = length;
194
195   return contents;
196 }
197
198 metadata_param_t * search_metadata( int idx, metadatalist_param_t *list)
199
200   metadata_param_t *found;
201
202   found = list->first;
203   
204   while( found){
205     
206     if( found->idx == idx)
207       return found;
208       
209     found = found->next;
210   }
211   return NULL;
212 }
213
214 int search_metadataidx( char boxtype[4], metadatalist_param_t *list)
215 {
216   metadata_param_t *ptr;
217   int i;
218
219   for( i=0; i<4; i++)
220     if( boxtype[i] == '_')
221       boxtype[i] = ' ';
222   
223   ptr = list->first;
224   while( ptr){
225     if( ptr->boxlist){
226       box_param_t *box = ptr->boxlist->first;
227       while( box){
228         if( strncmp ( boxtype, box->type, 4) == 0)
229           return ptr->idx;
230         box = box->next;
231       }
232     }
233     ptr = ptr->next;
234   }
235
236   ptr = list->first;
237   while( ptr){
238     if( ptr->placeholderlist){
239       placeholder_param_t *phld = ptr->placeholderlist->first;
240       while( phld){
241         if( strncmp ( boxtype, (char *)phld->OrigBH+4, 4) == 0){
242           return phld->OrigID;
243         }
244         phld = phld->next;
245       }
246     }
247     ptr = ptr->next;
248   }
249   return -1;
250 }