aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-01 01:41:30 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-02 12:40:06 +0200
commitde9f0a79850df74078b8c104ce1232d40ec9cc1f (patch)
tree3a2dcd1592f0409379d14a38a416d0998189e560 /lib/sqfs
parent2196bc61b8ba67c91927ba28538538b89ccf73f1 (diff)
Add id table code stub
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r--lib/sqfs/id_table.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c
new file mode 100644
index 0000000..cd31360
--- /dev/null
+++ b/lib/sqfs/id_table.c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#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;
+}