annotate C++/modules/Base64/Base64.cpp @ 393:875b5ce19fda

Base64: remove class, use namespace
author David Demelier <markand@malikania.fr>
date Mon, 28 Sep 2015 15:16:31 +0200
parents 0b576ee64d45
children d5ec1174b707
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
238
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * Base64.cpp -- base64 encoding and decoding
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <iterator>
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <sstream>
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
21
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include "Base64.h"
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
23
393
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
24 namespace base64 {
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
25
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
26 char lookup(int value) noexcept
238
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 {
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
29
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 return table[value];
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 }
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
32
393
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
33 int rlookup(char ch)
238
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 {
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 if (ch == '+')
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 return 62;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 if (ch == '/')
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 return 63;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
39
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 if (ch >= '0' && ch <= '9')
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 return ch + 4;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 if (ch >= 'A' && ch <= 'Z')
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 return ch - 65;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 if (ch >= 'a' && ch <= 'z')
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 return ch - 71;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
46
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 throw std::invalid_argument("not a valid base64 string");
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 }
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
49
393
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
50 std::string encode(const std::string &input)
238
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 {
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 std::string result;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 std::istringstream iss(input, std::istringstream::in);
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
54
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 encode(std::istreambuf_iterator<char>(iss), std::istreambuf_iterator<char>(), std::back_inserter(result));
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
56
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 return result;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 }
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
59
393
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
60 std::string decode(const std::string &input)
238
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 {
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 std::string result;
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 std::istringstream iss(input, std::istringstream::in);
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
64
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 decode(std::istreambuf_iterator<char>(iss), std::istreambuf_iterator<char>(), std::back_inserter(result));
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
b97d75a78e22 Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 return result;
393
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
68 }
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
69
875b5ce19fda Base64: remove class, use namespace
David Demelier <markand@malikania.fr>
parents: 334
diff changeset
70 } // !base64