Imported Upstream version 1.02
[platform/upstream/libzio.git] / lzw.h
1 /* lzw.h -- define the lzw functions.
2
3    Copyright (C) 1992-1993 Jean-loup Gailly.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef BITS
20 #  define BITS 16
21 #endif
22 #define INIT_BITS 9              /* Initial number of bits per code */
23
24 #define LZW_MAGIC  "\037\235"   /* Magic header for lzw files, 1F 9D */
25
26 #define BIT_MASK    0x1f /* Mask for 'number of compression bits' */
27 /* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
28  * It's a pity that old uncompress does not check bit 0x20. That makes
29  * extension of the format actually undesirable because old compress
30  * would just crash on the new format instead of giving a meaningful
31  * error message. It does check the number of bits, but it's more
32  * helpful to say "unsupported format, get a new version" than
33  * "can only handle 16 bits".
34  */
35
36 #define BLOCK_MODE  0x80
37 /* Block compression: if table is full and compression rate is dropping,
38  * clear the dictionary.
39  */
40
41 #define LZW_RESERVED 0x60 /* reserved bits */
42
43 #define CLEAR  256       /* flush the dictionary */
44 #define FIRST  (CLEAR+1) /* first free entry */
45
46 #include <stdint.h>
47 #include <ucontext.h>
48 #include "zioP.h"
49
50 #if defined _REENTRANT || defined _THREAD_SAFE
51 # include <pthread.h>
52 weak_symbol(pthread_sigmask);
53 #endif
54
55 __extension__
56 static __inline__ int sigucmask(int how, const sigset_t * __restrict set, sigset_t * __restrict oset)
57 {
58 #if defined _REENTRANT || defined _THREAD_SAFE
59     if (&pthread_sigmask)
60         return pthread_sigmask(how, set, oset);
61     else
62 #endif
63     return sigprocmask(how, set, oset);
64 }
65
66 #if defined(__ia64__) && defined(uc_sigmask)    /* Broken header sys/ucontext.h -> bits/sigcontext.h */
67 __extension__
68 static __inline__ unsigned long int sig_ia64_mask(const sigset_t set)
69 {
70     unsigned long int mask = 0;
71     int cnt = (8 * sizeof(unsigned long int));
72     if (cnt > NSIG) cnt = NSIG;
73     while (--cnt >= 0) {
74         if (!sigismember(&set, cnt))
75             continue;
76         mask |= (1 << (cnt - 1));               /* sigmask() macro is is not usable for BSD way */
77     }
78     return mask;
79 }
80 #endif
81
82 typedef struct _LZW_s {
83     uint8_t  *inbuf;
84     uint8_t  *outbuf;
85     uint16_t *d_buf;
86     uint8_t  *tab_suffix;
87     uint16_t *tab_prefix;
88     uint8_t  *transfer;
89     off_t     bytes_in;
90     off_t     bytes_out;
91     size_t    insize;
92     size_t    inptr;
93     size_t    outpos;
94     size_t    tcount;
95     ssize_t   tsize;
96     ssize_t   rsize;
97     struct unlzw_s {
98         uint8_t *stackp;
99         int32_t  code;
100         int      finchar;
101         int32_t  oldcode;
102         int32_t  incode;
103         uint32_t inbits;
104         uint32_t posbits;
105         uint32_t bitmask;
106         int32_t  free_ent;
107         int32_t  maxcode;
108         int32_t  maxmaxcode;
109         off_t    newdif;
110         int      n_bits;
111         int      block_mode;
112         int      maxbits;
113     } n;
114     int ifd;
115     char *ifname;
116     ucontext_t *uc;
117     uint8_t *stack;
118 } LZW_t;
119
120 extern LZW_t *openlzw(const char * __restrict, const char * __restrict);
121 extern LZW_t *dopenlzw(int fildes, const char * __restrict);
122 extern ssize_t readlzw(LZW_t * __restrict, char * __restrict, const size_t);
123 extern void closelzw(LZW_t * __restrict);