Skip to content

Commit

Permalink
auto: sync upstream / update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
paepckehh committed May 22, 2024
1 parent fcb2523 commit 657cf7e
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 26 deletions.
File renamed without changes.
19 changes: 19 additions & 0 deletions .attic/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package asn2pf

import "inet.af/netaddr"

// tailscale converter [... the missing parts that did not make it into golang std net/netip lib merger] [why?]
func range2net_tailscale(s, e string) (prefix []byte) {
netrange, err := netaddr.ParseIPRange(s + "-" + e)
if err != nil {
OutErr("SKIP - unable to parse range [" + s + "-" + e + "]")
return
}
prefixes := netrange.Prefixes()
for _, p := range prefixes {
net, _ := p.MarshalText()
prefix = append(prefix, net...)
prefix = append(prefix, []byte(" ")...)
}
return
}
1 change: 1 addition & 0 deletions .build.hqx
1 change: 1 addition & 0 deletions .build.sh
Empty file added .codeberg.org
Empty file.
1 change: 1 addition & 0 deletions .commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
n/a
219 changes: 219 additions & 0 deletions .dev/compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package asn2pf

import (
"bytes"
"compress/gzip"
"io"
"io/fs"
"math/bits"
"os"
"runtime"
"strconv"

"github.com/klauspost/compress/zstd"
"github.com/ulikunitz/xz"
)

//
// EXTERNAL INTERFACE
//

func decompress(algo string, in []byte) []byte {
switch algo {
case "xz":
switch {
case IsExec(_exec_xz):
return decompress_cmd(_exec_xz, in)
}
return decompress_go(algo, in)
case "gzip":
switch {
case IsExec(_exec_gzip):
return decompress_cmd(_exec_gzip, in)
}
return decompress_go(algo, in)
case "zstd":
switch {
case IsExec(_exec_zstd):
return decompress_cmd(_exec_zstd, in)
}
return decompress_go(algo, in)
}
panic("internal error: unsupported decompress algo [" + algo + "]")
}

func compress(algo string, level int, in []byte) []byte {
switch algo {
case "xz":
switch {
case IsExec(_exec_xz):
return compress_cmd(_exec_xz, level, in)
}
return compress_go(algo, level, in)
case "gzip":
switch {
case IsExec(_exec_gzip):
return compress_cmd(_exec_gzip, level, in)
}
return compress_go(algo, level, in)
case "zstd":
switch {
case IsExec(_exec_zstd):
return compress_cmd(_exec_zstd, level, in)
}
return compress_go(algo, level, in)
}
panic("internal error: unsupported compression algo [" + algo + "]")
}

//
// INTERNAL BACKEND: NATIVE GO
//

func decompress_go(algo string, in []byte) (out []byte) {
br := bytes.NewReader(in)
var err error
var r io.Reader
switch algo {
case "xz":
r, err = xz.NewReader(br)
case "gzip":
r, err = gzip.NewReader(br)
case "zstd":
r, err = zstd.NewReader(br)
default:
panic("internal error: unsupported native go decompress algo [" + algo + "]")
}
if err != nil {
panic("internal error: unable to create new decompress reader [" + algo + "]")
}
out, err = io.ReadAll(r)
if err != nil {
OutErr("decompress block [" + algo + "]")
return
}
return
}

func compress_go(algo string, level int, in []byte) (out []byte) {
threads := runtime.NumCPU()
switch algo {
case "zstd":
switch bits.UintSize {
case 32:
OutInf("32bit os mem addr range alloc workaround activated [slow single threaded mode]")
runtime.GC()
threads = 1
}
w, err := zstd.NewWriter(nil,
zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)),
zstd.WithEncoderCRC(false),
zstd.WithZeroFrames(false),
zstd.WithLowerEncoderMem(false),
zstd.WithAllLitEntropyCompression(true),
zstd.WithNoEntropyCompression(false),
zstd.WithWindowSize(zstd.MaxWindowSize),
zstd.WithEncoderConcurrency(threads))
if err != nil {
panic("internal error: unable to create new ecompress writer [" + algo + "]")
}
out = w.EncodeAll(in, nil)
w.Close()
default:
panic("internal error: unsupported native go compress algo [" + algo + "]")
}
return
}

//
// INTERNAL BACKENDS: CMD PIPE WRAPPER
//

const (
_exec_xz = "DISABLE/usr/bin/xz"
_exec_gzip = "DISABLE/usr/bin/gzip"
_exec_zstd = "DISABLE/usr/bin/zstd"
)

func decompress_cmd(exec string, in []byte) (out []byte) {
cmdopt := " --decompress --stdin --stdout --quiet"
switch exec {
case _exec_xz:
case _exec_gzip:
case _exec_zstd:
default:
panic("internal error: unsupported cmd decompress [" + exec + "]")
}
/*
cmd := exec.Command(exec,cmdopt)
cmd.Stdout =
cmd.Stderr = os.Stdout
cmd.Stdin =
cmd.Start()
cmd.Wait()
*/
_ = in
_ = cmdopt
return
}

func compress_cmd(exec string, level int, in []byte) (out []byte) {
cmdopt := " --compress --stdin --stdout --quiet "
threads := runtime.NumCPU()
switch exec {
case _exec_xz:
case _exec_gzip:
case _exec_zstd:
switch bits.UintSize {
case 32:
if level > 6 {
threads = 1
if level > 19 {
level = 19
}
}
case 64:
if level > 19 {
cmdopt += " --ultra --long"
}
default:
panic("unsupported os plattform, no [32bit|64bit]")
}
cmdopt += "-" + strconv.Itoa(level) + " --threads=" + strconv.Itoa(threads)
default:
panic("internal error: unsupported cmd compress [" + exec + "]")
}
/*
cmd := exec.Command(exec, cmdopt)
cmd.Stdout =
cmd.Stderr = os.Stdout
cmd.Stdin =
cmd.Start()
cmd.Wait()
*/
_ = in
_ = cmdopt
return
}

//
// INTERNAL BACKENDS: CGO BINDINGS
//

//
// Little Helper
//

func IsExec(filename string) bool {
inf, err := os.Lstat(filename)
if err != nil {
return false
}
switch mode := inf.Mode(); {
case mode.IsRegular():
return true
case mode&fs.ModeSymlink != 0:
return true
}
return false
}
13 changes: 13 additions & 0 deletions .dev/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package asn2pf

import "inet.af/netaddr"

// tailscale converter [... the missing parts that did not make it into golang std net/netip lib merger] [why?]
func range2net_tailscale(s, e string) []netaddr.IPPrefix {
netrange, err := netaddr.ParseIPRange(s + "-" + e)
if err != nil {
OutErr("SKIP - unable to parse range [" + s + "-" + e + "]")
return []netaddr.IPPrefix{}
}
return netrange.Prefixes()
}
62 changes: 62 additions & 0 deletions .dev/convert_internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package asn2pf

// internal opimized converter implementation
// faster, smaller, dependency free & and more eco friendly - but currently ip4 only!

import (
"fmt"
"math"
"strconv"
"strings"
)

func range2net_internal(s, e string) (cidrs []byte) {
start, end := ip2int(s), ip2int(e)
cidr2mask := get_cidr_ipv4_masks()
for end >= start {
maxSize := 32
for maxSize > 0 {
maskedBase := start & cidr2mask[maxSize-1]
if maskedBase != start {
break
}
maxSize--
}
x := math.Log(float64(end-start+1)) / math.Log(2)
maxDiff := 32 - int(math.Floor(x))
if maxSize < maxDiff {
maxSize = maxDiff
}
cidrs = append(cidrs, []byte(int2ip(start)+"/"+strconv.Itoa(maxSize)+" ")...)
start += uint32(math.Exp2(float64(32 - maxSize)))
}
return
}

func ip2int(ip string) uint32 {
octets := [4]uint64{}
for i, v := range strings.SplitN(ip, ".", 4) {
octets[i], _ = strconv.ParseUint(v, 10, 32)
}
return uint32((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3])
}

func int2ip(ip uint32) (iP string) {
return fmt.Sprintf("%d.%d.%d.%d", ip>>24, (ip&0x00FFFFFF)>>16, (ip&0x0000FFFF)>>8, ip&0x000000FF)
}

func get_cidr_ipv4_masks() []uint32 {
return []uint32{
0x00000000, 0x80000000, 0xC0000000,
0xE0000000, 0xF0000000, 0xF8000000,
0xFC000000, 0xFE000000, 0xFF000000,
0xFF800000, 0xFFC00000, 0xFFE00000,
0xFFF00000, 0xFFF80000, 0xFFFC0000,
0xFFFE0000, 0xFFFF0000, 0xFFFF8000,
0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00,
0xFFFFFF00, 0xFFFFFF80, 0xFFFFFFC0,
0xFFFFFFE0, 0xFFFFFFF0, 0xFFFFFFF8,
0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF,
}
}
Loading

0 comments on commit 657cf7e

Please sign in to comment.