cio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #ifndef OPJ_CIO_H
  40. #define OPJ_CIO_H
  41. /**
  42. @file cio.h
  43. @brief Implementation of a byte input-output process (CIO)
  44. The functions in CIO.C have for goal to realize a byte input / output process.
  45. */
  46. /** @defgroup CIO CIO - byte input-output stream */
  47. /*@{*/
  48. #include "opj_config_private.h"
  49. /* ----------------------------------------------------------------------- */
  50. #if defined(OPJ_BIG_ENDIAN)
  51. #define opj_write_bytes opj_write_bytes_BE
  52. #define opj_read_bytes opj_read_bytes_BE
  53. #define opj_write_double opj_write_double_BE
  54. #define opj_read_double opj_read_double_BE
  55. #define opj_write_float opj_write_float_BE
  56. #define opj_read_float opj_read_float_BE
  57. #else
  58. #define opj_write_bytes opj_write_bytes_LE
  59. #define opj_read_bytes opj_read_bytes_LE
  60. #define opj_write_double opj_write_double_LE
  61. #define opj_read_double opj_read_double_LE
  62. #define opj_write_float opj_write_float_LE
  63. #define opj_read_float opj_read_float_LE
  64. #endif
  65. #define OPJ_STREAM_STATUS_OUTPUT 0x1U
  66. #define OPJ_STREAM_STATUS_INPUT 0x2U
  67. #define OPJ_STREAM_STATUS_END 0x4U
  68. #define OPJ_STREAM_STATUS_ERROR 0x8U
  69. /**
  70. Byte input-output stream.
  71. */
  72. typedef struct opj_stream_private {
  73. /**
  74. * User data, be it files, ... The actual data depends on the type of the stream.
  75. */
  76. void * m_user_data;
  77. /**
  78. * Pointer to function to free m_user_data (NULL at initialization)
  79. * when destroying the stream. If pointer is NULL the function is not
  80. * called and the m_user_data is not freed (even if non-NULL).
  81. */
  82. opj_stream_free_user_data_fn m_free_user_data_fn;
  83. /**
  84. * User data length
  85. */
  86. OPJ_UINT64 m_user_data_length;
  87. /**
  88. * Pointer to actual read function (NULL at the initialization of the cio.
  89. */
  90. opj_stream_read_fn m_read_fn;
  91. /**
  92. * Pointer to actual write function (NULL at the initialization of the cio.
  93. */
  94. opj_stream_write_fn m_write_fn;
  95. /**
  96. * Pointer to actual skip function (NULL at the initialization of the cio.
  97. * There is no seek function to prevent from back and forth slow procedures.
  98. */
  99. opj_stream_skip_fn m_skip_fn;
  100. /**
  101. * Pointer to actual seek function (if available).
  102. */
  103. opj_stream_seek_fn m_seek_fn;
  104. /**
  105. * Actual data stored into the stream if read from. Data is read by chunk of fixed size.
  106. * you should never access this data directly.
  107. */
  108. OPJ_BYTE * m_stored_data;
  109. /**
  110. * Pointer to the current read data.
  111. */
  112. OPJ_BYTE * m_current_data;
  113. /**
  114. * FIXME DOC.
  115. */
  116. OPJ_OFF_T(* m_opj_skip)(struct opj_stream_private *, OPJ_OFF_T,
  117. struct opj_event_mgr *);
  118. /**
  119. * FIXME DOC.
  120. */
  121. OPJ_BOOL(* m_opj_seek)(struct opj_stream_private *, OPJ_OFF_T,
  122. struct opj_event_mgr *);
  123. /**
  124. * number of bytes containing in the buffer.
  125. */
  126. OPJ_SIZE_T m_bytes_in_buffer;
  127. /**
  128. * The number of bytes read/written from the beginning of the stream
  129. */
  130. OPJ_OFF_T m_byte_offset;
  131. /**
  132. * The size of the buffer.
  133. */
  134. OPJ_SIZE_T m_buffer_size;
  135. /**
  136. * Flags to tell the status of the stream.
  137. * Used with OPJ_STREAM_STATUS_* defines.
  138. */
  139. OPJ_UINT32 m_status;
  140. }
  141. opj_stream_private_t;
  142. /** @name Exported functions (see also openjpeg.h) */
  143. /*@{*/
  144. /* ----------------------------------------------------------------------- */
  145. /**
  146. * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
  147. * @param p_buffer pointer the data buffer to write data to.
  148. * @param p_value the value to write
  149. * @param p_nb_bytes the number of bytes to write
  150. */
  151. void opj_write_bytes_BE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value,
  152. OPJ_UINT32 p_nb_bytes);
  153. /**
  154. * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
  155. * @param p_buffer pointer the data buffer to read data from.
  156. * @param p_value pointer to the value that will store the data.
  157. * @param p_nb_bytes the nb bytes to read.
  158. * @return the number of bytes read or -1 if an error occurred.
  159. */
  160. void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value,
  161. OPJ_UINT32 p_nb_bytes);
  162. /**
  163. * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
  164. * @param p_buffer pointer the data buffer to write data to.
  165. * @param p_value the value to write
  166. * @param p_nb_bytes the number of bytes to write
  167. * @return the number of bytes written or -1 if an error occurred
  168. */
  169. void opj_write_bytes_LE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value,
  170. OPJ_UINT32 p_nb_bytes);
  171. /**
  172. * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
  173. * @param p_buffer pointer the data buffer to read data from.
  174. * @param p_value pointer to the value that will store the data.
  175. * @param p_nb_bytes the nb bytes to read.
  176. * @return the number of bytes read or -1 if an error occurred.
  177. */
  178. void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value,
  179. OPJ_UINT32 p_nb_bytes);
  180. /**
  181. * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
  182. * @param p_buffer pointer the data buffer to write data to.
  183. * @param p_value the value to write
  184. */
  185. void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
  186. /***
  187. * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
  188. * @param p_buffer pointer the data buffer to write data to.
  189. * @param p_value the value to write
  190. */
  191. void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
  192. /**
  193. * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
  194. * @param p_buffer pointer the data buffer to read data from.
  195. * @param p_value pointer to the value that will store the data.
  196. */
  197. void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
  198. /**
  199. * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
  200. * @param p_buffer pointer the data buffer to read data from.
  201. * @param p_value pointer to the value that will store the data.
  202. */
  203. void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
  204. /**
  205. * Reads some bytes from the given data buffer, this function is used in Little 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. */
  209. void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
  210. /**
  211. * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
  212. * @param p_buffer pointer the data buffer to read data from.
  213. * @param p_value pointer to the value that will store the data.
  214. */
  215. void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
  216. /**
  217. * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
  218. * @param p_buffer pointer the data buffer to write data to.
  219. * @param p_value the value to write
  220. */
  221. void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
  222. /***
  223. * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
  224. * @param p_buffer pointer the data buffer to write data to.
  225. * @param p_value the value to write
  226. */
  227. void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
  228. /**
  229. * Reads some bytes from the stream.
  230. * @param p_stream the stream to read data from.
  231. * @param p_buffer pointer to the data buffer that will receive the data.
  232. * @param p_size number of bytes to read.
  233. * @param p_event_mgr the user event manager to be notified of special events.
  234. * @return the number of bytes read, or -1 if an error occurred or if the stream is at the end.
  235. */
  236. OPJ_SIZE_T opj_stream_read_data(opj_stream_private_t * p_stream,
  237. OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
  238. /**
  239. * Writes some bytes to the stream.
  240. * @param p_stream the stream to write data to.
  241. * @param p_buffer pointer to the data buffer holds the data to be writtent.
  242. * @param p_size number of bytes to write.
  243. * @param p_event_mgr the user event manager to be notified of special events.
  244. * @return the number of bytes writtent, or -1 if an error occurred.
  245. */
  246. OPJ_SIZE_T opj_stream_write_data(opj_stream_private_t * p_stream,
  247. const OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size,
  248. struct opj_event_mgr * p_event_mgr);
  249. /**
  250. * Writes the content of the stream buffer to the stream.
  251. * @param p_stream the stream to write data to.
  252. * @param p_event_mgr the user event manager to be notified of special events.
  253. * @return true if the data could be flushed, false else.
  254. */
  255. OPJ_BOOL opj_stream_flush(opj_stream_private_t * p_stream,
  256. struct opj_event_mgr * p_event_mgr);
  257. /**
  258. * Skips a number of bytes from the stream.
  259. * @param p_stream the stream to skip data from.
  260. * @param p_size the number of bytes to skip.
  261. * @param p_event_mgr the user event manager to be notified of special events.
  262. * @return the number of bytes skipped, or -1 if an error occurred.
  263. */
  264. OPJ_OFF_T opj_stream_skip(opj_stream_private_t * p_stream, OPJ_OFF_T p_size,
  265. struct opj_event_mgr * p_event_mgr);
  266. /**
  267. * Tells the byte offset on the stream (similar to ftell).
  268. *
  269. * @param p_stream the stream to get the information from.
  270. *
  271. * @return the current position o fthe stream.
  272. */
  273. OPJ_OFF_T opj_stream_tell(const opj_stream_private_t * p_stream);
  274. /**
  275. * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
  276. *
  277. * @param p_stream the stream to get the information from.
  278. *
  279. * @return Number of bytes left before the end of the stream.
  280. */
  281. OPJ_OFF_T opj_stream_get_number_byte_left(const opj_stream_private_t *
  282. p_stream);
  283. /**
  284. * Skips a number of bytes from the stream.
  285. * @param p_stream the stream to skip data from.
  286. * @param p_size the number of bytes to skip.
  287. * @param p_event_mgr the user event manager to be notified of special events.
  288. * @return the number of bytes skipped, or -1 if an error occurred.
  289. */
  290. OPJ_OFF_T opj_stream_write_skip(opj_stream_private_t * p_stream,
  291. OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
  292. /**
  293. * Skips a number of bytes from the stream.
  294. * @param p_stream the stream to skip data from.
  295. * @param p_size the number of bytes to skip.
  296. * @param p_event_mgr the user event manager to be notified of special events.
  297. * @return the number of bytes skipped, or -1 if an error occurred.
  298. */
  299. OPJ_OFF_T opj_stream_read_skip(opj_stream_private_t * p_stream,
  300. OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
  301. /**
  302. * Skips a number of bytes from the stream.
  303. * @param p_stream the stream to skip data from.
  304. * @param p_size the number of bytes to skip.
  305. * @param p_event_mgr the user event manager to be notified of special events.
  306. * @return OPJ_TRUE if success, or OPJ_FALSE if an error occurred.
  307. */
  308. OPJ_BOOL opj_stream_read_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size,
  309. struct opj_event_mgr * p_event_mgr);
  310. /**
  311. * Skips a number of bytes from the stream.
  312. * @param p_stream the stream to skip data from.
  313. * @param p_size the number of bytes to skip.
  314. * @param p_event_mgr the user event manager to be notified of special events.
  315. * @return the number of bytes skipped, or -1 if an error occurred.
  316. */
  317. OPJ_BOOL opj_stream_write_seek(opj_stream_private_t * p_stream,
  318. OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
  319. /**
  320. * Seeks a number of bytes from the stream.
  321. * @param p_stream the stream to skip data from.
  322. * @param p_size the number of bytes to skip.
  323. * @param p_event_mgr the user event manager to be notified of special events.
  324. * @return true if the stream is seekable.
  325. */
  326. OPJ_BOOL opj_stream_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size,
  327. struct opj_event_mgr * p_event_mgr);
  328. /**
  329. * Tells if the given stream is seekable.
  330. */
  331. OPJ_BOOL opj_stream_has_seek(const opj_stream_private_t * p_stream);
  332. /**
  333. * FIXME DOC.
  334. */
  335. OPJ_SIZE_T opj_stream_default_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
  336. void * p_user_data);
  337. /**
  338. * FIXME DOC.
  339. */
  340. OPJ_SIZE_T opj_stream_default_write(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
  341. void * p_user_data);
  342. /**
  343. * FIXME DOC.
  344. */
  345. OPJ_OFF_T opj_stream_default_skip(OPJ_OFF_T p_nb_bytes, void * p_user_data);
  346. /**
  347. * FIXME DOC.
  348. */
  349. OPJ_BOOL opj_stream_default_seek(OPJ_OFF_T p_nb_bytes, void * p_user_data);
  350. /* ----------------------------------------------------------------------- */
  351. /*@}*/
  352. /*@}*/
  353. #endif /* OPJ_CIO_H */