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