This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·110 lines (85 loc) · 2.42 KB
/
main.go
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
package main
// #include <string.h>
// #include <stdbool.h>
// #include <mysql.h>
// #cgo CFLAGS: -O3 -I/usr/include/mysql -fno-omit-frame-pointer
import "C"
import (
"strings"
"unsafe"
"github.com/eknkc/basex"
)
func msg(message *C.char, s string) {
m := C.CString(s)
defer C.free(unsafe.Pointer(m))
C.strcpy(message, m)
}
var base24, _ = basex.NewEncoding("2346789bcdfghjkmpqrtvwxy")
//export to_base24_init
func to_base24_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 1 {
msg(message, "`to_base24` requires 1 parameter: the string to be encoded")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
initid.maybe_null = 1
return C.bool(false)
}
//export to_base24
func to_base24(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
*isNull = 1
c := 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:c:c]
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:c:c]
if argsArgs[0] == nil {
*length = 0
*isNull = 1
return nil
}
a := make([]string, c, c)
for i, argsArg := range argsArgs {
a[i] = C.GoStringN(argsArg, C.int(argsLengths[i]))
}
encoded := base24.Encode([]byte(a[0]))
*length = uint64(len(encoded))
*isNull = 0
return C.CString(encoded)
}
//export from_base24_init
func from_base24_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 1 {
msg(message, "`to_base24` requires 1 parameter: the string to be decoded")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
initid.maybe_null = 1
return C.bool(false)
}
//export from_base24
func from_base24(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
*isNull = 1
c := 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:c:c]
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:c:c]
if argsArgs[0] == nil {
*length = 0
*isNull = 1
return nil
}
a := make([]string, c, c)
for i, argsArg := range argsArgs {
a[i] = C.GoStringN(argsArg, C.int(argsLengths[i]))
}
decoded, err := base24.Decode(strings.ToLower(a[0]))
if err != nil {
*length = 0
*isNull = 1
return nil
}
*length = uint64(len(decoded))
*isNull = 0
return C.CString(string(decoded))
}
func main() {}