Skip to content

Commit 0985c1d

Browse files
committed
Initial commit.
0 parents  commit 0985c1d

File tree

8 files changed

+591
-0
lines changed

8 files changed

+591
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.a
2+
*.so
3+
4+
# Distribution / packaging
5+
env/
6+
build/
7+
dist/
8+
var/
9+
10+
# Other stuff
11+
*.swp
12+
*.mod
13+
*.o

LICENCE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2022, Philipp Engel
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose
4+
with or without fee is hereby granted, provided that the above copyright notice
5+
and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13+
THIS SOFTWARE.

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.POSIX:
2+
3+
FC = gfortran
4+
AR = ar
5+
FFLAGS =
6+
LDLAGS = -I/usr/include/ -L/usr/lib/
7+
LDLIBS = -lz
8+
ARFLAGS = rcs
9+
TARGET = libfortran-zlib.a
10+
11+
.PHONY: all clean test
12+
13+
all: $(TARGET)
14+
15+
$(TARGET):
16+
$(FC) $(FFLAGS) -c src/zlib.f90
17+
$(AR) $(ARFLAGS) $(TARGET) zlib.o
18+
19+
test: $(TARGET)
20+
$(FC) $(FFLAGS) $(LDFLAGS) -o test_zlib test/test_zlib.f90 $(TARGET) $(LDLIBS)
21+
22+
clean:
23+
rm *.o
24+
rm *.mod
25+
rm $(TARGET)
26+
rm test_zlib

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# fortran-zlib
2+
A collection of Fortran 2018 ISO_C_BINDING interfaces to selected zlib
3+
functions.
4+
5+
## Build Instructions
6+
Simply run the provided Makefile:
7+
8+
```
9+
$ make
10+
```
11+
12+
This outputs the static library `libfortran-zlib.a`. Link your program against
13+
`libfortran-zlib.a -lz`. Optionally, overwrite the default compiler:
14+
15+
```
16+
$ make FC=ifort
17+
```
18+
19+
To build the test program, run:
20+
21+
```
22+
$ make test
23+
$ ./test_zlib
24+
```
25+
26+
Alternatively, you can compile the library with *fpm*:
27+
28+
```
29+
$ fpm build --profile=release
30+
```
31+
32+
## Coverage
33+
| C function | Fortran interface |
34+
|----------------|-------------------|
35+
| `deflate` | `deflate` |
36+
| `deflateEnd` | `deflate_end` |
37+
| `deflateInit` | `deflate_init` |
38+
| `deflateInit2` | `deflate_init2` |
39+
| `inflate` | `inflate` |
40+
| `inflateEnd` | `inflate_end` |
41+
| `inflateInit` | `inflate_init` |
42+
| `inflateInit2` | `inflate_init2` |
43+
44+
## Licence
45+
ISC

fpm.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name = "fortran-zlib"
2+
version = "0.1.0"
3+
license = "ISC"
4+
author = "Philipp Engel"
5+
maintainer = "@interkosmos"
6+
copyright = "Copyright (c) 2022, Philipp Engel"
7+
description = "Fortran 2018 ISO_C_BINDING interfaces to zlib"
8+
keywords = ["zlib"]
9+
10+
[build]
11+
link = "z"
12+
13+
[library]
14+
source-dir = "src"
15+
16+
[[test]]
17+
name = "test_zlib"
18+
source-dir = "test"
19+
main = "test_zlib.f90"

src/zlib.f90

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

test/test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Now is the time for all good men to come to the aid of the party.
2+
Now is the time for all good men to come to the aid of the party.
3+
Now is the time for all good men to come to the aid of the party.
4+
Now is the time for all good men to come to the aid of the party.
5+
Now is the time for all good men to come to the aid of the party.
6+
Now is the time for all good men to come to the aid of the party.
7+
Now is the time for all good men to come to the aid of the party.
8+
Now is the time for all good men to come to the aid of the party.
9+
Now is the time for all good men to come to the aid of the party.
10+
Now is the time for all good men to come to the aid of the party.

0 commit comments

Comments
 (0)