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