Add POSIX swab.
Surprisingly to me, there are actual uses of `swab` in the codebases
I have available to search, including one with a #ifndef __ANDROID__
around it.
Bug: N/A
Test: ran tests
Change-Id: Ic91b78ae22bb65c346cb46dd38916f48d979abe0
diff --git a/docs/status.md b/docs/status.md
index c119439..ff44e4b 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -16,6 +16,7 @@
* `iconv`/`iconv_close`/`iconv_open` (adding <iconv.h>)
* `pthread_setschedprio`
* <spawn.h>
+ * `swab`
* `syncfs`
New libc functions in O:
@@ -92,7 +93,6 @@
pthread_setcanceltype
pthread_testcancel
sockatmark
-swab
wordexp
wordfree
```
diff --git a/libc/Android.bp b/libc/Android.bp
index 81135ea..2e5ec00 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1534,6 +1534,7 @@
"bionic/strings_l.cpp",
"bionic/strsignal.cpp",
"bionic/strtold.cpp",
+ "bionic/swab.cpp",
"bionic/symlink.cpp",
"bionic/sync_file_range.cpp",
"bionic/sys_msg.cpp",
diff --git a/libc/bionic/swab.cpp b/libc/bionic/swab.cpp
new file mode 100644
index 0000000..bc53ba4
--- /dev/null
+++ b/libc/bionic/swab.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2017 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 <unistd.h>
+
+void swab(const void* void_src, void* void_dst, ssize_t byte_count) {
+ const uint8_t* src = static_cast<const uint8_t*>(void_src);
+ uint8_t* dst = static_cast<uint8_t*>(void_dst);
+ while (byte_count > 1) {
+ uint8_t x = *src++;
+ uint8_t y = *src++;
+ *dst++ = y;
+ *dst++ = x;
+ byte_count -= 2;
+ }
+}
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 9cfb918..a2beb38 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -246,6 +246,8 @@
int getdomainname(char* __buf, size_t __buf_size) __INTRODUCED_IN(26);
int setdomainname(const char* __name, size_t __n) __INTRODUCED_IN(26);
+void swab(const void* __src, void* __dst, ssize_t __byte_count) __INTRODUCED_IN_FUTURE;
+
#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
#include <bits/fortify/unistd.h>
#endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index 981dd59..f1dafb1 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1366,6 +1366,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 29c5235..2ba32bf 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1286,6 +1286,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index eafbbd7..0f1e325 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1391,6 +1391,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index a32131f..8a525bc 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1350,6 +1350,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 29c5235..2ba32bf 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1286,6 +1286,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index f1308ea..b19e181 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1348,6 +1348,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 29c5235..2ba32bf 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1286,6 +1286,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/tests/linux_swab_test.cpp b/tests/linux_swab_test.cpp
index 6b964dc..ffd4072 100644
--- a/tests/linux_swab_test.cpp
+++ b/tests/linux_swab_test.cpp
@@ -21,7 +21,7 @@
// This test makes sure that references to all of the kernel swab
// macros/inline functions that are exported work properly.
// Verifies that any kernel header updates do not break these macros.
-TEST(swab, fswa) {
+TEST(linux_swab, smoke) {
EXPECT_EQ(0x3412U, __swab16(0x1234));
EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index ced0315..e52d01c 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -1382,3 +1382,46 @@
EXPECT_EQ(0, getlogin_r(buf, sizeof(buf)));
EXPECT_STREQ(getlogin(), buf);
}
+
+TEST(UNISTD_TEST, swab) {
+ // POSIX: "The swab() function shall copy nbytes bytes, which are pointed to by src,
+ // to the object pointed to by dest, exchanging adjacent bytes."
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("ehll oowlr\0d", buf, 12);
+ ASSERT_STREQ("hello world", buf);
+}
+
+TEST(UNISTD_TEST, swab_odd_byte_count) {
+ // POSIX: "If nbytes is odd, swab() copies and exchanges nbytes-1 bytes and the disposition
+ // of the last byte is unspecified."
+ // ...but it seems unreasonable to not just leave the last byte alone.
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("012345", buf, 3);
+ ASSERT_EQ('1', buf[0]);
+ ASSERT_EQ('0', buf[1]);
+ ASSERT_EQ('x', buf[2]);
+}
+
+TEST(UNISTD_TEST, swab_overlap) {
+ // POSIX: "If copying takes place between objects that overlap, the behavior is undefined."
+ // ...but it seems unreasonable to not just do the right thing.
+ char buf[] = "012345";
+ swab(buf, buf, 4);
+ ASSERT_EQ('1', buf[0]);
+ ASSERT_EQ('0', buf[1]);
+ ASSERT_EQ('3', buf[2]);
+ ASSERT_EQ('2', buf[3]);
+ ASSERT_EQ('4', buf[4]);
+ ASSERT_EQ('5', buf[5]);
+ ASSERT_EQ(0, buf[6]);
+}
+
+TEST(UNISTD_TEST, swab_negative_byte_count) {
+ // POSIX: "If nbytes is negative, swab() does nothing."
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("hello", buf, -1);
+ ASSERT_EQ('x', buf[0]);
+}