From c76e710bc70bb89971c44e971e62793f34953d71 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 15 Oct 2010 01:32:13 +0200 Subject: [PATCH] libblkid: consolidate magic strings detection code Signed-off-by: Karel Zak --- shlibs/blkid/src/blkidP.h | 3 ++ shlibs/blkid/src/partitions/partitions.c | 25 +------------- shlibs/blkid/src/probe.c | 39 ++++++++++++++++++++++ shlibs/blkid/src/superblocks/superblocks.c | 28 ++-------------- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 41eba910..6ac8910c 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -386,6 +386,9 @@ extern int blkid_probe_get_dimension(blkid_probe pr, extern int blkid_probe_set_dimension(blkid_probe pr, blkid_loff_t off, blkid_loff_t size); +extern int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id, + blkid_loff_t *offset, const struct blkid_idmag **res); + /* returns superblok according to 'struct blkid_idmag' */ #define blkid_probe_get_sb(_pr, _mag, type) \ ((type *) blkid_probe_get_buffer((_pr),\ diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index d6abc203..5b34d037 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -515,35 +515,12 @@ int blkid_is_nested_dimension(blkid_partition par, static int idinfo_probe(blkid_probe pr, const struct blkid_idinfo *id) { const struct blkid_idmag *mag; - int hasmag = 0; int rc = 1; /* = nothing detected */ if (pr->size <= 0 || (id->minsz && id->minsz > pr->size)) goto nothing; /* the device is too small */ - mag = id->magics ? &id->magics[0] : NULL; - - /* try to detect by magic string */ - while(mag && mag->magic) { - int idx; - unsigned char *buf; - - idx = mag->kboff + (mag->sboff >> 10); - buf = blkid_probe_get_buffer(pr, idx << 10, 1024); - - if (buf && !memcmp(mag->magic, - buf + (mag->sboff & 0x3ff), mag->len)) { - DBG(DEBUG_LOWPROBE, printf( - "%s: magic sboff=%u, kboff=%ld\n", - id->name, mag->sboff, mag->kboff)); - hasmag = 1; - break; - } - mag++; - } - - if (hasmag == 0 && id->magics && id->magics[0].magic) - /* magic string(s) defined, but not found */ + if (blkid_probe_get_idmag(pr, id, NULL, &mag)) goto nothing; /* final check by probing function */ diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index 9353ecd2..a748f2e2 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -714,6 +714,45 @@ int blkid_probe_set_dimension(blkid_probe pr, return 0; } +int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id, + blkid_loff_t *offset, const struct blkid_idmag **res) +{ + const struct blkid_idmag *mag = NULL; + blkid_loff_t off = 0; + + if (id) + mag = id->magics ? &id->magics[0] : NULL; + if (res) + *res = NULL; + + /* try to detect by magic string */ + while(mag && mag->magic) { + unsigned char *buf; + + off = (mag->kboff + (mag->sboff >> 10)) << 10; + buf = blkid_probe_get_buffer(pr, off, 1024); + + if (buf && !memcmp(mag->magic, + buf + (mag->sboff & 0x3ff), mag->len)) { + DBG(DEBUG_LOWPROBE, printf( + "\tmagic sboff=%u, kboff=%ld\n", + mag->sboff, mag->kboff)); + if (offset) + *offset = off + (mag->sboff & 0x3ff); + if (res) + *res = mag; + return 0; + } + mag++; + } + + if (id->magics && id->magics[0].magic) + /* magic string(s) defined, but not found */ + return 1; + + return 0; +} + static inline void blkid_probe_start(blkid_probe pr) { if (pr) { diff --git a/shlibs/blkid/src/superblocks/superblocks.c b/shlibs/blkid/src/superblocks/superblocks.c index 95c10d6e..268b2d9b 100644 --- a/shlibs/blkid/src/superblocks/superblocks.c +++ b/shlibs/blkid/src/superblocks/superblocks.c @@ -322,11 +322,9 @@ static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn) for ( ; i < ARRAY_SIZE(idinfos); i++) { const struct blkid_idinfo *id; - const struct blkid_idmag *mag; + const struct blkid_idmag *mag = NULL; blkid_loff_t off = 0; - int hasmag = 0; - chn->idx = i; if (chn->fltr && blkid_bmp_get_item(chn->fltr, i)) @@ -351,27 +349,7 @@ static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn) DBG(DEBUG_LOWPROBE, printf("[%d] %s:\n", i, id->name)); - /* try to detect by magic string */ - while(mag && mag->magic) { - unsigned char *buf; - - off = (mag->kboff + (mag->sboff >> 10)) << 10; - buf = blkid_probe_get_buffer(pr, off, 1024); - - if (buf && !memcmp(mag->magic, - buf + (mag->sboff & 0x3ff), mag->len)) { - DBG(DEBUG_LOWPROBE, printf( - "\tmagic sboff=%u, kboff=%ld\n", - mag->sboff, mag->kboff)); - hasmag = 1; - off += mag->sboff & 0x3ff; - break; - } - mag++; - } - - if (hasmag == 0 && id->magics && id->magics[0].magic) - /* magic string(s) defined, but not found */ + if (blkid_probe_get_idmag(pr, id, &off, &mag)) continue; /* final check by probing function */ @@ -391,7 +369,7 @@ static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn) blkid_probe_set_usage(pr, id->usage); - if (hasmag) + if (mag) blkid_probe_set_magic(pr, off, mag->len, (unsigned char *) mag->magic); -- 2.39.5