block: add support for mounting swap files
[project/ubox.git] / libblkid-tiny / squashfs.c
1 /*
2  * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3  *
4  * Inspired by libvolume_id by
5  *     Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This file may be redistributed under the terms of the
8  * GNU Lesser General Public License.
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdint.h>
15
16 #include "bitops.h"     /* swab16() */
17 #include "superblocks.h"
18
19 #include "md5.h"
20
21 struct squashfs_super_block {
22         uint32_t s_magic;
23         uint32_t inodes;
24         uint32_t mkfs_time;
25         uint32_t block_size;
26         uint32_t fragments;
27         uint16_t compression;
28         uint16_t block_log;
29         uint16_t flags;
30         uint16_t no_ids;
31         uint16_t s_major;
32         uint16_t s_minor;
33         uint64_t root_inode;
34         uint64_t bytes_used;
35         uint64_t id_table_start;
36         uint64_t xattr_id_table_start;
37         uint64_t inode_table_start;
38         uint64_t directory_table_start;
39         uint64_t fragment_table_start;
40         uint64_t lookup_table_start;
41 } __attribute__((packed));
42
43 static int probe_squashfs(blkid_probe pr, const struct blkid_idmag *mag)
44 {
45         md5_ctx_t ctx = { 0 };
46         uint32_t md5[4];
47         struct squashfs_super_block *sq;
48
49         sq = blkid_probe_get_sb(pr, mag, struct squashfs_super_block);
50         if (!sq)
51                 return -1;
52
53         if (strcmp(mag->magic, "sqsh") == 0 ||
54             strcmp(mag->magic, "qshs") == 0)
55                 blkid_probe_sprintf_version(pr, "%u.%u",
56                                 sq->s_major,
57                                 sq->s_minor);
58         else
59                 blkid_probe_sprintf_version(pr, "%u.%u",
60                                 swab16(sq->s_major),
61                                 swab16(sq->s_minor));
62         md5_begin(&ctx);
63         md5_hash(sq, sizeof(*sq), &ctx);
64         md5_end(&md5, &ctx);
65         blkid_probe_sprintf_uuid(pr, NULL, 4, "%08x-%08x-%08x-%08x",
66                         md5[3], md5[2], md5[1], md5[0]);
67         return 0;
68 }
69
70 const struct blkid_idinfo squashfs_idinfo =
71 {
72         .name           = "squashfs",
73         .usage          = BLKID_USAGE_FILESYSTEM,
74         .probefunc      = probe_squashfs,
75         .magics         =
76         {
77                 { .magic = "sqsh", .len = 4 },
78                 { .magic = "hsqs", .len = 4 }, /* swap */
79
80                 /* LZMA version */
81                 { .magic = "qshs", .len = 4 },
82                 { .magic = "shsq", .len = 4 }, /* swap */
83                 { NULL }
84         }
85 };
86
87