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
82
83
84
85
86
87
88
89
90
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::*;
#[repr(C)]
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct GUID {
pub data1: u32,
pub data2: u16,
pub data3: u16,
pub data4: [ u8; 8 ],
}
impl GUID {
pub fn parse( guid : &str ) -> Result< GUID, String >
{
enum GuidFormat { Braces, Hyphens, Raw }
let guid_format = match guid.len() {
38 => GuidFormat::Braces,
36 => GuidFormat::Hyphens,
32 => GuidFormat::Raw,
_ => return Err( format!(
"Unrecognized GUID format: '{}' ({})",
guid, guid.len() ) ),
};
let format = match guid_format {
GuidFormat::Braces => vec![
Some( b'{' ), None, None, None, None, None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None, None, None, None, None, None, None, None, None,
Some( b'}' )
],
GuidFormat::Hyphens => vec![
None, None, None, None, None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None,
Some( b'-' ), None, None, None, None, None, None, None, None, None, None, None, None
],
GuidFormat::Raw => vec![
None, None, None, None, None, None, None, None,
None, None, None, None,
None, None, None, None,
None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None
]
};
let mut buffer = [ 0u8; 16 ];
let mut digit = 0;
for ( i_char, chr ) in guid.bytes().enumerate() {
if let Some( b ) = format[ i_char ] {
if chr == b {
continue
} else {
return Err( format!( "Unexpected character in GUID: {}", chr ) );
}
}
let value : u8 = match chr {
b'0'...b'9' => chr - b'0',
b'a'...b'f' => chr - b'a' + 10,
b'A'...b'F' => chr - b'A' + 10,
_ => return Err( format!( "Unrecognized character in GUID: {}", chr ) )
};
let half = digit % 2;
let byte = ( digit - half ) / 2;
if half == 0 {
buffer[ byte ] += value * 16;
} else {
buffer[ byte ] += value;
}
digit += 1;
}
Ok( GUID {
data1:
( ( buffer[ 0 ] as u32 ) << 24 ) +
( ( buffer[ 1 ] as u32 ) << 16 ) +
( ( buffer[ 2 ] as u32 ) << 8 ) +
( ( buffer[ 3 ] as u32 ) << 0 ),
data2:
( ( buffer[ 4 ] as u16 ) << 8 ) +
( ( buffer[ 5 ] as u16 ) << 0 ),
data3:
( ( buffer[ 6 ] as u16 ) << 8 ) +
( ( buffer[ 7 ] as u16 ) << 0 ),
data4: [
buffer[ 8 ], buffer[ 9 ], buffer[ 10 ], buffer[ 11 ],
buffer[ 12 ], buffer[ 13 ], buffer[ 14 ], buffer[ 15 ],
]
} )
}
}
impl std::fmt::Display for GUID {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!( f,
"{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
self.data1,
self.data2,
self.data3,
self.data4[0],
self.data4[1],
self.data4[2],
self.data4[3],
self.data4[4],
self.data4[5],
self.data4[6],
self.data4[7] )
}
}
impl std::fmt::UpperHex for GUID {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!( f,
"{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
self.data1,
self.data2,
self.data3,
self.data4[0],
self.data4[1],
self.data4[2],
self.data4[3],
self.data4[4],
self.data4[5],
self.data4[6],
self.data4[7] )
}
}