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