/work/toxcore/events/file_recv_chunk.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_File_Recv_Chunk { |
27 | | uint32_t friend_number; |
28 | | uint32_t file_number; |
29 | | uint64_t position; |
30 | | uint8_t *_Nullable data; |
31 | | uint32_t data_length; |
32 | | }; |
33 | | |
34 | | static void tox_event_file_recv_chunk_set_friend_number(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk, uint32_t friend_number) |
35 | 1.15k | { |
36 | 1.15k | assert(file_recv_chunk != nullptr); |
37 | 1.15k | file_recv_chunk->friend_number = friend_number; |
38 | 1.15k | } |
39 | | uint32_t tox_event_file_recv_chunk_get_friend_number(const Tox_Event_File_Recv_Chunk *file_recv_chunk) |
40 | 0 | { |
41 | 0 | assert(file_recv_chunk != nullptr); |
42 | 0 | return file_recv_chunk->friend_number; |
43 | 0 | } |
44 | | |
45 | | static void tox_event_file_recv_chunk_set_file_number(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk, uint32_t file_number) |
46 | 1.15k | { |
47 | 1.15k | assert(file_recv_chunk != nullptr); |
48 | 1.15k | file_recv_chunk->file_number = file_number; |
49 | 1.15k | } |
50 | | uint32_t tox_event_file_recv_chunk_get_file_number(const Tox_Event_File_Recv_Chunk *file_recv_chunk) |
51 | 0 | { |
52 | 0 | assert(file_recv_chunk != nullptr); |
53 | 0 | return file_recv_chunk->file_number; |
54 | 0 | } |
55 | | |
56 | | static void tox_event_file_recv_chunk_set_position(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk, uint64_t position) |
57 | 1.15k | { |
58 | 1.15k | assert(file_recv_chunk != nullptr); |
59 | 1.15k | file_recv_chunk->position = position; |
60 | 1.15k | } |
61 | | uint64_t tox_event_file_recv_chunk_get_position(const Tox_Event_File_Recv_Chunk *file_recv_chunk) |
62 | 2 | { |
63 | 2 | assert(file_recv_chunk != nullptr); |
64 | 2 | return file_recv_chunk->position; |
65 | 2 | } |
66 | | |
67 | | static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk, |
68 | | const Memory *_Nonnull mem, const uint8_t *_Nullable data, uint32_t data_length) |
69 | 1.15k | { |
70 | 1.15k | assert(file_recv_chunk != nullptr); |
71 | 1.15k | if (file_recv_chunk->data != nullptr) { |
72 | 0 | mem_delete(mem, file_recv_chunk->data); |
73 | 0 | file_recv_chunk->data = nullptr; |
74 | 0 | file_recv_chunk->data_length = 0; |
75 | 0 | } |
76 | | |
77 | 1.15k | if (data == nullptr) { |
78 | 3 | assert(data_length == 0); |
79 | 3 | return true; |
80 | 3 | } |
81 | | |
82 | 1.14k | if (data_length == 0) { |
83 | 0 | file_recv_chunk->data = nullptr; |
84 | 0 | file_recv_chunk->data_length = 0; |
85 | 0 | return true; |
86 | 0 | } |
87 | | |
88 | 1.14k | uint8_t *data_copy = (uint8_t *)mem_balloc(mem, data_length); |
89 | | |
90 | 1.14k | if (data_copy == nullptr) { |
91 | 0 | return false; |
92 | 0 | } |
93 | | |
94 | 1.14k | memcpy(data_copy, data, data_length); |
95 | 1.14k | file_recv_chunk->data = data_copy; |
96 | 1.14k | file_recv_chunk->data_length = data_length; |
97 | 1.14k | return true; |
98 | 1.14k | } |
99 | | uint32_t tox_event_file_recv_chunk_get_data_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) |
100 | 1.15k | { |
101 | 1.15k | assert(file_recv_chunk != nullptr); |
102 | 1.15k | return file_recv_chunk->data_length; |
103 | 1.15k | } |
104 | | const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chunk *file_recv_chunk) |
105 | 1 | { |
106 | 1 | assert(file_recv_chunk != nullptr); |
107 | 1 | return file_recv_chunk->data; |
108 | 1 | } |
109 | | |
110 | | static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk) |
111 | 1.17k | { |
112 | 1.17k | *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { |
113 | 1.17k | 0 |
114 | 1.17k | }; |
115 | 1.17k | } |
116 | | static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *_Nonnull file_recv_chunk, const Memory *_Nonnull mem) |
117 | 1.17k | { |
118 | 1.17k | mem_delete(mem, file_recv_chunk->data); |
119 | 1.17k | } |
120 | | |
121 | | bool tox_event_file_recv_chunk_pack( |
122 | | const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp) |
123 | 20 | { |
124 | 20 | return bin_pack_array(bp, 4) |
125 | 20 | && bin_pack_u32(bp, event->friend_number) |
126 | 20 | && bin_pack_u32(bp, event->file_number) |
127 | 20 | && bin_pack_u64(bp, event->position) |
128 | 20 | && bin_pack_bin(bp, event->data, event->data_length); |
129 | 20 | } |
130 | | |
131 | | static bool tox_event_file_recv_chunk_unpack_into(Tox_Event_File_Recv_Chunk *_Nonnull event, Bin_Unpack *_Nonnull bu) |
132 | 24 | { |
133 | 24 | assert(event != nullptr); |
134 | 24 | if (!bin_unpack_array_fixed(bu, 4, nullptr)) { |
135 | 2 | return false; |
136 | 2 | } |
137 | | |
138 | 22 | return bin_unpack_u32(bu, &event->friend_number) |
139 | 22 | && bin_unpack_u32(bu, &event->file_number) |
140 | 22 | && bin_unpack_u64(bu, &event->position) |
141 | 22 | && bin_unpack_bin(bu, &event->data, &event->data_length); |
142 | 24 | } |
143 | | |
144 | | /***************************************************** |
145 | | * |
146 | | * :: new/free/add/get/size/unpack |
147 | | * |
148 | | *****************************************************/ |
149 | | |
150 | | const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk(const Tox_Event *event) |
151 | 0 | { |
152 | 0 | return event->type == TOX_EVENT_FILE_RECV_CHUNK ? event->data.file_recv_chunk : nullptr; |
153 | 0 | } |
154 | | |
155 | | Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem) |
156 | 1.17k | { |
157 | 1.17k | Tox_Event_File_Recv_Chunk *const file_recv_chunk = |
158 | 1.17k | (Tox_Event_File_Recv_Chunk *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Chunk)); |
159 | | |
160 | 1.17k | if (file_recv_chunk == nullptr) { |
161 | 1 | return nullptr; |
162 | 1 | } |
163 | | |
164 | 1.17k | tox_event_file_recv_chunk_construct(file_recv_chunk); |
165 | 1.17k | return file_recv_chunk; |
166 | 1.17k | } |
167 | | |
168 | | void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) |
169 | 1.17k | { |
170 | 1.17k | if (file_recv_chunk != nullptr) { |
171 | 1.17k | tox_event_file_recv_chunk_destruct(file_recv_chunk, mem); |
172 | 1.17k | } |
173 | 1.17k | mem_delete(mem, file_recv_chunk); |
174 | 1.17k | } |
175 | | |
176 | | static Tox_Event_File_Recv_Chunk *_Nullable tox_events_add_file_recv_chunk(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
177 | 1.15k | { |
178 | 1.15k | Tox_Event_File_Recv_Chunk *const file_recv_chunk = tox_event_file_recv_chunk_new(mem); |
179 | | |
180 | 1.15k | if (file_recv_chunk == nullptr) { |
181 | 0 | return nullptr; |
182 | 0 | } |
183 | | |
184 | 1.15k | Tox_Event event; |
185 | 1.15k | event.type = TOX_EVENT_FILE_RECV_CHUNK; |
186 | 1.15k | event.data.file_recv_chunk = file_recv_chunk; |
187 | | |
188 | 1.15k | if (!tox_events_add(events, &event)) { |
189 | 0 | tox_event_file_recv_chunk_free(file_recv_chunk, mem); |
190 | 0 | return nullptr; |
191 | 0 | } |
192 | 1.15k | return file_recv_chunk; |
193 | 1.15k | } |
194 | | |
195 | | bool tox_event_file_recv_chunk_unpack( |
196 | | Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) |
197 | 25 | { |
198 | 25 | assert(event != nullptr); |
199 | 25 | assert(*event == nullptr); |
200 | 25 | *event = tox_event_file_recv_chunk_new(mem); |
201 | | |
202 | 25 | if (*event == nullptr) { |
203 | 1 | return false; |
204 | 1 | } |
205 | | |
206 | 24 | return tox_event_file_recv_chunk_unpack_into(*event, bu); |
207 | 25 | } |
208 | | |
209 | | static Tox_Event_File_Recv_Chunk *_Nullable tox_event_file_recv_chunk_alloc(Tox_Events_State *_Nonnull state) |
210 | 1.15k | { |
211 | 1.15k | if (state->events == nullptr) { |
212 | 0 | return nullptr; |
213 | 0 | } |
214 | | |
215 | 1.15k | Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events, state->mem); |
216 | | |
217 | 1.15k | if (file_recv_chunk == nullptr) { |
218 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
219 | 0 | return nullptr; |
220 | 0 | } |
221 | | |
222 | 1.15k | return file_recv_chunk; |
223 | 1.15k | } |
224 | | |
225 | | /***************************************************** |
226 | | * |
227 | | * :: event handler |
228 | | * |
229 | | *****************************************************/ |
230 | | |
231 | | void tox_events_handle_file_recv_chunk( |
232 | | Tox *_Nonnull tox, |
233 | | uint32_t friend_number, |
234 | | uint32_t file_number, |
235 | | uint64_t position, |
236 | | const uint8_t *_Nullable data, size_t length, |
237 | | void *_Nullable user_data) |
238 | 1.15k | { |
239 | 1.15k | Tox_Events_State *state = tox_events_alloc(user_data); |
240 | 1.15k | Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_event_file_recv_chunk_alloc(state); |
241 | | |
242 | 1.15k | if (file_recv_chunk == nullptr) { |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | 1.15k | tox_event_file_recv_chunk_set_friend_number(file_recv_chunk, friend_number); |
247 | 1.15k | tox_event_file_recv_chunk_set_file_number(file_recv_chunk, file_number); |
248 | 1.15k | tox_event_file_recv_chunk_set_position(file_recv_chunk, position); |
249 | 1.15k | if (!tox_event_file_recv_chunk_set_data(file_recv_chunk, state->mem, data, length)) { |
250 | 0 | tox_event_file_recv_chunk_free(file_recv_chunk, state->mem); |
251 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
252 | 0 | return; |
253 | 0 | } |
254 | 1.15k | } |
255 | | |
256 | | void tox_events_handle_file_recv_chunk_dispatch(Tox *tox, const Tox_Event_File_Recv_Chunk *event, void *user_data) |
257 | 0 | { |
258 | 0 | if (tox->file_recv_chunk_callback == nullptr) { |
259 | 0 | return; |
260 | 0 | } |
261 | | |
262 | 0 | tox_unlock(tox); |
263 | 0 | tox->file_recv_chunk_callback(tox, event->friend_number, event->file_number, event->position, event->data, event->data_length, user_data); |
264 | 0 | tox_lock(tox); |
265 | 0 | } |