Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UTF-16 option to Base64 encoder/decoder #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dchristensen
Copy link

Adds the UTF-16 encoding type to the Base64 encoder/decoder

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • New feature or enhancement
  • UI change (please include screenshot!)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Internationalization and localization
  • Other (please describe):

What is the current behavior?

The Base64 encoder/decoder does not support UTF-16 encoding. This encoding is commonly used when working with PowerShell as it supports encoding commands using the powershel.exe -EncodedCommand option.

Issue Number: DevToys-app/DevToys#661

What is the new behavior?

  • Adds UTF-16 support

Other information

This only adds support for UTF-16LE with BOM (System.Text.Encoding.Unicode). I considered adding UTF-16LE and UTF-16BE explicitly as well, but felt that might clutter the UI with too many options. I think UTF-16LE BOM will be sufficient for most use-cases (PowerShell supports UTF-16LE with BOM).

Quality check

Before creating this PR:

  • Did you follow the code style guideline as described in CONTRIBUTING.md
  • Did you build the app and test your changes?
  • Did you check for accessibility? On Windows, you can use Accessibility Insights for this.
  • Did you verify that the change work in Release build configuration
  • Did you verify that all unit tests pass
  • If necessary and if possible, did you verify your changes on:
    • Windows
    • macOS
    • Linux

@dchristensen dchristensen changed the title Add utf16 base64 Add UTF-16 option to Base64 encoder/decoder Nov 23, 2024
@dchristensen
Copy link
Author

I realized I failed to actually output the BOM when encoding a Unicode string. As I've been going through to add that I've found some existing code in the codebase that I have questions about.

In the Base64Helper.FromBase64ToText it looks like we're encoding the UTF-8 preamble (using Encoding.Unicode) and including it in encoded output?
https://github.com/DevToys-app/DevToys.Tools/blob/main/src/DevToys.Tools/Helpers/Base64Helper.cs#L123-L143

I've found the related issue (DevToys-app/DevToys#342) and PR (DevToys-app/DevToys#378) that appears to have driven this change.

I find it strange that the preamble is included in the plain-text output. The Preamble seems like it should be an implementation detail of the encoding when converting characters to bytes. The same string could be encoded with or without a preamble depending on the encoder settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant