[OpenWrt-Devel,fstools,PATCH:,3/4] libblkid-tiny: adds blkid_probe_set_utf8label support
diff mbox series

Message ID 20190616170050.27126-3-ansuelsmth@gmail.com
State New
Headers show
Series
  • [OpenWrt-Devel,fstools,PATCH:,1/4] libblkid-tiny: fix wrong btrfs label length
Related show

Commit Message

Ansuel Smith June 16, 2019, 5 p.m. UTC
Currently set_utf8label support is missing. Adds a stripped down version of encode.c file from original libblkid and adds the function to libblkid-tiny.c.

Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
---
 CMakeLists.txt                |  1 +
 libblkid-tiny/encode.c        | 66 +++++++++++++++++++++++++++++++++++
 libblkid-tiny/encode.h        | 11 ++++++
 libblkid-tiny/libblkid-tiny.c | 15 ++++++++
 4 files changed, 93 insertions(+)
 create mode 100644 libblkid-tiny/encode.c
 create mode 100644 libblkid-tiny/encode.h

Patch
diff mbox series

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f86a4d5..7f13c4f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@  TARGET_LINK_LIBRARIES(fstools ubox)
 INSTALL(TARGETS fstools LIBRARY DESTINATION lib)
 
 ADD_LIBRARY(blkid-tiny SHARED
+		libblkid-tiny/encode.c
 		libblkid-tiny/libblkid-tiny.c
 		libblkid-tiny/mkdev.c
 		libblkid-tiny/ext.c
diff --git a/libblkid-tiny/encode.c b/libblkid-tiny/encode.c
new file mode 100644
index 0000000..4d8343e
--- /dev/null
+++ b/libblkid-tiny/encode.c
@@ -0,0 +1,66 @@ 
+/*
+ * encode.c - string conversion routines (mostly for compatibility with
+ *            udev/volume_id)
+ *
+ * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "encode.h"
+
+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 < count; i++) {
+		if (enc == BLKID_ENC_UTF16LE) {
+			if (i+2 > count)
+				break;
+			c = (src[i+1] << 8) | src[i];
+			i++;
+		} else if (enc == BLKID_ENC_UTF16BE) {
+			if (i+2 > count)
+				break;
+			c = (src[i] << 8) | src[i+1];
+			i++;
+		} else if (enc == BLKID_ENC_LATIN1) {
+			c = src[i];
+		} else {
+			return 0;
+		}
+		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;
+}
\ No newline at end of file
diff --git a/libblkid-tiny/encode.h b/libblkid-tiny/encode.h
new file mode 100644
index 0000000..92fcc21
--- /dev/null
+++ b/libblkid-tiny/encode.h
@@ -0,0 +1,11 @@ 
+#ifndef _ENCODE_H
+#define _ENCODE_H
+
+#define BLKID_ENC_UTF16BE	0
+#define BLKID_ENC_UTF16LE	1
+#define BLKID_ENC_LATIN1	2
+
+size_t blkid_encode_to_utf8(int enc, unsigned char *dest, size_t len,
+				const unsigned char *src, size_t count);
+
+#endif /* _ENCODE_H */
\ No newline at end of file
diff --git a/libblkid-tiny/libblkid-tiny.c b/libblkid-tiny/libblkid-tiny.c
index f020e23..a30f619 100644
--- a/libblkid-tiny/libblkid-tiny.c
+++ b/libblkid-tiny/libblkid-tiny.c
@@ -121,6 +121,21 @@  int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
 	return 0;
 }
 
+int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label,
+				size_t len, int enc)
+{
+	if (len > (sizeof(pr->label) - 1)) {
+		fprintf(stderr, "label buffer too small %d > %d\n",
+			(int) len, (int) sizeof(pr->label) - 1);
+		return -1;
+	}
+
+	blkid_encode_to_utf8(enc,(unsigned char*) pr->label, len,
+			label, len+1);
+
+	return 0;
+}
+
 int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name)
 {
 	short unsigned int*u = (short unsigned int*) uuid;