2 * << Haru Free PDF Library >> -- hpdf_streams.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
17 #ifndef _CRT_SECURE_NO_WARNINGS
18 #define _CRT_SECURE_NO_WARNINGS
25 #define HPDF_UNUSED(a) ((void)(a))
28 #include "hpdf_conf.h"
29 #include "hpdf_consts.h"
30 #include "hpdf_utils.h"
31 #include "hpdf_streams.h"
33 #ifndef LIBHPDF_HAVE_NOZLIB
36 #endif /* LIBHPDF_HAVE_NOZLIB */
39 HPDF_MemStream_WriteFunc (HPDF_Stream stream,
45 HPDF_MemStream_SeekFunc (HPDF_Stream stream,
47 HPDF_WhenceMode mode);
51 HPDF_MemStream_ReadFunc (HPDF_Stream stream,
57 HPDF_MemStream_TellFunc (HPDF_Stream stream);
61 HPDF_MemStream_SizeFunc (HPDF_Stream stream);
65 HPDF_MemStream_FreeFunc (HPDF_Stream stream);
68 HPDF_MemStream_InWrite (HPDF_Stream stream,
69 const HPDF_BYTE **ptr,
73 HPDF_Stream_WriteToStreamWithDeflate (HPDF_Stream src,
79 HPDF_FileReader_ReadFunc (HPDF_Stream stream,
85 HPDF_FileReader_SeekFunc (HPDF_Stream stream,
87 HPDF_WhenceMode mode);
91 HPDF_FileStream_TellFunc (HPDF_Stream stream);
95 HPDF_FileStream_SizeFunc (HPDF_Stream stream);
99 HPDF_FileWriter_WriteFunc (HPDF_Stream stream,
100 const HPDF_BYTE *ptr,
104 HPDF_FileStream_FreeFunc (HPDF_Stream stream);
111 * stream : Pointer to a HPDF_Stream object.
112 * ptr : Pointer to a buffer to copy read data.
113 * size : Pointer to a variable which indecates buffer size.
115 * HPDF_Stream_read returns HPDF_OK when success. On failer, it returns
116 * error-code returned by reading function of this stream.
121 HPDF_Stream_Read (HPDF_Stream stream,
125 if (!(stream->read_fn))
126 return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
129 if (HPDF_Error_GetCode(stream->error) != HPDF_NOERROR)
130 return HPDF_THIS_FUNC_WAS_SKIPPED;
133 return stream->read_fn(stream, ptr, size);
140 * stream : Pointer to a HPDF_Stream object.
141 * s : Pointer to a buffer to copy read data.
142 * size : buffer-size of s.
144 * Read from stream until the buffer is exhausted or line-feed charactor is
149 HPDF_Stream_ReadLn (HPDF_Stream stream,
153 char buf[HPDF_STREAM_BUF_SIZ];
154 HPDF_UINT r_size = *size;
155 HPDF_UINT read_size = HPDF_STREAM_BUF_SIZ;
157 HPDF_PTRACE((" HPDF_Stream_ReadLn\n"));
160 return HPDF_INVALID_PARAMETER;
162 if (!s || *size == 0)
163 return HPDF_SetError (stream->error, HPDF_INVALID_PARAMETER, 0);
165 if (!(stream->seek_fn) || !(stream->read_fn))
166 return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
168 if (r_size < HPDF_STREAM_BUF_SIZ)
175 HPDF_STATUS ret = HPDF_Stream_Read (stream, (HPDF_BYTE *)buf, &read_size);
177 if (ret != HPDF_OK && read_size == 0)
182 while (read_size > 0) {
183 if (*pbuf == 0x0A || *pbuf == 0x0D) {
187 /* handling CR-LF marker */
188 if (*pbuf == 0x0D || read_size > 1) {
196 return HPDF_Stream_Seek (stream, 0 - read_size,
207 if (r_size < HPDF_STREAM_BUF_SIZ)
210 read_size = HPDF_STREAM_BUF_SIZ;
212 if (ret == HPDF_STREAM_EOF)
213 return HPDF_STREAM_EOF;
218 return HPDF_STREAM_READLN_CONTINUE;
225 * stream : Pointer to a HPDF_Stream object.
226 * ptr : Pointer to a buffer to write.
227 * siz : The size of buffer to write.
229 * HPDF_Stream_Write returns HPDF_OK when success. On failer, it returns
230 * error-code returned by writing function of this stream.
234 HPDF_Stream_Write (HPDF_Stream stream,
235 const HPDF_BYTE *ptr,
240 HPDF_PTRACE((" HPDF_Stream_Write\n"));
242 if (!(stream->write_fn))
243 return HPDF_SetError(stream->error, HPDF_INVALID_OPERATION, 0);
246 if (HPDF_Error_GetCode(stream->error) != HPDF_NOERROR)
247 return HPDF_THIS_FUNC_WAS_SKIPPED;
250 ret = stream->write_fn(stream, ptr, size);
255 stream->size += size;
262 HPDF_Stream_WriteChar (HPDF_Stream stream,
265 return HPDF_Stream_Write(stream, (HPDF_BYTE *)&value, sizeof(char));
270 HPDF_Stream_WriteStr (HPDF_Stream stream,
273 HPDF_UINT len = HPDF_StrLen(value, -1);
275 return HPDF_Stream_Write(stream, (HPDF_BYTE *)value, len);
279 HPDF_Stream_WriteUChar (HPDF_Stream stream,
282 return HPDF_Stream_Write(stream, &value, sizeof(HPDF_BYTE));
286 HPDF_Stream_WriteInt (HPDF_Stream stream,
289 char buf[HPDF_INT_LEN + 1];
291 char* p = HPDF_IToA(buf, value, buf + HPDF_INT_LEN);
293 return HPDF_Stream_Write(stream, (HPDF_BYTE *)buf, (HPDF_UINT)(p - buf));
297 HPDF_Stream_WriteUInt (HPDF_Stream stream,
300 return HPDF_Stream_WriteInt(stream, (HPDF_INT)value);
304 HPDF_Stream_WriteReal (HPDF_Stream stream,
307 char buf[HPDF_REAL_LEN + 1];
309 char* p = HPDF_FToA(buf, value, buf + HPDF_REAL_LEN);
311 return HPDF_Stream_Write(stream, (HPDF_BYTE *)buf, (HPDF_UINT)(p - buf));
315 HPDF_Stream_Free (HPDF_Stream stream)
321 stream->free_fn(stream);
323 stream->sig_bytes = 0;
325 HPDF_FreeMem(stream->mmgr, stream);
329 HPDF_Stream_Seek (HPDF_Stream stream,
331 HPDF_WhenceMode mode)
333 HPDF_PTRACE((" HPDF_Stream_Seek\n"));
335 if (!(stream->seek_fn))
336 return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
338 if (HPDF_Error_GetCode(stream->error) != 0)
339 return HPDF_THIS_FUNC_WAS_SKIPPED;
341 return stream->seek_fn(stream, pos, mode);
346 HPDF_Stream_Tell (HPDF_Stream stream)
348 HPDF_PTRACE((" HPDF_Stream_Tell\n"));
350 if (!(stream->tell_fn))
351 return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
353 if (HPDF_Error_GetCode(stream->error) != 0)
354 return HPDF_THIS_FUNC_WAS_SKIPPED;
356 return stream->tell_fn(stream);
361 HPDF_Stream_Size (HPDF_Stream stream)
363 HPDF_PTRACE((" HPDF_Stream_Tell\n"));
365 if (stream->write_fn)
368 if (!(stream->size_fn)) {
369 HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
373 if (HPDF_Error_GetCode(stream->error) != 0)
376 return stream->size_fn(stream);
381 HPDF_Stream_WriteEscapeName (HPDF_Stream stream,
384 char tmp_char[HPDF_LIMIT_MAX_NAME_LEN * 3 + 2];
387 const HPDF_BYTE* pos1;
390 HPDF_PTRACE((" HPDF_Stream_WriteEscapeName\n"));
392 len = HPDF_StrLen (value, HPDF_LIMIT_MAX_NAME_LEN);
393 pos1 = (HPDF_BYTE*)value;
397 for (i = 0; i < (HPDF_INT32)len; i++) {
398 HPDF_BYTE c = *pos1++;
399 if (HPDF_NEEDS_ESCAPE(c)) {
401 *pos2 = (char)(c >> 4);
408 *pos2 = (char)(c & 0x0f);
419 return HPDF_Stream_Write (stream, (HPDF_BYTE *)tmp_char, HPDF_StrLen(tmp_char, -1));
423 HPDF_Stream_WriteEscapeText2 (HPDF_Stream stream,
427 char buf[HPDF_TEXT_DEFAULT_LEN];
430 const char* p = text;
433 HPDF_PTRACE((" HPDF_Stream_WriteEscapeText2\n"));
435 /* The following block is commented out because it violates "PDF Spec 7.3.4.2 Literal Strings".
436 * It states that the two matching parentheses must still be present to represent an empty
437 * string of zero length.
446 for (i = 0; i < (HPDF_INT)len; i++) {
447 HPDF_BYTE c = (HPDF_BYTE)*p++;
448 if (HPDF_NEEDS_ESCAPE(c)) {
451 buf[idx] = (char)(c >> 6);
454 buf[idx] = (char)((c & 0x38) >> 3);
457 buf[idx] = (char)(c & 0x07);
464 if (idx > HPDF_TEXT_DEFAULT_LEN - 4) {
465 ret = HPDF_Stream_Write (stream, (HPDF_BYTE *)buf, idx);
473 ret = HPDF_Stream_Write (stream, (HPDF_BYTE *)buf, idx);
479 HPDF_Stream_WriteEscapeText (HPDF_Stream stream,
484 HPDF_PTRACE((" HPDF_Stream_WriteEscapeText\n"));
486 len = (text == NULL) ? 0 : HPDF_StrLen (text, HPDF_LIMIT_MAX_STRING_LEN);
488 return HPDF_Stream_WriteEscapeText2(stream, text, len);
492 HPDF_Stream_WriteBinary (HPDF_Stream stream,
493 const HPDF_BYTE *data,
497 char buf[HPDF_TEXT_DEFAULT_LEN];
498 HPDF_BYTE ebuf[HPDF_TEXT_DEFAULT_LEN];
499 HPDF_BYTE *pbuf = NULL;
500 HPDF_BOOL flg = HPDF_FALSE;
504 HPDF_STATUS ret = HPDF_OK;
506 HPDF_PTRACE((" HPDF_Stream_WriteBinary\n"));
509 if (len <= HPDF_TEXT_DEFAULT_LEN)
512 pbuf = (HPDF_BYTE *)HPDF_GetMem (stream->mmgr, len);
516 HPDF_Encrypt_CryptBuf (e, data, pbuf, len);
522 for (i = 0; i < len; i++, p++) {
523 char c = (char)(*p >> 4);
531 c = (char)(*p & 0x0f);
538 if (idx > HPDF_TEXT_DEFAULT_LEN - 2) {
539 ret = HPDF_Stream_Write (stream, (HPDF_BYTE *)buf, idx);
540 if (ret != HPDF_OK) {
542 HPDF_FreeMem (stream->mmgr, pbuf);
550 ret = HPDF_Stream_Write (stream, (HPDF_BYTE *)buf, idx);
554 HPDF_FreeMem (stream->mmgr, pbuf);
561 HPDF_Stream_WriteToStreamWithDeflate (HPDF_Stream src,
565 #ifndef LIBHPDF_HAVE_NOZLIB
567 #define DEFLATE_BUF_SIZ ((HPDF_INT)(HPDF_STREAM_BUF_SIZ * 1.1) + 13)
573 Bytef inbuf[HPDF_STREAM_BUF_SIZ];
574 Bytef otbuf[DEFLATE_BUF_SIZ];
575 HPDF_BYTE ebuf[DEFLATE_BUF_SIZ];
577 HPDF_PTRACE((" HPDF_Stream_WriteToStreamWithDeflate\n"));
579 /* initialize input stream */
580 ret = HPDF_Stream_Seek (src, 0, HPDF_SEEK_SET);
584 /* initialize decompression stream. */
585 HPDF_MemSet(&strm, 0x00, sizeof(z_stream));
586 strm.next_out = otbuf;
587 strm.avail_out = DEFLATE_BUF_SIZ;
589 ret = deflateInit_(&strm, Z_DEFAULT_COMPRESSION, ZLIB_VERSION,
592 return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
594 strm.next_in = inbuf;
599 HPDF_UINT size = HPDF_STREAM_BUF_SIZ;
601 ret = HPDF_Stream_Read (src, inbuf, &size);
603 strm.next_in = inbuf;
604 strm.avail_in = size;
606 if (ret != HPDF_OK) {
607 if (ret == HPDF_STREAM_EOF) {
617 while (strm.avail_in > 0) {
618 ret = deflate(&strm, Z_NO_FLUSH);
619 if (ret != Z_OK && ret != Z_STREAM_END) {
621 return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
624 if (strm.avail_out == 0) {
626 HPDF_Encrypt_CryptBuf (e, otbuf, ebuf, DEFLATE_BUF_SIZ);
627 ret = HPDF_Stream_Write(dst, ebuf, DEFLATE_BUF_SIZ);
629 ret = HPDF_Stream_Write (dst, otbuf, DEFLATE_BUF_SIZ);
631 if (ret != HPDF_OK) {
633 return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
636 strm.next_out = otbuf;
637 strm.avail_out = DEFLATE_BUF_SIZ;
647 ret = deflate(&strm, Z_FINISH);
648 if (ret != Z_OK && ret != Z_STREAM_END) {
650 return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
653 if (ret == Z_STREAM_END)
656 if (strm.avail_out < DEFLATE_BUF_SIZ) {
657 HPDF_UINT osize = DEFLATE_BUF_SIZ - strm.avail_out;
659 HPDF_Encrypt_CryptBuf (e, otbuf, ebuf, osize);
660 ret = HPDF_Stream_Write(dst, ebuf, osize);
662 ret = HPDF_Stream_Write (dst, otbuf, osize);
664 if (ret != HPDF_OK) {
666 return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
669 strm.next_out = otbuf;
670 strm.avail_out = DEFLATE_BUF_SIZ;
679 #else /* LIBHPDF_HAVE_NOZLIB */
683 return HPDF_UNSUPPORTED_FUNC;
684 #endif /* LIBHPDF_HAVE_NOZLIB */
688 HPDF_Stream_WriteToStream (HPDF_Stream src,
694 HPDF_BYTE buf[HPDF_STREAM_BUF_SIZ];
695 HPDF_BYTE ebuf[HPDF_STREAM_BUF_SIZ];
698 HPDF_PTRACE((" HPDF_Stream_WriteToStream\n"));
699 HPDF_UNUSED (filter);
701 if (!dst || !(dst->write_fn)) {
702 HPDF_SetError (src->error, HPDF_INVALID_OBJECT, 0);
703 return HPDF_INVALID_OBJECT;
706 if (HPDF_Error_GetCode (src->error) != HPDF_NOERROR ||
707 HPDF_Error_GetCode (dst->error) != HPDF_NOERROR)
708 return HPDF_THIS_FUNC_WAS_SKIPPED;
710 /* initialize input stream */
711 if (HPDF_Stream_Size (src) == 0)
714 #ifndef LIBHPDF_HAVE_NOZLIB
715 if (filter & HPDF_STREAM_FILTER_FLATE_DECODE)
716 return HPDF_Stream_WriteToStreamWithDeflate (src, dst, e);
717 #endif /* LIBHPDF_HAVE_NOZLIB */
719 ret = HPDF_Stream_Seek (src, 0, HPDF_SEEK_SET);
725 HPDF_UINT size = HPDF_STREAM_BUF_SIZ;
727 ret = HPDF_Stream_Read (src, buf, &size);
729 if (ret != HPDF_OK) {
730 if (ret == HPDF_STREAM_EOF) {
740 HPDF_Encrypt_CryptBuf (e, buf, ebuf, size);
741 ret = HPDF_Stream_Write(dst, ebuf, size);
743 ret = HPDF_Stream_Write(dst, buf, size);
757 HPDF_FileReader_New (HPDF_MMgr mmgr,
761 HPDF_FILEP fp = HPDF_FOPEN (fname, "rb");
763 HPDF_PTRACE((" HPDF_FileReader_New\n"));
767 HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, GetLastError());
769 HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, errno);
774 stream = (HPDF_Stream)HPDF_GetMem(mmgr, sizeof(HPDF_Stream_Rec));
777 HPDF_MemSet(stream, 0, sizeof(HPDF_Stream_Rec));
778 stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
779 stream->type = HPDF_STREAM_FILE;
780 stream->error = mmgr->error;
782 stream->read_fn = HPDF_FileReader_ReadFunc;
783 stream->seek_fn = HPDF_FileReader_SeekFunc;
784 stream->tell_fn = HPDF_FileStream_TellFunc;
785 stream->size_fn = HPDF_FileStream_SizeFunc;
786 stream->free_fn = HPDF_FileStream_FreeFunc;
794 * HPDF_FileReader_ReadFunc
796 * Reading data function for HPDF_FileReader.
801 HPDF_FileReader_ReadFunc (HPDF_Stream stream,
805 HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
808 HPDF_PTRACE((" HPDF_FileReader_ReadFunc\n"));
810 HPDF_MemSet(ptr, 0, *siz);
811 rsiz = HPDF_FREAD(ptr, 1, *siz, fp);
818 return HPDF_STREAM_EOF;
821 return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
828 * HPDF_FileReader_SeekFunc
830 * Seeking data function for HPDF_FileReader.
832 * stream : Pointer to a HPDF_Stream object.
833 * pos : New position of stream object.
834 * HPDF_whence_mode : Seeking mode describing below.
835 * HPDF_SEEK_SET : Absolute file position
836 * HPDF_SEEK_CUR : Relative to the current file position
837 * HPDF_SEEK_END : Relative to the current end of file.
839 * HPDF_FileReader_seek_fn returns HPDF_OK when successful. On failer
840 * the result is HPDF_FILE_IO_ERROR and HPDF_Error_GetCode2() returns the
841 * error which returned by file seeking function of platform.
846 HPDF_FileReader_SeekFunc (HPDF_Stream stream,
848 HPDF_WhenceMode mode)
850 HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
853 HPDF_PTRACE((" HPDF_FileReader_SeekFunc\n"));
866 if (HPDF_FSEEK (fp, pos, whence) != 0) {
867 return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
875 HPDF_FileStream_TellFunc (HPDF_Stream stream)
878 HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
880 HPDF_PTRACE((" HPDF_FileReader_TellFunc\n"));
882 if ((ret = HPDF_FTELL (fp)) < 0) {
883 return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
892 HPDF_FileStream_SizeFunc (HPDF_Stream stream)
896 HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
898 HPDF_PTRACE((" HPDF_FileReader_SizeFunc\n"));
900 /* save current file-pointer */
901 if ((ptr = HPDF_FTELL (fp)) < 0) {
902 HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
907 /* move file-pointer to the end of the file */
908 if (HPDF_FSEEK (fp, 0, SEEK_END) < 0) {
909 HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
914 /* get the pointer of the end of the file */
915 if ((size = HPDF_FTELL (fp)) < 0) {
916 HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
921 /* restore current file-pointer */
922 if (HPDF_FSEEK (fp, ptr, SEEK_SET) < 0) {
923 HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
928 return (HPDF_UINT32)size;
933 HPDF_FileWriter_New (HPDF_MMgr mmgr,
937 HPDF_FILEP fp = HPDF_FOPEN (fname, "wb");
939 HPDF_PTRACE((" HPDF_FileWriter_New\n"));
943 HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, GetLastError());
945 HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, errno);
950 stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
953 HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
954 stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
955 stream->error = mmgr->error;
957 stream->write_fn = HPDF_FileWriter_WriteFunc;
958 stream->free_fn = HPDF_FileStream_FreeFunc;
959 stream->tell_fn = HPDF_FileStream_TellFunc;
961 stream->type = HPDF_STREAM_FILE;
968 HPDF_FileWriter_WriteFunc (HPDF_Stream stream,
969 const HPDF_BYTE *ptr,
975 HPDF_PTRACE((" HPDF_FileWriter_WriteFunc\n"));
977 fp = (HPDF_FILEP)stream->attr;
978 ret = HPDF_FWRITE (ptr, 1, siz, fp);
981 return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
989 HPDF_FileStream_FreeFunc (HPDF_Stream stream)
993 HPDF_PTRACE((" HPDF_FileStream_FreeFunc\n"));
995 fp = (HPDF_FILEP)stream->attr;
1000 stream->attr = NULL;
1004 HPDF_MemStream_InWrite (HPDF_Stream stream,
1005 const HPDF_BYTE **ptr,
1008 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1009 HPDF_UINT rsize = attr->buf_siz - attr->w_pos;
1011 HPDF_PTRACE((" HPDF_MemStream_InWrite\n"));
1016 if (rsize >= *count) {
1017 HPDF_MemCpy (attr->w_ptr, *ptr, *count);
1018 attr->w_ptr += *count;
1019 attr->w_pos += *count;
1023 HPDF_MemCpy (attr->w_ptr, *ptr, rsize);
1027 attr->w_ptr = (HPDF_BYTE*)HPDF_GetMem (stream->mmgr, attr->buf_siz);
1029 if (attr->w_ptr == NULL)
1030 return HPDF_Error_GetCode (stream->error);
1032 if (HPDF_List_Add (attr->buf, attr->w_ptr) != HPDF_OK) {
1033 HPDF_FreeMem (stream->mmgr, attr->w_ptr);
1036 return HPDF_Error_GetCode (stream->error);
1045 HPDF_MemStream_WriteFunc (HPDF_Stream stream,
1046 const HPDF_BYTE *ptr,
1049 HPDF_UINT wsiz = siz;
1051 HPDF_PTRACE((" HPDF_MemStream_WriteFunc\n"));
1053 if (HPDF_Error_GetCode (stream->error) != 0)
1054 return HPDF_THIS_FUNC_WAS_SKIPPED;
1057 HPDF_STATUS ret = HPDF_MemStream_InWrite (stream, &ptr, &wsiz);
1067 HPDF_MemStream_TellFunc (HPDF_Stream stream)
1070 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1072 HPDF_PTRACE((" HPDF_MemStream_TellFunc\n"));
1074 ret = attr->r_ptr_idx * attr->buf_siz;
1082 HPDF_MemStream_SizeFunc (HPDF_Stream stream)
1084 HPDF_PTRACE((" HPDF_MemStream_SizeFunc\n"));
1086 return stream->size;
1090 HPDF_MemStream_SeekFunc (HPDF_Stream stream,
1092 HPDF_WhenceMode mode)
1094 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1096 HPDF_PTRACE((" HPDF_MemStream_SeekFunc\n"));
1098 if (mode == HPDF_SEEK_CUR) {
1099 pos += (attr->r_ptr_idx * attr->buf_siz);
1101 } else if (mode == HPDF_SEEK_END)
1102 pos = stream->size - pos;
1104 if (pos > (HPDF_INT)stream->size) {
1105 return HPDF_SetError (stream->error, HPDF_STREAM_EOF, 0);
1108 if (stream->size == 0) {
1112 attr->r_ptr_idx = pos / attr->buf_siz;
1113 attr->r_pos = pos % attr->buf_siz;
1114 attr->r_ptr = (HPDF_BYTE*)HPDF_List_ItemAt (attr->buf, attr->r_ptr_idx);
1115 if (attr->r_ptr == NULL) {
1116 HPDF_SetError (stream->error, HPDF_INVALID_OBJECT, 0);
1117 return HPDF_INVALID_OBJECT;
1119 attr->r_ptr += attr->r_pos;
1126 HPDF_MemStream_GetBufPtr (HPDF_Stream stream,
1131 HPDF_MemStreamAttr attr;
1133 HPDF_PTRACE((" HPDF_MemStream_GetBufPtr\n"));
1135 if (stream->type != HPDF_STREAM_MEMORY) {
1136 HPDF_SetError (stream->error, HPDF_INVALID_OBJECT, 0);
1140 attr = (HPDF_MemStreamAttr)stream->attr;
1142 ret = (HPDF_BYTE *)HPDF_List_ItemAt (attr->buf, index);
1144 HPDF_SetError (stream->error, HPDF_INVALID_PARAMETER, 0);
1149 *length = (attr->buf->count - 1 == index) ? attr->w_pos : attr->buf_siz;
1155 HPDF_MemStream_FreeData (HPDF_Stream stream)
1157 HPDF_MemStreamAttr attr;
1160 HPDF_PTRACE((" HPDF_MemStream_FreeData\n"));
1162 if (!stream || stream->type != HPDF_STREAM_MEMORY)
1165 attr = (HPDF_MemStreamAttr)stream->attr;
1167 for (i = 0; i < attr->buf->count; i++)
1168 HPDF_FreeMem (stream->mmgr, HPDF_List_ItemAt (attr->buf, i));
1170 HPDF_List_Clear(attr->buf);
1173 attr->w_pos = attr->buf_siz;
1175 attr->r_ptr_idx = 0;
1180 HPDF_MemStream_FreeFunc (HPDF_Stream stream)
1182 HPDF_MemStreamAttr attr;
1184 HPDF_PTRACE((" HPDF_MemStream_FreeFunc\n"));
1186 attr = (HPDF_MemStreamAttr)stream->attr;
1187 HPDF_MemStream_FreeData (stream);
1188 HPDF_List_Free (attr->buf);
1189 HPDF_FreeMem (stream->mmgr, attr);
1190 stream->attr = NULL;
1194 HPDF_MemStream_New (HPDF_MMgr mmgr,
1199 HPDF_PTRACE((" HPDF_MemStream_New\n"));
1201 /* Create new HPDF_Stream object. */
1202 stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1205 /* Create attribute struct. */
1206 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)HPDF_GetMem (mmgr,
1207 sizeof(HPDF_MemStreamAttr_Rec));
1210 HPDF_FreeMem (mmgr, stream);
1214 HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1215 HPDF_MemSet (attr, 0, sizeof(HPDF_MemStreamAttr_Rec));
1217 attr->buf = HPDF_List_New (mmgr, HPDF_DEF_ITEMS_PER_BLOCK);
1219 HPDF_FreeMem (mmgr, stream);
1220 HPDF_FreeMem (mmgr, attr);
1224 stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1225 stream->type = HPDF_STREAM_MEMORY;
1226 stream->error = mmgr->error;
1227 stream->mmgr = mmgr;
1228 stream->attr = attr;
1229 attr->buf_siz = (buf_siz > 0) ? buf_siz : HPDF_STREAM_BUF_SIZ;
1230 attr->w_pos = attr->buf_siz;
1232 stream->write_fn = HPDF_MemStream_WriteFunc;
1233 stream->read_fn = HPDF_MemStream_ReadFunc;
1234 stream->seek_fn = HPDF_MemStream_SeekFunc;
1235 stream->tell_fn = HPDF_MemStream_TellFunc;
1236 stream->size_fn = HPDF_MemStream_SizeFunc;
1237 stream->free_fn = HPDF_MemStream_FreeFunc;
1244 HPDF_MemStream_GetBufSize (HPDF_Stream stream)
1246 HPDF_MemStreamAttr attr;
1248 HPDF_PTRACE((" HPDF_MemStream_GetBufSize\n"));
1250 if (!stream || stream->type != HPDF_STREAM_MEMORY)
1253 attr = (HPDF_MemStreamAttr)stream->attr;
1254 return attr->buf_siz;
1258 HPDF_MemStream_GetBufCount (HPDF_Stream stream)
1260 HPDF_MemStreamAttr attr;
1262 HPDF_PTRACE((" HPDF_MemStream_GetBufCount\n"));
1264 if (!stream || stream->type != HPDF_STREAM_MEMORY)
1267 attr = (HPDF_MemStreamAttr)stream->attr;
1268 return attr->buf->count;
1272 HPDF_MemStream_ReadFunc (HPDF_Stream stream,
1276 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1278 HPDF_UINT rlen = *size;
1280 HPDF_PTRACE((" HPDF_MemStream_ReadFunc\n"));
1287 if (attr->buf->count == 0)
1288 return HPDF_STREAM_EOF;
1290 if (attr->buf->count - 1 > attr->r_ptr_idx)
1291 tmp_len = attr->buf_siz - attr->r_pos;
1292 else if (attr->buf->count - 1 == attr->r_ptr_idx)
1293 tmp_len = attr->w_pos - attr->r_pos;
1295 return HPDF_STREAM_EOF;
1298 attr->r_ptr = (HPDF_BYTE*)HPDF_List_ItemAt (attr->buf,
1301 if (tmp_len >= rlen) {
1302 HPDF_MemCpy (buf, attr->r_ptr, rlen);
1303 attr->r_pos += rlen;
1305 attr->r_ptr += rlen;
1308 buf = HPDF_MemCpy (buf, attr->r_ptr, tmp_len);
1312 if (attr->r_ptr_idx == attr->buf->count - 1) {
1313 attr->r_ptr += tmp_len;
1314 attr->r_pos += tmp_len;
1315 return HPDF_STREAM_EOF;
1320 attr->r_ptr = HPDF_MemStream_GetBufPtr (stream, attr->r_ptr_idx,
1330 HPDF_MemStream_Rewrite (HPDF_Stream stream,
1334 HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1336 HPDF_UINT rlen = size;
1338 HPDF_PTRACE((" HPDF_MemStream_Rewrite\n"));
1343 if (attr->buf->count <= attr->r_ptr_idx) {
1344 HPDF_STATUS ret = HPDF_MemStream_WriteFunc (stream, buf, rlen);
1345 attr->r_ptr_idx = attr->buf->count;
1346 attr->r_pos = attr->w_pos;
1347 attr->r_ptr = attr->w_ptr;
1349 } else if (attr->buf->count == attr->r_ptr_idx)
1350 tmp_len = attr->w_pos - attr->r_pos;
1352 tmp_len = attr->buf_siz - attr->r_pos;
1354 if (tmp_len >= rlen) {
1355 HPDF_MemCpy(attr->r_ptr, buf, rlen);
1356 attr->r_pos += rlen;
1357 attr->r_ptr += rlen;
1360 HPDF_MemCpy(attr->r_ptr, buf, tmp_len);
1365 if (attr->buf->count > attr->r_ptr_idx) {
1367 attr->r_ptr = HPDF_MemStream_GetBufPtr (stream, attr->r_ptr_idx,
1376 * HPDF_CallbackReader_new
1378 * Constractor for HPDF_CallbackReader.
1380 * mmgr : Pointer to a HPDF_MMgr object.
1381 * read_fn : Pointer to a user function for reading data.
1382 * seek_fn : Pointer to a user function for seeking data.
1383 * data : Pointer to a data which defined by user.
1385 * return: If success, It returns pointer to new HPDF_Stream object,
1386 * otherwise, it returns NULL.
1392 HPDF_CallbackReader_New (HPDF_MMgr mmgr,
1393 HPDF_Stream_Read_Func read_fn,
1394 HPDF_Stream_Seek_Func seek_fn,
1395 HPDF_Stream_Tell_Func tell_fn,
1396 HPDF_Stream_Size_Func size_fn,
1401 HPDF_PTRACE((" HPDF_CallbackReader_New\n"));
1403 stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1406 HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1407 stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1408 stream->error = mmgr->error;
1409 stream->mmgr = mmgr;
1410 stream->read_fn = read_fn;
1411 stream->seek_fn = seek_fn;
1412 stream->tell_fn = tell_fn;
1413 stream->size_fn = size_fn;
1414 stream->attr = data;
1415 stream->type = HPDF_STREAM_CALLBACK;
1422 * HPDF_CallbackWriter_new
1424 * Constractor for HPDF_CallbackWriter.
1426 * mmgr : Pointer to a HPDF_MMgr object.
1427 * read_fn : Pointer to a user function for writing data.
1428 * data : Pointer to a data which defined by user.
1430 * return: If success, It returns pointer to new HPDF_Stream object,
1431 * otherwise, it returns NULL.
1437 HPDF_CallbackWriter_New (HPDF_MMgr mmgr,
1438 HPDF_Stream_Write_Func write_fn,
1443 HPDF_PTRACE((" HPDF_CallbackWriter_New\n"));
1445 stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1448 HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1449 stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1450 stream->error = mmgr->error;
1451 stream->mmgr = mmgr;
1452 stream->write_fn = write_fn;
1453 stream->attr = data;
1454 stream->type = HPDF_STREAM_CALLBACK;
1463 HPDF_Stream_Validate (HPDF_Stream stream)
1465 if (!stream || stream->sig_bytes != HPDF_STREAM_SIG_BYTES)