Coverage Report

Created: 2024-01-26 01:52

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