/work/toxcore/events/group_custom_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_Group_Custom_Packet { |
27 | | uint32_t group_number; |
28 | | uint32_t peer_id; |
29 | | uint8_t *data; |
30 | | uint32_t data_length; |
31 | | }; |
32 | | |
33 | | static void tox_event_group_custom_packet_set_group_number(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, uint32_t group_number) |
34 | 3 | { |
35 | 3 | assert(group_custom_packet != nullptr); |
36 | 3 | group_custom_packet->group_number = group_number; |
37 | 3 | } |
38 | | uint32_t tox_event_group_custom_packet_get_group_number(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
39 | 2 | { |
40 | 2 | assert(group_custom_packet != nullptr); |
41 | 2 | return group_custom_packet->group_number; |
42 | 2 | } |
43 | | |
44 | | static void tox_event_group_custom_packet_set_peer_id(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, uint32_t peer_id) |
45 | 3 | { |
46 | 3 | assert(group_custom_packet != nullptr); |
47 | 3 | group_custom_packet->peer_id = peer_id; |
48 | 3 | } |
49 | | uint32_t tox_event_group_custom_packet_get_peer_id(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
50 | 2 | { |
51 | 2 | assert(group_custom_packet != nullptr); |
52 | 2 | return group_custom_packet->peer_id; |
53 | 2 | } |
54 | | |
55 | | static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, |
56 | | const uint8_t *_Nullable data, uint32_t data_length) |
57 | 3 | { |
58 | 3 | assert(group_custom_packet != nullptr); |
59 | 3 | if (group_custom_packet->data != nullptr) { |
60 | 0 | free(group_custom_packet->data); |
61 | 0 | group_custom_packet->data = nullptr; |
62 | 0 | group_custom_packet->data_length = 0; |
63 | 0 | } |
64 | | |
65 | 3 | if (data == nullptr) { |
66 | 0 | assert(data_length == 0); |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | 3 | uint8_t *data_copy = (uint8_t *)malloc(data_length); |
71 | | |
72 | 3 | if (data_copy == nullptr) { |
73 | 0 | return false; |
74 | 0 | } |
75 | | |
76 | 3 | memcpy(data_copy, data, data_length); |
77 | 3 | group_custom_packet->data = data_copy; |
78 | 3 | group_custom_packet->data_length = data_length; |
79 | 3 | return true; |
80 | 3 | } |
81 | | uint32_t tox_event_group_custom_packet_get_data_length(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
82 | 3 | { |
83 | 3 | assert(group_custom_packet != nullptr); |
84 | 3 | return group_custom_packet->data_length; |
85 | 3 | } |
86 | | const uint8_t *tox_event_group_custom_packet_get_data(const Tox_Event_Group_Custom_Packet *group_custom_packet) |
87 | 3 | { |
88 | 3 | assert(group_custom_packet != nullptr); |
89 | 3 | return group_custom_packet->data; |
90 | 3 | } |
91 | | |
92 | | static void tox_event_group_custom_packet_construct(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet) |
93 | 17 | { |
94 | 17 | *group_custom_packet = (Tox_Event_Group_Custom_Packet) { |
95 | 17 | 0 |
96 | 17 | }; |
97 | 17 | } |
98 | | static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet *_Nonnull group_custom_packet, const Memory *_Nonnull mem) |
99 | 17 | { |
100 | 17 | free(group_custom_packet->data); |
101 | 17 | } |
102 | | |
103 | | bool tox_event_group_custom_packet_pack( |
104 | | const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp) |
105 | 20 | { |
106 | 20 | return bin_pack_array(bp, 3) |
107 | 20 | && bin_pack_u32(bp, event->group_number) |
108 | 20 | && bin_pack_u32(bp, event->peer_id) |
109 | 20 | && bin_pack_bin(bp, event->data, event->data_length); |
110 | 20 | } |
111 | | |
112 | | static bool tox_event_group_custom_packet_unpack_into(Tox_Event_Group_Custom_Packet *_Nonnull event, Bin_Unpack *_Nonnull bu) |
113 | 14 | { |
114 | 14 | assert(event != nullptr); |
115 | 14 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
116 | 1 | return false; |
117 | 1 | } |
118 | | |
119 | 13 | return bin_unpack_u32(bu, &event->group_number) |
120 | 13 | && bin_unpack_u32(bu, &event->peer_id) |
121 | 13 | && bin_unpack_bin(bu, &event->data, &event->data_length); |
122 | 14 | } |
123 | | |
124 | | /***************************************************** |
125 | | * |
126 | | * :: new/free/add/get/size/unpack |
127 | | * |
128 | | *****************************************************/ |
129 | | |
130 | | const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet(const Tox_Event *event) |
131 | 0 | { |
132 | 0 | return event->type == TOX_EVENT_GROUP_CUSTOM_PACKET ? event->data.group_custom_packet : nullptr; |
133 | 0 | } |
134 | | |
135 | | Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem) |
136 | 18 | { |
137 | 18 | Tox_Event_Group_Custom_Packet *const group_custom_packet = |
138 | 18 | (Tox_Event_Group_Custom_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Packet)); |
139 | | |
140 | 18 | if (group_custom_packet == nullptr) { |
141 | 1 | return nullptr; |
142 | 1 | } |
143 | | |
144 | 17 | tox_event_group_custom_packet_construct(group_custom_packet); |
145 | 17 | return group_custom_packet; |
146 | 18 | } |
147 | | |
148 | | void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem) |
149 | 18 | { |
150 | 18 | if (group_custom_packet != nullptr) { |
151 | 17 | tox_event_group_custom_packet_destruct(group_custom_packet, mem); |
152 | 17 | } |
153 | 18 | mem_delete(mem, group_custom_packet); |
154 | 18 | } |
155 | | |
156 | | static Tox_Event_Group_Custom_Packet *tox_events_add_group_custom_packet(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
157 | 3 | { |
158 | 3 | Tox_Event_Group_Custom_Packet *const group_custom_packet = tox_event_group_custom_packet_new(mem); |
159 | | |
160 | 3 | if (group_custom_packet == nullptr) { |
161 | 0 | return nullptr; |
162 | 0 | } |
163 | | |
164 | 3 | Tox_Event event; |
165 | 3 | event.type = TOX_EVENT_GROUP_CUSTOM_PACKET; |
166 | 3 | event.data.group_custom_packet = group_custom_packet; |
167 | | |
168 | 3 | if (!tox_events_add(events, &event)) { |
169 | 0 | tox_event_group_custom_packet_free(group_custom_packet, mem); |
170 | 0 | return nullptr; |
171 | 0 | } |
172 | 3 | return group_custom_packet; |
173 | 3 | } |
174 | | |
175 | | bool tox_event_group_custom_packet_unpack( |
176 | | Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) |
177 | 15 | { |
178 | 15 | assert(event != nullptr); |
179 | 15 | assert(*event == nullptr); |
180 | 15 | *event = tox_event_group_custom_packet_new(mem); |
181 | | |
182 | 15 | if (*event == nullptr) { |
183 | 1 | return false; |
184 | 1 | } |
185 | | |
186 | 14 | return tox_event_group_custom_packet_unpack_into(*event, bu); |
187 | 15 | } |
188 | | |
189 | | static Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_alloc(void *_Nonnull user_data) |
190 | 3 | { |
191 | 3 | Tox_Events_State *state = tox_events_alloc(user_data); |
192 | 3 | assert(state != nullptr); |
193 | | |
194 | 3 | if (state->events == nullptr) { |
195 | 0 | return nullptr; |
196 | 0 | } |
197 | | |
198 | 3 | Tox_Event_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events, state->mem); |
199 | | |
200 | 3 | if (group_custom_packet == nullptr) { |
201 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
202 | 0 | return nullptr; |
203 | 0 | } |
204 | | |
205 | 3 | return group_custom_packet; |
206 | 3 | } |
207 | | |
208 | | /***************************************************** |
209 | | * |
210 | | * :: event handler |
211 | | * |
212 | | *****************************************************/ |
213 | | |
214 | | void tox_events_handle_group_custom_packet( |
215 | | Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t data_length, |
216 | | void *user_data) |
217 | 3 | { |
218 | 3 | Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(user_data); |
219 | | |
220 | 3 | if (group_custom_packet == nullptr) { |
221 | 0 | return; |
222 | 0 | } |
223 | | |
224 | 3 | tox_event_group_custom_packet_set_group_number(group_custom_packet, group_number); |
225 | 3 | tox_event_group_custom_packet_set_peer_id(group_custom_packet, peer_id); |
226 | 3 | tox_event_group_custom_packet_set_data(group_custom_packet, data, data_length); |
227 | 3 | } |