1/*
2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3 *   British Columbia.
4 * Copyright (c) 2001-2002 Michael David Adams.
5 * All rights reserved.
6 */
7
8/* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A.  Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B.  Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C.  Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D.  User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E.  If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F.  This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113/*
114 * I/O Stream Class
115 *
116 * $Id: jas_stream.h 14449 2005-10-20 12:15:56Z stippi $
117 */
118
119#ifndef JAS_STREAM_H
120#define JAS_STREAM_H
121
122/******************************************************************************\
123* Includes.
124\******************************************************************************/
125
126#include <stdio.h>
127#include <fcntl.h>
128#include <string.h>
129#if defined(HAVE_UNISTD_H)
130#include <unistd.h>
131#endif
132#include <jasper/jas_types.h>
133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
138/******************************************************************************\
139* Constants.
140\******************************************************************************/
141
142/* On most UNIX systems, we probably need to define O_BINARY ourselves. */
143#ifndef O_BINARY
144#define O_BINARY	0
145#endif
146
147/*
148 * Stream open flags.
149 */
150
151/* The stream was opened for reading. */
152#define JAS_STREAM_READ	0x0001
153/* The stream was opened for writing. */
154#define JAS_STREAM_WRITE	0x0002
155/* The stream was opened for appending. */
156#define JAS_STREAM_APPEND	0x0004
157/* The stream was opened in binary mode. */
158#define JAS_STREAM_BINARY	0x0008
159/* The stream should be created/truncated. */
160#define JAS_STREAM_CREATE	0x0010
161
162#define JAS_STREAM_NOCLOSE	0x0020
163
164/*
165 * Stream buffering flags.
166 */
167
168/* The stream is unbuffered. */
169#define JAS_STREAM_UNBUF	0x0000
170/* The stream is line buffered. */
171#define JAS_STREAM_LINEBUF	0x0001
172/* The stream is fully buffered. */
173#define JAS_STREAM_FULLBUF	0x0002
174/* The buffering mode mask. */
175#define	JAS_STREAM_BUFMODEMASK	0x000f
176
177/* The memory associated with the buffer needs to be deallocated when the
178  stream is destroyed. */
179#define JAS_STREAM_FREEBUF	0x0008
180/* The buffer is currently being used for reading. */
181#define JAS_STREAM_RDBUF	0x0010
182/* The buffer is currently being used for writing. */
183#define JAS_STREAM_WRBUF	0x0020
184
185/*
186 * Stream error flags.
187 */
188
189/* The end-of-file has been encountered (on reading). */
190#define JAS_STREAM_EOF	0x0001
191/* An I/O error has been encountered on the stream. */
192#define JAS_STREAM_ERR	0x0002
193/* The read/write limit has been exceeded. */
194#define	JAS_STREAM_RWLIMIT	0x0004
195/* The error mask. */
196#define JAS_STREAM_ERRMASK \
197	(JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
198
199/*
200 * Other miscellaneous constants.
201 */
202
203/* The default buffer size (for fully-buffered operation). */
204#define JAS_STREAM_BUFSIZE	8192
205/* The default permission mask for file creation. */
206#define JAS_STREAM_PERMS	0666
207
208/* The maximum number of characters that can always be put back on a stream. */
209#define	JAS_STREAM_MAXPUTBACK	16
210
211/******************************************************************************\
212* Types.
213\******************************************************************************/
214
215/*
216 * Generic file object.
217 */
218
219typedef void jas_stream_obj_t;
220
221/*
222 * Generic file object operations.
223 */
224
225typedef struct {
226
227	/* Read characters from a file object. */
228	int (*read_)(jas_stream_obj_t *obj, char *buf, int cnt);
229
230	/* Write characters to a file object. */
231	int (*write_)(jas_stream_obj_t *obj, char *buf, int cnt);
232
233	/* Set the position for a file object. */
234	long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
235
236	/* Close a file object. */
237	int (*close_)(jas_stream_obj_t *obj);
238
239} jas_stream_ops_t;
240
241/*
242 * Stream object.
243 */
244
245typedef struct {
246
247	/* The mode in which the stream was opened. */
248	int openmode_;
249
250	/* The buffering mode. */
251	int bufmode_;
252
253	/* The stream status. */
254	int flags_;
255
256	/* The start of the buffer area to use for reading/writing. */
257	jpr_uchar_t *bufbase_;
258
259	/* The start of the buffer area excluding the extra initial space for
260	  character putback. */
261	jpr_uchar_t *bufstart_;
262
263	/* The buffer size. */
264	int bufsize_;
265
266	/* The current position in the buffer. */
267	jpr_uchar_t *ptr_;
268
269	/* The number of characters that must be read/written before
270	the buffer needs to be filled/flushed. */
271	int cnt_;
272
273	/* A trivial buffer to be used for unbuffered operation. */
274	jpr_uchar_t tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
275
276	/* The operations for the underlying stream file object. */
277	jas_stream_ops_t *ops_;
278
279	/* The underlying stream file object. */
280	jas_stream_obj_t *obj_;
281
282	/* The number of characters read/written. */
283	long rwcnt_;
284
285	/* The maximum number of characters that may be read/written. */
286	long rwlimit_;
287
288} jas_stream_t;
289
290/*
291 * Regular file object.
292 */
293
294/* Note: This is simply a file descriptor. */
295typedef int jas_stream_fileobj_t;
296
297/*
298 * Memory file object.
299 */
300
301typedef struct {
302
303	/* The data associated with this file. */
304	jpr_uchar_t *buf_;
305
306	/* The allocated size of the buffer for holding file data. */
307	int bufsize_;
308
309	/* The length of the file. */
310	int_fast32_t len_;
311
312	/* The seek position. */
313	int_fast32_t pos_;
314
315	/* Is the buffer growable? */
316	int growable_;
317
318	/* Was the buffer allocated internally? */
319	int myalloc_;
320
321} jas_stream_memobj_t;
322
323/******************************************************************************\
324* Macros/functions for opening and closing streams.
325\******************************************************************************/
326
327/* Open a file as a stream. */
328jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
329
330/* Open a memory buffer as a stream. */
331jas_stream_t *jas_stream_memopen(char *buf, int bufsize);
332
333/* Open a file descriptor as a stream. */
334jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
335
336/* Open a stdio stream as a stream. */
337jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
338
339/* Open a temporary file as a stream. */
340jas_stream_t *jas_stream_tmpfile();
341
342/* Close a stream. */
343int jas_stream_close(jas_stream_t *stream);
344
345/******************************************************************************\
346* Macros/functions for getting/setting the stream state.
347\******************************************************************************/
348
349/* Get the EOF indicator for a stream. */
350#define jas_stream_eof(stream) \
351	(((stream)->flags_ & JAS_STREAM_EOF) != 0)
352
353/* Get the error indicator for a stream. */
354#define jas_stream_error(stream) \
355	(((stream)->flags_ & JAS_STREAM_ERR) != 0)
356
357/* Clear the error indicator for a stream. */
358#define jas_stream_clearerr(stream) \
359	((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
360
361/* Get the read/write limit for a stream. */
362#define	jas_stream_getrwlimit(stream) \
363	(((const jas_stream_t *)(stream))->rwlimit_)
364
365/* Set the read/write limit for a stream. */
366int jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
367
368/* Get the read/write count for a stream. */
369#define	jas_stream_getrwcount(stream) \
370	(((const jas_stream_t *)(stream))->rwcnt_)
371
372/* Set the read/write count for a stream. */
373long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt);
374
375/******************************************************************************\
376* Macros/functions for I/O.
377\******************************************************************************/
378
379/* Read a character from a stream. */
380#if defined(DEBUG)
381#define	jas_stream_getc(stream)	jas_stream_getc_func(stream)
382#else
383#define jas_stream_getc(stream)	jas_stream_getc_macro(stream)
384#endif
385
386/* Write a character to a stream. */
387#if defined(DEBUG)
388#define jas_stream_putc(stream, c)	jas_stream_putc_func(stream, c)
389#else
390#define jas_stream_putc(stream, c)	jas_stream_putc_macro(stream, c)
391#endif
392
393/* Read characters from a stream into a buffer. */
394int jas_stream_read(jas_stream_t *stream, void *buf, int cnt);
395
396/* Write characters from a buffer to a stream. */
397int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt);
398
399/* Write formatted output to a stream. */
400int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...);
401
402/* Write a string to a stream. */
403int jas_stream_puts(jas_stream_t *stream, const char *s);
404
405/* Read a line of input from a stream. */
406char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize);
407
408/* Look at the next character to be read from a stream without actually
409  removing it from the stream. */
410#define	jas_stream_peekc(stream) \
411	(((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
412	  ((int)(*(stream)->ptr_)))
413
414/* Put a character back on a stream. */
415int jas_stream_ungetc(jas_stream_t *stream, int c);
416
417/******************************************************************************\
418* Macros/functions for getting/setting the stream position.
419\******************************************************************************/
420
421/* Is it possible to seek on this stream? */
422int jas_stream_isseekable(jas_stream_t *stream);
423
424/* Set the current position within the stream. */
425long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
426
427/* Get the current position within the stream. */
428long jas_stream_tell(jas_stream_t *stream);
429
430/* Seek to the beginning of a stream. */
431int jas_stream_rewind(jas_stream_t *stream);
432
433/******************************************************************************\
434* Macros/functions for flushing.
435\******************************************************************************/
436
437/* Flush any pending output to a stream. */
438int jas_stream_flush(jas_stream_t *stream);
439
440/******************************************************************************\
441* Miscellaneous macros/functions.
442\******************************************************************************/
443
444/* Copy data from one stream to another. */
445int jas_stream_copy(jas_stream_t *dst, jas_stream_t *src, int n);
446
447/* Display stream contents (for debugging purposes). */
448int jas_stream_display(jas_stream_t *stream, FILE *fp, int n);
449
450/* Consume (i.e., discard) characters from stream. */
451int jas_stream_gobble(jas_stream_t *stream, int n);
452
453/* Get the size of the file associated with the specified stream.
454  The specified stream must be seekable. */
455long jas_stream_length(jas_stream_t *stream);
456
457/******************************************************************************\
458* Internal functions.
459\******************************************************************************/
460
461/* The following functions are for internal use only!  If you call them
462directly, you will die a horrible, miserable, and painful death! */
463
464/* Read a character from a stream. */
465#define jas_stream_getc_macro(stream) \
466	((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
467	  JAS_STREAM_RWLIMIT))) ? \
468	  (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
469	  (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
470	  jas_stream_getc2(stream)) : EOF)
471#define jas_stream_getc2(stream) \
472	((--(stream)->cnt_ < 0) ? jas_stream_fillbuf(stream, 1) : \
473	  (++(stream)->rwcnt_, (int)(*(stream)->ptr_++)))
474
475/* Write a character to a stream. */
476#define jas_stream_putc_macro(stream, c) \
477	((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
478	  JAS_STREAM_RWLIMIT))) ? \
479	  (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
480	  (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
481	  jas_stream_putc2(stream, c)) : EOF)
482#define jas_stream_putc2(stream, c) \
483	(((stream)->bufmode_ |= JAS_STREAM_WRBUF, --(stream)->cnt_ < 0) ? \
484	  jas_stream_flushbuf((stream), (jpr_uchar_t)(c)) : \
485	  (++(stream)->rwcnt_, (int)(*(stream)->ptr_++ = (c))))
486
487/* These prototypes need to be here for the sake of the stream_getc and
488stream_putc macros. */
489int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
490int jas_stream_flushbuf(jas_stream_t *stream, int c);
491int jas_stream_getc_func(jas_stream_t *stream);
492int jas_stream_putc_func(jas_stream_t *stream, int c);
493
494#ifdef __cplusplus
495}
496#endif
497
498#endif
499