GNU Octave  8.1.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-2023 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 
37 
38 bool
39 base64_encode (const char *inc, const std::size_t inlen, char **out)
40 {
41  bool ret = false;
42 
43  std::ptrdiff_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 {
63  Array<double> retval;
64 
65  double *out;
66  std::ptrdiff_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 
99 base64_decode_bytes (const std::string& str)
100 {
102 
103  char *out;
104  std::ptrdiff_t outlen;
105 
106  bool ok
107  = octave_base64_decode_alloc_wrapper (str.data (), str.length (),
108  &out, &outlen);
109 
110  if (! ok)
111  (*current_liboctave_error_handler)
112  ("base64_decode: input was not valid base64");
113 
114  if (! out)
115  (*current_liboctave_error_handler)
116  ("base64_decode: memory allocation error");
117 
118  retval.resize (dim_vector (1, outlen));
119  std::copy (out, out + outlen, retval.fortran_vec ());
120  ::free (out);
121 
122  return retval;
123 }
124 
OCTAVE_END_NAMESPACE(octave)
ptrdiff_t octave_base64_encode_alloc_wrapper(const char *in, ptrdiff_t inlen, char **out)
bool octave_base64_decode_alloc_wrapper(const char *in, ptrdiff_t inlen, char **out, ptrdiff_t *outlen)
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array-base.cc:1032
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array-base.cc:1766
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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 std::size_t inlen, char **out)
Definition: oct-base64.cc:39
intNDArray< octave_uint8 > base64_decode_bytes(const std::string &str)
Definition: oct-base64.cc:99
void free(void *)
F77_RET_T len
Definition: xerbla.cc:61