Kevin Lubick 5d70f10ebb Add Base64::EncodedSize to tidy up allocations (flutter/engine#46624)
As a follow-up to #46543, this adds a dedicated function to compute the
size of the buffer needed to encode data using Base64 instead of calling
the encode function with nullptr.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
2023-10-06 16:50:20 -04:00

60 lines
1.6 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_COMMON_BASE64_H_
#define FLUTTER_SHELL_COMMON_BASE64_H_
#include <cstddef>
namespace flutter {
struct Base64 {
public:
enum class Error {
kNone,
kBadPadding,
kBadChar,
};
/**
Base64 encodes src into dst.
@param dst a pointer to a buffer large enough to receive the result.
@return the required length of dst for encoding.
*/
static size_t Encode(const void* src, size_t length, void* dst);
/**
Returns the length of the buffer that needs to be allocated to encode
srcDataLength bytes.
*/
static size_t EncodedSize(size_t srcDataLength) {
// Take the floor of division by 3 to find the number of groups that need to
// be encoded. Each group takes 4 bytes to be represented in base64.
return ((srcDataLength + 2) / 3) * 4;
}
/**
Base64 decodes src into dst.
This can be called once with 'dst' nullptr to get the required size,
then again with an allocated 'dst' pointer to do the actual decoding.
@param dst nullptr or a pointer to a buffer large enough to receive the
result
@param dstLength assigned the length dst is required to be. Must not be
nullptr.
*/
[[nodiscard]] static Error Decode(const void* src,
size_t srcLength,
void* dst,
size_t* dstLength);
};
} // namespace flutter
#endif // FLUTTER_SHELL_COMMON_BASE64_H_