Coverage Report

Created: 2024-01-26 01:52

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