aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-02 08:06:48 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-03 21:45:46 +0200
commit83183717feca5c673d614e341bfca49fae9a8001 (patch)
tree9fcb5ea12e01852f6fbb255ae4e6d2b612edbb43 /lib/sqfs
parent1796ff184ba862508fc578c985dd9ebcc15a048d (diff)
Add helper function for reading dir entries to meta reader
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r--lib/sqfs/readdir.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/sqfs/readdir.c b/lib/sqfs/readdir.c
new file mode 100644
index 0000000..142bafb
--- /dev/null
+++ b/lib/sqfs/readdir.c
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "meta_reader.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+int meta_reader_read_dir_header(meta_reader_t *m, sqfs_dir_header_t *hdr)
+{
+ if (meta_reader_read(m, hdr, sizeof(*hdr)))
+ return -1;
+
+ hdr->count = le32toh(hdr->count);
+ hdr->start_block = le32toh(hdr->start_block);
+ hdr->inode_number = le32toh(hdr->inode_number);
+ return 0;
+}
+
+sqfs_dir_entry_t *meta_reader_read_dir_ent(meta_reader_t *m)
+{
+ sqfs_dir_entry_t ent, *out;
+
+ if (meta_reader_read(m, &ent, sizeof(ent)))
+ return NULL;
+
+ ent.offset = le16toh(ent.offset);
+ ent.inode_number = le16toh(ent.inode_number);
+ ent.type = le16toh(ent.type);
+ ent.size = le16toh(ent.size);
+
+ out = calloc(1, sizeof(*out) + ent.size + 2);
+ if (out == NULL) {
+ perror("reading dir entry");
+ return NULL;
+ }
+
+ *out = ent;
+ if (meta_reader_read(m, out->name, ent.size + 1)) {
+ free(out);
+ return NULL;
+ }
+
+ if (strchr((char *)out->name, '/') != NULL ||
+ strchr((char *)out->name, '\\') != NULL) {
+ fputs("Found a file name that contains slashes\n", stderr);
+ free(out);
+ return NULL;
+ }
+
+ if (strcmp((char *)out->name, "..") == 0 ||
+ strcmp((char *)out->name, ".") == 0) {
+ fputs("Found '..' or '.' in file names\n", stderr);
+ free(out);
+ return NULL;
+ }
+
+ return out;
+}