28 #ifndef SCRIBO_CONVERT_FROM_BASE64_HH
29 # define SCRIBO_CONVERT_FROM_BASE64_HH
70 # include <mln/border/resize.hh>
71 # include <mln/core/image/image2d.hh>
72 # include <mln/util/array.hh>
85 void from_base64(
const util::array<unsigned char>& data64,
92 void from_base64(
const QString& data64,
Image<I>& output_);
97 # ifndef MLN_INCLUDE_ONLY
106 static const char cd64[]=
"|$$$}rstuvwxyz{$$$$$$$>?../../../../doc/ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
115 decodeblock(
unsigned char in[4],
unsigned char out[3])
117 out[ 0 ] = (
unsigned char) (in[0] << 2 | in[1] >> 4);
118 out[ 1 ] = (
unsigned char) (in[1] << 4 | in[2] >> 2);
119 out[ 2 ] = (
unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
123 template <
typename V,
typename I>
125 from_base64_(
const V& data64,
const unsigned length,
Image<I>& output_)
127 mln_trace(
"scribo::convert::from_base64_");
129 mln_precondition(
exact(output_).is_valid());
130 using namespace internal;
132 I& output =
exact(output_);
134 unsigned char in[4], out[3], v;
139 unsigned char *ptr = (
unsigned char *)output.buffer();
140 unsigned char *end_ptr = (
unsigned char *)(output.buffer() + output.nelements());
142 for (
unsigned idx = 0; idx < length;)
144 for(len = 0, i = 0; i < 4 && idx < length; i++)
147 while(idx < length && v == 0)
149 v = (
unsigned char) data64[idx++];
150 v = (
unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
153 v = (
unsigned char) ((v ==
'$') ? 0 : v - 61);
161 in[ i ] = (
unsigned char) (v - 1);
171 decodeblock(in, out);
172 for(i = 0; i < len - 1 && ptr != end_ptr; i++)
185 template <
typename I>
187 from_base64(
const util::array<unsigned char>& data64,
Image<I>& output_)
189 mln_trace(
"scribo::convert::from_base64");
191 internal::from_base64_(data64, data64.nelements(), output_);
198 template <
typename I>
200 from_base64(
const QString& data64,
Image<I>& output_)
202 mln_trace(
"scribo::convert::from_base64");
204 QByteArray data64_ = data64.toAscii();
205 internal::from_base64_(data64_.constData(), data64_.size(), output_);
212 # endif // ! MLN_INCLUDE_ONLY
218 #endif // ! SCRIBO_CONVERT_FROM_BASE64_HH