chiark / gitweb /
7499fc121e93db7277e3a421642f286136770744
[elogind.git] / extras / volume_id / volume_id / util.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <asm/types.h>
38
39 #include "volume_id.h"
40 #include "logging.h"
41 #include "util.h"
42
43 void volume_id_set_unicode16(char *str, unsigned int len, const __u8 *buf, enum endian endianess, unsigned int count)
44 {
45         unsigned int i, j;
46         __u16 c;
47
48         j = 0;
49         for (i = 0; i + 2 <= count; i += 2) {
50                 if (endianess == LE)
51                         c = (buf[i+1] << 8) | buf[i];
52                 else
53                         c = (buf[i] << 8) | buf[i+1];
54                 if (c == 0) {
55                         str[j] = '\0';
56                         break;
57                 } else if (c < 0x80) {
58                         if (j+1 >= len)
59                                 break;
60                         str[j++] = (__u8) c;
61                 } else if (c < 0x800) {
62                         if (j+2 >= len)
63                                 break;
64                         str[j++] = (__u8) (0xc0 | (c >> 6));
65                         str[j++] = (__u8) (0x80 | (c & 0x3f));
66                 } else {
67                         if (j+3 >= len)
68                                 break;
69                         str[j++] = (__u8) (0xe0 | (c >> 12));
70                         str[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f));
71                         str[j++] = (__u8) (0x80 | (c & 0x3f));
72                 }
73         }
74         str[j] = '\0';
75 }
76
77 static char *usage_to_string(enum volume_id_usage usage_id)
78 {
79         switch (usage_id) {
80         case VOLUME_ID_FILESYSTEM:
81                 return "filesystem";
82         case VOLUME_ID_PARTITIONTABLE:
83                 return "partitiontable";
84         case VOLUME_ID_OTHER:
85                 return "other";
86         case VOLUME_ID_RAID:
87                 return "raid";
88         case VOLUME_ID_DISKLABEL:
89                 return "disklabel";
90         case VOLUME_ID_CRYPTO:
91                 return "crypto";
92         case VOLUME_ID_UNPROBED:
93                 return "unprobed";
94         case VOLUME_ID_UNUSED:
95                 return "unused";
96         }
97         return NULL;
98 }
99
100 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
101 {
102         part->usage_id = usage_id;
103         part->usage = usage_to_string(usage_id);
104 }
105
106 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
107 {
108         id->usage_id = usage_id;
109         id->usage = usage_to_string(usage_id);
110 }
111
112 void volume_id_set_label_raw(struct volume_id *id, const __u8 *buf, unsigned int count)
113 {
114         memcpy(id->label_raw, buf, count);
115         id->label_raw_len = count;
116 }
117
118 void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned int count)
119 {
120         unsigned int i;
121
122         memcpy(id->label, buf, count);
123
124         /* remove trailing whitespace */
125         i = strnlen(id->label, count);
126         while (i--) {
127                 if (!isspace(id->label[i]))
128                         break;
129         }
130         id->label[i+1] = '\0';
131 }
132
133 void volume_id_set_label_unicode16(struct volume_id *id, const __u8 *buf, enum endian endianess, unsigned int count)
134 {
135          volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
136 }
137
138 void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format)
139 {
140         unsigned int i;
141         unsigned int count = 0;
142
143         switch(format) {
144         case UUID_DOS:
145                 count = 4;
146                 break;
147         case UUID_NTFS:
148         case UUID_HFS:
149                 count = 8;
150                 break;
151         case UUID_DCE:
152                 count = 16;
153                 break;
154         case UUID_DCE_STRING:
155                 count = 36;
156                 break;
157         }
158         memcpy(id->uuid_raw, buf, count);
159         id->uuid_raw_len = count;
160
161         /* if set, create string in the same format, the native platform uses */
162         for (i = 0; i < count; i++)
163                 if (buf[i] != 0)
164                         goto set;
165         return;
166
167 set:
168         switch(format) {
169         case UUID_DOS:
170                 sprintf(id->uuid, "%02X%02X-%02X%02X",
171                         buf[3], buf[2], buf[1], buf[0]);
172                 break;
173         case UUID_NTFS:
174                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
175                         buf[7], buf[6], buf[5], buf[4],
176                         buf[3], buf[2], buf[1], buf[0]);
177                 break;
178         case UUID_HFS:
179                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
180                         buf[0], buf[1], buf[2], buf[3],
181                         buf[4], buf[5], buf[6], buf[7]);
182                 break;
183         case UUID_DCE:
184                 sprintf(id->uuid,
185                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
186                         buf[0], buf[1], buf[2], buf[3],
187                         buf[4], buf[5],
188                         buf[6], buf[7],
189                         buf[8], buf[9],
190                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
191                 break;
192         case UUID_DCE_STRING:
193                 memcpy(id->uuid, buf, count);
194                 id->uuid[count] = '\0';
195                 break;
196         }
197 }
198
199 __u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len)
200 {
201         unsigned int buf_len;
202
203         dbg("get buffer off 0x%llx(%llu), len 0x%x", (unsigned long long) off, (unsigned long long) off, len);
204         /* check if requested area fits in superblock buffer */
205         if (off + len <= SB_BUFFER_SIZE) {
206                 if (id->sbbuf == NULL) {
207                         id->sbbuf = malloc(SB_BUFFER_SIZE);
208                         if (id->sbbuf == NULL)
209                                 return NULL;
210                 }
211
212                 /* check if we need to read */
213                 if ((off + len) > id->sbbuf_len) {
214                         dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
215                         lseek(id->fd, 0, SEEK_SET);
216                         buf_len = read(id->fd, id->sbbuf, off + len);
217                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
218                         id->sbbuf_len = buf_len;
219                         if (buf_len < off + len)
220                                 return NULL;
221                 }
222
223                 return &(id->sbbuf[off]);
224         } else {
225                 if (len > SEEK_BUFFER_SIZE) {
226                         dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
227                         return NULL;
228                 }
229
230                 /* get seek buffer */
231                 if (id->seekbuf == NULL) {
232                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
233                         if (id->seekbuf == NULL)
234                                 return NULL;
235                 }
236
237                 /* check if we need to read */
238                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
239                         dbg("read seekbuf off:0x%llx len:0x%x", (unsigned long long) off, len);
240                         if (lseek(id->fd, off, SEEK_SET) == -1)
241                                 return NULL;
242                         buf_len = read(id->fd, id->seekbuf, len);
243                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
244                         id->seekbuf_off = off;
245                         id->seekbuf_len = buf_len;
246                         if (buf_len < len) {
247                                 dbg("requested 0x%x bytes, got only 0x%x bytes", len, buf_len);
248                                 return NULL;
249                         }
250                 }
251
252                 return &(id->seekbuf[off - id->seekbuf_off]);
253         }
254 }
255
256 void volume_id_free_buffer(struct volume_id *id)
257 {
258         if (id->sbbuf != NULL) {
259                 free(id->sbbuf);
260                 id->sbbuf = NULL;
261                 id->sbbuf_len = 0;
262         }
263         if (id->seekbuf != NULL) {
264                 free(id->seekbuf);
265                 id->seekbuf = NULL;
266                 id->seekbuf_len = 0;
267         }
268 }