init.go (3037B)
1 package mangler 2 3 import ( 4 "net" 5 "net/netip" 6 "net/url" 7 "reflect" 8 "time" 9 _ "unsafe" 10 ) 11 12 func init() { 13 // Register standard library performant manglers. 14 Register(reflect.TypeOf(net.IPAddr{}), mangle_ipaddr) 15 Register(reflect.TypeOf(&net.IPAddr{}), mangle_ipaddr_ptr) 16 Register(reflect.TypeOf(netip.Addr{}), mangle_addr) 17 Register(reflect.TypeOf(&netip.Addr{}), mangle_addr_ptr) 18 Register(reflect.TypeOf(netip.AddrPort{}), mangle_addrport) 19 Register(reflect.TypeOf(&netip.AddrPort{}), mangle_addrport_ptr) 20 Register(reflect.TypeOf(time.Time{}), mangle_time) 21 Register(reflect.TypeOf(&time.Time{}), mangle_time_ptr) 22 Register(reflect.TypeOf(url.URL{}), mangle_url) 23 Register(reflect.TypeOf(&url.URL{}), mangle_url_ptr) 24 } 25 26 //go:linkname time_sec time.(*Time).sec 27 func time_sec(*time.Time) int64 28 29 //go:linkname time_nsec time.(*Time).nsec 30 func time_nsec(*time.Time) int32 31 32 //go:linkname time_stripMono time.(*Time).stripMono 33 func time_stripMono(*time.Time) 34 35 func mangle_url(buf []byte, a any) []byte { 36 u := (*url.URL)(iface_value(a)) 37 return append(buf, u.String()...) 38 } 39 40 func mangle_url_ptr(buf []byte, a any) []byte { 41 if ptr := (*url.URL)(iface_value(a)); ptr != nil { 42 s := ptr.String() 43 buf = append(buf, '1') 44 return append(buf, s...) 45 } 46 buf = append(buf, '0') 47 return buf 48 } 49 50 func mangle_time(buf []byte, a any) []byte { 51 t := *(*time.Time)(iface_value(a)) 52 time_stripMono(&t) // force non-monotonic time value. 53 buf = append_uint64(buf, uint64(time_sec(&t))) 54 buf = append_uint32(buf, uint32(time_nsec(&t))) 55 return buf 56 } 57 58 func mangle_time_ptr(buf []byte, a any) []byte { 59 if ptr := (*time.Time)(iface_value(a)); ptr != nil { 60 t := *ptr 61 buf = append(buf, '1') 62 time_stripMono(&t) // force non-monotonic time value. 63 buf = append_uint64(buf, uint64(time_sec(&t))) 64 buf = append_uint32(buf, uint32(time_nsec(&t))) 65 return buf 66 } 67 buf = append(buf, '0') 68 return buf 69 } 70 71 func mangle_ipaddr(buf []byte, a any) []byte { 72 i := *(*net.IPAddr)(iface_value(a)) 73 buf = append(buf, i.IP...) 74 buf = append(buf, i.Zone...) 75 return buf 76 } 77 78 func mangle_ipaddr_ptr(buf []byte, a any) []byte { 79 if ptr := (*net.IPAddr)(iface_value(a)); ptr != nil { 80 buf = append(buf, '1') 81 buf = append(buf, ptr.IP...) 82 buf = append(buf, ptr.Zone...) 83 return buf 84 } 85 buf = append(buf, '0') 86 return buf 87 } 88 89 func mangle_addr(buf []byte, a any) []byte { 90 i := (*netip.Addr)(iface_value(a)) 91 b, _ := i.MarshalBinary() 92 return append(buf, b...) 93 } 94 95 func mangle_addr_ptr(buf []byte, a any) []byte { 96 if ptr := (*netip.Addr)(iface_value(a)); ptr != nil { 97 buf = append(buf, '1') 98 b, _ := ptr.MarshalBinary() 99 return append(buf, b...) 100 } 101 buf = append(buf, '0') 102 return buf 103 } 104 105 func mangle_addrport(buf []byte, a any) []byte { 106 i := (*netip.AddrPort)(iface_value(a)) 107 b, _ := i.MarshalBinary() 108 return append(buf, b...) 109 } 110 111 func mangle_addrport_ptr(buf []byte, a any) []byte { 112 if ptr := (*netip.AddrPort)(iface_value(a)); ptr != nil { 113 buf = append(buf, '1') 114 b, _ := ptr.MarshalBinary() 115 return append(buf, b...) 116 } 117 buf = append(buf, '0') 118 return buf 119 }