1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
7 * High performance base64 encoder / decoder
8 * Version 1.3 -- 17-Mar-2006
10 * Copyright © 2005, 2006, Nick Galbreath -- nickg [at] modp [dot] com
11 * All rights reserved.
13 * http://modp.com/release/base64
15 * Released under bsd license. See modp_b64.c for details.
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.
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
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);
53 * printf("b64 = %s\n", dest);
58 size_t modp_b64_encode(char* dest
, const char* str
, size_t len
);
61 * Decode a base64 encoded string
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.
67 * dest should be allocated by the caller to contain at least
70 * Returns the length (strlen) of the output, or -1 if unable to
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 }
81 size_t modp_b64_decode(char* dest
, const char* src
, size_t len
);
84 * Given a source string of length len, this returns the amount of
85 * memory the destination string should have.
87 * remember, this is integer math
88 * 3 bytes turn into 4 chars
89 * ceiling[len / 3] * 4 + 1
91 * +1 is for any extra null.
93 #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
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
104 #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
107 * Will return the strlen of the output from encoding.
108 * This may be less than the required number of bytes allocated.
110 * This allows you to 'deserialized' a struct
112 * char* b64encoded = "...";
113 * int len = strlen(b64encoded);
115 * struct datastuff foo;
116 * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
121 * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
126 * // foo is filled out now
129 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
131 #define MODP_B64_ERROR ((size_t)-1)
138 inline std::string
& modp_b64_encode(std::string
& s
)
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
);
148 * base 64 decode a string (self-modifing)
149 * On failure, the string is empty.
151 * This function is for C++ only (duh)
153 * \param[in,out] s the string to be decoded
154 * \return a reference to the input string
156 inline std::string
& modp_b64_decode(std::string
& s
)
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
) {
163 x
.erase(d
, std::string::npos
);
169 #endif /* __cplusplus */
171 #endif /* MODP_B64 */