jas_stream.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * I/O Stream Library
  64. *
  65. * $Id: jas_stream.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <assert.h>
  71. #if defined(HAVE_FCNTL_H)
  72. #include <fcntl.h>
  73. #endif
  74. #include <stdlib.h>
  75. #include <stdarg.h>
  76. #include <stdio.h>
  77. #include <ctype.h>
  78. #if defined(HAVE_UNISTD_H)
  79. #include <unistd.h>
  80. #endif
  81. #if defined(WIN32) || defined(HAVE_IO_H)
  82. #include <io.h>
  83. #endif
  84. #include "jasper/jas_types.h"
  85. #include "jasper/jas_stream.h"
  86. #include "jasper/jas_malloc.h"
  87. #include "jasper/jas_math.h"
  88. /******************************************************************************\
  89. * Local function prototypes.
  90. \******************************************************************************/
  91. static int jas_strtoopenmode(const char *s);
  92. static void jas_stream_destroy(jas_stream_t *stream);
  93. static jas_stream_t *jas_stream_create(void);
  94. static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf,
  95. int bufsize);
  96. static int mem_read(jas_stream_obj_t *obj, char *buf, int cnt);
  97. static int mem_write(jas_stream_obj_t *obj, char *buf, int cnt);
  98. static long mem_seek(jas_stream_obj_t *obj, long offset, int origin);
  99. static int mem_close(jas_stream_obj_t *obj);
  100. static int sfile_read(jas_stream_obj_t *obj, char *buf, int cnt);
  101. static int sfile_write(jas_stream_obj_t *obj, char *buf, int cnt);
  102. static long sfile_seek(jas_stream_obj_t *obj, long offset, int origin);
  103. static int sfile_close(jas_stream_obj_t *obj);
  104. static int file_read(jas_stream_obj_t *obj, char *buf, int cnt);
  105. static int file_write(jas_stream_obj_t *obj, char *buf, int cnt);
  106. static long file_seek(jas_stream_obj_t *obj, long offset, int origin);
  107. static int file_close(jas_stream_obj_t *obj);
  108. /******************************************************************************\
  109. * Local data.
  110. \******************************************************************************/
  111. static jas_stream_ops_t jas_stream_fileops = {
  112. file_read,
  113. file_write,
  114. file_seek,
  115. file_close
  116. };
  117. static jas_stream_ops_t jas_stream_sfileops = {
  118. sfile_read,
  119. sfile_write,
  120. sfile_seek,
  121. sfile_close
  122. };
  123. static jas_stream_ops_t jas_stream_memops = {
  124. mem_read,
  125. mem_write,
  126. mem_seek,
  127. mem_close
  128. };
  129. /******************************************************************************\
  130. * Code for opening and closing streams.
  131. \******************************************************************************/
  132. static jas_stream_t *jas_stream_create()
  133. {
  134. jas_stream_t *stream;
  135. if (!(stream = jas_malloc(sizeof(jas_stream_t)))) {
  136. return 0;
  137. }
  138. stream->openmode_ = 0;
  139. stream->bufmode_ = 0;
  140. stream->flags_ = 0;
  141. stream->bufbase_ = 0;
  142. stream->bufstart_ = 0;
  143. stream->bufsize_ = 0;
  144. stream->ptr_ = 0;
  145. stream->cnt_ = 0;
  146. stream->ops_ = 0;
  147. stream->obj_ = 0;
  148. stream->rwcnt_ = 0;
  149. stream->rwlimit_ = -1;
  150. return stream;
  151. }
  152. jas_stream_t *jas_stream_memopen(char *buf, int bufsize)
  153. {
  154. jas_stream_t *stream;
  155. jas_stream_memobj_t *obj;
  156. if (!(stream = jas_stream_create())) {
  157. return 0;
  158. }
  159. /* A stream associated with a memory buffer is always opened
  160. for both reading and writing in binary mode. */
  161. stream->openmode_ = JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
  162. /* Since the stream data is already resident in memory, buffering
  163. is not necessary. */
  164. /* But... It still may be faster to use buffering anyways. */
  165. jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
  166. /* Select the operations for a memory stream. */
  167. stream->ops_ = &jas_stream_memops;
  168. /* Allocate memory for the underlying memory stream object. */
  169. if (!(obj = jas_malloc(sizeof(jas_stream_memobj_t)))) {
  170. jas_stream_destroy(stream);
  171. return 0;
  172. }
  173. stream->obj_ = (void *) obj;
  174. /* Initialize a few important members of the memory stream object. */
  175. obj->myalloc_ = 0;
  176. obj->buf_ = 0;
  177. /* If the buffer size specified is nonpositive, then the buffer
  178. is allocated internally and automatically grown as needed. */
  179. if (bufsize <= 0) {
  180. obj->bufsize_ = 1024;
  181. obj->growable_ = 1;
  182. } else {
  183. obj->bufsize_ = bufsize;
  184. obj->growable_ = 0;
  185. }
  186. if (buf) {
  187. obj->buf_ = (unsigned char *) buf;
  188. } else {
  189. obj->buf_ = jas_malloc(obj->bufsize_);
  190. obj->myalloc_ = 1;
  191. }
  192. if (!obj->buf_) {
  193. jas_stream_close(stream);
  194. return 0;
  195. }
  196. if (bufsize > 0 && buf) {
  197. /* If a buffer was supplied by the caller and its length is positive,
  198. make the associated buffer data appear in the stream initially. */
  199. obj->len_ = bufsize;
  200. } else {
  201. /* The stream is initially empty. */
  202. obj->len_ = 0;
  203. }
  204. obj->pos_ = 0;
  205. return stream;
  206. }
  207. jas_stream_t *jas_stream_fopen(const char *filename, const char *mode)
  208. {
  209. jas_stream_t *stream;
  210. jas_stream_fileobj_t *obj;
  211. int openflags;
  212. /* Allocate a stream object. */
  213. if (!(stream = jas_stream_create())) {
  214. return 0;
  215. }
  216. /* Parse the mode string. */
  217. stream->openmode_ = jas_strtoopenmode(mode);
  218. /* Determine the correct flags to use for opening the file. */
  219. if ((stream->openmode_ & JAS_STREAM_READ) &&
  220. (stream->openmode_ & JAS_STREAM_WRITE)) {
  221. openflags = O_RDWR;
  222. } else if (stream->openmode_ & JAS_STREAM_READ) {
  223. openflags = O_RDONLY;
  224. } else if (stream->openmode_ & JAS_STREAM_WRITE) {
  225. openflags = O_WRONLY;
  226. } else {
  227. openflags = 0;
  228. }
  229. if (stream->openmode_ & JAS_STREAM_APPEND) {
  230. openflags |= O_APPEND;
  231. }
  232. if (stream->openmode_ & JAS_STREAM_BINARY) {
  233. openflags |= O_BINARY;
  234. }
  235. if (stream->openmode_ & JAS_STREAM_CREATE) {
  236. openflags |= O_CREAT | O_TRUNC;
  237. }
  238. /* Allocate space for the underlying file stream object. */
  239. if (!(obj = jas_malloc(sizeof(jas_stream_fileobj_t)))) {
  240. jas_stream_destroy(stream);
  241. return 0;
  242. }
  243. obj->fd = -1;
  244. obj->flags = 0;
  245. obj->pathname[0] = '\0';
  246. stream->obj_ = (void *) obj;
  247. /* Select the operations for a file stream object. */
  248. stream->ops_ = &jas_stream_fileops;
  249. /* Open the underlying file. */
  250. if ((obj->fd = open(filename, openflags, JAS_STREAM_PERMS)) < 0) {
  251. jas_stream_destroy(stream);
  252. return 0;
  253. }
  254. /* By default, use full buffering for this type of stream. */
  255. jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
  256. return stream;
  257. }
  258. jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp)
  259. {
  260. jas_stream_t *stream;
  261. int openflags;
  262. /* Eliminate compiler warning about unused variable. */
  263. path = 0;
  264. /* Allocate a stream object. */
  265. if (!(stream = jas_stream_create())) {
  266. return 0;
  267. }
  268. /* Parse the mode string. */
  269. stream->openmode_ = jas_strtoopenmode(mode);
  270. /* Determine the correct flags to use for opening the file. */
  271. if ((stream->openmode_ & JAS_STREAM_READ) &&
  272. (stream->openmode_ & JAS_STREAM_WRITE)) {
  273. openflags = O_RDWR;
  274. } else if (stream->openmode_ & JAS_STREAM_READ) {
  275. openflags = O_RDONLY;
  276. } else if (stream->openmode_ & JAS_STREAM_WRITE) {
  277. openflags = O_WRONLY;
  278. } else {
  279. openflags = 0;
  280. }
  281. if (stream->openmode_ & JAS_STREAM_APPEND) {
  282. openflags |= O_APPEND;
  283. }
  284. if (stream->openmode_ & JAS_STREAM_BINARY) {
  285. openflags |= O_BINARY;
  286. }
  287. if (stream->openmode_ & JAS_STREAM_CREATE) {
  288. openflags |= O_CREAT | O_TRUNC;
  289. }
  290. stream->obj_ = JAS_CAST(void *, fp);
  291. /* Select the operations for a file stream object. */
  292. stream->ops_ = &jas_stream_sfileops;
  293. /* By default, use full buffering for this type of stream. */
  294. jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
  295. return stream;
  296. }
  297. jas_stream_t *jas_stream_tmpfile()
  298. {
  299. jas_stream_t *stream;
  300. jas_stream_fileobj_t *obj;
  301. char *tmpname;
  302. if (!(stream = jas_stream_create())) {
  303. return 0;
  304. }
  305. /* A temporary file stream is always opened for both reading and
  306. writing in binary mode. */
  307. stream->openmode_ = JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
  308. /* Allocate memory for the underlying temporary file object. */
  309. if (!(obj = jas_malloc(sizeof(jas_stream_fileobj_t)))) {
  310. jas_stream_destroy(stream);
  311. return 0;
  312. }
  313. obj->fd = -1;
  314. obj->flags = 0;
  315. stream->obj_ = obj;
  316. #ifdef _WIN32
  317. /* Choose a file name. */
  318. tmpname = tempnam(NULL, NULL);
  319. strcpy(obj->pathname, tmpname);
  320. free(tmpname);
  321. /* Open the underlying file. */
  322. if ((obj->fd = open(obj->pathname, O_CREAT | O_EXCL | O_RDWR | O_TRUNC | O_BINARY | O_TEMPORARY | _O_SHORT_LIVED,
  323. JAS_STREAM_PERMS)) < 0) {
  324. jas_stream_destroy(stream);
  325. return 0;
  326. }
  327. #else
  328. /* Choose a file name. */
  329. snprintf(obj->pathname, L_tmpnam, "%s/tmp.XXXXXXXXXX", P_tmpdir);
  330. /* Open the underlying file. */
  331. if ((obj->fd = mkstemp(obj->pathname)) < 0) {
  332. jas_stream_destroy(stream);
  333. return 0;
  334. }
  335. #endif
  336. /* Unlink the file so that it will disappear if the program
  337. terminates abnormally. */
  338. if (unlink(obj->pathname)) {
  339. jas_stream_destroy(stream);
  340. return 0;
  341. }
  342. /* Use full buffering. */
  343. jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
  344. stream->ops_ = &jas_stream_fileops;
  345. return stream;
  346. }
  347. jas_stream_t *jas_stream_fdopen(int fd, const char *mode)
  348. {
  349. jas_stream_t *stream;
  350. jas_stream_fileobj_t *obj;
  351. /* Allocate a stream object. */
  352. if (!(stream = jas_stream_create())) {
  353. return 0;
  354. }
  355. /* Parse the mode string. */
  356. stream->openmode_ = jas_strtoopenmode(mode);
  357. #if defined(WIN32)
  358. /* Argh!!! Someone ought to banish text mode (i.e., O_TEXT) to the
  359. greatest depths of purgatory! */
  360. /* Ensure that the file descriptor is in binary mode, if the caller
  361. has specified the binary mode flag. Arguably, the caller ought to
  362. take care of this, but text mode is a ugly wart anyways, so we save
  363. the caller some grief by handling this within the stream library. */
  364. /* This ugliness is mainly for the benefit of those who run the
  365. JasPer software under Windows from shells that insist on opening
  366. files in text mode. For example, in the Cygwin environment,
  367. shells often open files in text mode when I/O redirection is
  368. used. Grr... */
  369. if (stream->openmode_ & JAS_STREAM_BINARY) {
  370. setmode(fd, O_BINARY);
  371. }
  372. #endif
  373. /* Allocate space for the underlying file stream object. */
  374. if (!(obj = jas_malloc(sizeof(jas_stream_fileobj_t)))) {
  375. jas_stream_destroy(stream);
  376. return 0;
  377. }
  378. obj->fd = fd;
  379. obj->flags = 0;
  380. obj->pathname[0] = '\0';
  381. stream->obj_ = (void *) obj;
  382. /* Do not close the underlying file descriptor when the stream is
  383. closed. */
  384. obj->flags |= JAS_STREAM_FILEOBJ_NOCLOSE;
  385. /* By default, use full buffering for this type of stream. */
  386. jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
  387. /* Select the operations for a file stream object. */
  388. stream->ops_ = &jas_stream_fileops;
  389. return stream;
  390. }
  391. static void jas_stream_destroy(jas_stream_t *stream)
  392. {
  393. /* If the memory for the buffer was allocated with malloc, free
  394. this memory. */
  395. if ((stream->bufmode_ & JAS_STREAM_FREEBUF) && stream->bufbase_) {
  396. jas_free(stream->bufbase_);
  397. stream->bufbase_ = 0;
  398. }
  399. jas_free(stream);
  400. }
  401. int jas_stream_close(jas_stream_t *stream)
  402. {
  403. /* Flush buffer if necessary. */
  404. jas_stream_flush(stream);
  405. /* Close the underlying stream object. */
  406. (*stream->ops_->close_)(stream->obj_);
  407. jas_stream_destroy(stream);
  408. return 0;
  409. }
  410. /******************************************************************************\
  411. * Code for reading and writing streams.
  412. \******************************************************************************/
  413. int jas_stream_getc_func(jas_stream_t *stream)
  414. {
  415. assert(stream->ptr_ - stream->bufbase_ <= stream->bufsize_ +
  416. JAS_STREAM_MAXPUTBACK);
  417. return jas_stream_getc_macro(stream);
  418. }
  419. int jas_stream_putc_func(jas_stream_t *stream, int c)
  420. {
  421. assert(stream->ptr_ - stream->bufstart_ <= stream->bufsize_);
  422. return jas_stream_putc_macro(stream, c);
  423. }
  424. int jas_stream_ungetc(jas_stream_t *stream, int c)
  425. {
  426. if (!stream->ptr_ || stream->ptr_ == stream->bufbase_) {
  427. return -1;
  428. }
  429. /* Reset the EOF indicator (since we now have at least one character
  430. to read). */
  431. stream->flags_ &= ~JAS_STREAM_EOF;
  432. --stream->rwcnt_;
  433. --stream->ptr_;
  434. ++stream->cnt_;
  435. *stream->ptr_ = c;
  436. return 0;
  437. }
  438. int jas_stream_read(jas_stream_t *stream, void *buf, int cnt)
  439. {
  440. int n;
  441. int c;
  442. char *bufptr;
  443. bufptr = buf;
  444. n = 0;
  445. while (n < cnt) {
  446. if ((c = jas_stream_getc(stream)) == EOF) {
  447. return n;
  448. }
  449. *bufptr++ = c;
  450. ++n;
  451. }
  452. return n;
  453. }
  454. int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt)
  455. {
  456. int n;
  457. const char *bufptr;
  458. bufptr = buf;
  459. n = 0;
  460. while (n < cnt) {
  461. if (jas_stream_putc(stream, *bufptr) == EOF) {
  462. return n;
  463. }
  464. ++bufptr;
  465. ++n;
  466. }
  467. return n;
  468. }
  469. /* Note: This function uses a fixed size buffer. Therefore, it cannot
  470. handle invocations that will produce more output than can be held
  471. by the buffer. */
  472. int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...)
  473. {
  474. va_list ap;
  475. char buf[4096];
  476. int ret;
  477. va_start(ap, fmt);
  478. ret = vsnprintf(buf, sizeof buf, fmt, ap);
  479. jas_stream_puts(stream, buf);
  480. va_end(ap);
  481. return ret;
  482. }
  483. int jas_stream_puts(jas_stream_t *stream, const char *s)
  484. {
  485. while (*s != '\0') {
  486. if (jas_stream_putc_macro(stream, *s) == EOF) {
  487. return -1;
  488. }
  489. ++s;
  490. }
  491. return 0;
  492. }
  493. char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize)
  494. {
  495. int c;
  496. char *bufptr;
  497. assert(bufsize > 0);
  498. bufptr = buf;
  499. while (bufsize > 1) {
  500. if ((c = jas_stream_getc(stream)) == EOF) {
  501. break;
  502. }
  503. *bufptr++ = c;
  504. --bufsize;
  505. if (c == '\n') {
  506. break;
  507. }
  508. }
  509. *bufptr = '\0';
  510. return buf;
  511. }
  512. int jas_stream_gobble(jas_stream_t *stream, int n)
  513. {
  514. int m;
  515. m = n;
  516. for (m = n; m > 0; --m) {
  517. if (jas_stream_getc(stream) == EOF) {
  518. return n - m;
  519. }
  520. }
  521. return n;
  522. }
  523. int jas_stream_pad(jas_stream_t *stream, int n, int c)
  524. {
  525. int m;
  526. m = n;
  527. for (m = n; m > 0; --m) {
  528. if (jas_stream_putc(stream, c) == EOF)
  529. return n - m;
  530. }
  531. return n;
  532. }
  533. /******************************************************************************\
  534. * Code for getting and setting the stream position.
  535. \******************************************************************************/
  536. int jas_stream_isseekable(jas_stream_t *stream)
  537. {
  538. if (stream->ops_ == &jas_stream_memops) {
  539. return 1;
  540. } else if (stream->ops_ == &jas_stream_fileops) {
  541. if ((*stream->ops_->seek_)(stream->obj_, 0, SEEK_CUR) < 0) {
  542. return 0;
  543. }
  544. return 1;
  545. } else {
  546. return 0;
  547. }
  548. }
  549. int jas_stream_rewind(jas_stream_t *stream)
  550. {
  551. return jas_stream_seek(stream, 0, SEEK_SET);
  552. }
  553. long jas_stream_seek(jas_stream_t *stream, long offset, int origin)
  554. {
  555. long newpos;
  556. /* The buffer cannot be in use for both reading and writing. */
  557. assert(!((stream->bufmode_ & JAS_STREAM_RDBUF) && (stream->bufmode_ &
  558. JAS_STREAM_WRBUF)));
  559. /* Reset the EOF indicator (since we may not be at the EOF anymore). */
  560. stream->flags_ &= ~JAS_STREAM_EOF;
  561. if (stream->bufmode_ & JAS_STREAM_RDBUF) {
  562. if (origin == SEEK_CUR) {
  563. offset -= stream->cnt_;
  564. }
  565. } else if (stream->bufmode_ & JAS_STREAM_WRBUF) {
  566. if (jas_stream_flush(stream)) {
  567. return -1;
  568. }
  569. }
  570. stream->cnt_ = 0;
  571. stream->ptr_ = stream->bufstart_;
  572. stream->bufmode_ &= ~(JAS_STREAM_RDBUF | JAS_STREAM_WRBUF);
  573. if ((newpos = (*stream->ops_->seek_)(stream->obj_, offset, origin))
  574. < 0) {
  575. return -1;
  576. }
  577. return newpos;
  578. }
  579. long jas_stream_tell(jas_stream_t *stream)
  580. {
  581. int adjust;
  582. int offset;
  583. if (stream->bufmode_ & JAS_STREAM_RDBUF) {
  584. adjust = -stream->cnt_;
  585. } else if (stream->bufmode_ & JAS_STREAM_WRBUF) {
  586. adjust = stream->ptr_ - stream->bufstart_;
  587. } else {
  588. adjust = 0;
  589. }
  590. if ((offset = (*stream->ops_->seek_)(stream->obj_, 0, SEEK_CUR)) < 0) {
  591. return -1;
  592. }
  593. return offset + adjust;
  594. }
  595. /******************************************************************************\
  596. * Buffer initialization code.
  597. \******************************************************************************/
  598. static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf,
  599. int bufsize)
  600. {
  601. /* If this function is being called, the buffer should not have been
  602. initialized yet. */
  603. assert(!stream->bufbase_);
  604. if (bufmode != JAS_STREAM_UNBUF) {
  605. /* The full- or line-buffered mode is being employed. */
  606. if (!buf) {
  607. /* The caller has not specified a buffer to employ, so allocate
  608. one. */
  609. if ((stream->bufbase_ = jas_malloc(JAS_STREAM_BUFSIZE +
  610. JAS_STREAM_MAXPUTBACK))) {
  611. stream->bufmode_ |= JAS_STREAM_FREEBUF;
  612. stream->bufsize_ = JAS_STREAM_BUFSIZE;
  613. } else {
  614. /* The buffer allocation has failed. Resort to unbuffered
  615. operation. */
  616. stream->bufbase_ = stream->tinybuf_;
  617. stream->bufsize_ = 1;
  618. }
  619. } else {
  620. /* The caller has specified a buffer to employ. */
  621. /* The buffer must be large enough to accommodate maximum
  622. putback. */
  623. assert(bufsize > JAS_STREAM_MAXPUTBACK);
  624. stream->bufbase_ = JAS_CAST(uchar *, buf);
  625. stream->bufsize_ = bufsize - JAS_STREAM_MAXPUTBACK;
  626. }
  627. } else {
  628. /* The unbuffered mode is being employed. */
  629. /* A buffer should not have been supplied by the caller. */
  630. assert(!buf);
  631. /* Use a trivial one-character buffer. */
  632. stream->bufbase_ = stream->tinybuf_;
  633. stream->bufsize_ = 1;
  634. }
  635. stream->bufstart_ = &stream->bufbase_[JAS_STREAM_MAXPUTBACK];
  636. stream->ptr_ = stream->bufstart_;
  637. stream->cnt_ = 0;
  638. stream->bufmode_ |= bufmode & JAS_STREAM_BUFMODEMASK;
  639. }
  640. /******************************************************************************\
  641. * Buffer filling and flushing code.
  642. \******************************************************************************/
  643. int jas_stream_flush(jas_stream_t *stream)
  644. {
  645. if (stream->bufmode_ & JAS_STREAM_RDBUF) {
  646. return 0;
  647. }
  648. return jas_stream_flushbuf(stream, EOF);
  649. }
  650. int jas_stream_fillbuf(jas_stream_t *stream, int getflag)
  651. {
  652. int c;
  653. /* The stream must not be in an error or EOF state. */
  654. if ((stream->flags_ & (JAS_STREAM_ERRMASK)) != 0) {
  655. return EOF;
  656. }
  657. /* The stream must be open for reading. */
  658. if ((stream->openmode_ & JAS_STREAM_READ) == 0) {
  659. return EOF;
  660. }
  661. /* Make a half-hearted attempt to confirm that the buffer is not
  662. currently being used for writing. This check is not intended
  663. to be foolproof! */
  664. assert((stream->bufmode_ & JAS_STREAM_WRBUF) == 0);
  665. assert(stream->ptr_ - stream->bufstart_ <= stream->bufsize_);
  666. /* Mark the buffer as being used for reading. */
  667. stream->bufmode_ |= JAS_STREAM_RDBUF;
  668. /* Read new data into the buffer. */
  669. stream->ptr_ = stream->bufstart_;
  670. if ((stream->cnt_ = (*stream->ops_->read_)(stream->obj_,
  671. (char *) stream->bufstart_, stream->bufsize_)) <= 0) {
  672. if (stream->cnt_ < 0) {
  673. stream->flags_ |= JAS_STREAM_ERR;
  674. } else {
  675. stream->flags_ |= JAS_STREAM_EOF;
  676. }
  677. stream->cnt_ = 0;
  678. return EOF;
  679. }
  680. assert(stream->cnt_ > 0);
  681. /* Get or peek at the first character in the buffer. */
  682. c = (getflag) ? jas_stream_getc2(stream) : (*stream->ptr_);
  683. return c;
  684. }
  685. int jas_stream_flushbuf(jas_stream_t *stream, int c)
  686. {
  687. int len;
  688. int n;
  689. /* The stream should not be in an error or EOF state. */
  690. if ((stream->flags_ & (JAS_STREAM_ERRMASK)) != 0) {
  691. return EOF;
  692. }
  693. /* The stream must be open for writing. */
  694. if ((stream->openmode_ & (JAS_STREAM_WRITE | JAS_STREAM_APPEND)) == 0) {
  695. return EOF;
  696. }
  697. /* The buffer should not currently be in use for reading. */
  698. assert(!(stream->bufmode_ & JAS_STREAM_RDBUF));
  699. /* Note: Do not use the quantity stream->cnt to determine the number
  700. of characters in the buffer! Depending on how this function was
  701. called, the stream->cnt value may be "off-by-one". */
  702. len = stream->ptr_ - stream->bufstart_;
  703. if (len > 0) {
  704. n = (*stream->ops_->write_)(stream->obj_, (char *)
  705. stream->bufstart_, len);
  706. if (n != len) {
  707. stream->flags_ |= JAS_STREAM_ERR;
  708. return EOF;
  709. }
  710. }
  711. stream->cnt_ = stream->bufsize_;
  712. stream->ptr_ = stream->bufstart_;
  713. stream->bufmode_ |= JAS_STREAM_WRBUF;
  714. if (c != EOF) {
  715. assert(stream->cnt_ > 0);
  716. return jas_stream_putc2(stream, c);
  717. }
  718. return 0;
  719. }
  720. /******************************************************************************\
  721. * Miscellaneous code.
  722. \******************************************************************************/
  723. static int jas_strtoopenmode(const char *s)
  724. {
  725. int openmode = 0;
  726. while (*s != '\0') {
  727. switch (*s) {
  728. case 'r':
  729. openmode |= JAS_STREAM_READ;
  730. break;
  731. case 'w':
  732. openmode |= JAS_STREAM_WRITE | JAS_STREAM_CREATE;
  733. break;
  734. case 'b':
  735. openmode |= JAS_STREAM_BINARY;
  736. break;
  737. case 'a':
  738. openmode |= JAS_STREAM_APPEND;
  739. break;
  740. case '+':
  741. openmode |= JAS_STREAM_READ | JAS_STREAM_WRITE;
  742. break;
  743. default:
  744. break;
  745. }
  746. ++s;
  747. }
  748. return openmode;
  749. }
  750. int jas_stream_copy(jas_stream_t *out, jas_stream_t *in, int n)
  751. {
  752. int all;
  753. int c;
  754. int m;
  755. all = (n < 0) ? 1 : 0;
  756. m = n;
  757. while (all || m > 0) {
  758. if ((c = jas_stream_getc_macro(in)) == EOF) {
  759. /* The next character of input could not be read. */
  760. /* Return with an error if an I/O error occured
  761. (not including EOF) or if an explicit copy count
  762. was specified. */
  763. return (!all || jas_stream_error(in)) ? (-1) : 0;
  764. }
  765. if (jas_stream_putc_macro(out, c) == EOF) {
  766. return -1;
  767. }
  768. --m;
  769. }
  770. return 0;
  771. }
  772. long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt)
  773. {
  774. int old;
  775. old = stream->rwcnt_;
  776. stream->rwcnt_ = rwcnt;
  777. return old;
  778. }
  779. int jas_stream_display(jas_stream_t *stream, FILE *fp, int n)
  780. {
  781. unsigned char buf[16];
  782. int i;
  783. int j;
  784. int m;
  785. int c;
  786. int display;
  787. int cnt;
  788. cnt = n - (n % 16);
  789. display = 1;
  790. for (i = 0; i < n; i += 16) {
  791. if (n > 16 && i > 0) {
  792. display = (i >= cnt) ? 1 : 0;
  793. }
  794. if (display) {
  795. fprintf(fp, "%08x:", i);
  796. }
  797. m = JAS_MIN(n - i, 16);
  798. for (j = 0; j < m; ++j) {
  799. if ((c = jas_stream_getc(stream)) == EOF) {
  800. abort();
  801. return -1;
  802. }
  803. buf[j] = c;
  804. }
  805. if (display) {
  806. for (j = 0; j < m; ++j) {
  807. fprintf(fp, " %02x", buf[j]);
  808. }
  809. fputc(' ', fp);
  810. for (; j < 16; ++j) {
  811. fprintf(fp, " ");
  812. }
  813. for (j = 0; j < m; ++j) {
  814. if (isprint(buf[j])) {
  815. fputc(buf[j], fp);
  816. } else {
  817. fputc(' ', fp);
  818. }
  819. }
  820. fprintf(fp, "\n");
  821. }
  822. }
  823. return 0;
  824. }
  825. long jas_stream_length(jas_stream_t *stream)
  826. {
  827. long oldpos;
  828. long pos;
  829. if ((oldpos = jas_stream_tell(stream)) < 0) {
  830. return -1;
  831. }
  832. if (jas_stream_seek(stream, 0, SEEK_END) < 0) {
  833. return -1;
  834. }
  835. if ((pos = jas_stream_tell(stream)) < 0) {
  836. return -1;
  837. }
  838. if (jas_stream_seek(stream, oldpos, SEEK_SET) < 0) {
  839. return -1;
  840. }
  841. return pos;
  842. }
  843. /******************************************************************************\
  844. * Memory stream object.
  845. \******************************************************************************/
  846. static int mem_read(jas_stream_obj_t *obj, char *buf, int cnt)
  847. {
  848. int n;
  849. jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
  850. n = m->len_ - m->pos_;
  851. cnt = JAS_MIN(n, cnt);
  852. memcpy(buf, &m->buf_[m->pos_], cnt);
  853. m->pos_ += cnt;
  854. return cnt;
  855. }
  856. static int mem_resize(jas_stream_memobj_t *m, int bufsize)
  857. {
  858. unsigned char *buf;
  859. assert(m->buf_);
  860. if (!(buf = jas_realloc(m->buf_, bufsize))) {
  861. return -1;
  862. }
  863. m->buf_ = buf;
  864. m->bufsize_ = bufsize;
  865. return 0;
  866. }
  867. static int mem_write(jas_stream_obj_t *obj, char *buf, int cnt)
  868. {
  869. int n;
  870. int ret;
  871. jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
  872. long newbufsize;
  873. long newpos;
  874. newpos = m->pos_ + cnt;
  875. if (newpos > m->bufsize_ && m->growable_) {
  876. newbufsize = m->bufsize_;
  877. while (newbufsize < newpos) {
  878. newbufsize <<= 1;
  879. assert(newbufsize >= 0);
  880. }
  881. if (mem_resize(m, newbufsize)) {
  882. return -1;
  883. }
  884. }
  885. if (m->pos_ > m->len_) {
  886. /* The current position is beyond the end of the file, so
  887. pad the file to the current position with zeros. */
  888. n = JAS_MIN(m->pos_, m->bufsize_) - m->len_;
  889. if (n > 0) {
  890. memset(&m->buf_[m->len_], 0, n);
  891. m->len_ += n;
  892. }
  893. if (m->pos_ != m->len_) {
  894. /* The buffer is not big enough. */
  895. return 0;
  896. }
  897. }
  898. n = m->bufsize_ - m->pos_;
  899. ret = JAS_MIN(n, cnt);
  900. if (ret > 0) {
  901. memcpy(&m->buf_[m->pos_], buf, ret);
  902. m->pos_ += ret;
  903. }
  904. if (m->pos_ > m->len_) {
  905. m->len_ = m->pos_;
  906. }
  907. assert(ret == cnt);
  908. return ret;
  909. }
  910. static long mem_seek(jas_stream_obj_t *obj, long offset, int origin)
  911. {
  912. jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
  913. long newpos;
  914. switch (origin) {
  915. case SEEK_SET:
  916. newpos = offset;
  917. break;
  918. case SEEK_END:
  919. newpos = m->len_ - offset;
  920. break;
  921. case SEEK_CUR:
  922. newpos = m->pos_ + offset;
  923. break;
  924. default:
  925. abort();
  926. break;
  927. }
  928. if (newpos < 0) {
  929. return -1;
  930. }
  931. m->pos_ = newpos;
  932. return m->pos_;
  933. }
  934. static int mem_close(jas_stream_obj_t *obj)
  935. {
  936. jas_stream_memobj_t *m = (jas_stream_memobj_t *)obj;
  937. if (m->myalloc_ && m->buf_) {
  938. jas_free(m->buf_);
  939. m->buf_ = 0;
  940. }
  941. jas_free(obj);
  942. return 0;
  943. }
  944. /******************************************************************************\
  945. * File stream object.
  946. \******************************************************************************/
  947. static int file_read(jas_stream_obj_t *obj, char *buf, int cnt)
  948. {
  949. jas_stream_fileobj_t *fileobj = JAS_CAST(jas_stream_fileobj_t *, obj);
  950. return read(fileobj->fd, buf, cnt);
  951. }
  952. static int file_write(jas_stream_obj_t *obj, char *buf, int cnt)
  953. {
  954. jas_stream_fileobj_t *fileobj = JAS_CAST(jas_stream_fileobj_t *, obj);
  955. return write(fileobj->fd, buf, cnt);
  956. }
  957. static long file_seek(jas_stream_obj_t *obj, long offset, int origin)
  958. {
  959. jas_stream_fileobj_t *fileobj = JAS_CAST(jas_stream_fileobj_t *, obj);
  960. return lseek(fileobj->fd, offset, origin);
  961. }
  962. static int file_close(jas_stream_obj_t *obj)
  963. {
  964. jas_stream_fileobj_t *fileobj = JAS_CAST(jas_stream_fileobj_t *, obj);
  965. int ret;
  966. ret = close(fileobj->fd);
  967. if (fileobj->flags & JAS_STREAM_FILEOBJ_DELONCLOSE) {
  968. unlink(fileobj->pathname);
  969. }
  970. jas_free(fileobj);
  971. return ret;
  972. }
  973. /******************************************************************************\
  974. * Stdio file stream object.
  975. \******************************************************************************/
  976. static int sfile_read(jas_stream_obj_t *obj, char *buf, int cnt)
  977. {
  978. FILE *fp;
  979. fp = JAS_CAST(FILE *, obj);
  980. return fread(buf, 1, cnt, fp);
  981. }
  982. static int sfile_write(jas_stream_obj_t *obj, char *buf, int cnt)
  983. {
  984. FILE *fp;
  985. fp = JAS_CAST(FILE *, obj);
  986. return fwrite(buf, 1, cnt, fp);
  987. }
  988. static long sfile_seek(jas_stream_obj_t *obj, long offset, int origin)
  989. {
  990. FILE *fp;
  991. fp = JAS_CAST(FILE *, obj);
  992. return fseek(fp, offset, origin);
  993. }
  994. static int sfile_close(jas_stream_obj_t *obj)
  995. {
  996. FILE *fp;
  997. fp = JAS_CAST(FILE *, obj);
  998. return fclose(fp);
  999. }