All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 17/21] vvfat: correctly parse non-ASCII short and long file names
Date: Tue, 18 Jul 2017 16:18:02 +0200	[thread overview]
Message-ID: <1500387486-5469-18-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1500387486-5469-1-git-send-email-kwolf@redhat.com>

From: Hervé Poussineau <hpoussin@reactos.org>

Write support works again when image contains non-ASCII names. It is either the
case when user created a non-ASCII filename, or when initial directory contained
a non-ASCII filename (since 0c36111f57ec2188f679e7fa810291b7386bdca1)

Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/vvfat.c | 59 +++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 22 deletions(-)

diff --git a/block/vvfat.c b/block/vvfat.c
index 36b4be9..ea7775f 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -1669,6 +1669,7 @@ typedef struct {
      * filename length is 0x3f * 13 bytes.
      */
     unsigned char name[0x3f * 13 + 1];
+    gunichar2 name2[0x3f * 13 + 1];
     int checksum, len;
     int sequence_number;
 } long_file_name;
@@ -1690,16 +1691,21 @@ static int parse_long_name(long_file_name* lfn,
         return 1;
 
     if (pointer[0] & 0x40) {
+        /* first entry; do some initialization */
         lfn->sequence_number = pointer[0] & 0x3f;
         lfn->checksum = pointer[13];
         lfn->name[0] = 0;
         lfn->name[lfn->sequence_number * 13] = 0;
-    } else if ((pointer[0] & 0x3f) != --lfn->sequence_number)
+    } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
+        /* not the expected sequence number */
         return -1;
-    else if (pointer[13] != lfn->checksum)
+    } else if (pointer[13] != lfn->checksum) {
+        /* not the expected checksum */
         return -2;
-    else if (pointer[12] || pointer[26] || pointer[27])
+    } else if (pointer[12] || pointer[26] || pointer[27]) {
+        /* invalid zero fields */
         return -3;
+    }
 
     offset = 13 * (lfn->sequence_number - 1);
     for (i = 0, j = 1; i < 13; i++, j+=2) {
@@ -1708,16 +1714,29 @@ static int parse_long_name(long_file_name* lfn,
         else if (j == 26)
             j = 28;
 
-        if (pointer[j+1] == 0)
-            lfn->name[offset + i] = pointer[j];
-        else if (pointer[j+1] != 0xff || (pointer[0] & 0x40) == 0)
-            return -4;
-        else
-            lfn->name[offset + i] = 0;
+        if (pointer[j] == 0 && pointer[j + 1] == 0) {
+            /* end of long file name */
+            break;
+        }
+        gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
+        lfn->name2[offset + i] = c;
     }
 
-    if (pointer[0] & 0x40)
-        lfn->len = offset + strlen((char*)lfn->name + offset);
+    if (pointer[0] & 0x40) {
+        /* first entry; set len */
+        lfn->len = offset + i;
+    }
+    if ((pointer[0] & 0x3f) == 0x01) {
+        /* last entry; finalize entry */
+        glong olen;
+        gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
+        if (!utf8) {
+            return -4;
+        }
+        lfn->len = olen;
+        memcpy(lfn->name, utf8, olen + 1);
+        g_free(utf8);
+    }
 
     return 0;
 }
@@ -1733,12 +1752,14 @@ static int parse_short_name(BDRVVVFATState* s,
 
     for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
     for (i = 0; i <= j; i++) {
-        if (direntry->name[i] <= ' ' || direntry->name[i] > 0x7f)
+        uint8_t c = direntry->name[i];
+        if (c != to_valid_short_char(c)) {
             return -1;
-        else if (s->downcase_short_names)
+        } else if (s->downcase_short_names) {
             lfn->name[i] = qemu_tolower(direntry->name[i]);
-        else
+        } else {
             lfn->name[i] = direntry->name[i];
+        }
     }
 
     for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
@@ -1748,7 +1769,7 @@ static int parse_short_name(BDRVVVFATState* s,
         lfn->name[i + j + 1] = '\0';
         for (;j >= 0; j--) {
             uint8_t c = direntry->name[8 + j];
-            if (c <= ' ' || c > 0x7f) {
+            if (c != to_valid_short_char(c)) {
                 return -2;
             } else if (s->downcase_short_names) {
                 lfn->name[i + j] = qemu_tolower(c);
@@ -2966,7 +2987,6 @@ DLOG(checkpoint());
     /*
      * Some sanity checks:
      * - do not allow writing to the boot sector
-     * - do not allow to write non-ASCII filenames
      */
 
     if (sector_num < s->offset_to_fat)
@@ -3000,13 +3020,8 @@ DLOG(checkpoint());
                 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
 
                 for (k = 0; k < (end - begin) * 0x10; k++) {
-                    /* do not allow non-ASCII filenames */
-                    if (parse_long_name(&lfn, direntries + k) < 0) {
-                        fprintf(stderr, "Warning: non-ASCII filename\n");
-                        return -1;
-                    }
                     /* no access to the direntry of a read-only file */
-                    else if (is_short_name(direntries+k) &&
+                    if (is_short_name(direntries + k) &&
                             (direntries[k].attributes & 1)) {
                         if (memcmp(direntries + k,
                                     array_get(&(s->directory), dir_index + k),
-- 
1.8.3.1

  parent reply	other threads:[~2017-07-18 14:18 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-18 14:17 [Qemu-devel] [PULL 00/21] Block layer patches Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 01/21] commit: Add NULL check for overlay_bs Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 02/21] block: add clock_type field to ThrottleGroup Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 03/21] block: remove timer canceling in throttle_config() Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 04/21] block/vmdk: Report failures in vmdk_read_cid() Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 05/21] block/vpc.c: Handle write failures in get_image_offset() Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 06/21] block: Make blk_get_attached_dev_id() public Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 07/21] block/qapi: Add qdev device name to query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 08/21] block: Make blk_all_next() public Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 09/21] block/qapi: Use blk_all_next() for query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 10/21] block: List anonymous device BBs in query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 11/21] ide: bdrv_attach_dev() for empty CD-ROM Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 12/21] scsi-disk: " Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 13/21] qemu-iotests: Test 'info block' Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 14/21] qemu-iotests: Test unplug of -device without drive Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 15/21] vvfat: add constants for special values of name[0] Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 16/21] vvfat: add a constant for bootsector name Kevin Wolf
2017-07-18 14:18 ` Kevin Wolf [this message]
2017-07-18 14:18 ` [Qemu-devel] [PULL 18/21] vvfat: initialize memory after allocating it Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 19/21] block/vvfat: Fix compiler warning with gcc 7 Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 20/21] blockdev: move BDRV_O_NO_BACKING option forward Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 21/21] qemu-img: Check for backing image if specified during create Kevin Wolf
2017-07-18 18:57 ` [Qemu-devel] [PULL 00/21] Block layer patches no-reply
2017-07-19  6:11   ` Kevin Wolf
2017-07-18 21:23 ` no-reply
2017-07-19 11:28 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1500387486-5469-18-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.