GNU Octave  4.2.1
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-2017 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 #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 bool
34 octave_base64_encode (const char *inc, const size_t inlen, char **out)
35 {
36  bool ret = false;
37 
38  size_t outlen = octave_base64_encode_alloc_wrapper (inc, inlen, out);
39 
40  if (! out)
41  {
42  if (outlen == 0 && inlen != 0)
43  (*current_liboctave_error_handler)
44  ("base64_encode: input array too large");
45  else
47  ("base64_encode: memory allocation error");
48  }
49  else
50  ret = true;
51 
52  return ret;
53 }
54 
57 {
59 
60  char *out;
61  size_t outlen;
62 
63  bool ok = octave_base64_decode_alloc_wrapper (str.data (), str.length (),
64  &out, &outlen);
65 
66  if (! ok)
67  (*current_liboctave_error_handler)
68  ("base64_decode: input was not valid base64");
69  if (! out)
70  (*current_liboctave_error_handler)
71  ("base64_decode: memory allocation error");
72 
73  if ((outlen % (sizeof (double) / sizeof (char))) != 0)
74  {
75  ::free (out);
76  (*current_liboctave_error_handler)
77  ("base64_decode: incorrect input size");
78  }
79  else
80  {
81  octave_idx_type len = (outlen * sizeof (char)) / sizeof (double);
82  retval.resize (dim_vector (1, len));
83  double *dout = reinterpret_cast<double*> (out);
84  std::copy (dout, dout + len, retval.fortran_vec ());
85  ::free (out);
86  }
87 
88  return retval;
89 }
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
Array< double > octave_base64_decode(const std::string &str)
Definition: oct-base64.cc:56
std::string str
Definition: hash.cc:118
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1028
octave_value retval
Definition: data.cc:6294
size_t octave_base64_encode_alloc_wrapper(const char *in, size_t inlen, char **out)
bool octave_base64_encode(const char *inc, const size_t inlen, char **out)
Definition: oct-base64.cc:34
void free(void *)
issues an error eealso double
Definition: ov-bool-mat.cc:594
bool octave_base64_decode_alloc_wrapper(const char *in, size_t inlen, char **out, size_t *outlen)
const T * fortran_vec(void) const
Definition: Array.h:584
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:854