Implement mblen(3).
Change-Id: I65948ea5b9ecd63f966ba767ad6db4a2effc4700
diff --git a/libc/Android.mk b/libc/Android.mk
index 374ffa6..ac7cd75 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -133,6 +133,7 @@
bionic/lockf.cpp \
bionic/lstat.cpp \
bionic/malloc_info.cpp \
+ bionic/mblen.cpp \
bionic/mbrtoc16.cpp \
bionic/mbrtoc32.cpp \
bionic/mbstate.cpp \
diff --git a/libc/bionic/mblen.cpp b/libc/bionic/mblen.cpp
new file mode 100644
index 0000000..7def6f1
--- /dev/null
+++ b/libc/bionic/mblen.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+#include <wchar.h>
+
+int mblen(const char* s, size_t n) {
+ mbstate_t state = {};
+ return mbrlen(s, n, &state);
+}
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index 2abaec3..ba4b149 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -162,14 +162,11 @@
extern const char* getprogname(void);
extern void setprogname(const char*);
-/* make STLPort happy */
-extern int mblen(const char *, size_t) __UNAVAILABLE;
-extern size_t mbstowcs(wchar_t *, const char *, size_t);
-extern int mbtowc(wchar_t *, const char *, size_t);
-
-/* Likewise, make libstdc++-v3 happy. */
-extern int wctomb(char *, wchar_t);
-extern size_t wcstombs(char *, const wchar_t *, size_t);
+int mblen(const char*, size_t);
+size_t mbstowcs(wchar_t*, const char*, size_t);
+int mbtowc(wchar_t*, const char*, size_t);
+int wctomb(char*, wchar_t);
+size_t wcstombs(char*, const wchar_t*, size_t);
extern size_t __ctype_get_mb_cur_max(void);
#define MB_CUR_MAX __ctype_get_mb_cur_max()
diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map
index 3e78e30..a1373d8 100644
--- a/libc/libc.arm.brillo.map
+++ b/libc/libc.arm.brillo.map
@@ -1279,6 +1279,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index 420cd7e..4e69bf1 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1279,6 +1279,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index bdde35e..7ea74a6 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1201,6 +1201,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index be37ffb..8d1a1f9 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1304,6 +1304,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map
index 6b5744c..aac1d8c 100644
--- a/libc/libc.mips.brillo.map
+++ b/libc/libc.mips.brillo.map
@@ -1263,6 +1263,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 6ee9cf5..e514aa1 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1263,6 +1263,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index bdde35e..7ea74a6 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1201,6 +1201,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map
index d483d3e..f14861d 100644
--- a/libc/libc.x86.brillo.map
+++ b/libc/libc.x86.brillo.map
@@ -1261,6 +1261,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index f0679c0..f6b6ea1 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1261,6 +1261,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index bdde35e..7ea74a6 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1201,6 +1201,7 @@
getpwent;
getsubopt;
hasmntopt;
+ mblen;
pthread_getname_np;
quotactl;
setdomainname;
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 05438eb..773230f 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -537,3 +537,26 @@
ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
}
+
+TEST(stdlib, mblen) {
+ // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
+ // respectively, do or do not have state-dependent encodings." We're always UTF-8.
+ EXPECT_EQ(0, mblen(nullptr, 1));
+
+ ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
+
+ // 1-byte UTF-8.
+ EXPECT_EQ(1, mblen("abcdef", 6));
+ // 2-byte UTF-8.
+ EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
+ // 3-byte UTF-8.
+ EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
+ // 4-byte UTF-8.
+ EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
+
+ // Illegal over-long sequence.
+ ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
+
+ // "mblen() shall ... return 0 (if s points to the null byte)".
+ EXPECT_EQ(0, mblen("", 1));
+}