comparison liboctave/oct-base64.cc @ 15252:fa0118cb67d9

move base64 encode and decode functionality to liboctave * oct-base64.h, oct-base64.cc: New files. Extract core base64 encode and decode functionality from data.cc. * liboctave/Makefile.am (INCS): Add octave-base64.h to the list. (LIBOCTAVE_CXX_SOURCES): Add octave-base64.cc to the list. * data.cc: Don't include base64.h. Do include oct-base64.h. (do_base64_encode): Delete. (Fbase64_encode): Call octave_base64_encode, not do_base64_encode. (Fbase64_decode): Declare retval as octave_value, not octave_value_list. Simplify using octave_base64_decode.
author John W. Eaton <jwe@octave.org>
date Wed, 29 Aug 2012 16:43:25 -0400
parents
children 6be46886099f
comparison
equal deleted inserted replaced
15251:50156b22f87c 15252:fa0118cb67d9
1 /*
2
3 Copyright (C) 2012 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
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 = base64_encode_alloc (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
46 (*current_liboctave_error_handler)
47 ("base64_encode: memory allocation error");
48 }
49 else
50 ret = true;
51
52 return ret;
53 }
54
55 Array<double>
56 octave_base64_decode (const std::string& str)
57 {
58 Array<double> retval;
59
60 const char *inc = &(str[0]);
61
62 char *out;
63 size_t outlen;
64
65 bool ok = base64_decode_alloc (inc, str.length (), &out, &outlen);
66
67 if (! ok)
68 (*current_liboctave_error_handler)
69 ("base64_decode: input was not valid base64");
70 else if (! out)
71 (*current_liboctave_error_handler)
72 ("base64_decode: memory allocation error");
73 else
74 {
75 if ((outlen % (sizeof (double) / sizeof (char))) != 0)
76 (*current_liboctave_error_handler)
77 ("base64_decode: incorrect input size");
78 else
79 {
80 octave_idx_type len = (outlen * sizeof (char)) / sizeof (double);
81 retval.resize (dim_vector (1, len));
82 double *dout = reinterpret_cast<double*> (out);
83 std::copy (dout, dout + len, retval.fortran_vec ());
84 }
85 }
86
87 return retval;
88 }
89