GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-base64.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <algorithm>
28 
29 #include <base64.h>
30 
31 #include "Array.h"
32 #include "oct-base64.h"
33 
34 bool
35 octave_base64_encode (const char *inc, const size_t inlen, char **out)
36 {
37  bool ret = false;
38 
39  size_t outlen = base64_encode_alloc (inc, inlen, out);
40 
41  if (! out)
42  {
43  if (outlen == 0 && inlen != 0)
44  (*current_liboctave_error_handler)
45  ("base64_encode: input array too large");
46  else
48  ("base64_encode: memory allocation error");
49  }
50  else
51  ret = true;
52 
53  return ret;
54 }
55 
57 octave_base64_decode (const std::string& str)
58 {
59  Array<double> retval;
60 
61  const char *inc = &(str[0]);
62 
63  char *out;
64  size_t outlen;
65 
66  bool ok = base64_decode_alloc (inc, str.length (), &out, &outlen);
67 
68  if (! ok)
69  (*current_liboctave_error_handler)
70  ("base64_decode: input was not valid base64");
71  else if (! out)
72  (*current_liboctave_error_handler)
73  ("base64_decode: memory allocation error");
74  else
75  {
76  if ((outlen % (sizeof (double) / sizeof (char))) != 0)
77  {
78  ::free (out);
79  (*current_liboctave_error_handler)
80  ("base64_decode: incorrect input size");
81  }
82  else
83  {
84  octave_idx_type len = (outlen * sizeof (char)) / sizeof (double);
85  retval.resize (dim_vector (1, len));
86  double *dout = reinterpret_cast<double*> (out);
87  std::copy (dout, dout + len, retval.fortran_vec ());
88  ::free (out);
89  }
90  }
91 
92  return retval;
93 }
94