/work/toxcore/events/group_password.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_Password { |
27 | | uint32_t group_number; |
28 | | uint8_t *password; |
29 | | uint32_t password_length; |
30 | | }; |
31 | | |
32 | | static void tox_event_group_password_set_group_number(Tox_Event_Group_Password *_Nonnull group_password, uint32_t group_number) |
33 | 43 | { |
34 | 43 | assert(group_password != nullptr); |
35 | 43 | group_password->group_number = group_number; |
36 | 43 | } |
37 | | uint32_t tox_event_group_password_get_group_number(const Tox_Event_Group_Password *group_password) |
38 | 4 | { |
39 | 4 | assert(group_password != nullptr); |
40 | 4 | return group_password->group_number; |
41 | 4 | } |
42 | | |
43 | | static bool tox_event_group_password_set_password(Tox_Event_Group_Password *_Nonnull group_password, |
44 | | const uint8_t *_Nullable password, uint32_t password_length) |
45 | 43 | { |
46 | 43 | assert(group_password != nullptr); |
47 | 43 | if (group_password->password != nullptr) { |
48 | 0 | free(group_password->password); |
49 | 0 | group_password->password = nullptr; |
50 | 0 | group_password->password_length = 0; |
51 | 0 | } |
52 | | |
53 | 43 | if (password == nullptr) { |
54 | 0 | assert(password_length == 0); |
55 | 0 | return true; |
56 | 0 | } |
57 | | |
58 | 43 | uint8_t *password_copy = (uint8_t *)malloc(password_length); |
59 | | |
60 | 43 | if (password_copy == nullptr) { |
61 | 1 | return false; |
62 | 1 | } |
63 | | |
64 | 42 | memcpy(password_copy, password, password_length); |
65 | 42 | group_password->password = password_copy; |
66 | 42 | group_password->password_length = password_length; |
67 | 42 | return true; |
68 | 43 | } |
69 | | uint32_t tox_event_group_password_get_password_length(const Tox_Event_Group_Password *group_password) |
70 | 4 | { |
71 | 4 | assert(group_password != nullptr); |
72 | 4 | return group_password->password_length; |
73 | 4 | } |
74 | | const uint8_t *tox_event_group_password_get_password(const Tox_Event_Group_Password *group_password) |
75 | 4 | { |
76 | 4 | assert(group_password != nullptr); |
77 | 4 | return group_password->password; |
78 | 4 | } |
79 | | |
80 | | static void tox_event_group_password_construct(Tox_Event_Group_Password *_Nonnull group_password) |
81 | 69 | { |
82 | 69 | *group_password = (Tox_Event_Group_Password) { |
83 | 69 | 0 |
84 | 69 | }; |
85 | 69 | } |
86 | | static void tox_event_group_password_destruct(Tox_Event_Group_Password *_Nonnull group_password, const Memory *_Nonnull mem) |
87 | 69 | { |
88 | 69 | free(group_password->password); |
89 | 69 | } |
90 | | |
91 | | bool tox_event_group_password_pack( |
92 | | const Tox_Event_Group_Password *event, Bin_Pack *bp) |
93 | 36 | { |
94 | 36 | return bin_pack_array(bp, 2) |
95 | 36 | && bin_pack_u32(bp, event->group_number) |
96 | 36 | && bin_pack_bin(bp, event->password, event->password_length); |
97 | 36 | } |
98 | | |
99 | | static bool tox_event_group_password_unpack_into(Tox_Event_Group_Password *_Nonnull event, Bin_Unpack *_Nonnull bu) |
100 | 25 | { |
101 | 25 | assert(event != nullptr); |
102 | 25 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
103 | 1 | return false; |
104 | 1 | } |
105 | | |
106 | 24 | return bin_unpack_u32(bu, &event->group_number) |
107 | 24 | && bin_unpack_bin(bu, &event->password, &event->password_length); |
108 | 25 | } |
109 | | |
110 | | /***************************************************** |
111 | | * |
112 | | * :: new/free/add/get/size/unpack |
113 | | * |
114 | | *****************************************************/ |
115 | | |
116 | | const Tox_Event_Group_Password *tox_event_get_group_password(const Tox_Event *event) |
117 | 0 | { |
118 | 0 | return event->type == TOX_EVENT_GROUP_PASSWORD ? event->data.group_password : nullptr; |
119 | 0 | } |
120 | | |
121 | | Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem) |
122 | 71 | { |
123 | 71 | Tox_Event_Group_Password *const group_password = |
124 | 71 | (Tox_Event_Group_Password *)mem_alloc(mem, sizeof(Tox_Event_Group_Password)); |
125 | | |
126 | 71 | if (group_password == nullptr) { |
127 | 2 | return nullptr; |
128 | 2 | } |
129 | | |
130 | 69 | tox_event_group_password_construct(group_password); |
131 | 69 | return group_password; |
132 | 71 | } |
133 | | |
134 | | void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem) |
135 | 70 | { |
136 | 70 | if (group_password != nullptr) { |
137 | 69 | tox_event_group_password_destruct(group_password, mem); |
138 | 69 | } |
139 | 70 | mem_delete(mem, group_password); |
140 | 70 | } |
141 | | |
142 | | static Tox_Event_Group_Password *tox_events_add_group_password(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
143 | 45 | { |
144 | 45 | Tox_Event_Group_Password *const group_password = tox_event_group_password_new(mem); |
145 | | |
146 | 45 | if (group_password == nullptr) { |
147 | 1 | return nullptr; |
148 | 1 | } |
149 | | |
150 | 44 | Tox_Event event; |
151 | 44 | event.type = TOX_EVENT_GROUP_PASSWORD; |
152 | 44 | event.data.group_password = group_password; |
153 | | |
154 | 44 | if (!tox_events_add(events, &event)) { |
155 | 1 | tox_event_group_password_free(group_password, mem); |
156 | 1 | return nullptr; |
157 | 1 | } |
158 | 43 | return group_password; |
159 | 44 | } |
160 | | |
161 | | bool tox_event_group_password_unpack( |
162 | | Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) |
163 | 26 | { |
164 | 26 | assert(event != nullptr); |
165 | 26 | assert(*event == nullptr); |
166 | 26 | *event = tox_event_group_password_new(mem); |
167 | | |
168 | 26 | if (*event == nullptr) { |
169 | 1 | return false; |
170 | 1 | } |
171 | | |
172 | 25 | return tox_event_group_password_unpack_into(*event, bu); |
173 | 26 | } |
174 | | |
175 | | static Tox_Event_Group_Password *tox_event_group_password_alloc(void *_Nonnull user_data) |
176 | 46 | { |
177 | 46 | Tox_Events_State *state = tox_events_alloc(user_data); |
178 | 46 | assert(state != nullptr); |
179 | | |
180 | 46 | if (state->events == nullptr) { |
181 | 1 | return nullptr; |
182 | 1 | } |
183 | | |
184 | 45 | Tox_Event_Group_Password *group_password = tox_events_add_group_password(state->events, state->mem); |
185 | | |
186 | 45 | if (group_password == nullptr) { |
187 | 2 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
188 | 2 | return nullptr; |
189 | 2 | } |
190 | | |
191 | 43 | return group_password; |
192 | 45 | } |
193 | | |
194 | | /***************************************************** |
195 | | * |
196 | | * :: event handler |
197 | | * |
198 | | *****************************************************/ |
199 | | |
200 | | void tox_events_handle_group_password( |
201 | | Tox *tox, uint32_t group_number, const uint8_t *password, size_t password_length, |
202 | | void *user_data) |
203 | 46 | { |
204 | 46 | Tox_Event_Group_Password *group_password = tox_event_group_password_alloc(user_data); |
205 | | |
206 | 46 | if (group_password == nullptr) { |
207 | 3 | return; |
208 | 3 | } |
209 | | |
210 | 43 | tox_event_group_password_set_group_number(group_password, group_number); |
211 | 43 | tox_event_group_password_set_password(group_password, password, password_length); |
212 | 43 | } |