Debian Codegraph @

converter/other/jpeg2000/libjasper/base/jas_stream.c

raw
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 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
114 #define _SVID_SOURCE
115 /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
116 does it in other libc's). pm_config.h defines TMPDIR as P_tmpdir
117 in some environments.
118 */
119 #define _XOPEN_SOURCE 500 /* Make sure P_tmpdir is defined */
120
121 #include "pm_config.h"
122 #include <assert.h>
123 #include <fcntl.h>
124 #include <stdlib.h>
125 #include <stdarg.h>
126 #include <stdio.h>
127 #include <ctype.h>
128 #if defined(HAVE_UNISTD_H)
129 #include <unistd.h>
130 #endif
131 #if HAVE_IO_H
132 #include <io.h>
133 #endif
134
135 #include "netpbm/pm.h"
136
137 #include "jasper/jas_types.h"
138 #include "jasper/jas_stream.h"
139 #include "jasper/jas_malloc.h"
140 #include "jasper/jas_math.h"
141
142 /****************************************************************************\
143 * Local function prototypes.
144 \****************************************************************************/
145
146 static int jas_strtoopenmode(const char *s);
147 static void jas_stream_destroy(jas_stream_t *stream);
148 static jas_stream_t *jas_stream_create(void);
149 static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf,
150 int bufsize);
151
152 static int mem_read(jas_stream_obj_t *obj, char *buf, int cnt);
153 static int mem_write(jas_stream_obj_t *obj, char *buf, int cnt);
154 static long mem_seek(jas_stream_obj_t *obj, long offset, int origin);
155 static int mem_close(jas_stream_obj_t *obj);
156
157 static int sfile_read(jas_stream_obj_t *obj, char *buf, int cnt);
158 static int sfile_write(jas_stream_obj_t *obj, char *buf, int cnt);
159 static long sfile_seek(jas_stream_obj_t *obj, long offset, int origin);
160 static int sfile_close(jas_stream_obj_t *obj);
161
162 static int file_read(jas_stream_obj_t *obj, char *buf, int cnt);
163 static int file_write(jas_stream_obj_t *obj, char *buf, int cnt);
164 static long file_seek(jas_stream_obj_t *obj, long offset, int origin);
165 static int file_close(jas_stream_obj_t *obj);
166
167 /******************************************************************************\
168 * Local data.
169 \******************************************************************************/
170
171 static jas_stream_ops_t jas_stream_fileops = {
172 file_read,
173 file_write,
174 file_seek,
175 file_close
176 };
177
178 static jas_stream_ops_t jas_stream_sfileops = {
179 sfile_read,
180 sfile_write,
181 sfile_seek,
182 sfile_close
183 };
184
185 static jas_stream_ops_t jas_stream_memops = {
186 mem_read,
187 mem_write,
188 mem_seek,
189 mem_close
190 };
191
192 /******************************************************************************\
193 * Code for opening and closing streams.
194 \******************************************************************************/
195
196 static jas_stream_t *jas_stream_create()
197 {
198 jas_stream_t *stream;
199
200 if (!(stream = jas_malloc(sizeof(jas_stream_t)))) {
201 return 0;
202 }
203 stream->openmode_ = 0;
204 stream->bufmode_ = 0;
205 stream->flags_ = 0;
206 stream->bufbase_ = 0;
207 stream->bufstart_ = 0;
208 stream->bufsize_ = 0;
209 stream->ptr_ = 0;
210 stream->cnt_ = 0;
211 stream->ops_ = 0;
212 stream->obj_ = 0;
213 stream->rwcnt_ = 0;
214 stream->rwlimit_ = -1;
215
216 return stream;
217 }
218
219 jas_stream_t *jas_stream_memopen(char *buf, int bufsize)
220 {
221 jas_stream_t *stream;
222 jas_stream_memobj_t *obj;
223
224 if (!(stream = jas_stream_create())) {
225 return 0;
226 }
227
228 /* A stream associated with a memory buffer is always opened
229 for both reading and writing in binary mode. */
230 stream->openmode_ = JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
231
232 /* Since the stream data is already resident in memory, buffering
233 is not necessary. */
234 /* But... It still may be faster to use buffering anyways. */
235 jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
236
237 /* Select the operations for a memory stream. */
238 stream->ops_ = &jas_stream_memops;
239
240 /* Allocate memory for the underlying memory stream object. */
241 if (!(obj = jas_malloc(sizeof(jas_stream_memobj_t)))) {
242 jas_stream_destroy(stream);
243 return 0;
244 }
245 stream->obj_ = (void *) obj;
246
247 /* Initialize a few important members of the memory stream object. */
248 obj->myalloc_ = 0;
249 obj->buf_ = 0;
250
251 /* If the buffer size specified is nonpositive, then the buffer
252 is allocated internally and automatically grown as needed. */
253 if (bufsize <= 0) {
254 obj->bufsize_ = 1024;
255 obj->growable_ = 1;
256 } else {
257 obj->bufsize_ = bufsize;
258 obj->growable_ = 0;
259 }
260 if (buf) {
261 obj->buf_ = (unsigned char *) buf;
262 } else {
263 obj->buf_ = jas_malloc(obj->bufsize_ * sizeof(char));
264 obj->myalloc_ = 1;
265 }
266 if (!obj->buf_) {
267 jas_stream_close(stream);
268 return 0;
269 }
270
271 if (bufsize > 0 && buf) {
272 /* If a buffer was supplied by the caller and its length is positive,
273 make the associated buffer data appear in the stream initially. */
274 obj->len_ = bufsize;
275 } else {
276 /* The stream is initially empty. */
277 obj->len_ = 0;
278 }
279 obj->pos_ = 0;
280
281 return stream;
282 }
283
284 jas_stream_t *jas_stream_fopen(const char *filename, const char *mode)
285 {
286 jas_stream_t *stream;
287 int *obj;
288 int openflags;
289
290 /* Allocate a stream object. */
291 if (!(stream = jas_stream_create())) {
292 return 0;
293 }
294
295 /* Parse the mode string. */
296 stream->openmode_ = jas_strtoopenmode(mode);
297
298 /* Determine the correct flags to use for opening the file. */
299 if ((stream->openmode_ & JAS_STREAM_READ) &&
300 (stream->openmode_ & JAS_STREAM_WRITE)) {
301 openflags = O_RDWR;
302 } else if (stream->openmode_ & JAS_STREAM_READ) {
303 openflags = O_RDONLY;
304 } else if (stream->openmode_ & JAS_STREAM_WRITE) {
305 openflags = O_WRONLY;
306 } else {
307 openflags = 0;
308 }
309 if (stream->openmode_ & JAS_STREAM_APPEND) {
310 openflags |= O_APPEND;
311 }
312 if (stream->openmode_ & JAS_STREAM_BINARY) {
313 openflags |= O_BINARY;
314 }
315 if (stream->openmode_ & JAS_STREAM_CREATE) {
316 openflags |= O_CREAT | O_TRUNC;
317 }
318
319 /* Allocate space for the underlying file stream object. */
320 if (!(obj = jas_malloc(sizeof(int)))) {
321 jas_stream_destroy(stream);
322 return 0;
323 }
324 stream->obj_ = (void *) obj;
325
326 /* Select the operations for a file stream object. */
327 stream->ops_ = &jas_stream_fileops;
328
329 /* Open the underlying file. */
330 if ((*obj = open(filename, openflags, JAS_STREAM_PERMS)) < 0) {
331 jas_stream_destroy(stream);
332 return 0;
333 }
334
335 /* By default, use full buffering for this type of stream. */
336 jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
337
338 return stream;
339 }
340
341 jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp)
342 {
343 jas_stream_t *stream;
344 int openflags;
345
346 /* Allocate a stream object. */
347 if (!(stream = jas_stream_create())) {
348 return 0;
349 }
350
351 /* Parse the mode string. */
352 stream->openmode_ = jas_strtoopenmode(mode);
353
354 /* Determine the correct flags to use for opening the file. */
355 if ((stream->openmode_ & JAS_STREAM_READ) &&
356 (stream->openmode_ & JAS_STREAM_WRITE)) {
357 openflags = O_RDWR;
358 } else if (stream->openmode_ & JAS_STREAM_READ) {
359 openflags = O_RDONLY;
360 } else if (stream->openmode_ & JAS_STREAM_WRITE) {
361 openflags = O_WRONLY;
362 } else {
363 openflags = 0;
364 }
365 if (stream->openmode_ & JAS_STREAM_APPEND) {
366 openflags |= O_APPEND;
367 }
368 if (stream->openmode_ & JAS_STREAM_BINARY) {
369 openflags |= O_BINARY;
370 }
371 if (stream->openmode_ & JAS_STREAM_CREATE) {
372 openflags |= O_CREAT | O_TRUNC;
373 }
374
375 stream->obj_ = JAS_CAST(void *, fp);
376
377 /* Select the operations for a file stream object. */
378 stream->ops_ = &jas_stream_sfileops;
379
380 /* By default, use full buffering for this type of stream. */
381 jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
382
383 return stream;
384 }
385
386
387 jas_stream_t *jas_stream_tmpfile()
388 {
389 jas_stream_t *stream;
390 int *obj;
391
392 if (!(stream = jas_stream_create())) {
393 return 0;
394 }
395
396 /* A temporary file stream is always opened for both reading and
397 writing in binary mode. */
398 stream->openmode_ = JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
399
400 /* Allocate memory for the underlying temporary file object. */
401 if (!(obj = jas_malloc(sizeof(int)))) {
402 jas_stream_destroy(stream);
403 return 0;
404 }
405 stream->obj_ = obj;
406
407 {
408 /* This is a Netpbm enhancement. Original Jasper library uses
409 tmpnam(), which is unsafe.
410 */
411 jmp_buf jmpbuf;
412 int rc;
413
414 rc = setjmp(jmpbuf);
415 if (rc == 0) {
416 pm_setjmpbuf(&jmpbuf);
417 *obj = pm_tmpfile_fd();
418 pm_setjmpbuf(NULL);
419 } else {
420 /* pm_tmpfile_fd() threw an error */
421 jas_stream_destroy(stream);
422 pm_setjmpbuf(NULL);
423 return 0;
424 }
425 }
426 /* Use full buffering. */
427 jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
428
429 stream->ops_ = &jas_stream_fileops;
430
431 return stream;
432 }
433
434 jas_stream_t *jas_stream_fdopen(int fd, const char *mode)
435 {
436 jas_stream_t *stream;
437 int *obj;
438
439 /* Allocate a stream object. */
440 if (!(stream = jas_stream_create())) {
441 return 0;
442 }
443
444 /* Parse the mode string. */
445 stream->openmode_ = jas_strtoopenmode(mode);
446
447 #if defined(HAVE_SETMODE) && defined(O_BINARY)
448 /* Argh!!! Someone ought to banish text mode (i.e., O_TEXT) to the
449 greatest depths of purgatory! */
450 /* Ensure that the file descriptor is in binary mode, if the caller
451 has specified the binary mode flag. Arguably, the caller ought to
452 take care of this, but text mode is a ugly wart anyways, so we save
453 the caller some grief by handling this within the stream library. */
454 /* This ugliness is mainly for the benefit of those who run the
455 JasPer software under Windows from shells that insist on opening
456 files in text mode. For example, in the Cygwin environment,
457 shells often open files in text mode when I/O redirection is
458 used. Grr... */
459 if (stream->openmode_ & JAS_STREAM_BINARY) {
460 setmode(fd, O_BINARY);
461 }
462 #endif
463
464 /* Allocate space for the underlying file stream object. */
465 if (!(obj = jas_malloc(sizeof(int)))) {
466 jas_stream_destroy(stream);
467 return 0;
468 }
469 stream->obj_ = (void *) obj;
470 *obj = fd;
471
472 /* By default, use full buffering for this type of stream. */
473 jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
474
475 /* Select the operations for a file stream object. */
476 stream->ops_ = &jas_stream_fileops;
477
478 /* Do not close the underlying file descriptor when the stream is closed. */
479 stream->openmode_ |= JAS_STREAM_NOCLOSE;
480
481 return stream;
482 }
483
484 static void jas_stream_destroy(jas_stream_t *stream)
485 {
486 /* If the memory for the buffer was allocated with malloc, free
487 this memory. */
488 if ((stream->bufmode_ & JAS_STREAM_FREEBUF) && stream->bufbase_) {
489 jas_free(stream->bufbase_);
490 stream->bufbase_ = 0;
491 }
492 jas_free(stream);
493 }
494
495 int jas_stream_close(jas_stream_t *stream)
496 {
497 /* Flush buffer if necessary. */
498 jas_stream_flush(stream);
499
500 /* Close the underlying stream object. */
501 if (!(stream->openmode_ & JAS_STREAM_NOCLOSE)) {
502 (*stream->ops_->close_)(stream->obj_);
503 }
504
505 jas_stream_destroy(stream);
506
507 return 0;
508 }
509
510 /******************************************************************************\
511 * Code for reading and writing streams.
512 \******************************************************************************/
513
514 int jas_stream_getc_func(jas_stream_t *stream)
515 {
516 assert(stream->ptr_ - stream->bufbase_ <= stream->bufsize_ +
517 JAS_STREAM_MAXPUTBACK);
518 return jas_stream_getc_macro(stream);
519 }
520
521 int jas_stream_putc_func(jas_stream_t *stream, int c)
522 {
523 assert(stream->ptr_ - stream->bufstart_ <= stream->bufsize_);
524 return jas_stream_putc_macro(stream, c);
525 }
526
527 int jas_stream_ungetc(jas_stream_t *stream, int c)
528 {
529 if (!stream->ptr_ || stream->ptr_ == stream->bufbase_) {
530 return -1;
531 }
532
533 /* Reset the EOF indicator (since we now have at least one character
534 to read). */
535 stream->flags_ &= ~JAS_STREAM_EOF;
536
537 --stream->rwcnt_;
538 --stream->ptr_;
539 ++stream->cnt_;
540 *stream->ptr_ = c;
541 return 0;
542 }
543
544 int jas_stream_read(jas_stream_t *stream, void *buf, int cnt)
545 {
546 int n;
547 int c;
548 char *bufptr;
549
550 bufptr = buf;
551
552 n = 0;
553 while (n < cnt) {
554 if ((c = jas_stream_getc(stream)) == EOF) {
555 return n;
556 }
557 *bufptr++ = c;
558 ++n;
559 }
560
561 return n;
562 }
563
564 int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt)
565 {
566 int n;
567 const char *bufptr;
568
569 bufptr = buf;
570
571 n = 0;
572 while (n < cnt) {
573 if (jas_stream_putc(stream, *bufptr) == EOF) {
574 return n;
575 }
576 ++bufptr;
577 ++n;
578 }
579
580 return n;
581 }
582
583 /* Note: This function uses a fixed size buffer. Therefore, it cannot
584 handle invocations that will produce more output than can be held
585 by the buffer. */
586 int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...)
587 {
588 va_list ap;
589 char buf[4096];
590 int ret;
591
592 va_start(ap, fmt);
593 ret = vsprintf(buf, fmt, ap);
594 jas_stream_puts(stream, buf);
595 va_end(ap);
596 return ret;
597 }
598
599 int jas_stream_puts(jas_stream_t *stream, const char *s)
600 {
601 while (*s != '\0') {
602 if (jas_stream_putc_macro(stream, *s) == EOF) {
603 return -1;
604 }
605 ++s;
606 }
607 return 0;
608 }
609
610 char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize)
611 {
612 int c;
613 char *bufptr;
614 assert(bufsize > 0);
615
616 bufptr = buf;
617 while (bufsize > 1) {
618 if ((c = jas_stream_getc(stream)) == EOF) {
619 break;
620 }
621 *bufptr++ = c;
622 --bufsize;
623 if (c == '\n') {
624 break;
625 }
626 }
627 *bufptr = '\0';
628 return buf;
629 }
630
631 int jas_stream_gobble(jas_stream_t *stream, int n)
632 {
633 int m;
634 m = n;
635 for (m = n; m > 0; --m) {
636 if (jas_stream_getc(stream) == EOF) {
637 return n - m;
638 }
639 }
640 return n;
641 }
642
643 /******************************************************************************\
644 * Code for getting and setting the stream position.
645 \******************************************************************************/
646
647 int jas_stream_isseekable(jas_stream_t *stream)
648 {
649 if (stream->ops_ == &jas_stream_memops) {
650 return 1;
651 } else if (stream->ops_ == &jas_stream_fileops) {
652 if ((*stream->ops_->seek_)(stream->obj_, 0, SEEK_CUR) < 0) {
653 return 0;
654 }
655 return 1;
656 } else {
657 return 0;
658 }
659 }
660
661 int jas_stream_rewind(jas_stream_t *stream)
662 {
663 return jas_stream_seek(stream, 0, SEEK_SET);
664 }
665
666 long jas_stream_seek(jas_stream_t *stream, long offset, int origin)
667 {
668 long newpos;
669
670 /* The buffer cannot be in use for both reading and writing. */
671 assert(!((stream->bufmode_ & JAS_STREAM_RDBUF) && (stream->bufmode_ &
672 JAS_STREAM_WRBUF)));
673
674 /* Reset the EOF indicator (since we may not be at the EOF anymore). */
675 stream->flags_ &= ~JAS_STREAM_EOF;
676
677 if (stream->bufmode_ & JAS_STREAM_RDBUF) {
678 if (origin == SEEK_CUR) {
679 offset -= stream->cnt_;
680 }
681 } else if (stream->bufmode_ & JAS_STREAM_WRBUF) {
682 if (jas_stream_flush(stream)) {
683 return -1;
684 }
685 }
686 stream->cnt_ = 0;
687 stream->ptr_ = stream->bufstart_;
688 stream->bufmode_ &= ~(JAS_STREAM_RDBUF | JAS_STREAM_WRBUF);
689
690 if ((newpos = (*stream->ops_->seek_)(stream->obj_, offset, origin))
691 < 0) {
692 return -1;
693 }
694
695 return newpos;
696 }
697
698 long jas_stream_tell(jas_stream_t *stream)
699 {
700 int adjust;
701 int offset;
702
703 if (stream->bufmode_ & JAS_STREAM_RDBUF) {
704 adjust = -stream->cnt_;
705 } else if (stream->bufmode_ & JAS_STREAM_WRBUF) {
706 adjust = stream->ptr_ - stream->bufstart_;
707 } else {
708 adjust = 0;
709 }
710
711 if ((offset = (*stream->ops_->seek_)(stream->obj_, 0, SEEK_CUR)) < 0) {
712 return -1;
713 }
714
715 return offset + adjust;
716 }
717
718 /******************************************************************************\
719 * Buffer initialization code.
720 \******************************************************************************/
721
722 static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf,
723 int bufsize)
724 {
725 /* If this function is being called, the buffer should not have been
726 initialized yet. */
727 assert(!stream->bufbase_);
728
729 if (bufmode != JAS_STREAM_UNBUF) {
730 /* The full- or line-buffered mode is being employed. */
731 if (!buf) {
732 /* The caller has not specified a buffer to employ, so allocate
733 one. */
734 if ((stream->bufbase_ = jas_malloc(JAS_STREAM_BUFSIZE +
735 JAS_STREAM_MAXPUTBACK))) {
736 stream->bufmode_ |= JAS_STREAM_FREEBUF;
737 stream->bufsize_ = JAS_STREAM_BUFSIZE;
738 } else {
739 /* The buffer allocation has failed. Resort to unbuffered
740 operation. */
741 stream->bufbase_ = stream->tinybuf_;
742 stream->bufsize_ = 1;
743 }
744 } else {
745 /* The caller has specified a buffer to employ. */
746 /* The buffer must be large enough to accommodate maximum
747 putback. */
748 assert(bufsize > JAS_STREAM_MAXPUTBACK);
749 stream->bufbase_ = JAS_CAST(unsigned char *, buf);
750 stream->bufsize_ = bufsize - JAS_STREAM_MAXPUTBACK;
751 }
752 } else {
753 /* The unbuffered mode is being employed. */
754 /* A buffer should not have been supplied by the caller. */
755 assert(!buf);
756 /* Use a trivial one-character buffer. */
757 stream->bufbase_ = stream->tinybuf_;
758 stream->bufsize_ = 1;
759 }
760 stream->bufstart_ = &stream->bufbase_[JAS_STREAM_MAXPUTBACK];
761 stream->ptr_ = stream->bufstart_;
762 stream->cnt_ = 0;
763 stream->bufmode_ |= bufmode & JAS_STREAM_BUFMODEMASK;
764 }
765
766 /******************************************************************************\
767 * Buffer filling and flushing code.
768 \******************************************************************************/
769
770 int jas_stream_flush(jas_stream_t *stream)
771 {
772 if (stream->bufmode_ & JAS_STREAM_RDBUF) {
773 return 0;
774 }
775 return jas_stream_flushbuf(stream, EOF);
776 }
777
778 int jas_stream_fillbuf(jas_stream_t *stream, int getflag)
779 {
780 int c;
781
782 /* The stream must not be in an error or EOF state. */
783 if ((stream->flags_ & (JAS_STREAM_ERRMASK)) != 0) {
784 return EOF;
785 }
786
787 /* The stream must be open for reading. */
788 if ((stream->openmode_ & JAS_STREAM_READ) == 0) {
789 return EOF;
790 }
791
792 /* Make a half-hearted attempt to confirm that the buffer is not
793 currently being used for writing. This check is not intended
794 to be foolproof! */
795 assert((stream->bufmode_ & JAS_STREAM_WRBUF) == 0);
796
797 assert(stream->ptr_ - stream->bufstart_ <= stream->bufsize_);
798
799 /* Mark the buffer as being used for reading. */
800 stream->bufmode_ |= JAS_STREAM_RDBUF;
801
802 /* Read new data into the buffer. */
803 stream->ptr_ = stream->bufstart_;
804 if ((stream->cnt_ = (*stream->ops_->read_)(stream->obj_,
805 (char *) stream->bufstart_, stream->bufsize_)) <= 0) {
806 if (stream->cnt_ < 0) {
807 stream->flags_ |= JAS_STREAM_ERR;
808 } else {
809 stream->flags_ |= JAS_STREAM_EOF;
810 }
811 stream->cnt_ = 0;
812 return EOF;
813 }
814
815 assert(stream->cnt_ > 0);
816 /* Get or peek at the first character in the buffer. */
817 c = (getflag) ? jas_stream_getc2(stream) : (*stream->ptr_);
818
819 return c;
820 }
821
822 int jas_stream_flushbuf(jas_stream_t *stream, int c)
823 {
824 int len;
825 int n;
826
827 /* The stream should not be in an error or EOF state. */
828 if ((stream->flags_ & (JAS_STREAM_ERRMASK)) != 0) {
829 return EOF;
830 }
831
832 /* The stream must be open for writing. */
833 if ((stream->openmode_ & (JAS_STREAM_WRITE | JAS_STREAM_APPEND)) == 0) {
834 return EOF;
835 }
836
837 /* The buffer should not currently be in use for reading. */
838 assert(!(stream->bufmode_ & JAS_STREAM_RDBUF));
839
840 /* Note: Do not use the quantity stream->cnt to determine the number
841 of characters in the buffer! Depending on how this function was
842 called, the stream->cnt value may be "off-by-one". */
843 len = stream->ptr_ - stream->bufstart_;
844 if (len > 0) {
845 n = (*stream->ops_->write_)(stream->obj_, (char *)
846 stream->bufstart_, len);
847 if (n != len) {
848 stream->flags_ |= JAS_STREAM_ERR;
849 return EOF;
850 }
851 }
852 stream->cnt_ = stream->bufsize_;
853 stream->ptr_ = stream->bufstart_;
854
855 stream->bufmode_ |= JAS_STREAM_WRBUF;
856
857 if (c != EOF) {
858 assert(stream->cnt_ > 0);
859 jas_stream_putc2(stream, c);
860 }
861
862 return 0;
863 }
864
865 /******************************************************************************\
866 * Miscellaneous code.
867 \******************************************************************************/
868
869 static int jas_strtoopenmode(const char *s)
870 {
871 int openmode = 0;
872 while (*s != '\0') {
873 switch (*s) {
874 case 'r':
875 openmode |= JAS_STREAM_READ;
876 break;
877 case 'w':
878 openmode |= JAS_STREAM_WRITE | JAS_STREAM_CREATE;
879 break;
880 case 'b':
881 openmode |= JAS_STREAM_BINARY;
882 break;
883 case 'a':
884 openmode |= JAS_STREAM_APPEND;
885 break;
886 case '+':
887 openmode |= JAS_STREAM_READ | JAS_STREAM_WRITE;
888 break;
889 default:
890 break;
891 }
892 ++s;
893 }
894 return openmode;
895 }
896
897 int jas_stream_copy(jas_stream_t *out, jas_stream_t *in, int n)
898 {
899 int all;
900 int c;
901 int m;
902
903 all = (n < 0) ? 1 : 0;
904
905 m = n;
906 while (all || m > 0) {
907 if ((c = jas_stream_getc_macro(in)) == EOF) {
908 /* The next character of input could not be read. */
909 /* Return with an error if an I/O error occurred
910 (not including EOF) or if an explicit copy count
911 was specified. */
912 return (!all || jas_stream_error(in)) ? (-1) : 0;
913 }
914 if (jas_stream_putc_macro(out, c) == EOF) {
915 return -1;
916 }
917 --m;
918 }
919 return 0;
920 }
921
922 long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt)
923 {
924 int old;
925
926 old = stream->rwcnt_;
927 stream->rwcnt_ = rwcnt;
928 return old;
929 }
930
931 int jas_stream_display(jas_stream_t *stream, FILE *fp, int n)
932 {
933 unsigned char buf[16];
934 int i;
935 int j;
936 int m;
937 int c;
938 int display;
939 int cnt;
940
941 cnt = n - (n % 16);
942 display = 1;
943
944 for (i = 0; i < n; i += 16) {
945 if (n > 16 && i > 0) {
946 display = (i >= cnt) ? 1 : 0;
947 }
948 if (display) {
949 fprintf(fp, "%08x:", i);
950 }
951 m = JAS_MIN(n - i, 16);
952 for (j = 0; j < m; ++j) {
953 if ((c = jas_stream_getc(stream)) == EOF) {
954 abort();
955 return -1;
956 }
957 buf[j] = c;
958 }
959 if (display) {
960 for (j = 0; j < m; ++j) {
961 fprintf(fp, " %02x", buf[j]);
962 }
963 fputc(' ', fp);
964 for (; j < 16; ++j) {
965 fprintf(fp, " ");
966 }
967 for (j = 0; j < m; ++j) {
968 if (isprint(buf[j])) {
969 fputc(buf[j], fp);
970 } else {
971 fputc(' ', fp);
972 }
973 }
974 fprintf(fp, "\n");
975 }
976
977
978 }
979 return 0;
980 }
981
982 long jas_stream_length(jas_stream_t *stream)
983 {
984 long oldpos;
985 long pos;
986 if ((oldpos = jas_stream_tell(stream)) < 0) {
987 return -1;
988 }
989 if (jas_stream_seek(stream, 0, SEEK_END) < 0) {
990 return -1;
991 }
992 if ((pos = jas_stream_tell(stream)) < 0) {
993 return -1;
994 }
995 if (jas_stream_seek(stream, oldpos, SEEK_SET) < 0) {
996 return -1;
997 }
998 return pos;
999 }
1000
1001 /******************************************************************************\
1002 * Memory stream object.
1003 \******************************************************************************/
1004
1005 static int mem_read(jas_stream_obj_t *obj, char *buf, int cnt)
1006 {
1007 int n;
1008 jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
1009 n = m->len_ - m->pos_;
1010 cnt = JAS_MIN(n, cnt);
1011 memcpy(buf, &m->buf_[m->pos_], cnt);
1012 m->pos_ += cnt;
1013 return cnt;
1014 }
1015
1016 static int mem_resize(jas_stream_memobj_t *m, int bufsize)
1017 {
1018 unsigned char *buf;
1019
1020 assert(m->buf_);
1021 if (!(buf = jas_realloc(m->buf_, bufsize * sizeof(unsigned char)))) {
1022 return -1;
1023 }
1024 m->buf_ = buf;
1025 m->bufsize_ = bufsize;
1026 return 0;
1027 }
1028
1029 static int mem_write(jas_stream_obj_t *obj, char *buf, int cnt)
1030 {
1031 int n;
1032 int ret;
1033 jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
1034 long newbufsize;
1035 long newpos;
1036
1037 newpos = m->pos_ + cnt;
1038 if (newpos > m->bufsize_ && m->growable_) {
1039 newbufsize = m->bufsize_;
1040 while (newbufsize < newpos) {
1041 newbufsize <<= 1;
1042 assert(newbufsize >= 0);
1043 }
1044 if (mem_resize(m, newbufsize)) {
1045 return -1;
1046 }
1047 }
1048 if (m->pos_ > m->len_) {
1049 /* The current position is beyond the end of the file, so
1050 pad the file to the current position with zeros. */
1051 n = JAS_MIN(m->pos_, m->bufsize_) - m->len_;
1052 if (n > 0) {
1053 memset(&m->buf_[m->len_], 0, n);
1054 m->len_ += n;
1055 }
1056 if (m->pos_ != m->len_) {
1057 /* The buffer is not big enough. */
1058 return 0;
1059 }
1060 }
1061 n = m->bufsize_ - m->pos_;
1062 ret = JAS_MIN(n, cnt);
1063 if (ret > 0) {
1064 memcpy(&m->buf_[m->pos_], buf, ret);
1065 m->pos_ += ret;
1066 }
1067 if (m->pos_ > m->len_) {
1068 m->len_ = m->pos_;
1069 }
1070 assert(ret == cnt);
1071 return ret;
1072 }
1073
1074 static long mem_seek(jas_stream_obj_t *obj, long offset, int origin)
1075 {
1076 jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
1077 long newpos;
1078
1079 switch (origin) {
1080 case SEEK_SET:
1081 newpos = offset;
1082 break;
1083 case SEEK_END:
1084 newpos = m->len_ - offset;
1085 break;
1086 case SEEK_CUR:
1087 newpos = m->pos_ + offset;
1088 break;
1089 default:
1090 abort();
1091 break;
1092 }
1093 if (newpos < 0) {
1094 return -1;
1095 }
1096 m->pos_ = newpos;
1097
1098 return m->pos_;
1099 }
1100
1101 static int mem_close(jas_stream_obj_t *obj)
1102 {
1103 jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
1104 if (m->myalloc_ && m->buf_) {
1105 jas_free(m->buf_);
1106 m->buf_ = 0;
1107 }
1108 jas_free(obj);
1109 return 0;
1110 }
1111
1112 /******************************************************************************\
1113 * File stream object.
1114 \******************************************************************************/
1115
1116 static int file_read(jas_stream_obj_t *obj, char *buf, int cnt)
1117 {
1118 int fd;
1119 fd = *((int *)obj);
1120 return read(fd, buf, cnt);
1121 }
1122
1123 static int file_write(jas_stream_obj_t *obj, char *buf, int cnt)
1124 {
1125 int fd;
1126 fd = *((int *)obj);
1127 return write(fd, buf, cnt);
1128 }
1129
1130 static long file_seek(jas_stream_obj_t *obj, long offset, int origin)
1131 {
1132 int fd;
1133 fd = *((int *)obj);
1134 return lseek(fd, offset, origin);
1135 }
1136
1137 static int file_close(jas_stream_obj_t *obj)
1138 {
1139 int fd;
1140 fd = *((int *)obj);
1141 jas_free(obj);
1142 return close(fd);
1143 }
1144
1145 /******************************************************************************\
1146 * Stdio file stream object.
1147 \******************************************************************************/
1148
1149 static int sfile_read(jas_stream_obj_t *obj, char *buf, int cnt)
1150 {
1151 FILE *fp;
1152 fp = JAS_CAST(FILE *, obj);
1153 return fread(buf, 1, cnt, fp);
1154 }
1155
1156 static int sfile_write(jas_stream_obj_t *obj, char *buf, int cnt)
1157 {
1158 FILE *fp;
1159 fp = JAS_CAST(FILE *, obj);
1160 return fwrite(buf, 1, cnt, fp);
1161 }
1162
1163 static long sfile_seek(jas_stream_obj_t *obj, long offset, int origin)
1164 {
1165 FILE *fp;
1166 fp = JAS_CAST(FILE *, obj);
1167 return fseek(fp, offset, origin);
1168 }
1169
1170 static int sfile_close(jas_stream_obj_t *obj)
1171 {
1172 FILE *fp;
1173 fp = JAS_CAST(FILE *, obj);
1174 return fclose(fp);
1175 }