summaryrefslogtreecommitdiffstats
path: root/src/util/sss_iobuf.c
blob: fc288d2df2bfaaba393dd490d4da8976de804cb5 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <talloc.h>

#include "util/util.h"
#include "util/sss_iobuf.h"

/**
 * @brief The iobuf structure that holds the data, its capacity and
 * a pointer to the data.
 *
 * @see sss_iobuf_init_empty()
 * @see sss_iobuf_init_readonly()
 */
struct sss_iobuf {
    uint8_t *data;          /* Start of the data buffer */

    size_t dp;              /* Data pointer */
    size_t size;            /* Current data buffer size */
    size_t capacity;        /* Maximum capacity */
};

struct sss_iobuf *sss_iobuf_init_empty(TALLOC_CTX *mem_ctx,
                                       size_t size,
                                       size_t capacity)
{
    struct sss_iobuf *iobuf;
    uint8_t *buf;

    iobuf = talloc_zero(mem_ctx, struct sss_iobuf);
    if (iobuf == NULL) {
        return NULL;
    }

    buf = talloc_zero_array(iobuf, uint8_t, size);
    if (buf == NULL) {
        talloc_free(iobuf);
        return NULL;
    }

    if (capacity == 0) {
        capacity = SIZE_MAX / 2;
    }

    iobuf->data = buf;
    iobuf->size = size;
    iobuf->capacity = capacity;
    iobuf->dp = 0;

    return iobuf;
}

struct sss_iobuf *sss_iobuf_init_readonly(TALLOC_CTX *mem_ctx,
                                          const uint8_t *data,
                                          size_t size)
{
    struct sss_iobuf *iobuf;

    iobuf = sss_iobuf_init_empty(mem_ctx, size, size);
    if (iobuf == NULL) {
        return NULL;
    }

    if (data != NULL) {
        memcpy(iobuf->data, data, size);
    }

    return iobuf;
}

size_t sss_iobuf_get_len(struct sss_iobuf *iobuf)
{
    if (iobuf == NULL) {
        return 0;
    }

    return iobuf->dp;
}

size_t sss_iobuf_get_capacity(struct sss_iobuf *iobuf)
{
    if (iobuf == NULL) {
        return 0;
    }

    return iobuf->capacity;
}

size_t sss_iobuf_get_size(struct sss_iobuf *iobuf)
{
    if (iobuf == NULL) {
        return 0;
    }

    return iobuf->size;
}

uint8_t *sss_iobuf_get_data(struct sss_iobuf *iobuf)
{
    if (iobuf == NULL) {
        return NULL;
    }

    return iobuf->data;
}

static size_t iobuf_get_len(struct sss_iobuf *iobuf)
{
    if (iobuf == NULL) {
        return 0;
    }

    return (iobuf->size - iobuf->dp);
}

static errno_t ensure_bytes(struct sss_iobuf *iobuf,
                            size_t nbytes)
{
    size_t wantsize;
    size_t newsize;
    uint8_t *newdata;

    if (iobuf == NULL) {
        return EINVAL;
    }

    wantsize = iobuf->dp + nbytes;
    if (wantsize <= iobuf->size) {
        /* Enough space already */
        return EOK;
    }

    /* Else, try to extend the iobuf */
    if (wantsize > iobuf->capacity) {
        /* We will never grow past capacity */
        return ENOBUFS;
    }

    /* Double the size until we add at least nbytes, but stop if we double past capacity */
    for (newsize = iobuf->size;
         (newsize < wantsize) && (newsize < iobuf->capacity);
         newsize *= 2)
        ;

    if (newsize > iobuf->capacity) {
        newsize = iobuf->capacity;
    }

    newdata = talloc_realloc(iobuf, iobuf->data, uint8_t, newsize);
    if (newdata == NULL) {
        return ENOMEM;
    }

    iobuf->data = newdata;
    iobuf->size = newsize;

    return EOK;
}

static inline uint8_t *iobuf_ptr(struct sss_iobuf *iobuf)
{
    return iobuf->data + iobuf->dp;
}

errno_t sss_iobuf_read(struct sss_iobuf *iobuf,
                       size_t len,
                       uint8_t *_buf,
                       size_t *_read)
{
    size_t remaining;

    if (iobuf == NULL || _buf == NULL) {
        return EINVAL;
    }

    remaining = iobuf_get_len(iobuf);
    if (len > remaining) {
        len = remaining;
    }

    safealign_memcpy(_buf, iobuf_ptr(iobuf), len, &iobuf->dp);
    if (_read != NULL) {
        *_read = len;
    }

    return EOK;
}

errno_t sss_iobuf_read_len(struct sss_iobuf *iobuf,
                           size_t len,
                           uint8_t *_buf)
{
    size_t read;
    errno_t ret;

    ret = sss_iobuf_read(iobuf, len, _buf, &read);
    if (ret != EOK) {
        return ret;
    }

    if (read != len) {
        return ENOBUFS;
    }

    return EOK;
}

errno_t sss_iobuf_write_len(struct sss_iobuf *iobuf,
                            uint8_t *buf,
                            size_t len)
{
    errno_t ret;

    if (iobuf == NULL || buf == NULL) {
        return EINVAL;
    }

    ret = ensure_bytes(iobuf, len);
    if (ret != EOK) {
        return ret;
    }

    safealign_memcpy(iobuf_ptr(iobuf), buf, len, &iobuf->dp);

    return EOK;
}

errno_t sss_iobuf_read_uint32(struct sss_iobuf *iobuf,
                              uint32_t *_val)
{
    SAFEALIGN_COPY_UINT32_CHECK(_val, iobuf_ptr(iobuf),
                                iobuf->capacity, &iobuf->dp);
    return EOK;
}

errno_t sss_iobuf_read_int32(struct sss_iobuf *iobuf,
                             int32_t *_val)
{
    SAFEALIGN_COPY_INT32_CHECK(_val, iobuf_ptr(iobuf),
                               iobuf->capacity, &iobuf->dp);
    return EOK;
}

errno_t sss_iobuf_write_uint32(struct sss_iobuf *iobuf,
                               uint32_t val)
{
    errno_t ret;

    ret = ensure_bytes(iobuf, sizeof(uint32_t));
    if (ret != EOK) {
        return ret;
    }

    SAFEALIGN_SETMEM_UINT32(iobuf_ptr(iobuf), val, &iobuf->dp);
    return EOK;
}

errno_t sss_iobuf_write_int32(struct sss_iobuf *iobuf,
                              int32_t val)
{
    errno_t ret;

    ret = ensure_bytes(iobuf, sizeof(int32_t));
    if (ret != EOK) {
        return ret;
    }

    SAFEALIGN_SETMEM_INT32(iobuf_ptr(iobuf), val, &iobuf->dp);
    return EOK;
}

errno_t sss_iobuf_read_stringz(struct sss_iobuf *iobuf,
                               const char **_out)
{
    uint8_t *end;
    size_t len;

    if (iobuf == NULL) {
        return EINVAL;
    }

    if (_out == NULL) {
        return EINVAL;
    }

    *_out = NULL;

    end = memchr(iobuf_ptr(iobuf), '\0', sss_iobuf_get_size(iobuf));
    if (end == NULL) {
        return EINVAL;
    }

    len = end + 1 - iobuf_ptr(iobuf);
    if (sss_iobuf_get_size(iobuf) < len) {
        return EINVAL;
    }

    *_out = (const char *) iobuf_ptr(iobuf);
    iobuf->dp += len;
    return EOK;
}

errno_t sss_iobuf_write_stringz(struct sss_iobuf *iobuf,
                                const char *str)
{
    if (iobuf == NULL || str == NULL) {
        return EINVAL;
    }

    SAFEALIGN_MEMCPY_CHECK(iobuf_ptr(iobuf),
                           str, strlen(str)+1,
                           sss_iobuf_get_size(iobuf),
                           &iobuf->dp);
    return EOK;
}