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