Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/events/file_recv.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_File_Recv {
27
    uint32_t friend_number;
28
    uint32_t file_number;
29
    uint32_t kind;
30
    uint64_t file_size;
31
    uint8_t *filename;
32
    uint32_t filename_length;
33
};
34
35
static void tox_event_file_recv_set_friend_number(Tox_Event_File_Recv *_Nonnull file_recv, uint32_t friend_number)
36
2
{
37
2
    assert(file_recv != nullptr);
38
2
    file_recv->friend_number = friend_number;
39
2
}
40
uint32_t tox_event_file_recv_get_friend_number(const Tox_Event_File_Recv *file_recv)
41
2
{
42
2
    assert(file_recv != nullptr);
43
2
    return file_recv->friend_number;
44
2
}
45
46
static void tox_event_file_recv_set_file_number(Tox_Event_File_Recv *_Nonnull file_recv, uint32_t file_number)
47
2
{
48
2
    assert(file_recv != nullptr);
49
2
    file_recv->file_number = file_number;
50
2
}
51
uint32_t tox_event_file_recv_get_file_number(const Tox_Event_File_Recv *file_recv)
52
2
{
53
2
    assert(file_recv != nullptr);
54
2
    return file_recv->file_number;
55
2
}
56
57
static void tox_event_file_recv_set_kind(Tox_Event_File_Recv *_Nonnull file_recv, uint32_t kind)
58
2
{
59
2
    assert(file_recv != nullptr);
60
2
    file_recv->kind = kind;
61
2
}
62
uint32_t tox_event_file_recv_get_kind(const Tox_Event_File_Recv *file_recv)
63
2
{
64
2
    assert(file_recv != nullptr);
65
2
    return file_recv->kind;
66
2
}
67
68
static void tox_event_file_recv_set_file_size(Tox_Event_File_Recv *_Nonnull file_recv, uint64_t file_size)
69
2
{
70
2
    assert(file_recv != nullptr);
71
2
    file_recv->file_size = file_size;
72
2
}
73
uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv)
74
2
{
75
2
    assert(file_recv != nullptr);
76
2
    return file_recv->file_size;
77
2
}
78
79
static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *_Nonnull file_recv,
80
        const uint8_t *_Nullable filename, uint32_t filename_length)
81
2
{
82
2
    assert(file_recv != nullptr);
83
2
    if (file_recv->filename != nullptr) {
84
0
        free(file_recv->filename);
85
0
        file_recv->filename = nullptr;
86
0
        file_recv->filename_length = 0;
87
0
    }
88
89
2
    if (filename == nullptr) {
90
0
        assert(filename_length == 0);
91
0
        return true;
92
0
    }
93
94
2
    uint8_t *filename_copy = (uint8_t *)malloc(filename_length);
95
96
2
    if (filename_copy == nullptr) {
97
0
        return false;
98
0
    }
99
100
2
    memcpy(filename_copy, filename, filename_length);
101
2
    file_recv->filename = filename_copy;
102
2
    file_recv->filename_length = filename_length;
103
2
    return true;
104
2
}
105
uint32_t tox_event_file_recv_get_filename_length(const Tox_Event_File_Recv *file_recv)
106
2
{
107
2
    assert(file_recv != nullptr);
108
2
    return file_recv->filename_length;
109
2
}
110
const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_recv)
111
2
{
112
2
    assert(file_recv != nullptr);
113
2
    return file_recv->filename;
114
2
}
115
116
static void tox_event_file_recv_construct(Tox_Event_File_Recv *_Nonnull file_recv)
117
33
{
118
33
    *file_recv = (Tox_Event_File_Recv) {
119
33
        0
120
33
    };
121
33
}
122
static void tox_event_file_recv_destruct(Tox_Event_File_Recv *_Nonnull file_recv, const Memory *_Nonnull mem)
123
33
{
124
33
    free(file_recv->filename);
125
33
}
126
127
bool tox_event_file_recv_pack(
128
    const Tox_Event_File_Recv *event, Bin_Pack *bp)
129
36
{
130
36
    return bin_pack_array(bp, 5)
131
36
           && bin_pack_u32(bp, event->friend_number)
132
36
           && bin_pack_u32(bp, event->file_number)
133
36
           && bin_pack_u32(bp, event->kind)
134
36
           && bin_pack_u64(bp, event->file_size)
135
36
           && bin_pack_bin(bp, event->filename, event->filename_length);
136
36
}
137
138
static bool tox_event_file_recv_unpack_into(Tox_Event_File_Recv *_Nonnull event, Bin_Unpack *_Nonnull bu)
139
31
{
140
31
    assert(event != nullptr);
141
31
    if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
142
2
        return false;
143
2
    }
144
145
29
    return bin_unpack_u32(bu, &event->friend_number)
146
29
           && bin_unpack_u32(bu, &event->file_number)
147
29
           && bin_unpack_u32(bu, &event->kind)
148
29
           && bin_unpack_u64(bu, &event->file_size)
149
29
           && bin_unpack_bin(bu, &event->filename, &event->filename_length);
150
31
}
151
152
/*****************************************************
153
 *
154
 * :: new/free/add/get/size/unpack
155
 *
156
 *****************************************************/
157
158
const Tox_Event_File_Recv *tox_event_get_file_recv(const Tox_Event *event)
159
0
{
160
0
    return event->type == TOX_EVENT_FILE_RECV ? event->data.file_recv : nullptr;
161
0
}
162
163
Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem)
164
34
{
165
34
    Tox_Event_File_Recv *const file_recv =
166
34
        (Tox_Event_File_Recv *)mem_alloc(mem, sizeof(Tox_Event_File_Recv));
167
168
34
    if (file_recv == nullptr) {
169
1
        return nullptr;
170
1
    }
171
172
33
    tox_event_file_recv_construct(file_recv);
173
33
    return file_recv;
174
34
}
175
176
void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem)
177
34
{
178
34
    if (file_recv != nullptr) {
179
33
        tox_event_file_recv_destruct(file_recv, mem);
180
33
    }
181
34
    mem_delete(mem, file_recv);
182
34
}
183
184
static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *_Nonnull events, const Memory *_Nonnull mem)
185
2
{
186
2
    Tox_Event_File_Recv *const file_recv = tox_event_file_recv_new(mem);
187
188
2
    if (file_recv == nullptr) {
189
0
        return nullptr;
190
0
    }
191
192
2
    Tox_Event event;
193
2
    event.type = TOX_EVENT_FILE_RECV;
194
2
    event.data.file_recv = file_recv;
195
196
2
    if (!tox_events_add(events, &event)) {
197
0
        tox_event_file_recv_free(file_recv, mem);
198
0
        return nullptr;
199
0
    }
200
2
    return file_recv;
201
2
}
202
203
bool tox_event_file_recv_unpack(
204
    Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem)
205
32
{
206
32
    assert(event != nullptr);
207
32
    assert(*event == nullptr);
208
32
    *event = tox_event_file_recv_new(mem);
209
210
32
    if (*event == nullptr) {
211
1
        return false;
212
1
    }
213
214
31
    return tox_event_file_recv_unpack_into(*event, bu);
215
32
}
216
217
static Tox_Event_File_Recv *tox_event_file_recv_alloc(void *_Nonnull user_data)
218
2
{
219
2
    Tox_Events_State *state = tox_events_alloc(user_data);
220
2
    assert(state != nullptr);
221
222
2
    if (state->events == nullptr) {
223
0
        return nullptr;
224
0
    }
225
226
2
    Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events, state->mem);
227
228
2
    if (file_recv == nullptr) {
229
0
        state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
230
0
        return nullptr;
231
0
    }
232
233
2
    return file_recv;
234
2
}
235
236
/*****************************************************
237
 *
238
 * :: event handler
239
 *
240
 *****************************************************/
241
242
void tox_events_handle_file_recv(
243
    Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length,
244
    void *user_data)
245
2
{
246
2
    Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data);
247
248
2
    if (file_recv == nullptr) {
249
0
        return;
250
0
    }
251
252
2
    tox_event_file_recv_set_friend_number(file_recv, friend_number);
253
2
    tox_event_file_recv_set_file_number(file_recv, file_number);
254
2
    tox_event_file_recv_set_kind(file_recv, kind);
255
2
    tox_event_file_recv_set_file_size(file_recv, file_size);
256
2
    tox_event_file_recv_set_filename(file_recv, filename, filename_length);
257
2
}