Module: RubySMB::Compression::LZNT1
- Defined in:
- lib/ruby_smb/compression/lznt1.rb
Class Method Summary collapse
- .compress(buf, chunk_size: 0x1000) ⇒ Object
- .compress_chunk(chunk) ⇒ Object
- .decompress(buf, length_check: true) ⇒ Object
- .decompress_chunk(chunk) ⇒ Object
Class Method Details
.compress(buf, chunk_size: 0x1000) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ruby_smb/compression/lznt1.rb', line 4 def self.compress(buf, chunk_size: 0x1000) out = '' until buf.empty? chunk = buf[0...chunk_size] compressed = compress_chunk(chunk) # chunk is compressed if compressed.length < chunk.length out << [0xb000 | (compressed.length - 1)].pack('v') out << compressed else out << [0x3000 | (chunk.length - 1)].pack('v') out << chunk end buf = buf[chunk_size..-1] break if buf.nil? end out end |
.compress_chunk(chunk) ⇒ Object
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 |
# File 'lib/ruby_smb/compression/lznt1.rb', line 24 def self.compress_chunk(chunk) blob = chunk out = '' pow2 = 0x10 l_mask3 = 0x1002 o_shift = 12 until blob.empty? bits = 0 tmp = '' i = -1 loop do i += 1 bits >>= 1 while pow2 < (chunk.length - blob.length) pow2 <<= 1 l_mask3 = (l_mask3 >> 1) + 1 o_shift -= 1 end max_len = [blob.length, l_mask3].min offset, length = find(chunk[0...(chunk.length - blob.length)], blob, max_len) # try to find more compressed pattern _offset2, length2 = find(chunk[0...chunk.length - blob.length + 1], blob[1..-1], max_len) length = 0 if length < length2 if length > 0 symbol = ((offset - 1) << o_shift) | (length - 3) tmp << [symbol].pack('v') # set the highest bit bits |= 0x80 blob = blob[length..-1] else tmp += blob[0] blob = blob[1..-1] end break if blob.empty? || i == 7 end out << [bits >> (7 - i)].pack('C') out << tmp end out end |
.decompress(buf, length_check: true) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby_smb/compression/lznt1.rb', line 72 def self.decompress(buf, length_check: true) out = '' until buf.empty? header = buf.unpack1('v') length = (header & 0xfff) + 1 raise EncodingError, 'invalid chunk length' if length_check && length > (buf.length - 2) chunk = buf[2...length + 2] out << if header & 0x8000 == 0 chunk else decompress_chunk(chunk) end buf = buf[length + 2..-1] end out end |
.decompress_chunk(chunk) ⇒ Object
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 |
# File 'lib/ruby_smb/compression/lznt1.rb', line 91 def self.decompress_chunk(chunk) out = '' until chunk.empty? flags = chunk[0].unpack1('C') chunk = chunk[1..-1] 8.times do |i| if (flags >> i & 1) == 0 out << chunk[0] chunk = chunk[1..-1] else flag = chunk.unpack1('v') pos = out.length - 1 l_mask = 0xfff o_shift = 12 while pos >= 0x10 l_mask >>= 1 o_shift -= 1 pos >>= 1 end length = (flag & l_mask) + 3 offset = (flag >> o_shift) + 1 if length >= offset out_offset = out[-offset..-1] tmp = out_offset * (0xfff / out_offset.length + 1) out << tmp[0...length] else out << out[-offset..-offset + length - 1] end chunk = chunk[2..-1] end break if chunk.empty? end end out end |