/usr/lib/ruby/1.8/complearn/proto.rb is in libcomplearn-ruby1.8 1.0.8-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | require 'matrix'
module CompLearn
class UNCD
NCDCOMMAND="ncd -S"
def self.cp()
unless defined? @@cp
begin
pipe = IO.popen(NCDCOMMAND, "r+")
rescue
raise "Cannot execute #{NCDCOMMAND}, (please download from http://complearn.org/ ), exitting"
exit 1
end
@@cp = CommunicationProtocol.new(pipe, pipe)
@@cp.runcmd(:compressor_list)
end
@@cp
end
end
class CommunicationProtocol
class PositionIndicator
def initialize(inppkt)
@pkt = inppkt
@offset = 0
end
def eatchar(howmany=1)
result = @pkt[@offset, howmany]
@offset += howmany
raise "Advanced too far in packet." unless result.size == howmany
result
end
end
def decode_boolean(pi) ((decode_integer(pi) == 0)? false : true) end
def decode_integer(pi) pi.eatchar(4).unpack("N")[0] end
def decode_double(pi) decode_string(pi).to_f end
def decode_vector(pi) len = decode_integer(pi)
res = [ ]
len.times { res << decode_double(pi) }
Vector[*res]
end
def decode_string(pi) unwrap_packet(pi) end
def decode_pointer(pi) decode_string(pi) end
def encode_string(inpstr) wrap_packet(inpstr) end
def encode_boolean(t) encode_integer(t ? 1 : 0) end
def encode_integer(i) [i].pack("N") end
def encode_double(i) encode_string(i.to_s) end
def encode_pointer(i) encode_string(i.to_s) end
def encode_cdc(cdc) encode_intarray(cdc) end
def encode_cdcseq(cdcseq) res = [ ]
res << encode_integer(cdcseq.size)
cdcseq.each { |i| res << encode_cdc(i) }
res.join('')
end
def encode_intarray(a)
r = [ ]
r << encode_integer(a.size)
a.each { |j| r << encode_integer(j.to_i) }
r.join('')
end
def encode_matspec(ums)
ms = ums.to_s
if ms =~ /full/i
return encode_string('full')
end
if ms =~ /bigx/i
return encode_string('bigx')
end
if ms =~ /bigy/i
return encode_string('bigy')
end
raise "Illegal matspec #{ums.inspect}"
end
def decode_matrix(pi)
size1 = decode_integer(pi)
size2 = decode_integer(pi)
m = [ ]
size1.times { |i|
m << [ ]
size2.times { |j|
m[-1] << decode_double(pi)
}
}
Matrix[*m]
end
def initialize(inpstream, outstream)
@inp = inpstream
@out = outstream
end
def runcmd(cmd, params = '')
raise "illegal command #{cmd}" unless respond_to? "do_response_#{cmd}"
return nil if @out.closed?
testpacket = encode_string(cmd.to_s)
tosend = wrap_packet(testpacket + params)
# STDERR.puts "Sending command packet: <#{tosend}>"
begin
@out.write(tosend)
rescue Errno::EPIPE
STDERR.puts "runcmd: compression server died, exitting"
@out.close
exit 1
end
result = send("do_response_#{cmd.to_s}",PositionIndicator.new(eat_packet))
# puts "Got result for #{cmd}: #{result.inspect}"
result
end
def wrap_packet(inpstr) [inpstr.size].pack("N") + inpstr end
def eat_packet()
pheader = @inp.read(4)
unless pheader
STDERR.puts "eat_packet: compression server died, exitting"
@inp.close
exit 1
end
sz = pheader.unpack("N")[0]
raise "bad size" unless sz >= 0
pkt = @inp.read(sz)
pkt
end
def unwrap_packet(pi)
sz = pi.eatchar(4).unpack("N")[0]
raise "bad size" unless sz >= 0
pi.eatchar(sz)
end
end
end
require "complearn/cmdresp"
module CompLearn
class RealCompressor
private
def make_finalizer()
proc { |id| @cp.runcmd(:free_compressor,@cp.encode_pointer(@objhandle)) }
end
def make_simplecmd(*args)
r = [ ]
sym = args.shift
r << @cp.encode_pointer(@objhandle)
args.each { |i|
if (i.kind_of?(String))
r << @cp.encode_string(i)
else
if (i.kind_of?(Integer) || i.kind_of?(Fixnum))
r << @cp.encode_integer(i)
else
raise "Unkown object #{i.inspect}"
end
end
}
# puts "Running command: #{sym}(#{r.join(', ')})"
@cp.runcmd(sym, r.join(''))
end
public
def initialize(compname = nil, objhandle = nil)
compname = "bzlib" if compname.nil?
@cp = UNCD.cp
@objhandle=objhandle||@cp.runcmd(:new_compressor,@cp.encode_string(compname))
ObjectSpace.define_finalizer(self, make_finalizer)
end
def self.list() UNCD.cp.runcmd(:compressor_list) end
def self.compressor_list() list() end
def compress(str) make_simplecmd(:compress, str.to_s) end
def decompress(str) make_simplecmd(:decompress, str.to_s) end
def blurb() make_simplecmd(:blurb) end
def canonical_extension() make_simplecmd(:canonical_extension) end
def name() make_simplecmd(:name) end
def compressor_version() make_simplecmd(:compressor_version) end
def binding_version() make_simplecmd(:binding_version) end
def is_threadsafe() make_simplecmd(:is_threadsafe) end
def is_decompressible(str) make_simplecmd(:is_decompressible, str.to_s) end
def is_just_size() make_simplecmd(:is_just_size) end
def is_operational() make_simplecmd(:is_operational) end
def is_hash_function() make_simplecmd(:is_hash_function) end
def hash(str) make_simplecmd(:hash, str.to_s) end
def is_hash_function() make_simplecmd(:is_hash_function) end
def window_size() make_simplecmd(:window_size) end
def compressed_size(str) make_simplecmd(:compressed_size, str.to_s) end
def clone()
oh = make_simplecmd(:clone, @objhandle.to_s)
self.class.new(nil, oh)
end
def is_private_property(str)
make_simplecmd(:is_private_property, str.to_s)
end
def driver()
CompressorDriver.new(self)
end
def oh() @objhandle end
end
class CompressorDriver
private
def make_finalizer()
proc { |id| @cp.runcmd(:free_compressordriver,@cp.encode_pointer(@objhandle)) }
end
public
def initialize(rc)
@cp = UNCD.cp
@objhandle = @cp.runcmd(:new_compressordriver,@cp.encode_pointer(rc.oh))
ObjectSpace.define_finalizer(self, make_finalizer)
end
def make_simplecmd(*args)
r = [ ]
sym = ("cd_" + args.shift.to_s).to_sym
r << @cp.encode_pointer(@objhandle)
args.each { |i|
if (i.kind_of?(String))
r << @cp.encode_string(i)
else
if (i.kind_of?(Integer) || i.kind_of?(Fixnum))
r << @cp.encode_integer(i)
else
raise "Unkown object #{i.inspect}"
end
end
}
# puts "Running command: #{sym}(#{r.join(', ')})"
@cp.runcmd(sym, r.join(''))
end
def store(str) make_simplecmd(:store, str.to_s) end
def size() make_simplecmd(:size) end
def compression_vector() make_simplecmd(:compression_vector) end
def compress_single(i) make_simplecmd(:compress_single, i.to_i) end
def compress_pair(i,j) make_simplecmd(:compress_pair, i.to_i,j.to_i) end
def compression_matrix(dim1ind, dim2ind, matspec = :full)
r = [ ]
r << @cp.encode_pointer(@objhandle)
r << @cp.encode_intarray(dim1ind)
r << @cp.encode_intarray(dim2ind)
r << @cp.encode_matspec(matspec)
@cp.runcmd(:cd_compression_matrix, r.join(''))
end
def compression_sequence(compseq)
r = [ ]
r << @cp.encode_pointer(@objhandle)
r << @cp.encode_cdcseq(compseq)
@cp.runcmd(:cd_compression_sequence, r.join(''))
end
end
end
|