-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtcobs.h
More file actions
56 lines (46 loc) · 2.26 KB
/
tcobs.h
File metadata and controls
56 lines (46 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: MIT
//! \file tcobs.h
//! \brief tcobs declarations and macros.
#ifndef TCOBS_H_
#define TCOBS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
//! \brief Encode a raw byte stream with TCOBSv1.
//! \param output Destination buffer for encoded data.
//! \param input Source buffer with raw data to encode.
//! \param length Number of bytes in \p input.
//! \return Number of bytes written to \p output, or a negative value on error.
//! \details
//! A zero delimiter byte is not appended, so multiple encoded payloads can be concatenated
//! before adding a final `0x00` delimiter.
//! Buffer overlap is allowed when input lies inside output with sufficient positive offset.
//! In worst case, one sigil byte is inserted per 31 input bytes. Therefore provide at least
//! `length + ceil(length/31)` bytes in \p output.
//! \note If your compiler uses a pre-C99 dialect and does not support "__restrict",
//! define it in project settings.
int TCOBSEncode(void* __restrict output, const void* __restrict input, size_t length);
//! \brief Decode a TCOBSv1 payload.
//! \param output Destination buffer written backwards from `output + max`.
//! \param max Capacity of \p output in bytes.
//! \param input Source buffer with encoded payload (without delimiter).
//! \param length Number of bytes in \p input.
//! \return Number of decoded bytes, or a negative value on error.
//! \details
//! Decoding runs backwards from the end of \p input and writes backwards into \p output.
//! The decoded payload starts at `output + max - return_value`.
//! If the (positive) return value is larger than \p max, the output buffer was too small.
//! Partial overlap can work only when output is sufficiently behind input, because decoded
//! data can be longer than encoded data.
//! \note If your compiler uses a pre-C99 dialect and does not support "__restrict",
//! define it in project settings.
int TCOBSDecode(void* __restrict output, size_t max, const void* __restrict input, size_t length);
//! \brief Error code from TCOBSDecode when output capacity is insufficient.
#define OUT_BUFFER_TOO_SMALL (-1000000)
//! \brief Error code from TCOBSDecode when encoded input data is invalid.
#define INPUT_DATA_CORRUPTED (-2000000)
#ifdef __cplusplus
}
#endif
#endif // TCOBS_H_