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