base: add SystemErrorCodeToString() function.

Pulls the Windows error string generation out of adb into libbase so
that it can be used by fastboot as well. Also makes a Unix equivalent
that just wraps strerror() so that upcoming fastboot error reporting
code can be platform-independent.

The intent here is just to provide a portable way to report an error to
the user. More general cross-platform error handling is out of scope.

Bug: http://b/26236380
Change-Id: I5a784a844775949562d069bb41dcb0ebd13a32bc
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 3005652..35e5945 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -33,6 +33,7 @@
 #include <string>
 #include <vector>
 
+#include <android-base/errors.h>
 #include <android-base/logging.h>
 #include <android-base/macros.h>
 #include <android-base/parsenetaddress.h>
@@ -473,7 +474,7 @@
         // Show the handle value to give us a clue in case we have problems
         // with pseudo-handle values.
         fprintf(stderr, "Cannot make handle 0x%p non-inheritable: %s\n",
-                h, SystemErrorCodeToString(GetLastError()).c_str());
+                h, android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return false;
     }
 
@@ -489,7 +490,7 @@
     HANDLE pipe_write_raw = NULL;
     if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) {
         fprintf(stderr, "Cannot create pipe: %s\n",
-                SystemErrorCodeToString(GetLastError()).c_str());
+                android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return false;
     }
 
@@ -528,7 +529,7 @@
                 return EXIT_SUCCESS;
             } else {
                 fprintf(stderr, "Failed to read from %s: %s\n", output_name,
-                        SystemErrorCodeToString(err).c_str());
+                        android::base::SystemErrorCodeToString(err).c_str());
                 return EXIT_FAILURE;
             }
         }
@@ -540,7 +541,7 @@
             if (!WriteFile(write_handle, buf, bytes_read, &bytes_written,
                            NULL)) {
                 fprintf(stderr, "Failed to write to %s: %s\n", output_name,
-                        SystemErrorCodeToString(GetLastError()).c_str());
+                        android::base::SystemErrorCodeToString(GetLastError()).c_str());
                 return EXIT_FAILURE;
             }
 
@@ -591,7 +592,7 @@
             FILE_ATTRIBUTE_NORMAL, NULL));
     if (nul_read.get() == INVALID_HANDLE_VALUE) {
         fprintf(stderr, "Cannot open 'nul': %s\n",
-                SystemErrorCodeToString(GetLastError()).c_str());
+                android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return -1;
     }
 
@@ -661,7 +662,7 @@
     if ((module_result >= arraysize(program_path)) || (module_result == 0)) {
         // String truncation or some other error.
         fprintf(stderr, "Cannot get executable path: %s\n",
-                SystemErrorCodeToString(GetLastError()).c_str());
+                android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return -1;
     }
 
@@ -687,7 +688,7 @@
             &startup,                 /* startup info, i.e. std handles */
             &pinfo )) {
         fprintf(stderr, "Cannot create process: %s\n",
-                SystemErrorCodeToString(GetLastError()).c_str());
+                android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return -1;
     }
 
@@ -753,7 +754,7 @@
             fprintf(stderr, "could not read ok from ADB Server%s\n",
                     err == ERROR_BROKEN_PIPE ? "" :
                     android::base::StringPrintf(": %s",
-                            SystemErrorCodeToString(err).c_str()).c_str());
+                            android::base::SystemErrorCodeToString(err).c_str()).c_str());
         }
     }
 
@@ -784,7 +785,7 @@
 
     if (wait_result != WAIT_OBJECT_0) {
         fprintf(stderr, "Unexpected result waiting for threads: %lu: %s\n",
-                wait_result, SystemErrorCodeToString(GetLastError()).c_str());
+                wait_result, android::base::SystemErrorCodeToString(GetLastError()).c_str());
         return -1;
     }
 
diff --git a/adb/adb_auth_host.cpp b/adb/adb_auth_host.cpp
index facacef..8f154fd 100644
--- a/adb/adb_auth_host.cpp
+++ b/adb/adb_auth_host.cpp
@@ -43,6 +43,7 @@
 #include "mincrypt/rsa.h"
 #undef RSA_verify
 
+#include <android-base/errors.h>
 #include <android-base/strings.h>
 #include <cutils/list.h>
 
@@ -307,8 +308,7 @@
         WCHAR path[MAX_PATH];
         const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
         if (FAILED(hr)) {
-            D("SHGetFolderPathW failed: %s",
-              SystemErrorCodeToString(hr).c_str());
+            D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str());
             return -1;
         }
         if (!android::base::WideToUTF8(path, &home_str)) {
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index b37d04d..b7b30c5 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -27,6 +27,7 @@
 #include <sched.h>
 #endif
 
+#include <android-base/errors.h>
 #include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
@@ -54,7 +55,7 @@
     if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
         // If string truncation or some other error.
         fatal("cannot retrieve temporary file path: %s\n",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
     }
 
     std::string temp_path_utf8;
@@ -134,7 +135,7 @@
         DWORD written = 0;
         if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
             fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
-                  SystemErrorCodeToString(GetLastError()).c_str());
+                  android::base::SystemErrorCodeToString(GetLastError()).c_str());
         }
         if (written != bytes_to_write) {
             fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 0abade4..d89f0de 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -253,9 +253,6 @@
     return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
 }
 
-// Like strerror(), but for Win32 error codes.
-std::string SystemErrorCodeToString(DWORD error_code);
-
 // We later define a macro mapping 'stat' to 'adb_stat'. This causes:
 //   struct stat s;
 //   stat(filename, &s);
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index bea47a2..dece96f 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -32,6 +32,7 @@
 
 #include <cutils/sockets.h>
 
+#include <android-base/errors.h>
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
@@ -93,36 +94,6 @@
         if (!(cond)) fatal("assertion failed '%s' on %s:%d\n", #cond, __FILE__, __LINE__); \
     } while (0)
 
-std::string SystemErrorCodeToString(const DWORD error_code) {
-  const int kErrorMessageBufferSize = 256;
-  WCHAR msgbuf[kErrorMessageBufferSize];
-  DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
-  DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
-                             arraysize(msgbuf), nullptr);
-  if (len == 0) {
-    return android::base::StringPrintf(
-        "Error (%lu) while retrieving error. (%lu)", GetLastError(),
-        error_code);
-  }
-
-  // Convert UTF-16 to UTF-8.
-  std::string msg;
-  if (!android::base::WideToUTF8(msgbuf, &msg)) {
-      return android::base::StringPrintf(
-          "Error (%d) converting from UTF-16 to UTF-8 while retrieving error. (%lu)", errno,
-          error_code);
-  }
-
-  // Messages returned by the system end with line breaks.
-  msg = android::base::Trim(msg);
-  // There are many Windows error messages compared to POSIX, so include the
-  // numeric error code for easier, quicker, accurate identification. Use
-  // decimal instead of hex because there are decimal ranges like 10000-11999
-  // for Winsock.
-  android::base::StringAppendF(&msg, " (%lu)", error_code);
-  return msg;
-}
-
 void handle_deleter::operator()(HANDLE h) {
     // CreateFile() is documented to return INVALID_HANDLE_FILE on error,
     // implying that NULL is a valid handle, but this is probably impossible.
@@ -134,7 +105,7 @@
     if (h != INVALID_HANDLE_VALUE) {
         if (!CloseHandle(h)) {
             D("CloseHandle(%p) failed: %s", h,
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
         }
     }
 }
@@ -470,8 +441,7 @@
                 return -1;
 
             default:
-                D( "unknown error: %s",
-                   SystemErrorCodeToString( err ).c_str() );
+                D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
                 errno = ENOENT;
                 return -1;
         }
@@ -517,8 +487,7 @@
                 return -1;
 
             default:
-                D( "unknown error: %s",
-                   SystemErrorCodeToString( err ).c_str() );
+                D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
                 errno = ENOENT;
                 return -1;
         }
@@ -708,7 +677,7 @@
     f->event     = WSACreateEvent();
     if (f->event == WSA_INVALID_EVENT) {
         D("WSACreateEvent failed: %s",
-          SystemErrorCodeToString(WSAGetLastError()).c_str());
+          android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
 
         // _event_socket_start assumes that this field is INVALID_HANDLE_VALUE
         // on failure, instead of NULL which is what Windows really returns on
@@ -727,19 +696,19 @@
             // minimize logging spam, so don't log these errors for now.
 #if 0
             D("socket shutdown failed: %s",
-              SystemErrorCodeToString(WSAGetLastError()).c_str());
+              android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
 #endif
         }
         if (closesocket(f->fh_socket) == SOCKET_ERROR) {
             D("closesocket failed: %s",
-              SystemErrorCodeToString(WSAGetLastError()).c_str());
+              android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         }
         f->fh_socket = INVALID_SOCKET;
     }
     if (f->event != NULL) {
         if (!CloseHandle(f->event)) {
             D("CloseHandle failed: %s",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
         }
         f->event = NULL;
     }
@@ -760,7 +729,7 @@
         // that to reduce spam and confusion.
         if (err != WSAEWOULDBLOCK) {
             D("recv fd %d failed: %s", _fh_to_int(f),
-              SystemErrorCodeToString(err).c_str());
+              android::base::SystemErrorCodeToString(err).c_str());
         }
         _socket_set_errno(err);
         result = -1;
@@ -776,7 +745,7 @@
         // that to reduce spam and confusion.
         if (err != WSAEWOULDBLOCK) {
             D("send fd %d failed: %s", _fh_to_int(f),
-              SystemErrorCodeToString(err).c_str());
+              android::base::SystemErrorCodeToString(err).c_str());
         }
         _socket_set_errno(err);
         result = -1;
@@ -811,8 +780,8 @@
         WSADATA  wsaData;
         int      rc = WSAStartup( MAKEWORD(2,2), &wsaData);
         if (rc != 0) {
-            fatal( "adb: could not initialize Winsock: %s",
-                   SystemErrorCodeToString( rc ).c_str());
+            fatal("adb: could not initialize Winsock: %s",
+                  android::base::SystemErrorCodeToString(rc).c_str());
         }
         _winsock_init = 1;
 
@@ -870,7 +839,7 @@
     s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
     if(s == INVALID_SOCKET) {
         *error = android::base::StringPrintf("cannot create socket: %s",
-                SystemErrorCodeToString(WSAGetLastError()).c_str());
+                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("%s", error->c_str());
         return -1;
     }
@@ -881,7 +850,7 @@
         const DWORD err = WSAGetLastError();
         *error = android::base::StringPrintf("cannot connect to %s:%u: %s",
                 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
-                SystemErrorCodeToString(err).c_str());
+                android::base::SystemErrorCodeToString(err).c_str());
         D("could not connect to %s:%d: %s",
           type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
         return -1;
@@ -924,7 +893,7 @@
     s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
     if (s == INVALID_SOCKET) {
         *error = android::base::StringPrintf("cannot create socket: %s",
-                SystemErrorCodeToString(WSAGetLastError()).c_str());
+                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("%s", error->c_str());
         return -1;
     }
@@ -938,7 +907,7 @@
                    sizeof(n)) == SOCKET_ERROR) {
         *error = android::base::StringPrintf(
                 "cannot set socket option SO_EXCLUSIVEADDRUSE: %s",
-                SystemErrorCodeToString(WSAGetLastError()).c_str());
+                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("%s", error->c_str());
         return -1;
     }
@@ -948,7 +917,7 @@
         const DWORD err = WSAGetLastError();
         *error = android::base::StringPrintf("cannot bind to %s:%u: %s",
                 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
-                SystemErrorCodeToString(err).c_str());
+                android::base::SystemErrorCodeToString(err).c_str());
         D("could not bind to %s:%d: %s",
           type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
         return -1;
@@ -956,7 +925,7 @@
     if (type == SOCK_STREAM) {
         if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) {
             *error = android::base::StringPrintf("cannot listen on socket: %s",
-                    SystemErrorCodeToString(WSAGetLastError()).c_str());
+                    android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
             D("could not listen on %s:%d: %s",
               type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
             return -1;
@@ -1010,7 +979,7 @@
     if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) {
         *error = android::base::StringPrintf(
                 "cannot resolve host '%s' and port %s: %s", host.c_str(),
-                port_str, SystemErrorCodeToString(WSAGetLastError()).c_str());
+                port_str, android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("%s", error->c_str());
         return -1;
     }
@@ -1025,7 +994,7 @@
                       addrinfo->ai_protocol);
     if(s == INVALID_SOCKET) {
         *error = android::base::StringPrintf("cannot create socket: %s",
-                SystemErrorCodeToString(WSAGetLastError()).c_str());
+                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("%s", error->c_str());
         return -1;
     }
@@ -1037,7 +1006,7 @@
         // TODO: Use WSAAddressToString or inet_ntop on address.
         *error = android::base::StringPrintf("cannot connect to %s:%s: %s",
                 host.c_str(), port_str,
-                SystemErrorCodeToString(WSAGetLastError()).c_str());
+                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
         D("could not connect to %s:%s:%s: %s",
           type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), port_str,
           error->c_str());
@@ -1075,7 +1044,7 @@
     if (fh->fh_socket == INVALID_SOCKET) {
         const DWORD err = WSAGetLastError();
         LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd <<
-                      " failed: " + SystemErrorCodeToString(err);
+                      " failed: " + android::base::SystemErrorCodeToString(err);
         _socket_set_errno( err );
         return -1;
     }
@@ -1106,9 +1075,8 @@
                              reinterpret_cast<const char*>(optval), optlen );
     if ( result == SOCKET_ERROR ) {
         const DWORD err = WSAGetLastError();
-        D( "adb_setsockopt: setsockopt on fd %d level %d optname %d "
-           "failed: %s\n", fd, level, optname,
-           SystemErrorCodeToString(err).c_str() );
+        D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n",
+          fd, level, optname, android::base::SystemErrorCodeToString(err).c_str());
         _socket_set_errno( err );
         result = -1;
     }
@@ -1130,7 +1098,7 @@
     if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
         const DWORD err = WSAGetLastError();
         D("socket shutdown fd %d failed: %s", fd,
-          SystemErrorCodeToString(err).c_str());
+          android::base::SystemErrorCodeToString(err).c_str());
         _socket_set_errno(err);
         return -1;
     }
@@ -2575,7 +2543,7 @@
         memset(input_record, 0, sizeof(*input_record));
         if (!ReadConsoleInputA(console, input_record, 1, &read_count)) {
             D("_get_key_event_record: ReadConsoleInputA() failed: %s\n",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
             errno = EIO;
             return false;
         }
@@ -3344,7 +3312,7 @@
     if (!SetConsoleMode(in, new_console_mode)) {
         // This really should not fail.
         D("stdin_raw_init: SetConsoleMode() failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
     }
 
     // Once this is set, it means that stdin has been configured for
@@ -3364,7 +3332,7 @@
         if (!SetConsoleMode(in, _old_console_mode)) {
             // This really should not fail.
             D("stdin_raw_restore: SetConsoleMode() failed: %s",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
         }
     }
 }
diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp
index 8d3501e..e79008f 100644
--- a/adb/usb_windows.cpp
+++ b/adb/usb_windows.cpp
@@ -27,6 +27,8 @@
 #include <windows.h>
 #include <winerror.h>
 
+#include <android-base/errors.h>
+
 #include "adb.h"
 #include "transport.h"
 
@@ -221,7 +223,7 @@
   if (!instance) {
     // This is such a common API call that this should never fail.
     fatal("GetModuleHandleW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   WNDCLASSEXW wndclass;
@@ -232,14 +234,14 @@
   wndclass.lpszClassName = kPowerNotificationWindowClassName;
   if (!RegisterClassExW(&wndclass)) {
     fatal("RegisterClassExW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
                        L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
                        NULL, NULL, instance, NULL)) {
     fatal("CreateWindowExW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   MSG msg;
@@ -285,7 +287,7 @@
   ret->adb_interface = AdbCreateInterfaceByName(interface_name);
   if (NULL == ret->adb_interface) {
     D("AdbCreateInterfaceByName failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -296,7 +298,7 @@
                                    AdbOpenSharingModeReadWrite);
   if (NULL == ret->adb_read_pipe) {
     D("AdbOpenDefaultBulkReadEndpoint failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -307,7 +309,7 @@
                                     AdbOpenSharingModeReadWrite);
   if (NULL == ret->adb_write_pipe) {
     D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -319,7 +321,7 @@
                       false);
   if (0 == name_len) {
     D("AdbGetInterfaceName returned name length of zero: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -335,7 +337,7 @@
                            &name_len,
                            false)) {
     D("AdbGetInterfaceName failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -370,7 +372,7 @@
                             &written,
                             time_out)) {
     D("AdbWriteEndpointSync failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     err = EIO;
     goto fail;
   }
@@ -394,7 +396,7 @@
                               &written,
                               time_out)) {
       D("AdbWriteEndpointSync of zero length packet failed: %s",
-        SystemErrorCodeToString(GetLastError()).c_str());
+        android::base::SystemErrorCodeToString(GetLastError()).c_str());
       err = EIO;
       goto fail;
     }
@@ -431,7 +433,7 @@
     if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
                              time_out)) {
       D("AdbReadEndpointSync failed: %s",
-        SystemErrorCodeToString(GetLastError()).c_str());
+        android::base::SystemErrorCodeToString(GetLastError()).c_str());
       err = EIO;
       goto fail;
     }
@@ -460,7 +462,7 @@
 static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
   if (!AdbCloseHandle(adb_handle)) {
     D("AdbCloseHandle(%p) failed: %s", adb_handle,
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 }
 
@@ -539,7 +541,7 @@
   if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
                                  &device_desc)) {
     D("AdbGetUsbDeviceDescriptor failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return 0;
   }
 
@@ -549,7 +551,7 @@
   if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
                                     &interf_desc)) {
     D("AdbGetUsbInterfaceDescriptor failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return 0;
   }
 
@@ -569,7 +571,7 @@
         D("device zero_mask: 0x%x", handle->zero_mask);
       } else {
         D("AdbGetEndpointInformation failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
       }
     }
 
@@ -591,7 +593,7 @@
 
   if (NULL == enum_handle) {
     D("AdbEnumInterfaces failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return;
   }
 
@@ -627,7 +629,7 @@
             }
           } else {
             D("cannot get serial number: %s",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
             usb_cleanup_handle(handle);
             free(handle);
           }
@@ -644,7 +646,7 @@
   if (GetLastError() != ERROR_NO_MORE_ITEMS) {
     // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
     D("AdbNextInterface failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   _adb_close_handle(enum_handle);
diff --git a/base/Android.mk b/base/Android.mk
index 18f8686..d20a81f 100644
--- a/base/Android.mk
+++ b/base/Android.mk
@@ -24,10 +24,18 @@
     strings.cpp \
     test_utils.cpp \
 
+libbase_linux_src_files := \
+    errors_unix.cpp \
+
+libbase_darwin_src_files := \
+    errors_unix.cpp \
+
 libbase_windows_src_files := \
+    errors_windows.cpp \
     utf8.cpp \
 
 libbase_test_src_files := \
+    errors_test.cpp \
     file_test.cpp \
     logging_test.cpp \
     parseint_test.cpp \
@@ -55,10 +63,7 @@
 include $(CLEAR_VARS)
 LOCAL_MODULE := libbase
 LOCAL_CLANG := true
-LOCAL_SRC_FILES := $(libbase_src_files)
-LOCAL_SRC_FILES_darwin := $(libbase_darwin_src_files)
-LOCAL_SRC_FILES_linux := $(libbase_linux_src_files)
-LOCAL_SRC_FILES_windows := $(libbase_windows_src_files)
+LOCAL_SRC_FILES := $(libbase_src_files) $(libbase_linux_src_files)
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 LOCAL_CPPFLAGS := $(libbase_cppflags) $(libbase_linux_cppflags)
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
diff --git a/base/errors_test.cpp b/base/errors_test.cpp
new file mode 100644
index 0000000..8e7cdd1
--- /dev/null
+++ b/base/errors_test.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace base {
+
+// Error strings aren't consistent enough across systems to test the output,
+// just make sure we can compile correctly and nothing crashes even if we send
+// it possibly bogus error codes.
+TEST(ErrorsTest, TestSystemErrorString) {
+  SystemErrorCodeToString(-1);
+  SystemErrorCodeToString(0);
+  SystemErrorCodeToString(1);
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/base/errors_unix.cpp b/base/errors_unix.cpp
new file mode 100644
index 0000000..296995e
--- /dev/null
+++ b/base/errors_unix.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <errno.h>
+
+namespace android {
+namespace base {
+
+std::string SystemErrorCodeToString(int error_code) {
+  return strerror(error_code);
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/base/errors_windows.cpp b/base/errors_windows.cpp
new file mode 100644
index 0000000..a5ff511
--- /dev/null
+++ b/base/errors_windows.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <windows.h>
+
+#include "android-base/stringprintf.h"
+#include "android-base/strings.h"
+#include "android-base/utf8.h"
+
+// A Windows error code is a DWORD. It's simpler to use an int error code for
+// both Unix and Windows if possible, but if this fails we'll need a different
+// function signature for each.
+static_assert(sizeof(int) >= sizeof(DWORD),
+              "Windows system error codes are too large to fit in an int.");
+
+namespace android {
+namespace base {
+
+static constexpr DWORD kErrorMessageBufferSize = 256;
+
+std::string SystemErrorCodeToString(int int_error_code) {
+  WCHAR msgbuf[kErrorMessageBufferSize];
+  DWORD error_code = int_error_code;
+  DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
+  DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
+                             kErrorMessageBufferSize, nullptr);
+  if (len == 0) {
+    return android::base::StringPrintf(
+        "Error %lu while retrieving message for error %lu", GetLastError(),
+        error_code);
+  }
+
+  // Convert UTF-16 to UTF-8.
+  std::string msg;
+  if (!android::base::WideToUTF8(msgbuf, &msg)) {
+    return android::base::StringPrintf(
+        "Error %lu while converting message for error %lu from UTF-16 to UTF-8",
+        GetLastError(), error_code);
+  }
+
+  // Messages returned by the system end with line breaks.
+  msg = android::base::Trim(msg);
+
+  // There are many Windows error messages compared to POSIX, so include the
+  // numeric error code for easier, quicker, accurate identification. Use
+  // decimal instead of hex because there are decimal ranges like 10000-11999
+  // for Winsock.
+  android::base::StringAppendF(&msg, " (%lu)", error_code);
+  return msg;
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/base/include/android-base/errors.h b/base/include/android-base/errors.h
new file mode 100644
index 0000000..ca621fa
--- /dev/null
+++ b/base/include/android-base/errors.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Portable error handling functions. This is only necessary for host-side
+// code that needs to be cross-platform; code that is only run on Unix should
+// just use errno and strerror() for simplicity.
+//
+// There is some complexity since Windows has (at least) three different error
+// numbers, not all of which share the same type:
+//   * errno: for C runtime errors.
+//   * GetLastError(): Windows non-socket errors.
+//   * WSAGetLastError(): Windows socket errors.
+// errno can be passed to strerror() on all platforms, but the other two require
+// special handling to get the error string. Refer to Microsoft documentation
+// to determine which error code to check for each function.
+
+#ifndef BASE_ERRORS_H
+#define BASE_ERRORS_H
+
+#include <string>
+
+namespace android {
+namespace base {
+
+// Returns a string describing the given system error code. |error_code| must
+// be errno on Unix or GetLastError()/WSAGetLastError() on Windows. Passing
+// errno on Windows has undefined behavior.
+std::string SystemErrorCodeToString(int error_code);
+
+}  // namespace base
+}  // namespace android
+
+#endif  // BASE_ERRORS_H