busybox/util-linux/volume_id/util.c
<<
>>
Prefs
   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
  23void 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
  58static 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
  81void 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
  87void 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
  93void 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
 101static 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
 111void 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
 126void 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
 131void 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
 161set:
 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/* Do not use xlseek here. With it, single corrupted filesystem
 194 * may result in attempt to seek past device -> exit.
 195 * It's better to ignore such fs and continue.  */
 196void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
 197{
 198        uint8_t *dst;
 199        unsigned small_off;
 200        ssize_t read_len;
 201
 202        dbg("get buffer off 0x%llx(%llu), len 0x%zx",
 203                (unsigned long long) off, (unsigned long long) off, len);
 204
 205        /* check if requested area fits in superblock buffer */
 206        if (off + len <= SB_BUFFER_SIZE
 207         /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */
 208        ) {
 209                if (id->sbbuf == NULL) {
 210                        id->sbbuf = xmalloc(SB_BUFFER_SIZE);
 211                }
 212                small_off = off;
 213                dst = id->sbbuf;
 214
 215                /* check if we need to read */
 216                len += off;
 217                if (len <= id->sbbuf_len)
 218                        goto ret; /* we already have it */
 219
 220                dbg("read sbbuf len:0x%x", (unsigned) len);
 221                id->sbbuf_len = len;
 222                off = 0;
 223                goto do_read;
 224        }
 225
 226        if (len > SEEK_BUFFER_SIZE) {
 227                dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
 228                return NULL;
 229        }
 230        dst = id->seekbuf;
 231
 232        /* check if we need to read */
 233        if ((off >= id->seekbuf_off)
 234         && ((off + len) <= (id->seekbuf_off + id->seekbuf_len))
 235        ) {
 236                small_off = off - id->seekbuf_off; /* can't overflow */
 237                goto ret; /* we already have it */
 238        }
 239
 240        id->seekbuf_off = off;
 241        id->seekbuf_len = len;
 242        id->seekbuf = xrealloc(id->seekbuf, len);
 243        small_off = 0;
 244        dst = id->seekbuf;
 245        dbg("read seekbuf off:0x%llx len:0x%zx",
 246                                (unsigned long long) off, len);
 247 do_read:
 248        if (lseek(id->fd, off, SEEK_SET) != off) {
 249                dbg("seek(0x%llx) failed", (unsigned long long) off);
 250                goto err;
 251        }
 252        read_len = full_read(id->fd, dst, len);
 253        if (read_len != len) {
 254                dbg("requested 0x%x bytes, got 0x%x bytes",
 255                                (unsigned) len, (unsigned) read_len);
 256 err:
 257                /* No filesystem can be this tiny. It's most likely
 258                 * non-associated loop device, empty drive and so on.
 259                 * Flag it, making it possible to short circuit future
 260                 * accesses. Rationale:
 261                 * users complained of slow blkid due to empty floppy drives.
 262                 */
 263                if (off < 64*1024)
 264                        id->error = 1;
 265                /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */
 266                volume_id_free_buffer(id);
 267                return NULL;
 268        }
 269 ret:
 270        return dst + small_off;
 271}
 272
 273void volume_id_free_buffer(struct volume_id *id)
 274{
 275        free(id->sbbuf);
 276        id->sbbuf = NULL;
 277        id->sbbuf_len = 0;
 278        free(id->seekbuf);
 279        id->seekbuf = NULL;
 280        id->seekbuf_len = 0;
 281        id->seekbuf_off = 0; /* paranoia */
 282}
 283