From: Karel Zak Date: Thu, 14 Oct 2010 23:32:13 +0000 (+0200) Subject: libblkid: consolidate magic strings detection code X-Git-Tag: v2.19-rc1~310 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c76e710bc70bb89971c44e971e62793f34953d71;p=thirdparty%2Futil-linux.git libblkid: consolidate magic strings detection code Signed-off-by: Karel Zak --- diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 41eba9102d..6ac8910ccb 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 d6abc203de..5b34d037ea 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 9353ecd2e8..a748f2e249 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 95c10d6eb8..268b2d9b70 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);