| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type nodePair struct { |
| 10 | firstNode *node |
| 11 | secondNode *node |
| 12 | } |
| 13 | |
| 14 | type nodeRole uint8 |
| 15 | |
| 16 | const ( |
| 17 | expressionNode nodeRole = iota |
| 18 | licenseRefNode |
| 19 | licenseNode |
| 20 | ) |
| 21 | |
| 22 | type node struct { |
| 23 | role nodeRole |
| 24 | exp *expressionNodePartial |
| 25 | lic *licenseNodePartial |
| 26 | ref *referenceNodePartial |
| 27 | } |
| 28 | |
| 29 | type expressionNodePartial struct { |
| 30 | left *node |
| 31 | conjunction string |
| 32 | right *node |
| 33 | } |
| 34 | |
| 35 | type licenseNodePartial struct { |
| 36 | license string |
| 37 | hasPlus bool |
| 38 | hasException bool |
| 39 | exception string |
| 40 | } |
| 41 | |
| 42 | type referenceNodePartial struct { |
| 43 | hasDocumentRef bool |
| 44 | documentRef string |
| 45 | licenseRef string |
| 46 | } |
| 47 | |
| 48 | // ---------------------- Helper Methods ---------------------- |
| 49 | |
| 50 | func (n *node) isExpression() bool { |
| 51 | return n.role == expressionNode |
| 52 | } |
| 53 | |
| 54 | func (n *node) isOrExpression() bool { |
| 55 | if !n.isExpression() { |
| 56 | return false |
| 57 | } |
| 58 | return *n.conjunction() == "or" |
| 59 | } |
| 60 | |
| 61 | func (n *node) isAndExpression() bool { |
| 62 | if !n.isExpression() { |
| 63 | return false |
| 64 | } |
| 65 | return *n.conjunction() == "and" |
| 66 | } |
| 67 | |
| 68 | func (n *node) left() *node { |
| 69 | if !n.isExpression() { |
| 70 | return nil |
| 71 | } |
| 72 | return n.exp.left |
| 73 | } |
| 74 | |
| 75 | func (n *node) conjunction() *string { |
| 76 | if !n.isExpression() { |
| 77 | return nil |
| 78 | } |
| 79 | return &(n.exp.conjunction) |
| 80 | } |
| 81 | |
| 82 | func (n *node) right() *node { |
| 83 | if !n.isExpression() { |
| 84 | return nil |
| 85 | } |
| 86 | return n.exp.right |
| 87 | } |
| 88 | |
| 89 | func (n *node) isLicense() bool { |
| 90 | return n.role == licenseNode |
| 91 | } |
| 92 | |
| 93 | // license returns the value of the license field. |
| 94 | // See also reconstructedLicenseString() |
| 95 | func (n *node) license() *string { |
| 96 | if !n.isLicense() { |
| 97 | return nil |
| 98 | } |
| 99 | return &(n.lic.license) |
| 100 | } |
| 101 | |
| 102 | func (n *node) exception() *string { |
| 103 | if !n.hasException() { |
| 104 | return nil |
| 105 | } |
| 106 | return &(n.lic.exception) |
| 107 | } |
| 108 | |
| 109 | func (n *node) hasPlus() bool { |
| 110 | if !n.isLicense() { |
| 111 | return false |
| 112 | } |
| 113 | return n.lic.hasPlus |
| 114 | } |
| 115 | |
| 116 | func (n *node) hasException() bool { |
| 117 | if !n.isLicense() { |
| 118 | return false |
| 119 | } |
| 120 | return n.lic.hasException |
| 121 | } |
| 122 | |
| 123 | func (n *node) isLicenseRef() bool { |
| 124 | return n.role == licenseRefNode |
| 125 | } |
| 126 | |
| 127 | func (n *node) licenseRef() *string { |
| 128 | if !n.isLicenseRef() { |
| 129 | return nil |
| 130 | } |
| 131 | return &(n.ref.licenseRef) |
| 132 | } |
| 133 | |
| 134 | func (n *node) documentRef() *string { |
| 135 | if !n.hasDocumentRef() { |
| 136 | return nil |
| 137 | } |
| 138 | return &(n.ref.documentRef) |
| 139 | } |
| 140 | |
| 141 | func (n *node) hasDocumentRef() bool { |
| 142 | if !n.isLicenseRef() { |
| 143 | return false |
| 144 | } |
| 145 | return n.ref.hasDocumentRef |
| 146 | } |
| 147 | |
| 148 | // reconstructedLicenseString returns the string representation of a license, license ref, or expression. |
| 149 | // TODO: Original had "NOASSERTION". Does that still apply? |
| 150 | func (n *node) reconstructedLicenseString() *string { |
| 151 | switch n.role { |
| 152 | case expressionNode: |
| 153 | return n.reconstructedExpressionString() |
| 154 | case licenseNode: |
| 155 | license := *n.license() |
| 156 | if n.hasPlus() && !strings.HasSuffix(strings.ToLower(license), "-or-later") { |
| 157 | license += "+" |
| 158 | } |
| 159 | if n.hasException() { |
| 160 | license += " WITH " + *n.exception() |
| 161 | } |
| 162 | return &license |
| 163 | case licenseRefNode: |
| 164 | license := "LicenseRef-" + *n.licenseRef() |
| 165 | if n.hasDocumentRef() { |
| 166 | license = "DocumentRef-" + *n.documentRef() + ":" + license |
| 167 | } |
| 168 | return &license |
| 169 | } |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | func (n *node) reconstructedExpressionString() *string { |
| 174 | if n == nil || !n.isExpression() { |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | left := n.left() |
| 179 | right := n.right() |
| 180 | if left == nil || right == nil { |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | leftStr := left.reconstructedLicenseString() |
| 185 | rightStr := right.reconstructedLicenseString() |
| 186 | if leftStr == nil || rightStr == nil { |
| 187 | return nil |
| 188 | } |
| 189 | |
| 190 | conj := n.conjunction() |
| 191 | if conj == nil { |
| 192 | return nil |
| 193 | } |
| 194 | |
| 195 | operator := strings.ToUpper(*conj) |
| 196 | if operator != "AND" && operator != "OR" { |
| 197 | return nil |
| 198 | } |
| 199 | |
| 200 | parentPrec := nodePrecedence(n) |
| 201 | leftRendered := *leftStr |
| 202 | if left.isExpression() && nodePrecedence(left) < parentPrec { |
| 203 | leftRendered = "(" + leftRendered + ")" |
| 204 | } |
| 205 | rightRendered := *rightStr |
| 206 | if right.isExpression() && nodePrecedence(right) < parentPrec { |
| 207 | rightRendered = "(" + rightRendered + ")" |
| 208 | } |
| 209 | |
| 210 | s := fmt.Sprintf("%s %s %s", leftRendered, operator, rightRendered) |
| 211 | return &s |
| 212 | } |
| 213 | |
| 214 | func nodePrecedence(n *node) int { |
| 215 | if n == nil { |
| 216 | return 0 |
| 217 | } |
| 218 | if !n.isExpression() { |
| 219 | // atomic (license/licenseRef) |
| 220 | return 3 |
| 221 | } |
| 222 | conj := n.conjunction() |
| 223 | if conj == nil { |
| 224 | return 0 |
| 225 | } |
| 226 | switch strings.ToLower(*conj) { |
| 227 | case "and": |
| 228 | return 2 |
| 229 | case "or": |
| 230 | return 1 |
| 231 | default: |
| 232 | return 0 |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // sortLicenses sorts an array of license and license reference nodes alphabetically based |
| 237 | // on their reconstructedLicenseString() representation. The sort function does not expect |
| 238 | // expression nodes, but if one is in the nodes list, it will sort to the end. |
| 239 | func sortLicenses(nodes []*node) { |
| 240 | sort.Slice(nodes, func(i, j int) bool { |
| 241 | if nodes[j].isExpression() { |
| 242 | // push second license toward end by saying first license is less than |
| 243 | return true |
| 244 | } |
| 245 | if nodes[i].isExpression() { |
| 246 | // push first license toward end by saying second license is less than |
| 247 | return false |
| 248 | } |
| 249 | return *nodes[i].reconstructedLicenseString() < *nodes[j].reconstructedLicenseString() |
| 250 | }) |
| 251 | } |
| 252 | |
| 253 | // ---------------------- Comparator Methods ---------------------- |
| 254 | |
| 255 | // licensesAreCompatible returns true if two licenses are compatible; otherwise, false. |
| 256 | // Two licenses are compatible if they are the same license or if they are in the same |
| 257 | // license group and they meet one of the following rules: |
| 258 | // |
| 259 | // * both licenses have the `hasPlus` flag set to true |
| 260 | // * the first license has the `hasPlus` flag and the second license is in the first license's range or greater |
| 261 | // * the second license has the `hasPlus` flag and the first license is in the second license's range or greater |
| 262 | // * both licenses are in the same range |
| 263 | func (nodes *nodePair) licensesAreCompatible() bool { |
| 264 | // checking ranges is expensive, so check for simple cases first |
| 265 | if !nodes.firstNode.isLicense() || !nodes.secondNode.isLicense() { |
| 266 | return false |
| 267 | } |
| 268 | if !nodes.exceptionsAreCompatible() { |
| 269 | return false |
| 270 | } |
| 271 | if nodes.licensesExactlyEqual() { |
| 272 | return true |
| 273 | } |
| 274 | |
| 275 | // simple cases don't apply, so check license ranges |
| 276 | // NOTE: Ranges are organized into groups (referred to as license groups) of the same base license (e.g. GPL). |
| 277 | // Groups have sub-groups of license versions (referred to as the range) where each member is considered |
| 278 | // to be the same version (e.g. {GPL-2.0, GPL-2.0-only}). The sub-groups are in ascending order within |
| 279 | // the license group, such that the first sub-group is considered to be less than the second sub-group, |
| 280 | // and so on. (e.g. {{GPL-1.0}, {GPL-2.0, GPL-2.0-only}} implies {GPL-1.0} < {GPL-2.0, GPL-2.0-only}). |
| 281 | if nodes.secondNode.hasPlus() { |
| 282 | if nodes.firstNode.hasPlus() { |
| 283 | // first+, second+ just need to be in same range group |
| 284 | return nodes.rangesAreCompatible() |
| 285 | } |
| 286 | // first, second+ requires first to be in range of second |
| 287 | return nodes.identifierInRange() |
| 288 | } |
| 289 | // else secondNode does not have plus |
| 290 | if nodes.firstNode.hasPlus() { |
| 291 | // first+, second requires second to be in range of first |
| 292 | revNodes := &nodePair{firstNode: nodes.secondNode, secondNode: nodes.firstNode} |
| 293 | return revNodes.identifierInRange() |
| 294 | } |
| 295 | // first, second requires both to be in same range group |
| 296 | return nodes.rangesEqual() |
| 297 | } |
| 298 | |
| 299 | // licenseRefsAreCompatible returns true if two license references are compatible; otherwise, false. |
| 300 | func (nodes *nodePair) licenseRefsAreCompatible() bool { |
| 301 | if !nodes.firstNode.isLicenseRef() || !nodes.secondNode.isLicenseRef() { |
| 302 | return false |
| 303 | } |
| 304 | |
| 305 | compatible := *nodes.firstNode.licenseRef() == *nodes.secondNode.licenseRef() |
| 306 | compatible = compatible && (nodes.firstNode.hasDocumentRef() == nodes.secondNode.hasDocumentRef()) |
| 307 | if compatible && nodes.firstNode.hasDocumentRef() { |
| 308 | compatible = compatible && (*nodes.firstNode.documentRef() == *nodes.secondNode.documentRef()) |
| 309 | } |
| 310 | return compatible |
| 311 | } |
| 312 | |
| 313 | // licenseRefsAreCompatible returns true if two licenses are in the same license group (e.g. all "GPL" licenses are in the same |
| 314 | // license group); otherwise, false. |
| 315 | func (nodes *nodePair) rangesAreCompatible() bool { |
| 316 | firstNode := *nodes.firstNode |
| 317 | secondNode := *nodes.secondNode |
| 318 | |
| 319 | firstRange := getLicenseRange(*firstNode.license()) |
| 320 | secondRange := getLicenseRange(*secondNode.license()) |
| 321 | |
| 322 | // When both licenses allow later versions (i.e. hasPlus==true), being in the same license |
| 323 | // group is sufficient for compatibility, as long as, any exception is also compatible |
| 324 | // Example: All Apache licenses (e.g. Apache-1.0, Apache-2.0) are in the same license group |
| 325 | return sameLicenseGroup(firstRange, secondRange) |
| 326 | } |
| 327 | |
| 328 | // identifierInRange returns true if the (first) simple license is in range of the (second) |
| 329 | // ranged license; otherwise, false. |
| 330 | func (nodes *nodePair) identifierInRange() bool { |
| 331 | simpleLicense := nodes.firstNode |
| 332 | plusLicense := nodes.secondNode |
| 333 | |
| 334 | return compareGT(simpleLicense, plusLicense) || compareEQ(simpleLicense, plusLicense) |
| 335 | } |
| 336 | |
| 337 | // exceptionsAreCompatible returns true if neither license has an exception or they have |
| 338 | // the same exception; otherwise, false |
| 339 | func (nodes *nodePair) exceptionsAreCompatible() bool { |
| 340 | firstNode := *nodes.firstNode |
| 341 | secondNode := *nodes.secondNode |
| 342 | |
| 343 | if !firstNode.hasException() && !secondNode.hasException() { |
| 344 | // if neither has an exception, then licenses are compatible |
| 345 | return true |
| 346 | } |
| 347 | |
| 348 | if firstNode.hasException() != secondNode.hasException() { |
| 349 | // if one has and exception and the other does not, then the license are NOT compatible |
| 350 | return false |
| 351 | } |
| 352 | |
| 353 | return *nodes.firstNode.exception() == *nodes.secondNode.exception() |
| 354 | } |
| 355 | |
| 356 | // rangesEqual returns true if the licenses are in the same range; otherwise, false |
| 357 | // (e.g. GPL-2.0-only == GPL-2.0) |
| 358 | func (nodes *nodePair) rangesEqual() bool { |
| 359 | return compareEQ(nodes.firstNode, nodes.secondNode) |
| 360 | } |
| 361 | |
| 362 | // licensesExactlyEqual returns true if the licenses are the same; otherwise, false |
| 363 | func (nodes *nodePair) licensesExactlyEqual() bool { |
| 364 | return strings.EqualFold(*nodes.firstNode.reconstructedLicenseString(), *nodes.secondNode.reconstructedLicenseString()) |
| 365 | } |