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
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
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)
{
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;
}