1 /*
2  * Copyright (c) 2005 Michael Schroeder (mls@suse.de)
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7 
8 #include <zlib.h>
9 #include <bzlib.h>
10 #include <lzma.h>
11 
12 struct cfile {
13   int fd;
14   void *fp;
15   int comp;
16   int level;
17   size_t len;
18   unsigned char buf[4096];
19   int bufN;
20   int eof;
21   void *ctx;
22   void (*ctxup)(void *, unsigned char *, unsigned int);
23   unsigned int crc;
24   unsigned int crclen;
25   size_t bytes;
26   int nunread;
27   unsigned char *unreadbuf;
28   union {
29     bz_stream bz;
30     z_stream gz;
31     lzma_stream lz;
32   } strm;
33   int (*read)(struct cfile *f, void *buf, int len);
34   int (*write)(struct cfile *f, void *buf, int len);
35   int (*close)(struct cfile *f);
36   int (*unread)(struct cfile *f, void *buf, int len);
37   int (*oldread)(struct cfile *f, void *buf, int len);
38 };
39 
40 typedef void (*cfile_ctxup)(void *, unsigned char *, unsigned int);
41 
42 #define CFILE_IO_FILE   (-2)
43 #define CFILE_IO_CFILE  (-3)
44 #define CFILE_IO_BUFFER (-4)
45 #define CFILE_IO_ALLOC  (-5)
46 #define CFILE_IO_NULL   (-6)
47 
48 #define CFILE_IO_REOPEN     (-99)
49 #define CFILE_IO_PUSHBACK   (-100)	/* internal */
50 
51 #define CFILE_COMP_XX (255)
52 #define CFILE_COMP_UN (0)
53 #define CFILE_COMP_GZ (1)
54 #define CFILE_COMP_BZ_20 (2)
55 #define CFILE_COMP_GZ_RSYNC (3)
56 #define CFILE_COMP_BZ_17 (4)
57 #define CFILE_COMP_LZMA (5)
58 #define CFILE_COMP_XZ (6)
59 
60 #define CFILE_COMP_BZ CFILE_COMP_BZ_20
61 
62 #define CFILE_MKCOMP(comp, level) ((comp) | ((level) << 8))
63 #define CFILE_COMPALGO(comp) ((comp) & 255)
64 #define CFILE_COMPLEVEL(comp) ((comp) >> 8 & 255)
65 
66 #define CFILE_OPEN_RD ('r')
67 #define CFILE_OPEN_WR ('w')
68 
69 #define CFILE_LEN_UNLIMITED ((size_t)-1)
70 
71 #define CFILE_UNREAD_GET_LEN (-2)
72 
73 #define CFILE_COPY_CLOSE_IN    (1 << 0)
74 #define CFILE_COPY_CLOSE_OUT   (1 << 1)
75 #define CFILE_COPY_CLOSE_INOUT (CFILE_COPY_CLOSE_IN|CFILE_COPY_CLOSE_OUT)
76 
77 #define CFILE_UNREAD_GETBYTES  (-2)
78 
79 struct cfile *cfile_open(int mode, int fd, void *fp, int comp, size_t len, void (*ctxup)(void *, unsigned char *, unsigned int), void *ctx);
80 int cfile_copy(struct cfile *in, struct cfile *out, int flags);
81 int cfile_detect_rsync(struct cfile *f);
82 char *cfile_comp2str(int comp);
83 int cfile_setlevel(int comp, int level);
84