/work/toxcore/events/file_chunk_request.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 | | |
9 | | #include "../attributes.h" |
10 | | #include "../bin_pack.h" |
11 | | #include "../bin_unpack.h" |
12 | | #include "../ccompat.h" |
13 | | #include "../mem.h" |
14 | | #include "../tox.h" |
15 | | #include "../tox_event.h" |
16 | | #include "../tox_events.h" |
17 | | #include "../tox_struct.h" |
18 | | |
19 | | /***************************************************** |
20 | | * |
21 | | * :: struct and accessors |
22 | | * |
23 | | *****************************************************/ |
24 | | |
25 | | struct Tox_Event_File_Chunk_Request { |
26 | | uint32_t friend_number; |
27 | | uint32_t file_number; |
28 | | uint64_t position; |
29 | | uint16_t length; |
30 | | }; |
31 | | |
32 | | static void tox_event_file_chunk_request_set_friend_number(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request, uint32_t friend_number) |
33 | 1.15k | { |
34 | 1.15k | assert(file_chunk_request != nullptr); |
35 | 1.15k | file_chunk_request->friend_number = friend_number; |
36 | 1.15k | } |
37 | | uint32_t tox_event_file_chunk_request_get_friend_number(const Tox_Event_File_Chunk_Request *file_chunk_request) |
38 | 1.15k | { |
39 | 1.15k | assert(file_chunk_request != nullptr); |
40 | 1.15k | return file_chunk_request->friend_number; |
41 | 1.15k | } |
42 | | |
43 | | static void tox_event_file_chunk_request_set_file_number(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request, uint32_t file_number) |
44 | 1.15k | { |
45 | 1.15k | assert(file_chunk_request != nullptr); |
46 | 1.15k | file_chunk_request->file_number = file_number; |
47 | 1.15k | } |
48 | | uint32_t tox_event_file_chunk_request_get_file_number(const Tox_Event_File_Chunk_Request *file_chunk_request) |
49 | 1.15k | { |
50 | 1.15k | assert(file_chunk_request != nullptr); |
51 | 1.15k | return file_chunk_request->file_number; |
52 | 1.15k | } |
53 | | |
54 | | static void tox_event_file_chunk_request_set_position(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request, uint64_t position) |
55 | 1.15k | { |
56 | 1.15k | assert(file_chunk_request != nullptr); |
57 | 1.15k | file_chunk_request->position = position; |
58 | 1.15k | } |
59 | | uint64_t tox_event_file_chunk_request_get_position(const Tox_Event_File_Chunk_Request *file_chunk_request) |
60 | 1.15k | { |
61 | 1.15k | assert(file_chunk_request != nullptr); |
62 | 1.15k | return file_chunk_request->position; |
63 | 1.15k | } |
64 | | |
65 | | static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request, uint16_t length) |
66 | 1.15k | { |
67 | 1.15k | assert(file_chunk_request != nullptr); |
68 | 1.15k | file_chunk_request->length = length; |
69 | 1.15k | } |
70 | | uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Request *file_chunk_request) |
71 | 1.15k | { |
72 | 1.15k | assert(file_chunk_request != nullptr); |
73 | 1.15k | return file_chunk_request->length; |
74 | 1.15k | } |
75 | | |
76 | | static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request) |
77 | 1.38k | { |
78 | 1.38k | *file_chunk_request = (Tox_Event_File_Chunk_Request) { |
79 | 1.38k | 0 |
80 | 1.38k | }; |
81 | 1.38k | } |
82 | | static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *_Nonnull file_chunk_request, const Memory *_Nonnull mem) |
83 | 1.38k | { |
84 | 1.38k | return; |
85 | 1.38k | } |
86 | | |
87 | | bool tox_event_file_chunk_request_pack( |
88 | | const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp) |
89 | 110 | { |
90 | 110 | return bin_pack_array(bp, 4) |
91 | 110 | && bin_pack_u32(bp, event->friend_number) |
92 | 110 | && bin_pack_u32(bp, event->file_number) |
93 | 110 | && bin_pack_u64(bp, event->position) |
94 | 110 | && bin_pack_u16(bp, event->length); |
95 | 110 | } |
96 | | |
97 | | static bool tox_event_file_chunk_request_unpack_into(Tox_Event_File_Chunk_Request *_Nonnull event, Bin_Unpack *_Nonnull bu) |
98 | 232 | { |
99 | 232 | assert(event != nullptr); |
100 | 232 | if (!bin_unpack_array_fixed(bu, 4, nullptr)) { |
101 | 2 | return false; |
102 | 2 | } |
103 | | |
104 | 230 | return bin_unpack_u32(bu, &event->friend_number) |
105 | 230 | && bin_unpack_u32(bu, &event->file_number) |
106 | 230 | && bin_unpack_u64(bu, &event->position) |
107 | 230 | && bin_unpack_u16(bu, &event->length); |
108 | 232 | } |
109 | | |
110 | | /***************************************************** |
111 | | * |
112 | | * :: new/free/add/get/size/unpack |
113 | | * |
114 | | *****************************************************/ |
115 | | |
116 | | const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request(const Tox_Event *event) |
117 | 0 | { |
118 | 0 | return event->type == TOX_EVENT_FILE_CHUNK_REQUEST ? event->data.file_chunk_request : nullptr; |
119 | 0 | } |
120 | | |
121 | | Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem) |
122 | 1.38k | { |
123 | 1.38k | Tox_Event_File_Chunk_Request *const file_chunk_request = |
124 | 1.38k | (Tox_Event_File_Chunk_Request *)mem_alloc(mem, sizeof(Tox_Event_File_Chunk_Request)); |
125 | | |
126 | 1.38k | if (file_chunk_request == nullptr) { |
127 | 1 | return nullptr; |
128 | 1 | } |
129 | | |
130 | 1.38k | tox_event_file_chunk_request_construct(file_chunk_request); |
131 | 1.38k | return file_chunk_request; |
132 | 1.38k | } |
133 | | |
134 | | void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) |
135 | 1.38k | { |
136 | 1.38k | if (file_chunk_request != nullptr) { |
137 | 1.38k | tox_event_file_chunk_request_destruct(file_chunk_request, mem); |
138 | 1.38k | } |
139 | 1.38k | mem_delete(mem, file_chunk_request); |
140 | 1.38k | } |
141 | | |
142 | | static Tox_Event_File_Chunk_Request *_Nullable tox_events_add_file_chunk_request(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
143 | 1.15k | { |
144 | 1.15k | Tox_Event_File_Chunk_Request *const file_chunk_request = tox_event_file_chunk_request_new(mem); |
145 | | |
146 | 1.15k | if (file_chunk_request == nullptr) { |
147 | 0 | return nullptr; |
148 | 0 | } |
149 | | |
150 | 1.15k | Tox_Event event; |
151 | 1.15k | event.type = TOX_EVENT_FILE_CHUNK_REQUEST; |
152 | 1.15k | event.data.file_chunk_request = file_chunk_request; |
153 | | |
154 | 1.15k | if (!tox_events_add(events, &event)) { |
155 | 0 | tox_event_file_chunk_request_free(file_chunk_request, mem); |
156 | 0 | return nullptr; |
157 | 0 | } |
158 | 1.15k | return file_chunk_request; |
159 | 1.15k | } |
160 | | |
161 | | bool tox_event_file_chunk_request_unpack( |
162 | | Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) |
163 | 233 | { |
164 | 233 | assert(event != nullptr); |
165 | 233 | assert(*event == nullptr); |
166 | 233 | *event = tox_event_file_chunk_request_new(mem); |
167 | | |
168 | 233 | if (*event == nullptr) { |
169 | 1 | return false; |
170 | 1 | } |
171 | | |
172 | 232 | return tox_event_file_chunk_request_unpack_into(*event, bu); |
173 | 233 | } |
174 | | |
175 | | static Tox_Event_File_Chunk_Request *_Nullable tox_event_file_chunk_request_alloc(Tox_Events_State *_Nonnull state) |
176 | 1.15k | { |
177 | 1.15k | if (state->events == nullptr) { |
178 | 0 | return nullptr; |
179 | 0 | } |
180 | | |
181 | 1.15k | Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events, state->mem); |
182 | | |
183 | 1.15k | if (file_chunk_request == nullptr) { |
184 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
185 | 0 | return nullptr; |
186 | 0 | } |
187 | | |
188 | 1.15k | return file_chunk_request; |
189 | 1.15k | } |
190 | | |
191 | | /***************************************************** |
192 | | * |
193 | | * :: event handler |
194 | | * |
195 | | *****************************************************/ |
196 | | |
197 | | void tox_events_handle_file_chunk_request( |
198 | | Tox *_Nonnull tox, |
199 | | uint32_t friend_number, |
200 | | uint32_t file_number, |
201 | | uint64_t position, |
202 | | size_t length, |
203 | | void *_Nullable user_data) |
204 | 1.15k | { |
205 | 1.15k | Tox_Events_State *state = tox_events_alloc(user_data); |
206 | 1.15k | Tox_Event_File_Chunk_Request *file_chunk_request = tox_event_file_chunk_request_alloc(state); |
207 | | |
208 | 1.15k | if (file_chunk_request == nullptr) { |
209 | 0 | return; |
210 | 0 | } |
211 | | |
212 | 1.15k | tox_event_file_chunk_request_set_friend_number(file_chunk_request, friend_number); |
213 | 1.15k | tox_event_file_chunk_request_set_file_number(file_chunk_request, file_number); |
214 | 1.15k | tox_event_file_chunk_request_set_position(file_chunk_request, position); |
215 | 1.15k | tox_event_file_chunk_request_set_length(file_chunk_request, length); |
216 | 1.15k | } |
217 | | |
218 | | void tox_events_handle_file_chunk_request_dispatch(Tox *tox, const Tox_Event_File_Chunk_Request *event, void *user_data) |
219 | 0 | { |
220 | 0 | if (tox->file_chunk_request_callback == nullptr) { |
221 | 0 | return; |
222 | 0 | } |
223 | | |
224 | 0 | tox_unlock(tox); |
225 | 0 | tox->file_chunk_request_callback(tox, event->friend_number, event->file_number, event->position, event->length, user_data); |
226 | 0 | tox_lock(tox); |
227 | 0 | } |