/work/toxcore/events/group_custom_packet.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2023-2026 The TokTok team. |
3 | | */ |
4 | | |
5 | | #include "events_alloc.h" |
6 | | |
7 | | #include <assert.h> |
8 | | #include <string.h> |
9 | | |
10 | | #include "../attributes.h" |
11 | | #include "../bin_pack.h" |
12 | | #include "../bin_unpack.h" |
13 | | #include "../ccompat.h" |
14 | | #include "../mem.h" |
15 | | #include "../tox.h" |
16 | | #include "../tox_event.h" |
17 | | #include "../tox_events.h" |
18 | | #include "../tox_struct.h" |
19 | | |
20 | | /***************************************************** |
21 | | * |
22 | | * :: struct and accessors |
23 | | * |
24 | | *****************************************************/ |
25 | | |
26 | | struct Tox_Event_Group_Custom_Packet { |
27 | | uint32_t group_number; |
28 | | uint32_t peer_id; |
29 | | uint8_t *_Nullable data; |
30 | | uint32_t data_length; |
31 | | }; |
32 | | |
33 | | static void tox_event_group_custom_packet_set_group_number(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, uint32_t group_number) |
34 | 2 | { |
35 | 2 | assert(group_custom_packet != nullptr); |
36 | 2 | group_custom_packet->group_number = group_number; |
37 | 2 | } |
38 | | uint32_t tox_event_group_custom_packet_get_group_number(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
39 | 0 | { |
40 | 0 | assert(group_custom_packet != nullptr); |
41 | 0 | return group_custom_packet->group_number; |
42 | 0 | } |
43 | | |
44 | | static void tox_event_group_custom_packet_set_peer_id(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, uint32_t peer_id) |
45 | 2 | { |
46 | 2 | assert(group_custom_packet != nullptr); |
47 | 2 | group_custom_packet->peer_id = peer_id; |
48 | 2 | } |
49 | | uint32_t tox_event_group_custom_packet_get_peer_id(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
50 | 0 | { |
51 | 0 | assert(group_custom_packet != nullptr); |
52 | 0 | return group_custom_packet->peer_id; |
53 | 0 | } |
54 | | |
55 | | static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, |
56 | | const Memory *_Nonnull mem, const uint8_t *_Nullable data, uint32_t data_length) |
57 | 2 | { |
58 | 2 | assert(group_custom_packet != nullptr); |
59 | 2 | if (group_custom_packet->data != nullptr) { |
60 | 0 | mem_delete(mem, group_custom_packet->data); |
61 | 0 | group_custom_packet->data = nullptr; |
62 | 0 | group_custom_packet->data_length = 0; |
63 | 0 | } |
64 | | |
65 | 2 | if (data == nullptr) { |
66 | 0 | assert(data_length == 0); |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | 2 | if (data_length == 0) { |
71 | 0 | group_custom_packet->data = nullptr; |
72 | 0 | group_custom_packet->data_length = 0; |
73 | 0 | return true; |
74 | 0 | } |
75 | | |
76 | 2 | uint8_t *data_copy = (uint8_t *)mem_balloc(mem, data_length); |
77 | | |
78 | 2 | if (data_copy == nullptr) { |
79 | 0 | return false; |
80 | 0 | } |
81 | | |
82 | 2 | memcpy(data_copy, data, data_length); |
83 | 2 | group_custom_packet->data = data_copy; |
84 | 2 | group_custom_packet->data_length = data_length; |
85 | 2 | return true; |
86 | 2 | } |
87 | | uint32_t tox_event_group_custom_packet_get_data_length(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
88 | 0 | { |
89 | 0 | assert(group_custom_packet != nullptr); |
90 | 0 | return group_custom_packet->data_length; |
91 | 0 | } |
92 | | const uint8_t *tox_event_group_custom_packet_get_data(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
93 | 0 | { |
94 | 0 | assert(group_custom_packet != nullptr); |
95 | 0 | return group_custom_packet->data; |
96 | 0 | } |
97 | | |
98 | | static void tox_event_group_custom_packet_construct(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet) |
99 | 16 | { |
100 | 16 | *group_custom_packet = (Tox_Event_Group_Custom_Packet) { |
101 | 16 | 0 |
102 | 16 | }; |
103 | 16 | } |
104 | | static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, const Memory *_Nonnull mem) |
105 | 16 | { |
106 | 16 | mem_delete(mem, group_custom_packet->data); |
107 | 16 | } |
108 | | |
109 | | bool tox_event_group_custom_packet_pack( |
110 | | const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp) |
111 | 20 | { |
112 | 20 | return bin_pack_array(bp, 3) |
113 | 20 | && bin_pack_u32(bp, event->group_number) |
114 | 20 | && bin_pack_u32(bp, event->peer_id) |
115 | 20 | && bin_pack_bin(bp, event->data, event->data_length); |
116 | 20 | } |
117 | | |
118 | | static bool tox_event_group_custom_packet_unpack_into(Tox_Event_Group_Custom_Packet *_Nonnull event, Bin_Unpack *_Nonnull bu) |
119 | 14 | { |
120 | 14 | assert(event != nullptr); |
121 | 14 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
122 | 1 | return false; |
123 | 1 | } |
124 | | |
125 | 13 | return bin_unpack_u32(bu, &event->group_number) |
126 | 13 | && bin_unpack_u32(bu, &event->peer_id) |
127 | 13 | && bin_unpack_bin(bu, &event->data, &event->data_length); |
128 | 14 | } |
129 | | |
130 | | /***************************************************** |
131 | | * |
132 | | * :: new/free/add/get/size/unpack |
133 | | * |
134 | | *****************************************************/ |
135 | | |
136 | | const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet(const Tox_Event *event) |
137 | 0 | { |
138 | 0 | return event->type == TOX_EVENT_GROUP_CUSTOM_PACKET ? event->data.group_custom_packet : nullptr; |
139 | 0 | } |
140 | | |
141 | | Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem) |
142 | 17 | { |
143 | 17 | Tox_Event_Group_Custom_Packet *const group_custom_packet = |
144 | 17 | (Tox_Event_Group_Custom_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Packet)); |
145 | | |
146 | 17 | if (group_custom_packet == nullptr) { |
147 | 1 | return nullptr; |
148 | 1 | } |
149 | | |
150 | 16 | tox_event_group_custom_packet_construct(group_custom_packet); |
151 | 16 | return group_custom_packet; |
152 | 17 | } |
153 | | |
154 | | void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem) |
155 | 17 | { |
156 | 17 | if (group_custom_packet != nullptr) { |
157 | 16 | tox_event_group_custom_packet_destruct(group_custom_packet, mem); |
158 | 16 | } |
159 | 17 | mem_delete(mem, group_custom_packet); |
160 | 17 | } |
161 | | |
162 | | static Tox_Event_Group_Custom_Packet *_Nullable tox_events_add_group_custom_packet(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
163 | 2 | { |
164 | 2 | Tox_Event_Group_Custom_Packet *const group_custom_packet = tox_event_group_custom_packet_new(mem); |
165 | | |
166 | 2 | if (group_custom_packet == nullptr) { |
167 | 0 | return nullptr; |
168 | 0 | } |
169 | | |
170 | 2 | Tox_Event event; |
171 | 2 | event.type = TOX_EVENT_GROUP_CUSTOM_PACKET; |
172 | 2 | event.data.group_custom_packet = group_custom_packet; |
173 | | |
174 | 2 | if (!tox_events_add(events, &event)) { |
175 | 0 | tox_event_group_custom_packet_free(group_custom_packet, mem); |
176 | 0 | return nullptr; |
177 | 0 | } |
178 | 2 | return group_custom_packet; |
179 | 2 | } |
180 | | |
181 | | bool tox_event_group_custom_packet_unpack( |
182 | | Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) |
183 | 15 | { |
184 | 15 | assert(event != nullptr); |
185 | 15 | assert(*event == nullptr); |
186 | 15 | *event = tox_event_group_custom_packet_new(mem); |
187 | | |
188 | 15 | if (*event == nullptr) { |
189 | 1 | return false; |
190 | 1 | } |
191 | | |
192 | 14 | return tox_event_group_custom_packet_unpack_into(*event, bu); |
193 | 15 | } |
194 | | |
195 | | static Tox_Event_Group_Custom_Packet *_Nullable tox_event_group_custom_packet_alloc(Tox_Events_State *_Nonnull state) |
196 | 2 | { |
197 | 2 | if (state->events == nullptr) { |
198 | 0 | return nullptr; |
199 | 0 | } |
200 | | |
201 | 2 | Tox_Event_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events, state->mem); |
202 | | |
203 | 2 | if (group_custom_packet == nullptr) { |
204 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
205 | 0 | return nullptr; |
206 | 0 | } |
207 | | |
208 | 2 | return group_custom_packet; |
209 | 2 | } |
210 | | |
211 | | /***************************************************** |
212 | | * |
213 | | * :: event handler |
214 | | * |
215 | | *****************************************************/ |
216 | | |
217 | | void tox_events_handle_group_custom_packet( |
218 | | Tox *_Nonnull tox, |
219 | | uint32_t group_number, |
220 | | uint32_t peer_id, |
221 | | const uint8_t *_Nullable data, size_t data_length, |
222 | | void *_Nullable user_data) |
223 | 2 | { |
224 | 2 | Tox_Events_State *state = tox_events_alloc(user_data); |
225 | 2 | Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(state); |
226 | | |
227 | 2 | if (group_custom_packet == nullptr) { |
228 | 0 | return; |
229 | 0 | } |
230 | | |
231 | 2 | tox_event_group_custom_packet_set_group_number(group_custom_packet, group_number); |
232 | 2 | tox_event_group_custom_packet_set_peer_id(group_custom_packet, peer_id); |
233 | 2 | if (!tox_event_group_custom_packet_set_data(group_custom_packet, state->mem, data, data_length)) { |
234 | 0 | tox_event_group_custom_packet_free(group_custom_packet, state->mem); |
235 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
236 | 0 | return; |
237 | 0 | } |
238 | 2 | } |
239 | | |
240 | | void tox_events_handle_group_custom_packet_dispatch(Tox *tox, const Tox_Event_Group_Custom_Packet *event, void *user_data) |
241 | 0 | { |
242 | 0 | if (tox->group_custom_packet_callback == nullptr) { |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | 0 | tox_unlock(tox); |
247 | 0 | tox->group_custom_packet_callback(tox, event->group_number, event->peer_id, event->data, event->data_length, user_data); |
248 | 0 | tox_lock(tox); |
249 | 0 | } |