15
15
# limitations under the License.
17
17
# Requires Python 2.4+ and Openssl 1.0+
23
def FindFirstNode(xmlDoc, selector):
24
nodes = FindAllNodes(xmlDoc, selector)
23
import xml.dom.minidom as minidom
25
from distutils.version import LooseVersion
27
def parse_doc(xml_text):
29
Parse xml document from string
31
#The minidom lib has some issue with unicode in python2.
32
#Encode the string into utf-8 first
33
xml_text = xml_text.encode('utf-8')
34
return minidom.parseString(xml_text)
36
def findall(root, tag, namespace=None):
38
Get all nodes by tag and namespace under Node root.
44
return root.getElementsByTagName(tag)
46
return root.getElementsByTagNameNS(namespace, tag)
48
def find(root, tag, namespace=None):
50
Get first node by tag and namespace under Node root.
52
nodes = findall(root, tag, namespace=namespace)
53
if nodes is not None and len(nodes) >= 1:
28
def FindAllNodes(xmlDoc, selector):
29
nodes = xmlDoc.findall(selector)
32
def GetNodeTextData(a):
34
Filter non-text nodes from DOM tree
36
for b in a.childNodes:
37
if b.nodeType == b.TEXT_NODE:
40
def Unpack(buf, offset, range):
65
for child in node.childNodes:
66
if child.nodeType == child.TEXT_NODE:
70
def findtext(root, tag, namespace=None):
72
Get text of node by tag and namespace under Node root.
74
node = find(root, tag, namespace=namespace)
77
def getattrib(node, attr_name):
79
Get attribute of xml node
82
return node.getAttribute(attr_name)
86
def unpack(buf, offset, range):
42
88
Unpack bytes into python values.
46
result = (result << 8) | Ord(buf[offset + i])
92
result = (result << 8) | str_to_ord(buf[offset + i])
49
def UnpackLittleEndian(buf, offset, length):
95
def unpack_little_endian(buf, offset, length):
51
97
Unpack little endian bytes into python values.
53
return Unpack(buf, offset, list(range(length - 1, -1, -1)))
99
return unpack(buf, offset, list(range(length - 1, -1, -1)))
55
def UnpackBigEndian(buf, offset, length):
101
def unpack_big_endian(buf, offset, length):
57
103
Unpack big endian bytes into python values.
59
return Unpack(buf, offset, list(range(0, length)))
105
return unpack(buf, offset, list(range(0, length)))
61
def HexDump3(buf, offset, length):
107
def hex_dump3(buf, offset, length):
63
109
Dump range of buf in formatted hex.
65
return ''.join(['%02X' % Ord(char) for char in buf[offset:offset + length]])
111
return ''.join(['%02X' % str_to_ord(char) for char in buf[offset:offset + length]])
69
115
Dump buf in formatted hex.
71
return HexDump3(buf, 0, len(buf))
117
return hex_dump3(buf, 0, len(buf))
73
def IsInRangeInclusive(a, low, high):
119
def is_in_range(a, low, high):
75
121
Return True if 'a' in 'low' <= a >= 'high'
77
123
return (a >= low and a <= high)
125
def is_printable(ch):
81
127
Return True if character is displayable.
83
return (IsInRangeInclusive(ch, Ord('A'), Ord('Z'))
84
or IsInRangeInclusive(ch, Ord('a'), Ord('z'))
85
or IsInRangeInclusive(ch, Ord('0'), Ord('9')))
129
return (is_in_range(ch, str_to_ord('A'), str_to_ord('Z'))
130
or is_in_range(ch, str_to_ord('a'), str_to_ord('z'))
131
or is_in_range(ch, str_to_ord('0'), str_to_ord('9')))
87
def HexDump(buffer, size):
133
def hex_dump(buffer, size):
89
135
Return Hex formated dump of a 'buffer' of 'size'.
111
157
for j in range(i - (i % 16), i + 1):
113
159
if type(byte) == str:
114
byte = ord(byte.decode('latin1'))
160
byte = str_to_ord(byte.decode('latin1'))
116
if IsPrintable(byte):
162
if is_printable(byte):
119
165
if (i + 1) != size:
125
171
Allows indexing into a string or an array of integers transparently.
126
172
Generic utility function.
128
if type(a) == type("a"):
174
if type(a) == type(b'') or type(a) == type(u''):
132
def CompareBytes(a, b, start, length):
178
def compare_bytes(a, b, start, length):
133
179
for offset in range(start, start + length):
134
if Ord(a[offset]) != Ord(b[offset]):
180
if str_to_ord(a[offset]) != str_to_ord(b[offset]):
138
def IntegerToIpAddressV4String(a):
184
def int_to_ip4_addr(a):
140
186
Build DHCP request string.
142
return "%u.%u.%u.%u" % ((a >> 24) & 0xFF,
188
return "%u.%u.%u.%u" % ((a >> 24) & 0xFF,
150
uni = unicode(val, 'utf-8', errors='ignore')
154
raise ValueError('<Unsupported charset>')
156
return uni.encode('ascii', 'backslashreplace')
158
def HexStringToByteArray(a):
193
def hexstr_to_bytearray(a):
160
195
Return hex string packed into a binary struct.
177
212
config.insert(i, "{0} {1}".format(name, val))
181
if ord(c[0]) > 128 and ord(c[1]) > 128 and ord(c[2]) > 128:
216
if str_to_ord(c[0]) > 128 and str_to_ord(c[1]) > 128 and \
217
str_to_ord(c[2]) > 128:
185
def GetPasswordHash(password, useSalt, saltType, saltLength):
188
collection = string.ascii_letters + string.digits
189
salt = ''.join(random.choice(collection) for _ in range(saltLength))
190
salt = "${0}${1}".format(saltType, salt)
191
return crypt.crypt(password, salt)
193
def NumberToBytes(i):
195
Pack number into bytes. Retun as string.
199
result.append(chr(i & 0xFF))
202
return ''.join(result)
206
Return string representation of bits in a.
212
c = c | (bit << index)
215
s = s + struct.pack('>B', c)
221
def gen_password_hash(password, crypt_id, salt_len):
222
collection = string.ascii_letters + string.digits
223
salt = ''.join(random.choice(collection) for _ in range(salt_len))
224
salt = "${0}${1}".format(crypt_id, salt)
225
return crypt.crypt(password, salt)
227
Version = LooseVersion