File: Inflate.pm

package info (click to toggle)
tiarra 20100212-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 1,712
  • sloc: perl: 32,032; lisp: 193; sh: 109; makefile: 10
file content (132 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download | duplicates (4)
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
# -*- cperl -*-
# -----------------------------------------------------------------------------
# $Id: Inflate.pm 3009 2007-12-10 13:35:53Z topia $
# -----------------------------------------------------------------------------
# copyright (C) 2003 Topia <topia@clovery.jp>. all rights reserved.
package System::Inflate;
use strict;
use warnings;
use Carp;

use Tiarra::SharedMixin;
our $_shared_instance;

sub _new {
  my ($class) = @_;
  my $obj = 
    {
     datas => {}, # HASH<HASH*>; HASH key is [tag]
     # compressor: stream compressor
     # stream    : stream object
     # lasterr   : last error information
     # data      : compressor dependent datas
     compressor => {}, # HASH; compressor name => Process Classes
     const => 
     {
      COMP_OK => 0,
      COMP_STREAM_END => 1,
      COMP_DATA_ERR => -1,
      COMP_OTHER_ERR => -2
     }
    };
  bless $obj,$class;

  return $obj->_setup();
}

sub _setup {
  my ($this) = @_;

  foreach my $classname (map {'System::Inflate::' . $_} qw(Zlib Gzip)) {
    eval 'use ' . $classname;
    unless ($@) {
      eval $classname . '::setup(new ' . $classname . '(), $this)';
      if ($@) {
	print "------can't load $classname\n$@------\n";
      }
    } else {
      print "------can't load $classname\n$@------\n";
    }
  }

  return $this;
}

sub get_compclass {
  my ($this, $compressor) = @_;
  my ($compclass) = $this->shared->{compressor}->{$compressor};

  croak('compressor ' . $compressor . ' is not initialized!') unless defined $compclass;

  return $compclass
}

sub get_data_struct {
  my ($this, $tag) = @_;
  my ($datas) = $this->shared->{datas}->{$tag};

  croak('tag ' . $tag . ' is not initialized!') unless defined $datas;

  return $datas;
}

sub get_lasterr {
  my ($this, $tag) = @_;
  return $this->get_data_struct($tag)->{lasterr};
}

sub COMP_OK {
  return shift->{const}->{COMP_OK};
}

sub COMP_STREAM_END {
  return shift->{const}->{COMP_STREAM_END};
}

sub COMP_OTHER_ERR {
  return shift->{const}->{COMP_OTHER_ERR};
}

sub COMP_DATA_ERR {
  return shift->{const}->{COMP_DATA_ERR};
}

sub init {
  my ($this, $tag, $compressor, $data_chunk) = @_;
  my ($datas) = $this->shared->{datas}->{$tag} = 
    {
     stream => undef,
     compressor => $compressor,
     lasterr => undef,
     data => {}
    };

  return $this->get_compclass($compressor)->init($datas, $data_chunk);
}

sub inflate {
  my ($this, $tag, $data_chunk) = @_;
  my ($datas) = $this->get_data_struct($tag);

  return $this->get_compclass($datas->{compressor})->inflate($datas, $data_chunk);
}

sub check {
  my ($this, $tag) = @_;
  my ($datas) = $this->get_data_struct($tag);

  return $this->get_compclass($datas->{compressor})->check($datas);
}

sub final {
  my ($this, $tag) = @_;
  my ($datas) = $this->get_data_struct($tag);

  # ordinary void function
  my $ret = $this->shared->{compressor}->{$datas->{compressor}}->final($datas);
  return undef unless defined $ret; # return value is undef; maybe can't finalize....
  delete $this->shared->{datas}->{$tag};
  return $ret;
}

1;