summaryrefslogtreecommitdiff
path: root/lzw.h
blob: f1251f3bb726639ace302cf2f026e4a1809db18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* lzw.h -- define the lzw functions.

   Copyright (C) 1992-1993 Jean-loup Gailly.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

#ifndef BITS
#  define BITS 16
#endif
#define INIT_BITS 9              /* Initial number of bits per code */

#define	LZW_MAGIC  "\037\235"   /* Magic header for lzw files, 1F 9D */

#define BIT_MASK    0x1f /* Mask for 'number of compression bits' */
/* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
 * It's a pity that old uncompress does not check bit 0x20. That makes
 * extension of the format actually undesirable because old compress
 * would just crash on the new format instead of giving a meaningful
 * error message. It does check the number of bits, but it's more
 * helpful to say "unsupported format, get a new version" than
 * "can only handle 16 bits".
 */

#define BLOCK_MODE  0x80
/* Block compression: if table is full and compression rate is dropping,
 * clear the dictionary.
 */

#define LZW_RESERVED 0x60 /* reserved bits */

#define	CLEAR  256       /* flush the dictionary */
#define FIRST  (CLEAR+1) /* first free entry */

#include <stdint.h>
#include <ucontext.h>
#include "zioP.h"

#if defined _REENTRANT || defined _THREAD_SAFE
# include <pthread.h>
weak_symbol(pthread_sigmask);
#endif

__extension__
static __inline__ int sigucmask(int how, const sigset_t * __restrict set, sigset_t * __restrict oset)
{
#if defined _REENTRANT || defined _THREAD_SAFE
    if (&pthread_sigmask)
	return pthread_sigmask(how, set, oset);
    else
#endif
    return sigprocmask(how, set, oset);
}

#if defined(__ia64__) && defined(uc_sigmask)	/* Broken header sys/ucontext.h -> bits/sigcontext.h */
__extension__
static __inline__ unsigned long int sig_ia64_mask(const sigset_t set)
{
    unsigned long int mask = 0;
    int cnt = (8 * sizeof(unsigned long int));
    if (cnt > NSIG) cnt = NSIG;
    while (--cnt >= 0) {
	if (!sigismember(&set, cnt))
	    continue;
	mask |= (1 << (cnt - 1));		/* sigmask() macro is is not usable for BSD way */
    }
    return mask;
}
#endif

typedef struct _LZW_s {
    uint8_t  *inbuf;
    uint8_t  *outbuf;
    uint16_t *d_buf;
    uint8_t  *tab_suffix;
    uint16_t *tab_prefix;
    uint8_t  *transfer;
    off_t     bytes_in;
    off_t     bytes_out;
    size_t    insize;
    size_t    inptr;
    size_t    outpos;
    size_t    tcount;
    ssize_t   tsize;
    ssize_t   rsize;
    struct unlzw_s {
	uint8_t *stackp;
	int32_t  code;
	int      finchar;
	int32_t  oldcode;
	int32_t  incode;
	uint32_t inbits;
	uint32_t posbits;
	uint32_t bitmask;
	int32_t  free_ent;
	int32_t  maxcode;
	int32_t  maxmaxcode;
	off_t    newdif;
	int      n_bits;
	int      block_mode;
	int      maxbits;
    } n;
    int ifd;
    char *ifname;
    ucontext_t *uc;
    uint8_t *stack;
} LZW_t;

extern LZW_t *openlzw(const char * __restrict, const char * __restrict);
extern LZW_t *dopenlzw(int fildes, const char * __restrict);
extern ssize_t readlzw(LZW_t * __restrict, char * __restrict, const size_t);
extern void closelzw(LZW_t * __restrict);