1 /* HETLIB.H     (c) Copyright Leland Lucius, 2000-2009               */
2 /*              Header for the Hercules Emulated Tape library        */
3 
4 #if !defined( _HETLIB_H_ )
5 #define _HETLIB_H_
6 
7 /*
8 || ----------------------------------------------------------------------------
9 ||
10 || HETLIB.H     (c) Copyright Leland Lucius, 2000-2009
11 ||              Released under terms of the Q Public License.
12 ||
13 || Header for the Hercules Emulated Tape library.
14 ||
15 || ----------------------------------------------------------------------------
16 */
17 
18 #include "hercules.h"
19 
20 #ifndef _HETLIB_C_
21 #ifndef _HTAPE_DLL_
22 #define HET_DLL_IMPORT DLL_IMPORT
23 #else   /* _HUTIL_DLL_ */
24 #define HET_DLL_IMPORT extern
25 #endif  /* _HUTIL_DLL_ */
26 #else
27 #define HET_DLL_IMPORT DLL_EXPORT
28 #endif
29 
30 
31 #if !defined( TRUE )
32 #define TRUE                    (1L)
33 #endif
34 
35 #if !defined( FALSE )
36 #define FALSE                   (0L)
37 #endif
38 
39 /*
40 || Chunk header for an HET.  Physically compatable with AWSTAPE format.
41 */
42 typedef struct _hethdr
43 {
44     uint8_t        clen[ 2 ];          /* Length of current block           */
45     uint8_t        plen[ 2 ];          /* Length of previous block          */
46     uint8_t        flags1;             /* Flags byte 1                      */
47     uint8_t        flags2;             /* Flags byte 2                      */
48 } HETHDR;
49 
50 /*
51 || Macros for accessing current and previous sizes - accept ptr to HETHDR
52 */
53 #define HETHDR_CLEN( h ) ( ( (h)->chdr.clen[ 1 ] << 8 ) + (h)->chdr.clen[ 0 ] )
54 #define HETHDR_PLEN( h ) ( ( (h)->chdr.plen[ 1 ] << 8 ) + (h)->chdr.plen[ 0 ] )
55 
56 /*
57 || Definitions for HETHDR flags byte 1 (compression incompatable with AWSTAPE)
58 */
59 #define HETHDR_FLAGS1_BOR       0x80    /* Start of new record              */
60 #define HETHDR_FLAGS1_TAPEMARK  0x40    /* Tape mark                        */
61 #define HETHDR_FLAGS1_EOR       0x20    /* End of record                    */
62 #define HETHDR_FLAGS1_COMPRESS  0x03    /* Compression method mask          */
63 #define HETHDR_FLAGS1_BZLIB     0x02    /* BZLIB compression                */
64 #define HETHDR_FLAGS1_ZLIB      0x01    /* ZLIB compression                 */
65 
66 /*
67 || Definitions for HETHDR flags byte 2 (incompatable with AWSTAPE and HET)
68 */
69 #define HETHDR_FLAGS2_COMPRESS     0x80 /* Compression method mask          */
70 #define HETHDR_FLAGS2_ZLIB_BUSTECH 0x80 /* Bus-Tech ZLIB compression        */
71 
72 /*
73 || Control block for Hercules Emulated Tape files
74 */
75 typedef struct _hetb
76 {
77     FILE           *fd;                 /* Tape file descriptor             */
78     uint32_t        chksize;            /* Size of output chunks            */
79     uint32_t        ublksize;           /* Current block compressed size    */
80     uint32_t        cblksize;           /* Current block uncompressed size  */
81     uint32_t        cblk;               /* Current block number             */
82     HETHDR          chdr;               /* Current block header             */
83     u_int           writeprotect:1;     /* TRUE=write protected             */
84     u_int           readlast:1;         /* TRUE=last i/o was read           */
85     u_int           truncated:1;        /* TRUE=file truncated              */
86     u_int           compress:1;         /* TRUE=compress written data       */
87     u_int           decompress:1;       /* TRUE=decompress read data        */
88     u_int           method:2;           /* 1=ZLIB, 2=BZLIB compresion       */
89     u_int           level:4;            /* 1=<n<=9 compression level        */
90 } HETB;
91 
92 /*
93 || Compression methods
94 */
95 #define HETMETH_ZLIB            1       /* ZLIB compression                 */
96 #define HETMETH_BZLIB           2       /* BZLIB compression                */
97 
98 /*
99 || Limits
100 */
101 #define HETMIN_METHOD           1       /* Minimum compression method       */
102 #if defined( HET_BZIP2 )
103 #define HETMAX_METHOD           2       /* Maximum compression method       */
104 #else
105 #define HETMAX_METHOD           1       /* Maximum compression method       */
106 #endif /* defined( HET_BZIP2 ) */
107 #define HETMIN_LEVEL            1       /* Minimum compression level        */
108 #define HETMAX_LEVEL            9       /* Maximum compression level        */
109 #define HETMIN_CHUNKSIZE        4096    /* Minimum chunksize                */
110 #define HETMAX_CHUNKSIZE        65535   /* Maximum chunksize                */
111 #define HETMIN_BLOCKSIZE        1       /* Minimum blocksize                */
112 #define HETMAX_BLOCKSIZE        65535   /* Maximum blocksize                */
113 
114 /*
115 || Default settings
116 */
117 #define HETDFLT_COMPRESS        TRUE    /* Compress written data            */
118 #define HETDFLT_DECOMPRESS      TRUE    /* Decompress read data             */
119 #define HETDFLT_METHOD          HETMETH_ZLIB /* Use ZLIB compression        */
120 #define HETDFLT_LEVEL           4       /* Middle of the road               */
121 #define HETDFLT_CHKSIZE         HETMAX_BLOCKSIZE /* As big as it gets       */
122 
123 /*
124 || Flags for het_open()
125 */
126 #define HETOPEN_CREATE          0x01    /* Create NL tape, if file missing  */
127 #define HETOPEN_READONLY        0x02    /* Force the open read-only         */
128 
129 /*
130 || Modes for het_cntl()
131 */
132 #define HETCNTL_GET             0<<8    /* OR with func code to get value   */
133 #define HETCNTL_SET             1<<8    /* OR with func code to set value   */
134 
135 /*
136 || Function codes for het_cntl()
137 */
138 #define HETCNTL_COMPRESS        1       /* TRUE=write compression on        */
139 #define HETCNTL_DECOMPRESS      2       /* TRUE=read decomporession on      */
140 #define HETCNTL_METHOD          3       /* Compression method               */
141 #define HETCNTL_LEVEL           4       /* Compression level                */
142 #define HETCNTL_CHUNKSIZE       5       /* Chunk size                       */
143 
144 /*
145 || Error definitions
146 */
147 #define HETE_OK                 0       /* No error                         */
148 #define HETE_ERROR              -1      /* File error (check errno)         */
149 #define HETE_TAPEMARK           -2      /* Tapemark read                    */
150 #define HETE_BOT                -3      /* Beginning of tape                */
151 #define HETE_EOT                -4      /* End of tape                      */
152 #define HETE_BADBOR             -5      /* BOR not found                    */
153 #define HETE_BADEOR             -6      /* EOR not found                    */
154 #define HETE_BADMARK            -7      /* Unexpected tapemark              */
155 #define HETE_OVERFLOW           -8      /* Buffer not big enough            */
156 #define HETE_PREMEOF            -9      /* Premature EOF                    */
157 #define HETE_DECERR             -10     /* Decompression error              */
158 #define HETE_UNKMETH            -11     /* Unknown compression method       */
159 #define HETE_COMPERR            -12     /* Compression error                */
160 #define HETE_BADLEN             -13     /* Specified length to big          */
161 #define HETE_PROTECTED          -14     /* Write protected                  */
162 #define HETE_BADFUNC            -15     /* Bad function code passed         */
163 #define HETE_BADMETHOD          -16     /* Bad compression method           */
164 #define HETE_BADLEVEL           -17     /* Bad compression level            */
165 #define HETE_BADSIZE            -18     /* Bad write chunk size             */
166 #define HETE_BADDIR             -19     /* Invalid direction specified      */
167 #define HETE_NOMEM              -20     /* Insufficient memory              */
168 #define HETE_BADHDR             -21     /* Couldn't read block header       */
169 #define HETE_BADCOMPRESS        -22     /* Inconsistent compression flags   */
170 
171 /*
172 || Public functions
173 */
174 HET_DLL_IMPORT int het_open( HETB **hetb, char *filename, int flags );
175 HET_DLL_IMPORT int het_close( HETB **hetb );
176 HET_DLL_IMPORT int het_read_header( HETB *hetb );
177 HET_DLL_IMPORT int het_read( HETB *hetb, void *sbuf );
178 HET_DLL_IMPORT int het_write_header( HETB *hetb, int len, int flags1, int flags2 );
179 HET_DLL_IMPORT int het_write( HETB *hetb, void *sbuf, int slen );
180 HET_DLL_IMPORT int het_tapemark( HETB *hetb );
181 HET_DLL_IMPORT int het_sync( HETB *hetb );
182 HET_DLL_IMPORT int het_cntl( HETB *hetb, int func, unsigned long val );
183 HET_DLL_IMPORT int het_locate( HETB *hetb, int block );
184 HET_DLL_IMPORT int het_bsb( HETB *hetb );
185 HET_DLL_IMPORT int het_fsb( HETB *hetb );
186 HET_DLL_IMPORT int het_bsf( HETB *hetb );
187 HET_DLL_IMPORT int het_fsf( HETB *hetb );
188 HET_DLL_IMPORT int het_rewind( HETB *hetb );
189 HET_DLL_IMPORT const char *het_error( int rc );
190 HET_DLL_IMPORT off_t het_tell ( HETB *hetb );
191 
192 #endif /* defined( _HETLIB_H_ ) */
193