From c7695c7bbf327320716706dc0737c396eef4c68c Mon Sep 17 00:00:00 2001 From: Jeffy Chen Date: Sat, 7 Oct 2023 17:08:03 +0800 Subject: [PATCH 7/7] adb: daemon: Fix build issue with musl and uclibc Signed-off-by: Jeffy Chen --- adb/daemon/auth.cpp | 129 +++++++++++++++++++++++++ adb/transport.cpp | 6 ++ base/include/android-base/quick_exit.h | 8 +- base/logging.cpp | 4 +- libcutils/sockets_unix.cpp | 2 +- meson.build | 2 - 6 files changed, 142 insertions(+), 9 deletions(-) diff --git a/adb/daemon/auth.cpp b/adb/daemon/auth.cpp index 1266a48..5a4f404 100644 --- a/adb/daemon/auth.cpp +++ b/adb/daemon/auth.cpp @@ -51,6 +51,135 @@ static bool needs_retry = false; bool auth_required = true; +#ifndef b64_pton + +/* Based on glibc-2.37-2: resolv/base64.c */ + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +int +b64_pton (char const *src, u_char *target, size_t targsize) +{ + int tarindex, state, ch; + char *pos; + + state = 0; + tarindex = 0; + + while ((ch = *src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 4; + target[tarindex+1] = ((pos - Base64) & 0x0f) + << 4 ; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 2; + target[tarindex+1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + default: + abort(); + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /* We got a pad char. */ + ch = *src++; /* Skip it, get next. */ + switch (state) { + case 0: /* Invalid = in first position */ + case 1: /* Invalid = in second position */ + return (-1); + + case 2: /* Valid, means one byte of info */ + /* Skip any number of spaces. */ + for ((void)NULL; ch != '\0'; ch = *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /* Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /* Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for ((void)NULL; ch != '\0'; ch = *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + return (tarindex); +} + +#endif + bool adbd_auth_verify(const char* token, size_t token_size, const std::string& sig, std::string* auth_key) { static constexpr const char* key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", nullptr }; diff --git a/adb/transport.cpp b/adb/transport.cpp index 841865a..3c321d0 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -42,7 +42,9 @@ #include #include +#if ADB_HOST #include +#endif #include "adb.h" #include "adb_auth.h" @@ -849,10 +851,12 @@ atransport* acquire_one_transport(TransportType type, const char* serial, Transp std::unique_lock lock(transport_lock); for (const auto& t : transport_list) { +#if ADB_HOST if (t->GetConnectionState() == kCsNoPerm) { *error_out = UsbNoPermissionsLongHelpText(); continue; } +#endif if (transport_id) { if (t->id == transport_id) { @@ -999,6 +1003,7 @@ void atransport::SetConnection(std::unique_ptr connection) { connection_ = std::shared_ptr(std::move(connection)); } +#if ADB_HOST std::string atransport::connection_state_name() const { ConnectionState state = GetConnectionState(); switch (state) { @@ -1028,6 +1033,7 @@ std::string atransport::connection_state_name() const { return "unknown"; } } +#endif void atransport::update_version(int version, size_t payload) { protocol_version = std::min(version, A_VERSION); diff --git a/base/include/android-base/quick_exit.h b/base/include/android-base/quick_exit.h index a03b14f..55b542d 100644 --- a/base/include/android-base/quick_exit.h +++ b/base/include/android-base/quick_exit.h @@ -23,12 +23,12 @@ namespace android { namespace base { // Bionic and glibc have quick_exit, Darwin and Windows don't. -#if !defined(__linux__) - void quick_exit(int exit_code) __attribute__((noreturn)); - int at_quick_exit(void (*func)()); -#else +#if defined(__BIONIC__) || (defined(__GLIBC__) && !defined(__UCLIBC__)) using ::at_quick_exit; using ::quick_exit; +#else + void quick_exit(int exit_code) __attribute__((noreturn)); + int at_quick_exit(void (*func)()); #endif } } diff --git a/base/logging.cpp b/base/logging.cpp index 967684f..17e56f4 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -64,9 +64,9 @@ namespace android { namespace base { // BSD-based systems like Android/macOS have getprogname(). Others need us to provide one. -#if defined(__GLIBC__) || defined(_WIN32) +#if defined(__GNUC__) || defined(_WIN32) static const char* getprogname() { -#if defined(__GLIBC__) +#if defined(__GNUC__) return program_invocation_short_name; #elif defined(_WIN32) static bool first = true; diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp index 6acdcd8..3922ca3 100644 --- a/libcutils/sockets_unix.cpp +++ b/libcutils/sockets_unix.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include "android_get_control_env.h" diff --git a/meson.build b/meson.build index 03c8341..38cc102 100644 --- a/meson.build +++ b/meson.build @@ -57,7 +57,6 @@ adbd_srcs += [ 'base/strings.cpp', 'base/stringprintf.cpp', 'base/parsenetaddress.cpp', - 'diagnose_usb/diagnose_usb.cpp', 'libasyncio/AsyncIO.cpp', 'libcutils/sockets.cpp', 'libcutils/sockets_unix.cpp', @@ -73,7 +72,6 @@ adbd_inc = [ 'adb', 'adb/daemon/include', 'base/include', - 'diagnose_usb/include', 'libutils/include', 'libcutils/include', 'libasyncio/include', -- 2.20.1