chiark / gitweb /
vol_id: fix logging glue
[elogind.git] / extras / volume_id / lib / hfs.c
index f666b183790d8c74ccff836d46034fbb06b3d5ac..dbc5f36195e6cb6d32566b7fa772e150d81590d1 100644 (file)
@@ -1,21 +1,26 @@
 /*
  * volume_id - reads filesystem label and uuid
  *
- * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE 1
 #endif
 
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -24,7 +29,8 @@
 #include <ctype.h>
 
 #include "libvolume_id.h"
-#include "util.h"
+#include "libvolume_id-private.h"
+#include "md5.h"
 
 struct hfs_finder_info{
        uint32_t        boot_folder;
@@ -137,7 +143,29 @@ static struct hfsplus_vol_header {
 #define HFS_NODE_LEAF                  0xff
 #define HFSPLUS_POR_CNID               1
 
-int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
+static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
+{
+       struct md5_ctx md5c;
+       static const uint8_t hash_init[16] = {
+               0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
+               0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
+       };
+       uint8_t uuid[16];
+
+       if (*((uint64_t *)hfs_id) == 0)
+               return;
+
+       md5_init(&md5c);
+       md5_update(&md5c, hash_init, 16);
+       md5_update(&md5c, hfs_id, 8);
+       md5_final(&md5c, uuid);
+
+       uuid[6] = 0x30 | (uuid[6] & 0x0f);
+       uuid[8] = 0x80 | (uuid[8] & 0x3f);
+       volume_id_set_uuid(id, uuid, 0, UUID_DCE);
+}
+
+int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
 {
        unsigned int blocksize;
        unsigned int cat_block;
@@ -160,7 +188,7 @@ int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
        struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
        const uint8_t *buf;
 
-       dbg("probing at offset 0x%llx", (unsigned long long) off);
+       info("probing at offset 0x%llx\n", (unsigned long long) off);
 
        buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
        if (buf == NULL)
@@ -173,17 +201,17 @@ int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
        /* it may be just a hfs wrapper for hfs+ */
        if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
                alloc_block_size = be32_to_cpu(hfs->al_blk_size);
-               dbg("alloc_block_size 0x%x", alloc_block_size);
+               dbg("alloc_block_size 0x%x\n", alloc_block_size);
 
                alloc_first_block = be16_to_cpu(hfs->al_bl_st);
-               dbg("alloc_first_block 0x%x", alloc_first_block);
+               dbg("alloc_first_block 0x%x\n", alloc_first_block);
 
                embed_first_block = be16_to_cpu(hfs->embed_startblock);
-               dbg("embed_first_block 0x%x", embed_first_block);
+               dbg("embed_first_block 0x%x\n", embed_first_block);
 
                off += (alloc_first_block * 512) +
                       (embed_first_block * alloc_block_size);
-               dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
+               dbg("hfs wrapped hfs+ found at offset 0x%llx\n", (unsigned long long) off);
 
                buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
                if (buf == NULL)
@@ -196,7 +224,7 @@ int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
                volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
        }
 
-       volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
+       hfsid_set_uuid(id, hfs->finder_info.id);
 
        volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
        id->type = "hfs";
@@ -212,14 +240,14 @@ checkplus:
        return -1;
 
 hfsplus:
-       volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
+       hfsid_set_uuid(id, hfsplus->finder_info.id);
 
        blocksize = be32_to_cpu(hfsplus->blocksize);
-       dbg("blocksize %u", blocksize);
+       dbg("blocksize %u\n", blocksize);
 
        memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
        cat_block = be32_to_cpu(extents[0].start_block);
-       dbg("catalog start block 0x%x", cat_block);
+       dbg("catalog start block 0x%x\n", cat_block);
 
        buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
        if (buf == NULL)
@@ -229,13 +257,13 @@ hfsplus:
                &buf[sizeof(struct hfsplus_bnode_descriptor)];
 
        leaf_node_head = be32_to_cpu(bnode->leaf_head);
-       dbg("catalog leaf node 0x%x", leaf_node_head);
+       dbg("catalog leaf node 0x%x\n", leaf_node_head);
 
        leaf_node_size = be16_to_cpu(bnode->node_size);
-       dbg("leaf node size 0x%x", leaf_node_size);
+       dbg("leaf node size 0x%x\n", leaf_node_size);
 
        leaf_node_count = be32_to_cpu(bnode->leaf_count);
-       dbg("leaf node count 0x%x", leaf_node_count);
+       dbg("leaf node count 0x%x\n", leaf_node_count);
        if (leaf_node_count == 0)
                goto found;
 
@@ -245,7 +273,7 @@ hfsplus:
        for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
                ext_block_start = be32_to_cpu(extents[ext].start_block);
                ext_block_count = be32_to_cpu(extents[ext].block_count);
-               dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
+               dbg("extent start block 0x%x, count 0x%x\n", ext_block_start, ext_block_count);
 
                if (ext_block_count == 0)
                        goto found;
@@ -258,7 +286,7 @@ hfsplus:
        }
        if (ext == HFSPLUS_EXTENT_COUNT)
                goto found;
-       dbg("found block in extent %i", ext);
+       dbg("found block in extent %i\n", ext);
 
        leaf_off = (ext_block_start + leaf_block) * blocksize;
 
@@ -267,10 +295,10 @@ hfsplus:
                goto found;
 
        descr = (struct hfsplus_bnode_descriptor *) buf;
-       dbg("descriptor type 0x%x", descr->type);
+       dbg("descriptor type 0x%x\n", descr->type);
 
        record_count = be16_to_cpu(descr->num_recs);
-       dbg("number of records %u", record_count);
+       dbg("number of records %u\n", record_count);
        if (record_count == 0)
                goto found;
 
@@ -280,12 +308,12 @@ hfsplus:
        key = (struct hfsplus_catalog_key *)
                &buf[sizeof(struct hfsplus_bnode_descriptor)];
 
-       dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
+       dbg("parent id 0x%x\n", be32_to_cpu(key->parent_id));
        if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
                goto found;
 
        label_len = be16_to_cpu(key->unicode_len) * 2;
-       dbg("label unicode16 len %i", label_len);
+       dbg("label unicode16 len %i\n", label_len);
        volume_id_set_label_raw(id, key->unicode, label_len);
        volume_id_set_label_unicode16(id, key->unicode, BE, label_len);