/work/toxcore/events/group_topic.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_Topic { |
27 | | uint32_t group_number; |
28 | | uint32_t peer_id; |
29 | | uint8_t *topic; |
30 | | uint32_t topic_length; |
31 | | }; |
32 | | |
33 | | non_null() |
34 | | static void tox_event_group_topic_set_group_number(Tox_Event_Group_Topic *group_topic, |
35 | | uint32_t group_number) |
36 | 927 | { |
37 | 927 | assert(group_topic != nullptr); |
38 | 927 | group_topic->group_number = group_number; |
39 | 927 | } |
40 | | uint32_t tox_event_group_topic_get_group_number(const Tox_Event_Group_Topic *group_topic) |
41 | 12 | { |
42 | 12 | assert(group_topic != nullptr); |
43 | 12 | return group_topic->group_number; |
44 | 12 | } |
45 | | |
46 | | non_null() |
47 | | static void tox_event_group_topic_set_peer_id(Tox_Event_Group_Topic *group_topic, |
48 | | uint32_t peer_id) |
49 | 927 | { |
50 | 927 | assert(group_topic != nullptr); |
51 | 927 | group_topic->peer_id = peer_id; |
52 | 927 | } |
53 | | uint32_t tox_event_group_topic_get_peer_id(const Tox_Event_Group_Topic *group_topic) |
54 | 0 | { |
55 | 0 | assert(group_topic != nullptr); |
56 | 0 | return group_topic->peer_id; |
57 | 0 | } |
58 | | |
59 | | non_null(1) nullable(2) |
60 | | static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic, |
61 | | const uint8_t *topic, uint32_t topic_length) |
62 | 927 | { |
63 | 927 | assert(group_topic != nullptr); |
64 | | |
65 | 927 | if (group_topic->topic != nullptr) { |
66 | 0 | free(group_topic->topic); |
67 | 0 | group_topic->topic = nullptr; |
68 | 0 | group_topic->topic_length = 0; |
69 | 0 | } |
70 | | |
71 | 927 | if (topic == nullptr) { |
72 | 0 | assert(topic_length == 0); |
73 | 0 | return true; |
74 | 0 | } |
75 | | |
76 | 927 | uint8_t *topic_copy = (uint8_t *)malloc(topic_length); |
77 | | |
78 | 927 | if (topic_copy == nullptr) { |
79 | 1 | return false; |
80 | 1 | } |
81 | | |
82 | 926 | memcpy(topic_copy, topic, topic_length); |
83 | 926 | group_topic->topic = topic_copy; |
84 | 926 | group_topic->topic_length = topic_length; |
85 | 926 | return true; |
86 | 927 | } |
87 | | uint32_t tox_event_group_topic_get_topic_length(const Tox_Event_Group_Topic *group_topic) |
88 | 887 | { |
89 | 887 | assert(group_topic != nullptr); |
90 | 887 | return group_topic->topic_length; |
91 | 887 | } |
92 | | const uint8_t *tox_event_group_topic_get_topic(const Tox_Event_Group_Topic *group_topic) |
93 | 887 | { |
94 | 887 | assert(group_topic != nullptr); |
95 | 887 | return group_topic->topic; |
96 | 887 | } |
97 | | |
98 | | non_null() |
99 | | static void tox_event_group_topic_construct(Tox_Event_Group_Topic *group_topic) |
100 | 950 | { |
101 | 950 | *group_topic = (Tox_Event_Group_Topic) { |
102 | 950 | 0 |
103 | 950 | }; |
104 | 950 | } |
105 | | non_null() |
106 | | static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *group_topic, const Memory *mem) |
107 | 949 | { |
108 | 949 | free(group_topic->topic); |
109 | 949 | } |
110 | | |
111 | | bool tox_event_group_topic_pack( |
112 | | const Tox_Event_Group_Topic *event, Bin_Pack *bp) |
113 | 28 | { |
114 | 28 | return bin_pack_array(bp, 3) |
115 | 28 | && bin_pack_u32(bp, event->group_number) |
116 | 28 | && bin_pack_u32(bp, event->peer_id) |
117 | 28 | && bin_pack_bin(bp, event->topic, event->topic_length); |
118 | 28 | } |
119 | | |
120 | | non_null() |
121 | | static bool tox_event_group_topic_unpack_into( |
122 | | Tox_Event_Group_Topic *event, Bin_Unpack *bu) |
123 | 23 | { |
124 | 23 | assert(event != nullptr); |
125 | 23 | if (!bin_unpack_array_fixed(bu, 3, nullptr)) { |
126 | 1 | return false; |
127 | 1 | } |
128 | | |
129 | 22 | return bin_unpack_u32(bu, &event->group_number) |
130 | 22 | && bin_unpack_u32(bu, &event->peer_id) |
131 | 22 | && bin_unpack_bin(bu, &event->topic, &event->topic_length); |
132 | 23 | } |
133 | | |
134 | | |
135 | | /***************************************************** |
136 | | * |
137 | | * :: new/free/add/get/size/unpack |
138 | | * |
139 | | *****************************************************/ |
140 | | |
141 | | const Tox_Event_Group_Topic *tox_event_get_group_topic(const Tox_Event *event) |
142 | 0 | { |
143 | 0 | return event->type == TOX_EVENT_GROUP_TOPIC ? event->data.group_topic : nullptr; |
144 | 0 | } |
145 | | |
146 | | Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem) |
147 | 952 | { |
148 | 952 | Tox_Event_Group_Topic *const group_topic = |
149 | 952 | (Tox_Event_Group_Topic *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic)); |
150 | | |
151 | 952 | if (group_topic == nullptr) { |
152 | 2 | return nullptr; |
153 | 2 | } |
154 | | |
155 | 950 | tox_event_group_topic_construct(group_topic); |
156 | 950 | return group_topic; |
157 | 952 | } |
158 | | |
159 | | void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem) |
160 | 950 | { |
161 | 950 | if (group_topic != nullptr) { |
162 | 949 | tox_event_group_topic_destruct(group_topic, mem); |
163 | 949 | } |
164 | 950 | mem_delete(mem, group_topic); |
165 | 950 | } |
166 | | |
167 | | non_null() |
168 | | static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *events, const Memory *mem) |
169 | 928 | { |
170 | 928 | Tox_Event_Group_Topic *const group_topic = tox_event_group_topic_new(mem); |
171 | | |
172 | 928 | if (group_topic == nullptr) { |
173 | 1 | return nullptr; |
174 | 1 | } |
175 | | |
176 | 927 | Tox_Event event; |
177 | 927 | event.type = TOX_EVENT_GROUP_TOPIC; |
178 | 927 | event.data.group_topic = group_topic; |
179 | | |
180 | 927 | tox_events_add(events, &event); |
181 | 927 | return group_topic; |
182 | 928 | } |
183 | | |
184 | | bool tox_event_group_topic_unpack( |
185 | | Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) |
186 | 24 | { |
187 | 24 | assert(event != nullptr); |
188 | 24 | assert(*event == nullptr); |
189 | 24 | *event = tox_event_group_topic_new(mem); |
190 | | |
191 | 24 | if (*event == nullptr) { |
192 | 1 | return false; |
193 | 1 | } |
194 | | |
195 | 23 | return tox_event_group_topic_unpack_into(*event, bu); |
196 | 24 | } |
197 | | |
198 | | non_null() |
199 | | static Tox_Event_Group_Topic *tox_event_group_topic_alloc(void *user_data) |
200 | 929 | { |
201 | 929 | Tox_Events_State *state = tox_events_alloc(user_data); |
202 | 929 | assert(state != nullptr); |
203 | | |
204 | 929 | if (state->events == nullptr) { |
205 | 1 | return nullptr; |
206 | 1 | } |
207 | | |
208 | 928 | Tox_Event_Group_Topic *group_topic = tox_events_add_group_topic(state->events, state->mem); |
209 | | |
210 | 928 | if (group_topic == nullptr) { |
211 | 1 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
212 | 1 | return nullptr; |
213 | 1 | } |
214 | | |
215 | 927 | return group_topic; |
216 | 928 | } |
217 | | |
218 | | |
219 | | /***************************************************** |
220 | | * |
221 | | * :: event handler |
222 | | * |
223 | | *****************************************************/ |
224 | | |
225 | | |
226 | | void tox_events_handle_group_topic(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length, |
227 | | void *user_data) |
228 | 929 | { |
229 | 929 | Tox_Event_Group_Topic *group_topic = tox_event_group_topic_alloc(user_data); |
230 | | |
231 | 929 | if (group_topic == nullptr) { |
232 | 2 | return; |
233 | 2 | } |
234 | | |
235 | 927 | tox_event_group_topic_set_group_number(group_topic, group_number); |
236 | 927 | tox_event_group_topic_set_peer_id(group_topic, peer_id); |
237 | 927 | tox_event_group_topic_set_topic(group_topic, topic, length); |
238 | 927 | } |