Skip to content

Commit

Permalink
Merge pull request #15 from magnostherobot/sysvinit/czinspect
Browse files Browse the repository at this point in the history
Bugfixes in the extraction and portability code of czinspect. Closes #2 and #3.
  • Loading branch information
sysvinit authored Aug 1, 2018
2 parents 5e237c0 + b15b19e commit cee90c9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/czinspect/src/czinspect.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ static void parse_opt_extract(int opt) {
if (errstr)
errx(1, "invalid filter level '%s': %s", optarg, errstr);
cfg.filter = filt;
cfg.eflags |= EXT_F_FILT;
cfg.filtflags |= EXT_FI_FILT;
break;
case 'g':
cfg.eflags |= EXT_F_FFUZZ;
cfg.filtflags |= EXT_FI_FFUZZ;
break;
case 's':
cfg.eflags |= EXT_F_SBLK;
Expand Down
27 changes: 0 additions & 27 deletions src/czinspect/src/endian.h

This file was deleted.

50 changes: 33 additions & 17 deletions src/czinspect/src/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum subopt_index {
static int dirfd;
static uint8_t sblk_opts;
static uint8_t ext_opts;
static uint8_t filt_opts;
static uint32_t filterlevel;
static lzstring fname;
static lzstring suffix;
Expand Down Expand Up @@ -93,20 +94,23 @@ static void parse_opts(struct config *cfg) {
}

xfree(cfg->esopts);
} else if (cfg->eflags & EXT_F_FILT) {
errx(1, "cannot perform subsampling level filtering when not extracting subblocks");
} else if (cfg->eflags & EXT_F_FFUZZ) {
errx(1, "cannot perform rounded subsampling level filtering when not extracting subblocks");
}

if ((cfg->eflags & EXT_F_FFUZZ) == EXT_F_FFUZZ && (cfg->eflags & EXT_F_FILT) == 0)
errx(1, "cannot round subsampling level when not performing filtering");

if (cfg->eflags == 0) {
ext_opts = (EXT_F_META | EXT_F_SBLK | EXT_F_ATTACH);
sblk_opts = EXT_S_ALL;
} else
} else {
ext_opts = cfg->eflags;
}

if (((ext_opts & EXT_F_SBLK) == 0) && (cfg->filtflags & (EXT_FI_FILT | EXT_FI_FFUZZ))) {
errx(1, "cannot perform subsampling level filtering when not extracting subblocks");
}

if ((cfg->filtflags & EXT_FI_FFUZZ) == EXT_FI_FFUZZ && (cfg->filtflags & EXT_FI_FILT) == 0)
errx(1, "cannot round subsampling level when not performing filtering");

filt_opts = cfg->filtflags;

return;
}
Expand Down Expand Up @@ -298,7 +302,7 @@ static void extract_subblock(struct map_ctx *c) {
offset = 256 - offset;
if (offset > 0)
if (map_seek(c, offset, MAP_FORW) == -1)
ferrx("cannot seek forwards %" PRIu64 " bytes", offset);
ferrx("cannot seek forwards %zu bytes", offset);

make_suffix(suffix, dimensions, sblk.dir_entry.dimension_count);

Expand Down Expand Up @@ -388,7 +392,7 @@ static void extract_sblk_directory(struct map_ctx *c, uint64_t pos) {
extract_subblock(c);

if (map_seek(c, offset, MAP_SET) == -1)
ferrx("could not seek to previous offset %" PRIu64 " to resume scanning subblocks", offset);
ferrx("could not seek to previous offset %zu to resume scanning subblocks", offset);
}

lzstr_free(fname);
Expand All @@ -401,8 +405,10 @@ static void extract_sblk_directory(struct map_ctx *c, uint64_t pos) {
static void setup_filterlevel(struct config *cfg, struct map_ctx *c, uint64_t dirpos) {
lzbuf reslist;
uint32_t rnum = 0;
uint32_t max = 0;
uint32_t l;

if ((cfg->eflags & EXT_F_FILT) == 0)
if ((filt_opts & EXT_FI_FILT) == 0)
return;

if (map_seek(c, dirpos, MAP_SET) == -1)
Expand All @@ -414,12 +420,22 @@ static void setup_filterlevel(struct config *cfg, struct map_ctx *c, uint64_t di
if (make_reslist(c, reslist, &rnum) == -1)
ferrx1("could not scan for subsampling levels in input file");

for (uint32_t i = 0; i < rnum; i++)
if ((cfg->eflags & EXT_F_FFUZZ) ?
(lzbuf_get(uint32_t, reslist, i) <= cfg->filter) :
(lzbuf_get(uint32_t, reslist, i) == cfg->filter))
filterlevel = lzbuf_get(uint32_t, reslist, i);


if (filt_opts & EXT_FI_FFUZZ) {
for (uint32_t i = 0; i < rnum; i++) {
l = lzbuf_get(uint32_t, reslist, i);

if (l <= cfg->filter && l > max) {
filterlevel = l;
max = l;
}
}
} else {
for (uint32_t i = 0; i < rnum; i++)
if (lzbuf_get(uint32_t, reslist, i) == cfg->filter)
filterlevel = cfg->filter;
}

if (filterlevel == 0)
errx(1, "invalid filter level: %" PRIu32, cfg->filter);

Expand Down
14 changes: 7 additions & 7 deletions src/czinspect/src/mapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ int map_seek(struct map_ctx *ctx, size_t offset, int whence) {
switch (whence) {
case MAP_SET:
if (offset > ctx->fsize)
return fwarnx("invalid seek offset of %" PRIi64, offset), -1;
return fwarnx("invalid seek offset of %zu", offset), -1;
target = offset;
break;
case MAP_FORW:
if (map_file_remaining(ctx) + offset > ctx->fsize)
return fwarnx("invalid seek offset of %" PRIi64, offset), -1;
return fwarnx("invalid seek offset of %zu", offset), -1;
target = map_file_offset(ctx) + offset;
break;
case MAP_BACK:
if (offset > map_file_offset(ctx))
return fwarnx("invalid seek offset of %" PRIi64, offset), -1;
return fwarnx("invalid seek offset of %zu", offset), -1;
target = map_file_offset(ctx) - offset;
break;
default:
Expand All @@ -173,7 +173,7 @@ int map_seek(struct map_ctx *ctx, size_t offset, int whence) {
int map_read(struct map_ctx *ctx, void *buf, size_t len) {

if (len > map_file_remaining(ctx))
return fwarnx("cannot read %" PRIu64 " bytes from \"%s\"", len, ctx->fname), -1;
return fwarnx("cannot read %zu bytes from \"%s\"", len, ctx->fname), -1;

while ((ctx->offset + len) > ctx->chunklen) {
memcpy(buf, map_chunk_ptr(ctx), map_chunk_remain(ctx));
Expand All @@ -200,7 +200,7 @@ static int write_loop(int fd, void *buf, size_t len) {
if (errno == EINTR || errno == EAGAIN)
continue;

return fwarn("cannot write %" PRIu64" bytes to file descriptor %u",
return fwarn("cannot write %zu bytes to file descriptor %u",
len, fd), -1;
}

Expand All @@ -215,7 +215,7 @@ static int write_loop(int fd, void *buf, size_t len) {
int map_dwrite(struct map_ctx *ctx, int fd, size_t len) {

if (len > map_file_remaining(ctx))
return fwarnx("cannot read %" PRIu64 " bytes from \"%s\"", len, ctx->fname), -1;
return fwarnx("cannot read %zu bytes from \"%s\"", len, ctx->fname), -1;

while ((ctx->offset + len) > ctx->chunklen) {
if (write_loop(fd, map_chunk_ptr(ctx), map_chunk_remain(ctx)) == -1)
Expand All @@ -240,7 +240,7 @@ int map_splice(struct map_ctx *from, struct map_ctx *to, size_t len) {
size_t adv; /* how many bytes to advance in the loop */

if (len > map_file_remaining(from) || len > map_file_remaining(to))
return fwarnx("cannot copy %" PRIu64 " bytes from \"%s\" to \"%s\"",
return fwarnx("cannot copy %zu bytes from \"%s\" to \"%s\"",
len, from->fname, to->fname), -1;

while ((from->offset + len) > from->chunklen || (to->offset + len) > to->chunklen) {
Expand Down
6 changes: 4 additions & 2 deletions src/czinspect/src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#define EXT_F_META 0x01
#define EXT_F_ATTACH 0x02
#define EXT_F_SBLK 0x04
#define EXT_F_FILT 0x08
#define EXT_F_FFUZZ 0x10 /* round the extraction level */

/* filtering flags */
#define EXT_FI_FILT 0x01
#define EXT_FI_FFUZZ 0x02 /* round the extraction level */

void do_scan(struct config *);
void do_extract(struct config *);
Expand Down
1 change: 1 addition & 0 deletions src/czinspect/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct config {
/* extraction options */
char *outdir;
uint8_t eflags; /* whole-file extraction flags */
uint8_t filtflags; /* extraction filtering flags */
uint32_t filter; /* filter level */
char *esopts; /* option string for extracting data from subblocks */

Expand Down
Loading

0 comments on commit cee90c9

Please sign in to comment.