Coverage for /root/GitHubProjects/impacket/impacket/uuid.py : 84%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
2#
3# This software is provided under under a slightly modified version
4# of the Apache Software License. See the accompanying LICENSE file
5# for more information.
6#
7# Description:
8# Generate UUID compliant with http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt.
9# A different, much simpler (not necessarily better) algorithm is used.
10#
11# Author:
12# Javier Kohen (jkohen)
13#
14from __future__ import absolute_import
15from __future__ import print_function
16import re
17import binascii
19from random import randrange
20from struct import pack, unpack
22EMPTY_UUID = b'\x00'*16
24def generate():
25 # UHm... crappy Python has an maximum integer of 2**31-1.
26 top = (1<<31)-1
27 return pack("IIII", randrange(top), randrange(top), randrange(top), randrange(top))
29def bin_to_string(uuid):
30 uuid1, uuid2, uuid3 = unpack('<LHH', uuid[:8])
31 uuid4, uuid5, uuid6 = unpack('>HHL', uuid[8:16])
32 return '%08X-%04X-%04X-%04X-%04X%08X' % (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6)
34def string_to_bin(uuid):
35 # If a UUID in the 00000000000000000000000000000000 format, let's return bytes as is
36 if '-' not in uuid: 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true
37 return binascii.unhexlify(uuid)
39 # If a UUID in the 00000000-0000-0000-0000-000000000000 format, parse it as Variant 2 UUID
40 # The first three components of the UUID are little-endian, and the last two are big-endian
41 matches = re.match('([\dA-Fa-f]{8})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})([\dA-Fa-f]{8})', uuid)
42 (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) = [int(x, 16) for x in matches.groups()]
43 uuid = pack('<LHH', uuid1, uuid2, uuid3)
44 uuid += pack('>HHL', uuid4, uuid5, uuid6)
45 return uuid
47def stringver_to_bin(s):
48 (maj,min) = s.split('.')
49 return pack('<H',int(maj)) + pack('<H',int(min))
51def uuidtup_to_bin(tup):
52 if len(tup) != 2: 52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true
53 return
54 return string_to_bin(tup[0]) + stringver_to_bin(tup[1])
56def bin_to_uuidtup(bin):
57 assert len(bin) == 20
58 uuidstr = bin_to_string(bin[:16])
59 maj, min = unpack("<HH", bin[16:])
60 return uuidstr, "%d.%d" % (maj, min)
62#input: string
63#output: tuple (uuid,version)
64#if version is not found in the input string "1.0" is returned
65#example:
66# "00000000-0000-0000-0000-000000000000 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
67# "10000000-2000-3000-4000-500000000000 version 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
68# "10000000-2000-3000-4000-500000000000 v 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
69# "10000000-2000-3000-4000-500000000000" returns ('00000000-0000-0000-0000-000000000000','1.0')
70def string_to_uuidtup(s):
71 g = re.search("([A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}).*?([0-9]{1,5}\.[0-9]{1,5})",s+" 1.0")
72 if g: 72 ↛ 75line 72 didn't jump to line 75, because the condition on line 72 was never false
73 (u,v) = g.groups()
74 return (u,v)
75 return
77def uuidtup_to_string(tup):
78 uuid, (maj, min) = tup
79 return "%s v%d.%d" % (uuid, maj, min)