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