forked from ymomoi/goval-parser
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds several data structures and memo methods to support parsing dpkginfo oval structures. Signed-off-by: louis <[email protected]> Co-authored-by: louis <[email protected]>
- Loading branch information
Louis DeLosSantos
and
louis
authored
Aug 11, 2020
1 parent
1ff8552
commit 08a0863
Showing
7 changed files
with
196 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package oval | ||
|
||
import ( | ||
"encoding/xml" | ||
) | ||
|
||
// DpkgInfoTest : >tests>dpkginfo_test | ||
type DpkgInfoTest struct { | ||
XMLName xml.Name `xml:"dpkginfo_test"` | ||
ID string `xml:"id,attr"` | ||
Comment string `xml:"comment,attr"` | ||
Check string `xml:"check,attr"` | ||
Version int `xml:"version,attr"` | ||
ObjectRefs []ObjectRef `xml:"object"` | ||
StateRefs []StateRef `xml:"state"` | ||
} | ||
|
||
// DpkgName : >objects>dpkginfo_object>name | ||
// | ||
// when parsing ubuntu var_ref is a reference | ||
// to the <variables> section of the document | ||
// | ||
// when parsing debian var_ref is empty and | ||
// the Body field is used directly | ||
type DpkgName struct { | ||
XMLName xml.Name `xml:"name"` | ||
Ref string `xml:"var_ref,attr"` | ||
Body string `xml:",chardata"` | ||
} | ||
|
||
// DpkgInfoObject : >objects>dpkginfo_object | ||
type DpkgInfoObject struct { | ||
XMLName xml.Name `xml:"dpkginfo_object"` | ||
ID string `xml:"id,attr"` | ||
Version int `xml:"version,attr"` | ||
Name *DpkgName `xml:"name"` | ||
} | ||
|
||
// DpkgInfoState : >states>dpkginfo_state | ||
type DpkgInfoState struct { | ||
XMLName xml.Name `xml:"dpkginfo_state"` | ||
ID string `xml:"id,attr"` | ||
Version int `xml:"version,attr"` | ||
Arch *Arch `xml:"arch"` | ||
Epoch *Epoch `xml:"epoch"` | ||
Release *Release `xml:"release"` | ||
DpkgVersion *Version `xml:"version"` | ||
EVR *EVR `xml:"evr"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package oval | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
func (o *Variables) init() { | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
o.dpkginfoMemo = make(map[string]int, len(o.ConstantVariables)) | ||
for i, v := range o.ConstantVariables { | ||
o.dpkginfoMemo[v.ID] = i | ||
} | ||
}() | ||
|
||
wg.Wait() | ||
} | ||
|
||
// Lookup returns the kind of object and index into that kind-specific slice, if | ||
// found. | ||
func (o *Variables) Lookup(ref string) (kind string, index int, err error) { | ||
o.once.Do(o.init) | ||
if i, ok := o.dpkginfoMemo[ref]; ok { | ||
return o.ConstantVariables[i].XMLName.Local, i, nil | ||
} | ||
|
||
// We didn't find it, maybe we can say why. | ||
id, err := ParseID(ref) | ||
if err != nil { | ||
return "", -1, err | ||
} | ||
if id.Type != OvalVariable { | ||
return "", -1, fmt.Errorf("oval: wrong identifier type %q", id.Type) | ||
} | ||
return "", -1, ErrNotFound(ref) | ||
} |