@@ -106,6 +106,7 @@ module Cipher
106
106
107
107
# @return [Boolean] Compression enabled internally
108
108
attr_reader :compression_enabled
109
+ attr_reader :compression_configured
109
110
110
111
# @return [Boolean] Encryption enabled internally
111
112
attr_reader :encryption_enabled
@@ -181,7 +182,7 @@ def connect(handshake = true)
181
182
end
182
183
183
184
# create a new socket for connection
184
- @socket = Socket ::Socket . new ( :INET , :STREAM )
185
+ @socket = Socket ::Socket . new ( :INET , :STREAM )
185
186
remote_addr = Socket ::Socket . pack_sockaddr_in ( @port , @hostname )
186
187
187
188
@socket . set_encoding ( "ASCII-8BIT" )
@@ -207,6 +208,11 @@ def connect(handshake = true)
207
208
return false
208
209
end
209
210
211
+ # configure compression
212
+ @compression_enabled = true if @compression_configured
213
+
214
+ # configure checksum
215
+
210
216
# perform the Wired key exange
211
217
if !connect_key_exange ( nil , nil )
212
218
Wired ::Log . error "Handshake failed to #{ @hostname } :#{ @port } "
@@ -275,16 +281,16 @@ def read
275
281
276
282
if binary . length > 0
277
283
# decompress data
278
- if @compression_enabled
279
- binary = inflate_data ( binary )
280
- end
284
+ if @compression_enabled
285
+ binary = inflate_data ( binary )
286
+ end
281
287
282
- # decrypt data
283
- if @encryption_enabled
284
- binary = @ssl_cipher . decrypt ( binary )
285
- end
288
+ # decrypt data
289
+ if @encryption_enabled
290
+ binary = @ssl_cipher . decrypt ( binary )
291
+ end
286
292
287
- # create message with binary data
293
+ # create message with binary data
288
294
message = Wired ::Message . new ( :spec => @spec , :binary => binary )
289
295
290
296
# internally handle the message (logging, errors, etc.)
@@ -428,15 +434,15 @@ def connect_handshake?
428
434
# get socket options
429
435
if ( @serialization == Wired ::Socket ::Serialization ::BINARY )
430
436
if ( message . parameter ( "p7.handshake.compression" ) == 0 )
431
- @compression_enabled = true
437
+ @compression_configured = true
432
438
end
433
439
434
440
if ( message . parameter ( "p7.handshake.encryption" ) != nil )
435
441
@cipher = message . parameter ( "p7.handshake.encryption" ) . to_i
436
442
end
437
443
end
438
444
439
- puts "@compression_enabled : #{ @compression_enabled } "
445
+ puts "@compression_configured : #{ @compression_configured } "
440
446
441
447
if ( message . parameter ( "p7.handshake.compatibility_check" ) == false )
442
448
@remote_compatibility_check = false
@@ -579,8 +585,6 @@ def send_compatibilit_check
579
585
580
586
581
587
def handle_message ( message )
582
- return if !message
583
-
584
588
Wired ::Log . debug "Received Message: " + message . name
585
589
handle_error message if message . name == "wired.error"
586
590
end
@@ -590,7 +594,7 @@ def handle_message(message)
590
594
591
595
def handle_error ( message )
592
596
Wired ::Log . error "Wired Error: " + message . to_s
593
- @errors . push message
597
+ @errors . push message
594
598
end
595
599
596
600
@@ -630,7 +634,35 @@ def deflate_data(data)
630
634
631
635
632
636
def inflate_data ( data )
633
- #return Zlib::Inflate.inflate(data, Zlib::DEFAULT_COMPRESSION)
637
+ puts "inflate_data"
638
+ puts "compressed_data : " + data . unpack ( "H*" ) . to_s
639
+
640
+ bytes = nil
641
+ c_stream = Z_stream . new
642
+ c_stream . data_type = Z_UNKNOWN
643
+
644
+ data = Bytef . new ( data )
645
+ data_length = data . length
646
+
647
+ ( 2 ..16 ) . step ( 1 ) do |multiple |
648
+ compressed_length = data_length * ( 1 << multiple ) ;
649
+ compressed_data = Bytef . new ( 0 . chr * compressed_length )
650
+
651
+ c_stream . next_in = data
652
+ c_stream . avail_in = data . length
653
+ c_stream . next_out = compressed_data
654
+ c_stream . avail_out = compressed_length
655
+
656
+ err = inflate ( c_stream , Z_FINISH )
657
+ bytes = compressed_data . buffer
658
+ errend = deflateEnd ( c_stream )
659
+
660
+ puts "data : " + bytes . unpack ( "H*" ) . to_s
661
+
662
+ break if err == Z_STREAM_END and enderr != Z_BUF_ERROR
663
+ end
664
+
665
+ return bytes
634
666
end
635
667
636
668
0 commit comments