]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: move UTF encoding function to lib/
authorKarel Zak <kzak@redhat.com>
Tue, 25 Feb 2020 11:00:27 +0000 (12:00 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 25 Feb 2020 11:00:27 +0000 (12:00 +0100)
Let's consolidate the code, we need to use it in libfdisk too. It
seems better to keep it generic and libblkid independent.

This patch also removes blkid_encode_alloc(), this function is overkill.

Signed-off-by: Karel Zak <kzak@redhat.com>
15 files changed:
include/Makemodule.am
include/encode.h [new file with mode: 0644]
lib/Makemodule.am
lib/encode.c [new file with mode: 0644]
libblkid/src/blkidP.h
libblkid/src/encode.c
libblkid/src/partitions/gpt.c
libblkid/src/partitions/partitions.c
libblkid/src/superblocks/exfat.c
libblkid/src/superblocks/f2fs.c
libblkid/src/superblocks/hfs.c
libblkid/src/superblocks/iso9660.c
libblkid/src/superblocks/ntfs.c
libblkid/src/superblocks/superblocks.c
libblkid/src/superblocks/udf.c

index c55c262c9bf9dfa7c1d28d13dee6c0a97c34278a..3f8b6233f4e6aca0d6dda75691e2914692869713 100644 (file)
@@ -16,6 +16,7 @@ dist_noinst_HEADERS += \
        include/crc32c.h \
        include/debug.h \
        include/debugobj.h \
+       include/encode.h \
        include/env.h \
        include/exec_shell.h \
        include/exitcodes.h \
diff --git a/include/encode.h b/include/encode.h
new file mode 100644 (file)
index 0000000..b259ab5
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef UTIL_LINUX_ENCODE_H
+#define UTIL_LINUX_ENCODE_H
+
+extern size_t ul_encode_to_utf8(int enc, unsigned char *dest, size_t len,
+                               const unsigned char *src, size_t count)
+                       __attribute__((nonnull));
+
+enum {
+       UL_ENCODE_UTF16BE = 0,
+       UL_ENCODE_UTF16LE,
+       UL_ENCODE_LATIN1
+};
+
+#endif
index b4d0fba2bffbd71282e1e9d037b240b4bbb9f653..16a77e3b4ab117fa9be992575454217896b6ab2f 100644 (file)
@@ -8,6 +8,7 @@ libcommon_la_SOURCES = \
        lib/crc32c.c \
        lib/env.c \
        lib/idcache.c \
+       lib/encode.c \
        lib/fileutils.c \
        lib/ismounted.c \
        lib/color-names.c \
diff --git a/lib/encode.c b/lib/encode.c
new file mode 100644 (file)
index 0000000..bee5bd5
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Based on code from libblkid,
+ *
+ * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
+ * Copyright (C) 2020 Pali Rohár <pali.rohar@gmail.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#include "c.h"
+#include "encode.h"
+
+size_t ul_encode_to_utf8(int enc, unsigned char *dest, size_t len,
+                       const unsigned char *src, size_t count)
+{
+       size_t i, j;
+       uint32_t c;
+       uint16_t c2;
+
+       for (j = i = 0; i < count; i++) {
+               if (enc == UL_ENCODE_UTF16LE) {
+                       if (i+2 > count)
+                               break;
+                       c = (src[i+1] << 8) | src[i];
+                       i++;
+               } else if (enc == UL_ENCODE_UTF16BE) {
+                       if (i+2 > count)
+                               break;
+                       c = (src[i] << 8) | src[i+1];
+                       i++;
+               } else if (enc == UL_ENCODE_LATIN1) {
+                       c = src[i];
+               } else {
+                       return 0;
+               }
+               if ((enc == UL_ENCODE_UTF16LE || enc == UL_ENCODE_UTF16BE) &&
+                   c >= 0xD800 && c <= 0xDBFF && i+2 < count) {
+                       if (enc == UL_ENCODE_UTF16LE)
+                               c2 = (src[i+2] << 8) | src[i+1];
+                       else
+                               c2 = (src[i+1] << 8) | src[i+2];
+                       if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
+                               c = 0x10000 + ((c - 0xD800) << 10) + (c2 - 0xDC00);
+                               i += 2;
+                       }
+               }
+               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 (c < 0x10000) {
+                       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));
+               } else {
+                       if (j+4 >= len)
+                               break;
+                       dest[j++] = (uint8_t) (0xf0 | (c >> 18));
+                       dest[j++] = (uint8_t) (0x80 | ((c >> 12) & 0x3f));
+                       dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
+                       dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
+               }
+       }
+       dest[j] = '\0';
+       return j;
+}
index f9bbe008406f40e96da36bf5143b8bb9ea8e39db..0660c997b58efea4ca9dcddfde7104b87cd4ad11 100644 (file)
@@ -34,6 +34,7 @@
 #include "debug.h"
 #include "blkid.h"
 #include "list.h"
+#include "encode.h"
 
 /*
  * This describes the attributes of a specific device.
@@ -543,14 +544,4 @@ extern void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size)
 #define blkid_bmp_nbytes(max_items) \
                (blkid_bmp_nwords(max_items) * sizeof(unsigned long))
 
-/* encode.c */
-extern unsigned char *blkid_encode_alloc(size_t count, size_t *reslen);
-extern size_t blkid_encode_to_utf8(int enc, unsigned char *dest, size_t len,
-                               const unsigned char *src, size_t count)
-                       __attribute__((nonnull));
-
-#define BLKID_ENC_UTF16BE      0
-#define BLKID_ENC_UTF16LE      1
-#define BLKID_ENC_LATIN1       2
-
 #endif /* _BLKID_BLKIDP_H */
index 36ad1c9569f4cebda516535e3c023e8b797bd61e..9c2220428b00518e23c9df79d0befcb71fb3f28f 100644 (file)
@@ -233,77 +233,6 @@ 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;
-       uint32_t c;
-       uint16_t c2;
-
-       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 ((enc == BLKID_ENC_UTF16LE || enc == BLKID_ENC_UTF16BE) &&
-                   c >= 0xD800 && c <= 0xDBFF && i+2 < count) {
-                       if (enc == BLKID_ENC_UTF16LE)
-                               c2 = (src[i+2] << 8) | src[i+1];
-                       else
-                               c2 = (src[i+1] << 8) | src[i+2];
-                       if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
-                               c = 0x10000 + ((c - 0xD800) << 10) + (c2 - 0xDC00);
-                               i += 2;
-                       }
-               }
-               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 (c < 0x10000) {
-                       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));
-               } else {
-                       if (j+4 >= len)
-                               break;
-                       dest[j++] = (uint8_t) (0xf0 | (c >> 18));
-                       dest[j++] = (uint8_t) (0x80 | ((c >> 12) & 0x3f));
-                       dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
-                       dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
-               }
-       }
-       dest[j] = '\0';
-       return j;
-}
-
-unsigned char *blkid_encode_alloc(size_t count, size_t *reslen)
-{
-       *reslen = (count * 3) + 1;
-       return calloc(1, *reslen);
-}
-
 /**
  * blkid_encode_string:
  * @str: input string to be encoded
index 52704ed63aa9e509794a54248ed22840bba44345..3ca64183c9a1968c6d4cc0fd534aa33861e0fbc9 100644 (file)
@@ -396,7 +396,7 @@ static int probe_gpt_pt(blkid_probe pr,
 
                blkid_partition_set_utf8name(par,
                        (unsigned char *) e->partition_name,
-                       sizeof(e->partition_name), BLKID_ENC_UTF16LE);
+                       sizeof(e->partition_name), UL_ENCODE_UTF16LE);
 
                guid = e->unique_partition_guid;
                swap_efi_guid(&guid);
index f12638e76488669754acb70378dcb92bd1022916..5bbf45cf1f5ac397999595c60c5beb2ca17b3daa 100644 (file)
@@ -1357,7 +1357,7 @@ int blkid_partition_set_utf8name(blkid_partition par, const unsigned char *name,
        if (!par)
                return -1;
 
-       blkid_encode_to_utf8(enc, par->name, sizeof(par->name), name, len);
+       ul_encode_to_utf8(enc, par->name, sizeof(par->name), name, len);
        blkid_rtrim_whitespace(par->name);
        return 0;
 }
index 62fb8ce0b86f6fed4b735b6cb87f1b1ecf8d6927..ca7d6c813abb810fc103b3f5997a533c0964eaee 100644 (file)
@@ -127,7 +127,7 @@ static int probe_exfat(blkid_probe pr, const struct blkid_idmag *mag)
        if (label)
                blkid_probe_set_utf8label(pr, label->name,
                                min(label->length * 2, sizeof(label->name)),
-                               BLKID_ENC_UTF16LE);
+                               UL_ENCODE_UTF16LE);
        else if (errno)
                return -errno;
 
index 255ef6384fc4f4cc5da80f892ea2042c861590ac..aed93e25b3e7fbe6927d4a537f1f663afacc4d88 100644 (file)
@@ -74,7 +74,7 @@ static int probe_f2fs(blkid_probe pr, const struct blkid_idmag *mag)
        if (*((unsigned char *) sb->volume_name))
                blkid_probe_set_utf8label(pr, (unsigned char *) sb->volume_name,
                                                sizeof(sb->volume_name),
-                                               BLKID_ENC_UTF16LE);
+                                               UL_ENCODE_UTF16LE);
 
        blkid_probe_set_uuid(pr, sb->uuid);
        blkid_probe_sprintf_version(pr, "%u.%u", vermaj, vermin);
index e537bbbd3cb1480acc65bfecdfb88e14d7097d40..ebf90e49e6ebfd92e5359b19257f63c1d2142761 100644 (file)
@@ -305,7 +305,7 @@ static int probe_hfsplus(blkid_probe pr, const struct blkid_idmag *mag)
 
        blkid_probe_set_utf8label(pr, key->unicode,
                        be16_to_cpu(key->unicode_len) * 2,
-                       BLKID_ENC_UTF16BE);
+                       UL_ENCODE_UTF16BE);
        return 0;
 }
 
index 8dc2e53948f7840ab72dee88dbeb49308d617bbf..1057da8614f62e383a368f0c08477024995d2c55 100644 (file)
@@ -246,7 +246,7 @@ static int probe_iso9660(blkid_probe pr, const struct blkid_idmag *mag)
                        blkid_probe_set_utf8label(pr,
                                        iso->volume_id,
                                        sizeof(iso->volume_id),
-                                       BLKID_ENC_UTF16BE);
+                                       UL_ENCODE_UTF16BE);
                        goto has_label;
                }
                off += ISO_SECTOR_SIZE;
index 0e6f6b748b33ee7336332a54df655883a12d6aaf..02487e21981b16dad50834741b53fed951fd9b9b 100644 (file)
@@ -201,7 +201,7 @@ static int __probe_ntfs(blkid_probe pr, const struct blkid_idmag *mag, int save_
 
                        if (attr_off + val_off + val_len <= mft_record_size)
                                blkid_probe_set_utf8label(pr, val, val_len,
-                                                         BLKID_ENC_UTF16LE);
+                                                         UL_ENCODE_UTF16LE);
                        break;
                }
 
index 93d5abd4c7655ce07171a73cbf36012a8c2083db..baf35e51b575dc80d61135e54d0114181d8065f3 100644 (file)
@@ -625,12 +625,13 @@ int blkid_probe_set_utf8_id_label(blkid_probe pr, const char *name,
        if (!v)
                return -ENOMEM;
 
-       v->data = blkid_encode_alloc(len, &v->len);
+       v->len = (len * 3) + 1;
+       v->data = calloc(1, v->len);
        if (!v->data)
                rc = -ENOMEM;
 
        if (!rc) {
-               blkid_encode_to_utf8(enc, v->data, v->len, data, len);
+               ul_encode_to_utf8(enc, v->data, v->len, data, len);
                v->len = blkid_rtrim_whitespace(v->data) + 1;
                if (v->len > 1)
                        v->len = blkid_ltrim_whitespace(v->data) + 1;
@@ -688,11 +689,12 @@ int blkid_probe_set_utf8label(blkid_probe pr, const unsigned char *label,
        if (!v)
                return -ENOMEM;
 
-       v->data = blkid_encode_alloc(len, &v->len);
+       v->len = (len * 3) + 1;
+       v->data = calloc(1, v->len);
        if (!v->data)
                rc = -ENOMEM;
        if (!rc) {
-               blkid_encode_to_utf8(enc, v->data, v->len, label, len);
+               ul_encode_to_utf8(enc, v->data, v->len, label, len);
                v->len = blkid_rtrim_whitespace(v->data) + 1;
                if (v->len > 1)
                        return 0;
index c27debd29d9808f32540857d836028ab065a1577..a8f0099642e5709d492dd4760810c33c1b87a970 100644 (file)
@@ -19,7 +19,7 @@
 
 #include "superblocks.h"
 
-#define udf_cid_to_enc(cid) ((cid) == 8 ? BLKID_ENC_LATIN1 : (cid) == 16 ? BLKID_ENC_UTF16BE : -1)
+#define udf_cid_to_enc(cid) ((cid) == 8 ? UL_ENCODE_LATIN1 : (cid) == 16 ? UL_ENCODE_UTF16BE : -1)
 
 struct dstring128 {
        uint8_t cid;
@@ -144,7 +144,7 @@ static inline int gen_uuid_from_volset_id(unsigned char uuid[17], struct dstring
        if (enc == -1)
                return -1;
 
-       len = blkid_encode_to_utf8(enc, buf, sizeof(buf), volset_id->c, clen);
+       len = ul_encode_to_utf8(enc, buf, sizeof(buf), volset_id->c, clen);
        if (len < 8)
                return -1;