question wrt <https://github.com/osquery/osquery/i...
# windows
f
question wrt https://github.com/osquery/osquery/issues/6160 Is using windows API okay to convert UTF-8 <--> UTF-16 instead of relying on
std::codecvt_utf8_utf16
? I put my comments on the issue, with what i found reversing
RegQueryValueExA
*
Mmm, codecvt is actually deprecated in C++17. So, CP_ACP is the remaining correct approach.
👍 2
t
I feel under qualified to have an opinion but your research and reasoning is sound. How complicated is it to adapt the code/implementation to CP_ACP?
f
mmm, it's straight-forward. Sorry, I wasn't clear. CP_ACP is a constant that you pass into these two windows api functions to convert between UTF-16 and other multi-byte charactersets: MultiByteToWideChar and WideCharToMultiByte, where CP_ACP is to be used when converting from UTF-8 to UTF-16
so, for example:
Copy code
std::string to_bytes(const std::wstring& str) {
    std::string result;
    if (str.length() > 0) {
      char* buffer = new (std::nothrow) char[str.length() * 4];
      if (nullptr != buffer) {
        if (0 != WideCharToMultiByte(CP_ACP,
                                     0,
                                     str.c_str(),
                                     -1,
                                     buffer,
                                     str.length() * 4,
                                     NULL,
                                     NULL)) {
          result = buffer;
          delete[] buffer;
        }
      }
    }

    return result;
  }
and in WinNls.h:
Copy code
//
//  Code Page Default Values.
//  Please Use Unicode, either UTF-16 (as in WCHAR) or UTF-8 (code page CP_ACP)
//
#define CP_ACP                    0           // default to ANSI code page