]> err.no Git - util-linux/commitdiff
libblkid: consolidate magic strings detection code
authorKarel Zak <kzak@redhat.com>
Thu, 14 Oct 2010 23:32:13 +0000 (01:32 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 14 Oct 2010 23:32:13 +0000 (01:32 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/blkid/src/blkidP.h
shlibs/blkid/src/partitions/partitions.c
shlibs/blkid/src/probe.c
shlibs/blkid/src/superblocks/superblocks.c

index 41eba9102da99346e586250ee36cc436cde67321..6ac8910ccbcb093938eb8b1def3205d080e0a40d 100644 (file)
@@ -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),\
index d6abc203de35a3e825689b75d8fa2a2944225893..5b34d037eac79c84a40b315b54d5a488367438bd 100644 (file)
@@ -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 */
index 9353ecd2e8382da5c8571d102f11acfe1b44bb6a..a748f2e249dbcd682cbc7d46788a30f7e4d2d604 100644 (file)
@@ -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) {
index 95c10d6eb8872c377bbc633be93fed5d5864ec8b..268b2d9b70a7579869809e9c2691a4202afae4bc 100644 (file)
@@ -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);