keepassxc icon indicating copy to clipboard operation
keepassxc copied to clipboard

Add Unicode support to CLI on Windows

Open mdonoughe opened this issue 2 years ago • 4 comments

On Windows APIs, text is stored in 16-bit wide strings or 8-bit ANSI strings. If you use byte-oriented APIs such as QFile to interact with stdin/stdout/stderr, by default Windows will try to encode text using the ANSI code page. For most users, that's 1252, which cannot represent the characters users typically think of as "Unicode." For backwards compatibility reasons, Windows does not globally switch the ANSI code page to UTF-8, even though UTF-8 has become the de facto 8-bit character encoding.

This PR sets the process code page to UTF-8 for keepassxc-cli.exe (supported since Windows 10 1903) and then ensures that the code page used for console input and output matches the process code page (QTextStream uses the ANSI code page by default and will automatically pick up UTF-8). On older versions of Windows, the code should still work, but only for characters that can be represented by the system ANSI code page.

Fixes #8305

Screenshots

image

Testing strategy

I added an account to the, previously empty, NonAscii.kbdx file and added a new test that executes the CLI binary to read the password. Unlike the other CLI tests, this one needs to execute the CLI as a separate process because of the way the console interaction works.

Type of change

  • ✅ New feature (change that adds functionality)

mdonoughe avatar Oct 23 '22 00:10 mdonoughe

Very nice!

droidmonkey avatar Oct 23 '22 01:10 droidmonkey

Codecov Report

Base: 64.25% // Head: 64.29% // Increases project coverage by +0.04% :tada:

Coverage data is based on head (da4db1e) compared to base (a73f5bc). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8618      +/-   ##
===========================================
+ Coverage    64.25%   64.29%   +0.04%     
===========================================
  Files          341      341              
  Lines        44330    44331       +1     
===========================================
+ Hits         28483    28501      +18     
+ Misses       15847    15830      -17     
Impacted Files Coverage Δ
src/cli/Utils.cpp 81.97% <100.00%> (+6.66%) :arrow_up:
src/core/FileWatcher.cpp 86.75% <0.00%> (+1.20%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov-commenter avatar Oct 23 '22 02:10 codecov-commenter

The coverage run failed because of an error in testcli, but it was in testAdd, which I didn't change, on Linux, where my changes to the CLI itself should be removed by the preprocessor.

mdonoughe avatar Oct 23 '22 03:10 mdonoughe

Probably a random anomaly, I reran it

droidmonkey avatar Oct 23 '22 03:10 droidmonkey