jas_stream.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2003 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * I/O Stream Class
  64. *
  65. * $Id: jas_stream.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $
  66. */
  67. #ifndef JAS_STREAM_H
  68. #define JAS_STREAM_H
  69. /******************************************************************************\
  70. * Includes.
  71. \******************************************************************************/
  72. #include <jasper/jas_config.h>
  73. #include <stdio.h>
  74. #include <limits.h>
  75. #if defined(HAVE_FCNTL_H)
  76. #include <fcntl.h>
  77. #endif
  78. #include <string.h>
  79. #if defined(HAVE_UNISTD_H)
  80. #include <unistd.h>
  81. #endif
  82. #include <jasper/jas_types.h>
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /******************************************************************************\
  87. * Constants.
  88. \******************************************************************************/
  89. /* On most UNIX systems, we probably need to define O_BINARY ourselves. */
  90. #ifndef O_BINARY
  91. #define O_BINARY 0
  92. #endif
  93. /*
  94. * Stream open flags.
  95. */
  96. /* The stream was opened for reading. */
  97. #define JAS_STREAM_READ 0x0001
  98. /* The stream was opened for writing. */
  99. #define JAS_STREAM_WRITE 0x0002
  100. /* The stream was opened for appending. */
  101. #define JAS_STREAM_APPEND 0x0004
  102. /* The stream was opened in binary mode. */
  103. #define JAS_STREAM_BINARY 0x0008
  104. /* The stream should be created/truncated. */
  105. #define JAS_STREAM_CREATE 0x0010
  106. /*
  107. * Stream buffering flags.
  108. */
  109. /* The stream is unbuffered. */
  110. #define JAS_STREAM_UNBUF 0x0000
  111. /* The stream is line buffered. */
  112. #define JAS_STREAM_LINEBUF 0x0001
  113. /* The stream is fully buffered. */
  114. #define JAS_STREAM_FULLBUF 0x0002
  115. /* The buffering mode mask. */
  116. #define JAS_STREAM_BUFMODEMASK 0x000f
  117. /* The memory associated with the buffer needs to be deallocated when the
  118. stream is destroyed. */
  119. #define JAS_STREAM_FREEBUF 0x0008
  120. /* The buffer is currently being used for reading. */
  121. #define JAS_STREAM_RDBUF 0x0010
  122. /* The buffer is currently being used for writing. */
  123. #define JAS_STREAM_WRBUF 0x0020
  124. /*
  125. * Stream error flags.
  126. */
  127. /* The end-of-file has been encountered (on reading). */
  128. #define JAS_STREAM_EOF 0x0001
  129. /* An I/O error has been encountered on the stream. */
  130. #define JAS_STREAM_ERR 0x0002
  131. /* The read/write limit has been exceeded. */
  132. #define JAS_STREAM_RWLIMIT 0x0004
  133. /* The error mask. */
  134. #define JAS_STREAM_ERRMASK \
  135. (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
  136. /*
  137. * Other miscellaneous constants.
  138. */
  139. /* The default buffer size (for fully-buffered operation). */
  140. #define JAS_STREAM_BUFSIZE 8192
  141. /* The default permission mask for file creation. */
  142. #define JAS_STREAM_PERMS 0666
  143. /* The maximum number of characters that can always be put back on a stream. */
  144. #define JAS_STREAM_MAXPUTBACK 16
  145. /******************************************************************************\
  146. * Types.
  147. \******************************************************************************/
  148. /*
  149. * Generic file object.
  150. */
  151. typedef void jas_stream_obj_t;
  152. /*
  153. * Generic file object operations.
  154. */
  155. typedef struct {
  156. /* Read characters from a file object. */
  157. int (*read_)(jas_stream_obj_t *obj, char *buf, int cnt);
  158. /* Write characters to a file object. */
  159. int (*write_)(jas_stream_obj_t *obj, char *buf, int cnt);
  160. /* Set the position for a file object. */
  161. long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
  162. /* Close a file object. */
  163. int (*close_)(jas_stream_obj_t *obj);
  164. } jas_stream_ops_t;
  165. /*
  166. * Stream object.
  167. */
  168. typedef struct {
  169. /* The mode in which the stream was opened. */
  170. int openmode_;
  171. /* The buffering mode. */
  172. int bufmode_;
  173. /* The stream status. */
  174. int flags_;
  175. /* The start of the buffer area to use for reading/writing. */
  176. uchar *bufbase_;
  177. /* The start of the buffer area excluding the extra initial space for
  178. character putback. */
  179. uchar *bufstart_;
  180. /* The buffer size. */
  181. int bufsize_;
  182. /* The current position in the buffer. */
  183. uchar *ptr_;
  184. /* The number of characters that must be read/written before
  185. the buffer needs to be filled/flushed. */
  186. int cnt_;
  187. /* A trivial buffer to be used for unbuffered operation. */
  188. uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
  189. /* The operations for the underlying stream file object. */
  190. jas_stream_ops_t *ops_;
  191. /* The underlying stream file object. */
  192. jas_stream_obj_t *obj_;
  193. /* The number of characters read/written. */
  194. long rwcnt_;
  195. /* The maximum number of characters that may be read/written. */
  196. long rwlimit_;
  197. } jas_stream_t;
  198. /*
  199. * Regular file object.
  200. */
  201. /*
  202. * File descriptor file object.
  203. */
  204. typedef struct {
  205. int fd;
  206. int flags;
  207. #if defined _WIN32 && !defined __MINGW__ && !defined __MINGW32__
  208. char pathname[MAX_PATH + 1];
  209. #else
  210. char pathname[PATH_MAX + 1];
  211. #endif
  212. } jas_stream_fileobj_t;
  213. #define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01
  214. #define JAS_STREAM_FILEOBJ_NOCLOSE 0x02
  215. /*
  216. * Memory file object.
  217. */
  218. typedef struct {
  219. /* The data associated with this file. */
  220. uchar *buf_;
  221. /* The allocated size of the buffer for holding file data. */
  222. int bufsize_;
  223. /* The length of the file. */
  224. int_fast32_t len_;
  225. /* The seek position. */
  226. int_fast32_t pos_;
  227. /* Is the buffer growable? */
  228. int growable_;
  229. /* Was the buffer allocated internally? */
  230. int myalloc_;
  231. } jas_stream_memobj_t;
  232. /******************************************************************************\
  233. * Macros/functions for opening and closing streams.
  234. \******************************************************************************/
  235. /* Open a file as a stream. */
  236. jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
  237. /* Open a memory buffer as a stream. */
  238. jas_stream_t *jas_stream_memopen(char *buf, int bufsize);
  239. /* Open a file descriptor as a stream. */
  240. jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
  241. /* Open a stdio stream as a stream. */
  242. jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
  243. /* Open a temporary file as a stream. */
  244. jas_stream_t *jas_stream_tmpfile(void);
  245. /* Close a stream. */
  246. int jas_stream_close(jas_stream_t *stream);
  247. /******************************************************************************\
  248. * Macros/functions for getting/setting the stream state.
  249. \******************************************************************************/
  250. /* Get the EOF indicator for a stream. */
  251. #define jas_stream_eof(stream) \
  252. (((stream)->flags_ & JAS_STREAM_EOF) != 0)
  253. /* Get the error indicator for a stream. */
  254. #define jas_stream_error(stream) \
  255. (((stream)->flags_ & JAS_STREAM_ERR) != 0)
  256. /* Clear the error indicator for a stream. */
  257. #define jas_stream_clearerr(stream) \
  258. ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
  259. /* Get the read/write limit for a stream. */
  260. #define jas_stream_getrwlimit(stream) \
  261. (((const jas_stream_t *)(stream))->rwlimit_)
  262. /* Set the read/write limit for a stream. */
  263. int jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
  264. /* Get the read/write count for a stream. */
  265. #define jas_stream_getrwcount(stream) \
  266. (((const jas_stream_t *)(stream))->rwcnt_)
  267. /* Set the read/write count for a stream. */
  268. long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt);
  269. /******************************************************************************\
  270. * Macros/functions for I/O.
  271. \******************************************************************************/
  272. /* Read a character from a stream. */
  273. #if defined(DEBUG)
  274. #define jas_stream_getc(stream) jas_stream_getc_func(stream)
  275. #else
  276. #define jas_stream_getc(stream) jas_stream_getc_macro(stream)
  277. #endif
  278. /* Write a character to a stream. */
  279. #if defined(DEBUG)
  280. #define jas_stream_putc(stream, c) jas_stream_putc_func(stream, c)
  281. #else
  282. #define jas_stream_putc(stream, c) jas_stream_putc_macro(stream, c)
  283. #endif
  284. /* Read characters from a stream into a buffer. */
  285. int jas_stream_read(jas_stream_t *stream, void *buf, int cnt);
  286. /* Write characters from a buffer to a stream. */
  287. int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt);
  288. /* Write formatted output to a stream. */
  289. int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...);
  290. /* Write a string to a stream. */
  291. int jas_stream_puts(jas_stream_t *stream, const char *s);
  292. /* Read a line of input from a stream. */
  293. char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize);
  294. /* Look at the next character to be read from a stream without actually
  295. removing it from the stream. */
  296. #define jas_stream_peekc(stream) \
  297. (((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
  298. ((int)(*(stream)->ptr_)))
  299. /* Put a character back on a stream. */
  300. int jas_stream_ungetc(jas_stream_t *stream, int c);
  301. /******************************************************************************\
  302. * Macros/functions for getting/setting the stream position.
  303. \******************************************************************************/
  304. /* Is it possible to seek on this stream? */
  305. int jas_stream_isseekable(jas_stream_t *stream);
  306. /* Set the current position within the stream. */
  307. long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
  308. /* Get the current position within the stream. */
  309. long jas_stream_tell(jas_stream_t *stream);
  310. /* Seek to the beginning of a stream. */
  311. int jas_stream_rewind(jas_stream_t *stream);
  312. /******************************************************************************\
  313. * Macros/functions for flushing.
  314. \******************************************************************************/
  315. /* Flush any pending output to a stream. */
  316. int jas_stream_flush(jas_stream_t *stream);
  317. /******************************************************************************\
  318. * Miscellaneous macros/functions.
  319. \******************************************************************************/
  320. /* Copy data from one stream to another. */
  321. int jas_stream_copy(jas_stream_t *dst, jas_stream_t *src, int n);
  322. /* Display stream contents (for debugging purposes). */
  323. int jas_stream_display(jas_stream_t *stream, FILE *fp, int n);
  324. /* Consume (i.e., discard) characters from stream. */
  325. int jas_stream_gobble(jas_stream_t *stream, int n);
  326. /* Write a character multiple times to a stream. */
  327. int jas_stream_pad(jas_stream_t *stream, int n, int c);
  328. /* Get the size of the file associated with the specified stream.
  329. The specified stream must be seekable. */
  330. long jas_stream_length(jas_stream_t *stream);
  331. /******************************************************************************\
  332. * Internal functions.
  333. \******************************************************************************/
  334. /* The following functions are for internal use only! If you call them
  335. directly, you will die a horrible, miserable, and painful death! */
  336. /* Read a character from a stream. */
  337. #define jas_stream_getc_macro(stream) \
  338. ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
  339. JAS_STREAM_RWLIMIT))) ? \
  340. (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
  341. (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
  342. jas_stream_getc2(stream)) : EOF)
  343. #define jas_stream_getc2(stream) \
  344. ((--(stream)->cnt_ < 0) ? jas_stream_fillbuf(stream, 1) : \
  345. (++(stream)->rwcnt_, (int)(*(stream)->ptr_++)))
  346. /* Write a character to a stream. */
  347. #define jas_stream_putc_macro(stream, c) \
  348. ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
  349. JAS_STREAM_RWLIMIT))) ? \
  350. (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
  351. (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
  352. jas_stream_putc2(stream, c)) : EOF)
  353. #define jas_stream_putc2(stream, c) \
  354. (((stream)->bufmode_ |= JAS_STREAM_WRBUF, --(stream)->cnt_ < 0) ? \
  355. jas_stream_flushbuf((stream), (uchar)(c)) : \
  356. (++(stream)->rwcnt_, (int)(*(stream)->ptr_++ = (c))))
  357. /* These prototypes need to be here for the sake of the stream_getc and
  358. stream_putc macros. */
  359. int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
  360. int jas_stream_flushbuf(jas_stream_t *stream, int c);
  361. int jas_stream_getc_func(jas_stream_t *stream);
  362. int jas_stream_putc_func(jas_stream_t *stream, int c);
  363. #ifdef __cplusplus
  364. }
  365. #endif
  366. #endif