Mercurial > code
annotate C++/Base64.cpp @ 238:b97d75a78e22
Add Base64, base64 encoding and decoding
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 11 Sep 2014 17:21:51 +0200 |
parents | |
children |
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 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
24 char Base64::lookup(int value) noexcept |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 { |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
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 return table[value]; |
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 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 int Base64::rlookup(char ch) |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 { |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
33 if (ch == '+') |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 return 62; |
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 63; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
37 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
38 if (ch >= '0' && ch <= '9') |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
39 return ch + 4; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
40 if (ch >= 'A' && ch <= 'Z') |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
41 return ch - 65; |
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 - 71; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
44 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 throw std::invalid_argument("not a valid base64 string"); |
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 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 std::string Base64::encode(const std::string &input) |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
49 { |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 std::string result; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 std::istringstream iss(input, std::istringstream::in); |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
53 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
|
54 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
55 return 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 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
58 std::string Base64::decode(const std::string &input) |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
59 { |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 std::string result; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
61 std::istringstream iss(input, std::istringstream::in); |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 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
|
64 |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
65 return result; |
b97d75a78e22
Add Base64, base64 encoding and decoding
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 } |