Bump to libzio 1.02
[platform/upstream/libzio.git] / zioP.h
1 /*
2  * zioP.h       Internal header for libzio, including required standard glibc
3  *              header, zlib.h, lzma.h or lzmadec.h, and bzlib.h.
4  *              Making the used libz, liblzma, and bzlib functions weak symbols.
5  *
6  * Copyright 2004 Werner Fink, 2004 SuSE LINUX AG, Germany.
7  * Copyright 2006 Werner Fink, 2006 SuSE Products GmbH, Germany.
8  * Copyright 2013 Werner Fink, 2013 SuSE Products GmbH, Germany.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
22  *
23  * Author:      Werner Fink <werner@suse.de>
24  */
25
26 #ifndef _ZIO_P_H
27 #define _ZIO_P_H
28
29 #ifndef _GNU_SOURCE
30 # define _GNU_SOURCE
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #ifdef HAVE_LIBIO_H
39 # include <libio.h>
40 #endif
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <errno.h>
44
45 #ifndef  unused
46 # define unused         __attribute__((__unused__))
47 #endif
48 #ifndef  nonnull
49 # define nonnull(parm)  __attribute__((__nonnull__ parm))
50 #endif
51 #ifndef  wur
52 # define wur            __attribute__((__warn_unused_result__))
53 #endif
54 #define alignof(type)   (sizeof(type)+(sizeof(type)%sizeof(void*)))
55 #define strsize(str)    ((strlen(str)+1)*sizeof(char))
56
57 #if !defined(HAVE_FOPENCOOKIE) && !defined(HAVE_FUNOPEN)
58 # error Requires fopencookie(3GNU) or funopen(3BSD)
59 #endif
60
61 #if defined(HAVE_LIBIO_H) || defined(HAVE_FOPENCOOKIE)
62 # if defined __GLIBC__ && __GLIBC__ > 1
63 #  undef  LIBIO_IS_FIXED
64 #  if __GLIBC__ > 2 || (__GLIBC__ >= 2 && __GLIBC_MINOR__ > 0)
65 #   define LIBIO_IS_FIXED
66 #  endif
67 # else
68 #  error The libzio requires the GLIBC
69 # endif
70 #endif
71
72 #if defined __GNUC__
73 #  if defined __USE_ISOC99
74 #    define _cat_pragma(exp)    _Pragma(#exp)
75 #    define _weak_pragma(exp)   _cat_pragma(weak name)
76 #  else
77 #    define _weak_pragma(exp)
78 #  endif
79 #  define _declare(name)        __extension__ extern __typeof__(name) name
80 #  define weak_symbol(name)     _weak_pragma(name) _declare(name) __attribute__((weak))
81 #else
82 #  error The libzio requires the GCC
83 #endif
84
85 #if defined(HAS_ZLIB_H)
86 # include <zlib.h>
87 # ifndef NO_WEAK
88 weak_symbol(gzopen);
89 weak_symbol(gzdopen);
90 weak_symbol(gzread);
91 weak_symbol(gzwrite);
92 weak_symbol(gzseek);
93 weak_symbol(gzflush);
94 weak_symbol(gzclose);
95 # endif
96 #endif
97
98 #if defined(HAS_BZLIB_H)
99 # include <bzlib.h>
100 # ifndef NO_WEAK
101 weak_symbol(BZ2_bzopen);
102 weak_symbol(BZ2_bzdopen);
103 weak_symbol(BZ2_bzread);
104 weak_symbol(BZ2_bzwrite);
105 /* no BZ2_bzseek */
106 weak_symbol(BZ2_bzflush);
107 weak_symbol(BZ2_bzclose);
108 # endif
109 #endif
110
111 #if defined(HAS_LZMA_H)
112 # include <stdint.h>
113 # include <lzma.h>
114 # ifndef NO_WEAK
115 weak_symbol(lzma_easy_encoder);
116 weak_symbol(lzma_lzma_preset);
117 weak_symbol(lzma_alone_encoder);
118 weak_symbol(lzma_auto_decoder);
119 weak_symbol(lzma_code);
120 weak_symbol(lzma_end);
121 # endif
122 #else /* !HAS_LZMA_H */
123 # if defined(HAS_LZMADEC_H)
124 #  include <stdint.h>
125 #  include <lzmadec.h>
126 #  ifndef NO_WEAK
127 weak_symbol(lzmadec_open);
128 weak_symbol(lzmadec_dopen);
129 weak_symbol(lzmadec_read);
130 /* no lzmadec_write() */
131 weak_symbol(lzmadec_seek);
132 weak_symbol(lzmadec_close);
133 /* no lzmadec_flush() */
134 #  endif
135 # endif
136 #endif /* !HAS_LZMA_H */
137
138 #if defined(HAVE_FOPENCOOKIE)
139 # undef HAVE_FUNOPEN
140 __extension__ typedef off_t   zio_off_t;
141 __extension__ typedef int     zio_int_t;
142 # if !defined(LIBIO_IS_FIXED)
143 __extension__ typedef _IO_cookie_io_functions_t cookie_io_functions_t;
144 __extension__ typedef ssize_t cookie_read_function_t  __P ((void *, char *, size_t));
145 __extension__ typedef ssize_t cookie_write_function_t __P ((void *, const char *, size_t));
146 __extension__ typedef int     cookie_seek_function_t  __P ((void *, off_t, int));
147 __extension__ typedef int     cookie_close_function_t __P ((void *));
148 # endif
149 #endif
150 #if defined(HAVE_FUNOPEN)
151 __extension__ typedef size_t zio_off_t;
152 __extension__ typedef fpos_t zio_int_t;
153 __extension__ typedef int    cookie_read_function_t  __P ((void *, char *, int));
154 __extension__ typedef int    cookie_write_function_t __P ((void *, const char *, int));
155 __extension__ typedef fpos_t cookie_seek_function_t  __P ((void *, fpos_t, int));
156 __extension__ typedef int    cookie_close_function_t __P ((void *));
157 __extension__ typedef struct
158 {
159     cookie_read_function_t  *read;
160     cookie_write_function_t *write;
161     cookie_seek_function_t  *seek;
162     cookie_close_function_t *close;
163 } cookie_io_functions_t;
164 static __inline__ FILE *fopencookie(void *__restrict,
165                                     const char *__restrict,
166                                     cookie_io_functions_t) nonnull((1,2)) wur;
167 static __inline__ FILE *fopencookie(void *__restrict cookie,
168                                     const char *__restrict mode unused,
169                                     cookie_io_functions_t io_funcs)
170 {
171     return funopen(cookie, io_funcs.read, io_funcs.write, io_funcs.seek, io_funcs.close);
172 }
173 #endif
174 #endif /* _ZIO_P_H */