| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | package mangler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"math/bits" | 
					
						
							|  |  |  | 	_ "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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_string(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	return append(buf, *(*string)(iface_value(a))...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_string_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*string)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		return append(buf, *ptr...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_string_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	s := *(*[]string)(iface_value(a)) | 
					
						
							|  |  |  | 	for _, s := range s { | 
					
						
							|  |  |  | 		buf = append(buf, s...) | 
					
						
							|  |  |  | 		buf = append(buf, ',') | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(s) > 0 { | 
					
						
							|  |  |  | 		buf = buf[:len(buf)-1] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_bool(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if *(*bool)(iface_value(a)) { | 
					
						
							|  |  |  | 		return append(buf, '1') | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return append(buf, '0') | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_bool_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*bool)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		if *ptr { | 
					
						
							|  |  |  | 			return append(buf, '1') | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return append(buf, '0') | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_bool_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	for _, b := range *(*[]bool)(iface_value(a)) { | 
					
						
							|  |  |  | 		if b { | 
					
						
							|  |  |  | 			buf = append(buf, '1') | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			buf = append(buf, '0') | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_8bit(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	return append(buf, *(*uint8)(iface_value(a))) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_8bit_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*uint8)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		return append(buf, *ptr) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_8bit_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	return append(buf, *(*[]uint8)(iface_value(a))...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_16bit(buf []byte, a any) []byte { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 	return append_uint16(buf, *(*uint16)(iface_value(a))) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_16bit_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*uint16)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		return append_uint16(buf, *ptr) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_16bit_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	for _, u := range *(*[]uint16)(iface_value(a)) { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		buf = append_uint16(buf, u) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_32bit(buf []byte, a any) []byte { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 	return append_uint32(buf, *(*uint32)(iface_value(a))) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_32bit_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*uint32)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		return append_uint32(buf, *ptr) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_32bit_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	for _, u := range *(*[]uint32)(iface_value(a)) { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		buf = append_uint32(buf, u) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_64bit(buf []byte, a any) []byte { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 	return append_uint64(buf, *(*uint64)(iface_value(a))) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_64bit_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*uint64)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		return append_uint64(buf, *ptr) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_64bit_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	for _, u := range *(*[]uint64)(iface_value(a)) { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		buf = append_uint64(buf, u) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mangle_platform_int contains the correct iface mangler on runtime for platform int size. | 
					
						
							|  |  |  | var mangle_platform_int = func() Mangler { | 
					
						
							|  |  |  | 	switch bits.UintSize { | 
					
						
							|  |  |  | 	case 32: | 
					
						
							|  |  |  | 		return mangle_32bit | 
					
						
							|  |  |  | 	case 64: | 
					
						
							|  |  |  | 		return mangle_64bit | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		panic("unexpected platform int size") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mangle_platform_int_ptr contains the correct iface mangler on runtime for platform int size. | 
					
						
							|  |  |  | var mangle_platform_int_ptr = func() Mangler { | 
					
						
							|  |  |  | 	switch bits.UintSize { | 
					
						
							|  |  |  | 	case 32: | 
					
						
							|  |  |  | 		return mangle_32bit_ptr | 
					
						
							|  |  |  | 	case 64: | 
					
						
							|  |  |  | 		return mangle_64bit_ptr | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		panic("unexpected platform int size") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mangle_platform_int_slice contains the correct iface mangler on runtime for platform int size. | 
					
						
							|  |  |  | var mangle_platform_int_slice = func() Mangler { | 
					
						
							|  |  |  | 	switch bits.UintSize { | 
					
						
							|  |  |  | 	case 32: | 
					
						
							|  |  |  | 		return mangle_32bit_slice | 
					
						
							|  |  |  | 	case 64: | 
					
						
							|  |  |  | 		return mangle_64bit_slice | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		panic("unexpected platform int size") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // uint128 provides an easily mangleable data type for 128bit data types to be cast into. | 
					
						
							|  |  |  | type uint128 [2]uint64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_128bit(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	u2 := *(*uint128)(iface_value(a)) | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 	buf = append_uint64(buf, u2[0]) | 
					
						
							|  |  |  | 	buf = append_uint64(buf, u2[1]) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_128bit_ptr(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if ptr := (*uint128)(iface_value(a)); ptr != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		buf = append_uint64(buf, (*ptr)[0]) | 
					
						
							|  |  |  | 		buf = append_uint64(buf, (*ptr)[1]) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func mangle_128bit_slice(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	for _, u2 := range *(*[]uint128)(iface_value(a)) { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		buf = append_uint64(buf, u2[0]) | 
					
						
							|  |  |  | 		buf = append_uint64(buf, u2[1]) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | func mangle_mangled(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if v := a.(Mangled); v != nil { | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		return v.Mangle(buf) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | func mangle_binary(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if v := a.(binarymarshaler); v != nil { | 
					
						
							|  |  |  | 		b, err := v.MarshalBinary() | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 			panic("mangle_binary: " + err.Error()) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		return append(buf, b...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | func mangle_stringer(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if v := a.(stringer); v != nil { | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		return append(buf, v.String()...) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | func mangle_text(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if v := a.(textmarshaler); v != nil { | 
					
						
							|  |  |  | 		b, err := v.MarshalText() | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 			panic("mangle_text: " + err.Error()) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		buf = append(buf, '1') | 
					
						
							|  |  |  | 		return append(buf, b...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | func mangle_json(buf []byte, a any) []byte { | 
					
						
							|  |  |  | 	if v := a.(jsonmarshaler); v != nil { | 
					
						
							|  |  |  | 		b, err := v.MarshalJSON() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic("mangle_json: " + err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 		buf = append(buf, '1') | 
					
						
							| 
									
										
										
										
											2023-02-19 09:36:38 +00:00
										 |  |  | 		return append(buf, b...) | 
					
						
							| 
									
										
										
										
											2022-11-11 12:18:38 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	buf = append(buf, '0') | 
					
						
							|  |  |  | 	return buf | 
					
						
							|  |  |  | } |