Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/fs change wget_utf8() to not require NUL-terminated stri...



details:   https://anonhg.NetBSD.org/src/rev/e40fc4c5a47b
branches:  trunk
changeset: 572256:e40fc4c5a47b
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Mon Dec 27 18:14:36 2004 +0000

description:
change wget_utf8() to not require NUL-terminated string as input (added
'size' parameter), and adjust callers appropriately

diffstat:

 sys/fs/ntfs/ntfs.h      |   4 ++--
 sys/fs/ntfs/ntfs_conv.c |   8 ++++----
 sys/fs/ntfs/ntfs_subr.c |  25 ++++++++++++-------------
 sys/fs/unicode.h        |  17 ++++++++---------
 4 files changed, 26 insertions(+), 28 deletions(-)

diffs (174 lines):

diff -r 5f0e5df7c0d9 -r e40fc4c5a47b sys/fs/ntfs/ntfs.h
--- a/sys/fs/ntfs/ntfs.h        Mon Dec 27 17:21:46 2004 +0000
+++ b/sys/fs/ntfs/ntfs.h        Mon Dec 27 18:14:36 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ntfs.h,v 1.7 2003/06/29 22:31:10 fvdl Exp $    */
+/*     $NetBSD: ntfs.h,v 1.8 2004/12/27 18:14:36 jdolecek Exp $        */
 
 /*-
  * Copyright (c) 1998, 1999 Semen Ustimenko
@@ -242,7 +242,7 @@
 
 #pragma pack()
 
-typedef wchar (ntfs_wget_func_t) __P((const char **));
+typedef wchar (ntfs_wget_func_t) __P((const char **, size_t *));
 typedef int (ntfs_wput_func_t) __P((char *, size_t, wchar));
 typedef int (ntfs_wcmp_func_t) __P((wchar, wchar));
 
diff -r 5f0e5df7c0d9 -r e40fc4c5a47b sys/fs/ntfs/ntfs_conv.c
--- a/sys/fs/ntfs/ntfs_conv.c   Mon Dec 27 17:21:46 2004 +0000
+++ b/sys/fs/ntfs/ntfs_conv.c   Mon Dec 27 18:14:36 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ntfs_conv.c,v 1.3 2004/11/21 16:29:57 jdolecek Exp $   */
+/*     $NetBSD: ntfs_conv.c,v 1.4 2004/12/27 18:14:36 jdolecek Exp $   */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ntfs_conv.c,v 1.3 2004/11/21 16:29:57 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ntfs_conv.c,v 1.4 2004/12/27 18:14:36 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -74,9 +74,9 @@
  * and return the character.
  */
 wchar
-ntfs_utf8_wget(const char **str)
+ntfs_utf8_wget(const char **str, size_t *sz)
 {
-       return (wchar) wget_utf8(str);
+       return (wchar) wget_utf8(str, sz);
 }
 
 /*
diff -r 5f0e5df7c0d9 -r e40fc4c5a47b sys/fs/ntfs/ntfs_subr.c
--- a/sys/fs/ntfs/ntfs_subr.c   Mon Dec 27 17:21:46 2004 +0000
+++ b/sys/fs/ntfs/ntfs_subr.c   Mon Dec 27 18:14:36 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ntfs_subr.c,v 1.10 2004/06/24 16:52:03 drochner Exp $  */
+/*     $NetBSD: ntfs_subr.c,v 1.11 2004/12/27 18:14:36 jdolecek Exp $  */
 
 /*-
  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu%FreeBSD.org@localhost)
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.10 2004/06/24 16:52:03 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.11 2004/12/27 18:14:36 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -681,17 +681,16 @@
        size_t astrlen;
 {
        size_t  i;
-       int             res;
-       const char *astrend = astr + astrlen;
+       int res;
 
-       for (i = 0; i < ustrlen && astr < astrend; i++) {
+       for (i = 0; i < ustrlen && astrlen > 0; i++) {
                res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
-                               NTFS_TOUPPER((*ntmp->ntm_wget)(&astr)) );
+                   NTFS_TOUPPER((*ntmp->ntm_wget)(&astr, &astrlen)) );
                if (res)
                        return res;
        }
 
-       if (i == ustrlen && astr == astrend)
+       if (i == ustrlen && astrlen == 0)
                return 0;
        else if (i == ustrlen)
                return -1;
@@ -710,17 +709,17 @@
        const char *astr;
        size_t astrlen;
 {
-       size_t             i;
-       int             res;
-       const char *astrend = astr + astrlen;
+       size_t i;
+       int res;
 
-       for (i = 0; (i < ustrlen) && (astr < astrend); i++) {
-               res = (*ntmp->ntm_wcmp)(ustr[i], (*ntmp->ntm_wget)(&astr));
+       for (i = 0; (i < ustrlen) && astrlen > 0; i++) {
+               res = (*ntmp->ntm_wcmp)(ustr[i],
+                    (*ntmp->ntm_wget)(&astr, &astrlen));
                if (res)
                        return res;
        }
 
-       if (i == ustrlen && astr == astrend)
+       if (i == ustrlen && astrlen == 0)
                return 0;
        else if (i == ustrlen)
                return -1;
diff -r 5f0e5df7c0d9 -r e40fc4c5a47b sys/fs/unicode.h
--- a/sys/fs/unicode.h  Mon Dec 27 17:21:46 2004 +0000
+++ b/sys/fs/unicode.h  Mon Dec 27 18:14:36 2004 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: unicode.h,v 1.1 2004/11/21 16:28:40 jdolecek Exp $ */
+/* $NetBSD: unicode.h,v 1.2 2004/12/27 18:14:36 jdolecek Exp $ */
 
 /*-
  * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
@@ -39,11 +39,11 @@
  */
 
 /*
- * Read one wide character off the string, shift the string pointer
+ * Read one UTF8-encoded character off the string, shift the string pointer
  * and return the character.
  */
 static u_int16_t
-wget_utf8(const char **str)
+wget_utf8(const char **str, size_t *sz)
 {
        int c;
        u_int16_t rune = 0;
@@ -53,9 +53,11 @@
                0, 0, 0, 0, 2, 2, 3, 0,
        };
 
+       /* must be called with at least one byte remaining */
+       KASSERT(*sz > 0);
 
        c = _utf_count[(s[0] & 0xf0) >> 4];
-       if (c == 0) {
+       if (c == 0 || c > *sz) {
     decoding_error:
                /*
                 * The first character is in range 128-255 and doesn't
@@ -72,15 +74,11 @@
                rune = s[0] & 0xff;
                break;
        case 2:
-               if (!s[0] || !s[1])
-                       goto decoding_error;
                if ((s[1] & 0xc0) != 0x80)
                        goto decoding_error;
                rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
                break;
        case 3:
-               if (!s[0] || !s[1] || !s[2])
-                       goto decoding_error;
                if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
                        goto decoding_error;
                rune = ((s[0] & 0x1F) << 12) | ((s[1] & 0x3F) << 6)
@@ -88,7 +86,8 @@
                break;
        }
 
-       *str = *str + c;
+       *str += c;
+       *sz -= c;
        return rune;
 }
 



Home | Main Index | Thread Index | Old Index