buffer.c 4.5 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
W
wujing 已提交
3 4 5 6
 * lcr licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
W
wujing 已提交
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Author: wujing
 * Create: 2018-11-08
 * Description: provide container buffer definition
 ******************************************************************************/
#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>

#include "buffer.h"
#include "log.h"
#include "utils.h"

/* buffer allocate */
Buffer *buffer_alloc(size_t initial_size)
{
    Buffer *buf = NULL;
    char *tmp = NULL;

    if (initial_size == 0) {
        return NULL;
    }

L
LiFeng 已提交
34
    buf = lcr_util_common_calloc_s(sizeof(Buffer));
O
overweight 已提交
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
    if (buf == NULL) {
        return NULL;
    }

    tmp = calloc(1, initial_size);
    if (tmp == NULL) {
        free(buf);
        return NULL;
    }

    buf->contents = tmp;
    buf->bytes_used = 0;
    buf->total_size = initial_size;

    return buf;
}

/* buffer strlen */
static size_t buffer_strlen(const Buffer *buf)
{
    return buf == NULL ? 0 : buf->bytes_used;
}

/* buffer free */
void buffer_free(Buffer *buf)
{
    if (buf == NULL) {
        return;
    }
    free(buf->contents);
    buf->contents = NULL;
    free(buf);
}

/* buffer has space */
static bool buffer_has_space(const Buffer *buf, size_t desired_length)
{
    size_t bytes_remaining = 0;

    if (buf == NULL) {
        return false;
    }
    bytes_remaining = buf->total_size - buf->bytes_used;

    return desired_length <= bytes_remaining;
}

/* buffer grow */
static int buffer_grow(Buffer *buf, size_t minimum_size)
{
    size_t factor = 0;
    size_t new_size = 0;
    char *tmp = NULL;

    if (buf == NULL) {
        return -1;
    }

    factor = buf->total_size;
    if (factor < minimum_size) {
        factor = minimum_size;
    }

    if (factor > SIZE_MAX / 2) {
        return -1;
    }

    new_size = factor * 2;
    if (new_size == 0) {
        return -1;
    }

L
LiFeng 已提交
107
    tmp = lcr_util_common_calloc_s(new_size);
O
overweight 已提交
108 109 110 111 112
    if (tmp == NULL) {
        ERROR("Out of memory");
        return -1;
    }

O
openeuler-iSula 已提交
113
    (void)memcpy(tmp, buf->contents, buf->total_size);
O
overweight 已提交
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

    free(buf->contents);
    buf->contents = tmp;
    buf->total_size = new_size;

    return 0;
}

/* buffer cat */
static void buffer_cat(Buffer *buf, const char *append, size_t length)
{
    size_t i = 0;

    if (buf == NULL) {
        return;
    }

    for (i = 0; i < length; i++) {
        if (append[i] == '\0') {
            break;
        }

        *(buf->contents + buf->bytes_used + i) = append[i];
    }

    buf->bytes_used += i;
    *(buf->contents + buf->bytes_used) = '\0';
}

/* buffer append */
static int buffer_append(Buffer *buf, const char *append, size_t length)
{
    size_t desired_length = 0;

    if (buf == NULL) {
        return -1;
    }

    desired_length = length + 1;
    if (!buffer_has_space(buf, desired_length)) {
        if (buffer_grow(buf, desired_length) != 0) {
            goto error;
        }
    }

    buffer_cat(buf, append, length);

    return 0;
error:
    return -1;
}

/* buffer nappendf */
int buffer_nappendf(Buffer *buf, size_t length, const char *format, ...)
{
    int status = 0;
    size_t printf_length = 0;
    char *tmp = NULL;
    va_list argp;

    if (buf == NULL) {
        return -1;
    }

    if (length > SIZE_MAX / sizeof(char) - 1) {
        return -1;
    }
    printf_length = length + 1;
    tmp = calloc(1, printf_length * sizeof(char));
    if (tmp == NULL) {
        return -1;
    }

    va_start(argp, format);
O
openeuler-iSula 已提交
188
    status = vsprintf(tmp, format, argp);
O
overweight 已提交
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
    va_end(argp);
    if (status < 0) {
        goto error;
    }

    status = buffer_append(buf, tmp, length);
    if (status != 0) {
        goto error;
    }

    free(tmp);
    return 0;
error:
    free(tmp);
    return -1;
}

/* buffer to string */
char *buffer_to_s(const Buffer *buf)
{
    size_t len;
    char *result = NULL;

    if (buf == NULL) {
        return NULL;
    }

    len = buffer_strlen(buf);
    if (len == SIZE_MAX) {
        return NULL;
    }

    result = calloc(1, len + 1);
    if (result == NULL) {
        return NULL;
    }
O
openeuler-iSula 已提交
225
    (void)strncpy(result, buf->contents, len);
O
overweight 已提交
226 227 228

    return result;
}