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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! # CPE Well-Formed Name
//!
//! A CPE 2.3 WFN as defined in
//! [CPE-N:5.3](https://nvlpubs.nist.gov/nistpubs/Legacy/IR/nistir7695.pdf)
//!
//! The values of a `Wfn` can be set directly via the fields, but this is discouraged,
//! as there is no validation of the values. There are a few options for creating a `Wfn`:
//!
//! 1. Parsing a WFN string
//! ```
//! use cpe::wfn::Wfn;
//!
//! let cpe: Wfn = Wfn::parse(r#"wfn:[part="a",vendor="rust",product="cargo"]"#).unwrap();
//! println!("{:?}", cpe);
//! ```
//! 2. Using the builder pattern, with [builder](#method.builder)
//! 3. Using the "setter" methods
//! ```
//! use cpe::wfn::Wfn;
//!
//! let mut cpe: Wfn = Wfn::new();
//! cpe.set_part("a").unwrap();
//! cpe.set_vendor("rust").unwrap();
//! cpe.set_product("cargo").unwrap();
//!
//! println!("{:?}", cpe);
//! ```
//! 4. Using the `wfn!{}` macro
//! ```
//! use cpe::{wfn, wfn::Wfn};
//!
//! let cpe: Wfn = wfn!{
//!                   part: "a",
//!                   vendor: "rust",
//!                   product: "cargo",
//!               }.unwrap();
//!
//! println!("{:?}", cpe);
//! ```
//!
//!
//! ## Valid values
//! The valid values for WFN attributes come from the grammar for WFNs in [CPE-N:5.3.2]

use std::convert::TryFrom;
use std::fmt;

use crate::component::{Component, OwnedComponent};
use crate::cpe::{CpeType, Language};
use crate::error::{CpeError, Result};
use crate::uri::{OwnedUri, Uri};

use crate::builder::CpeBuilder;

fn split_unescaped_comma(s: &str) -> Vec<&str> {
    let indices = s
        .match_indices(',')
        .map(|(index, _)| index)
        .filter(|index| !s[..*index].ends_with('\\'))
        .collect::<Vec<_>>();
    let mut parts = Vec::with_capacity(indices.len() + 1);
    let mut last = None;
    for index in indices {
        if let Some(last) = last {
            parts.push(&s[last..index]);
        } else {
            parts.push(&s[..index]);
        }
        last = Some(index + 1);
    }
    if let Some(last) = last {
        parts.push(&s[last..]);
    } else {
        parts.push(s);
    }
    parts
}

/// Helper macro to create a `Wfn` from literal values.
#[macro_export]
macro_rules! wfn {
    ($($field:ident : $value:literal),*$(,)*) => {{
        let mut wfn = Wfn::builder();
        $(
            wfn.$field($value);
        )*
        wfn.validate()
    }}
}

/// A CPE 2.3 Well-Formed Name
///
/// Note: field access is limited to ensure values only contain
/// semantically valid components. Fields can be accessed through the
/// "getter" methods, or through the [Cpe](../cpe/trait.Cpe.html) methods,
/// although the former is preferred with a `Cpe` as opposed to an `OwnedCpe`.
///
/// Display is implemented to show the decoded contents by default, or to re-encode
/// the components when `#` is used to specify an alternate.
///
///```
/// use cpe::wfn::Wfn;
/// let wfn = Wfn::builder()
///           .part("a")
///           .vendor("foo\\!")
///           .validate()
///           .unwrap();
///
/// assert_eq!(format!("{}", wfn), "wfn:[part=a,vendor=foo!,product=ANY,version=ANY,update=ANY,edition=ANY,language=ANY,sw_edition=ANY,target_sw=ANY,target_hw=ANY,other=ANY]".to_owned());
/// assert_eq!(format!("{:#}", wfn), "wfn:[part=a,vendor=foo\\!,product=ANY,version=ANY,update=ANY,edition=ANY,language=ANY,sw_edition=ANY,target_sw=ANY,target_hw=ANY,other=ANY]".to_owned());
///```
///
/// Additionally, the `0` for zero-padding integers can be used to omit default "ANY" fields.
///```
/// use cpe::wfn::Wfn;
/// let wfn = Wfn::builder()
///           .part("a")
///           .vendor("foo\\!")
///           .validate()
///           .unwrap();
///
/// assert_eq!(format!("{:0}", wfn), "wfn:[part=a,vendor=foo!]".to_owned());
/// assert_eq!(format!("{:#0}", wfn), "wfn:[part=a,vendor=foo\\!]".to_owned());
///```
#[derive(Default, Debug, PartialEq)]
pub struct Wfn<'a> {
    pub(crate) part: CpeType,
    pub(crate) vendor: Component<'a>,
    pub(crate) product: Component<'a>,
    pub(crate) version: Component<'a>,
    pub(crate) update: Component<'a>,
    pub(crate) edition: Component<'a>,
    pub(crate) language: Language,
    pub(crate) sw_edition: Component<'a>,
    pub(crate) target_sw: Component<'a>,
    pub(crate) target_hw: Component<'a>,
    pub(crate) other: Component<'a>,
}

impl<'a> Wfn<'a> {
    /// Create a default Wfn with all fields set to ANY.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a `CpeBuilder` struct to construct a new Wfn.
    ///
    /// ```
    /// use cpe::wfn::Wfn;
    ///
    /// let cpe: Wfn = Wfn::builder()
    ///               .part("a")
    ///               .vendor("rust")
    ///               .product("cargo")
    ///               .validate()
    ///               .unwrap();
    ///
    /// println!("{:?}", cpe);
    /// ```
    pub fn builder() -> CpeBuilder<'a, Wfn<'a>> {
        CpeBuilder::default()
    }

    /// Set the CPE type part, `a`, `o`, `h`, or `*`.
    ///
    /// The provided string slice will be parsed to its semantic meaning.
    pub fn set_part(&mut self, part: &'a str) -> Result<()> {
        self.part = CpeType::try_from(part)?;
        Ok(())
    }

    /// Set the CPE vendor.
    ///
    /// The provided string slice will be parsed to its semantic meaning.
    pub fn set_vendor(&mut self, vendor: &'a str) -> Result<()> {
        self.vendor = Component::parse_wfn_field(vendor)?;
        Ok(())
    }

    /// Set the CPE product.
    ///
    /// The provided string slice will be parsed to its semantic meaning.
    pub fn set_product(&mut self, product: &'a str) -> Result<()> {
        self.product = Component::parse_wfn_field(product)?;
        Ok(())
    }

    /// Set the CPE product.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_version(&mut self, version: &'a str) -> Result<()> {
        self.version = Component::parse_wfn_field(version)?;
        Ok(())
    }

    /// Set the CPE update.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_update(&mut self, update: &'a str) -> Result<()> {
        self.update = Component::parse_wfn_field(update)?;
        Ok(())
    }

    /// Set the CPE edition.
    ///
    /// The provided string will be parsed to its semantic meaning.
    /// Note that this funciton will not unpack a packed `~` delimited edition component.
    pub fn set_edition(&mut self, edition: &'a str) -> Result<()> {
        self.edition = Component::parse_wfn_field(edition)?;
        Ok(())
    }

    /// Set the CPE language.
    ///
    /// The provided string will be parsed to its semantic meaning.
    /// `language` must be a valid RFC-5646 language tag.
    pub fn set_language(&mut self, language: &'a str) -> Result<()> {
        self.language = if language == "ANY" {
            Language::Any
        } else {
            Language::Language(language.parse()?)
        };
        Ok(())
    }

    /// Set the CPE software edition.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_sw_edition(&mut self, sw_edition: &'a str) -> Result<()> {
        self.sw_edition = Component::parse_wfn_field(sw_edition)?;
        Ok(())
    }

    /// Set the CPE target software.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_target_sw(&mut self, target_sw: &'a str) -> Result<()> {
        self.target_sw = Component::parse_wfn_field(target_sw)?;
        Ok(())
    }

    /// Set the CPE target hardware.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_target_hw(&mut self, target_hw: &'a str) -> Result<()> {
        self.target_hw = Component::parse_wfn_field(target_hw)?;
        Ok(())
    }

    /// Set the CPE "other" value.
    ///
    /// The provided string will be parsed to its semantic meaning.
    pub fn set_other(&mut self, other: &'a str) -> Result<()> {
        self.other = Component::parse_wfn_field(other)?;
        Ok(())
    }

    /// Create an Owned copy of this CPE WFN.
    pub fn to_owned(&self) -> OwnedWfn {
        self.into()
    }

    /// Create a `Uri`, perserving lifetimes of the original input.
    /// Note that strings may be cloned if the input was decoded.
    pub fn as_uri(&self) -> Uri<'a> {
        self.into()
    }

    /// Parse a CPE URI string.
    ///
    /// This function will decode percent encodings and special characters to their
    /// semantic meaning.
    pub fn parse(value: &'a str) -> Result<Self> {
        let offset = if value.starts_with("wfn:[") {
            5
        } else {
            return Err(CpeError::InvalidPrefix {
                value: value.to_owned(),
            });
        };

        let mut wfn = Self::new();

        let remainder = &value[offset..value.len() - 1];

        let mut set_part = false;
        let mut set_vendor = false;
        let mut set_product = false;
        let mut set_version = false;
        let mut set_update = false;
        let mut set_edition = false;
        let mut set_language = false;
        let mut set_sw_edition = false;
        let mut set_target_sw = false;
        let mut set_target_hw = false;
        let mut set_other = false;

        let parts = split_unescaped_comma(remainder);
        eprintln!("{:?}", parts);
        for part in parts {
            let (attribute, value) = {
                let mut parts = part.splitn(2, '=');
                let att = parts
                    .next()
                    .ok_or_else(|| CpeError::InvalidWfn {
                        value: value.to_owned(),
                        expected: format!("malformed attribute value pair `{}`", part),
                    })?
                    .to_lowercase();
                let val = parts
                    .next()
                    .ok_or_else(|| CpeError::InvalidWfn {
                        value: value.to_owned(),
                        expected: format!("malformed attribute value pair `{}`", part),
                    })?
                    .trim_start_matches('"')
                    .trim_end_matches('"');
                (att, val)
            };

            macro_rules! set_field {
                ($name:literal, $check:ident) => {
                    if $check {
                        return Err(CpeError::DuplicateAttribute {
                            value: value.to_owned(),
                            name: $name,
                        });
                    } else {
                        $check = true;
                        wfn.$check(value)?;
                    }
                };
            }

            match attribute.as_str() {
                "part" => set_field!("part", set_part),
                "language" => set_field!("language", set_language),
                "vendor" => set_field!("vendor", set_vendor),
                "product" => set_field!("product", set_product),
                "version" => set_field!("version", set_version),
                "update" => set_field!("update", set_update),
                "edition" => set_field!("edition", set_edition),
                "sw_edition" => set_field!("sw_edition", set_sw_edition),
                "target_sw" => set_field!("target_sw", set_target_sw),
                "target_hw" => set_field!("target_hw", set_target_hw),
                "other" => set_field!("other", set_other),
                _ => {
                    return Err(CpeError::InvalidWfn {
                        value: value.to_owned(),
                        expected: format!("invalid attribute `{}`", attribute),
                    })
                }
            }
        }

        Ok(wfn)
    }
}

impl fmt::Display for Wfn<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        macro_rules! write_field {
            ($field:ident) => {
                if !f.sign_aware_zero_pad() || &self.$field != &Component::Any {
                    if f.alternate() {
                        write!(f, ",{}={}", stringify!($field), self.$field.encode_wfn())?;
                    } else {
                        write!(f, ",{}={}", stringify!($field), self.$field)?;
                    }
                }
            };
        }
        write!(f, "wfn:[")?;
        write!(f, "part={}", self.part)?;
        write_field!(vendor);
        write_field!(product);
        write_field!(version);
        write_field!(update);
        write_field!(edition);
        if !f.sign_aware_zero_pad() || self.language != Language::Any {
            write!(f, ",language={}", self.language)?;
        }
        write_field!(sw_edition);
        write_field!(target_sw);
        write_field!(target_hw);
        write_field!(other);
        write!(f, "]")
    }
}

impl<'a> From<Uri<'a>> for Wfn<'a> {
    fn from(uri: Uri<'a>) -> Self {
        Self {
            part: uri.part,
            vendor: uri.vendor,
            product: uri.product,
            version: uri.version,
            update: uri.update,
            edition: uri.edition,
            language: uri.language,
            sw_edition: uri.sw_edition,
            target_sw: uri.target_sw,
            target_hw: uri.target_hw,
            other: uri.other,
        }
    }
}

impl<'a> From<&Uri<'a>> for Wfn<'a> {
    fn from(uri: &Uri<'a>) -> Self {
        Self {
            part: uri.part,
            vendor: uri.vendor.clone(),
            product: uri.product.clone(),
            version: uri.version.clone(),
            update: uri.update.clone(),
            edition: uri.edition.clone(),
            language: uri.language.clone(),
            sw_edition: uri.sw_edition.clone(),
            target_sw: uri.target_sw.clone(),
            target_hw: uri.target_hw.clone(),
            other: uri.other.clone(),
        }
    }
}

/// Owned copy of a Wfn for when lifetimes do not permit borrowing
/// from the input.
pub struct OwnedWfn {
    pub(crate) part: CpeType,
    pub(crate) vendor: OwnedComponent,
    pub(crate) product: OwnedComponent,
    pub(crate) version: OwnedComponent,
    pub(crate) update: OwnedComponent,
    pub(crate) edition: OwnedComponent,
    pub(crate) language: Language,
    pub(crate) sw_edition: OwnedComponent,
    pub(crate) target_sw: OwnedComponent,
    pub(crate) target_hw: OwnedComponent,
    pub(crate) other: OwnedComponent,
}

macro_rules! into {
    ($t:ty) => {
        impl From<$t> for OwnedWfn {
            fn from(cpe: $t) -> Self {
                Self {
                    part: cpe.part,
                    vendor: cpe.vendor.to_owned(),
                    product: cpe.product.to_owned(),
                    version: cpe.version.to_owned(),
                    update: cpe.update.to_owned(),
                    edition: cpe.edition.to_owned(),
                    language: cpe.language.clone(),
                    sw_edition: cpe.sw_edition.to_owned(),
                    target_sw: cpe.target_sw.to_owned(),
                    target_hw: cpe.target_hw.to_owned(),
                    other: cpe.other.to_owned(),
                }
            }
        }
    };
}

into!(Uri<'_>);
into!(&Uri<'_>);
into!(Wfn<'_>);
into!(&Wfn<'_>);
into!(OwnedUri);

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn basic_wfn() {
        let wfn = Wfn::parse(r#"wfn:[part="a",vendor="microsoft",product="internet_explorer",version="8\.0\.6001",update="beta",edition=NA]"#).unwrap();
        assert_eq!(wfn.part, CpeType::Application);
        assert_eq!(wfn.vendor, Component::new("microsoft"));
        assert_eq!(wfn.product, Component::new("internet_explorer"));
        assert_eq!(wfn.version, Component::new("8.0.6001"));
        assert_eq!(wfn.update, Component::new("beta"));
        assert_eq!(wfn.edition, Component::NotApplicable);
    }

    #[test]
    fn with_macro() {
        let wfn = Wfn::parse(r#"wfn:[part="a",vendor="microsoft",product="internet_explorer",version="8\.*",update="sp?",edition=NA,language=ANY]"#).unwrap();
        assert_eq!(
            wfn,
            wfn! {
                part: "a",
                vendor: "microsoft",
                product: "internet_explorer",
                version: r"8\.*",
                update: "sp?",
                edition: "NA",
            }
            .unwrap()
        );
        assert_eq!(
            wfn,
            wfn! {
                part: "a",
                vendor: "microsoft",
                product: "internet_explorer",
                version: r"8\.*",
                update: "sp?",
                edition: "NA",
                language: "ANY",
            }
            .unwrap()
        )
    }

    #[test]
    fn with_escaped_comma() {
        let wfn = Wfn::parse(r#"wfn:[part="a",vendor="micr\,osoft",product="internet_explorer",version="8\.*",update="sp?",edition=NA,language=ANY]"#).unwrap();
        assert_eq!(
            wfn,
            wfn! {
                part: "a",
                vendor: r"micr\,osoft",
                product: "internet_explorer",
                version: r"8\.*",
                update: "sp?",
                edition: "NA",
            }
            .unwrap()
        );
        assert_eq!(wfn.part, CpeType::Application);
        assert_eq!(wfn.vendor, Component::new("micr,osoft"));
        assert_eq!(wfn.product, Component::new("internet_explorer"));
        assert_eq!(wfn.version, Component::new("8.*"));
        assert_eq!(wfn.update, Component::new("sp?"));
        assert_eq!(wfn.edition, Component::NotApplicable);
    }
}