summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mangler/helpers.go
blob: 26d31152b2b73aa41cbbc845aa44768200373eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package mangler

import (
	"reflect"
	"unsafe"
)

func append_uint16(b []byte, u uint16) []byte {
	return append(b, // LE
		byte(u),
		byte(u>>8),
	)
}

func append_uint32(b []byte, u uint32) []byte {
	return append(b, // LE
		byte(u),
		byte(u>>8),
		byte(u>>16),
		byte(u>>24),
	)
}

func append_uint64(b []byte, u uint64) []byte {
	return append(b, // LE
		byte(u),
		byte(u>>8),
		byte(u>>16),
		byte(u>>24),
		byte(u>>32),
		byte(u>>40),
		byte(u>>48),
		byte(u>>56),
	)
}

type typecontext struct {
	ntype reflect.Type
	rtype reflect.Type
}

func deref_ptr_mangler(ctx typecontext, mangle Mangler, n uint) Mangler {
	if mangle == nil || n == 0 {
		panic("bad input")
	}

	// Non-nested value types,
	// i.e. just direct ptrs to
	// primitives require one
	// less dereference to ptr.
	if ctx.ntype == nil {
		n--
	}

	return func(buf []byte, ptr unsafe.Pointer) []byte {

		// Deref n number times.
		for i := n; i > 0; i-- {

			if ptr == nil {
				// Check for nil values
				buf = append(buf, '0')
				return buf
			}

			// Further deref ptr
			buf = append(buf, '1')
			ptr = *(*unsafe.Pointer)(ptr)
		}

		if ptr == nil {
			// Check for nil values
			buf = append(buf, '0')
			return buf
		}

		// Mangle fully deref'd
		buf = append(buf, '1')
		buf = mangle(buf, ptr)
		return buf
	}
}

func iter_slice_mangler(ctx typecontext, mangle Mangler) Mangler {
	if ctx.rtype == nil || mangle == nil {
		panic("bad input")
	}

	// memory size of elem.
	esz := ctx.rtype.Size()

	return func(buf []byte, ptr unsafe.Pointer) []byte {
		// Get data as slice hdr.
		hdr := (*slice_header)(ptr)

		for i := 0; i < hdr.len; i++ {
			// Mangle data at slice index.
			eptr := array_at(hdr.data, esz, i)
			buf = mangle(buf, eptr)
			buf = append(buf, ',')
		}

		if hdr.len > 0 {
			// Drop final comma.
			buf = buf[:len(buf)-1]
		}

		return buf
	}
}

func iter_array_mangler(ctx typecontext, mangle Mangler) Mangler {
	if ctx.rtype == nil || mangle == nil {
		panic("bad input")
	}

	// no. array elements.
	n := ctx.ntype.Len()

	// memory size of elem.
	esz := ctx.rtype.Size()

	return func(buf []byte, ptr unsafe.Pointer) []byte {
		for i := 0; i < n; i++ {
			// Mangle data at array index.
			offset := esz * uintptr(i)
			eptr := add(ptr, offset)
			buf = mangle(buf, eptr)
			buf = append(buf, ',')
		}

		if n > 0 {
			// Drop final comma.
			buf = buf[:len(buf)-1]
		}

		return buf
	}
}

func iter_struct_mangler(ctx typecontext, manglers []Mangler) Mangler {
	if ctx.rtype == nil || len(manglers) != ctx.rtype.NumField() {
		panic("bad input")
	}

	type field struct {
		offset uintptr
		mangle Mangler
	}

	// Bundle together the fields and manglers.
	fields := make([]field, ctx.rtype.NumField())
	for i := range fields {
		rfield := ctx.rtype.FieldByIndex([]int{i})
		fields[i].offset = rfield.Offset
		fields[i].mangle = manglers[i]
		if fields[i].mangle == nil {
			panic("bad input")
		}
	}

	return func(buf []byte, ptr unsafe.Pointer) []byte {
		for i := range fields {
			// Get struct field ptr via offset.
			fptr := add(ptr, fields[i].offset)

			// Mangle the struct field data.
			buf = fields[i].mangle(buf, fptr)
			buf = append(buf, ',')
		}

		if len(fields) > 0 {
			// Drop final comma.
			buf = buf[:len(buf)-1]
		}

		return buf
	}
}

// array_at returns ptr to index in array at ptr, given element size.
func array_at(ptr unsafe.Pointer, esz uintptr, i int) unsafe.Pointer {
	return unsafe.Pointer(uintptr(ptr) + esz*uintptr(i))
}

// add returns the ptr addition of starting ptr and a delta.
func add(ptr unsafe.Pointer, delta uintptr) unsafe.Pointer {
	return unsafe.Pointer(uintptr(ptr) + delta)
}

type slice_header struct {
	data unsafe.Pointer
	len  int
	cap  int
}

func eface_data(a any) unsafe.Pointer {
	type eface struct{ _, data unsafe.Pointer }
	return (*eface)(unsafe.Pointer(&a)).data
}