/src/wxwidgets/include/wx/base64.h
Line | Count | Source |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/base64.h |
3 | | // Purpose: declaration of BASE64 encoding/decoding functionality |
4 | | // Author: Charles Reimers, Vadim Zeitlin |
5 | | // Created: 2007-06-18 |
6 | | // Licence: wxWindows licence |
7 | | /////////////////////////////////////////////////////////////////////////////// |
8 | | |
9 | | #ifndef _WX_BASE64_H_ |
10 | | #define _WX_BASE64_H_ |
11 | | |
12 | | #include "wx/defs.h" |
13 | | |
14 | | #if wxUSE_BASE64 |
15 | | |
16 | | #include "wx/string.h" |
17 | | #include "wx/buffer.h" |
18 | | |
19 | | // ---------------------------------------------------------------------------- |
20 | | // encoding functions |
21 | | // ---------------------------------------------------------------------------- |
22 | | |
23 | | // return the size needed for the buffer containing the encoded representation |
24 | | // of a buffer of given length |
25 | 0 | inline size_t wxBase64EncodedSize(size_t len) { return 4*((len+2)/3); } |
26 | | |
27 | | // raw base64 encoding function which encodes the contents of a buffer of the |
28 | | // specified length into the buffer of the specified size |
29 | | // |
30 | | // returns the length of the encoded data or wxCONV_FAILED if the buffer is not |
31 | | // large enough; to determine the needed size you can either allocate a buffer |
32 | | // of wxBase64EncodedSize(srcLen) size or call the function with null buffer in |
33 | | // which case the required size will be returned |
34 | | WXDLLIMPEXP_BASE size_t |
35 | | wxBase64Encode(char *dst, size_t dstLen, const void *src, size_t srcLen); |
36 | | |
37 | | // encode the contents of the given buffer using base64 and return as string |
38 | | // (there is no error return) |
39 | | inline wxString wxBase64Encode(const void *src, size_t srcLen) |
40 | 0 | { |
41 | 0 | const size_t dstLen = wxBase64EncodedSize(srcLen); |
42 | 0 | wxCharBuffer dst(dstLen); |
43 | 0 | wxBase64Encode(dst.data(), dstLen, src, srcLen); |
44 | |
|
45 | 0 | return wxASCII_STR(dst); |
46 | 0 | } |
47 | | |
48 | | inline wxString wxBase64Encode(const wxMemoryBuffer& buf) |
49 | 0 | { |
50 | 0 | return wxBase64Encode(buf.GetData(), buf.GetDataLen()); |
51 | 0 | } |
52 | | |
53 | | // ---------------------------------------------------------------------------- |
54 | | // decoding functions |
55 | | // ---------------------------------------------------------------------------- |
56 | | |
57 | | // elements of this enum specify the possible behaviours of wxBase64Decode() |
58 | | // when an invalid character is encountered |
59 | | enum wxBase64DecodeMode |
60 | | { |
61 | | // normal behaviour: stop at any invalid characters |
62 | | wxBase64DecodeMode_Strict, |
63 | | |
64 | | // skip whitespace characters |
65 | | wxBase64DecodeMode_SkipWS, |
66 | | |
67 | | // the most lenient behaviour: simply ignore all invalid characters |
68 | | wxBase64DecodeMode_Relaxed |
69 | | }; |
70 | | |
71 | | // return the buffer size necessary for decoding a base64 string of the given |
72 | | // length |
73 | 0 | inline size_t wxBase64DecodedSize(size_t srcLen) { return 3*srcLen/4; } |
74 | | |
75 | | // raw decoding function which decodes the contents of the string of specified |
76 | | // length (or NUL-terminated by default) into the provided buffer of the given |
77 | | // size |
78 | | // |
79 | | // the function normally stops at any character invalid inside a base64-encoded |
80 | | // string (i.e. not alphanumeric nor '+' nor '/') but can be made to skip the |
81 | | // whitespace or all invalid characters using its mode argument |
82 | | // |
83 | | // returns the length of the decoded data or wxCONV_FAILED if an error occurs |
84 | | // such as the buffer is too small or the encoded string is invalid; in the |
85 | | // latter case the posErr is filled with the position where the decoding |
86 | | // stopped if it is not null |
87 | | WXDLLIMPEXP_BASE size_t |
88 | | wxBase64Decode(void *dst, size_t dstLen, |
89 | | const char *src, size_t srcLen = wxNO_LEN, |
90 | | wxBase64DecodeMode mode = wxBase64DecodeMode_Strict, |
91 | | size_t *posErr = nullptr); |
92 | | |
93 | | inline size_t |
94 | | wxBase64Decode(void *dst, size_t dstLen, |
95 | | const wxString& src, |
96 | | wxBase64DecodeMode mode = wxBase64DecodeMode_Strict, |
97 | | size_t *posErr = nullptr) |
98 | 0 | { |
99 | 0 | // don't use str.length() here as the ASCII buffer is shorter than it for |
100 | 0 | // strings with embedded NULs |
101 | 0 | return wxBase64Decode(dst, dstLen, src.ToAscii(), wxNO_LEN, mode, posErr); |
102 | 0 | } |
103 | | |
104 | | // decode the contents of the given string; the returned buffer is empty if an |
105 | | // error occurs during decoding |
106 | | WXDLLIMPEXP_BASE wxMemoryBuffer |
107 | | wxBase64Decode(const char *src, size_t srcLen = wxNO_LEN, |
108 | | wxBase64DecodeMode mode = wxBase64DecodeMode_Strict, |
109 | | size_t *posErr = nullptr); |
110 | | |
111 | | inline wxMemoryBuffer |
112 | | wxBase64Decode(const wxString& src, |
113 | | wxBase64DecodeMode mode = wxBase64DecodeMode_Strict, |
114 | | size_t *posErr = nullptr) |
115 | 0 | { |
116 | | // don't use str.length() here as the ASCII buffer is shorter than it for |
117 | | // strings with embedded NULs |
118 | 0 | return wxBase64Decode(src.ToAscii(), wxNO_LEN, mode, posErr); |
119 | 0 | } |
120 | | |
121 | | #endif // wxUSE_BASE64 |
122 | | |
123 | | #endif // _WX_BASE64_H_ |