chiark / gitweb /
volume_id: add internal UUID_STRING
[elogind.git] / extras / volume_id / lib / 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 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 _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "util.h"
30
31 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
32 {
33         unsigned int i, j;
34         uint16_t c;
35
36         j = 0;
37         for (i = 0; i + 2 <= count; i += 2) {
38                 if (endianess == LE)
39                         c = (buf[i+1] << 8) | buf[i];
40                 else
41                         c = (buf[i] << 8) | buf[i+1];
42                 if (c == 0) {
43                         str[j] = '\0';
44                         break;
45                 } else if (c < 0x80) {
46                         if (j+1 >= len)
47                                 break;
48                         str[j++] = (uint8_t) c;
49                 } else if (c < 0x800) {
50                         if (j+2 >= len)
51                                 break;
52                         str[j++] = (uint8_t) (0xc0 | (c >> 6));
53                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
54                 } else {
55                         if (j+3 >= len)
56                                 break;
57                         str[j++] = (uint8_t) (0xe0 | (c >> 12));
58                         str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
59                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
60                 }
61         }
62         str[j] = '\0';
63 }
64
65 static char *usage_to_string(enum volume_id_usage usage_id)
66 {
67         switch (usage_id) {
68         case VOLUME_ID_FILESYSTEM:
69                 return "filesystem";
70         case VOLUME_ID_OTHER:
71                 return "other";
72         case VOLUME_ID_RAID:
73                 return "raid";
74         case VOLUME_ID_DISKLABEL:
75                 return "disklabel";
76         case VOLUME_ID_CRYPTO:
77                 return "crypto";
78         case VOLUME_ID_UNPROBED:
79                 return "unprobed";
80         case VOLUME_ID_UNUSED:
81                 return "unused";
82         }
83         return NULL;
84 }
85
86 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
87 {
88         id->usage_id = usage_id;
89         id->usage = usage_to_string(usage_id);
90 }
91
92 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
93 {
94         memcpy(id->label_raw, buf, count);
95         id->label_raw_len = count;
96 }
97
98 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
99 {
100         unsigned int i;
101
102         memcpy(id->label, buf, count);
103
104         /* remove trailing whitespace */
105         i = strnlen(id->label, count);
106         while (i--) {
107                 if (!isspace(id->label[i]))
108                         break;
109         }
110         id->label[i+1] = '\0';
111 }
112
113 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
114 {
115          volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
116 }
117
118 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
119 {
120         unsigned int i;
121         unsigned int count = 0;
122
123         if (len > sizeof(id->uuid_raw))
124                 len = sizeof(id->uuid_raw);
125
126         switch(format) {
127         case UUID_DOS:
128                 count = 4;
129                 break;
130         case UUID_NTFS:
131         case UUID_HFS:
132                 count = 8;
133                 break;
134         case UUID_DCE:
135                 count = 16;
136                 break;
137         case UUID_HEX_STRING:
138                 count = len;
139                 break;
140         case UUID_STRING:
141                 count = len;
142                 break;
143         case UUID_FOURINT:
144                 count = 35;
145                 break;
146         }
147         memcpy(id->uuid_raw, buf, count);
148         id->uuid_raw_len = count;
149
150         /* if set, create string in the same format, the native platform uses */
151         for (i = 0; i < count; i++)
152                 if (buf[i] != 0)
153                         goto set;
154         return;
155
156 set:
157         switch(format) {
158         case UUID_DOS:
159                 sprintf(id->uuid, "%02X%02X-%02X%02X",
160                         buf[3], buf[2], buf[1], buf[0]);
161                 break;
162         case UUID_NTFS:
163                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
164                         buf[7], buf[6], buf[5], buf[4],
165                         buf[3], buf[2], buf[1], buf[0]);
166                 break;
167         case UUID_HFS:
168                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
169                         buf[0], buf[1], buf[2], buf[3],
170                         buf[4], buf[5], buf[6], buf[7]);
171                 break;
172         case UUID_DCE:
173                 sprintf(id->uuid,
174                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
175                         buf[0], buf[1], buf[2], buf[3],
176                         buf[4], buf[5],
177                         buf[6], buf[7],
178                         buf[8], buf[9],
179                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
180                 break;
181         case UUID_HEX_STRING:
182                 for (i = 0; i < count; i++)
183                         id->uuid[i] = tolower(buf[i]);
184                 id->uuid[count] = '\0';
185                 break;
186         case UUID_STRING:
187                 memcpy(id->uuid, buf, count);
188                 id->uuid[count] = '\0';
189                 break;
190         case UUID_FOURINT:
191                 sprintf(id->uuid,
192                         "%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
193                         buf[0], buf[1], buf[2], buf[3],
194                         buf[4], buf[5], buf[6], buf[7],
195                         buf[8], buf[9], buf[10], buf[11],
196                         buf[12], buf[13], buf[14],buf[15]);
197                 break;
198         }
199 }
200
201 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
202 {
203         ssize_t buf_len;
204
205         info("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
206         /* check if requested area fits in superblock buffer */
207         if (off + len <= SB_BUFFER_SIZE) {
208                 if (id->sbbuf == NULL) {
209                         id->sbbuf = malloc(SB_BUFFER_SIZE);
210                         if (id->sbbuf == NULL) {
211                                 dbg("error malloc");
212                                 return NULL;
213                         }
214                 }
215
216                 /* check if we need to read */
217                 if ((off + len) > id->sbbuf_len) {
218                         info("read sbbuf len:0x%llx", (unsigned long long) (off + len));
219                         if (lseek(id->fd, 0, SEEK_SET) < 0) {
220                                 dbg("lseek failed (%s)", strerror(errno));
221                                 return NULL;
222                         }
223                         buf_len = read(id->fd, id->sbbuf, off + len);
224                         if (buf_len < 0) {
225                                 dbg("read failed (%s)", strerror(errno));
226                                 return NULL;
227                         }
228                         dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
229                         id->sbbuf_len = buf_len;
230                         if ((size_t)buf_len < off + len) {
231                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
232                                 return NULL;
233                         }
234                 }
235
236                 return &(id->sbbuf[off]);
237         } else {
238                 if (len > SEEK_BUFFER_SIZE) {
239                         dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
240                         return NULL;
241                 }
242
243                 /* get seek buffer */
244                 if (id->seekbuf == NULL) {
245                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
246                         if (id->seekbuf == NULL) {
247                                 dbg("error malloc");
248                                 return NULL;
249                         }
250                 }
251
252                 /* check if we need to read */
253                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
254                         info("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
255                         if (lseek(id->fd, off, SEEK_SET) < 0) {
256                                 dbg("lseek failed (%s)", strerror(errno));
257                                 return NULL;
258                         }
259                         buf_len = read(id->fd, id->seekbuf, len);
260                         if (buf_len < 0) {
261                                 dbg("read failed (%s)", strerror(errno));
262                                 return NULL;
263                         }
264                         dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
265                         id->seekbuf_off = off;
266                         id->seekbuf_len = buf_len;
267                         if ((size_t)buf_len < len) {
268                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
269                                 return NULL;
270                         }
271                 }
272
273                 return &(id->seekbuf[off - id->seekbuf_off]);
274         }
275 }
276
277 void volume_id_free_buffer(struct volume_id *id)
278 {
279         if (id->sbbuf != NULL) {
280                 free(id->sbbuf);
281                 id->sbbuf = NULL;
282                 id->sbbuf_len = 0;
283         }
284         if (id->seekbuf != NULL) {
285                 free(id->seekbuf);
286                 id->seekbuf = NULL;
287                 id->seekbuf_len = 0;
288         }
289 }