Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package compliance |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 21 | // LicenseCondition identifies a recognized license condition by setting the |
| 22 | // corresponding bit. |
| 23 | type LicenseCondition uint16 |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 24 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 25 | // LicenseConditionMask is a bitmask for the recognized license conditions. |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 26 | const LicenseConditionMask = LicenseCondition(0x1ff) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 27 | |
| 28 | const ( |
| 29 | // UnencumberedCondition identifies public domain or public domain- |
| 30 | // like license that disclaims copyright. |
| 31 | UnencumberedCondition = LicenseCondition(0x0001) |
| 32 | // PermissiveCondition identifies a license without notice or other |
| 33 | // significant requirements. |
| 34 | PermissiveCondition = LicenseCondition(0x0002) |
| 35 | // NoticeCondition identifies a typical open-source license with only |
| 36 | // notice or attribution requirements. |
| 37 | NoticeCondition = LicenseCondition(0x0004) |
| 38 | // ReciprocalCondition identifies a license with requirement to share |
| 39 | // the module's source only. |
| 40 | ReciprocalCondition = LicenseCondition(0x0008) |
| 41 | // RestrictedCondition identifies a license with requirement to share |
| 42 | // all source code linked to the module's source. |
| 43 | RestrictedCondition = LicenseCondition(0x0010) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 44 | // WeaklyRestrictedCondition identifies a RestrictedCondition waived |
| 45 | // for dynamic linking. |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 46 | WeaklyRestrictedCondition = LicenseCondition(0x0020) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 47 | // ProprietaryCondition identifies a license with source privacy |
| 48 | // requirements. |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 49 | ProprietaryCondition = LicenseCondition(0x0040) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 50 | // ByExceptionOnly identifies a license where policy requires product |
| 51 | // counsel review prior to use. |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 52 | ByExceptionOnlyCondition = LicenseCondition(0x0080) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 53 | // NotAllowedCondition identifies a license with onerous conditions |
| 54 | // where policy prohibits use. |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 55 | NotAllowedCondition = LicenseCondition(0x0100) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 56 | ) |
| 57 | |
| 58 | var ( |
| 59 | // RecognizedConditionNames maps condition strings to LicenseCondition. |
| 60 | RecognizedConditionNames = map[string]LicenseCondition{ |
Bob Badour | cac8a3c | 2022-11-22 12:04:10 -0800 | [diff] [blame] | 61 | "unencumbered": UnencumberedCondition, |
| 62 | "permissive": PermissiveCondition, |
| 63 | "notice": NoticeCondition, |
| 64 | "reciprocal": ReciprocalCondition, |
| 65 | "restricted": RestrictedCondition, |
| 66 | "restricted_if_statically_linked": WeaklyRestrictedCondition, |
| 67 | "proprietary": ProprietaryCondition, |
| 68 | "by_exception_only": ByExceptionOnlyCondition, |
| 69 | "not_allowed": NotAllowedCondition, |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 70 | } |
| 71 | ) |
| 72 | |
| 73 | // Name returns the condition string corresponding to the LicenseCondition. |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 74 | func (lc LicenseCondition) Name() string { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 75 | switch lc { |
| 76 | case UnencumberedCondition: |
| 77 | return "unencumbered" |
| 78 | case PermissiveCondition: |
| 79 | return "permissive" |
| 80 | case NoticeCondition: |
| 81 | return "notice" |
| 82 | case ReciprocalCondition: |
| 83 | return "reciprocal" |
| 84 | case RestrictedCondition: |
| 85 | return "restricted" |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 86 | case WeaklyRestrictedCondition: |
Bob Badour | cac8a3c | 2022-11-22 12:04:10 -0800 | [diff] [blame] | 87 | return "restricted_if_statically_linked" |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 88 | case ProprietaryCondition: |
| 89 | return "proprietary" |
| 90 | case ByExceptionOnlyCondition: |
| 91 | return "by_exception_only" |
| 92 | case NotAllowedCondition: |
| 93 | return "not_allowed" |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 94 | } |
Bob Badour | 10f5c48 | 2022-09-20 21:44:17 -0700 | [diff] [blame] | 95 | panic(fmt.Errorf("unrecognized license condition: %#v", lc)) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 96 | } |