chiark / gitweb /
switch tools and volume_id from LGPL to GPLv2
[elogind.git] / extras / volume_id / volume_id / util.h
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _VOLUME_ID_UTIL_
12 #define _VOLUME_ID_UTIL_
13
14 #ifndef _GNU_SOURCE
15 #define _GNU_SOURCE 1
16 #endif
17
18 #ifdef HAVE_CONFIG_H
19 #  include <config.h>
20 #endif
21
22 #include <endian.h>
23
24 /* size of superblock buffer, reiserfs block is at 64k */
25 #define SB_BUFFER_SIZE                          0x11000
26 /* size of seek buffer, FAT cluster is 32k max */
27 #define SEEK_BUFFER_SIZE                        0x10000
28
29 /* probe volume for all known filesystems in specific order */
30 #define bswap16(x) (uint16_t)   ((((uint16_t)(x) & 0x00ffu) << 8) | \
31                                 (((uint16_t)(x) & 0xff00u) >> 8))
32
33 #define bswap32(x) (uint32_t)   ((((uint32_t)(x) & 0xff000000u) >> 24) | \
34                                 (((uint32_t)(x) & 0x00ff0000u) >>  8) | \
35                                 (((uint32_t)(x) & 0x0000ff00u) <<  8) | \
36                                 (((uint32_t)(x) & 0x000000ffu) << 24))
37
38 #define bswap64(x) (uint64_t)   ((((uint64_t)(x) & 0xff00000000000000ull) >> 56) | \
39                                 (((uint64_t)(x) & 0x00ff000000000000ull) >> 40) | \
40                                 (((uint64_t)(x) & 0x0000ff0000000000ull) >> 24) | \
41                                 (((uint64_t)(x) & 0x000000ff00000000ull) >>  8) | \
42                                 (((uint64_t)(x) & 0x00000000ff000000ull) <<  8) | \
43                                 (((uint64_t)(x) & 0x0000000000ff0000ull) << 24) | \
44                                 (((uint64_t)(x) & 0x000000000000ff00ull) << 40) | \
45                                 (((uint64_t)(x) & 0x00000000000000ffull) << 56))
46
47 #ifdef __BYTE_ORDER
48 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
49 #define le16_to_cpu(x) (x)
50 #define le32_to_cpu(x) (x)
51 #define le64_to_cpu(x) (x)
52 #define be16_to_cpu(x) bswap16(x)
53 #define be32_to_cpu(x) bswap32(x)
54 #define cpu_to_le32(x) (x)
55 #define cpu_to_be32(x) bswap32(x)
56 #elif (__BYTE_ORDER == __BIG_ENDIAN)
57 #define le16_to_cpu(x) bswap16(x)
58 #define le32_to_cpu(x) bswap32(x)
59 #define le64_to_cpu(x) bswap64(x)
60 #define be16_to_cpu(x) (x)
61 #define be32_to_cpu(x) (x)
62 #define cpu_to_le32(x) bswap32(x)
63 #define cpu_to_be32(x) (x)
64 #endif
65 #endif /* __BYTE_ORDER */
66
67 enum uuid_format {
68         UUID_DCE_STRING,
69         UUID_DCE,
70         UUID_DOS,
71         UUID_NTFS,
72         UUID_HFS,
73 };
74
75 enum endian {
76         LE = 0,
77         BE = 1
78 };
79
80 extern void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count);
81 extern void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id);
82 extern void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id);
83 extern void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count);
84 extern void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count);
85 extern void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count);
86 extern void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format);
87 extern uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len);
88 extern void volume_id_free_buffer(struct volume_id *id);
89
90 #endif /* _VOLUME_ID_UTIL_ */
91