/work/toxcore/events/file_recv.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2023-2024 The TokTok team. |
3 | | */ |
4 | | |
5 | | #include "events_alloc.h" |
6 | | |
7 | | #include <assert.h> |
8 | | #include <stdlib.h> |
9 | | #include <string.h> |
10 | | |
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_events.h" |
17 | | |
18 | | |
19 | | /***************************************************** |
20 | | * |
21 | | * :: struct and accessors |
22 | | * |
23 | | *****************************************************/ |
24 | | |
25 | | |
26 | | struct Tox_Event_File_Recv { |
27 | | uint32_t friend_number; |
28 | | uint32_t file_number; |
29 | | uint32_t kind; |
30 | | uint64_t file_size; |
31 | | uint8_t *filename; |
32 | | uint32_t filename_length; |
33 | | }; |
34 | | |
35 | | non_null() |
36 | | static void tox_event_file_recv_set_friend_number(Tox_Event_File_Recv *file_recv, |
37 | | uint32_t friend_number) |
38 | 2 | { |
39 | 2 | assert(file_recv != nullptr); |
40 | 2 | file_recv->friend_number = friend_number; |
41 | 2 | } |
42 | | uint32_t tox_event_file_recv_get_friend_number(const Tox_Event_File_Recv *file_recv) |
43 | 2 | { |
44 | 2 | assert(file_recv != nullptr); |
45 | 2 | return file_recv->friend_number; |
46 | 2 | } |
47 | | |
48 | | non_null() |
49 | | static void tox_event_file_recv_set_file_number(Tox_Event_File_Recv *file_recv, |
50 | | uint32_t file_number) |
51 | 2 | { |
52 | 2 | assert(file_recv != nullptr); |
53 | 2 | file_recv->file_number = file_number; |
54 | 2 | } |
55 | | uint32_t tox_event_file_recv_get_file_number(const Tox_Event_File_Recv *file_recv) |
56 | 2 | { |
57 | 2 | assert(file_recv != nullptr); |
58 | 2 | return file_recv->file_number; |
59 | 2 | } |
60 | | |
61 | | non_null() |
62 | | static void tox_event_file_recv_set_kind(Tox_Event_File_Recv *file_recv, |
63 | | uint32_t kind) |
64 | 2 | { |
65 | 2 | assert(file_recv != nullptr); |
66 | 2 | file_recv->kind = kind; |
67 | 2 | } |
68 | | uint32_t tox_event_file_recv_get_kind(const Tox_Event_File_Recv *file_recv) |
69 | 2 | { |
70 | 2 | assert(file_recv != nullptr); |
71 | 2 | return file_recv->kind; |
72 | 2 | } |
73 | | |
74 | | non_null() |
75 | | static void tox_event_file_recv_set_file_size(Tox_Event_File_Recv *file_recv, |
76 | | uint64_t file_size) |
77 | 2 | { |
78 | 2 | assert(file_recv != nullptr); |
79 | 2 | file_recv->file_size = file_size; |
80 | 2 | } |
81 | | uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv) |
82 | 2 | { |
83 | 2 | assert(file_recv != nullptr); |
84 | 2 | return file_recv->file_size; |
85 | 2 | } |
86 | | |
87 | | non_null(1) nullable(2) |
88 | | static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, |
89 | | const uint8_t *filename, uint32_t filename_length) |
90 | 2 | { |
91 | 2 | assert(file_recv != nullptr); |
92 | | |
93 | 2 | if (file_recv->filename != nullptr) { |
94 | 0 | free(file_recv->filename); |
95 | 0 | file_recv->filename = nullptr; |
96 | 0 | file_recv->filename_length = 0; |
97 | 0 | } |
98 | | |
99 | 2 | if (filename == nullptr) { |
100 | 0 | assert(filename_length == 0); |
101 | 0 | return true; |
102 | 0 | } |
103 | | |
104 | 2 | uint8_t *filename_copy = (uint8_t *)malloc(filename_length); |
105 | | |
106 | 2 | if (filename_copy == nullptr) { |
107 | 0 | return false; |
108 | 0 | } |
109 | | |
110 | 2 | memcpy(filename_copy, filename, filename_length); |
111 | 2 | file_recv->filename = filename_copy; |
112 | 2 | file_recv->filename_length = filename_length; |
113 | 2 | return true; |
114 | 2 | } |
115 | | uint32_t tox_event_file_recv_get_filename_length(const Tox_Event_File_Recv *file_recv) |
116 | 2 | { |
117 | 2 | assert(file_recv != nullptr); |
118 | 2 | return file_recv->filename_length; |
119 | 2 | } |
120 | | const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_recv) |
121 | 2 | { |
122 | 2 | assert(file_recv != nullptr); |
123 | 2 | return file_recv->filename; |
124 | 2 | } |
125 | | |
126 | | non_null() |
127 | | static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) |
128 | 8 | { |
129 | 8 | *file_recv = (Tox_Event_File_Recv) { |
130 | 8 | 0 |
131 | 8 | }; |
132 | 8 | } |
133 | | non_null() |
134 | | static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const Memory *mem) |
135 | 8 | { |
136 | 8 | free(file_recv->filename); |
137 | 8 | } |
138 | | |
139 | | bool tox_event_file_recv_pack( |
140 | | const Tox_Event_File_Recv *event, Bin_Pack *bp) |
141 | 0 | { |
142 | 0 | return bin_pack_array(bp, 5) |
143 | 0 | && bin_pack_u32(bp, event->friend_number) |
144 | 0 | && bin_pack_u32(bp, event->file_number) |
145 | 0 | && bin_pack_u32(bp, event->kind) |
146 | 0 | && bin_pack_u64(bp, event->file_size) |
147 | 0 | && bin_pack_bin(bp, event->filename, event->filename_length); |
148 | 0 | } |
149 | | |
150 | | non_null() |
151 | | static bool tox_event_file_recv_unpack_into( |
152 | | Tox_Event_File_Recv *event, Bin_Unpack *bu) |
153 | 6 | { |
154 | 6 | assert(event != nullptr); |
155 | 6 | if (!bin_unpack_array_fixed(bu, 5, nullptr)) { |
156 | 1 | return false; |
157 | 1 | } |
158 | | |
159 | 5 | return bin_unpack_u32(bu, &event->friend_number) |
160 | 5 | && bin_unpack_u32(bu, &event->file_number) |
161 | 5 | && bin_unpack_u32(bu, &event->kind) |
162 | 5 | && bin_unpack_u64(bu, &event->file_size) |
163 | 5 | && bin_unpack_bin(bu, &event->filename, &event->filename_length); |
164 | 6 | } |
165 | | |
166 | | |
167 | | /***************************************************** |
168 | | * |
169 | | * :: new/free/add/get/size/unpack |
170 | | * |
171 | | *****************************************************/ |
172 | | |
173 | | const Tox_Event_File_Recv *tox_event_get_file_recv(const Tox_Event *event) |
174 | 0 | { |
175 | 0 | return event->type == TOX_EVENT_FILE_RECV ? event->data.file_recv : nullptr; |
176 | 0 | } |
177 | | |
178 | | Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem) |
179 | 9 | { |
180 | 9 | Tox_Event_File_Recv *const file_recv = |
181 | 9 | (Tox_Event_File_Recv *)mem_alloc(mem, sizeof(Tox_Event_File_Recv)); |
182 | | |
183 | 9 | if (file_recv == nullptr) { |
184 | 1 | return nullptr; |
185 | 1 | } |
186 | | |
187 | 8 | tox_event_file_recv_construct(file_recv); |
188 | 8 | return file_recv; |
189 | 9 | } |
190 | | |
191 | | void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem) |
192 | 9 | { |
193 | 9 | if (file_recv != nullptr) { |
194 | 8 | tox_event_file_recv_destruct(file_recv, mem); |
195 | 8 | } |
196 | 9 | mem_delete(mem, file_recv); |
197 | 9 | } |
198 | | |
199 | | non_null() |
200 | | static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events, const Memory *mem) |
201 | 2 | { |
202 | 2 | Tox_Event_File_Recv *const file_recv = tox_event_file_recv_new(mem); |
203 | | |
204 | 2 | if (file_recv == nullptr) { |
205 | 0 | return nullptr; |
206 | 0 | } |
207 | | |
208 | 2 | Tox_Event event; |
209 | 2 | event.type = TOX_EVENT_FILE_RECV; |
210 | 2 | event.data.file_recv = file_recv; |
211 | | |
212 | 2 | tox_events_add(events, &event); |
213 | 2 | return file_recv; |
214 | 2 | } |
215 | | |
216 | | bool tox_event_file_recv_unpack( |
217 | | Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) |
218 | 7 | { |
219 | 7 | assert(event != nullptr); |
220 | 7 | assert(*event == nullptr); |
221 | 7 | *event = tox_event_file_recv_new(mem); |
222 | | |
223 | 7 | if (*event == nullptr) { |
224 | 1 | return false; |
225 | 1 | } |
226 | | |
227 | 6 | return tox_event_file_recv_unpack_into(*event, bu); |
228 | 7 | } |
229 | | |
230 | | non_null() |
231 | | static Tox_Event_File_Recv *tox_event_file_recv_alloc(void *user_data) |
232 | 2 | { |
233 | 2 | Tox_Events_State *state = tox_events_alloc(user_data); |
234 | 2 | assert(state != nullptr); |
235 | | |
236 | 2 | if (state->events == nullptr) { |
237 | 0 | return nullptr; |
238 | 0 | } |
239 | | |
240 | 2 | Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events, state->mem); |
241 | | |
242 | 2 | if (file_recv == nullptr) { |
243 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
244 | 0 | return nullptr; |
245 | 0 | } |
246 | | |
247 | 2 | return file_recv; |
248 | 2 | } |
249 | | |
250 | | |
251 | | /***************************************************** |
252 | | * |
253 | | * :: event handler |
254 | | * |
255 | | *****************************************************/ |
256 | | |
257 | | |
258 | | void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, |
259 | | void *user_data) |
260 | 2 | { |
261 | 2 | Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data); |
262 | | |
263 | 2 | if (file_recv == nullptr) { |
264 | 0 | return; |
265 | 0 | } |
266 | | |
267 | 2 | tox_event_file_recv_set_friend_number(file_recv, friend_number); |
268 | 2 | tox_event_file_recv_set_file_number(file_recv, file_number); |
269 | 2 | tox_event_file_recv_set_kind(file_recv, kind); |
270 | 2 | tox_event_file_recv_set_file_size(file_recv, file_size); |
271 | 2 | tox_event_file_recv_set_filename(file_recv, filename, filename_length); |
272 | 2 | } |