Skip to content

API Reference

CVE Utils provides a complete set of CVE processing functions, covering everything from basic format validation to complex analysis and processing.

Function Categories

🔍 Format & Validation

Functions for CVE format standardization and validity verification:

FunctionDescription
Format(cve string) stringConvert CVE to standard uppercase format
IsCve(text string) boolCheck if string is a valid CVE format
IsContainsCve(text string) boolCheck if string contains CVE
IsCveYearOk(cve string, cutoff int) boolCheck if CVE year is reasonable
ValidateCve(cve string) boolComprehensive validation of CVE identifier

📝 Extraction Methods

Functions for extracting information from text or CVE:

FunctionDescription
ExtractCve(text string) []stringExtract all CVE identifiers from text
ExtractFirstCve(text string) stringExtract the first CVE identifier
ExtractLastCve(text string) stringExtract the last CVE identifier
Split(cve string) (year string, seq string)Split CVE into year and sequence
ExtractCveYear(cve string) stringExtract CVE year (string)
ExtractCveYearAsInt(cve string) intExtract CVE year (integer)
ExtractCveSeq(cve string) stringExtract CVE sequence (string)
ExtractCveSeqAsInt(cve string) intExtract CVE sequence (integer)

🔄 Comparison & Sorting

Functions for CVE comparison and sorting:

FunctionDescription
CompareByYear(cveA, cveB string) intCompare two CVEs by year
SubByYear(cveA, cveB string) intCalculate year difference between two CVEs
CompareCves(cveA, cveB string) intComprehensive comparison of two CVEs
SortCves(cveSlice []string) []stringSort CVE slice

🎯 Filtering & Grouping

Functions for CVE filtering, grouping, and deduplication:

FunctionDescription
FilterCvesByYear(cveSlice []string, year int) []stringFilter CVEs by specific year
FilterCvesByYearRange(cveSlice []string, startYear, endYear int) []stringFilter CVEs by year range
GetRecentCves(cveSlice []string, years int) []stringGet CVEs from recent years
GroupByYear(cveSlice []string) map[string][]stringGroup CVEs by year
RemoveDuplicateCves(cveSlice []string) []stringRemove duplicate CVEs

Generation & Construction

Functions for generating new CVE identifiers:

FunctionDescription
GenerateCve(year int, seq int) stringGenerate CVE from year and sequence

Quick Reference

Common Operations

go
import "github.com/scagogogo/cve"

// Format
formatted := cve.Format(" cve-2022-12345 ")  // "CVE-2022-12345"

// Validate
isValid := cve.ValidateCve("CVE-2022-12345")  // true

// Extract
cves := cve.ExtractCve("Text contains CVE-2022-12345")  // ["CVE-2022-12345"]

// Sort
sorted := cve.SortCves([]string{"CVE-2022-2", "CVE-2021-1"})

// Group
grouped := cve.GroupByYear([]string{"CVE-2021-1", "CVE-2022-1"})

Function Return Types

Return TypeDescriptionExample
stringSingle string result, returns empty string for invalid input"CVE-2022-12345"
[]stringString slice, returns empty slice when no results["CVE-2022-1", "CVE-2022-2"]
boolBoolean value, indicates yes/no or true/falsetrue
intInteger, usually returns 0 for invalid input2022
map[string][]stringMap from string to string slice{"2022": ["CVE-2022-1"]}

Error Handling

CVE Utils functions are designed with good fault tolerance for invalid input:

  • String functions return empty string "" for invalid input
  • Integer functions return 0 for invalid input
  • Slice functions return empty slice []string{} for invalid input
  • Boolean functions return false for invalid or non-matching input

Performance Characteristics

  • Memory Efficient: Functions avoid unnecessary memory allocations
  • Concurrency Safe: All functions are concurrency-safe (stateless)
  • Regex Optimization: Uses compiled regular expressions internally
  • Batch Processing: Supports efficient batch operations

Usage Patterns

1. Data Cleaning Pipeline

go
func cleanCveData(rawData []string) []string {
    // 1. Extract all possible CVEs
    var allCves []string
    for _, text := range rawData {
        cves := cve.ExtractCve(text)
        allCves = append(allCves, cves...)
    }

    // 2. Remove duplicates
    unique := cve.RemoveDuplicateCves(allCves)

    // 3. Validate
    var valid []string
    for _, cveId := range unique {
        if cve.ValidateCve(cveId) {
            valid = append(valid, cveId)
        }
    }

    // 4. Sort
    return cve.SortCves(valid)
}

2. Conditional Filtering

go
func filterRecentHighPriority(cveList []string) []string {
    // Get CVEs from recent 2 years
    recent := cve.GetRecentCves(cveList, 2)

    // Further filtering (example: sequence number > 10000)
    var highPriority []string
    for _, cveId := range recent {
        if cve.ExtractCveSeqAsInt(cveId) > 10000 {
            highPriority = append(highPriority, cveId)
        }
    }
    
    return highPriority
}

3. Statistical Analysis

go
func analyzeCveDistribution(cveList []string) {
    // Group by year
    grouped := cve.GroupByYear(cveList)

    // Count for each year
    for year, cves := range grouped {
        fmt.Printf("Year %s: %d CVEs\n", year, len(cves))

        // Analyze sequence number range
        if len(cves) > 0 {
            sorted := cve.SortCves(cves)
            firstSeq := cve.ExtractCveSeqAsInt(sorted[0])
            lastSeq := cve.ExtractCveSeqAsInt(sorted[len(sorted)-1])
            fmt.Printf("  Sequence range: %d - %d\n", firstSeq, lastSeq)
        }
    }
}

Version Compatibility

Current API version: v1.0.0

  • Stable API: All public function signatures remain stable
  • Backward Compatible: New versions maintain backward compatibility
  • Semantic Versioning: Follows semantic versioning specification

Next Steps

Choose the API category you're interested in to learn more:

Or check out Examples to see real-world usage scenarios.

Released under the MIT License.