Class: Zlib::ZStream

Inherits:
Object
  • Object
show all
Defined in:
lib/pr/zlib/zstream.rb

Direct Known Subclasses

Deflate, Inflate

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



7
8
9
# File 'lib/pr/zlib/zstream.rb', line 7

def buf
  @buf
end

#flagsObject

Returns the value of attribute flags.



7
8
9
# File 'lib/pr/zlib/zstream.rb', line 7

def flags
  @flags
end

#funcObject

Returns the value of attribute func.



7
8
9
# File 'lib/pr/zlib/zstream.rb', line 7

def func
  @func
end

#inputObject

Returns the value of attribute input.



7
8
9
# File 'lib/pr/zlib/zstream.rb', line 7

def input
  @input
end

#streamObject

Returns the value of attribute stream.



7
8
9
# File 'lib/pr/zlib/zstream.rb', line 7

def stream
  @stream
end

Instance Method Details

#raise_zlib_error(err, msg) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pr/zlib/zstream.rb', line 9

def raise_zlib_error(err, msg)
  msg = zError(err) if msg.nil? || msg == ''

  case err
    when Z_STREAM_END
      raise StreamEnd, msg
    when Z_NEED_DICT
      raise NeedDict, msg
    when Z_STREAM_ERROR
      raise StreamError, msg
    when Z_DATA_ERROR
      raise DataError, msg
    when Z_BUF_ERROR
      raise BufError, msg
    when Z_VERSION_ERROR
      raise VersionError, msg
    when Z_MEM_ERROR
      raise MemError, msg
    when Z_ERRNO
      raise SystemCallError, msg
    else
      raise Error, 'unknown zlib error #errend: #msgend'
  end
end

#zstream_append_buffer(src, len) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pr/zlib/zstream.rb', line 58

def zstream_append_buffer(src, len)
  if @buf.nil?
    @buf = Bytef.new(src[0, len], len)
    @stream.next_out = Bytef.new(@buf)
    @stream.avail_out = 0
    return
  end
  if @buf.length < @buf.offset + len
    @buf.buffer << (0.chr * (@buf.offset + len - @buf.length))
    @stream.avail_out = 0
  else
    if @stream.avail_out >= len
      @stream.avail_out -= len
    else
      @stream.avail_out = 0
    end
  end
  @buf.buffer[@buf.offset, len] = src[0, len]
  @buf += len
  @stream.next_out = Bytef.new(@buf, @buf.offset)
end

#zstream_append_input(src, len) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/pr/zlib/zstream.rb', line 123

def zstream_append_input(src, len)
  return if len <= 0
  src = src.current if src.class != String
  if @input.nil?
    @input = src[0, len]
  else
    @input << src[0, len]
  end
end

#zstream_buffer_ungetc(c) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pr/zlib/zstream.rb', line 111

def zstream_buffer_ungetc(c)
  if @buf.nil? || (@buf.length - @buf.offset).zero?
    zstream_expand_buffer()
  end
  @buf.buffer[0, 0] = c.chr
  @buf += 1
  if @stream.avail_out > 0
    @stream.next_out += 1
    @stream.avail_out -= 1
  end
end

#zstream_detach_bufferObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pr/zlib/zstream.rb', line 80

def zstream_detach_buffer
  if @buf.nil?
    dst = ''
  else
    dst = @buf.buffer[0, @buf.offset]
  end

  @buf = Bytef.new(0.chr * ZSTREAM_INITIAL_BUFSIZE)
  @stream.next_out = Bytef.new(@buf)
  @stream.avail_out = ZSTREAM_INITIAL_BUFSIZE
  @buf_filled = 0

  dst
end

#zstream_detach_inputObject



152
153
154
155
156
157
158
159
160
# File 'lib/pr/zlib/zstream.rb', line 152

def zstream_detach_input
  if @input.nil?
    dst = ''
  else
    dst = @input
  end
  @input = nil
  dst
end

#zstream_discard_input(len) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/pr/zlib/zstream.rb', line 133

def zstream_discard_input(len)
  if @input.nil? || @input.length <= len
    @input = nil
  else
    @input[0, len] = ''
  end
end

#zstream_endObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pr/zlib/zstream.rb', line 175

def zstream_end
  if !ZSTREAM_IS_READY()
    warn('attempt to close uninitialized zstream; ignored.')
    return nil
  end
  if (@flags & ZSTREAM_FLAG_IN_STREAM).nonzero?
    warn('attempt to close unfinished zstream; reset forced.')
    zstream_reset()
  end

  zstream_reset_input()
  err = send(@func.end, @stream)
  if err != Z_OK
    raise_zlib_error(err, @stream.msg)
  end
  @flags = 0
  nil
end

#zstream_expand_bufferObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pr/zlib/zstream.rb', line 34

def zstream_expand_buffer
  if @buf.nil?
    @buf = Bytef.new(0.chr * ZSTREAM_INITIAL_BUFSIZE)
    @stream.next_out = Bytef.new(@buf)
    @stream.avail_out = ZSTREAM_INITIAL_BUFSIZE
    return
  end

  if @buf.length - @buf.offset >= ZSTREAM_AVAIL_OUT_STEP_MAX
    @stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX
  else
    inc = @buf.offset / 2
    if inc < ZSTREAM_AVAIL_OUT_STEP_MIN
      inc = ZSTREAM_AVAIL_OUT_STEP_MIN
    end
    if @buf.length < @buf.offset + inc
      @buf.buffer << 0.chr * (@buf.offset + inc - @buf.length)
    end
    @stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
        inc : ZSTREAM_AVAIL_OUT_STEP_MAX
  end
  @stream.next_out = Bytef.new(@buf, @buf.offset)
end

#zstream_init(func) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/pr/zlib/zstream.rb', line 227

def zstream_init(func)
  @flags = 0
  @buf = nil
  @input = nil
  @stream = Z_stream.new
  @stream.msg = ''
  @stream.next_in = nil
  @stream.avail_in = 0
  @stream.next_out = nil
  @stream.avail_out = 0
  @func = func
end

#ZSTREAM_IS_CLOSINGObject



298
299
300
# File 'lib/pr/zlib/zstream.rb', line 298

def ZSTREAM_IS_CLOSING
  !(@flags & ZSTREAM_FLAG_CLOSING).zero?
end

#ZSTREAM_IS_FINISHEDObject



294
295
296
# File 'lib/pr/zlib/zstream.rb', line 294

def ZSTREAM_IS_FINISHED
  !(@flags & ZSTREAM_FLAG_FINISHED).zero?
end

#ZSTREAM_IS_READYObject



290
291
292
# File 'lib/pr/zlib/zstream.rb', line 290

def ZSTREAM_IS_READY
  !(@flags & ZSTREAM_FLAG_READY).zero?
end

#zstream_passthrough_inputObject



145
146
147
148
149
150
# File 'lib/pr/zlib/zstream.rb', line 145

def zstream_passthrough_input
  if @input
    zstream_append_buffer(@input, @input.length)
    @input = nil
  end
end

#ZSTREAM_READYObject



286
287
288
# File 'lib/pr/zlib/zstream.rb', line 286

def ZSTREAM_READY
  (@flags |= ZSTREAM_FLAG_READY)
end

#zstream_resetObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pr/zlib/zstream.rb', line 162

def zstream_reset
  err = send(@func.reset, @stream)
  if err != Z_OK
    raise_zlib_error(err, @stream.msg)
  end
  @flags = ZSTREAM_FLAG_READY
  @buf = nil
  @buf_filled = 0
  @stream.next_out = 0
  @stream.avail_out = 0
  zstream_reset_input()
end

#zstream_reset_inputObject



141
142
143
# File 'lib/pr/zlib/zstream.rb', line 141

def zstream_reset_input
  @input = nil
end

#zstream_run(src, len, flush) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/pr/zlib/zstream.rb', line 240

def zstream_run(src, len, flush)
  if @input.nil? && len == 0
    @stream.next_in = ''
    @stream.avail_in = 0
  else
    zstream_append_input(src, len)
    @stream.next_in = Bytef.new(@input)
    @stream.avail_in = @input.length
  end
  if @stream.avail_out.zero?
    zstream_expand_buffer()
  end

  loop do
    n = @stream.avail_out
    err = send(@func.run, @stream, flush)
    @buf += n - @stream.avail_out
    if err == Z_STREAM_END
      @flags &= ~ZSTREAM_FLAG_IN_STREAM
      @flags |= ZSTREAM_FLAG_FINISHED
      break
    end
    if err != Z_OK
      if flush != Z_FINISH && err == Z_BUF_ERROR && @stream.avail_out > 0
        @flags |= ZSTREAM_FLAG_IN_STREAM
        break
      end
      @input = nil
      if @stream.avail_in > 0
        zstream_append_input(@stream.next_in, @stream.avail_in)
      end
      raise_zlib_error(err, @stream.msg)
    end
    if @stream.avail_out > 0
      @flags |= ZSTREAM_FLAG_IN_STREAM
      break
    end
    zstream_expand_buffer()
  end

  @input = nil
  if @stream.avail_in > 0
    zstream_append_input(@stream.next_in, @stream.avail_in)
  end
end

#zstream_shift_buffer(len) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pr/zlib/zstream.rb', line 95

def zstream_shift_buffer(len)
  if @buf.offset <= len
    return zstream_detach_buffer()
  end

  dst = @buf.buffer[0, len]
  @buf -= len
  @buf.buffer[0, @buf.offset] = @buf.buffer[len, @buf.offset]
  @stream.next_out = Bytef.new(@buf, @buf.offset)
  @stream.avail_out = @buf.length - @buf.offset
  if @stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX
    @stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX
  end
  dst
end

#zstream_sync(src, len) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/pr/zlib/zstream.rb', line 194

def zstream_sync(src, len)
  if @input
    @stream.next_in = Bytef.new(@input)
    @stream.avail_in = @input.length
    err = inflateSync(@stream)
    if err == Z_OK
      zstream_discard_input(@input.length - @stream.avail_in)
      zstream_append_input(src, len)
      return true
    end
    zstream_reset_input()
    if err != Z_DATA_ERROR
      @stream.next_in.buffer[0, @stream.avail_in]
      raise_zlib_error(err, @stream.msg)
    end
  end

  return false if len <= 0

  @stream.next_in = src
  @stream.avail_in = len
  err = inflateSync(@stream)
  if err == Z_OK
    zstream_append_input(@stream.next_in, @stream.avail_in)
    return true
  end
  if err != Z_DATA_ERROR
    @stream.next_in.buffer[0, @stream.avail_in]
    raise_zlib_error(err, @stream.msg)
  end
  false
end