Class: Zlib::ZStream

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

Direct Known Subclasses

Deflate, Inflate

Constant Summary collapse

@@final =
proc do |z|
  proc do
    if z && z.ZSTREAM_IS_READY()
      err = send(z.func.end, z.stream)
      if (err == Z_STREAM_ERROR)
        warn("the stream state was inconsistent.")
      end
      if (err == Z_DATA_ERROR)
        warn("the stream was freed prematurely.")
      end
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZStream

Returns a new instance of ZStream.



497
498
499
500
# File 'lib/pr/zlib.rb', line 497

def initialize
  @z = nil
  ObjectSpace.define_finalizer self, @@final.call(@z)
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



87
88
89
# File 'lib/pr/zlib.rb', line 87

def buf
  @buf
end

#flagsObject

Returns the value of attribute flags.



87
88
89
# File 'lib/pr/zlib.rb', line 87

def flags
  @flags
end

#funcObject

Returns the value of attribute func.



87
88
89
# File 'lib/pr/zlib.rb', line 87

def func
  @func
end

#inputObject

Returns the value of attribute input.



87
88
89
# File 'lib/pr/zlib.rb', line 87

def input
  @input
end

#streamObject

Returns the value of attribute stream.



87
88
89
# File 'lib/pr/zlib.rb', line 87

def stream
  @stream
end

#zObject (readonly)

Returns the value of attribute z.



401
402
403
# File 'lib/pr/zlib.rb', line 401

def z
  @z
end

Instance Method Details

#adlerObject



439
440
441
# File 'lib/pr/zlib.rb', line 439

def adler
  @z.stream.adler
end

#avail_inObject



421
422
423
# File 'lib/pr/zlib.rb', line 421

def avail_in
  @z.input.nil? ? 0 : @z.input.length
end

#avail_outObject



403
404
405
# File 'lib/pr/zlib.rb', line 403

def avail_out()
  @z.stream.avail_out
end

#avail_out=(size) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/pr/zlib.rb', line 407

def avail_out=(size)
  if @z.buf.nil?
    @z.buf = Bytef.new(0.chr * size)
    @z.stream.next_out = Bytef.new(@z.buf)
    @z.stream.avail_out = size
  elsif @z.stream.avail_out != size
    if @z.buf.offset + size > @z.buf.length
      @z.buf.buffer << 0.chr * (@z.buf.offset + size - @z.buf.length)
    end
    @z.stream.next_out = Bytef.new(@z.buf,@z.buf.offset)
    @z.stream.avail_out = size
  end
end

#closeObject Also known as: end



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/pr/zlib.rb', line 453

def close()
  if (!@z.ZSTREAM_IS_READY())
     warn("attempt to close uninitialized zstream ignored.")
     return nil
  end
  if (@z.flags & ZSTREAM_FLAG_IN_STREAM).nonzero?
     warn("attempt to close unfinished zstream reset forced.")
     @z.input = nil
  end

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

#closed?Boolean Also known as: ended?

Returns:

  • (Boolean)


448
449
450
# File 'lib/pr/zlib.rb', line 448

def closed?
  @z.ZSTREAM_IS_READY()
end

#data_typeObject



435
436
437
# File 'lib/pr/zlib.rb', line 435

def data_type
  @z.stream.data_type
end

#finishObject



484
485
486
487
# File 'lib/pr/zlib.rb', line 484

def finish()
  @z.zstream_run("", 0, Z_FINISH)
  @z.zstream_detach_buffer()
end

#finished?Boolean Also known as: stream_end?

Returns:

  • (Boolean)


443
444
445
# File 'lib/pr/zlib.rb', line 443

def finished?
  @z.ZSTREAM_IS_FINISHED()
end

#flush_next_inObject



489
490
491
# File 'lib/pr/zlib.rb', line 489

def flush_next_in
  @z.zstream_detach_input
end

#flush_next_outObject



493
494
495
# File 'lib/pr/zlib.rb', line 493

def flush_next_out
  @z.zstream_detach_buffer
end

#raise_zlib_error(err, msg) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pr/zlib.rb', line 89

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

#resetObject



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/pr/zlib.rb', line 472

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

#total_inObject

Raises:



425
426
427
428
# File 'lib/pr/zlib.rb', line 425

def total_in
  raise GzipFile::Error,"closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @z.stream.total_in
end

#total_outObject

Raises:



430
431
432
433
# File 'lib/pr/zlib.rb', line 430

def total_out
  raise GzipFile::Error,"closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @z.stream.total_out
end

#zstream_append_buffer(src, len) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pr/zlib.rb', line 138

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



203
204
205
206
207
208
209
210
211
# File 'lib/pr/zlib.rb', line 203

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



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/pr/zlib.rb', line 191

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



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

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

  return dst
end

#zstream_detach_inputObject



232
233
234
235
236
237
238
239
240
# File 'lib/pr/zlib.rb', line 232

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

#zstream_discard_input(len) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/pr/zlib.rb', line 213

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

#zstream_endObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/pr/zlib.rb', line 255

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
  return nil
end

#zstream_expand_bufferObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pr/zlib.rb', line 114

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



307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/pr/zlib.rb', line 307

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



380
381
382
# File 'lib/pr/zlib.rb', line 380

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

#ZSTREAM_IS_FINISHEDObject



376
377
378
# File 'lib/pr/zlib.rb', line 376

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

#ZSTREAM_IS_READYObject



372
373
374
# File 'lib/pr/zlib.rb', line 372

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

#zstream_passthrough_inputObject



225
226
227
228
229
230
# File 'lib/pr/zlib.rb', line 225

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

#ZSTREAM_READYObject



368
369
370
# File 'lib/pr/zlib.rb', line 368

def ZSTREAM_READY()
  (@flags |= ZSTREAM_FLAG_READY)
end

#zstream_resetObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/pr/zlib.rb', line 242

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



221
222
223
# File 'lib/pr/zlib.rb', line 221

def zstream_reset_input()
  @input = nil
end

#zstream_run(src, len, flush) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/pr/zlib.rb', line 320

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
    guard = @input
  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)
    guard = nil
  end
end

#zstream_shift_buffer(len) ⇒ Object



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

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
  return dst
end

#zstream_sync(src, len) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/pr/zlib.rb', line 274

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)
       rest = @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)
   rest = @stream.next_in.buffer[0,@stream.avail_in]
   raise_zlib_error(err, @stream.msg)
  end
  return false
end