• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff --git a/misc/blkid.c b/misc/blkid.c
2index e46efc707daaf5867bea6cd415b9637e6c5bba61..7b7e015ec66e1800b0e9691aa7f23495a7de66fe 100644
3--- a/misc/blkid.c
4+++ b/misc/blkid.c
5@@ -37,6 +37,9 @@ extern int optind;
6
7 #include "ext2fs/ext2fs.h"
8 #include "blkid/blkid.h"
9+#include "blkidP.h"
10+#include "iconv.h"
11+#include "securec.h"
12
13 static const char *progname = "blkid";
14
15@@ -228,6 +231,54 @@ static void pretty_print_dev(blkid_dev dev)
16 	pretty_print_line(devname, fs_type, label, mtpt, uuid);
17 }
18
19+static int is_str_utf8(const char* str)
20+{
21+	unsigned int nBytes = 0;
22+	unsigned char chr = *str;
23+	int bAllAscii = 1;
24+
25+	for (unsigned int i = 0; str[i] != '\0'; ++i) {
26+		chr = *(str + i);
27+		if ((chr & 0x80) != 0)
28+			bAllAscii = 0;
29+		if(nBytes == 0 && ((chr & 0x80) != 0)) {
30+			while((chr & 0x80) != 0) {
31+				chr <<= 1;
32+				nBytes ++;
33+			}
34+			if((nBytes < 2) || (nBytes > 6)) {
35+				return 0;
36+			}
37+			nBytes --;
38+		} else if (nBytes != 0) {
39+			if((chr & 0xc0) != 0x80) {
40+				return 0;
41+			}
42+			nBytes --;
43+		}
44+	}
45+	if (nBytes && !bAllAscii)  {
46+		return 0;
47+	}
48+
49+	return 1;
50+}
51+
52+
53+static int code_convert(char *from_charset, char *to_charset,char *inbuf, size_t inlen, char *outbuf, size_t outlen)
54+{
55+	iconv_t cd;
56+	char **pin = &inbuf;
57+	char **pout = &outbuf;
58+
59+	cd = iconv_open(to_charset, from_charset);
60+	if (cd == 0) return -1;
61+	if (memset_s(outbuf, outlen, 0, outlen) != EOK) return -1;
62+	if (iconv(cd, pin, &inlen, pout, &outlen) == (size_t)-1) return -1;
63+	iconv_close(cd);
64+	return 0;
65+}
66+
67 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
68 {
69 	blkid_tag_iterate	iter;
70@@ -257,7 +308,17 @@ static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
71 				continue;
72 		}
73 		if (output & OUTPUT_VALUE_ONLY) {
74-			fputs(value, stdout);
75+			if (!strncmp(type, "LABEL", 5) && !strncmp(dev->bid_type, "vfat", 4) && !is_str_utf8(value)) {
76+				char outbuf[255];
77+				int res = code_convert("gbk","utf-8", (char *)value, strlen(value), outbuf, 255);
78+				if (!res) {
79+					fputs(outbuf, stdout);
80+				} else {
81+					fputs(value, stdout);
82+				}
83+			} else {
84+				fputs(value, stdout);
85+			}
86 			fputc('\n', stdout);
87 		} else {
88 			if (first) {
89