/
guid.rs
384 lines (331 loc) · 12.4 KB
/
guid.rs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// <3 winapi
// (Re-defining these here as not to pull the whole winapi as runtime dependency)
/// Binary GUID format as defined for the COM interfaces.
#[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 zero_guid() -> GUID {
GUID { data1: 0, data2: 0, data3: 0, data4: [ 0; 8 ] }
}
/// Parses the given string as a GUID.
///
/// Supported formats include:
///
/// - Braces and hyphens: {00000000-0000-0000-0000-000000000000}
/// - Hyphens only: 00000000-0000-0000-0000-000000000000
/// - Raw hexadecimal: 00000000000000000000000000000000
pub fn parse( guid : &str ) -> Result< GUID, String >
{
// We support the following formats:
//
// - {00000000-0000-0000-0000-000000000000} (38 chars)
// - 00000000-0000-0000-0000-000000000000 (36 chars)
// - 00000000000000000000000000000000 (32 chars)
//
// Use the total length to make the assumption on the format.
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() ) ),
};
// Define the format requirements.
//
// Some(_) values correspond to required characters while None values
// correspond to the hexadecimal digits themselves.
#[rustfmt::skip]
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
]
};
// Read the hexadecimal values into a buffer.
let mut buffer = [ 0u8; 16 ];
let mut digit = 0;
for ( i_char, chr ) in guid.bytes().enumerate() {
// If this is a fixed character, ensure we have the correct one.
// If we had the correct char, continue onto next character,
// otherwise error out. In any case we'll go past this guard
// only if we're expeting a hexadecimal-digit.
if let Some( b ) = format[ i_char ] {
if chr == b {
continue
} else {
return Err( format!( "Unexpected character in GUID: {}", chr ) );
}
}
// Convert the hexadecimal character into a numerical value.
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 ) )
};
// Each digit corresponds to one half of a byte in the final [u8]
// buffer. Resolve which half and which byte this is.
let half = digit % 2;
let byte = ( digit - half ) / 2;
// Finally add the value into the buffer and proceed to the next
// digit.
if half == 0 {
buffer[ byte ] += value * 16;
} else {
buffer[ byte ] += value;
}
// 'digit' is incremented only when we actually match a hex-digit
// in the GUID. 'i_char', which is the loop counter, also includes
// all the GUID formatting characters, such as {} and -.
digit += 1;
}
// We got the whole buffer. Convert it into a GUID.
//
// Note: We could probably do this with a memcopy - we might have even
// been able to do it directly into the GUID by preallocating it and
// then interpreting it as a *[u8] array.
//
// However I can't be bothered to figure out endianness bits here, so
// we'll just go with raw calculations.
Ok( GUID {
data1:
( u32::from( buffer[ 0 ] ) << 24 ) +
( u32::from( buffer[ 1 ] ) << 16 ) +
( u32::from( buffer[ 2 ] ) << 8 ) +
( u32::from( buffer[ 3 ] ) ),
data2:
( u16::from( buffer[ 4 ] ) << 8 ) +
( u16::from( buffer[ 5 ] ) ),
data3:
( u16::from( buffer[ 6 ] ) << 8 ) +
( u16::from( buffer[ 7 ] ) ),
data4: [
buffer[ 8 ], buffer[ 9 ], buffer[ 10 ], buffer[ 11 ],
buffer[ 12 ], buffer[ 13 ], buffer[ 14 ], buffer[ 15 ],
]
} )
}
// Get GUID as bytes.
pub fn as_bytes( &self ) -> &[u8; 16]
{
// We know the GUIDs are 128 bits (16 bytes).
// Do the equivalent of a reinterpret_cast.
unsafe {
&*( self as *const _ as *const [u8; 16] )
}
}
}
impl Default for GUID {
fn default() -> GUID {
GUID::zero_guid()
}
}
impl std::fmt::Display for GUID {
/// Formats the GUID in brace-hyphenated format for display.
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt_guid( self, f, &GuidFmtCase::Upper, &GuidFmt::Braces )
}
}
impl std::fmt::LowerHex for GUID {
/// Formats the GUID in brace-hyphenated format for display.
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmt = if f.sign_minus() {
GuidFmt::Hyphens
} else {
GuidFmt::Raw
};
fmt_guid( self, f, &GuidFmtCase::Lower, &fmt )
}
}
impl std::fmt::UpperHex for GUID {
/// Formats the GUID in brace-hyphenated format for display.
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmt = if f.sign_minus() {
GuidFmt::Hyphens
} else {
GuidFmt::Raw
};
fmt_guid( self, f, &GuidFmtCase::Upper, &fmt )
}
}
enum GuidFmtCase { Lower, Upper }
enum GuidFmt { Braces, Hyphens, Raw }
fn fmt_guid(
g : &GUID,
f: &mut std::fmt::Formatter,
case : &GuidFmtCase,
fmt : &GuidFmt
) -> std::fmt::Result
{
match *case {
GuidFmtCase::Lower => match *fmt {
GuidFmt::Braces =>
write!( f,
"{{{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}}}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
GuidFmt::Hyphens =>
write!( f,
"{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
GuidFmt::Raw =>
write!( f,
"{:08x}{:04x}{:04x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
},
GuidFmtCase::Upper => match *fmt {
GuidFmt::Braces =>
write!( f,
"{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
GuidFmt::Hyphens =>
write!( f,
"{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
GuidFmt::Raw =>
write!( f,
"{:08X}{:04X}{:04X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
g.data1, g.data2, g.data3,
g.data4[0], g.data4[1], g.data4[2], g.data4[3],
g.data4[4], g.data4[5], g.data4[6], g.data4[7] ),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn zero_guid() {
let guid = GUID::zero_guid();
assert_eq!( 0, guid.data1 );
assert_eq!( 0, guid.data2 );
assert_eq!( 0, guid.data3 );
assert_eq!( [ 0, 0, 0, 0, 0, 0, 0, 0 ], guid.data4 );
}
#[test]
fn parse_braces() {
let expected = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
let actual = GUID::parse( "{12345678-90ab-cdef-fedc-ba0987654321}" )
.unwrap();
assert_eq!( expected, actual );
}
#[test]
fn parse_hyphenated() {
let expected = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
let actual = GUID::parse( "12345678-90ab-cdef-fedc-ba0987654321" )
.unwrap();
assert_eq!( expected, actual );
}
#[test]
fn parse_raw() {
let expected = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
let actual = GUID::parse( "1234567890abcdeffedcba0987654321" )
.unwrap();
assert_eq!( expected, actual );
}
#[test]
fn format_default() {
let expected = "{12345678-90AB-CDEF-FEDC-BA0987654321}";
let guid = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
assert_eq!( expected, format!( "{}", guid ) );
}
#[test]
fn format_lowerhex() {
let expected = "1234567890abcdeffedcba0987654321";
let guid = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
assert_eq!( expected, format!( "{:x}", guid ) );
}
#[test]
fn format_lowerhex_hyphens() {
let expected = "12345678-90ab-cdef-fedc-ba0987654321";
let guid = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
assert_eq!( expected, format!( "{:-x}", guid ) );
}
#[test]
fn format_upperhex() {
let expected = "1234567890ABCDEFFEDCBA0987654321";
let guid = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
assert_eq!( expected, format!( "{:X}", guid ) );
}
#[test]
fn format_upperhex_hyphens() {
let expected = "12345678-90AB-CDEF-FEDC-BA0987654321";
let guid = GUID {
data1: 0x12345678,
data2: 0x90ab,
data3: 0xcdef,
data4: [ 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21 ]
};
assert_eq!( expected, format!( "{:-X}", guid ) );
}
}