aboutsummaryrefslogtreecommitdiff
path: root/cvss/v2score.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-06 11:00:19 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-06 11:00:19 -0500
commit7dff00e81184aed2a6074c0e8e396be2b5445af9 (patch)
treeb6a53faa768e74b8c1e643526a0d06ed80c170ec /cvss/v2score.go
parentd33541ff79e5d81959900b757e4857af14e4f4b2 (diff)
downloadcvez-7dff00e81184aed2a6074c0e8e396be2b5445af9.tar.bz2
cvez-7dff00e81184aed2a6074c0e8e396be2b5445af9.zip
add cvss/v2score and tests
Diffstat (limited to 'cvss/v2score.go')
-rw-r--r--cvss/v2score.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/cvss/v2score.go b/cvss/v2score.go
new file mode 100644
index 0000000..5740c09
--- /dev/null
+++ b/cvss/v2score.go
@@ -0,0 +1,32 @@
+package cvss
+
+import (
+ "fmt"
+)
+
+// Individual CVSS v2 score.
+//
+// Note: since scores range from 0.0 to 10.0 with one decimal place of
+// precision, they can be safely represented as a uint8.
+type v2Score uint8
+
+// Return floating point representation of score.
+func newV2Score(val float64) (v2Score, error) {
+ // check score range
+ if val < 0.0 || val > 10.0 {
+ return v2Score(0), fmt.Errorf("score value out of range [0, 10]: %f", val)
+ }
+
+ // convert to score, return success
+ return v2Score(uint8(10.0 * val)), nil
+}
+
+// Return string representation of score.
+func (s v2Score) String() string {
+ return fmt.Sprintf("%d.%d", s / 10, s % 10)
+}
+
+// Return floating point representation of score.
+func (s v2Score) Float() float32 {
+ return float32(s) / 10.0
+}