buffer.c 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

13
#include <stdio.h>
14 15
#include <stdlib.h>
#include <string.h>
16
#include <stdarg.h>
17

18 19
#include "buffer.h"
#include "refobject.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

struct buffer_tag {
    refobject_base_t __base;
    /* Buffer itself */
    void *buffer;
    /* Length in bytes of buffer */
    size_t length;
    /* Amount of bytes in use of the buffer. This includes offset bytes */
    size_t fill;
    /* Bytes of offset at the start of the buffer */
    size_t offset;
};

static void __free(refobject_t self, void **userdata)
{
    buffer_t *buffer = REFOBJECT_TO_TYPE(self, buffer_t*);

    free(buffer->buffer);
}

40
REFOBJECT_DEFINE_TYPE(buffer_t,
41 42
        REFOBJECT_DEFINE_TYPE_FREE(__free),
        REFOBJECT_DEFINE_TYPE_NEW_NOOP()
43 44
        );

45 46
buffer_t *  buffer_new(ssize_t preallocation, void *userdata, const char *name, refobject_t associated)
{
47
    buffer_t *buffer = refobject_new_ext(buffer_t, userdata, name, associated);
48

49
    if (!buffer)
50 51 52 53 54 55 56 57 58 59
        return NULL;

    if (preallocation > 0)
        buffer_preallocate(buffer, preallocation);

    return buffer;
}

buffer_t *  buffer_new_simple(void)
{
60
    return refobject_new(buffer_t);
61 62 63 64 65 66 67
}

void        buffer_preallocate(buffer_t *buffer, size_t request)
{
    void *n;
    size_t newlen;

68 69
    if (!buffer)
        return;
70

71 72 73 74 75 76 77 78 79 80 81
    /* Remove the offset if it makes sense to do so. */
    if (buffer->offset == buffer->fill) {
        buffer->offset = 0;
        buffer->fill = 0;
    } else if ((2*buffer->offset) < buffer->fill || buffer->offset >= 512 || (buffer->offset > 128 && buffer->offset >= request)) {
        buffer->fill -= buffer->offset;
        memmove(buffer->buffer, buffer->buffer + buffer->offset, buffer->fill);
        buffer->offset = 0;
    }

    if (!request)
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
        return;

    newlen = buffer->fill + request;

    if (buffer->length >= newlen)
        return;

    /* Make sure we at least add 64 bytes and are 64 byte aligned */
    newlen = newlen + 64 - (newlen % 64);

    n = realloc(buffer->buffer, newlen);

    /* Just return if this failed */
    if (!n)
        return;

    buffer->buffer = n;
    buffer->length = newlen;
}

int         buffer_get_data(buffer_t *buffer, const void **data, size_t *length)
{
    if (!buffer)
        return -1;

    if (data) {
        *data = buffer->buffer + buffer->offset;
    }

    if (length) {
        *length = buffer->fill - buffer->offset;
    }

    return 0;
}

int         buffer_get_string(buffer_t *buffer, const char **string)
{
    char *ret;

    if (!buffer || !string)
        return -1;

    /* Ensure we have space for one additional byte ('\0'-termination). */
    if (buffer->length == buffer->fill) {
        buffer_preallocate(buffer, 1);
        if (buffer->length == buffer->fill)
            return -1;
    }

    /* Actually add a '\0'-termination. */
    ret = buffer->buffer;
    ret[buffer->fill] = 0;
135
    *string = ret + buffer->offset;
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

    return 0;
}

int         buffer_set_length(buffer_t *buffer, size_t length)
{
    if (!buffer)
        return -1;

    if (length > (buffer->fill - buffer->offset))
        return -1;

    buffer->fill = length + buffer->offset;

    return 0;
}

int         buffer_shift(buffer_t *buffer, size_t amount)
{
    if (!buffer)
        return -1;

    if (amount > (buffer->fill - buffer->offset))
        return -1;

    buffer->offset += amount;

163 164 165
    /* run cleanup */
    buffer_preallocate(buffer, 0);

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
    return 0;
}

int         buffer_push_data(buffer_t *buffer, const void *data, size_t length)
{
    void *buf;
    int ret;

    if (!buffer)
        return -1;

    if (!length)
        return 0;

    if (!data)
        return -1;

    ret = buffer_zerocopy_push_request(buffer, &buf, length);
    if (ret != 0)
        return ret;

    memcpy(buf, data, length);

    ret = buffer_zerocopy_push_complete(buffer, length);

    return ret;
}

int         buffer_push_string(buffer_t *buffer, const char *string)
{
    if (!buffer || !string)
        return -1;

    return buffer_push_data(buffer, string, strlen(string));
}

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

int         buffer_push_printf(buffer_t *buffer, const char *format, ...)
{
    int ret;
    va_list ap;

    if (!buffer || !format)
        return -1;

    if (!*format)
        return 0;

    va_start(ap, format);
    ret = buffer_push_vprintf(buffer, format, ap);
    va_end(ap);

    return ret;
}

int         buffer_push_vprintf(buffer_t *buffer, const char *format, va_list ap)
{
    void *buf;
    int ret;
    size_t length = 1024;

    if (!buffer || !format)
        return -1;

    if (!*format)
        return 0;

    ret = buffer_zerocopy_push_request(buffer, &buf, length);
    if (ret != 0)
        return ret;

    ret = vsnprintf(buf, length, format, ap);
    if (ret >= 0 && (size_t)ret < length) {
        return buffer_zerocopy_push_complete(buffer, ret);
    } else if (ret < 0) {
        /* This vsnprintf() likely does not follow POSIX.
         * We don't know what length we need to asume. So asume a big one and hope for the best. */
        length = 8192;
    } else {
        /* Reallocate the buffer to the size reported plus one for '\0'-termination */
        length = ret + 1;
    }

    /* We have not written any data yet. */
    ret = buffer_zerocopy_push_complete(buffer, 0);
    if (ret != 0)
        return ret;

    /* Now let's try again. */
    ret = buffer_zerocopy_push_request(buffer, &buf, length);
    if (ret != 0)
        return ret;

    ret = vsnprintf(buf, length, format, ap);
    if (ret < 0 || (size_t)ret >= length) {
        /* This still didn't work. Giving up. */
        buffer_zerocopy_push_complete(buffer, 0);
        return -1;
    }

    return buffer_zerocopy_push_complete(buffer, ret);
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
int         buffer_push_buffer(buffer_t *buffer, buffer_t *source)
{
    const void *data;
    size_t length;
    int ret;

    if (!buffer || !source)
        return -1;

    ret = buffer_get_data(source, &data, &length);
    if (ret != 0)
        return ret;

    return buffer_push_data(buffer, data, length);
}

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
int         buffer_zerocopy_push_request(buffer_t *buffer, void **data, size_t request)
{
    if (!buffer || !data)
        return -1;

    buffer_preallocate(buffer, request);

    if (request > (buffer->length - buffer->fill))
        return -1;

    *data = buffer->buffer + buffer->fill;

    return 0;
}

int         buffer_zerocopy_push_complete(buffer_t *buffer, size_t done)
{
    if (!buffer)
        return -1;

    if (done > (buffer->length - buffer->fill))
        return -1;

    buffer->fill += done;

    return 0;
}