forked from OpenSCAP/openscap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscap_buffer.c
120 lines (104 loc) · 2.88 KB
/
oscap_buffer.c
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
/*
* Copyright 2015 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Jan Černý <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "oscap_buffer.h"
#define INITIAL_CAPACITY 64
/**
* Buffer with unlimited length
* contains:
* - pointer to data,
* - actual length of string,
* - capacity of allocated memory
*/
struct oscap_buffer {
char *data;
size_t length;
size_t capacity;
};
struct oscap_buffer *oscap_buffer_new()
{
struct oscap_buffer *s;
s = malloc(sizeof(struct oscap_buffer));
s->data = malloc(INITIAL_CAPACITY);
s->data[0] = '\0';
s->length = 0;
s->capacity = INITIAL_CAPACITY;
return s;
}
void oscap_buffer_clear(struct oscap_buffer *s)
{
s->data[0] = '\0';
s->length = 0;
}
void oscap_buffer_free(struct oscap_buffer *s)
{
if (s != NULL) {
free(s->data);
free(s);
}
}
char* oscap_buffer_bequeath(struct oscap_buffer *s){
char* str = s->data;
free(s);
return str;
}
void oscap_buffer_append_binary_data(struct oscap_buffer *s, const char *data, const size_t append_length)
{
if (s == NULL || data == NULL)
return;
if (s->length + append_length + 1 > s->capacity) {
/* Aligning allocated memory to multiples of INITIAL_CAPACITY.
* We pass to realloc the nearest greater muliple of INITIAL_CAPACITY
* rather than only needed capacity in order to not fragment
* the memory.
*/
size_t current_capacity = s->capacity;
s->capacity = ((s->capacity + append_length - 1) / INITIAL_CAPACITY + 1) * INITIAL_CAPACITY;
void *new_data = realloc(s->data, s->capacity);
if (new_data == NULL) {
s->capacity = current_capacity;
return;
}
s->data = new_data;
}
memcpy(s->data + s->length, data, append_length);
s->length += append_length;
s->data[s->length] = '\0';
}
void oscap_buffer_append_string(struct oscap_buffer *s, const char *t)
{
if (t == NULL)
return;
size_t append_length = strlen(t);
oscap_buffer_append_binary_data(s, t, append_length);
}
char *oscap_buffer_get_raw(const struct oscap_buffer *s)
{
return s->data;
}
size_t oscap_buffer_get_length(const struct oscap_buffer *s){
return s->length;
}