chiark / gitweb /
361d7058d021e4c25978eff3850c9bdd983e0901
[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 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 "volume_id.h"
29 #include "logging.h"
30 #include "util.h"
31
32 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
33 {
34         unsigned int i, j;
35         uint16_t c;
36
37         j = 0;
38         for (i = 0; i + 2 <= count; i += 2) {
39                 if (endianess == LE)
40                         c = (buf[i+1] << 8) | buf[i];
41                 else
42                         c = (buf[i] << 8) | buf[i+1];
43                 if (c == 0) {
44                         str[j] = '\0';
45                         break;
46                 } else if (c < 0x80) {
47                         if (j+1 >= len)
48                                 break;
49                         str[j++] = (uint8_t) c;
50                 } else if (c < 0x800) {
51                         if (j+2 >= len)
52                                 break;
53                         str[j++] = (uint8_t) (0xc0 | (c >> 6));
54                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
55                 } else {
56                         if (j+3 >= len)
57                                 break;
58                         str[j++] = (uint8_t) (0xe0 | (c >> 12));
59                         str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
60                         str[j++] = (uint8_t) (0x80 | (c & 0x3f));
61                 }
62         }
63         str[j] = '\0';
64 }
65
66 static char *usage_to_string(enum volume_id_usage usage_id)
67 {
68         switch (usage_id) {
69         case VOLUME_ID_FILESYSTEM:
70                 return "filesystem";
71         case VOLUME_ID_PARTITIONTABLE:
72                 return "partitiontable";
73         case VOLUME_ID_OTHER:
74                 return "other";
75         case VOLUME_ID_RAID:
76                 return "raid";
77         case VOLUME_ID_DISKLABEL:
78                 return "disklabel";
79         case VOLUME_ID_CRYPTO:
80                 return "crypto";
81         case VOLUME_ID_UNPROBED:
82                 return "unprobed";
83         case VOLUME_ID_UNUSED:
84                 return "unused";
85         }
86         return NULL;
87 }
88
89 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
90 {
91         part->usage_id = usage_id;
92         part->usage = usage_to_string(usage_id);
93 }
94
95 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
96 {
97         id->usage_id = usage_id;
98         id->usage = usage_to_string(usage_id);
99 }
100
101 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
102 {
103         memcpy(id->label_raw, buf, count);
104         id->label_raw_len = count;
105 }
106
107 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
108 {
109         unsigned int i;
110
111         memcpy(id->label, buf, count);
112
113         /* remove trailing whitespace */
114         i = strnlen(id->label, count);
115         while (i--) {
116                 if (!isspace(id->label[i]))
117                         break;
118         }
119         id->label[i+1] = '\0';
120 }
121
122 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
123 {
124          volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
125 }
126
127 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
128 {
129         unsigned int i;
130         unsigned int count = 0;
131
132         switch(format) {
133         case UUID_DOS:
134                 count = 4;
135                 break;
136         case UUID_NTFS:
137         case UUID_HFS:
138                 count = 8;
139                 break;
140         case UUID_DCE:
141                 count = 16;
142                 break;
143         case UUID_DCE_STRING:
144                 count = 36;
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_DCE_STRING:
182                 memcpy(id->uuid, buf, count);
183                 id->uuid[count] = '\0';
184                 break;
185         }
186 }
187
188 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
189 {
190         ssize_t buf_len;
191
192         dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
193         /* check if requested area fits in superblock buffer */
194         if (off + len <= SB_BUFFER_SIZE) {
195                 if (id->sbbuf == NULL) {
196                         id->sbbuf = malloc(SB_BUFFER_SIZE);
197                         if (id->sbbuf == NULL) {
198                                 dbg("error malloc");
199                                 return NULL;
200                         }
201                 }
202
203                 /* check if we need to read */
204                 if ((off + len) > id->sbbuf_len) {
205                         dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
206                         if (lseek(id->fd, 0, SEEK_SET) < 0) {
207                                 dbg("lseek failed (%s)", strerror(errno));
208                                 return NULL;
209                         }
210                         buf_len = read(id->fd, id->sbbuf, off + len);
211                         if (buf_len < 0) {
212                                 dbg("read failed (%s)", strerror(errno));
213                                 return NULL;
214                         }
215                         dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
216                         id->sbbuf_len = buf_len;
217                         if ((size_t)buf_len < off + len) {
218                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
219                                 return NULL;
220                         }
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                                 dbg("error malloc");
235                                 return NULL;
236                         }
237                 }
238
239                 /* check if we need to read */
240                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
241                         dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
242                         if (lseek(id->fd, off, SEEK_SET) < 0) {
243                                 dbg("lseek failed (%s)", strerror(errno));
244                                 return NULL;
245                         }
246                         buf_len = read(id->fd, id->seekbuf, len);
247                         if (buf_len < 0) {
248                                 dbg("read failed (%s)", strerror(errno));
249                                 return NULL;
250                         }
251                         dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
252                         id->seekbuf_off = off;
253                         id->seekbuf_len = buf_len;
254                         if ((size_t)buf_len < len) {
255                                 dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
256                                 return NULL;
257                         }
258                 }
259
260                 return &(id->seekbuf[off - id->seekbuf_off]);
261         }
262 }
263
264 void volume_id_free_buffer(struct volume_id *id)
265 {
266         if (id->sbbuf != NULL) {
267                 free(id->sbbuf);
268                 id->sbbuf = NULL;
269                 id->sbbuf_len = 0;
270         }
271         if (id->seekbuf != NULL) {
272                 free(id->seekbuf);
273                 id->seekbuf = NULL;
274                 id->seekbuf_len = 0;
275         }
276 }