1 // Copyright 2019 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Base64 escaping methods to encode/decode strings.
30 
31 #ifndef TOOLS_WINDOWS_CONVERTER_EXE_ESCAPING_H_
32 #define TOOLS_WINDOWS_CONVERTER_EXE_ESCAPING_H_
33 
34 #include <string>
35 
36 namespace strings {
37 
38 using std::string;
39 
40 // ----------------------------------------------------------------------
41 // Base64Escape()
42 // WebSafeBase64Escape()
43 //    Encode "src" to "dest" using base64 encoding.
44 //    src is not null terminated, instead specify len.
45 //    'dest' should have at least CalculateBase64EscapedLen() length.
46 //    RETURNS the length of dest.
47 //    The WebSafe variation use '-' instead of '+' and '_' instead of '/'
48 //    so that we can place the out in the URL or cookies without having
49 //    to escape them.  It also has an extra parameter "do_padding",
50 //    which when set to false will prevent padding with "=".
51 // ----------------------------------------------------------------------
52 void Base64Escape(const string& src, string* dest);
53 int Base64Escape(const unsigned char* src, int slen, char* dest, int szdest);
54 // Encode src into dest with padding.
55 void Base64Escape(const unsigned char* src, int szsrc,
56                   string* dest, bool do_padding);
57 
58 int WebSafeBase64Escape(const unsigned char* src, int slen, char* dest,
59                         int szdest, bool do_padding);
60 // Encode src into dest web-safely without padding.
61 void WebSafeBase64Escape(const string& src, string* dest);
62 // Encode src into dest web-safely with padding.
63 void WebSafeBase64EscapeWithPadding(const string& src, string* dest);
64 void WebSafeBase64Escape(const unsigned char* src, int szsrc,
65                          string* dest, bool do_padding);
66 
67 // ----------------------------------------------------------------------
68 // Base64Unescape()
69 // WebSafeBase64Unescape()
70 //    Copies "src" to "dest", where src is in base64 and is written to its
71 //    ASCII equivalents. src is not null terminated, instead specify len.
72 //    I recommend that slen<szdest, but we honor szdest anyway.
73 //    RETURNS the length of dest, or -1 if src contains invalid chars.
74 //    The WebSafe variation use '-' instead of '+' and '_' instead of '/'.
75 //    The variations that store into a string clear the string first, and
76 //    return false (with dest empty) if src contains invalid chars; for
77 //    these versions src and dest must be different strings.
78 // ----------------------------------------------------------------------
79 int Base64Unescape(const char* src, int slen, char* dest, int szdest);
80 bool Base64Unescape(const char* src, int slen, string* dest);
Base64Unescape(const string & src,string * dest)81 inline bool Base64Unescape(const string& src, string* dest) {
82   return Base64Unescape(src.data(), src.size(), dest);
83 }
84 
85 
86 int WebSafeBase64Unescape(const char* src, int slen, char* dest, int szdest);
87 bool WebSafeBase64Unescape(const char* src, int slen, string* dest);
88 bool WebSafeBase64Unescape(const string& src, string* dest);
89 
90 // Return the length to use for the output buffer given to the base64 escape
91 // routines. Make sure to use the same value for do_padding in both.
92 // This function may return incorrect results if given input_len values that
93 // are extremely high, which should happen rarely.
94 int CalculateBase64EscapedLen(int input_len, bool do_padding);
95 // Use this version when calling Base64Escape without a do_padding arg.
96 int CalculateBase64EscapedLen(int input_len);
97 }  // namespace strings
98 
99 #endif  // TOOLS_WINDOWS_CONVERTER_EXE_ESCAPING_H_
100