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