1 // Copyright (C) 2009-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef BASE64_H
8 #define BASE64_H 1
9 
10 #include <stdint.h>
11 #include <string>
12 #include <vector>
13 
14 //
15 // Note: this helper module isn't specific to the DNS protocol per se.
16 // We should probably move this to somewhere else, possibly in some common
17 // utility area.
18 //
19 
20 namespace isc {
21 namespace util {
22 namespace encode {
23 
24 /// \brief Encode binary data in the base64 format.
25 ///
26 /// This function returns a new \c std::string object that stores a text
27 /// encoded in the base64 format for the given \c binary %data.
28 /// The resulting string will be a valid, canonical form of base64
29 /// representation as specified in RFC4648.
30 ///
31 /// If memory allocation for the returned string fails, a corresponding
32 /// standard exception will be thrown.  This function never throws exceptions
33 /// otherwise.
34 ///
35 /// \param binary A vector object storing the data to be encoded.
36 /// \return A newly created string that stores base64 encoded value for binary.
37 std::string encodeBase64(const std::vector<uint8_t>& binary);
38 
39 /// \brief Decode a text encoded in the base64 format into the original %data.
40 ///
41 /// The \c input argument must be a valid string represented in the base64
42 /// format as specified in RFC4648.  Space characters (spaces, tabs, newlines)
43 /// can be included in \c input and will be ignored.  Without spaces, the
44 /// length of string must be a multiple of 4 bytes with necessary paddings.
45 /// Also it must be encoded using the canonical encoding (see RFC4648).
46 /// If any of these conditions is not met, an exception of class
47 /// \c isc::BadValue will be thrown.
48 ///
49 /// If \c result doesn't have sufficient capacity to store all decoded %data
50 /// and memory allocation fails, a corresponding standard exception will be
51 /// thrown.  If the caller knows the necessary length (which can in theory
52 /// be calculated from the input string), this situation can be avoided by
53 /// reserving sufficient space for \c result beforehand.
54 ///
55 /// Any existing %data in \c result will be removed.  This is the case in some
56 /// of the cases where an exception is thrown; that is, this function only
57 /// provides the basic exception guarantee.
58 ///
59 /// \param input A text encoded in the base64 format.
60 /// \param result A vector in which the decoded %data is to be stored.
61 void decodeBase64(const std::string& input, std::vector<uint8_t>& result);
62 
63 } // namespace encode
64 } // namespace util
65 } // namespace isc
66 
67 #endif  // BASE64_H
68 
69 // Local Variables:
70 // mode: c++
71 // End:
72