GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-base64.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <algorithm>
28 
29 #include "Array.h"
30 #include "base64-wrappers.h"
31 #include "oct-base64.h"
32 
33 namespace octave
34 {
35  bool
36  base64_encode (const char *inc, const size_t inlen, char **out)
37  {
38  bool ret = false;
39 
40  size_t outlen = octave_base64_encode_alloc_wrapper (inc, inlen, out);
41 
42  if (! out)
43  {
44  if (outlen == 0 && inlen != 0)
45  (*current_liboctave_error_handler)
46  ("base64_encode: input array too large");
47  else
49  ("base64_encode: memory allocation error");
50  }
51  else
52  ret = true;
53 
54  return ret;
55  }
56 
59  {
61 
62  double *out;
63  size_t outlen;
64 
65  bool ok
66  = octave_base64_decode_alloc_wrapper (str.data (), str.length (),
67  reinterpret_cast<char **> (&out),
68  &outlen);
69 
70  if (! ok)
71  (*current_liboctave_error_handler)
72  ("base64_decode: input was not valid base64");
73 
74  if (! out)
75  (*current_liboctave_error_handler)
76  ("base64_decode: memory allocation error");
77 
78  if ((outlen % (sizeof (double) / sizeof (char))) != 0)
79  {
80  ::free (out);
81  (*current_liboctave_error_handler)
82  ("base64_decode: incorrect input size");
83  }
84  else
85  {
86  octave_idx_type len = (outlen * sizeof (char)) / sizeof (double);
87  retval.resize (dim_vector (1, len));
88  std::copy (out, out + len, retval.fortran_vec ());
89  ::free (out);
90  }
91 
92  return retval;
93  }
94 }
95 
96 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
97 
98 bool
99 octave_base64_encode (const char *inc, const size_t inlen, char **out)
100 {
101  return octave::base64_encode (inc, inlen, out);
102 }
103 
105 octave_base64_decode (const std::string& str)
106 {
107  return octave::base64_decode (str);
108 }
109 
110 #endif
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
bool base64_encode(const char *inc, const size_t inlen, char **out)
Definition: oct-base64.cc:36
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
Array< double > base64_decode(const std::string &str)
Definition: oct-base64.cc:58
std::string str
Definition: hash.cc:118
octave_value retval
Definition: data.cc:6246
size_t octave_base64_encode_alloc_wrapper(const char *in, size_t inlen, char **out)
bool octave_base64_decode_alloc_wrapper(const char *in, size_t inlen, char **out, size_t *outlen)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888