4a37a13be62993b6e6ab92d01a085b54a9de00c8
[openjpeg.git] / src / lib / openjpip / placeholder_manager.c
1 /*
2  * $Id: placeholder_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 "placeholder_manager.h"
35 #include "opj_inttypes.h"
36
37 #ifdef SERVER
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
40 #else
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
44 #endif /*SERVER*/
45
46
47
48 placeholderlist_param_t * gene_placeholderlist(void)
49 {
50   placeholderlist_param_t *list;
51
52   list = (placeholderlist_param_t *)malloc( sizeof(placeholderlist_param_t));
53   
54   list->first = NULL;
55   list->last  = NULL;
56
57   return list;
58 }
59
60 void delete_placeholderlist( placeholderlist_param_t **list)
61 {
62   placeholder_param_t *ptr, *next;
63
64   if(!(*list))
65     return;
66
67   ptr = (*list)->first;
68   
69   while( ptr){
70     next=ptr->next;
71     delete_placeholder( &ptr);
72     ptr=next;
73   }
74   free( *list);
75 }
76
77 placeholder_param_t * gene_placeholder( box_param_t *box, Byte8_t origID)
78 {
79   placeholder_param_t *placeholder;
80
81   placeholder = (placeholder_param_t *)malloc( sizeof(placeholder_param_t));
82   
83   strncpy( placeholder->TBox, "phld", 4);
84   placeholder->Flags = 1; /* only the access to the original contents of this box, for now */
85   placeholder->OrigID = origID;
86   placeholder->OrigBH = fetch_headbytes( box);
87   placeholder->OrigBHlen = box->headlen;
88   placeholder->LBox = 20+(Byte4_t)box->headlen;
89   placeholder->next = NULL;
90
91   return placeholder;
92 }
93
94 void delete_placeholder( placeholder_param_t **placeholder)
95 {
96   if( (*placeholder)->OrigBH)
97     free((*placeholder)->OrigBH);
98   free(*placeholder);
99 }
100
101 void insert_placeholder_into_list( placeholder_param_t *phld, placeholderlist_param_t *phldlist)
102 {
103   if( phldlist->first)
104     phldlist->last->next = phld;
105   else
106     phldlist->first = phld;
107   phldlist->last = phld;
108 }
109
110 void print_placeholder( placeholder_param_t *phld)
111 {
112   int i;
113
114   fprintf( logstream, "placeholder info:\n");
115   fprintf( logstream, "\t LBox: %d %#x\n", phld->LBox, phld->LBox);
116   fprintf( logstream, "\t TBox: %.4s\n", phld->TBox);
117   fprintf( logstream, "\t Flags: %#x %#x\n", phld->Flags, phld->Flags);
118   fprintf( logstream, "\t OrigID: %" PRId64 "\n", phld->OrigID);
119   fprintf( logstream, "\t OrigBH: ");
120   
121   for( i=0; i< phld->OrigBHlen; i++)
122     fprintf( logstream, "%02x ", phld->OrigBH[i]);
123   fprintf( logstream, "\t");
124
125   for( i=0; i< phld->OrigBHlen; i++)
126     fprintf( logstream, "%c", phld->OrigBH[i]);
127   fprintf( logstream, "\n");
128 }
129
130 void print_allplaceholder( placeholderlist_param_t *list)
131 {
132   placeholder_param_t *ptr;
133
134   if( !list)
135     return;
136   
137   fprintf( logstream, "all placeholder info: \n");
138   ptr = list->first;
139   while( ptr != NULL){
140     print_placeholder( ptr);
141     ptr=ptr->next;
142   }
143 }