wfp2

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wfp2 generates WFP2 (token-aware) fingerprint minutiae: a language-aware winnowing that detects the source language, strips comments and normalizes the source to a byte stream (identifiers, type-runs, reserved words and punctuation each mapped to fixed codes) before winnowing it. Cosmetic edits — renamed identifiers, reformatting, changed comments — leave the fingerprint unchanged, which is what lets wfp2 match snippets wfp1 would miss.

This is a faithful port of the reference implementation (scanoss-go pkg/fingerprint/wfp2), which the engine mirrors byte-for-byte in C (engine src/wfp2.c) and which the mined KB was generated with. The output MUST stay byte-identical to both, so the algorithm here — the normalization tables, GRAM/WINDOW, the CRC32C winnowing and the little-endian min-hash re-hash — is reproduced verbatim and must not be "improved". The sole deliberate departure is a deterministic language tie-break; see DetectLanguage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectLanguage

func DetectLanguage(content string) string

DetectLanguage returns the best-scoring language's key, or "" when nothing matches (which leaves the source uncommented-stripped — see RemoveCommentsFromSource). Scoring counts every (token, keyword) hit where the token equals or is prefixed by the keyword, exactly as the reference does.

func Minutiae

func Minutiae(src []byte) map[int][]uint32

Minutiae computes the wfp2 minutiae of a source file: a map from line number to the CRC32C minutiae recorded on that line. It runs the full pipeline — DetectLanguage → RemoveCommentsFromSource → Normalizex → winnowing — then, per window, hashes the little-endian bytes of the window's minimum gram-hash and records it when it differs from the previous one (consecutive duplicates are dropped).

The returned map matches the reference's line→hashes map exactly; callers format it as the "N=hash,hash,..." minutiae lines of the WFP (see pkg/wfp).

func Normalizex

func Normalizex(input string) string

Normalizex maps source text to the wfp2 normalized byte stream, line by line:

  • a reserved word or a single punctuation char → its elementsMap byte,
  • a type keyword → 0x00 (its identity is dropped; the lastWasType guard suppresses a repeated 0x00 only for an immediately-adjacent type, which separators prevent),
  • any other identifier → 0xff,
  • characters not in elementsMap (whitespace and the like) → dropped,
  • newlines preserved between lines (so winnowing can number lines).

elementsMap is consulted before typesMap, so a token that is both takes its element code. Input is lower-cased first, exactly as the reference does.

func RemoveCommentsFromSource

func RemoveCommentsFromSource(code string, lang string) string

RemoveCommentsFromSource strips comments from code for the given language, preserving the line count: a line that becomes empty is kept as an empty line, so line numbers in the fingerprint still line up with the source. An unknown language returns the code unchanged. The algorithm is a line-by-line port of the reference and must track its trimming exactly.

func SortedLines

func SortedLines(m map[int][]uint32) []int

SortedLines returns the line numbers of a minutiae map in ascending order — the order the minutiae must be emitted in.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL