JasPer 2.0.33
 
Loading...
Searching...
No Matches
jas_stream.h
Go to the documentation of this file.
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
69#ifndef JAS_STREAM_H
70#define JAS_STREAM_H
71
72/******************************************************************************\
73* Includes.
74\******************************************************************************/
75
76/* The configuration header file should be included first. */
77#include <jasper/jas_config.h> /* IWYU pragma: export */
78
79#include <stdio.h>
80#if defined(JAS_HAVE_FCNTL_H)
81#include <fcntl.h>
82#endif
83#include <jasper/jas_types.h>
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
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 * Stream buffering flags.
120 */
121
122/* The stream is unbuffered. */
123#define JAS_STREAM_UNBUF 0x0000
124/* The stream is line buffered. */
125#define JAS_STREAM_LINEBUF 0x0001
126/* The stream is fully buffered. */
127#define JAS_STREAM_FULLBUF 0x0002
128/* The buffering mode mask. */
129#define JAS_STREAM_BUFMODEMASK 0x000f
130
131/* The memory associated with the buffer needs to be deallocated when the
132 stream is destroyed. */
133#define JAS_STREAM_FREEBUF 0x0008
134/* The buffer is currently being used for reading. */
135#define JAS_STREAM_RDBUF 0x0010
136/* The buffer is currently being used for writing. */
137#define JAS_STREAM_WRBUF 0x0020
138
139/*
140 * Stream error flags.
141 */
142
143/* The end-of-file has been encountered (on reading). */
144#define JAS_STREAM_EOF 0x0001
145/* An I/O error has been encountered on the stream. */
146#define JAS_STREAM_ERR 0x0002
147/* The read/write limit has been exceeded. */
148#define JAS_STREAM_RWLIMIT 0x0004
149/* The error mask. */
150#define JAS_STREAM_ERRMASK \
151 (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
152
153/*
154 * Other miscellaneous constants.
155 */
156
157/* The default buffer size (for fully-buffered operation). */
158#define JAS_STREAM_BUFSIZE 8192
159/* The default permission mask for file creation. */
160#define JAS_STREAM_PERMS 0666
161
162/* The maximum number of characters that can always be put back on a stream. */
163#define JAS_STREAM_MAXPUTBACK 16
164
165/******************************************************************************\
166* Types.
167\******************************************************************************/
168
169/*
170 * Generic file object.
171 */
172
173typedef void jas_stream_obj_t;
174
175/*
176 * Generic file object operations.
177 */
178
179typedef struct {
180
181 /* Read characters from a file object. */
182 int (*read_)(jas_stream_obj_t *obj, char *buf, unsigned cnt);
183
184 /* Write characters to a file object. */
185 int (*write_)(jas_stream_obj_t *obj, const char *buf, unsigned cnt);
186
187 /* Set the position for a file object. */
188 long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
189
190 /* Close a file object. */
191 int (*close_)(jas_stream_obj_t *obj);
192
193} jas_stream_ops_t;
194
195/*
196 * Stream object.
197 */
198
199typedef struct {
200
201 /* The mode in which the stream was opened. */
202 int openmode_;
203
204 /* The buffering mode. */
205 int bufmode_;
206
207 /* The stream status. */
208 int flags_;
209
210 /* The start of the buffer area to use for reading/writing. */
211 jas_uchar *bufbase_;
212
213 /* The start of the buffer area excluding the extra initial space for
214 character putback. */
215 jas_uchar *bufstart_;
216
217 /* The buffer size. */
218 int bufsize_;
219
220 /* The current position in the buffer. */
221 jas_uchar *ptr_;
222
223 /* The number of characters that must be read/written before
224 the buffer needs to be filled/flushed. */
225 int cnt_;
226
227 /* A trivial buffer to be used for unbuffered operation. */
228 jas_uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
229
230 /* The operations for the underlying stream file object. */
231 const jas_stream_ops_t *ops_;
232
233 /* The underlying stream file object. */
234 jas_stream_obj_t *obj_;
235
236 /* The number of characters read/written. */
237 long rwcnt_;
238
239 /* The maximum number of characters that may be read/written. */
240 long rwlimit_;
241
242} jas_stream_t;
243
244/*
245 * Regular file object.
246 */
247
248/*
249 * File descriptor file object.
250 */
251typedef struct {
252 int fd;
253 int flags;
254 char pathname[L_tmpnam + 1];
255} jas_stream_fileobj_t;
256
257/* Delete underlying file object upon stream close. */
258#define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01
259/* Do not close underlying file object upon stream close. */
260#define JAS_STREAM_FILEOBJ_NOCLOSE 0x02
261
262/*
263 * Memory file object.
264 */
265
266typedef struct {
267
268 /* The data associated with this file. */
269 jas_uchar *buf_;
270
271 /* The allocated size of the buffer for holding file data. */
272 size_t bufsize_;
273
274 /* The length of the file. */
275 uint_fast32_t len_;
276
277 /* The seek position. */
278 uint_fast32_t pos_;
279
280 /* Is the buffer growable? */
281 int growable_;
282
283 /* Was the buffer allocated internally? */
284 int myalloc_;
285
286} jas_stream_memobj_t;
287
288/******************************************************************************\
289* Macros/functions for opening and closing streams.
290\******************************************************************************/
291
306JAS_DLLEXPORT
307jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
308
343JAS_DLLEXPORT
344jas_stream_t *jas_stream_memopen(char *buffer, int buffer_size);
345
351JAS_DLLEXPORT
352jas_stream_t *jas_stream_memopen2(char *buffer, size_t buffer_size);
353
368JAS_DLLEXPORT
369jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
370
393JAS_DLLEXPORT
394jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
395
410JAS_DLLEXPORT jas_stream_t *jas_stream_tmpfile(void);
411
430JAS_DLLEXPORT
431int jas_stream_close(jas_stream_t *stream);
432
433/******************************************************************************\
434* Macros/functions for getting/setting the stream state.
435\******************************************************************************/
436
447#define jas_stream_eof(stream) \
448 (((stream)->flags_ & JAS_STREAM_EOF) != 0)
449
460#define jas_stream_error(stream) \
461 (((stream)->flags_ & JAS_STREAM_ERR) != 0)
462
471#define jas_stream_clearerr(stream) \
472 ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
473
484#define jas_stream_getrwlimit(stream) \
485 (((const jas_stream_t *)(stream))->rwlimit_)
486
501JAS_DLLEXPORT long jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
502
513#define jas_stream_getrwcount(stream) \
514 (((const jas_stream_t *)(stream))->rwcnt_)
515
528JAS_DLLEXPORT long jas_stream_setrwcount(jas_stream_t *stream, long rw_count);
529
530/******************************************************************************\
531* Macros/functions for I/O.
532\******************************************************************************/
533
534/* Read a character from a stream. */
535#ifndef NDEBUG
540#define jas_stream_getc(stream) jas_stream_getc_func(stream)
541#else
542#define jas_stream_getc(stream) jas_stream_getc_macro(stream)
543#endif
544
545/* Write a character to a stream. */
546#ifndef NDEBUG
551#define jas_stream_putc(stream, c) jas_stream_putc_func(stream, c)
552#else
553#define jas_stream_putc(stream, c) jas_stream_putc_macro(stream, c)
554#endif
555
593JAS_DLLEXPORT
594unsigned jas_stream_read(jas_stream_t *stream, void *buffer, unsigned count);
595
619JAS_DLLEXPORT
620unsigned jas_stream_peek(jas_stream_t *stream, void *buffer, size_t count);
621
653JAS_DLLEXPORT
654unsigned jas_stream_write(jas_stream_t *stream, const void *buffer,
655 unsigned count);
656
678JAS_DLLEXPORT
679int jas_stream_printf(jas_stream_t *stream, const char *format, ...);
680
696JAS_DLLEXPORT int jas_stream_puts(jas_stream_t *stream, const char *s);
697
719JAS_DLLEXPORT char *
720jas_stream_gets(jas_stream_t *stream, char *buffer, int buffer_size);
721
740#define jas_stream_peekc(stream) \
741 (((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
742 ((int)(*(stream)->ptr_)))
743
767JAS_DLLEXPORT int
768jas_stream_ungetc(jas_stream_t *stream, int c);
769
770/******************************************************************************\
771* Macros/functions for getting/setting the stream position.
772\******************************************************************************/
773
789JAS_ATTRIBUTE_PURE JAS_DLLEXPORT
790int jas_stream_isseekable(jas_stream_t *stream);
791
811JAS_DLLEXPORT
812long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
813
826JAS_ATTRIBUTE_PURE JAS_DLLEXPORT
827long jas_stream_tell(jas_stream_t *stream);
828
841JAS_DLLEXPORT int jas_stream_rewind(jas_stream_t *stream);
842
843/******************************************************************************\
844* Macros/functions for flushing.
845\******************************************************************************/
846
857JAS_DLLEXPORT int jas_stream_flush(jas_stream_t *stream);
858
859/******************************************************************************\
860* Miscellaneous macros/functions.
861\******************************************************************************/
862
883JAS_DLLEXPORT int jas_stream_copy(jas_stream_t *destination, jas_stream_t *source, int count);
884
907JAS_DLLEXPORT
908int jas_stream_display(jas_stream_t *stream, FILE *fp, int count);
909
932JAS_DLLEXPORT int jas_stream_gobble(jas_stream_t *stream, int count);
933
958JAS_DLLEXPORT int jas_stream_pad(jas_stream_t *stream, int count, int value);
959
977JAS_ATTRIBUTE_PURE JAS_DLLEXPORT
978long jas_stream_length(jas_stream_t *stream);
979
980/******************************************************************************\
981* Internal functions.
982\******************************************************************************/
983
984/* The following functions are for internal use only! If you call them
985directly, you will die a horrible, miserable, and painful death! */
986
987/* These prototypes need to be here for the sake of the stream_getc and
988stream_putc macros. */
989/* Library users must not invoke these functions directly. */
990JAS_DLLEXPORT int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
991JAS_DLLEXPORT int jas_stream_flushbuf(jas_stream_t *stream, int c);
992JAS_DLLEXPORT int jas_stream_getc_func(jas_stream_t *stream);
993JAS_DLLEXPORT int jas_stream_putc_func(jas_stream_t *stream, int c);
994
995/* Read a character from a stream. */
996static inline int jas_stream_getc2(jas_stream_t *stream)
997{
998 if (--stream->cnt_ < 0)
999 return jas_stream_fillbuf(stream, 1);
1000
1001 ++stream->rwcnt_;
1002 return (int)(*stream->ptr_++);
1003}
1004
1005static inline int jas_stream_getc_macro(jas_stream_t *stream)
1006{
1007 if (stream->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | JAS_STREAM_RWLIMIT))
1008 return EOF;
1009
1010 if (stream->rwlimit_ >= 0 && stream->rwcnt_ >= stream->rwlimit_) {
1011 stream->flags_ |= JAS_STREAM_RWLIMIT;
1012 return EOF;
1013 }
1014
1015 return jas_stream_getc2(stream);
1016}
1017
1018/* Write a character to a stream. */
1019static inline int jas_stream_putc2(jas_stream_t *stream, jas_uchar c)
1020{
1021 stream->bufmode_ |= JAS_STREAM_WRBUF;
1022
1023 if (--stream->cnt_ < 0)
1024 return jas_stream_flushbuf(stream, c);
1025 else {
1026 ++stream->rwcnt_;
1027 return (int)(*stream->ptr_++ = c);
1028 }
1029}
1030
1031static inline int jas_stream_putc_macro(jas_stream_t *stream, jas_uchar c)
1032{
1033 if (stream->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | JAS_STREAM_RWLIMIT))
1034 return EOF;
1035
1036 if (stream->rwlimit_ >= 0 && stream->rwcnt_ >= stream->rwlimit_) {
1037 stream->flags_ |= JAS_STREAM_RWLIMIT;
1038 return EOF;
1039 }
1040
1041 return jas_stream_putc2(stream, c);
1042}
1043
1048#ifdef __cplusplus
1049}
1050#endif
1051
1052#endif
JAS_DLLEXPORT int jas_stream_flush(jas_stream_t *stream)
Flush any pending output to a stream.
Definition jas_stream.c:1001
JAS_DLLEXPORT int jas_stream_pad(jas_stream_t *stream, int count, int value)
Write a fill character multiple times to a stream.
Definition jas_stream.c:850
JAS_DLLEXPORT unsigned jas_stream_peek(jas_stream_t *stream, void *buffer, size_t count)
Attempt to retrieve one or more pending characters of input from a stream into a buffer without actua...
Definition jas_stream.c:720
JAS_DLLEXPORT long jas_stream_seek(jas_stream_t *stream, long offset, int origin)
Set the current position within the stream.
Definition jas_stream.c:891
JAS_ATTRIBUTE_PURE JAS_DLLEXPORT long jas_stream_tell(jas_stream_t *stream)
Get the current position within the stream.
Definition jas_stream.c:926
JAS_ATTRIBUTE_PURE JAS_DLLEXPORT int jas_stream_isseekable(jas_stream_t *stream)
Determine if stream supports seeking.
Definition jas_stream.c:871
JAS_DLLEXPORT jas_stream_t * jas_stream_memopen(char *buffer, int buffer_size)
Open a memory buffer as a stream.
Definition jas_stream.c:276
JAS_DLLEXPORT int jas_stream_printf(jas_stream_t *stream, const char *format,...)
Write formatted output to a stream.
Definition jas_stream.c:781
JAS_DLLEXPORT int jas_stream_ungetc(jas_stream_t *stream, int c)
Put a character back on a stream.
Definition jas_stream.c:652
JAS_DLLEXPORT int jas_stream_puts(jas_stream_t *stream, const char *s)
Write a string to a stream.
Definition jas_stream.c:794
JAS_DLLEXPORT jas_stream_t * jas_stream_fdopen(int fd, const char *mode)
Open a file descriptor as a stream.
Definition jas_stream.c:540
JAS_DLLEXPORT jas_stream_t * jas_stream_tmpfile(void)
Open a temporary file as a stream.
Definition jas_stream.c:490
JAS_DLLEXPORT unsigned jas_stream_write(jas_stream_t *stream, const void *buffer, unsigned count)
Write characters from a buffer to a stream.
Definition jas_stream.c:735
JAS_ATTRIBUTE_PURE JAS_DLLEXPORT long jas_stream_length(jas_stream_t *stream)
Get the size of the file associated with the specified stream.
Definition jas_stream.c:1225
JAS_DLLEXPORT char * jas_stream_gets(jas_stream_t *stream, char *buffer, int buffer_size)
Read a line of input from a stream.
Definition jas_stream.c:807
JAS_DLLEXPORT jas_stream_t * jas_stream_memopen2(char *buffer, size_t buffer_size)
Definition jas_stream.c:189
JAS_DLLEXPORT long jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit)
Set the read/write limit for a stream.
Definition jas_stream.c:1164
JAS_DLLEXPORT jas_stream_t * jas_stream_freopen(const char *path, const char *mode, FILE *fp)
Open a stdio (i.e., C standard library) stream as a stream.
Definition jas_stream.c:368
JAS_DLLEXPORT jas_stream_t * jas_stream_fopen(const char *filename, const char *mode)
Open a file as a stream.
Definition jas_stream.c:301
JAS_DLLEXPORT int jas_stream_gobble(jas_stream_t *stream, int count)
Consume (i.e., discard) characters from stream.
Definition jas_stream.c:831
JAS_DLLEXPORT int jas_stream_close(jas_stream_t *stream)
Close a stream.
Definition jas_stream.c:610
JAS_DLLEXPORT unsigned jas_stream_read(jas_stream_t *stream, void *buffer, unsigned count)
Read characters from a stream into a buffer.
Definition jas_stream.c:670
JAS_DLLEXPORT long jas_stream_setrwcount(jas_stream_t *stream, long rw_count)
Set the read/write count for a stream.
Definition jas_stream.c:1154
JAS_DLLEXPORT int jas_stream_rewind(jas_stream_t *stream)
Seek to the beginning of a stream.
Definition jas_stream.c:885
JAS_DLLEXPORT int jas_stream_display(jas_stream_t *stream, FILE *fp, int count)
Print a hex dump of data read from a stream.
Definition jas_stream.c:1174
JAS_DLLEXPORT int jas_stream_copy(jas_stream_t *destination, jas_stream_t *source, int count)
Copy data from one stream to another.
Definition jas_stream.c:1129
Primitive Types.