Coverage Report

Created: 2025-10-08 19:34

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