1 /*
    2  * << Haru Free PDF Library >> -- hpdf_streams.h
    3  *
    4  * URL: http://libharu.org
    5  *
    6  * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
    7  * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
    8  *
    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.
   15  *
   16  * 2005.12.20 Created.
   17  *
   18  */
   19 
   20 #ifndef _HPDF_STREAMS_H
   21 #define _HPDF_STREAMS_H
   22 
   23 #include "hpdf_list.h"
   24 #include "hpdf_encrypt.h"
   25 
   26 #ifdef __cplusplus
   27 extern "C" {
   28 #endif
   29 
   30 
   31 #define HPDF_STREAM_SIG_BYTES  0x5354524DL
   32 
   33 typedef enum _HPDF_StreamType {
   34     HPDF_STREAM_UNKNOWN = 0,
   35     HPDF_STREAM_CALLBACK,
   36     HPDF_STREAM_FILE,
   37     HPDF_STREAM_MEMORY
   38 } HPDF_StreamType;
   39 
   40 #define HPDF_STREAM_FILTER_NONE          0x0000
   41 #define HPDF_STREAM_FILTER_ASCIIHEX      0x0100
   42 #define HPDF_STREAM_FILTER_ASCII85       0x0200
   43 #define HPDF_STREAM_FILTER_FLATE_DECODE  0x0400
   44 #define HPDF_STREAM_FILTER_DCT_DECODE    0x0800
   45 #define HPDF_STREAM_FILTER_CCITT_DECODE  0x1000
   46 
   47 typedef enum _HPDF_WhenceMode {
   48     HPDF_SEEK_SET = 0,
   49     HPDF_SEEK_CUR,
   50     HPDF_SEEK_END
   51 } HPDF_WhenceMode;
   52 
   53 typedef struct _HPDF_Stream_Rec  *HPDF_Stream;
   54 
   55 typedef HPDF_STATUS
   56 (*HPDF_Stream_Write_Func)  (HPDF_Stream      stream,
   57                             const HPDF_BYTE  *ptr,
   58                             HPDF_UINT        siz);
   59 
   60 
   61 typedef HPDF_STATUS
   62 (*HPDF_Stream_Read_Func)  (HPDF_Stream  stream,
   63                            HPDF_BYTE    *ptr,
   64                            HPDF_UINT    *siz);
   65 
   66 
   67 typedef HPDF_STATUS
   68 (*HPDF_Stream_Seek_Func)  (HPDF_Stream      stream,
   69                            HPDF_INT         pos,
   70                            HPDF_WhenceMode  mode);
   71 
   72 
   73 typedef HPDF_INT32
   74 (*HPDF_Stream_Tell_Func)  (HPDF_Stream      stream);
   75 
   76 
   77 typedef void
   78 (*HPDF_Stream_Free_Func)  (HPDF_Stream  stream);
   79 
   80 
   81 typedef HPDF_UINT32
   82 (*HPDF_Stream_Size_Func)  (HPDF_Stream  stream);
   83 
   84 
   85 typedef struct _HPDF_MemStreamAttr_Rec  *HPDF_MemStreamAttr;
   86 
   87 
   88 typedef struct _HPDF_MemStreamAttr_Rec {
   89     HPDF_List  buf;
   90     HPDF_UINT  buf_siz;
   91     HPDF_UINT  w_pos;
   92     HPDF_BYTE  *w_ptr;
   93     HPDF_UINT  r_ptr_idx;
   94     HPDF_UINT  r_pos;
   95     HPDF_BYTE  *r_ptr;
   96 } HPDF_MemStreamAttr_Rec;
   97 
   98 
   99 typedef struct _HPDF_Stream_Rec {
  100     HPDF_UINT32               sig_bytes;
  101     HPDF_StreamType           type;
  102     HPDF_MMgr                 mmgr;
  103     HPDF_Error                error;
  104     HPDF_UINT                 size;
  105     HPDF_Stream_Write_Func    write_fn;
  106     HPDF_Stream_Read_Func     read_fn;
  107     HPDF_Stream_Seek_Func     seek_fn;
  108     HPDF_Stream_Free_Func     free_fn;
  109     HPDF_Stream_Tell_Func     tell_fn;
  110     HPDF_Stream_Size_Func     size_fn;
  111     void*                     attr;
  112 } HPDF_Stream_Rec;
  113 
  114 
  115 
  116 HPDF_Stream
  117 HPDF_MemStream_New  (HPDF_MMgr  mmgr,
  118                      HPDF_UINT  buf_siz);
  119 
  120 
  121 HPDF_BYTE*
  122 HPDF_MemStream_GetBufPtr  (HPDF_Stream  stream,
  123                            HPDF_UINT    index,
  124                            HPDF_UINT    *length);
  125 
  126 
  127 HPDF_UINT
  128 HPDF_MemStream_GetBufSize  (HPDF_Stream  stream);
  129 
  130 
  131 HPDF_UINT
  132 HPDF_MemStream_GetBufCount  (HPDF_Stream  stream);
  133 
  134 
  135 HPDF_STATUS
  136 HPDF_MemStream_Rewrite  (HPDF_Stream  stream,
  137                          HPDF_BYTE    *buf,
  138                          HPDF_UINT    size);
  139 
  140 
  141 void
  142 HPDF_MemStream_FreeData  (HPDF_Stream  stream);
  143 
  144 
  145 HPDF_STATUS
  146 HPDF_Stream_WriteToStream  (HPDF_Stream   src,
  147                             HPDF_Stream   dst,
  148                             HPDF_UINT     filter,
  149                             HPDF_Encrypt  e);
  150 
  151 
  152 HPDF_Stream
  153 HPDF_FileReader_New  (HPDF_MMgr   mmgr,
  154                       const char  *fname);
  155 
  156 
  157 HPDF_Stream
  158 HPDF_FileWriter_New  (HPDF_MMgr        mmgr,
  159                       const char  *fname);
  160 
  161 
  162 HPDF_Stream
  163 HPDF_CallbackReader_New  (HPDF_MMgr              mmgr,
  164                           HPDF_Stream_Read_Func  read_fn,
  165                           HPDF_Stream_Seek_Func  seek_fn,
  166                           HPDF_Stream_Tell_Func  tell_fn,
  167                           HPDF_Stream_Size_Func  size_fn,
  168                           void*                  data);
  169 
  170 
  171 HPDF_Stream
  172 HPDF_CallbackWriter_New (HPDF_MMgr               mmgr,
  173                          HPDF_Stream_Write_Func  write_fn,
  174                          void*                   data);
  175 
  176 
  177 void
  178 HPDF_Stream_Free  (HPDF_Stream  stream);
  179 
  180 
  181 HPDF_STATUS
  182 HPDF_Stream_WriteChar  (HPDF_Stream  stream,
  183                         char    value);
  184 
  185 
  186 HPDF_STATUS
  187 HPDF_Stream_WriteStr  (HPDF_Stream      stream,
  188                        const char  *value);
  189 
  190 
  191 HPDF_STATUS
  192 HPDF_Stream_WriteUChar  (HPDF_Stream  stream,
  193                          HPDF_BYTE    value);
  194 
  195 
  196 HPDF_STATUS
  197 HPDF_Stream_WriteInt  (HPDF_Stream  stream,
  198                        HPDF_INT     value);
  199 
  200 
  201 HPDF_STATUS
  202 HPDF_Stream_WriteUInt  (HPDF_Stream  stream,
  203                         HPDF_UINT    value);
  204 
  205 
  206 HPDF_STATUS
  207 HPDF_Stream_WriteReal  (HPDF_Stream  stream,
  208                         HPDF_REAL    value);
  209 
  210 
  211 HPDF_STATUS
  212 HPDF_Stream_Write  (HPDF_Stream      stream,
  213                     const HPDF_BYTE  *ptr,
  214                     HPDF_UINT        size);
  215 
  216 
  217 HPDF_STATUS
  218 HPDF_Stream_Read  (HPDF_Stream  stream,
  219                    HPDF_BYTE    *ptr,
  220                    HPDF_UINT    *size);
  221 
  222 HPDF_STATUS
  223 HPDF_Stream_ReadLn  (HPDF_Stream  stream,
  224                      char    *s,
  225                      HPDF_UINT    *size);
  226 
  227 
  228 HPDF_INT32
  229 HPDF_Stream_Tell  (HPDF_Stream  stream);
  230 
  231 
  232 HPDF_STATUS
  233 HPDF_Stream_Seek  (HPDF_Stream      stream,
  234                    HPDF_INT         pos,
  235                    HPDF_WhenceMode  mode);
  236 
  237 
  238 HPDF_BOOL
  239 HPDF_Stream_EOF  (HPDF_Stream  stream);
  240 
  241 
  242 HPDF_UINT32
  243 HPDF_Stream_Size  (HPDF_Stream  stream);
  244 
  245 HPDF_STATUS
  246 HPDF_Stream_Flush  (HPDF_Stream  stream);
  247 
  248 
  249 HPDF_STATUS
  250 HPDF_Stream_WriteEscapeName  (HPDF_Stream      stream,
  251                               const char  *value);
  252 
  253 
  254 HPDF_STATUS
  255 HPDF_Stream_WriteEscapeText2  (HPDF_Stream    stream,
  256                                const char    *text,
  257                                HPDF_UINT      len);
  258 
  259 
  260 HPDF_STATUS
  261 HPDF_Stream_WriteEscapeText  (HPDF_Stream      stream,
  262                               const char  *text);
  263 
  264 
  265 HPDF_STATUS
  266 HPDF_Stream_WriteBinary  (HPDF_Stream      stream,
  267                           const HPDF_BYTE  *data,
  268                           HPDF_UINT        len,
  269                           HPDF_Encrypt     e);
  270 
  271 
  272 HPDF_STATUS
  273 HPDF_Stream_Validate  (HPDF_Stream  stream);
  274 
  275 
  276 #ifdef __cplusplus
  277 }
  278 #endif /* __cplusplus */
  279 
  280 #endif /* _HPDF_STREAMS_H */