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