JasPer 4.0.0 |

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#include <limits.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
95/******************************************************************************\
96* Constants.
97\******************************************************************************/
98
99/* On most UNIX systems, we probably need to define O_BINARY ourselves. */
100#ifndef O_BINARY
101#define O_BINARY 0
102#endif
103
104#ifdef PATH_MAX
105#define JAS_PATH_MAX PATH_MAX
106#else
107#define JAS_PATH_MAX 4096
108#endif
109
110/*
111 * Stream open flags.
112 */
113
114/* The stream was opened for reading. */
115#define JAS_STREAM_READ 0x0001
116/* The stream was opened for writing. */
117#define JAS_STREAM_WRITE 0x0002
118/* The stream was opened for appending. */
119#define JAS_STREAM_APPEND 0x0004
120/* The stream was opened in binary mode. */
121#define JAS_STREAM_BINARY 0x0008
122/* The stream should be created/truncated. */
123#define JAS_STREAM_CREATE 0x0010
124
125/*
126 * Stream buffering flags.
127 */
128
129/* The stream is unbuffered. */
130#define JAS_STREAM_UNBUF 0x0000
131/* The stream is line buffered. */
132#define JAS_STREAM_LINEBUF 0x0001
133/* The stream is fully buffered. */
134#define JAS_STREAM_FULLBUF 0x0002
135/* The buffering mode mask. */
136#define JAS_STREAM_BUFMODEMASK 0x000f
137
138/* The memory associated with the buffer needs to be deallocated when the
139 stream is destroyed. */
140#define JAS_STREAM_FREEBUF 0x0008
141/* The buffer is currently being used for reading. */
142#define JAS_STREAM_RDBUF 0x0010
143/* The buffer is currently being used for writing. */
144#define JAS_STREAM_WRBUF 0x0020
145
146/*
147 * Stream error flags.
148 */
149
150/* The end-of-file has been encountered (on reading). */
151#define JAS_STREAM_EOF 0x0001
152/* An I/O error has been encountered on the stream. */
153#define JAS_STREAM_ERR 0x0002
154/* The read/write limit has been exceeded. */
155#define JAS_STREAM_RWLIMIT 0x0004
156/* The error mask. */
157#define JAS_STREAM_ERRMASK \
158 (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
159
160/*
161 * Other miscellaneous constants.
162 */
163
164/* The default buffer size (for fully-buffered operation). */
165#define JAS_STREAM_BUFSIZE 8192
166/* The default permission mask for file creation. */
167#define JAS_STREAM_PERMS 0666
168
169/* The maximum number of characters that can always be put back on a stream. */
170#define JAS_STREAM_MAXPUTBACK 16
171
172/******************************************************************************\
173* Types.
174\******************************************************************************/
175
176/*
177 * Generic file object.
178 */
179
180 typedef void jas_stream_obj_t;
181
182/*
183 * Generic file object operations.
184 */
185
186 typedef struct {
187
188 /* Read characters from a file object. */
189 ssize_t (*read_)(jas_stream_obj_t *obj, char *buf, size_t cnt);
190
191 /* Write characters to a file object. */
192 ssize_t (*write_)(jas_stream_obj_t *obj, const char *buf, size_t cnt);
193
194 /* Set the position for a file object. */
195 long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
196
197 /* Close a file object. */
198 int (*close_)(jas_stream_obj_t *obj);
199
200} jas_stream_ops_t;
201
213 typedef struct {
214
215 /* The mode in which the stream was opened. */
216 int openmode_;
217
218 /* The buffering mode. */
219 int bufmode_;
220
221 /* The stream status. */
222 int flags_;
223
224 /* The start of the buffer area to use for reading/writing. */
225 jas_uchar *bufbase_;
226
227 /* The start of the buffer area excluding the extra initial space for
228 character putback. */
229 jas_uchar *bufstart_;
230
231 /* The buffer size. */
232 int bufsize_;
233
234 /* The current position in the buffer. */
235 jas_uchar *ptr_;
236
237 /* The number of characters that must be read/written before
238 the buffer needs to be filled/flushed. */
239 int cnt_;
240
241 /* A trivial buffer to be used for unbuffered operation. */
242 jas_uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
243
244 /* The operations for the underlying stream file object. */
245 const jas_stream_ops_t *ops_;
246
247 /* The underlying stream file object. */
248 jas_stream_obj_t *obj_;
249
250 /* The number of characters read/written. */
251 long rwcnt_;
252
253 /* The maximum number of characters that may be read/written. */
254 long rwlimit_;
255
256} jas_stream_t;
257
258/*
259 * Regular file object.
260 */
261
262/*
263 * File descriptor file object.
264 */
265 typedef struct {
266 int fd;
267 int flags;
268 char pathname[JAS_PATH_MAX + 1];
269} jas_stream_fileobj_t;
270
271/* Delete underlying file object upon stream close. */
272#define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01
273/* Do not close underlying file object upon stream close. */
274#define JAS_STREAM_FILEOBJ_NOCLOSE 0x02
275
276/*
277 * Memory file object.
278 */
279
280 typedef struct {
281
282 /* The data associated with this file. */
283 jas_uchar *buf_;
284
285 /* The allocated size of the buffer for holding file data. */
286 size_t bufsize_;
287
288 /* The length of the file. */
289 size_t len_;
290
291 /* The seek position. */
292 size_t pos_;
293
294 /* Is the buffer growable? */
295 int growable_;
296
297 /* Was the buffer allocated internally? */
298 int myalloc_;
299
300} jas_stream_memobj_t;
301
302/******************************************************************************\
303* Macros/functions for opening and closing streams.
304\******************************************************************************/
305
320JAS_EXPORT
321 jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
322
347JAS_EXPORT
348 jas_stream_t *jas_stream_memopen(char *buffer, size_t buffer_size);
349
358JAS_DEPRECATED
359JAS_EXPORT
360 jas_stream_t *jas_stream_memopen2(char *buffer, size_t buffer_size);
361
376JAS_EXPORT
377 jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
378
401JAS_EXPORT
402 jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
403
418JAS_EXPORT
419 jas_stream_t *jas_stream_tmpfile(void);
420
439JAS_EXPORT
440 int jas_stream_close(jas_stream_t *stream);
441
442/******************************************************************************\
443* Macros/functions for getting/setting the stream state.
444\******************************************************************************/
445
456#define jas_stream_eof(stream) \
457 (((stream)->flags_ & JAS_STREAM_EOF) != 0)
458
470#define jas_stream_error(stream) \
471 (((stream)->flags_ & JAS_STREAM_ERR) != 0)
472
482#define jas_stream_clearerr(stream) \
483 ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
484
495#define jas_stream_getrwlimit(stream) \
496 (((const jas_stream_t *)(stream))->rwlimit_)
497
513JAS_EXPORT long jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
514
525#define jas_stream_getrwcount(stream) \
526 (((const jas_stream_t *)(stream))->rwcnt_)
527
541JAS_EXPORT
542 long jas_stream_setrwcount(jas_stream_t *stream, long rw_count);
543
544/******************************************************************************\
545* Macros/functions for I/O.
546\******************************************************************************/
547
548/* Read a character from a stream. */
549#ifndef NDEBUG
561#define jas_stream_getc(stream) jas_stream_getc_func(stream)
562#else
563#define jas_stream_getc(stream) jas_stream_getc_macro(stream)
564#endif
565
566/* Write a character to a stream. */
567#ifndef NDEBUG
582#define jas_stream_putc(stream, c) jas_stream_putc_func(stream, c)
583#else
584#define jas_stream_putc(stream, c) jas_stream_putc_macro(stream, c)
585#endif
586
625JAS_EXPORT
626 size_t jas_stream_read(jas_stream_t *stream, void *buffer, size_t count);
627
653JAS_EXPORT
654 unsigned jas_stream_peek(jas_stream_t *stream, void *buffer, size_t count);
655
685JAS_EXPORT
686 size_t jas_stream_write(jas_stream_t *stream, const void *buffer,
687 size_t count);
688
710JAS_EXPORT
711 int jas_stream_printf(jas_stream_t *stream, const char *format, ...);
712
730JAS_EXPORT
731 int jas_stream_puts(jas_stream_t *stream, const char *s);
732
756JAS_EXPORT
757 char *jas_stream_gets(jas_stream_t *stream, char *buffer, int buffer_size);
758
777#define jas_stream_peekc(stream) \
778 (((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
779 ((int)(*(stream)->ptr_)))
780
804JAS_EXPORT
805 int jas_stream_ungetc(jas_stream_t *stream, int c);
806
807/******************************************************************************\
808* Macros/functions for getting/setting the stream position.
809\******************************************************************************/
810
826JAS_EXPORT
827JAS_ATTRIBUTE_PURE
828 int jas_stream_isseekable(jas_stream_t *stream);
829
849JAS_EXPORT
850 long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
851
867JAS_EXPORT
868 long jas_stream_tell(jas_stream_t *stream);
869
887JAS_EXPORT
888 int jas_stream_rewind(jas_stream_t *stream);
889
890/******************************************************************************\
891* Macros/functions for flushing.
892\******************************************************************************/
893
909JAS_EXPORT
910 int jas_stream_flush(jas_stream_t *stream);
911
912/******************************************************************************\
913* Miscellaneous macros/functions.
914\******************************************************************************/
915
945JAS_EXPORT
946 int jas_stream_copy(jas_stream_t *destination, jas_stream_t *source,
947 ssize_t count);
948
971JAS_EXPORT
972 int jas_stream_display(jas_stream_t *stream, FILE *fp, int count);
973
993JAS_EXPORT
994ssize_t jas_stream_gobble(jas_stream_t *stream, size_t count);
995
1017JAS_EXPORT
1018ssize_t jas_stream_pad(jas_stream_t *stream, size_t count, int value);
1019
1039JAS_EXPORT
1040 long jas_stream_length(jas_stream_t *stream);
1041
1042/******************************************************************************\
1043* Internal functions.
1044\******************************************************************************/
1045
1046/* The following functions are for internal use only! If you call them
1047 directly, you will die a horrible, miserable, and painful death! */
1048
1049/* These prototypes need to be here for the sake of the stream_getc and
1050 stream_putc macros. */
1051/* Library users must not invoke these functions directly. */
1052JAS_EXPORT int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
1053JAS_EXPORT int jas_stream_flushbuf(jas_stream_t *stream, int c);
1054JAS_EXPORT int jas_stream_getc_func(jas_stream_t *stream);
1055JAS_EXPORT int jas_stream_putc_func(jas_stream_t *stream, int c);
1056
1057/* Read a character from a stream. */
1058 static inline int jas_stream_getc2(jas_stream_t *stream)
1059{
1060 if (--stream->cnt_ < 0)
1061 return jas_stream_fillbuf(stream, 1);
1062
1063 ++stream->rwcnt_;
1064 return (int)(*stream->ptr_++);
1065}
1066
1067 static inline int jas_stream_getc_macro(jas_stream_t *stream)
1068{
1069 if (stream->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | JAS_STREAM_RWLIMIT))
1070 return EOF;
1071
1072 if (stream->rwlimit_ >= 0 && stream->rwcnt_ >= stream->rwlimit_) {
1073 stream->flags_ |= JAS_STREAM_RWLIMIT;
1074 return EOF;
1075 }
1076
1077 return jas_stream_getc2(stream);
1078}
1079
1080/* Write a character to a stream. */
1081 static inline int jas_stream_putc2(jas_stream_t *stream, jas_uchar c)
1082{
1083 stream->bufmode_ |= JAS_STREAM_WRBUF;
1084
1085 if (--stream->cnt_ < 0)
1086 return jas_stream_flushbuf(stream, c);
1087 else {
1088 ++stream->rwcnt_;
1089 return (int)(*stream->ptr_++ = c);
1090 }
1091}
1092
1093 static inline int jas_stream_putc_macro(jas_stream_t *stream, jas_uchar c)
1094{
1095 if (stream->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | JAS_STREAM_RWLIMIT))
1096 return EOF;
1097
1098 if (stream->rwlimit_ >= 0 && stream->rwcnt_ >= stream->rwlimit_) {
1099 stream->flags_ |= JAS_STREAM_RWLIMIT;
1100 return EOF;
1101 }
1102
1103 return jas_stream_putc2(stream, c);
1104}
1105
1110#ifdef __cplusplus
1111}
1112#endif
1113
1114#endif
jas_stream_tmpfile
JAS_EXPORT jas_stream_t * jas_stream_tmpfile(void)
Open a temporary file as a stream.
Definition: jas_stream.c:471
jas_stream_display
JAS_EXPORT 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:1146
jas_stream_setrwlimit
JAS_EXPORT long jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit)
Set the read/write limit for a stream.
Definition: jas_stream.c:1136
jas_stream_memopen2
JAS_DEPRECATED JAS_EXPORT jas_stream_t * jas_stream_memopen2(char *buffer, size_t buffer_size)
Do not use.
Definition: jas_stream.c:277
jas_stream_peek
JAS_EXPORT 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:704
jas_stream_fopen
JAS_EXPORT jas_stream_t * jas_stream_fopen(const char *filename, const char *mode)
Open a file as a stream.
Definition: jas_stream.c:283
jas_stream_tell
JAS_EXPORT long jas_stream_tell(jas_stream_t *stream)
Get the current position within the stream.
Definition: jas_stream.c:899
jas_stream_gobble
JAS_EXPORT ssize_t jas_stream_gobble(jas_stream_t *stream, size_t count)
Consume (i.e., discard) characters from stream.
Definition: jas_stream.c:817
jas_stream_pad
JAS_EXPORT ssize_t jas_stream_pad(jas_stream_t *stream, size_t count, int value)
Write a fill character multiple times to a stream.
Definition: jas_stream.c:829
jas_stream_printf
JAS_EXPORT int jas_stream_printf(jas_stream_t *stream, const char *format,...)
Write formatted output to a stream.
Definition: jas_stream.c:768
jas_stream_puts
JAS_EXPORT int jas_stream_puts(jas_stream_t *stream, const char *s)
Write a string to a stream.
Definition: jas_stream.c:781
jas_stream_gets
JAS_EXPORT 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:794
jas_stream_ungetc
JAS_EXPORT int jas_stream_ungetc(jas_stream_t *stream, int c)
Put a character back on a stream.
Definition: jas_stream.c:633
jas_stream_flush
JAS_EXPORT int jas_stream_flush(jas_stream_t *stream)
Flush any pending output to a stream.
Definition: jas_stream.c:974
jas_stream_seek
JAS_EXPORT long jas_stream_seek(jas_stream_t *stream, long offset, int origin)
Set the current position within the stream.
Definition: jas_stream.c:865
jas_stream_memopen
JAS_EXPORT jas_stream_t * jas_stream_memopen(char *buffer, size_t buffer_size)
Open a memory buffer as a stream.
Definition: jas_stream.c:203
jas_stream_close
JAS_EXPORT int jas_stream_close(jas_stream_t *stream)
Close a stream.
Definition: jas_stream.c:591
jas_stream_write
JAS_EXPORT size_t jas_stream_write(jas_stream_t *stream, const void *buffer, size_t count)
Write characters from a buffer to a stream.
Definition: jas_stream.c:720
jas_stream_isseekable
JAS_EXPORT JAS_ATTRIBUTE_PURE int jas_stream_isseekable(jas_stream_t *stream)
Determine if stream supports seeking.
Definition: jas_stream.c:845
jas_stream_fdopen
JAS_EXPORT jas_stream_t * jas_stream_fdopen(int fd, const char *mode)
Open a file descriptor as a stream.
Definition: jas_stream.c:521
jas_stream_copy
JAS_EXPORT int jas_stream_copy(jas_stream_t *destination, jas_stream_t *source, ssize_t count)
Copy data from one stream to another.
Definition: jas_stream.c:1101
jas_stream_length
JAS_EXPORT long jas_stream_length(jas_stream_t *stream)
Get the size of the file associated with the specified stream.
Definition: jas_stream.c:1196
jas_stream_rewind
JAS_EXPORT int jas_stream_rewind(jas_stream_t *stream)
Seek to the beginning of a stream.
Definition: jas_stream.c:859
jas_stream_read
JAS_EXPORT size_t jas_stream_read(jas_stream_t *stream, void *buffer, size_t count)
Read characters from a stream into a buffer.
Definition: jas_stream.c:650
jas_stream_freopen
JAS_EXPORT 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:350
jas_stream_setrwcount
JAS_EXPORT long jas_stream_setrwcount(jas_stream_t *stream, long rw_count)
Set the read/write count for a stream.
Definition: jas_stream.c:1126
jas_types.h
Primitive Types.
jas_stream_t
I/O stream object.
Definition: jas_stream.h:213