xref: /aosp_15_r20/external/spdx-tools/rdfloader/parser2v2/license_utils.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package parser2v2
4
5import (
6	"fmt"
7	"strings"
8
9	gordfParser "github.com/spdx/gordf/rdfloader/parser"
10	"github.com/spdx/tools-golang/spdx/common"
11)
12
13/* util methods for licenses and checksums below:*/
14
15// Given the license URI, returns the name of the license defined
16// in the last part of the uri.
17// This function is susceptible to false-positives.
18func getLicenseStringFromURI(uri string) string {
19	licenseEnd := strings.TrimSpace(getLastPartOfURI(uri))
20	lower := strings.ToLower(licenseEnd)
21	if lower == "none" || lower == "noassertion" {
22		return strings.ToUpper(licenseEnd)
23	}
24	return licenseEnd
25}
26
27// returns the checksum algorithm and it's value
28// In the newer versions, these two strings will be bound to a single checksum struct
29// whose pointer will be returned.
30func (parser *rdfParser2_2) getChecksumFromNode(checksumNode *gordfParser.Node) (algorithm common.ChecksumAlgorithm, value string, err error) {
31	var checksumValue, checksumAlgorithm string
32	for _, checksumTriple := range parser.nodeToTriples(checksumNode) {
33		switch checksumTriple.Predicate.ID {
34		case RDF_TYPE:
35			continue
36		case SPDX_CHECKSUM_VALUE:
37			// cardinality: exactly 1
38			checksumValue = strings.TrimSpace(checksumTriple.Object.ID)
39		case SPDX_ALGORITHM:
40			// cardinality: exactly 1
41			checksumAlgorithm, err = getAlgorithmFromURI(checksumTriple.Object.ID)
42			if err != nil {
43				return
44			}
45		default:
46			err = fmt.Errorf("unknown predicate '%s' while parsing checksum node", checksumTriple.Predicate.ID)
47			return
48		}
49	}
50	return common.ChecksumAlgorithm(checksumAlgorithm), checksumValue, nil
51}
52
53func getAlgorithmFromURI(algorithmURI string) (checksumAlgorithm string, err error) {
54	fragment := getLastPartOfURI(algorithmURI)
55	if !strings.HasPrefix(fragment, "checksumAlgorithm_") {
56		return "", fmt.Errorf("checksum algorithm uri must begin with checksumAlgorithm_. found %s", fragment)
57	}
58	algorithm := strings.TrimPrefix(fragment, "checksumAlgorithm_")
59	algorithm = strings.ToLower(strings.TrimSpace(algorithm))
60	switch algorithm {
61	case "md2", "md4", "md5", "md6":
62		checksumAlgorithm = strings.ToUpper(algorithm)
63	case "sha1", "sha224", "sha256", "sha384", "sha512":
64		checksumAlgorithm = strings.ToUpper(algorithm)
65	default:
66		return "", fmt.Errorf("unknown checksum algorithm %s", algorithm)
67	}
68	return
69}
70
71// from a list of licenses, it returns a
72// list of string representation of those licenses.
73func mapLicensesToStrings(licences []AnyLicenseInfo) []string {
74	res := make([]string, len(licences), len(licences))
75	for i, lic := range licences {
76		res[i] = lic.ToLicenseString()
77	}
78	return res
79}
80
81/****** Type Functions ******/
82
83// TODO: should probably add brackets while linearizing a nested license.
84func (lic ConjunctiveLicenseSet) ToLicenseString() string {
85	return strings.Join(mapLicensesToStrings(lic.members), " AND ")
86}
87
88// TODO: should probably add brackets while linearizing a nested license.
89func (lic DisjunctiveLicenseSet) ToLicenseString() string {
90	return strings.Join(mapLicensesToStrings(lic.members), " OR ")
91}
92
93func (lic ExtractedLicensingInfo) ToLicenseString() string {
94	return lic.licenseID
95}
96
97func (operator OrLaterOperator) ToLicenseString() string {
98	return operator.member.ToLicenseString()
99}
100
101func (lic License) ToLicenseString() string {
102	return lic.licenseID
103}
104
105func (lic ListedLicense) ToLicenseString() string {
106	return lic.licenseID
107}
108
109func (lic WithExceptionOperator) ToLicenseString() string {
110	return lic.member.ToLicenseString()
111}
112
113func (lic SpecialLicense) ToLicenseString() string {
114	return string(lic.value)
115}
116
117func (lic SimpleLicensingInfo) ToLicenseString() string {
118	return lic.licenseID
119}
120