vfat: deduplicate hex2bin()
authorOGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fri, 8 Sep 2017 23:16:50 +0000 (16:16 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 9 Sep 2017 01:26:50 +0000 (18:26 -0700)
We may use hex2bin() instead of custom approach.

Link: http://lkml.kernel.org/r/87zibktpil.fsf@devron
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/fat/namei_vfat.c

index 6a7152d0c2503944d5e870ca8d3b78eb159b1003..02c066663a3a8329500836933383cc45bcd2e372 100644 (file)
@@ -19,6 +19,8 @@
 #include <linux/ctype.h>
 #include <linux/slab.h>
 #include <linux/namei.h>
+#include <linux/kernel.h>
+
 #include "fat.h"
 
 static inline unsigned long vfat_d_version(struct dentry *dentry)
@@ -510,10 +512,8 @@ xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
             struct nls_table *nls)
 {
        const unsigned char *ip;
-       unsigned char nc;
        unsigned char *op;
-       unsigned int ec;
-       int i, k, fill;
+       int i, fill;
        int charlen;
 
        if (utf8) {
@@ -530,33 +530,22 @@ xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
                         i < len && *outlen < FAT_LFN_LEN;
                         *outlen += 1) {
                        if (escape && (*ip == ':')) {
+                               u8 uc[2];
+
                                if (i > len - 5)
                                        return -EINVAL;
-                               ec = 0;
-                               for (k = 1; k < 5; k++) {
-                                       nc = ip[k];
-                                       ec <<= 4;
-                                       if (nc >= '0' && nc <= '9') {
-                                               ec |= nc - '0';
-                                               continue;
-                                       }
-                                       if (nc >= 'a' && nc <= 'f') {
-                                               ec |= nc - ('a' - 10);
-                                               continue;
-                                       }
-                                       if (nc >= 'A' && nc <= 'F') {
-                                               ec |= nc - ('A' - 10);
-                                               continue;
-                                       }
+
+                               if (hex2bin(uc, ip + 1, 2) < 0)
                                        return -EINVAL;
-                               }
-                               *op++ = ec & 0xFF;
-                               *op++ = ec >> 8;
+
+                               *(wchar_t *)op = uc[0] << 8 | uc[1];
+
+                               op += 2;
                                ip += 5;
                                i += 5;
                        } else {
                                charlen = nls->char2uni(ip, len - i,
-                                                                       (wchar_t *)op);
+                                                       (wchar_t *)op);
                                if (charlen < 0)
                                        return -EINVAL;
                                ip += charlen;