xref: /freebsd/contrib/elftoolchain/libelftc/libelftc_vstr.c (revision c2bffd0a9793be8a23ab719557756b2a2ce2c3b5)
1a85fe12eSEd Maste /*-
2a85fe12eSEd Maste  * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com>
3a85fe12eSEd Maste  * All rights reserved.
4a85fe12eSEd Maste  *
5a85fe12eSEd Maste  * Redistribution and use in source and binary forms, with or without
6a85fe12eSEd Maste  * modification, are permitted provided that the following conditions
7a85fe12eSEd Maste  * are met:
8a85fe12eSEd Maste  * 1. Redistributions of source code must retain the above copyright
9a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer
10a85fe12eSEd Maste  *    in this position and unchanged.
11a85fe12eSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
12a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer in the
13a85fe12eSEd Maste  *    documentation and/or other materials provided with the distribution.
14a85fe12eSEd Maste  *
15a85fe12eSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16a85fe12eSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a85fe12eSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a85fe12eSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a85fe12eSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a85fe12eSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a85fe12eSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a85fe12eSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a85fe12eSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a85fe12eSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a85fe12eSEd Maste  */
26a85fe12eSEd Maste 
27a85fe12eSEd Maste #include <sys/types.h>
28a85fe12eSEd Maste #include <assert.h>
29a85fe12eSEd Maste #include <libelftc.h>
30a85fe12eSEd Maste #include <stdio.h>
31a85fe12eSEd Maste #include <stdlib.h>
32a85fe12eSEd Maste #include <string.h>
33a85fe12eSEd Maste 
34a85fe12eSEd Maste #include "_libelftc.h"
35a85fe12eSEd Maste 
3618f4c9dbSEd Maste ELFTC_VCSID("$Id: libelftc_vstr.c 3531 2017-06-05 05:08:43Z kaiwang27 $");
37a85fe12eSEd Maste 
38a85fe12eSEd Maste /**
39a85fe12eSEd Maste  * @file vector_str.c
40a85fe12eSEd Maste  * @brief Dynamic vector data for string implementation.
41a85fe12eSEd Maste  *
42a85fe12eSEd Maste  * Resemble to std::vector<std::string> in C++.
43a85fe12eSEd Maste  */
44a85fe12eSEd Maste 
45a85fe12eSEd Maste static size_t	get_strlen_sum(const struct vector_str *v);
46a85fe12eSEd Maste static bool	vector_str_grow(struct vector_str *v);
47a85fe12eSEd Maste 
48a85fe12eSEd Maste static size_t
get_strlen_sum(const struct vector_str * v)49a85fe12eSEd Maste get_strlen_sum(const struct vector_str *v)
50a85fe12eSEd Maste {
51a85fe12eSEd Maste 	size_t i, len = 0;
52a85fe12eSEd Maste 
53a85fe12eSEd Maste 	if (v == NULL)
54a85fe12eSEd Maste 		return (0);
55a85fe12eSEd Maste 
56a85fe12eSEd Maste 	assert(v->size > 0);
57a85fe12eSEd Maste 
58a85fe12eSEd Maste 	for (i = 0; i < v->size; ++i)
59a85fe12eSEd Maste 		len += strlen(v->container[i]);
60a85fe12eSEd Maste 
61a85fe12eSEd Maste 	return (len);
62a85fe12eSEd Maste }
63a85fe12eSEd Maste 
64a85fe12eSEd Maste /**
65a85fe12eSEd Maste  * @brief Deallocate resource in vector_str.
66a85fe12eSEd Maste  */
67a85fe12eSEd Maste void
vector_str_dest(struct vector_str * v)68a85fe12eSEd Maste vector_str_dest(struct vector_str *v)
69a85fe12eSEd Maste {
70a85fe12eSEd Maste 	size_t i;
71a85fe12eSEd Maste 
72a85fe12eSEd Maste 	if (v == NULL)
73a85fe12eSEd Maste 		return;
74a85fe12eSEd Maste 
75a85fe12eSEd Maste 	for (i = 0; i < v->size; ++i)
76a85fe12eSEd Maste 		free(v->container[i]);
77a85fe12eSEd Maste 
78a85fe12eSEd Maste 	free(v->container);
79a85fe12eSEd Maste }
80a85fe12eSEd Maste 
81a85fe12eSEd Maste /**
82a85fe12eSEd Maste  * @brief Find string in vector_str.
83a85fe12eSEd Maste  * @param v Destination vector.
84a85fe12eSEd Maste  * @param o String to find.
85a85fe12eSEd Maste  * @param l Length of the string.
86a85fe12eSEd Maste  * @return -1 at failed, 0 at not found, 1 at found.
87a85fe12eSEd Maste  */
88a85fe12eSEd Maste int
vector_str_find(const struct vector_str * v,const char * o,size_t l)89a85fe12eSEd Maste vector_str_find(const struct vector_str *v, const char *o, size_t l)
90a85fe12eSEd Maste {
91a85fe12eSEd Maste 	size_t i;
92a85fe12eSEd Maste 
93a85fe12eSEd Maste 	if (v == NULL || o == NULL)
94a85fe12eSEd Maste 		return (-1);
95a85fe12eSEd Maste 
96a85fe12eSEd Maste 	for (i = 0; i < v->size; ++i)
97a85fe12eSEd Maste 		if (strncmp(v->container[i], o, l) == 0)
98a85fe12eSEd Maste 			return (1);
99a85fe12eSEd Maste 
100a85fe12eSEd Maste 	return (0);
101a85fe12eSEd Maste }
102a85fe12eSEd Maste 
103a85fe12eSEd Maste /**
104a85fe12eSEd Maste  * @brief Get new allocated flat string from vector.
105a85fe12eSEd Maste  *
106a85fe12eSEd Maste  * If l is not NULL, return length of the string.
107a85fe12eSEd Maste  * @param v Destination vector.
108a85fe12eSEd Maste  * @param l Length of the string.
109a85fe12eSEd Maste  * @return NULL at failed or NUL terminated new allocated string.
110a85fe12eSEd Maste  */
111a85fe12eSEd Maste char *
vector_str_get_flat(const struct vector_str * v,size_t * l)112a85fe12eSEd Maste vector_str_get_flat(const struct vector_str *v, size_t *l)
113a85fe12eSEd Maste {
114a85fe12eSEd Maste 	ssize_t elem_pos, elem_size, rtn_size;
115a85fe12eSEd Maste 	size_t i;
116a85fe12eSEd Maste 	char *rtn;
117a85fe12eSEd Maste 
118a85fe12eSEd Maste 	if (v == NULL || v->size == 0)
119a85fe12eSEd Maste 		return (NULL);
120a85fe12eSEd Maste 
121a85fe12eSEd Maste 	if ((rtn_size = get_strlen_sum(v)) == 0)
122a85fe12eSEd Maste 		return (NULL);
123a85fe12eSEd Maste 
124a85fe12eSEd Maste 	if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL)
125a85fe12eSEd Maste 		return (NULL);
126a85fe12eSEd Maste 
127a85fe12eSEd Maste 	elem_pos = 0;
128a85fe12eSEd Maste 	for (i = 0; i < v->size; ++i) {
129a85fe12eSEd Maste 		elem_size = strlen(v->container[i]);
130a85fe12eSEd Maste 
131a85fe12eSEd Maste 		memcpy(rtn + elem_pos, v->container[i], elem_size);
132a85fe12eSEd Maste 
133a85fe12eSEd Maste 		elem_pos += elem_size;
134a85fe12eSEd Maste 	}
135a85fe12eSEd Maste 
136a85fe12eSEd Maste 	rtn[rtn_size] = '\0';
137a85fe12eSEd Maste 
138a85fe12eSEd Maste 	if (l != NULL)
139a85fe12eSEd Maste 		*l = rtn_size;
140a85fe12eSEd Maste 
141a85fe12eSEd Maste 	return (rtn);
142a85fe12eSEd Maste }
143a85fe12eSEd Maste 
144a85fe12eSEd Maste static bool
vector_str_grow(struct vector_str * v)145a85fe12eSEd Maste vector_str_grow(struct vector_str *v)
146a85fe12eSEd Maste {
147a85fe12eSEd Maste 	size_t i, tmp_cap;
148a85fe12eSEd Maste 	char **tmp_ctn;
149a85fe12eSEd Maste 
150a85fe12eSEd Maste 	if (v == NULL)
151a85fe12eSEd Maste 		return (false);
152a85fe12eSEd Maste 
153a85fe12eSEd Maste 	assert(v->capacity > 0);
154a85fe12eSEd Maste 
155*c2bffd0aSDimitry Andric 	tmp_cap = BUFFER_GROW(v->capacity);
156a85fe12eSEd Maste 
157a85fe12eSEd Maste 	assert(tmp_cap > v->capacity);
158a85fe12eSEd Maste 
159a85fe12eSEd Maste 	if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
160a85fe12eSEd Maste 		return (false);
161a85fe12eSEd Maste 
162a85fe12eSEd Maste 	for (i = 0; i < v->size; ++i)
163a85fe12eSEd Maste 		tmp_ctn[i] = v->container[i];
164a85fe12eSEd Maste 
165a85fe12eSEd Maste 	free(v->container);
166a85fe12eSEd Maste 
167a85fe12eSEd Maste 	v->container = tmp_ctn;
168a85fe12eSEd Maste 	v->capacity = tmp_cap;
169a85fe12eSEd Maste 
170a85fe12eSEd Maste 	return (true);
171a85fe12eSEd Maste }
172a85fe12eSEd Maste 
173a85fe12eSEd Maste /**
174a85fe12eSEd Maste  * @brief Initialize vector_str.
175a85fe12eSEd Maste  * @return false at failed, true at success.
176a85fe12eSEd Maste  */
177a85fe12eSEd Maste bool
vector_str_init(struct vector_str * v)178a85fe12eSEd Maste vector_str_init(struct vector_str *v)
179a85fe12eSEd Maste {
180a85fe12eSEd Maste 
181a85fe12eSEd Maste 	if (v == NULL)
182a85fe12eSEd Maste 		return (false);
183a85fe12eSEd Maste 
184a85fe12eSEd Maste 	v->size = 0;
185a85fe12eSEd Maste 	v->capacity = VECTOR_DEF_CAPACITY;
186a85fe12eSEd Maste 
187a85fe12eSEd Maste 	assert(v->capacity > 0);
188a85fe12eSEd Maste 
189a85fe12eSEd Maste 	if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL)
190a85fe12eSEd Maste 		return (false);
191a85fe12eSEd Maste 
192a85fe12eSEd Maste 	assert(v->container != NULL);
193a85fe12eSEd Maste 
194a85fe12eSEd Maste 	return (true);
195a85fe12eSEd Maste }
196a85fe12eSEd Maste 
197a85fe12eSEd Maste /**
198a85fe12eSEd Maste  * @brief Remove last element in vector_str.
199a85fe12eSEd Maste  * @return false at failed, true at success.
200a85fe12eSEd Maste  */
201a85fe12eSEd Maste bool
vector_str_pop(struct vector_str * v)202a85fe12eSEd Maste vector_str_pop(struct vector_str *v)
203a85fe12eSEd Maste {
204a85fe12eSEd Maste 
205a85fe12eSEd Maste 	if (v == NULL)
206a85fe12eSEd Maste 		return (false);
207a85fe12eSEd Maste 
208a85fe12eSEd Maste 	if (v->size == 0)
209a85fe12eSEd Maste 		return (true);
210a85fe12eSEd Maste 
211a85fe12eSEd Maste 	--v->size;
212a85fe12eSEd Maste 
213a85fe12eSEd Maste 	free(v->container[v->size]);
214a85fe12eSEd Maste 	v->container[v->size] = NULL;
215a85fe12eSEd Maste 
216a85fe12eSEd Maste 	return (true);
217a85fe12eSEd Maste }
218a85fe12eSEd Maste 
219a85fe12eSEd Maste /**
220a85fe12eSEd Maste  * @brief Push back string to vector.
221a85fe12eSEd Maste  * @return false at failed, true at success.
222a85fe12eSEd Maste  */
223a85fe12eSEd Maste bool
vector_str_push(struct vector_str * v,const char * str,size_t len)224a85fe12eSEd Maste vector_str_push(struct vector_str *v, const char *str, size_t len)
225a85fe12eSEd Maste {
226a85fe12eSEd Maste 
227a85fe12eSEd Maste 	if (v == NULL || str == NULL)
228a85fe12eSEd Maste 		return (false);
229a85fe12eSEd Maste 
230a85fe12eSEd Maste 	if (v->size == v->capacity && vector_str_grow(v) == false)
231a85fe12eSEd Maste 		return (false);
232a85fe12eSEd Maste 
233a85fe12eSEd Maste 	if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL)
234a85fe12eSEd Maste 		return (false);
235a85fe12eSEd Maste 
236a85fe12eSEd Maste 	snprintf(v->container[v->size], len + 1, "%s", str);
237a85fe12eSEd Maste 
238a85fe12eSEd Maste 	++v->size;
239a85fe12eSEd Maste 
240a85fe12eSEd Maste 	return (true);
241a85fe12eSEd Maste }
242a85fe12eSEd Maste 
243a85fe12eSEd Maste /**
244a85fe12eSEd Maste  * @brief Push front org vector to det vector.
245a85fe12eSEd Maste  * @return false at failed, true at success.
246a85fe12eSEd Maste  */
247a85fe12eSEd Maste bool
vector_str_push_vector_head(struct vector_str * dst,struct vector_str * org)248a85fe12eSEd Maste vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org)
249a85fe12eSEd Maste {
250a85fe12eSEd Maste 	size_t i, j, tmp_cap;
251a85fe12eSEd Maste 	char **tmp_ctn;
252a85fe12eSEd Maste 
253a85fe12eSEd Maste 	if (dst == NULL || org == NULL)
254a85fe12eSEd Maste 		return (false);
255a85fe12eSEd Maste 
256*c2bffd0aSDimitry Andric 	tmp_cap = BUFFER_GROW(dst->size + org->size);
257a85fe12eSEd Maste 
258a85fe12eSEd Maste 	if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
259a85fe12eSEd Maste 		return (false);
260a85fe12eSEd Maste 
261a85fe12eSEd Maste 	for (i = 0; i < org->size; ++i)
262a85fe12eSEd Maste 		if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) {
263a85fe12eSEd Maste 			for (j = 0; j < i; ++j)
264a85fe12eSEd Maste 				free(tmp_ctn[j]);
265a85fe12eSEd Maste 
266a85fe12eSEd Maste 			free(tmp_ctn);
267a85fe12eSEd Maste 
268a85fe12eSEd Maste 			return (false);
269a85fe12eSEd Maste 		}
270a85fe12eSEd Maste 
271a85fe12eSEd Maste 	for (i = 0; i < dst->size; ++i)
272a85fe12eSEd Maste 		tmp_ctn[i + org->size] = dst->container[i];
273a85fe12eSEd Maste 
274a85fe12eSEd Maste 	free(dst->container);
275a85fe12eSEd Maste 
276a85fe12eSEd Maste 	dst->container = tmp_ctn;
277a85fe12eSEd Maste 	dst->capacity = tmp_cap;
278a85fe12eSEd Maste 	dst->size += org->size;
279a85fe12eSEd Maste 
280a85fe12eSEd Maste 	return (true);
281a85fe12eSEd Maste }
282a85fe12eSEd Maste 
283a85fe12eSEd Maste /**
28418f4c9dbSEd Maste  * @brief Push org vector to the tail of det vector.
28518f4c9dbSEd Maste  * @return false at failed, true at success.
28618f4c9dbSEd Maste  */
28718f4c9dbSEd Maste bool
vector_str_push_vector(struct vector_str * dst,struct vector_str * org)28818f4c9dbSEd Maste vector_str_push_vector(struct vector_str *dst, struct vector_str *org)
28918f4c9dbSEd Maste {
29018f4c9dbSEd Maste 	size_t i, j, tmp_cap;
29118f4c9dbSEd Maste 	char **tmp_ctn;
29218f4c9dbSEd Maste 
29318f4c9dbSEd Maste 	if (dst == NULL || org == NULL)
29418f4c9dbSEd Maste 		return (false);
29518f4c9dbSEd Maste 
296*c2bffd0aSDimitry Andric 	tmp_cap = BUFFER_GROW(dst->size + org->size);
29718f4c9dbSEd Maste 
29818f4c9dbSEd Maste 	if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
29918f4c9dbSEd Maste 		return (false);
30018f4c9dbSEd Maste 
30118f4c9dbSEd Maste 	for (i = 0; i < dst->size; ++i)
30218f4c9dbSEd Maste 		tmp_ctn[i] = dst->container[i];
30318f4c9dbSEd Maste 
30418f4c9dbSEd Maste 	for (i = 0; i < org->size; ++i)
30518f4c9dbSEd Maste 		if ((tmp_ctn[i + dst->size] = strdup(org->container[i])) ==
30618f4c9dbSEd Maste 		    NULL) {
30718f4c9dbSEd Maste 			for (j = 0; j < i + dst->size; ++j)
30818f4c9dbSEd Maste 				free(tmp_ctn[j]);
30918f4c9dbSEd Maste 
31018f4c9dbSEd Maste 			free(tmp_ctn);
31118f4c9dbSEd Maste 
31218f4c9dbSEd Maste 			return (false);
31318f4c9dbSEd Maste 		}
31418f4c9dbSEd Maste 
31518f4c9dbSEd Maste 	free(dst->container);
31618f4c9dbSEd Maste 
31718f4c9dbSEd Maste 	dst->container = tmp_ctn;
31818f4c9dbSEd Maste 	dst->capacity = tmp_cap;
31918f4c9dbSEd Maste 	dst->size += org->size;
32018f4c9dbSEd Maste 
32118f4c9dbSEd Maste 	return (true);
32218f4c9dbSEd Maste }
32318f4c9dbSEd Maste 
32418f4c9dbSEd Maste /**
325a85fe12eSEd Maste  * @brief Get new allocated flat string from vector between begin and end.
326a85fe12eSEd Maste  *
327a85fe12eSEd Maste  * If r_len is not NULL, string length will be returned.
328a85fe12eSEd Maste  * @return NULL at failed or NUL terminated new allocated string.
329a85fe12eSEd Maste  */
330a85fe12eSEd Maste char *
vector_str_substr(const struct vector_str * v,size_t begin,size_t end,size_t * r_len)331a85fe12eSEd Maste vector_str_substr(const struct vector_str *v, size_t begin, size_t end,
332a85fe12eSEd Maste     size_t *r_len)
333a85fe12eSEd Maste {
334a85fe12eSEd Maste 	size_t cur, i, len;
335a85fe12eSEd Maste 	char *rtn;
336a85fe12eSEd Maste 
337a85fe12eSEd Maste 	if (v == NULL || begin > end)
338a85fe12eSEd Maste 		return (NULL);
339a85fe12eSEd Maste 
340a85fe12eSEd Maste 	len = 0;
341a85fe12eSEd Maste 	for (i = begin; i < end + 1; ++i)
342a85fe12eSEd Maste 		len += strlen(v->container[i]);
343a85fe12eSEd Maste 
344a85fe12eSEd Maste 	if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL)
345a85fe12eSEd Maste 		return (NULL);
346a85fe12eSEd Maste 
347a85fe12eSEd Maste 	if (r_len != NULL)
348a85fe12eSEd Maste 		*r_len = len;
349a85fe12eSEd Maste 
350a85fe12eSEd Maste 	cur = 0;
351a85fe12eSEd Maste 	for (i = begin; i < end + 1; ++i) {
352a85fe12eSEd Maste 		len = strlen(v->container[i]);
353a85fe12eSEd Maste 		memcpy(rtn + cur, v->container[i], len);
354a85fe12eSEd Maste 		cur += len;
355a85fe12eSEd Maste 	}
356a85fe12eSEd Maste 	rtn[cur] = '\0';
357a85fe12eSEd Maste 
358a85fe12eSEd Maste 	return (rtn);
359a85fe12eSEd Maste }
360