2024-04-02 12:03:40 +02:00
|
|
|
package mangler
|
|
|
|
|
|
|
|
import (
|
2024-06-21 17:43:17 +02:00
|
|
|
"unsafe"
|
2024-04-02 12:03:40 +02:00
|
|
|
_ "unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notes:
|
|
|
|
// the use of unsafe conversion from the direct interface values to
|
|
|
|
// the chosen types in each of the below functions allows us to convert
|
|
|
|
// not only those types directly, but anything type-aliased to those
|
|
|
|
// types. e.g. `time.Duration` directly as int64.
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_string(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append(buf, *(*string)(ptr)...)
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_string_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
s := *(*[]string)(ptr)
|
2024-04-02 12:03:40 +02:00
|
|
|
for _, s := range s {
|
|
|
|
buf = append(buf, s...)
|
|
|
|
buf = append(buf, ',')
|
|
|
|
}
|
|
|
|
if len(s) > 0 {
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_bool(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
if *(*bool)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
return append(buf, '1')
|
|
|
|
}
|
|
|
|
return append(buf, '0')
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_bool_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, b := range *(*[]bool)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
if b {
|
|
|
|
buf = append(buf, '1')
|
|
|
|
} else {
|
|
|
|
buf = append(buf, '0')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_8bit(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append(buf, *(*uint8)(ptr))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_8bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append(buf, *(*[]uint8)(ptr)...)
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_16bit(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append_uint16(buf, *(*uint16)(ptr))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_16bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, u := range *(*[]uint16)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
buf = append_uint16(buf, u)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_32bit(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append_uint32(buf, *(*uint32)(ptr))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_32bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, u := range *(*[]uint32)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
buf = append_uint32(buf, u)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_64bit(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append_uint64(buf, *(*uint64)(ptr))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_64bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, u := range *(*[]uint64)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
buf = append_uint64(buf, u)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_int(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
return append_uint64(buf, uint64(*(*uint)(ptr)))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_int_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, u := range *(*[]uint)(ptr) {
|
|
|
|
buf = append_uint64(buf, uint64(u))
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
2024-06-21 17:43:17 +02:00
|
|
|
return buf
|
2024-04-02 12:03:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_128bit(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
u2 := *(*[2]uint64)(ptr)
|
2024-04-02 12:03:40 +02:00
|
|
|
buf = append_uint64(buf, u2[0])
|
|
|
|
buf = append_uint64(buf, u2[1])
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2024-06-21 17:43:17 +02:00
|
|
|
func mangle_128bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
|
|
|
|
for _, u2 := range *(*[][2]uint64)(ptr) {
|
2024-04-02 12:03:40 +02:00
|
|
|
buf = append_uint64(buf, u2[0])
|
|
|
|
buf = append_uint64(buf, u2[1])
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|