summaryrefslogtreecommitdiff
path: root/ccan/pushpull/pull.h
blob: e675a8dda03727e0f92c3a9233869bcd73a77c88 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* CC0 license (public domain) - see LICENSE file for details */
#ifndef CCAN_PUSHPULL_PULL_H
#define CCAN_PUSHPULL_PULL_H
#include "config.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

/**
 * pull_bytes - unmarshall bytes from push_bytes.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @dst: destination to copy bytes (or NULL to discard)
 * @len: length to copy into @dst.
 *
 * If @max_len isn't long enough, @p is set to NULL, @max_len is set to
 * 0 (making chaining safe), and false is returned.  Otherwise, @len
 * bytes are copied from *@p into @dst, *@p is incremented by @len,
 * @max_len is decremented by @len, and true is returned.
 */
bool pull_bytes(const char **p, size_t *max_len, void *dst, size_t len);

/**
 * pull_u64 - unmarshall a little-endian 64-bit value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 8 bytes and converts from little-endian (if necessary for
 * this platform).  It returns false and sets @p to NULL on error (ie. not
 * enough bytes).
 *
 * Example:
 * struct foo {
 *	uint64_t vu64;
 *	uint32_t vu32;
 *	uint16_t vu16;
 *	uint8_t vu8;
 * };
 *
 * static bool pull_foo(const char **p, size_t *len, struct foo *foo)
 * {
 * 	pull_u64(p, len, &foo->vu64);
 * 	pull_u32(p, len, &foo->vu32);
 * 	pull_u16(p, len, &foo->vu16);
 * 	pull_u8(p, len, &foo->vu8);
 *
 *      // p is set to NULL on error (we could also use return codes)
 *	return p != NULL;
 * }
 */
bool pull_u64(const char **p, size_t *max_len, uint64_t *val);

/**
 * pull_u32 - unmarshall a little-endian 32-bit value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 4 bytes and converts from little-endian (if necessary for
 * this platform).
 */
bool pull_u32(const char **p, size_t *max_len, uint32_t *val);

/**
 * pull_u16 - unmarshall a little-endian 16-bit value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 2 bytes and converts from little-endian (if necessary for
 * this platform).
 */
bool pull_u16(const char **p, size_t *max_len, uint16_t *val);

/**
 * pull_u8 - unmarshall a single byte value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls one byte.
 */
bool pull_u8(const char **p, size_t *max_len, uint8_t *val);
#define pull_uchar pull_u8

/**
 * pull_s64 - unmarshall a little-endian 64-bit signed value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 8 bytes and converts from little-endian (if necessary for
 * this platform).
 */
bool pull_s64(const char **p, size_t *max_len, int64_t *val);

/**
 * pull_s32 - unmarshall a little-endian 32-bit signed value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 4 bytes and converts from little-endian (if necessary for
 * this platform).
 */
bool pull_s32(const char **p, size_t *max_len, int32_t *val);

/**
 * pull_s16 - unmarshall a little-endian 16-bit signed value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls 2 bytes and converts from little-endian (if necessary for
 * this platform).
 */
bool pull_s16(const char **p, size_t *max_len, int16_t *val);

/**
 * pull_s8 - unmarshall a single byte signed value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls one byte.
 */
bool pull_s8(const char **p, size_t *max_len, int8_t *val);

/**
 * pull_char - unmarshall a single char value.
 * @p: pointer to bytes to unmarshal
 * @max_len: pointer to number of bytes in unmarshal buffer
 * @val: the value (or NULL to discard)
 *
 * This pulls one character.
 */
bool pull_char(const char **p, size_t *max_len, char *val);
#endif /* CCAN_PUSHPULL_PULL_H */