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: jas_stream.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $
68  */
69 
70 #ifndef JAS_STREAM_H
71 #define JAS_STREAM_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 #include <jasper/jas_config.h>
78 
79 #include <stdio.h>
80 #include <limits.h>
81 #if defined(HAVE_FCNTL_H)
82 #include <fcntl.h>
83 #endif
84 #include <string.h>
85 #if defined(HAVE_UNISTD_H)
86 #include <unistd.h>
87 #endif
88 #include <jasper/jas_types.h>
89 
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93 
94 /******************************************************************************\
95 * Constants.
96 \******************************************************************************/
97 
98 /* On most UNIX systems, we probably need to define O_BINARY ourselves. */
99 #ifndef O_BINARY
100 #define O_BINARY	0
101 #endif
102 
103 /*
104  * Stream open flags.
105  */
106 
107 /* The stream was opened for reading. */
108 #define JAS_STREAM_READ	0x0001
109 /* The stream was opened for writing. */
110 #define JAS_STREAM_WRITE	0x0002
111 /* The stream was opened for appending. */
112 #define JAS_STREAM_APPEND	0x0004
113 /* The stream was opened in binary mode. */
114 #define JAS_STREAM_BINARY	0x0008
115 /* The stream should be created/truncated. */
116 #define JAS_STREAM_CREATE	0x0010
117 
118 
119 /*
120  * Stream buffering flags.
121  */
122 
123 /* The stream is unbuffered. */
124 #define JAS_STREAM_UNBUF	0x0000
125 /* The stream is line buffered. */
126 #define JAS_STREAM_LINEBUF	0x0001
127 /* The stream is fully buffered. */
128 #define JAS_STREAM_FULLBUF	0x0002
129 /* The buffering mode mask. */
130 #define	JAS_STREAM_BUFMODEMASK	0x000f
131 
132 /* The memory associated with the buffer needs to be deallocated when the
133   stream is destroyed. */
134 #define JAS_STREAM_FREEBUF	0x0008
135 /* The buffer is currently being used for reading. */
136 #define JAS_STREAM_RDBUF	0x0010
137 /* The buffer is currently being used for writing. */
138 #define JAS_STREAM_WRBUF	0x0020
139 
140 /*
141  * Stream error flags.
142  */
143 
144 /* The end-of-file has been encountered (on reading). */
145 #define JAS_STREAM_EOF	0x0001
146 /* An I/O error has been encountered on the stream. */
147 #define JAS_STREAM_ERR	0x0002
148 /* The read/write limit has been exceeded. */
149 #define	JAS_STREAM_RWLIMIT	0x0004
150 /* The error mask. */
151 #define JAS_STREAM_ERRMASK \
152     (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
153 
154 /*
155  * Other miscellaneous constants.
156  */
157 
158 /* The default buffer size (for fully-buffered operation). */
159 #define JAS_STREAM_BUFSIZE	8192
160 /* The default permission mask for file creation. */
161 #define JAS_STREAM_PERMS	0666
162 
163 /* The maximum number of characters that can always be put back on a stream. */
164 #define	JAS_STREAM_MAXPUTBACK	16
165 
166 /******************************************************************************\
167 * Types.
168 \******************************************************************************/
169 
170 /*
171  * Generic file object.
172  */
173 
174 typedef void jas_stream_obj_t;
175 
176 /*
177  * Generic file object operations.
178  */
179 
180 typedef struct {
181 
182     /* Read characters from a file object. */
183     int (*read_)(jas_stream_obj_t *obj, char *buf, int cnt);
184 
185     /* Write characters to a file object. */
186     int (*write_)(jas_stream_obj_t *obj, char *buf, int cnt);
187 
188     /* Set the position for a file object. */
189     long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
190 
191     /* Close a file object. */
192     int (*close_)(jas_stream_obj_t *obj);
193 
194 } jas_stream_ops_t;
195 
196 /*
197  * Stream object.
198  */
199 
200 typedef struct {
201 
202     /* The mode in which the stream was opened. */
203     int openmode_;
204 
205     /* The buffering mode. */
206     int bufmode_;
207 
208     /* The stream status. */
209     int flags_;
210 
211     /* The start of the buffer area to use for reading/writing. */
212     uchar *bufbase_;
213 
214     /* The start of the buffer area excluding the extra initial space for
215       character putback. */
216     uchar *bufstart_;
217 
218     /* The buffer size. */
219     int bufsize_;
220 
221     /* The current position in the buffer. */
222     uchar *ptr_;
223 
224     /* The number of characters that must be read/written before
225     the buffer needs to be filled/flushed. */
226     int cnt_;
227 
228     /* A trivial buffer to be used for unbuffered operation. */
229     uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
230 
231     /* The operations for the underlying stream file object. */
232     jas_stream_ops_t *ops_;
233 
234     /* The underlying stream file object. */
235     jas_stream_obj_t *obj_;
236 
237     /* The number of characters read/written. */
238     long rwcnt_;
239 
240     /* The maximum number of characters that may be read/written. */
241     long rwlimit_;
242 
243 } jas_stream_t;
244 
245 /*
246  * Regular file object.
247  */
248 
249 /*
250  * File descriptor file object.
251  */
252 typedef struct {
253     int fd;
254     int flags;
255 #if defined _WIN32 && !defined __MINGW__ && !defined __MINGW32__
256     char pathname[MAX_PATH + 1];
257 #else
258     char pathname[PATH_MAX + 1];
259 #endif
260 } jas_stream_fileobj_t;
261 
262 #define	JAS_STREAM_FILEOBJ_DELONCLOSE	0x01
263 #define JAS_STREAM_FILEOBJ_NOCLOSE	0x02
264 
265 /*
266  * Memory file object.
267  */
268 
269 typedef struct {
270 
271     /* The data associated with this file. */
272     uchar *buf_;
273 
274     /* The allocated size of the buffer for holding file data. */
275     int bufsize_;
276 
277     /* The length of the file. */
278     int_fast32_t len_;
279 
280     /* The seek position. */
281     int_fast32_t pos_;
282 
283     /* Is the buffer growable? */
284     int growable_;
285 
286     /* Was the buffer allocated internally? */
287     int myalloc_;
288 
289 } jas_stream_memobj_t;
290 
291 /******************************************************************************\
292 * Macros/functions for opening and closing streams.
293 \******************************************************************************/
294 
295 /* Open a file as a stream. */
296 jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
297 
298 /* Open a memory buffer as a stream. */
299 jas_stream_t *jas_stream_memopen(char *buf, int bufsize);
300 
301 /* Open a file descriptor as a stream. */
302 jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
303 
304 /* Open a stdio stream as a stream. */
305 jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
306 
307 /* Open a temporary file as a stream. */
308 jas_stream_t *jas_stream_tmpfile(void);
309 
310 /* Close a stream. */
311 int jas_stream_close(jas_stream_t *stream);
312 
313 /******************************************************************************\
314 * Macros/functions for getting/setting the stream state.
315 \******************************************************************************/
316 
317 /* Get the EOF indicator for a stream. */
318 #define jas_stream_eof(stream) \
319     (((stream)->flags_ & JAS_STREAM_EOF) != 0)
320 
321 /* Get the error indicator for a stream. */
322 #define jas_stream_error(stream) \
323     (((stream)->flags_ & JAS_STREAM_ERR) != 0)
324 
325 /* Clear the error indicator for a stream. */
326 #define jas_stream_clearerr(stream) \
327     ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
328 
329 /* Get the read/write limit for a stream. */
330 #define	jas_stream_getrwlimit(stream) \
331     (((const jas_stream_t *)(stream))->rwlimit_)
332 
333 /* Set the read/write limit for a stream. */
334 int jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
335 
336 /* Get the read/write count for a stream. */
337 #define	jas_stream_getrwcount(stream) \
338     (((const jas_stream_t *)(stream))->rwcnt_)
339 
340 /* Set the read/write count for a stream. */
341 long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt);
342 
343 /******************************************************************************\
344 * Macros/functions for I/O.
345 \******************************************************************************/
346 
347 /* Read a character from a stream. */
348 #if defined(DEBUG)
349 #define	jas_stream_getc(stream)	jas_stream_getc_func(stream)
350 #else
351 #define jas_stream_getc(stream)	jas_stream_getc_macro(stream)
352 #endif
353 
354 /* Write a character to a stream. */
355 #if defined(DEBUG)
356 #define jas_stream_putc(stream, c)	jas_stream_putc_func(stream, c)
357 #else
358 #define jas_stream_putc(stream, c)	jas_stream_putc_macro(stream, c)
359 #endif
360 
361 /* Read characters from a stream into a buffer. */
362 int jas_stream_read(jas_stream_t *stream, void *buf, int cnt);
363 
364 /* Write characters from a buffer to a stream. */
365 int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt);
366 
367 /* Write formatted output to a stream. */
368 int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...);
369 
370 /* Write a string to a stream. */
371 int jas_stream_puts(jas_stream_t *stream, const char *s);
372 
373 /* Read a line of input from a stream. */
374 char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize);
375 
376 /* Look at the next character to be read from a stream without actually
377   removing it from the stream. */
378 #define	jas_stream_peekc(stream) \
379     (((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
380       ((int)(*(stream)->ptr_)))
381 
382 /* Put a character back on a stream. */
383 int jas_stream_ungetc(jas_stream_t *stream, int c);
384 
385 /******************************************************************************\
386 * Macros/functions for getting/setting the stream position.
387 \******************************************************************************/
388 
389 /* Is it possible to seek on this stream? */
390 int jas_stream_isseekable(jas_stream_t *stream);
391 
392 /* Set the current position within the stream. */
393 long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
394 
395 /* Get the current position within the stream. */
396 long jas_stream_tell(jas_stream_t *stream);
397 
398 /* Seek to the beginning of a stream. */
399 int jas_stream_rewind(jas_stream_t *stream);
400 
401 /******************************************************************************\
402 * Macros/functions for flushing.
403 \******************************************************************************/
404 
405 /* Flush any pending output to a stream. */
406 int jas_stream_flush(jas_stream_t *stream);
407 
408 /******************************************************************************\
409 * Miscellaneous macros/functions.
410 \******************************************************************************/
411 
412 /* Copy data from one stream to another. */
413 int jas_stream_copy(jas_stream_t *dst, jas_stream_t *src, int n);
414 
415 /* Display stream contents (for debugging purposes). */
416 int jas_stream_display(jas_stream_t *stream, FILE *fp, int n);
417 
418 /* Consume (i.e., discard) characters from stream. */
419 int jas_stream_gobble(jas_stream_t *stream, int n);
420 
421 /* Write a character multiple times to a stream. */
422 int jas_stream_pad(jas_stream_t *stream, int n, int c);
423 
424 /* Get the size of the file associated with the specified stream.
425   The specified stream must be seekable. */
426 long jas_stream_length(jas_stream_t *stream);
427 
428 /******************************************************************************\
429 * Internal functions.
430 \******************************************************************************/
431 
432 /* The following functions are for internal use only!  If you call them
433 directly, you will die a horrible, miserable, and painful death! */
434 
435 /* Read a character from a stream. */
436 #define jas_stream_getc_macro(stream) \
437     ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
438       JAS_STREAM_RWLIMIT))) ? \
439       (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
440       (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
441       jas_stream_getc2(stream)) : EOF)
442 #define jas_stream_getc2(stream) \
443     ((--(stream)->cnt_ < 0) ? jas_stream_fillbuf(stream, 1) : \
444       (++(stream)->rwcnt_, (int)(*(stream)->ptr_++)))
445 
446 /* Write a character to a stream. */
447 #define jas_stream_putc_macro(stream, c) \
448     ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
449       JAS_STREAM_RWLIMIT))) ? \
450       (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
451       (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
452       jas_stream_putc2(stream, c)) : EOF)
453 #define jas_stream_putc2(stream, c) \
454     (((stream)->bufmode_ |= JAS_STREAM_WRBUF, --(stream)->cnt_ < 0) ? \
455       jas_stream_flushbuf((stream), (uchar)(c)) : \
456       (++(stream)->rwcnt_, (int)(*(stream)->ptr_++ = (c))))
457 
458 /* These prototypes need to be here for the sake of the stream_getc and
459 stream_putc macros. */
460 int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
461 int jas_stream_flushbuf(jas_stream_t *stream, int c);
462 int jas_stream_getc_func(jas_stream_t *stream);
463 int jas_stream_putc_func(jas_stream_t *stream, int c);
464 
465 #ifdef __cplusplus
466 }
467 #endif
468 
469 #endif
470