util.c revision 2603474ff9be2418900581da1af5cccc9f13cae1
/*
* volume_id - reads filesystem label and uuid
*
* Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
*
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include "libvolume_id.h"
#include "libvolume_id-private.h"
#include "util.h"
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str)
{
unsigned char c = (unsigned char)str[0];
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
return 2;
if ((c & 0xf0) == 0xe0)
return 3;
if ((c & 0xf8) == 0xf0)
return 4;
if ((c & 0xfc) == 0xf8)
return 5;
if ((c & 0xfe) == 0xfc)
return 6;
return 0;
}
/* decode one unicode char */
static int utf8_encoded_to_unichar(const char *str)
{
int unichar;
int len;
int i;
switch (len) {
case 1:
return (int)str[0];
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
return -1;
}
for (i = 1; i < len; i++) {
return -1;
unichar <<= 6;
}
return unichar;
}
/* expected size used to encode one unicode char */
static int utf8_unichar_to_encoded_len(int unichar)
{
if (unichar < 0x80)
return 1;
if (unichar < 0x800)
return 2;
if (unichar < 0x10000)
return 3;
if (unichar < 0x200000)
return 4;
if (unichar < 0x4000000)
return 5;
return 6;
}
/* check if unicode char has a valid numeric range */
static int utf8_unichar_valid_range(int unichar)
{
if (unichar > 0x10ffff)
return 0;
return 0;
return 0;
return 0;
return 1;
}
/* validate one encoded unicode char and return its length */
int volume_id_utf8_encoded_valid_unichar(const char *str)
{
int len;
int unichar;
int i;
if (len == 0)
return -1;
/* ascii is valid */
if (len == 1)
return 1;
/* check if expected encoded chars are available */
for (i = 0; i < len; i++)
return -1;
/* check if encoded length matches encoded value */
return -1;
/* check if value has valid range */
if (!utf8_unichar_valid_range(unichar))
return -1;
return len;
}
size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
{
size_t i, j;
uint16_t c;
j = 0;
else
if (c == 0) {
str[j] = '\0';
break;
} else if (c < 0x80) {
if (j+1 >= len)
break;
} else if (c < 0x800) {
if (j+2 >= len)
break;
} else {
if (j+3 >= len)
break;
}
}
str[j] = '\0';
return j;
}
{
switch (usage_id) {
case VOLUME_ID_FILESYSTEM:
return "filesystem";
case VOLUME_ID_OTHER:
return "other";
case VOLUME_ID_RAID:
return "raid";
case VOLUME_ID_DISKLABEL:
return "disklabel";
case VOLUME_ID_CRYPTO:
return "crypto";
case VOLUME_ID_UNPROBED:
return "unprobed";
case VOLUME_ID_UNUSED:
return "unused";
}
return NULL;
}
{
}
{
}
{
size_t i;
/* remove trailing whitespace */
while (i--) {
break;
}
}
void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
{
}
void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format)
{
unsigned int i;
unsigned int count = 0;
switch(format) {
case UUID_STRING:
break;
case UUID_HEX_STRING:
break;
case UUID_DOS:
count = 4;
break;
case UUID_64BIT_LE:
case UUID_64BIT_BE:
count = 8;
break;
case UUID_DCE:
count = 16;
break;
case UUID_MD:
count = 35;
break;
case UUID_LVM:
count = 32;
break;
}
/* if set, create string in the same format, the native platform uses */
for (i = 0; i < count; i++)
if (buf[i] != 0)
goto set;
return;
set:
switch(format) {
case UUID_DOS:
break;
case UUID_64BIT_LE:
break;
case UUID_64BIT_BE:
break;
case UUID_DCE:
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
break;
case UUID_HEX_STRING:
/* translate A..F to a..f */
for (i = 0; i < count; i++)
break;
case UUID_STRING:
break;
case UUID_MD:
"%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x",
break;
case UUID_LVM:
"%c%c%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c%c%c",
break;
}
}
{
info("get buffer off 0x%llx(%llu), len 0x%zx\n", (unsigned long long) off, (unsigned long long) off, len);
/* check if requested area fits in superblock buffer */
dbg("error malloc\n");
return NULL;
}
}
/* check if we need to read */
return NULL;
}
if (buf_len < 0) {
return NULL;
}
return NULL;
}
}
} else {
if (len > SEEK_BUFFER_SIZE) {
return NULL;
}
/* get seek buffer */
dbg("error malloc\n");
return NULL;
}
}
/* check if we need to read */
return NULL;
}
if (buf_len < 0) {
return NULL;
}
return NULL;
}
}
}
}
{
}
id->seekbuf_len = 0;
}
}