[trunk] WIP: correct a segfault inside j2k_dump output
[openjpeg.git] / libopenjpeg / cio.h
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef __CIO_H
33 #define __CIO_H
34 /**
35 @file cio.h
36 @brief Implementation of a byte input-output process (CIO)
37
38 The functions in CIO.C have for goal to realize a byte input / output process.
39 */
40
41 /** @defgroup CIO CIO - byte input-output stream */
42 /*@{*/
43
44 #include "opj_config.h"
45
46 /** @name Exported functions (see also openjpeg.h) */
47 /*@{*/
48 /* ----------------------------------------------------------------------- */
49 /**
50 Number of bytes left before the end of the stream
51 @param cio CIO handle
52 @return Returns the number of bytes before the end of the stream
53 */
54 int cio_numbytesleft(opj_cio_t *cio);
55 /**
56 Get pointer to the current position in the stream
57 @param cio CIO handle
58 @return Returns a pointer to the current position
59 */
60 unsigned char *cio_getbp(opj_cio_t *cio);
61 /**
62 Write some bytes
63 @param cio CIO handle
64 @param v Value to write
65 @param n Number of bytes to write
66 @return Returns the number of bytes written or 0 if an error occured
67 */
68 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n);
69 /**
70 Read some bytes
71 @param cio CIO handle
72 @param n Number of bytes to read
73 @return Returns the value of the n bytes read
74 */
75 unsigned int cio_read(opj_cio_t *cio, int n);
76 /**
77 Skip some bytes
78 @param cio CIO handle
79 @param n Number of bytes to skip
80 */
81 void cio_skip(opj_cio_t *cio, int n);
82 /* ----------------------------------------------------------------------- */
83 /*@}*/
84
85 /*@}*/
86
87
88
89 /* ----------------------------------------------------------------------- */
90
91 #if defined(OPJ_BIG_ENDIAN)
92         #define opj_write_bytes         opj_write_bytes_BE
93         #define opj_read_bytes          opj_read_bytes_BE
94         #define opj_write_double        opj_write_double_BE
95         #define opj_read_double         opj_read_double_BE
96         #define opj_write_float         opj_write_float_BE
97         #define opj_read_float          opj_read_float_BE
98 #else
99         #define opj_write_bytes         opj_write_bytes_LE
100         #define opj_read_bytes          opj_read_bytes_LE
101         #define opj_write_double        opj_write_double_LE
102         #define opj_read_double         opj_read_double_LE
103         #define opj_write_float         opj_write_float_LE
104         #define opj_read_float          opj_read_float_LE
105 #endif
106
107
108
109 typedef enum
110 {
111         opj_stream_e_output             = 0x1,
112         opj_stream_e_input              = 0x2,
113         opj_stream_e_end                = 0x4,
114         opj_stream_e_error              = 0x8
115 }
116 opj_stream_flag ;
117
118 /**
119 Byte input-output stream.
120 */
121 typedef struct opj_stream_private
122 {
123         /**
124          * User data, be it files, ... The actual data depends on the type of the stream.
125          */
126         void *                                  m_user_data;
127
128         /**
129          * User data length
130          */
131         OPJ_UINT32                              m_user_data_length;
132
133         /**
134          * Pointer to actual read function (NULL at the initialization of the cio.
135          */
136         opj_stream_read_fn              m_read_fn;
137
138         /**
139          * Pointer to actual write function (NULL at the initialization of the cio.
140          */
141         opj_stream_write_fn             m_write_fn;
142
143         /**
144          * Pointer to actual skip function (NULL at the initialization of the cio.
145          * There is no seek function to prevent from back and forth slow procedures.
146          */
147         opj_stream_skip_fn              m_skip_fn;
148
149         /**
150          * Pointer to actual seek function (if available).
151          */
152         opj_stream_seek_fn              m_seek_fn;
153
154
155
156
157         /**
158          * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
159          * you should never access this data directly.
160          */
161         OPJ_BYTE *                                      m_stored_data;
162
163         /**
164          * Pointer to the current read data.
165          */
166         OPJ_BYTE *                                      m_current_data;
167
168         OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *);
169
170         opj_bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *);
171
172         /**
173          * number of bytes containing in the buffer.
174          */
175         OPJ_UINT32                      m_bytes_in_buffer;
176
177         /**
178          * The number of bytes read/written.
179          */
180         OPJ_SIZE_T                      m_byte_offset;
181
182         /**
183          * The size of the buffer.
184          */
185         OPJ_UINT32                      m_buffer_size;
186
187         /**
188          * Flags to tell the status of the stream.
189          */
190         OPJ_UINT32                      m_status;
191
192 }
193 opj_stream_private_t;
194
195
196 /**
197  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
198  * @param p_buffer              pointer the data buffer to write data to.
199  * @param p_value               the value to write
200  * @param p_nb_bytes    the number of bytes to write
201 */
202 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
203
204 /**
205  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
206  * @param p_buffer              pointer the data buffer to read data from.
207  * @param p_value               pointer to the value that will store the data.
208  * @param p_nb_bytes    the nb bytes to read.
209  * @return                              the number of bytes read or -1 if an error occured.
210  */
211 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
212
213 /**
214  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
215  * @param p_buffer              pointer the data buffer to write data to.
216  * @param p_value               the value to write
217  * @param p_nb_bytes    the number of bytes to write
218  * @return                              the number of bytes written or -1 if an error occured
219 */
220 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
221
222 /**
223  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
224  * @param p_buffer              pointer the data buffer to read data from.
225  * @param p_value               pointer to the value that will store the data.
226  * @param p_nb_bytes    the nb bytes to read.
227  * @return                              the number of bytes read or -1 if an error occured.
228  */
229 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
230
231
232 /**
233  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
234  * @param p_buffer              pointer the data buffer to write data to.
235  * @param p_value               the value to write
236  */
237 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
238
239 /***
240  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
241  * @param p_buffer              pointer the data buffer to write data to.
242  * @param p_value               the value to write
243  */
244 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
245
246 /**
247  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
248  * @param p_buffer              pointer the data buffer to read data from.
249  * @param p_value               pointer to the value that will store the data.
250  */
251 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
252
253 /**
254  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
255  * @param p_buffer              pointer the data buffer to read data from.
256  * @param p_value               pointer to the value that will store the data.
257  */
258 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
259
260 /**
261  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
262  * @param p_buffer              pointer the data buffer to read data from.
263  * @param p_value               pointer to the value that will store the data.
264  */
265 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
266
267 /**
268  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
269  * @param p_buffer              pointer the data buffer to read data from.
270  * @param p_value               pointer to the value that will store the data.
271  */
272 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
273
274 /**
275  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
276  * @param p_buffer              pointer the data buffer to write data to.
277  * @param p_value               the value to write
278  */
279 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
280
281 /***
282  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
283  * @param p_buffer              pointer the data buffer to write data to.
284  * @param p_value               the value to write
285  */
286 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
287
288 /**
289  * Reads some bytes from the stream.
290  * @param               p_stream        the stream to read data from.
291  * @param               p_buffer        pointer to the data buffer that will receive the data.
292  * @param               p_size          number of bytes to read.
293  * @param               p_event_mgr     the user event manager to be notified of special events.
294  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
295  */
296 OPJ_UINT32 opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr);
297
298 /**
299  * Writes some bytes to the stream.
300  * @param               p_stream        the stream to write data to.
301  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
302  * @param               p_size          number of bytes to write.
303  * @param               p_event_mgr     the user event manager to be notified of special events.
304  * @return              the number of bytes writtent, or -1 if an error occured.
305  */
306 OPJ_UINT32 opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr);
307
308 /**
309  * Writes the content of the stream buffer to the stream.
310  * @param               p_stream        the stream to write data to.
311  * @param               p_event_mgr     the user event manager to be notified of special events.
312  * @return              true if the data could be flushed, false else.
313  */
314 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
315
316 /**
317  * Skips a number of bytes from the stream.
318  * @param               p_stream        the stream to skip data from.
319  * @param               p_size          the number of bytes to skip.
320  * @param               p_event_mgr     the user event manager to be notified of special events.
321  * @return              the number of bytes skipped, or -1 if an error occured.
322  */
323 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
324
325 /**
326  * Tells the byte offset on the stream (similar to ftell).
327  *
328  * @param               p_stream        the stream to get the information from.
329  *
330  * @return              the current position o fthe stream.
331  */
332 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream);
333
334
335 /**
336  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
337  *
338  * @param               p_stream        the stream to get the information from.
339  *
340  * @return              Number of bytes left before the end of the stream.
341  */
342 OPJ_SIZE_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
343
344 /**
345  * Skips a number of bytes from the stream.
346  * @param               p_stream        the stream to skip data from.
347  * @param               p_size          the number of bytes to skip.
348  * @param               p_event_mgr     the user event manager to be notified of special events.
349  * @return              the number of bytes skipped, or -1 if an error occured.
350  */
351 OPJ_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
352
353 /**
354  * Skips a number of bytes from the stream.
355  * @param               p_stream        the stream to skip data from.
356  * @param               p_size          the number of bytes to skip.
357  * @param               p_event_mgr     the user event manager to be notified of special events.
358  * @return              the number of bytes skipped, or -1 if an error occured.
359  */
360 OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
361
362 /**
363  * Skips a number of bytes from the stream.
364  * @param               p_stream        the stream to skip data from.
365  * @param               p_size          the number of bytes to skip.
366  * @param               p_event_mgr     the user event manager to be notified of special events.
367  * @return              the number of bytes skipped, or -1 if an error occured.
368  */
369 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
370
371 /**
372  * Skips a number of bytes from the stream.
373  * @param               p_stream        the stream to skip data from.
374  * @param               p_size          the number of bytes to skip.
375  * @param               p_event_mgr     the user event manager to be notified of special events.
376  * @return              the number of bytes skipped, or -1 if an error occured.
377  */
378 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
379
380 /**
381  * Seeks a number of bytes from the stream.
382  * @param               p_stream        the stream to skip data from.
383  * @param               p_size          the number of bytes to skip.
384  * @param               p_event_mgr     the user event manager to be notified of special events.
385  * @return              true if the stream is seekable.
386  */
387 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
388
389 /**
390  * Tells if the given stream is seekable.
391  */
392 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
393
394 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
395 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
396 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
397 opj_bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
398
399
400
401 #endif /* __CIO_H */
402