CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rbmysql/packet.rb
Views: 11766
1
# coding: ascii-8bit
2
class RbMysql
3
class Packet
4
# convert Numeric to LengthCodedBinary
5
def self.lcb(num)
6
return "\xfb" if num.nil?
7
return [num].pack("C") if num < 251
8
return [252, num].pack("Cv") if num < 65536
9
return [253, num&0xffff, num>>16].pack("CvC") if num < 16777216
10
return [254, num&0xffffffff, num>>32].pack("CVV")
11
end
12
13
# convert String to LengthCodedString
14
def self.lcs(str)
15
str = Charset.to_binary str.dup
16
lcb(str.length)+str
17
end
18
19
def initialize(data)
20
@data = data
21
end
22
23
def lcb
24
return nil if @data.empty?
25
case v = utiny
26
when 0xfb
27
return nil
28
when 0xfc
29
return ushort
30
when 0xfd
31
c, v = utiny, ushort
32
return (v << 8)+c
33
when 0xfe
34
v1, v2 = ulong, ulong
35
return (v2 << 32)+v1
36
else
37
return v
38
end
39
end
40
41
def lcs
42
len = self.lcb
43
return nil unless len
44
@data.slice!(0, len)
45
end
46
47
def read(len)
48
@data.slice!(0, len)
49
end
50
51
def string
52
str = @data.unpack('Z*').first
53
@data.slice!(0, str.length+1)
54
str
55
end
56
57
def utiny
58
@data.slice!(0, 1).unpack('C').first
59
end
60
61
def ushort
62
@data.slice!(0, 2).unpack('v').first
63
end
64
65
def ulong
66
@data.slice!(0, 4).unpack('V').first
67
end
68
69
def eof?
70
@data[0] == ?\xfe && @data.length == 5
71
end
72
73
def to_s
74
@data
75
end
76
77
end
78
end
79