-
Notifications
You must be signed in to change notification settings - Fork 1
/
zlib.rb
213 lines (160 loc) · 7.87 KB
/
zlib.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# =============================================================================
require "rubygems"
require "ffi"
# =============================================================================
module FFI::Zlib
extend FFI::Library
ffi_lib("z")
# -------------------------------------------------------------------------
attach_function :zlibVersion, [ ], :string
# -------------------------------------------------------------------------
ZLIB_VERSION = zlibVersion()
# Allowed flush values.
Z_NO_FLUSH = 0
Z_PARTIAL_FLUSH = 1
Z_SYNC_FLUSH = 2
Z_FULL_FLUSH = 3
Z_FINISH = 4
Z_BLOCK = 5
# Return codes for the compression/decompression functions.
# Negative values are errors, positive values are used for special but
# normal events.
Z_OK = 0
Z_STREAM_END = 1
Z_NEED_DICT = 2
Z_ERRNO = -1
Z_STREAM_ERROR = -2
Z_DATA_ERROR = -3
Z_MEM_ERROR = -4
Z_BUF_ERROR = -5
Z_VERSION_ERROR = -6
# Compression levels.
Z_NO_COMPRESSION = 0
Z_BEST_SPEED = 1
Z_BEST_COMPRESSION = 9
Z_DEFAULT_COMPRESSION = -1
# Compression strategies.
Z_FILTERED = 1
Z_HUFFMAN_ONLY = 2
Z_RLE = 3
Z_FIXED = 4
Z_DEFAULT_STRATEGY = 0
# Possible values of the data_type field.
Z_BINARY = 0
Z_TEXT = 1
Z_UNKNOWN = 2
# The deflate compression method (the only one supported).
Z_DEFLATED = 8
# -------------------------------------------------------------------------
class Z_stream < FFI::Struct
layout :next_in, :pointer,
:avail_in, :uint,
:total_in, :ulong,
:next_out, :pointer,
:avail_out, :uint,
:total_out, :ulong,
:msg, :string,
:state, :pointer,
:alloc_func, :pointer,
:free_func, :pointer,
:opaque, :pointer,
:data_type, :int,
:adler, :ulong,
:reserved, :ulong
end
# -------------------------------------------------------------------------
class GZ_header < FFI::Struct
layout :text, :int,
:time, :ulong,
:xflags, :int,
:os, :int,
:extra, :pointer,
:extra_len, :uint,
:extra_max, :uint,
:name, :string,
:name_max, :uint,
:comment, :string,
:comm_max, :uint,
:hcrc, :int,
:done, :int
end
# -------------------------------------------------------------------------
def self.deflateInit(zstream, level)
deflateInit_(zstream, level, ZLIB_VERSION, zstream.size)
end
def self.deflateInit2(zstream, level, method, window_bits, mem_level, strategy)
deflateInit2_(zstream, level, method, window_bits, mem_level, strategy, ZLIB_VERSION, zstream.size)
end
attach_function :deflateInit_, [:pointer, :int, :string, :int], :int
attach_function :deflateInit2_, [:pointer, :int, :int, :int, :int, :int, :string, :int], :int
attach_function :deflate, [:pointer, :int], :int
attach_function :deflateEnd, [:pointer], :int
attach_function :deflateSetDictionary, [:pointer, :pointer, :uint], :int
attach_function :deflateCopy, [:pointer, :pointer], :int
attach_function :deflateReset, [:pointer], :int
attach_function :deflateParams, [:pointer, :int, :int], :int
attach_function :deflateTune, [:pointer, :int, :int, :int, :int], :int
attach_function :deflateBound, [:pointer, :ulong], :ulong
attach_function :deflatePrime, [:pointer, :int, :int], :int
attach_function :deflateSetHeader, [:pointer, :pointer], :int
# -------------------------------------------------------------------------
def self.inflateInit(zstream)
inflateInit_(zstream, ZLIB_VERSION, zstream.size)
end
def self.inflateInit2(zstream, window_bits)
inflateInit2_(zstream, window_bits, ZLIB_VERSION, zstream.size)
end
attach_function :inflateInit_, [:pointer, :string, :int], :int
attach_function :inflateInit2_, [:pointer, :int, :string, :int], :int
attach_function :inflate, [:pointer, :int], :int
attach_function :inflateEnd, [:pointer], :int
attach_function :inflateSetDictionary, [:pointer, :pointer, :uint], :int
attach_function :inflateSync, [:pointer], :int
attach_function :inflateCopy, [:pointer, :pointer], :int
attach_function :inflateReset, [:pointer], :int
attach_function :inflatePrime, [:pointer, :int, :int], :int
attach_function :inflateGetHeader, [:pointer, :pointer], :int
callback :in_func, [:pointer, :pointer], :uint
callback :out_func, [:pointer, :pointer, :uint], :int
attach_function :inflateBackInit_, [:pointer, :int, :pointer], :int
attach_function :inflateBack, [:pointer, :in_func, :pointer, :out_func, :pointer], :int
attach_function :inflateBackEnd, [:pointer], :int
# -------------------------------------------------------------------------
attach_function :compress, [:pointer, :buffer_inout, :pointer, :ulong], :int
attach_function :compress2, [:pointer, :pointer, :pointer, :ulong, :int], :int
attach_function :compressBound, [:ulong], :ulong
attach_function :uncompress, [:pointer, :buffer_inout, :pointer, :ulong], :int
# -------------------------------------------------------------------------
attach_function :gzopen, [:string, :string], :pointer
attach_function :gzdopen, [:int, :string], :pointer
attach_function :gzsetparams, [:pointer, :int, :int], :int
attach_function :gzread, [:pointer, :pointer, :uint], :int
attach_function :gzwrite, [:pointer, :pointer, :uint], :int
attach_function :gzprintf, [:pointer, :string, :varargs], :int
attach_function :gzputs, [:pointer, :string], :int
attach_function :gzgets, [:pointer, :pointer, :int], :pointer
attach_function :gzputc, [:pointer, :int], :int
attach_function :gzgetc, [:pointer, :pointer], :int
attach_function :gzungetc, [:int, :pointer], :int
attach_function :gzflush, [:pointer, :int], :int
attach_function :gzseek, [:pointer, :long, :int], :long
attach_function :gzrewind, [:pointer], :int
attach_function :gztell, [:pointer], :long
attach_function :gzeof, [:pointer], :int
attach_function :gzdirect, [:pointer], :int
attach_function :gzclose, [:pointer], :int
attach_function :gzerror, [:pointer, :buffer_out], :string
attach_function :gzclearerr, [:pointer], :void
# -------------------------------------------------------------------------
attach_function :adler32, [:ulong, :pointer, :uint], :ulong
attach_function :adler32_combine, [:ulong, :ulong, :long], :ulong
attach_function :crc32, [:ulong, :pointer, :uint], :ulong
attach_function :crc32_combine, [:ulong, :ulong, :long], :ulong
# -------------------------------------------------------------------------
attach_function :zlibCompileFlags, [ ], :ulong
attach_function :zError, [:int], :string
attach_function :inflateSyncPoint, [:pointer], :int
attach_function :get_crc_table, [ ], :pointer
# -------------------------------------------------------------------------
end
# =============================================================================