Mercurial > code
comparison C++/Base64.h @ 243:73e5381d7baf
Base64: return output #291
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 13 Sep 2014 10:27:41 +0200 |
parents | a9883eeb9757 |
children |
comparison
equal
deleted
inserted
replaced
242:a9883eeb9757 | 243:73e5381d7baf |
---|---|
52 * InputIt must be InputIterator | 52 * InputIt must be InputIterator |
53 * OutputIt must be OutputIterator | 53 * OutputIt must be OutputIterator |
54 * | 54 * |
55 * @param input the beginning | 55 * @param input the beginning |
56 * @param end the end of the data | 56 * @param end the end of the data |
57 * @output the output destination | 57 * @param output the output destination |
58 * @return output | |
58 */ | 59 */ |
59 template <typename InputIt, typename OutputIt> | 60 template <typename InputIt, typename OutputIt> |
60 static void encode(InputIt input, InputIt end, OutputIt output) | 61 static OutputIt encode(InputIt input, InputIt end, OutputIt output) |
61 { | 62 { |
62 while (input != end) { | 63 while (input != end) { |
63 char inputbuf[3] = { 0, 0, 0 }; | 64 char inputbuf[3] = { 0, 0, 0 }; |
64 int count; | 65 int count; |
65 | 66 |
69 *output++ = lookup(inputbuf[0] >> 2 & 0x3f); | 70 *output++ = lookup(inputbuf[0] >> 2 & 0x3f); |
70 *output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f)); | 71 *output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f)); |
71 *output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03)); | 72 *output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03)); |
72 *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f); | 73 *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f); |
73 } | 74 } |
75 | |
76 return output; | |
74 } | 77 } |
75 | 78 |
76 /** | 79 /** |
77 * Decode the input to the output. Requirements: | 80 * Decode the input to the output. Requirements: |
78 * InputIt must be InputIterator | 81 * InputIt must be InputIterator |
79 * OutputIt must be OutputIterator | 82 * OutputIt must be OutputIterator |
80 * | 83 * |
81 * @param input the beginning | 84 * @param input the beginning |
82 * @param end the end of the data | 85 * @param end the end of the data |
83 * @output the output destination | 86 * @param output the output destination |
87 * @return output | |
84 * @throw std::invalid_argument on bad base64 string | 88 * @throw std::invalid_argument on bad base64 string |
85 */ | 89 */ |
86 template <typename InputIt, typename OutputIt> | 90 template <typename InputIt, typename OutputIt> |
87 static void decode(InputIt input, InputIt end, OutputIt output) | 91 static OutputIt decode(InputIt input, InputIt end, OutputIt output) |
88 { | 92 { |
89 while (input != end) { | 93 while (input != end) { |
90 char inputbuf[4] = { 0, 0, 0, 0 }; | 94 char inputbuf[4] = { 0, 0, 0, 0 }; |
91 int count; | 95 int count; |
92 | 96 |
103 if (inputbuf[2] != '=') | 107 if (inputbuf[2] != '=') |
104 *output++ = (inputbuf[1] << 4 & 0xf0) | (inputbuf[2] >> 2 & 0x0f); | 108 *output++ = (inputbuf[1] << 4 & 0xf0) | (inputbuf[2] >> 2 & 0x0f); |
105 if (inputbuf[3] != '=') | 109 if (inputbuf[3] != '=') |
106 *output++ = (inputbuf[2] << 6 & 0xc0) | (inputbuf[3] & 0x3f); | 110 *output++ = (inputbuf[2] << 6 & 0xc0) | (inputbuf[3] & 0x3f); |
107 } | 111 } |
112 | |
113 return output; | |
108 } | 114 } |
109 | 115 |
110 /** | 116 /** |
111 * Encode a string. | 117 * Encode a string. |
112 * | 118 * |