My Project: jas_stream.h Source File

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