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