Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/state.h
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2020 The TokTok team.
3
 * Copyright © 2014 Tox project.
4
 */
5
6
/**
7
 * The state module is responsible for parsing the Tox save data format and for
8
 * saving state in that format.
9
 *
10
 * This module provides functions for iterating over serialised data sections
11
 * and reading/writing numbers in the correct format (little endian).
12
 *
13
 * Note that unlike the Tox network protocol, the save data stores its values in
14
 * little endian, which is native to most desktop and server architectures in
15
 * 2018.
16
 */
17
#ifndef C_TOXCORE_TOXCORE_STATE_H
18
#define C_TOXCORE_TOXCORE_STATE_H
19
20
#include "attributes.h"
21
#include "logger.h"
22
23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26
27
2.51k
#define STATE_COOKIE_GLOBAL 0x15ed1b1f
28
29
12.8k
#define STATE_COOKIE_TYPE  0x01ce
30
31
typedef enum State_Type {
32
    STATE_TYPE_NOSPAMKEYS    = 1,
33
    STATE_TYPE_DHT           = 2,
34
    STATE_TYPE_FRIENDS       = 3,
35
    STATE_TYPE_NAME          = 4,
36
    STATE_TYPE_STATUSMESSAGE = 5,
37
    STATE_TYPE_STATUS        = 6,
38
    STATE_TYPE_GROUPS        = 7,
39
    STATE_TYPE_TCP_RELAY     = 10,
40
    STATE_TYPE_PATH_NODE     = 11,
41
    STATE_TYPE_CONFERENCES   = 20,
42
    STATE_TYPE_END           = 255,
43
} State_Type;
44
45
// Returned by the state_load_cb to instruct the loader on what to do next.
46
typedef enum State_Load_Status {
47
    // Continue loading state data sections.
48
    STATE_LOAD_STATUS_CONTINUE,
49
    // An error occurred. Stop loading sections.
50
    STATE_LOAD_STATUS_ERROR,
51
    // We're at the end of the save data, terminate loading successfully.
52
    STATE_LOAD_STATUS_END,
53
} State_Load_Status;
54
55
typedef State_Load_Status state_load_cb(void *_Nonnull outer, const uint8_t *_Nonnull data, uint32_t length, uint16_t type);
56
57
/** state load/save */
58
int state_load(const Logger *_Nonnull log, state_load_cb *_Nonnull state_load_callback, void *_Nonnull outer, const uint8_t *_Nonnull data, uint32_t length, uint16_t cookie_inner);
59
60
uint8_t *_Nonnull state_write_section_header(uint8_t *_Nonnull data, uint16_t cookie_type, uint32_t len, uint32_t section_type);
61
62
// Utilities for state data serialisation.
63
64
uint16_t lendian_to_host16(uint16_t lendian);
65
uint16_t host_to_lendian16(uint16_t host);
66
67
void host_to_lendian_bytes64(uint8_t *_Nonnull dest, uint64_t num);
68
void lendian_bytes_to_host64(uint64_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
69
70
void host_to_lendian_bytes32(uint8_t *_Nonnull dest, uint32_t num);
71
void lendian_bytes_to_host32(uint32_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
72
73
void host_to_lendian_bytes16(uint8_t *_Nonnull dest, uint16_t num);
74
void lendian_bytes_to_host16(uint16_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
75
76
#ifdef __cplusplus
77
} /* extern "C" */
78
#endif
79
80
#endif /* C_TOXCORE_TOXCORE_STATE_H */