GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <algorithm>
31 
32 #include "Array.h"
33 #include "base64-wrappers.h"
34 #include "oct-base64.h"
35 
36 namespace octave
37 {
38  bool
39  base64_encode (const char *inc, const size_t inlen, char **out)
40  {
41  bool ret = false;
42 
43  size_t outlen = octave_base64_encode_alloc_wrapper (inc, inlen, out);
44 
45  if (! out)
46  {
47  if (outlen == 0 && inlen != 0)
48  (*current_liboctave_error_handler)
49  ("base64_encode: input array too large");
50  else
52  ("base64_encode: memory allocation error");
53  }
54  else
55  ret = true;
56 
57  return ret;
58  }
59 
61  base64_decode (const std::string& str)
62  {
64 
65  double *out;
66  size_t outlen;
67 
68  bool ok
69  = octave_base64_decode_alloc_wrapper (str.data (), str.length (),
70  reinterpret_cast<char **> (&out),
71  &outlen);
72 
73  if (! ok)
74  (*current_liboctave_error_handler)
75  ("base64_decode: input was not valid base64");
76 
77  if (! out)
78  (*current_liboctave_error_handler)
79  ("base64_decode: memory allocation error");
80 
81  if ((outlen % (sizeof (double) / sizeof (char))) != 0)
82  {
83  ::free (out);
84  (*current_liboctave_error_handler)
85  ("base64_decode: incorrect input size");
86  }
87  else
88  {
89  octave_idx_type len = (outlen * sizeof (char)) / sizeof (double);
90  retval.resize (dim_vector (1, len));
91  std::copy (out, out + len, retval.fortran_vec ());
92  ::free (out);
93  }
94 
95  return retval;
96  }
97 }
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)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:41
Array< double > base64_decode(const std::string &str)
Definition: oct-base64.cc:61
bool base64_encode(const char *inc, const size_t inlen, char **out)
Definition: oct-base64.cc:39
void free(void *)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
F77_RET_T len
Definition: xerbla.cc:61