1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 
4 /**
5  * \file
6  * <PRE>
7  * High performance base64 encoder / decoder
8  * Version 1.3 -- 17-Mar-2006
9  *
10  * Copyright &copy; 2005, 2006, Nick Galbreath -- nickg [at] modp [dot] com
11  * All rights reserved.
12  *
13  * http://modp.com/release/base64
14  *
15  * Released under bsd license.  See modp_b64.c for details.
16  * </pre>
17  *
18  * The default implementation is the standard b64 encoding with padding.
19  * It's easy to change this to use "URL safe" characters and to remove
20  * padding.  See the modp_b64.c source code for details.
21  *
22  */
23 
24 #ifndef MODP_B64
25 #define MODP_B64
26 
27 #include <stddef.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * Encode a raw binary string into base 64.
35  * src contains the bytes
36  * len contains the number of bytes in the src
37  * dest should be allocated by the caller to contain
38  *   at least modp_b64_encode_len(len) bytes (see below)
39  *   This will contain the null-terminated b64 encoded result
40  * returns length of the destination string plus the ending null byte
41  *    i.e.  the result will be equal to strlen(dest) + 1
42  *
43  * Example
44  *
45  * \code
46  * char* src = ...;
47  * int srclen = ...; //the length of number of bytes in src
48  * char* dest = (char*) malloc(modp_b64_encode_len);
49  * int len = modp_b64_encode(dest, src, sourcelen);
50  * if (len == -1) {
51  *   printf("Error\n");
52  * } else {
53  *   printf("b64 = %s\n", dest);
54  * }
55  * \endcode
56  *
57  */
58 size_t modp_b64_encode(char* dest, const char* str, size_t len);
59 
60 /**
61  * Decode a base64 encoded string
62  *
63  * src should contain exactly len bytes of b64 characters.
64  *     if src contains -any- non-base characters (such as white
65  *     space, -1 is returned.
66  *
67  * dest should be allocated by the caller to contain at least
68  *    len * 3 / 4 bytes.
69  *
70  * Returns the length (strlen) of the output, or -1 if unable to
71  * decode
72  *
73  * \code
74  * char* src = ...;
75  * int srclen = ...; // or if you don't know use strlen(src)
76  * char* dest = (char*) malloc(modp_b64_decode_len(srclen));
77  * int len = modp_b64_decode(dest, src, sourcelen);
78  * if (len == -1) { error }
79  * \endcode
80  */
81 size_t modp_b64_decode(char* dest, const char* src, size_t len);
82 
83 /**
84  * Given a source string of length len, this returns the amount of
85  * memory the destination string should have.
86  *
87  * remember, this is integer math
88  * 3 bytes turn into 4 chars
89  * ceiling[len / 3] * 4 + 1
90  *
91  * +1 is for any extra null.
92  */
93 #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
94 
95 /**
96  * Given a base64 string of length len,
97  *   this returns the amount of memory required for output string
98  *  It maybe be more than the actual number of bytes written.
99  * NOTE: remember this is integer math
100  * this allocates a bit more memory than traditional versions of b64
101  * decode  4 chars turn into 3 bytes
102  * floor[len * 3/4] + 2
103  */
104 #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
105 
106 /**
107  * Will return the strlen of the output from encoding.
108  * This may be less than the required number of bytes allocated.
109  *
110  * This allows you to 'deserialized' a struct
111  * \code
112  * char* b64encoded = "...";
113  * int len = strlen(b64encoded);
114  *
115  * struct datastuff foo;
116  * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
117  *    // wrong size
118  *    return false;
119  * } else {
120  *    // safe to do;
121  *    if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
122  *      // bad characters
123  *      return false;
124  *    }
125  * }
126  * // foo is filled out now
127  * \endcode
128  */
129 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
130 
131 #define MODP_B64_ERROR ((size_t)-1)
132 
133 #ifdef __cplusplus
134 }
135 
136 #include <string>
137 
modp_b64_encode(std::string & s)138 inline std::string& modp_b64_encode(std::string& s)
139 {
140     std::string x(modp_b64_encode_len(s.size()), '\0');
141     size_t d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), (int)s.size());
142     x.erase(d, std::string::npos);
143     s.swap(x);
144     return s;
145 }
146 
147 /**
148  * base 64 decode a string (self-modifing)
149  * On failure, the string is empty.
150  *
151  * This function is for C++ only (duh)
152  *
153  * \param[in,out] s the string to be decoded
154  * \return a reference to the input string
155  */
modp_b64_decode(std::string & s)156 inline std::string& modp_b64_decode(std::string& s)
157 {
158     std::string x(modp_b64_decode_len(s.size()), '\0');
159     size_t d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), (int)s.size());
160     if (d == MODP_B64_ERROR) {
161         x.clear();
162     } else {
163         x.erase(d, std::string::npos);
164     }
165     s.swap(x);
166     return s;
167 }
168 
169 #endif /* __cplusplus */
170 
171 #endif /* MODP_B64 */
172