From 83183717feca5c673d614e341bfca49fae9a8001 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 2 May 2019 08:06:48 +0200 Subject: Add helper function for reading dir entries to meta reader Signed-off-by: David Oberhollenzer --- lib/sqfs/readdir.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/sqfs/readdir.c (limited to 'lib/sqfs') 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 +#include +#include + +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; +} -- cgit v1.2.3