Debian Codegraph @

src/common/oscap_buffer.c

raw symbols SCIP
1 /*
2 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Jan Černý <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include "oscap_buffer.h"
31
32 #define INITIAL_CAPACITY 64
33
34 /**
35 * Buffer with unlimited length
36 * contains:
37 * - pointer to data,
38 * - actual length of string,
39 * - capacity of allocated memory
40 */
41 struct oscap_buffer {
42 char *data;
43 size_t length;
44 size_t capacity;
45 };
46
47 struct oscap_buffer *oscap_buffer_new()
48 {
49 struct oscap_buffer *s;
50 s = malloc(sizeof(struct oscap_buffer));
51 s->data = malloc(INITIAL_CAPACITY);
52 s->data[0] = '\0';
53 s->length = 0;
54 s->capacity = INITIAL_CAPACITY;
55 return s;
56 }
57
58 void oscap_buffer_clear(struct oscap_buffer *s)
59 {
60 s->data[0] = '\0';
61 s->length = 0;
62 }
63
64 void oscap_buffer_free(struct oscap_buffer *s)
65 {
66 if (s != NULL) {
67 free(s->data);
68 free(s);
69 }
70 }
71
72 char* oscap_buffer_bequeath(struct oscap_buffer *s){
73 char* str = s->data;
74 free(s);
75 return str;
76 }
77
78 void oscap_buffer_append_binary_data(struct oscap_buffer *s, const char *data, const size_t append_length)
79 {
80 if (s == NULL || data == NULL)
81 return;
82 if (s->length + append_length + 1 > s->capacity) {
83 /* Aligning allocated memory to multiples of INITIAL_CAPACITY.
84 * We pass to realloc the nearest greater muliple of INITIAL_CAPACITY
85 * rather than only needed capacity in order to not fragment
86 * the memory.
87 */
88 size_t current_capacity = s->capacity;
89 s->capacity = ((s->capacity + append_length - 1) / INITIAL_CAPACITY + 1) * INITIAL_CAPACITY;
90 void *new_data = realloc(s->data, s->capacity);
91 if (new_data == NULL) {
92 s->capacity = current_capacity;
93 return;
94 }
95 s->data = new_data;
96 }
97
98 memcpy(s->data + s->length, data, append_length);
99 s->length += append_length;
100 s->data[s->length] = '\0';
101 }
102
103 void oscap_buffer_append_string(struct oscap_buffer *s, const char *t)
104 {
105 if (t == NULL)
106 return;
107 size_t append_length = strlen(t);
108 oscap_buffer_append_binary_data(s, t, append_length);
109 }
110
111 char *oscap_buffer_get_raw(const struct oscap_buffer *s)
112 {
113 return s->data;
114 }
115
116 size_t oscap_buffer_get_length(const struct oscap_buffer *s){
117 return s->length;
118 }
119
120