Title
#windows
f

farfella

01/17/2020, 10:29 PM
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
*
11:10 PM
Mmm, codecvt is actually deprecated in C++17. So, CP_ACP is the remaining correct approach.
theopolis

theopolis

01/17/2020, 11:21 PM
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

farfella

01/17/2020, 11:29 PM
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
11:30 PM
so, for example:
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;
  }
11:31 PM
and in WinNls.h:
//
//  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