aboutsummaryrefslogtreecommitdiff
path: root/lib/util/array.c
blob: 40bac50d230fe324755bfb366c6d8eb1dfd76ed6 (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * array.c
 *
 * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "compat.h"
#include "util/array.h"

#include "sqfs/error.h"

#include <stdlib.h>

int array_init(array_t *array, size_t size, size_t capacity)
{
	size_t total;

	memset(array, 0, sizeof(*array));

	if (capacity > 0) {
		if (SZ_MUL_OV(size, capacity, &total))
			return SQFS_ERROR_OVERFLOW;

		array->data = malloc(total);
		if (array->data == NULL)
			return SQFS_ERROR_ALLOC;
	}

	array->size = size;
	array->count = capacity;
	return 0;
}

int array_init_copy(array_t *array, const array_t *src)
{
	int ret;

	ret = array_init(array, src->size, src->used);
	if (ret != 0)
		return ret;

	memcpy(array->data, src->data, src->used * src->size);
	array->used = src->used;
	return 0;
}

void array_cleanup(array_t *array)
{
	free(array->data);
	memset(array, 0, sizeof(*array));
}

int array_append(array_t *array, const void *data)
{
	size_t new_sz, new_count;
	void *new;

	if (array->used == array->count) {
		if (array->count == 0) {
			new_count = 128;
		} else {
			if (SZ_MUL_OV(array->count, 2, &new_count))
				return SQFS_ERROR_ALLOC;
		}

		if (SZ_MUL_OV(new_count, array->size, &new_sz))
			return SQFS_ERROR_ALLOC;

		new = realloc(array->data, new_sz);
		if (new == NULL)
			return SQFS_ERROR_ALLOC;

		array->data = new;
		array->count = new_count;
	}

	memcpy((char *)array->data + array->size * array->used,
	       data, array->size);

	array->used += 1;
	return 0;
}

int array_set_capacity(array_t *array, size_t capacity)
{
	size_t new_sz, new_count;
	void *new;

	if (capacity <= array->count)
		return 0;

	if (array->count == 0) {
		new_count = 128;
	} else {
		if (SZ_MUL_OV(array->count, 2, &new_count))
			return SQFS_ERROR_ALLOC;
	}

	while (new_count < capacity) {
		if (SZ_MUL_OV(new_count, 2, &new_count))
			return SQFS_ERROR_ALLOC;
	}

	if (SZ_MUL_OV(new_count, array->size, &new_sz))
		return SQFS_ERROR_ALLOC;

	new = realloc(array->data, new_sz);
	if (new == NULL)
		return SQFS_ERROR_ALLOC;

	array->data = new;
	array->count = new_count;
	return 0;
}