/work/toxcore/events/friend_lossy_packet.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 | | #include <stdlib.h> |
9 | | #include <string.h> |
10 | | |
11 | | #include "../attributes.h" |
12 | | #include "../bin_pack.h" |
13 | | #include "../bin_unpack.h" |
14 | | #include "../ccompat.h" |
15 | | #include "../mem.h" |
16 | | #include "../tox.h" |
17 | | #include "../tox_event.h" |
18 | | #include "../tox_events.h" |
19 | | |
20 | | /***************************************************** |
21 | | * |
22 | | * :: struct and accessors |
23 | | * |
24 | | *****************************************************/ |
25 | | |
26 | | struct Tox_Event_Friend_Lossy_Packet { |
27 | | uint32_t friend_number; |
28 | | uint8_t *data; |
29 | | uint32_t data_length; |
30 | | }; |
31 | | |
32 | | static void tox_event_friend_lossy_packet_set_friend_number(Tox_Event_Friend_Lossy_Packet *_Nonnull friend_lossy_packet, uint32_t friend_number) |
33 | 5 | { |
34 | 5 | assert(friend_lossy_packet != nullptr); |
35 | 5 | friend_lossy_packet->friend_number = friend_number; |
36 | 5 | } |
37 | | uint32_t tox_event_friend_lossy_packet_get_friend_number(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) |
38 | 0 | { |
39 | 0 | assert(friend_lossy_packet != nullptr); |
40 | 0 | return friend_lossy_packet->friend_number; |
41 | 0 | } |
42 | | |
43 | | static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet *_Nonnull friend_lossy_packet, |
44 | | const uint8_t *_Nullable data, uint32_t data_length) |
45 | 5 | { |
46 | 5 | assert(friend_lossy_packet != nullptr); |
47 | 5 | if (friend_lossy_packet->data != nullptr) { |
48 | 0 | free(friend_lossy_packet->data); |
49 | 0 | friend_lossy_packet->data = nullptr; |
50 | 0 | friend_lossy_packet->data_length = 0; |
51 | 0 | } |
52 | | |
53 | 5 | if (data == nullptr) { |
54 | 0 | assert(data_length == 0); |
55 | 0 | return true; |
56 | 0 | } |
57 | | |
58 | 5 | uint8_t *data_copy = (uint8_t *)malloc(data_length); |
59 | | |
60 | 5 | if (data_copy == nullptr) { |
61 | 1 | return false; |
62 | 1 | } |
63 | | |
64 | 4 | memcpy(data_copy, data, data_length); |
65 | 4 | friend_lossy_packet->data = data_copy; |
66 | 4 | friend_lossy_packet->data_length = data_length; |
67 | 4 | return true; |
68 | 5 | } |
69 | | uint32_t tox_event_friend_lossy_packet_get_data_length(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) |
70 | 5 | { |
71 | 5 | assert(friend_lossy_packet != nullptr); |
72 | 5 | return friend_lossy_packet->data_length; |
73 | 5 | } |
74 | | const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) |
75 | 5 | { |
76 | 5 | assert(friend_lossy_packet != nullptr); |
77 | 5 | return friend_lossy_packet->data; |
78 | 5 | } |
79 | | |
80 | | static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *_Nonnull friend_lossy_packet) |
81 | 32 | { |
82 | 32 | *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { |
83 | 32 | 0 |
84 | 32 | }; |
85 | 32 | } |
86 | | static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *_Nonnull friend_lossy_packet, const Memory *_Nonnull mem) |
87 | 31 | { |
88 | 31 | free(friend_lossy_packet->data); |
89 | 31 | } |
90 | | |
91 | | bool tox_event_friend_lossy_packet_pack( |
92 | | const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp) |
93 | 26 | { |
94 | 26 | return bin_pack_array(bp, 2) |
95 | 26 | && bin_pack_u32(bp, event->friend_number) |
96 | 26 | && bin_pack_bin(bp, event->data, event->data_length); |
97 | 26 | } |
98 | | |
99 | | static bool tox_event_friend_lossy_packet_unpack_into(Tox_Event_Friend_Lossy_Packet *_Nonnull event, Bin_Unpack *_Nonnull bu) |
100 | 26 | { |
101 | 26 | assert(event != nullptr); |
102 | 26 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
103 | 1 | return false; |
104 | 1 | } |
105 | | |
106 | 25 | return bin_unpack_u32(bu, &event->friend_number) |
107 | 25 | && bin_unpack_bin(bu, &event->data, &event->data_length); |
108 | 26 | } |
109 | | |
110 | | /***************************************************** |
111 | | * |
112 | | * :: new/free/add/get/size/unpack |
113 | | * |
114 | | *****************************************************/ |
115 | | |
116 | | const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet(const Tox_Event *event) |
117 | 0 | { |
118 | 0 | return event->type == TOX_EVENT_FRIEND_LOSSY_PACKET ? event->data.friend_lossy_packet : nullptr; |
119 | 0 | } |
120 | | |
121 | | Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem) |
122 | 34 | { |
123 | 34 | Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = |
124 | 34 | (Tox_Event_Friend_Lossy_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossy_Packet)); |
125 | | |
126 | 34 | if (friend_lossy_packet == nullptr) { |
127 | 2 | return nullptr; |
128 | 2 | } |
129 | | |
130 | 32 | tox_event_friend_lossy_packet_construct(friend_lossy_packet); |
131 | 32 | return friend_lossy_packet; |
132 | 34 | } |
133 | | |
134 | | void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) |
135 | 32 | { |
136 | 32 | if (friend_lossy_packet != nullptr) { |
137 | 31 | tox_event_friend_lossy_packet_destruct(friend_lossy_packet, mem); |
138 | 31 | } |
139 | 32 | mem_delete(mem, friend_lossy_packet); |
140 | 32 | } |
141 | | |
142 | | static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
143 | 7 | { |
144 | 7 | Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); |
145 | | |
146 | 7 | if (friend_lossy_packet == nullptr) { |
147 | 1 | return nullptr; |
148 | 1 | } |
149 | | |
150 | 6 | Tox_Event event; |
151 | 6 | event.type = TOX_EVENT_FRIEND_LOSSY_PACKET; |
152 | 6 | event.data.friend_lossy_packet = friend_lossy_packet; |
153 | | |
154 | 6 | if (!tox_events_add(events, &event)) { |
155 | 1 | tox_event_friend_lossy_packet_free(friend_lossy_packet, mem); |
156 | 1 | return nullptr; |
157 | 1 | } |
158 | 5 | return friend_lossy_packet; |
159 | 6 | } |
160 | | |
161 | | bool tox_event_friend_lossy_packet_unpack( |
162 | | Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) |
163 | 27 | { |
164 | 27 | assert(event != nullptr); |
165 | 27 | assert(*event == nullptr); |
166 | 27 | *event = tox_event_friend_lossy_packet_new(mem); |
167 | | |
168 | 27 | if (*event == nullptr) { |
169 | 1 | return false; |
170 | 1 | } |
171 | | |
172 | 26 | return tox_event_friend_lossy_packet_unpack_into(*event, bu); |
173 | 27 | } |
174 | | |
175 | | static Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_alloc(void *_Nonnull user_data) |
176 | 8 | { |
177 | 8 | Tox_Events_State *state = tox_events_alloc(user_data); |
178 | 8 | assert(state != nullptr); |
179 | | |
180 | 8 | if (state->events == nullptr) { |
181 | 1 | return nullptr; |
182 | 1 | } |
183 | | |
184 | 7 | Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events, state->mem); |
185 | | |
186 | 7 | if (friend_lossy_packet == nullptr) { |
187 | 2 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
188 | 2 | return nullptr; |
189 | 2 | } |
190 | | |
191 | 5 | return friend_lossy_packet; |
192 | 7 | } |
193 | | |
194 | | /***************************************************** |
195 | | * |
196 | | * :: event handler |
197 | | * |
198 | | *****************************************************/ |
199 | | |
200 | | void tox_events_handle_friend_lossy_packet( |
201 | | Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, |
202 | | void *user_data) |
203 | 8 | { |
204 | 8 | Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_event_friend_lossy_packet_alloc(user_data); |
205 | | |
206 | 8 | if (friend_lossy_packet == nullptr) { |
207 | 3 | return; |
208 | 3 | } |
209 | | |
210 | 5 | tox_event_friend_lossy_packet_set_friend_number(friend_lossy_packet, friend_number); |
211 | 5 | tox_event_friend_lossy_packet_set_data(friend_lossy_packet, data, length); |
212 | 5 | } |