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

create higher level base64 functions #377

Open
lemire opened this issue Mar 16, 2024 · 4 comments
Open

create higher level base64 functions #377

lemire opened this issue Mar 16, 2024 · 4 comments

Comments

@lemire
Copy link
Member

lemire commented Mar 16, 2024

We should also provide base64_to_binary(const char* input) -> std::vector<uint8_t>, that does calculation of safe size and allocate memory internally. Or maybe something like base_to_binary(const char* input, cont: &Container) and static_assert that the Container has method resize. (credit: @WojciechMula)

@lemire lemire mentioned this issue Mar 16, 2024
@WojciechMula
Copy link
Collaborator

WojciechMula commented Mar 29, 2024

I was thinking a little about API. My generic proposal is providing a convenient wrapper that would work incrementally. I mean: user provides partial data (like input buffer when reading from file) and output buffer of fixed size. Using for decoding would be something like:

auto decoder = Base64Decoder::new();

std::string input;
input.resize(32 * 1024);

std::string output;
output.resize(16 * 1024);
while (/**/) {
      // read a few kilobytes data from into `input`
  
      const size_t bytes_stored = decoder.decode(input.data(), input.size(), output.data(), output.size());
      // bytes_stored will never be greater than output.size()

      write (output.data(), bytes_stored)

      if input file reached EOF {
          while (decoder.pending_output()) {
              const size_t bytes_stored = decoder.flush(output.data(), output.size());
              write (output.data(), bytes)
         }
      }
}

Of course this flexibility is at cost of performance, but my gut feeling is that if somebody want to process data in chunks, than problem is likely I/O bound.

@WojciechMula
Copy link
Collaborator

Another thing for base64 encoding - it would be practical if we allowed wrapping output, for instance:

const size_t max_line_length = 72;
const char* separator = "\n";
encode(input, output, max_line_length, separator);
```

Again, nobody would expect that this variant will be as fast as the plain encoding.

@lemire
Copy link
Member Author

lemire commented Mar 29, 2024

@WojciechMula I'm pinging you later today as I have a major upgrade to the base64 support, with a slightly improved API.

@lemire
Copy link
Member Author

lemire commented Mar 30, 2024

Please see #382 where the base64 API was slightly extended (i.e., we have _safe functions).

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

No branches or pull requests

2 participants