aboutsummaryrefslogtreecommitdiff
path: root/cpe/avstring.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-04 00:35:31 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-04 00:35:31 -0500
commit9c17b97cd0f83be3fff9fa4e87fd1d29052ea616 (patch)
tree0d97030a0d0c3ad983be281ce89f80571338887f /cpe/avstring.go
parent92400d731546557d110c9c3cc3906d700f83dda8 (diff)
downloadcvez-9c17b97cd0f83be3fff9fa4e87fd1d29052ea616.tar.bz2
cvez-9c17b97cd0f83be3fff9fa4e87fd1d29052ea616.zip
rename to github.com/pablotron/cvez, remove internal libs
Diffstat (limited to 'cpe/avstring.go')
-rw-r--r--cpe/avstring.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cpe/avstring.go b/cpe/avstring.go
new file mode 100644
index 0000000..46e16cf
--- /dev/null
+++ b/cpe/avstring.go
@@ -0,0 +1,54 @@
+package cpe
+
+//go:generate stringer -linecomment -type=AvStringType
+
+import (
+ "fmt"
+)
+
+// type of avstring.
+type AvStringType byte
+
+const (
+ AnyString AvStringType = iota // any
+ NaString // na
+ ValString // val
+)
+
+// String value (NISTIR 7695, CPE 2.3 spec, Figure 6-3)
+type AvString struct {
+ Type AvStringType // value type
+ Val string // value
+}
+
+// token type to avstring type map
+var avTypes = map[tokenType]AvStringType {
+ anyToken: AnyString,
+ naToken: NaString,
+ valToken: ValString,
+}
+
+// create new AvString from token.
+func newAvString(t token) (AvString, error) {
+ if at, ok := avTypes[t.Type]; ok {
+ return AvString { at, t.Val }, nil
+ } else {
+ err := fmt.Errorf("invalid token type: 0x%02x", byte(t.Type))
+ return AvString { 0, "" }, err
+ }
+}
+
+// Serialize as string.
+func (s AvString) String() string {
+ switch s.Type {
+ case AnyString:
+ return "*"
+ case NaString:
+ return "-"
+ case ValString:
+ return s.Val
+ default:
+ // not sure what to return here
+ return ""
+ }
+}