$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
from_base64.hh
1 // Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 
27 
28 #ifndef SCRIBO_CONVERT_FROM_BASE64_HH
29 # define SCRIBO_CONVERT_FROM_BASE64_HH
30 
63 #include <cstdio>
64 #include <cstdlib>
65 
66 # ifdef HAVE_QT
67 # include <QString>
68 # endif // ! HAVE_QT
69 
70 # include <mln/border/resize.hh>
71 # include <mln/core/image/image2d.hh>
72 # include <mln/util/array.hh>
73 
74 
75 
76 namespace scribo
77 {
78 
79  namespace convert
80  {
81 
82  using namespace mln;
83 
84  template <typename I>
85  void from_base64(const util::array<unsigned char>& data64,
86  Image<I>& output);
87 
88 
89 # ifdef HAVE_QT
90 
91  template <typename I>
92  void from_base64(const QString& data64, Image<I>& output_);
93 
94 # endif // ! HAVE_QT
95 
96 
97 # ifndef MLN_INCLUDE_ONLY
98 
99 
100  namespace internal
101  {
102 
103  /*
104  ** Translation Table to decode
105  */
106  static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?../../../../doc/ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
107 
108  /*
109  ** decodeblock
110  **
111  ** decode 4 '6-bit' characters into 3 8-bit binary bytes
112  */
113  inline
114  void
115  decodeblock(unsigned char in[4], unsigned char out[3])
116  {
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]);
120  }
121 
122 
123  template <typename V, typename I>
124  void
125  from_base64_(const V& data64, const unsigned length, Image<I>& output_)
126  {
127  mln_trace("scribo::convert::from_base64_");
128 
129  mln_precondition(exact(output_).is_valid());
130  using namespace internal;
131 
132  I& output = exact(output_);
133 
134  unsigned char in[4], out[3], v;
135  int i, len;
136 
137  border::resize(output, 0); // Make sure there is no border!
138 
139  unsigned char *ptr = (unsigned char *)output.buffer();
140  unsigned char *end_ptr = (unsigned char *)(output.buffer() + output.nelements());
141 
142  for (unsigned idx = 0; idx < length;)
143  {
144  for(len = 0, i = 0; i < 4 && idx < length; i++)
145  {
146  v = 0;
147  while(idx < length && v == 0)
148  {
149  v = (unsigned char) data64[idx++];
150  v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
151  if(v)
152  {
153  v = (unsigned char) ((v == '$') ? 0 : v - 61);
154  }
155  }
156  if(idx < length)
157  {
158  len++;
159  if(v)
160  {
161  in[ i ] = (unsigned char) (v - 1);
162  }
163  }
164  else
165  {
166  in[i] = 0;
167  }
168  }
169  if(len)
170  {
171  decodeblock(in, out);
172  for(i = 0; i < len - 1 && ptr != end_ptr; i++)
173  {
174  *ptr++ = out[i];
175  }
176  }
177  }
178 
179  }
180 
181 
182  } // end of namespace scribo::convert::internal
183 
184 
185  template <typename I>
186  void
187  from_base64(const util::array<unsigned char>& data64, Image<I>& output_)
188  {
189  mln_trace("scribo::convert::from_base64");
190 
191  internal::from_base64_(data64, data64.nelements(), output_);
192 
193  }
194 
195 
196 # ifdef HAVE_QT
197 
198  template <typename I>
199  void
200  from_base64(const QString& data64, Image<I>& output_)
201  {
202  mln_trace("scribo::convert::from_base64");
203 
204  QByteArray data64_ = data64.toAscii();
205  internal::from_base64_(data64_.constData(), data64_.size(), output_);
206 
207  }
208 
209 # endif // ! HAVE_QT
210 
211 
212 # endif // ! MLN_INCLUDE_ONLY
213 
214  } // end of namespace scribo::convert
215 
216 } // end of namespace scribo
217 
218 #endif // ! SCRIBO_CONVERT_FROM_BASE64_HH