Require `stdint.h` & `inttypes.h`
[openjpeg.git] / src / lib / openjpip / mhixbox_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
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <inttypes.h>
35 #include "mhixbox_manager.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 mhixbox_param_t * gene_mhixbox(box_param_t *box)
48 {
49     mhixbox_param_t *mhix;
50     markeridx_param_t  *mkridx, *lastmkidx;
51     OPJ_OFF_T pos = 0;
52
53     mhix = (mhixbox_param_t *)malloc(sizeof(mhixbox_param_t));
54
55     mhix->tlen = fetch_DBox8bytebigendian(box, (pos += 8) - 8);
56
57     mhix->first = lastmkidx = NULL;
58     while ((OPJ_SIZE_T)pos < get_DBoxlen(box)) {
59
60         mkridx = (markeridx_param_t *)malloc(sizeof(markeridx_param_t));
61         mkridx->code       = fetch_DBox2bytebigendian(box, (pos += 2) - 2);
62         mkridx->num_remain = fetch_DBox2bytebigendian(box, (pos += 2) - 2);
63         mkridx->offset     = (OPJ_OFF_T)fetch_DBox8bytebigendian(box, (pos += 8) - 8);
64         mkridx->length     = fetch_DBox2bytebigendian(box, (pos += 2) - 2);
65         mkridx->next = NULL;
66
67         if (mhix->first) {
68             lastmkidx->next = mkridx;
69         } else {
70             mhix->first = mkridx;
71         }
72         lastmkidx = mkridx;
73     }
74     return mhix;
75 }
76
77
78 markeridx_param_t * search_markeridx(Byte2_t code, mhixbox_param_t *mhix)
79 {
80     markeridx_param_t *found;
81
82     found = mhix->first;
83
84     while (found != NULL) {
85
86         if (code == found->code) {
87             return found;
88         }
89
90         found = found->next;
91     }
92     fprintf(FCGI_stderr, "Error: Marker index %#x not found\n", code);
93
94     return NULL;
95 }
96
97
98 void print_mhixbox(mhixbox_param_t *mhix)
99 {
100     markeridx_param_t *ptr;
101
102     fprintf(logstream, "mhix box info:\n");
103     fprintf(logstream, "\t tlen: %#" PRIx64 "\n", mhix->tlen);
104
105     ptr = mhix->first;
106     while (ptr != NULL) {
107         fprintf(logstream, "marker index info:\n"
108                 "\t code: %#x\n"
109                 "\t num_remain: %#x\n"
110                 "\t offset: %#" PRIx64 "\n"
111                 "\t length: %#x\n", ptr->code, ptr->num_remain, ptr->offset, ptr->length);
112         ptr = ptr->next;
113     }
114 }
115
116
117 void print_markeridx(markeridx_param_t *markeridx)
118 {
119     fprintf(logstream, "marker index info:\n"
120             "\t code: %#x\n"
121             "\t num_remain: %#x\n"
122             "\t offset: %#" PRIx64 "\n"
123             "\t length: %#x\n", markeridx->code, markeridx->num_remain, markeridx->offset,
124             markeridx->length);
125 }
126
127
128 void delete_mhixbox(mhixbox_param_t **mhix)
129 {
130     markeridx_param_t *mkPtr, *mkNext;
131
132     mkPtr = (*mhix)->first;
133     while (mkPtr != NULL) {
134         mkNext = mkPtr->next;
135 #ifndef SERVER
136         /*      fprintf( logstream, "local log: marker index %#x deleted!\n", mkPtr->code); */
137 #endif
138         free(mkPtr);
139         mkPtr = mkNext;
140     }
141     free(*mhix);
142 }
143
144