2ffa20348672ec4aade23341e9986657c777903a
[openjpeg.git] / src / lib / openjpip / placeholder_manager.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, 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
68     ptr = (*list)->first;
69
70     while (ptr) {
71         next = ptr->next;
72         delete_placeholder(&ptr);
73         ptr = next;
74     }
75     free(*list);
76 }
77
78 placeholder_param_t * gene_placeholder(box_param_t *box, Byte8_t origID)
79 {
80     placeholder_param_t *placeholder;
81
82     placeholder = (placeholder_param_t *)malloc(sizeof(placeholder_param_t));
83
84     strncpy(placeholder->TBox, "phld", 4);
85     placeholder->Flags =
86         1; /* only the access to the original contents of this box, for now */
87     placeholder->OrigID = origID;
88     placeholder->OrigBH = fetch_headbytes(box);
89     placeholder->OrigBHlen = box->headlen;
90     placeholder->LBox = 20 + (Byte4_t)box->headlen;
91     placeholder->next = NULL;
92
93     return placeholder;
94 }
95
96 void delete_placeholder(placeholder_param_t **placeholder)
97 {
98     if ((*placeholder)->OrigBH) {
99         free((*placeholder)->OrigBH);
100     }
101     free(*placeholder);
102 }
103
104 void insert_placeholder_into_list(placeholder_param_t *phld,
105                                   placeholderlist_param_t *phldlist)
106 {
107     if (phldlist->first) {
108         phldlist->last->next = phld;
109     } else {
110         phldlist->first = phld;
111     }
112     phldlist->last = phld;
113 }
114
115 void print_placeholder(placeholder_param_t *phld)
116 {
117     int i;
118
119     fprintf(logstream, "placeholder info:\n");
120     fprintf(logstream, "\t LBox: %d %#x\n", phld->LBox, phld->LBox);
121     fprintf(logstream, "\t TBox: %.4s\n", phld->TBox);
122     fprintf(logstream, "\t Flags: %#x %#x\n", phld->Flags, phld->Flags);
123     fprintf(logstream, "\t OrigID: %" PRId64 "\n", phld->OrigID);
124     fprintf(logstream, "\t OrigBH: ");
125
126     for (i = 0; i < phld->OrigBHlen; i++) {
127         fprintf(logstream, "%02x ", phld->OrigBH[i]);
128     }
129     fprintf(logstream, "\t");
130
131     for (i = 0; i < phld->OrigBHlen; i++) {
132         fprintf(logstream, "%c", phld->OrigBH[i]);
133     }
134     fprintf(logstream, "\n");
135 }
136
137 void print_allplaceholder(placeholderlist_param_t *list)
138 {
139     placeholder_param_t *ptr;
140
141     if (!list) {
142         return;
143     }
144
145     fprintf(logstream, "all placeholder info: \n");
146     ptr = list->first;
147     while (ptr != NULL) {
148         print_placeholder(ptr);
149         ptr = ptr->next;
150     }
151 }