• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkBase64_DEFINED
9 #define SkBase64_DEFINED
10 
11 #include "include/private/base/SkAPI.h"
12 #include <cstddef>
13 
14 struct SK_API SkBase64 {
15 public:
16     enum Error {
17         kNoError,
18         kPadError,
19         kBadCharError
20     };
21 
22     /**
23        Base64 encodes src into dst.
24 
25        Normally this is called once with 'dst' nullptr to get the required size, then again with an
26        allocated 'dst' pointer to do the actual encoding.
27 
28        @param dst nullptr or a pointer to a buffer large enough to receive the result
29 
30        @param encode nullptr for default encoding or a pointer to at least 65 chars.
31                      encode[64] will be used as the pad character.
32                      Encodings other than the default encoding cannot be decoded.
33 
34        @return the required length of dst for encoding.
35     */
36     static size_t Encode(const void* src, size_t length, void* dst, const char* encode = nullptr);
37 
38     /**
39        Returns the length of the buffer that needs to be allocated to encode srcDataLength bytes.
40     */
EncodedSizeSkBase6441     static size_t EncodedSize(size_t srcDataLength) {
42         // Take the floor of division by 3 to find the number of groups that need to be encoded.
43         // Each group takes 4 bytes to be represented in base64.
44         return ((srcDataLength + 2) / 3) * 4;
45     }
46 
47     /**
48        Base64 decodes src into dst.
49 
50        Normally this is called once with 'dst' nullptr to get the required size, then again with an
51        allocated 'dst' pointer to do the actual encoding.
52 
53        @param dst nullptr or a pointer to a buffer large enough to receive the result
54 
55        @param dstLength assigned the length dst is required to be. Must not be nullptr.
56     */
57     [[nodiscard]] static Error Decode(const void* src, size_t  srcLength,
58                                       void* dst, size_t* dstLength);
59 };
60 
61 #endif // SkBase64_DEFINED
62