Coverage Report

Created: 2024-01-26 01:52

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