From: Karel Zak Date: Tue, 15 Sep 2009 18:45:47 +0000 (+0200) Subject: libblkid: create a generic blkid_encode_to_utf8() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6db756a54898e51d447587692a0e1532e474edcd;p=util-linux libblkid: create a generic blkid_encode_to_utf8() Signed-off-by: Karel Zak --- diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index c12fdb13..4ad76bd1 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -340,6 +340,9 @@ extern int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const ch extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len); +extern size_t blkid_encode_to_utf8(int enc, unsigned char *dest, size_t len, + const unsigned char *src, size_t count); + #define BLKID_ENC_UTF16BE 0 #define BLKID_ENC_UTF16LE 1 diff --git a/shlibs/blkid/src/encode.c b/shlibs/blkid/src/encode.c index 0317be1f..38c5d231 100644 --- a/shlibs/blkid/src/encode.c +++ b/shlibs/blkid/src/encode.c @@ -227,6 +227,41 @@ static int replace_chars(char *str, const char *white) return replaced; } +size_t blkid_encode_to_utf8(int enc, unsigned char *dest, size_t len, + const unsigned char *src, size_t count) +{ + size_t i, j; + uint16_t c; + + for (j = i = 0; i + 2 <= count; i += 2) { + if (enc == BLKID_ENC_UTF16LE) + c = (src[i+1] << 8) | src[i]; + else /* BLKID_ENC_UTF16BE */ + c = (src[i] << 8) | src[i+1]; + if (c == 0) { + dest[j] = '\0'; + break; + } else if (c < 0x80) { + if (j+1 >= len) + break; + dest[j++] = (uint8_t) c; + } else if (c < 0x800) { + if (j+2 >= len) + break; + dest[j++] = (uint8_t) (0xc0 | (c >> 6)); + dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); + } else { + if (j+3 >= len) + break; + dest[j++] = (uint8_t) (0xe0 | (c >> 12)); + dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); + dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); + } + } + dest[j] = '\0'; + return j; +} + /** * blkid_encode_string: * @str: input string to be encoded diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index 1b7d4b8b..b9d67add 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -717,41 +717,6 @@ int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len) return 0; } -static size_t encode_to_utf8(int enc, unsigned char *dest, size_t len, - unsigned char *src, size_t count) -{ - size_t i, j; - uint16_t c; - - for (j = i = 0; i + 2 <= count; i += 2) { - if (enc == BLKID_ENC_UTF16LE) - c = (src[i+1] << 8) | src[i]; - else /* BLKID_ENC_UTF16BE */ - c = (src[i] << 8) | src[i+1]; - if (c == 0) { - dest[j] = '\0'; - break; - } else if (c < 0x80) { - if (j+1 >= len) - break; - dest[j++] = (uint8_t) c; - } else if (c < 0x800) { - if (j+2 >= len) - break; - dest[j++] = (uint8_t) (0xc0 | (c >> 6)); - dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); - } else { - if (j+3 >= len) - break; - dest[j++] = (uint8_t) (0xe0 | (c >> 12)); - dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); - dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); - } - } - dest[j] = '\0'; - return j; -} - int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, size_t len, int enc) { @@ -766,7 +731,7 @@ int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, if (!v) return -1; - v->len = encode_to_utf8(enc, v->data, sizeof(v->data), label, len); + v->len = blkid_encode_to_utf8(enc, v->data, sizeof(v->data), label, len); return 0; }