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 © 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 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32 * Encode a raw binary string into base 64.
33 * src contains the bytes
34 * len contains the number of bytes in the src
35 * dest should be allocated by the caller to contain
36 * at least modp_b64_encode_len(len) bytes (see below)
37 * This will contain the null-terminated b64 encoded result
38 * returns length of the destination string plus the ending null byte
39 * i.e. the result will be equal to strlen(dest) + 1
40 *
41 * Example
42 *
43 * \code
44 * char* src = ...;
45 * int srclen = ...; //the length of number of bytes in src
46 * char* dest = (char*) malloc(modp_b64_encode_len);
47 * int len = modp_b64_encode(dest, src, sourcelen);
48 * if (len == -1) {
49 * printf("Error\n");
50 * } else {
51 * printf("b64 = %s\n", dest);
52 * }
53 * \endcode
54 *
55 */
56 int modp_b64_encode(char* dest, const char* str, int len);
57
58 /**
59 * Decode a base64 encoded string
60 *
61 * src should contain exactly len bytes of b64 characters.
62 * if src contains -any- non-base characters (such as white
63 * space, -1 is returned.
64 *
65 * dest should be allocated by the caller to contain at least
66 * len * 3 / 4 bytes.
67 *
68 * Returns the length (strlen) of the output, or -1 if unable to
69 * decode
70 *
71 * \code
72 * char* src = ...;
73 * int srclen = ...; // or if you don't know use strlen(src)
74 * char* dest = (char*) malloc(modp_b64_decode_len(srclen));
75 * int len = modp_b64_decode(dest, src, sourcelen);
76 * if (len == -1) { error }
77 * \endcode
78 */
79 int modp_b64_decode(char* dest, const char* src, int len);
80
81 /**
82 * Given a source string of length len, this returns the amount of
83 * memory the destination string should have.
84 *
85 * remember, this is integer math
86 * 3 bytes turn into 4 chars
87 * ceiling[len / 3] * 4 + 1
88 *
89 * +1 is for any extra null.
90 */
91 #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
92
93 /**
94 * Given a base64 string of length len,
95 * this returns the amount of memory required for output string
96 * It maybe be more than the actual number of bytes written.
97 * NOTE: remember this is integer math
98 * this allocates a bit more memory than traditional versions of b64
99 * decode 4 chars turn into 3 bytes
100 * floor[len * 3/4] + 2
101 */
102 #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
103
104 /**
105 * Will return the strlen of the output from encoding.
106 * This may be less than the required number of bytes allocated.
107 *
108 * This allows you to 'deserialized' a struct
109 * \code
110 * char* b64encoded = "...";
111 * int len = strlen(b64encoded);
112 *
113 * struct datastuff foo;
114 * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
115 * // wrong size
116 * return false;
117 * } else {
118 * // safe to do;
119 * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
120 * // bad characters
121 * return false;
122 * }
123 * }
124 * // foo is filled out now
125 * \endcode
126 */
127 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
128
129 #ifdef __cplusplus
130 }
131
132 #include <string>
133
modp_b64_encode(std::string & s)134 inline std::string& modp_b64_encode(std::string& s)
135 {
136 std::string x(modp_b64_encode_len(s.size()), '\0');
137 int d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), s.size());
138 x.erase(d, std::string::npos);
139 s.swap(x);
140 return s;
141 }
142
143 /**
144 * base 64 decode a string (self-modifing)
145 * On failure, the string is empty.
146 *
147 * This function is for C++ only (duh)
148 *
149 * \param[in,out] s the string to be decoded
150 * \return a reference to the input string
151 */
modp_b64_decode(std::string & s)152 inline std::string& modp_b64_decode(std::string& s)
153 {
154 std::string x(modp_b64_decode_len(s.size()), '\0');
155 int d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), s.size());
156 if (d < 0) {
157 x.clear();
158 } else {
159 x.erase(d, std::string::npos);
160 }
161 s.swap(x);
162 return s;
163 }
164
165 #endif /* __cplusplus */
166
167 #endif /* MODP_B64 */
168