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