From 75e24bea70ac02172dcb7b9891f01a035ad28ecb Mon Sep 17 00:00:00 2001
From: Daniel <dhaavi@users.noreply.github.com>
Date: Mon, 18 Dec 2023 17:10:01 +0100
Subject: [PATCH] Remove binmeta utils, which moved to portmaster/profile/icons

---
 utils/osdetail/binmeta.go           | 121 ----------------------------
 utils/osdetail/binmeta_default.go   |  15 ----
 utils/osdetail/binmeta_test.go      |  47 -----------
 utils/osdetail/binmeta_windows.go   |  78 ------------------
 utils/osdetail/test/.gitignore      |   2 -
 utils/osdetail/test/main_windows.go |  48 -----------
 6 files changed, 311 deletions(-)
 delete mode 100644 utils/osdetail/binmeta.go
 delete mode 100644 utils/osdetail/binmeta_default.go
 delete mode 100644 utils/osdetail/binmeta_test.go
 delete mode 100644 utils/osdetail/binmeta_windows.go
 delete mode 100644 utils/osdetail/test/.gitignore
 delete mode 100644 utils/osdetail/test/main_windows.go

diff --git a/utils/osdetail/binmeta.go b/utils/osdetail/binmeta.go
deleted file mode 100644
index 76c0e8d..0000000
--- a/utils/osdetail/binmeta.go
+++ /dev/null
@@ -1,121 +0,0 @@
-package osdetail
-
-import (
-	"path/filepath"
-	"regexp"
-	"strings"
-)
-
-var (
-	segmentsSplitter  = regexp.MustCompile("[^A-Za-z0-9]*[A-Z]?[a-z0-9]*")
-	nameOnly          = regexp.MustCompile("^[A-Za-z0-9]+$")
-	delimitersAtStart = regexp.MustCompile("^[^A-Za-z0-9]+")
-	delimitersOnly    = regexp.MustCompile("^[^A-Za-z0-9]+$")
-	removeQuotes      = strings.NewReplacer(`"`, ``, `'`, ``)
-)
-
-// GenerateBinaryNameFromPath generates a more human readable binary name from
-// the given path. This function is used as fallback in the GetBinaryName
-// functions.
-func GenerateBinaryNameFromPath(path string) string {
-	// Get file name from path.
-	_, fileName := filepath.Split(path)
-
-	// Split up into segments.
-	segments := segmentsSplitter.FindAllString(fileName, -1)
-
-	// Remove last segment if it's an extension.
-	if len(segments) >= 2 {
-		switch strings.ToLower(segments[len(segments)-1]) {
-		case
-			".exe",      // Windows Executable
-			".msi",      // Windows Installer
-			".bat",      // Windows Batch File
-			".cmd",      // Windows Command Script
-			".ps1",      // Windows Powershell Cmdlet
-			".run",      // Linux Executable
-			".appimage", // Linux AppImage
-			".app",      // MacOS Executable
-			".action",   // MacOS Automator Action
-			".out":      // Generic Compiled Executable
-			segments = segments[:len(segments)-1]
-		}
-	}
-
-	// Debugging snippet:
-	// fmt.Printf("segments: %s\n", segments)
-
-	// Go through segments and collect name parts.
-	nameParts := make([]string, 0, len(segments))
-	var fragments string
-	for _, segment := range segments {
-		// Group very short segments.
-		if len(delimitersAtStart.ReplaceAllString(segment, "")) <= 2 {
-			fragments += segment
-			continue
-		} else if fragments != "" {
-			nameParts = append(nameParts, fragments)
-			fragments = ""
-		}
-
-		// Add segment to name.
-		nameParts = append(nameParts, segment)
-	}
-	// Add last fragment.
-	if fragments != "" {
-		nameParts = append(nameParts, fragments)
-	}
-
-	// Debugging snippet:
-	// fmt.Printf("parts: %s\n", nameParts)
-
-	// Post-process name parts
-	for i := range nameParts {
-		// Remove any leading delimiters.
-		nameParts[i] = delimitersAtStart.ReplaceAllString(nameParts[i], "")
-
-		// Title-case name-only parts.
-		if nameOnly.MatchString(nameParts[i]) {
-			nameParts[i] = strings.Title(nameParts[i]) //nolint:staticcheck
-		}
-	}
-
-	// Debugging snippet:
-	// fmt.Printf("final: %s\n", nameParts)
-
-	return strings.Join(nameParts, " ")
-}
-
-func cleanFileDescription(fileDescr string) string {
-	fields := strings.Fields(fileDescr)
-
-	// Clean out and `"` and `'`.
-	for i := range fields {
-		fields[i] = removeQuotes.Replace(fields[i])
-	}
-
-	// If there is a 1 or 2 character delimiter field, only use fields before it.
-	endIndex := len(fields)
-	for i, field := range fields {
-		// Ignore the first field as well as fields with more than two characters.
-		if i >= 1 && len(field) <= 2 && !nameOnly.MatchString(field) {
-			endIndex = i
-			break
-		}
-	}
-
-	// Concatenate name
-	binName := strings.Join(fields[:endIndex], " ")
-
-	// If there are multiple sentences, only use the first.
-	if strings.Contains(binName, ". ") {
-		binName = strings.SplitN(binName, ". ", 2)[0]
-	}
-
-	// If does not have any characters or numbers, return an empty string.
-	if delimitersOnly.MatchString(binName) {
-		return ""
-	}
-
-	return strings.TrimSpace(binName)
-}
diff --git a/utils/osdetail/binmeta_default.go b/utils/osdetail/binmeta_default.go
deleted file mode 100644
index 754cceb..0000000
--- a/utils/osdetail/binmeta_default.go
+++ /dev/null
@@ -1,15 +0,0 @@
-//go:build !windows
-
-package osdetail
-
-// GetBinaryNameFromSystem queries the operating system for a human readable
-// name for the given binary path.
-func GetBinaryNameFromSystem(path string) (string, error) {
-	return "", ErrNotSupported
-}
-
-// GetBinaryIconFromSystem queries the operating system for the associated icon
-// for a given binary path.
-func GetBinaryIconFromSystem(path string) (string, error) {
-	return "", ErrNotSupported
-}
diff --git a/utils/osdetail/binmeta_test.go b/utils/osdetail/binmeta_test.go
deleted file mode 100644
index cec2a5d..0000000
--- a/utils/osdetail/binmeta_test.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package osdetail
-
-import (
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestGenerateBinaryNameFromPath(t *testing.T) {
-	t.Parallel()
-
-	assert.Equal(t, "Nslookup", GenerateBinaryNameFromPath("nslookup.exe"))
-	assert.Equal(t, "System Settings", GenerateBinaryNameFromPath("SystemSettings.exe"))
-	assert.Equal(t, "One Drive Setup", GenerateBinaryNameFromPath("OneDriveSetup.exe"))
-	assert.Equal(t, "Msedge", GenerateBinaryNameFromPath("msedge.exe"))
-	assert.Equal(t, "SIH Client", GenerateBinaryNameFromPath("SIHClient.exe"))
-	assert.Equal(t, "Openvpn Gui", GenerateBinaryNameFromPath("openvpn-gui.exe"))
-	assert.Equal(t, "Portmaster Core v0-1-2", GenerateBinaryNameFromPath("portmaster-core_v0-1-2.exe"))
-	assert.Equal(t, "Win Store App", GenerateBinaryNameFromPath("WinStore.App.exe"))
-	assert.Equal(t, "Test Script", GenerateBinaryNameFromPath(".test-script"))
-	assert.Equal(t, "Browser Broker", GenerateBinaryNameFromPath("browser_broker.exe"))
-	assert.Equal(t, "Virtual Box VM", GenerateBinaryNameFromPath("VirtualBoxVM"))
-	assert.Equal(t, "Io Elementary Appcenter", GenerateBinaryNameFromPath("io.elementary.appcenter"))
-	assert.Equal(t, "Microsoft Windows Store", GenerateBinaryNameFromPath("Microsoft.WindowsStore"))
-}
-
-func TestCleanFileDescription(t *testing.T) {
-	t.Parallel()
-
-	assert.Equal(t, "Product Name", cleanFileDescription("Product Name. Does this and that."))
-	assert.Equal(t, "Product Name", cleanFileDescription("Product Name - Does this and that."))
-	assert.Equal(t, "Product Name", cleanFileDescription("Product Name / Does this and that."))
-	assert.Equal(t, "Product Name", cleanFileDescription("Product Name :: Does this and that."))
-	assert.Equal(t, "/ Product Name", cleanFileDescription("/ Product Name"))
-	assert.Equal(t, "Product", cleanFileDescription("Product / Name"))
-	assert.Equal(t, "Software 2", cleanFileDescription("Software 2"))
-	assert.Equal(t, "Launcher for Software 2", cleanFileDescription("Launcher for 'Software 2'"))
-	assert.Equal(t, "", cleanFileDescription(". / Name"))
-	assert.Equal(t, "", cleanFileDescription(". "))
-	assert.Equal(t, "", cleanFileDescription("."))
-	assert.Equal(t, "N/A", cleanFileDescription("N/A"))
-
-	assert.Equal(t,
-		"Product Name a Does this and that.",
-		cleanFileDescription("Product Name a Does this and that."),
-	)
-}
diff --git a/utils/osdetail/binmeta_windows.go b/utils/osdetail/binmeta_windows.go
deleted file mode 100644
index 056445c..0000000
--- a/utils/osdetail/binmeta_windows.go
+++ /dev/null
@@ -1,78 +0,0 @@
-package osdetail
-
-import (
-	"fmt"
-)
-
-const powershellGetFileDescription = `Get-ItemProperty %q | Select -ExpandProperty VersionInfo | Select -ExpandProperty FileDescription`
-
-// GetBinaryNameFromSystem queries the operating system for a human readable
-// name for the given binary path.
-func GetBinaryNameFromSystem(path string) (string, error) {
-	// Get FileProperties via Powershell call.
-	output, err := RunPowershellCmd(fmt.Sprintf(powershellGetFileDescription, path))
-	if err != nil {
-		return "", fmt.Errorf("failed to get file properties of %s: %s", path, err)
-	}
-
-	// Clean name.
-	binName := cleanFileDescription(string(output))
-	if binName != "" {
-		return binName, nil
-	}
-
-	// Generate a default name as default.
-	return "", ErrNotFound
-}
-
-const powershellGetIcon = `Add-Type -AssemblyName System.Drawing
-$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon(%q)
-$MemoryStream = New-Object System.IO.MemoryStream
-$Icon.save($MemoryStream)
-$Bytes = $MemoryStream.ToArray()
-$MemoryStream.Flush()
-$MemoryStream.Dispose()
-[convert]::ToBase64String($Bytes)`
-
-// TODO: This returns a small and crappy icon.
-
-// Saving a better icon to file works:
-/*
-Add-Type -AssemblyName System.Drawing
-$ImgList = New-Object System.Windows.Forms.ImageList
-$ImgList.ImageSize = New-Object System.Drawing.Size(256,256)
-$ImgList.ColorDepth = 32
-$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
-$ImgList.Images.Add($Icon);
-$BigIcon = $ImgList.Images.Item(0)
-$BigIcon.Save("test.png")
-*/
-
-// But not saving to a memory stream:
-/*
-Add-Type -AssemblyName System.Drawing
-$ImgList = New-Object System.Windows.Forms.ImageList
-$ImgList.ImageSize = New-Object System.Drawing.Size(256,256)
-$ImgList.ColorDepth = 32
-$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
-$ImgList.Images.Add($Icon);
-$MemoryStream = New-Object System.IO.MemoryStream
-$BigIcon = $ImgList.Images.Item(0)
-$BigIcon.Save($MemoryStream)
-$Bytes = $MemoryStream.ToArray()
-$MemoryStream.Flush()
-$MemoryStream.Dispose()
-[convert]::ToBase64String($Bytes)
-*/
-
-// GetBinaryIconFromSystem queries the operating system for the associated icon
-// for a given binary path and returns it as a data-URL.
-func GetBinaryIconFromSystem(path string) (string, error) {
-	// Get Associated File Icon via Powershell call.
-	output, err := RunPowershellCmd(fmt.Sprintf(powershellGetIcon, path))
-	if err != nil {
-		return "", fmt.Errorf("failed to get file properties of %s: %s", path, err)
-	}
-
-	return "data:image/png;base64," + string(output), nil
-}
diff --git a/utils/osdetail/test/.gitignore b/utils/osdetail/test/.gitignore
deleted file mode 100644
index 35ccb46..0000000
--- a/utils/osdetail/test/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-test
-test.exe
diff --git a/utils/osdetail/test/main_windows.go b/utils/osdetail/test/main_windows.go
deleted file mode 100644
index 77928bd..0000000
--- a/utils/osdetail/test/main_windows.go
+++ /dev/null
@@ -1,48 +0,0 @@
-package main
-
-import (
-	"fmt"
-
-	"github.com/safing/portbase/utils/osdetail"
-)
-
-func main() {
-	fmt.Println("Binary Names:")
-	printBinaryName("openvpn-gui.exe", `C:\Program Files\OpenVPN\bin\openvpn-gui.exe`)
-	printBinaryName("firefox.exe", `C:\Program Files (x86)\Mozilla Firefox\firefox.exe`)
-	printBinaryName("powershell.exe", `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`)
-	printBinaryName("explorer.exe", `C:\Windows\explorer.exe`)
-	printBinaryName("svchost.exe", `C:\Windows\System32\svchost.exe`)
-
-	fmt.Println("\n\nBinary Icons:")
-	printBinaryIcon("openvpn-gui.exe", `C:\Program Files\OpenVPN\bin\openvpn-gui.exe`)
-	printBinaryIcon("firefox.exe", `C:\Program Files (x86)\Mozilla Firefox\firefox.exe`)
-	printBinaryIcon("powershell.exe", `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`)
-	printBinaryIcon("explorer.exe", `C:\Windows\explorer.exe`)
-	printBinaryIcon("svchost.exe", `C:\Windows\System32\svchost.exe`)
-
-	fmt.Println("\n\nSvcHost Service Names:")
-	names, err := osdetail.GetAllServiceNames()
-	if err != nil {
-		panic(err)
-	}
-	fmt.Printf("%+v\n", names)
-}
-
-func printBinaryName(name, path string) {
-	binName, err := osdetail.GetBinaryNameFromSystem(path)
-	if err != nil {
-		fmt.Printf("%s: ERROR: %s\n", name, err)
-	} else {
-		fmt.Printf("%s: %s\n", name, binName)
-	}
-}
-
-func printBinaryIcon(name, path string) {
-	binIcon, err := osdetail.GetBinaryIconFromSystem(path)
-	if err != nil {
-		fmt.Printf("%s: ERROR: %s\n", name, err)
-	} else {
-		fmt.Printf("%s: %s\n", name, binIcon)
-	}
-}