-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshtype.py
81 lines (58 loc) · 1.79 KB
/
sshtype.py
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
# Copyright (c) 2014-2015 Sam Maloney.
# License: LGPL
import struct
import logging
import llog
import putil
log = logging.getLogger(__name__)
def parseNameList(buf):
return parseString(buf)
def parse_string_from(buf, i):
l, v = parse_binary_from(buf, i)
return l, v.decode()
def parseString(buf):
l, v = parseBinary(buf)
return l, v.decode()
def parse_binary_from(buf, i):
length = struct.unpack_from(">L", buf, i)[0]
start = i + 4
end = start + length
value = buf[start:end]
return end, value
def parseBinary(buf):
length = struct.unpack(">L", buf[0:4])[0]
if log.isEnabledFor(logging.DEBUG):
log.debug("length={}".format(length))
value = buf[4:4 + length]
return length + 4, value
def parse_mpint_from(buf, i):
length = struct.unpack_from(">L", buf, i)[0]
if log.isEnabledFor(logging.DEBUG):
log.debug("length={}".format(length))
start = i + 4
end = start + length
value = putil.inflate_long(buf[start:end])
return end, value
def parseMpint(buf):
length = struct.unpack(">L", buf[0:4])[0]
if log.isEnabledFor(logging.DEBUG):
log.debug("length={}".format(length))
return length + 4, putil.inflate_long(buf[4:4+length])
def encodeMpint(val):
buf = putil.deflate_long(val)
length = struct.pack(">L", len(buf))
return length + buf
def encodeNameList(val):
return encodeString(val)
def encodeString(val):
# if log.isEnabledFor(logging.DEBUG):
# log.debug("type=[{}].".format(type(val)))
# if isinstance(val, bytes) or isinstance(val, bytearray):
# buf = val
# else:
buf = val.encode(encoding="UTF-8")
length = struct.pack(">L", len(buf))
return length + buf
def encodeBinary(buf):
length = struct.pack(">L", len(buf))
return length + buf