]>
src.bluestatic.org Git - macgdbp.git/blob - modp_b64/modp_b64.h
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.
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
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);
51 * printf("b64 = %s\n", dest);
56 int modp_b64_encode(char* dest
, const char* str
, int len
);
59 * Decode a base64 encoded string
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.
65 * dest should be allocated by the caller to contain at least
68 * Returns the length (strlen) of the output, or -1 if unable to
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 }
79 int modp_b64_decode(char* dest
, const char* src
, int len
);
82 * Given a source string of length len, this returns the amount of
83 * memory the destination string should have.
85 * remember, this is integer math
86 * 3 bytes turn into 4 chars
87 * ceiling[len / 3] * 4 + 1
89 * +1 is for any extra null.
91 #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
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
102 #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
105 * Will return the strlen of the output from encoding.
106 * This may be less than the required number of bytes allocated.
108 * This allows you to 'deserialized' a struct
110 * char* b64encoded = "...";
111 * int len = strlen(b64encoded);
113 * struct datastuff foo;
114 * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
119 * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
124 * // foo is filled out now
127 #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
134 inline std::string
& modp_b64_encode(std::string
& s
)
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
);
144 * base 64 decode a string (self-modifing)
145 * On failure, the string is empty.
147 * This function is for C++ only (duh)
149 * \param[in,out] s the string to be decoded
150 * \return a reference to the input string
152 inline std::string
& modp_b64_decode(std::string
& s
)
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());
159 x
.erase(d
, std::string::npos
);
165 #endif /* __cplusplus */
167 #endif /* MODP_B64 */