|
| 1 | +! zlib.f90 |
| 2 | +! |
| 3 | +! Fortran 2018 interface bindings to zlib. |
| 4 | +! |
| 5 | +! Author: Philipp Engel |
| 6 | +! Licence: ISC |
| 7 | +module zlib |
| 8 | + use, intrinsic :: iso_c_binding |
| 9 | + implicit none (type, external) |
| 10 | + private |
| 11 | + |
| 12 | + integer, parameter, public :: z_uint = c_int |
| 13 | + integer, parameter, public :: z_ulong = c_long |
| 14 | + integer, parameter, public :: z_byte = c_char |
| 15 | + |
| 16 | + integer(kind=c_int), parameter, public :: Z_NO_FLUSH = 0 |
| 17 | + integer(kind=c_int), parameter, public :: Z_PARTIAL_FLUSH = 1 |
| 18 | + integer(kind=c_int), parameter, public :: Z_SYNC_FLUSH = 2 |
| 19 | + integer(kind=c_int), parameter, public :: Z_FULL_FLUSH = 3 |
| 20 | + integer(kind=c_int), parameter, public :: Z_FINISH = 4 |
| 21 | + integer(kind=c_int), parameter, public :: Z_BLOCK = 5 |
| 22 | + integer(kind=c_int), parameter, public :: Z_TREES = 6 |
| 23 | + |
| 24 | + integer(kind=c_int), parameter, public :: Z_OK = 0 |
| 25 | + integer(kind=c_int), parameter, public :: Z_STREAM_END = 1 |
| 26 | + integer(kind=c_int), parameter, public :: Z_NEED_DICT = 2 |
| 27 | + integer(kind=c_int), parameter, public :: Z_ERRNO = -1 |
| 28 | + integer(kind=c_int), parameter, public :: Z_STREAM_ERROR = -2 |
| 29 | + integer(kind=c_int), parameter, public :: Z_DATA_ERROR = -3 |
| 30 | + integer(kind=c_int), parameter, public :: Z_MEM_ERROR = -4 |
| 31 | + integer(kind=c_int), parameter, public :: Z_BUF_ERROR = -5 |
| 32 | + integer(kind=c_int), parameter, public :: Z_VERSION_ERROR = -6 |
| 33 | + |
| 34 | + integer(kind=c_int), parameter, public :: Z_NO_COMPRESSION = 0 |
| 35 | + integer(kind=c_int), parameter, public :: Z_BEST_SPEED = 1 |
| 36 | + integer(kind=c_int), parameter, public :: Z_BEST_COMPRESSION = 9 |
| 37 | + integer(kind=c_int), parameter, public :: Z_DEFAULT_COMPRESSION = -1 |
| 38 | + |
| 39 | + integer(kind=c_int), parameter, public :: Z_FILTERED = 1 |
| 40 | + integer(kind=c_int), parameter, public :: Z_HUFFMAN_ONLY = 2 |
| 41 | + integer(kind=c_int), parameter, public :: Z_RLE = 3 |
| 42 | + integer(kind=c_int), parameter, public :: Z_FIXED = 4 |
| 43 | + integer(kind=c_int), parameter, public :: Z_DEFAULT_STRATEGY = 0 |
| 44 | + |
| 45 | + integer(kind=c_int), parameter, public :: Z_BINARY = 0 |
| 46 | + integer(kind=c_int), parameter, public :: Z_TEXT = 1 |
| 47 | + integer(kind=c_int), parameter, public :: Z_ASCII = Z_TEXT |
| 48 | + integer(kind=c_int), parameter, public :: Z_UNKNOWN = 2 |
| 49 | + |
| 50 | + integer(kind=c_int), parameter, public :: Z_DEFLATED = 8 |
| 51 | + |
| 52 | + type, bind(c), public :: z_stream |
| 53 | + type(c_ptr) :: next_in = c_null_ptr |
| 54 | + integer(kind=z_uint) :: avail_in = 0 |
| 55 | + integer(kind=z_ulong) :: total_in = 0 |
| 56 | + type(c_ptr) :: next_out = c_null_ptr |
| 57 | + integer(kind=z_uint) :: avail_out = 0 |
| 58 | + integer(kind=z_ulong) :: total_out = 0 |
| 59 | + type(c_ptr) :: msg = c_null_ptr |
| 60 | + type(c_ptr) :: state = c_null_ptr |
| 61 | + type(c_funptr) :: zalloc = c_null_funptr |
| 62 | + type(c_funptr) :: zfree = c_null_funptr |
| 63 | + type(c_ptr) :: opaque = c_null_ptr |
| 64 | + integer(kind=c_int) :: data_type = 0 |
| 65 | + integer(kind=z_ulong) :: adler = 0 |
| 66 | + integer(kind=z_ulong) :: reserved = 0 |
| 67 | + end type z_stream |
| 68 | + |
| 69 | + public :: deflate |
| 70 | + public :: deflate_end |
| 71 | + public :: deflate_init |
| 72 | + public :: deflate_init2 |
| 73 | + public :: inflate |
| 74 | + public :: inflate_end |
| 75 | + public :: inflate_init |
| 76 | + public :: inflate_init2 |
| 77 | + |
| 78 | + interface |
| 79 | + ! int deflate(z_streamp strm, int flush) |
| 80 | + function deflate(strm, flush) bind(c, name='deflate') |
| 81 | + import :: c_int, z_stream |
| 82 | + implicit none |
| 83 | + type(z_stream), intent(in) :: strm |
| 84 | + integer(kind=c_int), intent(in), value :: flush |
| 85 | + integer(kind=c_int) :: deflate |
| 86 | + end function deflate |
| 87 | + |
| 88 | + ! int deflateEnd(z_streamp strm) |
| 89 | + function deflate_end(strm) bind(c, name='deflateEnd') |
| 90 | + import :: c_int, z_stream |
| 91 | + implicit none |
| 92 | + type(z_stream), intent(in) :: strm |
| 93 | + integer(kind=c_int) :: deflate_end |
| 94 | + end function deflate_end |
| 95 | + |
| 96 | + ! int deflateInit_(z_streamp strm, int level, const char *version, int stream_size) |
| 97 | + function deflate_init_(strm, level, version, stream_size) bind(c, name='deflateInit_') |
| 98 | + import :: c_int, c_ptr, z_stream |
| 99 | + implicit none |
| 100 | + type(z_stream), intent(in) :: strm |
| 101 | + integer(kind=c_int), intent(in), value :: level |
| 102 | + type(c_ptr), intent(in), value :: version |
| 103 | + integer(kind=c_int), intent(in), value :: stream_size |
| 104 | + integer(kind=c_int) :: deflate_init_ |
| 105 | + end function deflate_init_ |
| 106 | + |
| 107 | + ! int deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, |
| 108 | + ! int strategy, const char *version, int stream_size) |
| 109 | + function deflate_init2_(strm, level, method, window_bits, mem_level, strategy, & |
| 110 | + version, stream_size) bind(c, name='deflateInit2_') |
| 111 | + import :: c_int, c_ptr, z_stream |
| 112 | + implicit none |
| 113 | + type(z_stream), intent(in) :: strm |
| 114 | + integer(kind=c_int), intent(in), value :: level |
| 115 | + integer(kind=c_int), intent(in), value :: method |
| 116 | + integer(kind=c_int), intent(in), value :: window_bits |
| 117 | + integer(kind=c_int), intent(in), value :: mem_level |
| 118 | + integer(kind=c_int), intent(in), value :: strategy |
| 119 | + type(c_ptr), intent(in), value :: version |
| 120 | + integer(kind=c_int), intent(in), value :: stream_size |
| 121 | + integer(kind=c_int) :: deflate_init2_ |
| 122 | + end function deflate_init2_ |
| 123 | + |
| 124 | + ! int inflate(z_streamp strm, int flush) |
| 125 | + function inflate(strm, flush) bind(c, name='inflate') |
| 126 | + import :: c_int, z_stream |
| 127 | + implicit none |
| 128 | + type(z_stream), intent(in) :: strm |
| 129 | + integer(kind=c_int), intent(in), value :: flush |
| 130 | + integer(kind=c_int) :: inflate |
| 131 | + end function inflate |
| 132 | + |
| 133 | + ! int inflateEnd(z_streamp strm) |
| 134 | + function inflate_end(strm) bind(c, name='inflateEnd') |
| 135 | + import :: c_int, z_stream |
| 136 | + implicit none |
| 137 | + type(z_stream), intent(in) :: strm |
| 138 | + integer(kind=c_int) :: inflate_end |
| 139 | + end function inflate_end |
| 140 | + |
| 141 | + ! int inflateInit_(z_streamp strm, const char *version, int stream_size) |
| 142 | + function inflate_init_(strm, version, stream_size) bind(c, name='inflateInit_') |
| 143 | + import :: c_int, c_ptr, z_stream |
| 144 | + implicit none |
| 145 | + type(z_stream), intent(in) :: strm |
| 146 | + type(c_ptr), intent(in), value :: version |
| 147 | + integer(kind=c_int), intent(in), value :: stream_size |
| 148 | + integer(kind=c_int) :: inflate_init_ |
| 149 | + end function inflate_init_ |
| 150 | + |
| 151 | + ! int inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size) |
| 152 | + function inflate_init2_(strm, window_bits, version, stream_size) bind(c, name='inflateInit2_') |
| 153 | + import :: c_int, c_ptr, z_stream |
| 154 | + implicit none |
| 155 | + type(z_stream), intent(in) :: strm |
| 156 | + integer(kind=c_int), intent(in), value :: window_bits |
| 157 | + type(c_ptr), intent(in), value :: version |
| 158 | + integer(kind=c_int), intent(in), value :: stream_size |
| 159 | + integer(kind=c_int) :: inflate_init2_ |
| 160 | + end function inflate_init2_ |
| 161 | + |
| 162 | + function zlib_version_() bind(c, name='zlibVersion') |
| 163 | + import :: c_ptr |
| 164 | + implicit none |
| 165 | + type(c_ptr) :: zlib_version_ |
| 166 | + end function zlib_version_ |
| 167 | + end interface |
| 168 | +contains |
| 169 | + ! int deflateInit(z_streamp strm, int level) |
| 170 | + integer function deflate_init(strm, level) result(rc) |
| 171 | + type(z_stream), intent(inout) :: strm |
| 172 | + integer, intent(in) :: level |
| 173 | + |
| 174 | + rc = deflate_init_(strm, level, zlib_version_(), int(c_sizeof(strm), kind=c_int)) |
| 175 | + end function deflate_init |
| 176 | + |
| 177 | + ! int deflateInit2(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy) |
| 178 | + integer function deflate_init2(strm, level, method, window_bits, mem_level, strategy) result(rc) |
| 179 | + type(z_stream), intent(inout) :: strm |
| 180 | + integer, intent(in) :: level |
| 181 | + integer, intent(in) :: method |
| 182 | + integer, intent(in) :: window_bits |
| 183 | + integer, intent(in) :: mem_level |
| 184 | + integer, intent(in) :: strategy |
| 185 | + |
| 186 | + rc = deflate_init2_(strm, level, method, window_bits, mem_level, & |
| 187 | + strategy, zlib_version_(), int(c_sizeof(strm), kind=c_int)) |
| 188 | + end function deflate_init2 |
| 189 | + |
| 190 | + ! int inflateInit(z_streamp strm) |
| 191 | + integer function inflate_init(strm) result(rc) |
| 192 | + type(z_stream), intent(inout) :: strm |
| 193 | + |
| 194 | + rc = inflate_init_(strm, zlib_version_(), int(c_sizeof(strm), kind=c_int)) |
| 195 | + end function inflate_init |
| 196 | + |
| 197 | + ! int inflateInit2(z_streamp strm, int windowBits) |
| 198 | + integer function inflate_init2(strm, window_bits) result(rc) |
| 199 | + type(z_stream), intent(inout) :: strm |
| 200 | + integer, intent(in) :: window_bits |
| 201 | + |
| 202 | + rc = inflate_init2_(strm, window_bits, zlib_version_(), int(c_sizeof(strm), kind=c_int)) |
| 203 | + end function inflate_init2 |
| 204 | +end module zlib |
0 commit comments