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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
//! This crate provides types for identifiers of object files, such as executables, dynamic //! libraries or debug companion files. The concept originates in Google Breakpad and defines two //! types: //! //! - [`CodeId`]: Identifies the file containing source code, i.e. the actual library or //! executable. The identifier is platform dependent and implementation defined. Thus, there is //! no canonical representation. //! - [`DebugId`]: Identifies a debug information file, which may or may not use information from //! the Code ID. The contents are also implementation defined, but as opposed to `CodeId`, the //! structure is streamlined across platforms. It is also guaranteed to be 32 bytes in size. //! //! [`CodeId`]: struct.CodeId.html [`DebugId`]: struct.DebugId.html #![warn(missing_docs)] use std::error; use std::fmt; use std::fmt::Write; use std::str; use regex::Regex; use uuid::Uuid; lazy_static::lazy_static! { static ref DEBUG_ID_RE: Regex = Regex::new( r"(?ix) ^ (?P<uuid> [0-9a-f]{8}-? [0-9a-f]{4}-? [0-9a-f]{4}-? [0-9a-f]{4}-? [0-9a-f]{12} ) -? (?P<appendix> [0-9a-f]{1,8} )? ( # ignored tail (?:-?[0-9a-f]){1,24} )? $ " ) .unwrap(); } /// Indicates an error parsing a [`DebugId`](struct.DebugId.html). #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct ParseDebugIdError; impl error::Error for ParseDebugIdError {} impl fmt::Display for ParseDebugIdError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "invalid debug identifier") } } /// Unique identifier for debug information files and their debug information. /// /// This type is analogous to `CodeId`, except that it identifies a debug file instead of the actual /// library or executable. One some platforms, a `DebugId` is an alias for a `CodeId` but the exact /// rules around this are complex. On Windows, the identifiers are completely different and refer to /// separate files. /// /// The string representation must be between 33 and 40 characters /// long and consist of: /// /// 1. 36 character hyphenated hex representation of the UUID field /// 2. 1-16 character lowercase hex representation of the u32 appendix /// /// The debug identifier is compatible to Google Breakpad. Use [`DebugId::breakpad`] to get a /// breakpad string representation of this debug identifier. /// /// # Example /// /// ``` /// # extern crate debugid; /// use std::str::FromStr; /// use debugid::DebugId; /// /// # fn foo() -> Result<(), ::debugid::ParseDebugIdError> { /// let id = DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-a")?; /// assert_eq!("dfb8e43a-f242-3d73-a453-aeb6a777ef75-a".to_string(), id.to_string()); /// # Ok(()) /// # } /// /// # fn main() { foo().unwrap() } /// ``` #[repr(C, packed)] #[derive(Default, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub struct DebugId { uuid: Uuid, appendix: u32, _padding: [u8; 12], } impl DebugId { /// Constructs an empty debug identifier, containing only zeros. pub fn nil() -> Self { Self::default() } /// Constructs a `DebugId` from its `uuid`. pub fn from_uuid(uuid: Uuid) -> Self { Self::from_parts(uuid, 0) } /// Constructs a `DebugId` from its `uuid` and `appendix` parts. pub fn from_parts(uuid: Uuid, appendix: u32) -> Self { DebugId { uuid, appendix, _padding: [0; 12], } } /// Constructs a `DebugId` from a Microsoft little-endian GUID and age. pub fn from_guid_age(guid: &[u8], age: u32) -> Result<Self, ParseDebugIdError> { if guid.len() != 16 { return Err(ParseDebugIdError); } let uuid = Uuid::from_bytes([ guid[3], guid[2], guid[1], guid[0], guid[5], guid[4], guid[7], guid[6], guid[8], guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15], ]); Ok(DebugId::from_parts(uuid, age)) } /// Parses a breakpad identifier from a string. pub fn from_breakpad(string: &str) -> Result<Self, ParseDebugIdError> { // Technically, we are are too permissive here by allowing dashes, but // we are complete. string.parse() } /// Returns the UUID part of the code module's debug_identifier. pub fn uuid(&self) -> Uuid { self.uuid } /// Returns the appendix part of the code module's debug identifier. /// /// On Windows, this is an incrementing counter to identify the build. /// On all other platforms, this value will always be zero. pub fn appendix(&self) -> u32 { self.appendix } /// Returns whether this identifier is nil, i.e. it consists only of zeros. pub fn is_nil(&self) -> bool { self.uuid.is_nil() && self.appendix() == 0 } /// Returns a wrapper which when formatted via `fmt::Display` will format a /// a breakpad identifier. pub fn breakpad(&self) -> BreakpadFormat<'_> { BreakpadFormat { inner: self } } } impl fmt::Debug for DebugId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("DebugId") .field("uuid", &self.uuid().to_hyphenated_ref().to_string()) .field("appendix", &self.appendix()) .finish() } } impl fmt::Display for DebugId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.uuid.fmt(f)?; if self.appendix > 0 { write!(f, "-{:x}", { self.appendix })?; } Ok(()) } } impl str::FromStr for DebugId { type Err = ParseDebugIdError; fn from_str(string: &str) -> Result<DebugId, ParseDebugIdError> { let captures = DEBUG_ID_RE.captures(string).ok_or(ParseDebugIdError)?; let uuid = captures .name("uuid") .unwrap() .as_str() .parse() .map_err(|_| ParseDebugIdError)?; let appendix = captures .name("appendix") .map_or(Ok(0), |s| u32::from_str_radix(s.as_str(), 16)) .map_err(|_| ParseDebugIdError)?; Ok(DebugId::from_parts(uuid, appendix)) } } impl From<Uuid> for DebugId { fn from(uuid: Uuid) -> DebugId { DebugId::from_uuid(uuid) } } impl From<(Uuid, u32)> for DebugId { fn from(tuple: (Uuid, u32)) -> DebugId { let (uuid, appendix) = tuple; DebugId::from_parts(uuid, appendix) } } /// Wrapper around `DebugId` for Breakpad formatting. /// /// **Example:** /// /// ``` /// # extern crate debugid; /// use std::str::FromStr; /// use debugid::DebugId; /// /// # fn foo() -> Result<(), debugid::ParseDebugIdError> { /// let id = DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF75a")?; /// assert_eq!("DFB8E43AF2423D73A453AEB6A777EF75a".to_string(), id.breakpad().to_string()); /// # Ok(()) /// # } /// /// # fn main() { foo().unwrap() } /// ``` #[derive(Debug)] pub struct BreakpadFormat<'a> { inner: &'a DebugId, } impl<'a> fmt::Display for BreakpadFormat<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{:X}{:x}", self.inner.uuid().to_simple_ref(), self.inner.appendix() ) } } /// Indicates an error parsing a [`CodeId`](struct.CodeId.html). #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct ParseCodeIdError; impl error::Error for ParseCodeIdError {} impl fmt::Display for ParseCodeIdError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "invalid code identifier") } } /// Unique platform-dependent identifier of code files. /// /// This identifier assumes a string representation that depends on the platform and compiler used. /// The representation only retains hex characters and canonically stores lower case. /// /// There are the following known formats: /// /// - **MachO UUID**: The unique identifier of a Mach binary, specified in the `LC_UUID` load /// command header. /// - **GNU Build ID**: Contents of the `.gnu.build-id` note or section contents formatted as /// lowercase hex string. /// - **PE Timestamp**: Timestamp and size of image values from a Windows PE header. The size of /// image value is truncated, so the length of the `CodeId` might not be a multiple of 2. #[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct CodeId { inner: String, } impl CodeId { /// Constructs an empty code identifier. pub fn nil() -> Self { Self::default() } /// Constructs a `CodeId` from its string representation. pub fn new(mut string: String) -> Self { string.retain(|c| c.is_ascii_hexdigit()); string.make_ascii_lowercase(); CodeId { inner: string } } /// Constructs a `CodeId` from a binary slice. pub fn from_binary(slice: &[u8]) -> Self { let mut string = String::with_capacity(slice.len() * 2); for byte in slice { write!(&mut string, "{:02x}", byte).expect(""); } Self::new(string) } /// Returns whether this identifier is nil, i.e. it is empty. pub fn is_nil(&self) -> bool { self.inner.is_empty() } /// Returns the string representation of this code identifier. pub fn as_str(&self) -> &str { self.inner.as_str() } } impl fmt::Display for CodeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.inner) } } impl fmt::Debug for CodeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "CodeId({})", self) } } impl From<String> for CodeId { fn from(string: String) -> Self { Self::new(string) } } impl From<&'_ str> for CodeId { fn from(string: &str) -> Self { Self::new(string.into()) } } impl AsRef<str> for CodeId { fn as_ref(&self) -> &str { self.as_str() } } impl str::FromStr for CodeId { type Err = ParseCodeIdError; fn from_str(string: &str) -> Result<Self, ParseCodeIdError> { Ok(Self::new(string.into())) } } #[cfg(feature = "serde")] mod serde_support { use serde::de::{self, Deserialize, Deserializer, Unexpected, Visitor}; use serde::ser::{Serialize, Serializer}; use super::*; impl Serialize for CodeId { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { serializer.serialize_str(self.as_str()) } } impl<'de> Deserialize<'de> for CodeId { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { let string = String::deserialize(deserializer)?; Ok(CodeId::new(string)) } } impl<'de> Deserialize<'de> for DebugId { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { struct V; impl<'de> Visitor<'de> for V { type Value = DebugId; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("DebugId") } fn visit_str<E: de::Error>(self, value: &str) -> Result<DebugId, E> { value .parse() .map_err(|_| de::Error::invalid_value(Unexpected::Str(value), &self)) } } deserializer.deserialize_str(V) } } impl Serialize for DebugId { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { serializer.serialize_str(&self.to_string()) } } }