summaryrefslogtreecommitdiffstats
path: root/src/util/support/k5buf.c
blob: 778e68b39b938135ee0c739f81e43646bf30953a (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/support/k5buf.c - string buffer functions */

/*
 * Copyright 2008 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 * Can't include krb5.h here, or k5-int.h which includes it, because krb5.h
 * needs to be generated with error tables, after util/et, which builds after
 * this directory.
 */
#include "k5buf-int.h"
#include <assert.h>

/*
 * Structure invariants:
 *
 * buftype is BUFTYPE_FIXED, BUFTYPE_DYNAMIC, or BUFTYPE_ERROR
 * if buftype is not BUFTYPE_ERROR:
 *   space > 0
 *   space <= floor(SIZE_MAX / 2) (to fit within ssize_t)
 *   len < space
 *   data[len] = '\0'
 */

/*
 * Make sure there is room for LEN more characters in BUF, in addition to the
 * null terminator and what's already in there.  Return true on success.  On
 * failure, set the error flag and return false.
 */
static int
ensure_space(struct k5buf *buf, size_t len)
{
    size_t new_space;
    char *new_data;

    if (buf->buftype == BUFTYPE_ERROR)
        return 0;
    if (buf->space - 1 - buf->len >= len) /* Enough room already. */
        return 1;
    if (buf->buftype == BUFTYPE_FIXED) /* Can't resize a fixed buffer. */
        goto error_exit;
    assert(buf->buftype == BUFTYPE_DYNAMIC);
    new_space = buf->space * 2;
    while (new_space <= SPACE_MAX && new_space - buf->len - 1 < len)
        new_space *= 2;
    if (new_space > SPACE_MAX)
        goto error_exit;
    new_data = realloc(buf->data, new_space);
    if (new_data == NULL)
        goto error_exit;
    buf->data = new_data;
    buf->space = new_space;
    return 1;

error_exit:
    if (buf->buftype == BUFTYPE_DYNAMIC) {
        free(buf->data);
        buf->data = NULL;
    }
    buf->buftype = BUFTYPE_ERROR;
    return 0;
}

void
k5_buf_init_fixed(struct k5buf *buf, char *data, size_t space)
{
    assert(space > 0);
    buf->buftype = BUFTYPE_FIXED;
    buf->data = data;
    buf->space = space;
    buf->len = 0;
    buf->data[0] = '\0';
}

void
k5_buf_init_dynamic(struct k5buf *buf)
{
    buf->buftype = BUFTYPE_DYNAMIC;
    buf->space = DYNAMIC_INITIAL_SIZE;
    buf->data = malloc(buf->space);
    if (buf->data == NULL) {
        buf->buftype = BUFTYPE_ERROR;
        return;
    }
    buf->len = 0;
    buf->data[0] = '\0';
}

void
k5_buf_add(struct k5buf *buf, const char *data)
{
    k5_buf_add_len(buf, data, strlen(data));
}

void
k5_buf_add_len(struct k5buf *buf, const char *data, size_t len)
{
    if (!ensure_space(buf, len))
        return;
    if (len > 0)
        memcpy(buf->data + buf->len, data, len);
    buf->len += len;
    buf->data[buf->len] = '\0';
}

void
k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
{
    va_list ap;
    int r;
    size_t remaining;
    char *tmp;

    if (buf->buftype == BUFTYPE_ERROR)
        return;
    remaining = buf->space - buf->len;

    if (buf->buftype == BUFTYPE_FIXED) {
        /* Format the data directly into the fixed buffer. */
        va_start(ap, fmt);
        r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
        va_end(ap);
        if (SNPRINTF_OVERFLOW(r, remaining))
            buf->buftype = BUFTYPE_ERROR;
        else
            buf->len += (unsigned int) r;
        return;
    }

    /* Optimistically format the data directly into the dynamic buffer. */
    assert(buf->buftype == BUFTYPE_DYNAMIC);
    va_start(ap, fmt);
    r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
    va_end(ap);
    if (!SNPRINTF_OVERFLOW(r, remaining)) {
        buf->len += (unsigned int) r;
        return;
    }

    if (r >= 0) {
        /* snprintf correctly told us how much space is required. */
        if (!ensure_space(buf, r))
            return;
        remaining = buf->space - buf->len;
        va_start(ap, fmt);
        r = vsnprintf(buf->data + buf->len, remaining, fmt, ap);
        va_end(ap);
        if (SNPRINTF_OVERFLOW(r, remaining))  /* Shouldn't ever happen. */
            buf->buftype = BUFTYPE_ERROR;
        else
            buf->len += (unsigned int) r;
        return;
    }

    /* It's a pre-C99 snprintf implementation, or something else went wrong.
     * Fall back to asprintf. */
    va_start(ap, fmt);
    r = vasprintf(&tmp, fmt, ap);
    va_end(ap);
    if (r < 0) {
        buf->buftype = BUFTYPE_ERROR;
        return;
    }
    if (ensure_space(buf, r)) {
        /* Copy the temporary string into buf, including terminator. */
        memcpy(buf->data + buf->len, tmp, r + 1);
        buf->len += r;
    }
    free(tmp);
}

void
k5_buf_truncate(struct k5buf *buf, size_t len)
{
    if (buf->buftype == BUFTYPE_ERROR)
        return;
    assert(len <= buf->len);
    buf->len = len;
    buf->data[buf->len] = '\0';
}


char *
k5_buf_data(struct k5buf *buf)
{
    return (buf->buftype == BUFTYPE_ERROR) ? NULL : buf->data;
}

ssize_t
k5_buf_len(struct k5buf *buf)
{
    return (buf->buftype == BUFTYPE_ERROR) ? -1 : (ssize_t) buf->len;
}

void
k5_free_buf(struct k5buf *buf)
{
    if (buf->buftype == BUFTYPE_ERROR)
        return;
    assert(buf->buftype == BUFTYPE_DYNAMIC);
    free(buf->data);
    buf->data = NULL;
    buf->buftype = BUFTYPE_ERROR;
}