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