Coverage Report

Created: 2026-10-24 08:16

/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 <stdint.h>
21
22
#include "attributes.h"
23
#include "logger.h"
24
25
#ifdef __cplusplus
26
extern "C" {
27
#endif
28
29
2.38k
#define STATE_COOKIE_GLOBAL 0x15ed1b1f
30
31
11.5k
#define STATE_COOKIE_TYPE  0x01ce
32
33
typedef enum State_Type {
34
    STATE_TYPE_NOSPAMKEYS    = 1,
35
    STATE_TYPE_DHT           = 2,
36
    STATE_TYPE_FRIENDS       = 3,
37
    STATE_TYPE_NAME          = 4,
38
    STATE_TYPE_STATUSMESSAGE = 5,
39
    STATE_TYPE_STATUS        = 6,
40
    STATE_TYPE_GROUPS        = 7,
41
    STATE_TYPE_TCP_RELAY     = 10,
42
    STATE_TYPE_PATH_NODE     = 11,
43
    STATE_TYPE_CONFERENCES   = 20,
44
    STATE_TYPE_END           = 255,
45
} State_Type;
46
47
// Returned by the state_load_cb to instruct the loader on what to do next.
48
typedef enum State_Load_Status {
49
    // Continue loading state data sections.
50
    STATE_LOAD_STATUS_CONTINUE,
51
    // An error occurred. Stop loading sections.
52
    STATE_LOAD_STATUS_ERROR,
53
    // We're at the end of the save data, terminate loading successfully.
54
    STATE_LOAD_STATUS_END,
55
} State_Load_Status;
56
57
typedef State_Load_Status state_load_cb(void *_Nonnull outer, const uint8_t *_Nonnull data, uint32_t length, uint16_t type);
58
59
/** state load/save */
60
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);
61
62
uint8_t *_Nonnull state_write_section_header(uint8_t *_Nonnull data, uint16_t cookie_type, uint32_t len, uint32_t section_type);
63
64
// Utilities for state data serialisation.
65
66
uint16_t lendian_to_host16(uint16_t lendian);
67
uint16_t host_to_lendian16(uint16_t host);
68
69
void host_to_lendian_bytes64(uint8_t *_Nonnull dest, uint64_t num);
70
void lendian_bytes_to_host64(uint64_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
71
72
void host_to_lendian_bytes32(uint8_t *_Nonnull dest, uint32_t num);
73
void lendian_bytes_to_host32(uint32_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
74
75
void host_to_lendian_bytes16(uint8_t *_Nonnull dest, uint16_t num);
76
void lendian_bytes_to_host16(uint16_t *_Nonnull dest, const uint8_t *_Nonnull lendian);
77
78
#ifdef __cplusplus
79
} /* extern "C" */
80
#endif
81
82
#endif /* C_TOXCORE_TOXCORE_STATE_H */