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