Skip to content

Commit adaeebb

Browse files
committed
Ensure valid UTF-8 strings for filenames
(and fallback to hex-encoded filenames)
1 parent ebe03e2 commit adaeebb

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

hooks.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package gotox
22

33
//#include <tox/tox.h>
44
import "C"
5+
import "encoding/hex"
56
import "unsafe"
7+
import "unicode/utf8"
68

79
//export hook_callback_self_connection_status
810
func hook_callback_self_connection_status(t unsafe.Pointer, status C.enum_TOX_CONNECTION, tox unsafe.Pointer) {
@@ -11,12 +13,12 @@ func hook_callback_self_connection_status(t unsafe.Pointer, status C.enum_TOX_CO
1113

1214
//export hook_callback_friend_name
1315
func hook_callback_friend_name(t unsafe.Pointer, friendnumber C.uint32_t, name *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
14-
(*Tox)(tox).onFriendNameChanges((*Tox)(tox), uint32(friendnumber), string(C.GoBytes((unsafe.Pointer)(name), (C.int)(length))))
16+
(*Tox)(tox).onFriendNameChanges((*Tox)(tox), uint32(friendnumber), string(C.GoBytes(unsafe.Pointer(name), C.int(length))))
1517
}
1618

1719
//export hook_callback_friend_status_message
18-
func hook_callback_friend_status_message(t unsafe.Pointer, friendnumber C.uint32_t, message *C.uint8_t, length C.uint16_t, tox unsafe.Pointer) {
19-
(*Tox)(tox).onFriendStatusMessageChanges((*Tox)(tox), uint32(friendnumber), string(C.GoBytes((unsafe.Pointer)(message), (C.int)(length))))
20+
func hook_callback_friend_status_message(t unsafe.Pointer, friendnumber C.uint32_t, message *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
21+
(*Tox)(tox).onFriendStatusMessageChanges((*Tox)(tox), uint32(friendnumber), string(C.GoBytes(unsafe.Pointer(message), C.int(length))))
2022
}
2123

2224
//export hook_callback_friend_status
@@ -41,12 +43,12 @@ func hook_callback_friend_read_receipt(t unsafe.Pointer, friendnumber C.uint32_t
4143

4244
//export hook_callback_friend_request
4345
func hook_callback_friend_request(t unsafe.Pointer, publicKey *C.uint8_t, message *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
44-
(*Tox)(tox).onFriendRequest((*Tox)(tox), C.GoBytes((unsafe.Pointer)(publicKey), TOX_PUBLIC_KEY_SIZE), string(C.GoBytes((unsafe.Pointer)(message), (C.int)(length))))
46+
(*Tox)(tox).onFriendRequest((*Tox)(tox), C.GoBytes((unsafe.Pointer)(publicKey), TOX_PUBLIC_KEY_SIZE), string(C.GoBytes(unsafe.Pointer(message), C.int(length))))
4547
}
4648

4749
//export hook_callback_friend_message
4850
func hook_callback_friend_message(t unsafe.Pointer, friendnumber C.uint32_t, messagetype C.enum_TOX_MESSAGE_TYPE, message *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
49-
(*Tox)(tox).onFriendMessage((*Tox)(tox), uint32(friendnumber), ToxMessageType(messagetype), string(C.GoBytes((unsafe.Pointer)(message), (C.int)(length))))
51+
(*Tox)(tox).onFriendMessage((*Tox)(tox), uint32(friendnumber), ToxMessageType(messagetype), string(C.GoBytes(unsafe.Pointer(message), C.int(length))))
5052
}
5153

5254
//export hook_callback_file_recv_control
@@ -60,18 +62,26 @@ func hook_callback_file_chunk_request(t unsafe.Pointer, friendnumber C.uint32_t,
6062
}
6163

6264
//export hook_callback_file_recv
63-
func hook_callback_file_recv(t unsafe.Pointer, friendnumber C.uint32_t, filenumber C.uint32_t, kind C.uint32_t, filesize C.uint64_t, filename *C.uint8_t, filenamelength C.size_t, tox unsafe.Pointer) {
64-
(*Tox)(tox).onFileRecv((*Tox)(tox), uint32(friendnumber), uint32(filenumber), ToxFileKind(kind), uint64(filesize), string(C.GoBytes((unsafe.Pointer)(filename), (C.int)(filenamelength))))
65+
func hook_callback_file_recv(t unsafe.Pointer, friendnumber C.uint32_t, filenumber C.uint32_t, kind C.uint32_t, filesize C.uint64_t, filename *C.uint8_t, filenameLength C.size_t, tox unsafe.Pointer) {
66+
// convert the filename from CString to a GoString and encode hexadecimal if needed
67+
goFilenameBytes := C.GoBytes(unsafe.Pointer(filename), C.int(filenameLength))
68+
goFilename := string(goFilenameBytes)
69+
70+
if !utf8.ValidString(goFilename) {
71+
goFilename = hex.EncodeToString(goFilenameBytes)
72+
}
73+
74+
(*Tox)(tox).onFileRecv((*Tox)(tox), uint32(friendnumber), uint32(filenumber), ToxFileKind(kind), uint64(filesize), goFilename)
6575
}
6676

6777
//export hook_callback_file_recv_chunk
6878
func hook_callback_file_recv_chunk(t unsafe.Pointer, friendnumber C.uint32_t, filenumber C.uint32_t, position C.uint64_t, data *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
69-
(*Tox)(tox).onFileRecvChunk((*Tox)(tox), uint32(friendnumber), uint32(filenumber), uint64(position), C.GoBytes((unsafe.Pointer)(data), (C.int)(length)))
79+
(*Tox)(tox).onFileRecvChunk((*Tox)(tox), uint32(friendnumber), uint32(filenumber), uint64(position), C.GoBytes((unsafe.Pointer)(data), C.int(length)))
7080
}
7181

7282
//export hook_callback_friend_lossy_packet
7383
func hook_callback_friend_lossy_packet(t unsafe.Pointer, friendnumber C.uint32_t, data *C.uint8_t, length C.size_t, tox unsafe.Pointer) {
74-
(*Tox)(tox).onFriendLossyPacket((*Tox)(tox), uint32(friendnumber), C.GoBytes((unsafe.Pointer)(data), (C.int)(length)))
84+
(*Tox)(tox).onFriendLossyPacket((*Tox)(tox), uint32(friendnumber), C.GoBytes((unsafe.Pointer)(data), C.int(length)))
7585
}
7686

7787
//export hook_callback_friend_lossless_packet

0 commit comments

Comments
 (0)