summaryrefslogtreecommitdiffstats
path: root/common/basicobjects/simplebuffer.c
blob: b47d6b69c6b226bbbdccbecce262009760eb65c9 (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
/*
    Simple buffer

    Basic buffer manipulation routines. Taken from ELAPI code.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009 - 2010

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#define _GNU_SOURCE
#include <errno.h>      /* for errors */
#include <stdlib.h>     /* for free() */
#include <unistd.h>     /* for write() */
#include <string.h>     /* for memcpy() */

#include "simplebuffer.h"
#include "trace.h"
#include "config.h"

/* End line string */
#define ENDLNSTR "\n"

/* Function to free buffer */
void simplebuffer_free(struct simplebuffer *data)
{
    TRACE_FLOW_ENTRY();

    if (data) {
        free(data->buffer);
        free(data);
    }

    TRACE_FLOW_EXIT();
}

/* Allocate buffer structure */
int simplebuffer_alloc(struct simplebuffer **data)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    if (!data) {
        TRACE_ERROR_STRING("Invalid argument", "");
        error = EINVAL;
    }
    else {
        *data = (struct simplebuffer *)calloc(1,
                                              sizeof(struct simplebuffer));
        if (*data == NULL) {
            TRACE_ERROR_STRING("Failed to allocate memory", "");
            error = ENOMEM;
        }
        else error = EOK;
    }

    TRACE_FLOW_RETURN(error);
    return error;
}


/* Grow buffer */
int simplebuffer_grow(struct simplebuffer *data,
                      uint32_t len,
                      uint32_t block)
{
    int error = EOK;
    unsigned char *newbuf = NULL;

    TRACE_FLOW_ENTRY();

    TRACE_INFO_NUMBER("Current length: ", data->length);
    TRACE_INFO_NUMBER("Current size: ", data->size);
    TRACE_INFO_NUMBER("Length to have: ", len);
    TRACE_INFO_NUMBER("Increment length: ", block);

    /* Grow buffer if needed */
    while (data->length + len >= data->size) {
        newbuf = realloc(data->buffer, data->size + block);
        if (newbuf == NULL) {
            TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM);
            return ENOMEM;
        }
        data->buffer = newbuf;
        data->size += block;
        TRACE_INFO_NUMBER("New size: ", data->size);
    }

    TRACE_INFO_NUMBER("Final size: ", data->size);
    TRACE_FLOW_RETURN(error);
    return error;
}

/* Function to add raw data to the end of the buffer.
 * Terminating 0 is not counted in length but appended
 * automatically.
 */
int simplebuffer_add_raw(struct simplebuffer *data,
                         void *data_in,
                         uint32_t len,
                         uint32_t block)
{

    int error = EOK;
    uint32_t size;

    TRACE_FLOW_ENTRY();

    size = len + 1;
    error = simplebuffer_grow(data, size,
                             ((block > size) ? block : size));
    if (error) {
        TRACE_ERROR_NUMBER("Failed to grow buffer.", error);
        return error;
    }

    memcpy(data->buffer + data->length, data_in, len);
    data->length += len;
    data->buffer[data->length] = '\0';

    TRACE_FLOW_EXIT();
    return error;
}

/* Function to add string to the end of the buffer. */
int simplebuffer_add_str(struct simplebuffer *data,
                         const char *str,
                         uint32_t len,
                         uint32_t block)
{

    int error = EOK;
    uint32_t size;

    TRACE_FLOW_ENTRY();

    size = len + 1;
    error = simplebuffer_grow(data, size,
                             ((block > size) ? block : size));
    if (error) {
        TRACE_ERROR_NUMBER("Failed to grow buffer.", error);
        return error;
    }

    memcpy(data->buffer + data->length, str, len);
    data->length += len;
    data->buffer[data->length] = '\0';

    TRACE_FLOW_EXIT();
    return error;
}

/* Finction to add CR to the buffer */
int simplebuffer_add_cr(struct simplebuffer *data)
{
    int error = EOK;
    char cr[] = ENDLNSTR;

    TRACE_FLOW_ENTRY();

    error = simplebuffer_add_raw(data,
                                 (void *)cr,
                                 sizeof(ENDLNSTR) - 1,
                                 sizeof(ENDLNSTR));

    TRACE_FLOW_RETURN(error);
    return error;
}


/* Function to write data synchroniusly */
int simplebuffer_write(int fd, struct simplebuffer *data, uint32_t *left)
{
    size_t res;
    int error;

    TRACE_FLOW_ENTRY();

    /* Write given number of bytes to the socket */
    res = write(fd,
                data->buffer + (data->length - *left),
                (size_t)(*left));

    if (res == -1) {
        error = errno;
        TRACE_ERROR_NUMBER("Write failed.", error);
        return error;
    }

    (*left) -= res;

    TRACE_FLOW_EXIT();
    return EOK;
}

/* Get buffer */
const unsigned char *simplebuffer_get_buf(struct simplebuffer *data)
{
    return data->buffer;
}

/* Get length */
uint32_t simplebuffer_get_len(struct simplebuffer *data)
{
    return data->length;
}