summaryrefslogtreecommitdiff
path: root/lib/sqfs/id_table.c
blob: deefd6c4cec0f949d44f52dca7afe8e143949183 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * id_table.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "id_table.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int id_table_init(id_table_t *tbl)
{
	memset(tbl, 0, sizeof(*tbl));
	return 0;
}

void id_table_cleanup(id_table_t *tbl)
{
	free(tbl->ids);
}

int id_table_id_to_index(id_table_t *tbl, uint32_t id, uint16_t *out)
{
	size_t i, sz;
	void *ptr;

	for (i = 0; i < tbl->num_ids; ++i) {
		if (tbl->ids[i] == id) {
			*out = i;
			return 0;
		}
	}

	if (tbl->num_ids == 0x10000) {
		fputs("Too many unique UIDs/GIDs (more than 64k)!\n", stderr);
		return -1;
	}

	if (tbl->num_ids == tbl->max_ids) {
		sz = (tbl->max_ids ? tbl->max_ids * 2 : 16);
		ptr = realloc(tbl->ids, sizeof(tbl->ids[0]) * sz);

		if (ptr == NULL) {
			perror("growing ID table");
			return -1;
		}

		tbl->ids = ptr;
		tbl->max_ids = sz;
	}

	*out = tbl->num_ids;
	tbl->ids[tbl->num_ids++] = id;
	return 0;
}