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