/work/toxcore/events/friend_lossless_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_Friend_Lossless_Packet { |
27 | | uint32_t friend_number; |
28 | | uint8_t *_Nullable data; |
29 | | uint32_t data_length; |
30 | | }; |
31 | | |
32 | | static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_Lossless_Packet *_Nonnull friend_lossless_packet, uint32_t friend_number) |
33 | 1 | { |
34 | 1 | assert(friend_lossless_packet != nullptr); |
35 | 1 | friend_lossless_packet->friend_number = friend_number; |
36 | 1 | } |
37 | | uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) |
38 | 0 | { |
39 | 0 | assert(friend_lossless_packet != nullptr); |
40 | 0 | return friend_lossless_packet->friend_number; |
41 | 0 | } |
42 | | |
43 | | static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_Packet *_Nonnull friend_lossless_packet, |
44 | | const Memory *_Nonnull mem, const uint8_t *_Nullable data, uint32_t data_length) |
45 | 1 | { |
46 | 1 | assert(friend_lossless_packet != nullptr); |
47 | 1 | if (friend_lossless_packet->data != nullptr) { |
48 | 0 | mem_delete(mem, friend_lossless_packet->data); |
49 | 0 | friend_lossless_packet->data = nullptr; |
50 | 0 | friend_lossless_packet->data_length = 0; |
51 | 0 | } |
52 | | |
53 | 1 | if (data == nullptr) { |
54 | 0 | assert(data_length == 0); |
55 | 0 | return true; |
56 | 0 | } |
57 | | |
58 | 1 | if (data_length == 0) { |
59 | 0 | friend_lossless_packet->data = nullptr; |
60 | 0 | friend_lossless_packet->data_length = 0; |
61 | 0 | return true; |
62 | 0 | } |
63 | | |
64 | 1 | uint8_t *data_copy = (uint8_t *)mem_balloc(mem, data_length); |
65 | | |
66 | 1 | if (data_copy == nullptr) { |
67 | 0 | return false; |
68 | 0 | } |
69 | | |
70 | 1 | memcpy(data_copy, data, data_length); |
71 | 1 | friend_lossless_packet->data = data_copy; |
72 | 1 | friend_lossless_packet->data_length = data_length; |
73 | 1 | return true; |
74 | 1 | } |
75 | | uint32_t tox_event_friend_lossless_packet_get_data_length(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) |
76 | 0 | { |
77 | 0 | assert(friend_lossless_packet != nullptr); |
78 | 0 | return friend_lossless_packet->data_length; |
79 | 0 | } |
80 | | const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) |
81 | 0 | { |
82 | 0 | assert(friend_lossless_packet != nullptr); |
83 | 0 | return friend_lossless_packet->data; |
84 | 0 | } |
85 | | |
86 | | static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *_Nonnull friend_lossless_packet) |
87 | 21 | { |
88 | 21 | *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { |
89 | 21 | 0 |
90 | 21 | }; |
91 | 21 | } |
92 | | static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *_Nonnull friend_lossless_packet, const Memory *_Nonnull mem) |
93 | 21 | { |
94 | 21 | mem_delete(mem, friend_lossless_packet->data); |
95 | 21 | } |
96 | | |
97 | | bool tox_event_friend_lossless_packet_pack( |
98 | | const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp) |
99 | 20 | { |
100 | 20 | return bin_pack_array(bp, 2) |
101 | 20 | && bin_pack_u32(bp, event->friend_number) |
102 | 20 | && bin_pack_bin(bp, event->data, event->data_length); |
103 | 20 | } |
104 | | |
105 | | static bool tox_event_friend_lossless_packet_unpack_into(Tox_Event_Friend_Lossless_Packet *_Nonnull event, Bin_Unpack *_Nonnull bu) |
106 | 20 | { |
107 | 20 | assert(event != nullptr); |
108 | 20 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
109 | 1 | return false; |
110 | 1 | } |
111 | | |
112 | 19 | return bin_unpack_u32(bu, &event->friend_number) |
113 | 19 | && bin_unpack_bin(bu, &event->data, &event->data_length); |
114 | 20 | } |
115 | | |
116 | | /***************************************************** |
117 | | * |
118 | | * :: new/free/add/get/size/unpack |
119 | | * |
120 | | *****************************************************/ |
121 | | |
122 | | const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet(const Tox_Event *event) |
123 | 0 | { |
124 | 0 | return event->type == TOX_EVENT_FRIEND_LOSSLESS_PACKET ? event->data.friend_lossless_packet : nullptr; |
125 | 0 | } |
126 | | |
127 | | Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem) |
128 | 22 | { |
129 | 22 | Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = |
130 | 22 | (Tox_Event_Friend_Lossless_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossless_Packet)); |
131 | | |
132 | 22 | if (friend_lossless_packet == nullptr) { |
133 | 1 | return nullptr; |
134 | 1 | } |
135 | | |
136 | 21 | tox_event_friend_lossless_packet_construct(friend_lossless_packet); |
137 | 21 | return friend_lossless_packet; |
138 | 22 | } |
139 | | |
140 | | void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) |
141 | 22 | { |
142 | 22 | if (friend_lossless_packet != nullptr) { |
143 | 21 | tox_event_friend_lossless_packet_destruct(friend_lossless_packet, mem); |
144 | 21 | } |
145 | 22 | mem_delete(mem, friend_lossless_packet); |
146 | 22 | } |
147 | | |
148 | | static Tox_Event_Friend_Lossless_Packet *_Nullable tox_events_add_friend_lossless_packet(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
149 | 1 | { |
150 | 1 | Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); |
151 | | |
152 | 1 | if (friend_lossless_packet == nullptr) { |
153 | 0 | return nullptr; |
154 | 0 | } |
155 | | |
156 | 1 | Tox_Event event; |
157 | 1 | event.type = TOX_EVENT_FRIEND_LOSSLESS_PACKET; |
158 | 1 | event.data.friend_lossless_packet = friend_lossless_packet; |
159 | | |
160 | 1 | if (!tox_events_add(events, &event)) { |
161 | 0 | tox_event_friend_lossless_packet_free(friend_lossless_packet, mem); |
162 | 0 | return nullptr; |
163 | 0 | } |
164 | 1 | return friend_lossless_packet; |
165 | 1 | } |
166 | | |
167 | | bool tox_event_friend_lossless_packet_unpack( |
168 | | Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) |
169 | 21 | { |
170 | 21 | assert(event != nullptr); |
171 | 21 | assert(*event == nullptr); |
172 | 21 | *event = tox_event_friend_lossless_packet_new(mem); |
173 | | |
174 | 21 | if (*event == nullptr) { |
175 | 1 | return false; |
176 | 1 | } |
177 | | |
178 | 20 | return tox_event_friend_lossless_packet_unpack_into(*event, bu); |
179 | 21 | } |
180 | | |
181 | | static Tox_Event_Friend_Lossless_Packet *_Nullable tox_event_friend_lossless_packet_alloc(Tox_Events_State *_Nonnull state) |
182 | 1 | { |
183 | 1 | if (state->events == nullptr) { |
184 | 0 | return nullptr; |
185 | 0 | } |
186 | | |
187 | 1 | Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events, state->mem); |
188 | | |
189 | 1 | if (friend_lossless_packet == nullptr) { |
190 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
191 | 0 | return nullptr; |
192 | 0 | } |
193 | | |
194 | 1 | return friend_lossless_packet; |
195 | 1 | } |
196 | | |
197 | | /***************************************************** |
198 | | * |
199 | | * :: event handler |
200 | | * |
201 | | *****************************************************/ |
202 | | |
203 | | void tox_events_handle_friend_lossless_packet( |
204 | | Tox *_Nonnull tox, |
205 | | uint32_t friend_number, |
206 | | const uint8_t *_Nullable data, size_t length, |
207 | | void *_Nullable user_data) |
208 | 1 | { |
209 | 1 | Tox_Events_State *state = tox_events_alloc(user_data); |
210 | 1 | Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_event_friend_lossless_packet_alloc(state); |
211 | | |
212 | 1 | if (friend_lossless_packet == nullptr) { |
213 | 0 | return; |
214 | 0 | } |
215 | | |
216 | 1 | tox_event_friend_lossless_packet_set_friend_number(friend_lossless_packet, friend_number); |
217 | 1 | if (!tox_event_friend_lossless_packet_set_data(friend_lossless_packet, state->mem, data, length)) { |
218 | 0 | tox_event_friend_lossless_packet_free(friend_lossless_packet, state->mem); |
219 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
220 | 0 | return; |
221 | 0 | } |
222 | 1 | } |
223 | | |
224 | | void tox_events_handle_friend_lossless_packet_dispatch(Tox *tox, const Tox_Event_Friend_Lossless_Packet *event, void *user_data) |
225 | 0 | { |
226 | 0 | if (event->data_length == 0 || tox->friend_lossless_packet_callback_per_pktid[event->data[0]] == nullptr) { |
227 | 0 | return; |
228 | 0 | } |
229 | | |
230 | 0 | tox_unlock(tox); |
231 | 0 | tox->friend_lossless_packet_callback_per_pktid[event->data[0]](tox, event->friend_number, event->data, event->data_length, user_data); |
232 | 0 | tox_lock(tox); |
233 | 0 | } |