summaryrefslogtreecommitdiff
path: root/usr/local_strings.c
blob: 2a40c2a80fd5df0ec35f448cab67ec2df07894e9 (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
/*
 * Copyright (C) 2002 Cisco Systems, Inc.
 * maintained by linux-iscsi-devel@lists.sourceforge.net
 *
 * 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 2 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.
 *
 * See the file COPYING included with this distribution for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/param.h>

#include "local_strings.h"
#include "log.h"

int str_init_buffer(struct str_buffer *s, size_t initial_allocation)
{
	if (s) {
		memset(s, 0, sizeof (*s));
		s->buffer = NULL;
		if (initial_allocation) {
			s->buffer = malloc(initial_allocation);
			if (s->buffer) {
				s->allocated_length = initial_allocation;
				memset(s->buffer, 0, initial_allocation);
			}
		}
		s->data_length = 0;
		return 1;
	}

	return 0;
}

struct str_buffer *str_alloc_buffer(size_t initial_allocation)
{
	struct str_buffer *s = calloc(1, sizeof (*s));

	if (s)
		str_init_buffer(s, initial_allocation);

	return s;
}

void str_free_buffer(struct str_buffer *s)
{
	if (s) {
		if (s->buffer) {
			free(s->buffer);
			s->buffer = NULL;
		}
		s->allocated_length = 0;
		s->data_length = 0;
	}
}

int str_enlarge_data(struct str_buffer *s, int length)
{
	void *new_buf;

	if (s) {
		s->data_length += length;
		if (s->data_length > s->allocated_length) {
			log_debug(7, "enlarge buffer from %zu to %zu",
				  s->allocated_length, s->data_length);
			new_buf = realloc(s->buffer, s->data_length);
			if (!new_buf) {
				/* too big */
				log_error("enlarged buffer %p to %d data "
					  "bytes, with only %d bytes of buffer "
					  "space", s, (int)s->data_length,
					   (int)s->allocated_length);
				return ENOMEM;
			}
			s->buffer = new_buf;
			memset(s->buffer + s->allocated_length, 0,
			       s->data_length - s->allocated_length);
			s->allocated_length = s->data_length;
		}
	}

	return 0;
}

void str_remove_initial(struct str_buffer *s, int length)
{
	char *remaining;
	int amount;

	if (s && length) {
		remaining = s->buffer + length;
		amount = s->data_length - length;

		if (amount < 0)
			amount = 0;
		if (amount)
			memmove(s->buffer, remaining, amount);
		s->data_length = amount;
		s->buffer[amount] = '\0';
	}
}

/* truncate the data length down */
void str_truncate_buffer(struct str_buffer *s, size_t length)
{
	if (s) {
		if (!s->data_length)
			return;
		if (length <= s->data_length) {
			s->data_length = length;
			s->buffer[s->data_length] = '\0';
		} else if (length <= s->allocated_length) {
			/* clear the data, and declare the
			 * data length to be larger
			 */
			memset(s->buffer + s->data_length, 0,
			       length - s->data_length);
			s->data_length = length;
		} else {
			log_error(
			       "couldn't truncate data buffer to length %d, "
			       "only allocated %d",
			       (int)length, (int)s->allocated_length);
		}
	}
}

char *str_buffer_data(struct str_buffer *s)
{
	if (s)
		return s->buffer;
	else
		return NULL;
}

size_t str_data_length(struct str_buffer * s)
{
	if (s)
		return s->data_length;
	else
		return 0;
}

size_t str_unused_length(struct str_buffer * s)
{
	if (s)
		return s->allocated_length - s->data_length;
	else
		return 0;
}