Merge pull request #1301 from rouault/fix_1299
[openjpeg.git] / src / lib / openjpip / box_manager.h
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 #ifndef     BOX_MANAGER_H_
32 # define    BOX_MANAGER_H_
33
34 #include "byte_manager.h"
35
36 /** box parameters*/
37 typedef struct box_param {
38     int fd;                 /**< file descriptor*/
39     OPJ_OFF_T offset;         /**< byte position of the whole Box (LBox) in the file*/
40     Byte_t  headlen;        /**< header length  8 or 16*/
41     Byte8_t length;         /**< length of the whole Box*/
42     char    type[4];        /**< type of information in the DBox*/
43     struct box_param *next; /**< pointer to the next box*/
44 } box_param_t;
45
46
47 /** Box list parameters*/
48 typedef struct boxlist_param {
49     box_param_t *first; /**< first box pointer of the list*/
50     box_param_t *last;  /**< last  box pointer of the list*/
51 } boxlist_param_t;
52
53
54 /**
55  * generate a box list
56  *
57  * @return pointer to the generated box list
58  */
59 boxlist_param_t * gene_boxlist(void);
60
61 /**
62  * get box structure of JP2 file
63  *
64  * @param[in]  fd     file descriptor
65  * @param[in]  offset offset of the decomposing region
66  * @param[in]  length length of the decomposing region
67  * @return            pointer to the generated boxlist
68  */
69 boxlist_param_t * get_boxstructure(int fd, OPJ_OFF_T offset, OPJ_SIZE_T length);
70
71
72 /**
73  * generate box from JP2 file at the given offset
74  *
75  * @param[in] fd     file discriptor of the JP2 file
76  * @param[in] offset Box offset
77  * @return           pointer to the structure of generate box parameters
78  */
79 box_param_t * gene_boxbyOffset(int fd, OPJ_OFF_T offset);
80
81
82 /**
83  * generate box from code stream (JPP or JPT stream) at the given offset
84  *
85  * @param[in] stream code stream of a box
86  * @param[in] offset Box offset of the whole stream
87  * @return           pointer to the structure of generate box parameters
88  */
89 box_param_t * gene_boxbyOffinStream(Byte_t *stream, OPJ_OFF_T offset);
90
91 /**
92  * generate(search) box from JP2 file
93  *
94  * @param[in] fd     file discriptor of the JP2 file
95  * @param[in] offset start Byte position of the search
96  * @param[in] length Byte length of the search, if 0, size to the end of file
97  * @param[in] TBox   Box Type
98  * @return           pointer to the structure of generate/found box parameters
99  */
100 box_param_t * gene_boxbyType(int fd, OPJ_OFF_T offset, OPJ_SIZE_T length,
101                              const char TBox[]);
102
103 /**
104  * generate(search) box from code stream
105  *
106  * @param[in] stream code stream ( from the first byte)
107  * @param[in] offset start Byte position of the search
108  * @param[in] length Byte length of the search, if 0, size to the end of file
109  * @param[in] TBox   Box Type
110  * @return           pointer to the structure of generate/found box parameters
111  */
112 box_param_t * gene_boxbyTypeinStream(Byte_t *stream, OPJ_OFF_T offset,
113                                      OPJ_SIZE_T length, const char TBox[]);
114
115 /**
116  * generate child box from JP2 file at the given offset
117  *
118  * @param[in] superbox super box pointer
119  * @param[in] offset   offset from DBox first byte of superbox
120  * @return             pointer to the structure of generate box parameters
121  */
122 box_param_t * gene_childboxbyOffset(box_param_t *superbox, OPJ_OFF_T offset);
123
124 /**
125  * generate(search) box from JP2 file
126  *
127  * @param[in] superbox super box pointer
128  * @param[in] offset   offset from DBox first byte of superbox
129  * @param[in] TBox     Box Type
130  * @return             pointer to the structure of generate/found box parameters
131  */
132 box_param_t * gene_childboxbyType(box_param_t *superbox, OPJ_OFF_T offset,
133                                   const char TBox[]);
134
135 /**
136  * get DBox offset
137  *
138  * @param[in] box box pointer
139  * @return        DBox offset (byte position) in the file
140  */
141 OPJ_OFF_T get_DBoxoff(box_param_t *box);
142
143
144 /**
145  * get DBox length
146  *
147  * @param[in] box box pointer
148  * @return        DBox length ( content length)
149  */
150 OPJ_SIZE_T get_DBoxlen(box_param_t *box);
151
152
153 /**
154  * fetch header bytes in file stream
155  *
156  * @param[in] box    box pointer
157  * @return           pointer to the fetched bytes
158  */
159 Byte_t * fetch_headbytes(box_param_t *box);
160
161
162 /**
163  * fetch DBox (Box Contents) bytes of data in file stream
164  *
165  * @param[in] box    box pointer
166  * @param[in] offset start Byte position in DBox
167  * @param[in] size   Byte length
168  * @return           pointer to the fetched data
169  */
170 Byte_t * fetch_DBoxbytes(box_param_t *box, OPJ_OFF_T offset, OPJ_SIZE_T size);
171
172 /**
173  * fetch DBox (Box Contents) 1-byte Byte codes in file stream
174  *
175  * @param[in] box    box pointer
176  * @param[in] offset start Byte position in DBox
177  * @return           fetched code
178  */
179 Byte_t fetch_DBox1byte(box_param_t *box, OPJ_OFF_T offset);
180
181 /**
182  * fetch DBox (Box Contents) 2-byte big endian Byte codes in file stream
183  *
184  * @param[in] box    box pointer
185  * @param[in] offset start Byte position in DBox
186  * @return           fetched code
187  */
188 Byte2_t fetch_DBox2bytebigendian(box_param_t *box, OPJ_OFF_T offset);
189
190 /**
191  * fetch DBox (Box Contents) 4-byte big endian Byte codes in file stream
192  *
193  * @param[in] box    box pointer
194  * @param[in] offset start Byte position in DBox
195  * @return           fetched code
196  */
197 Byte4_t fetch_DBox4bytebigendian(box_param_t *box, OPJ_OFF_T offset);
198
199 /**
200  * fetch DBox (Box Contents) 8-byte big endian Byte codes in file stream
201  *
202  * @param[in] box    box pointer
203  * @param[in] offset start Byte position in DBox
204  * @return           fetched code
205  */
206 Byte8_t fetch_DBox8bytebigendian(box_param_t *box, OPJ_OFF_T offset);
207
208
209 /**
210  * search a box by box type
211  *
212  * @param[in] type    box type
213  * @param[in] boxlist box list pointer
214  * @return            found box pointer
215  */
216 box_param_t * search_box(const char type[], boxlist_param_t *boxlist);
217
218 /**
219  * print box parameters
220  *
221  * @param[in] box box pointer
222  */
223 void print_box(box_param_t *box);
224
225
226 /**
227  * print all box parameters
228  *
229  * @param[in] boxlist box list pointer
230  */
231 void print_allbox(boxlist_param_t *boxlist);
232
233 /**
234  * delete a box in list
235  *
236  * @param[in,out] box     address of the deleting box pointer
237  * @param[in]     boxlist box list pointer
238  */
239 void delete_box_in_list(box_param_t **box, boxlist_param_t *boxlist);
240
241
242 /**
243  * delete a box in list by Type
244  *
245  * @param[in,out] type    box type
246  * @param[in]     boxlist box list pointer
247  */
248 void delete_box_in_list_by_type(const char type[], boxlist_param_t *boxlist);
249
250
251 /**
252  * delete box list
253  *
254  * @param[in,out] boxlist address of the box list pointer
255  */
256 void delete_boxlist(boxlist_param_t **boxlist);
257
258
259 /**
260  * insert a box into list
261  *
262  * @param[in] box     box pointer
263  * @param[in] boxlist box list pointer
264  */
265 void insert_box_into_list(box_param_t *box, boxlist_param_t *boxlist);
266
267 #endif      /* !BOX_MANAGER_H_ */