# HG changeset patch # User John W. Eaton # Date 1346273005 14400 # Node ID fa0118cb67d96675a75e4e9a7a58b3c52bb605c1 # Parent 50156b22f87c3c9048bb29e03a6c3b575b8c3fb4 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. diff -r 50156b22f87c -r fa0118cb67d9 libinterp/interpfcn/data.cc --- a/libinterp/interpfcn/data.cc Wed Aug 29 16:31:39 2012 -0400 +++ b/libinterp/interpfcn/data.cc Wed Aug 29 16:43:25 2012 -0400 @@ -38,10 +38,10 @@ #include #include -#include #include "lo-ieee.h" #include "lo-math.h" +#include "oct-base64.h" #include "oct-time.h" #include "str-vec.h" #include "quit.h" @@ -7235,22 +7235,8 @@ return retval; } -bool do_base64_encode (const char * inc, const size_t inlen, char *& out) -{ - bool ret = false; - size_t outlen = base64_encode_alloc (inc, inlen, &out); - - if (! out && outlen == 0 && inlen != 0) - error ("base64_encode: input array too large"); - else if (! out) - error ("base64_encode: memory allocation error"); - else - ret = true; - - return ret; -} - -DEFUN (base64_encode, args, , "-*- texinfo -*-\n\ +DEFUN (base64_encode, args, , + "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {@var{s} =} base64_encode (@var{x})\n\ Encode a double matrix or array @var{x} into the base64 format string\n\ @var{s}.\n\ @@ -7282,7 +7268,7 @@ reinterpret_cast (in.data ()); \ char* out; \ if (! error_state \ - && do_base64_encode (inc, inlen, out)) \ + && octave_base64_encode (inc, inlen, &out)) \ retval(0) = octave_value (out); \ } @@ -7308,7 +7294,7 @@ inc = reinterpret_cast (in.data ()); char* out; if (! error_state - && do_base64_encode (inc, inlen, out)) + && octave_base64_encode (inc, inlen, &out)) retval(0) = octave_value (out); } else @@ -7320,7 +7306,7 @@ inc = reinterpret_cast (in.data ()); char* out; if (! error_state - && do_base64_encode (inc, inlen, out)) + && octave_base64_encode (inc, inlen, &out)) retval(0) = octave_value (out); } } @@ -7342,16 +7328,17 @@ %!error base64_encode (struct ()) */ -DEFUN (base64_decode, args, , "-*- texinfo -*-\n\ +DEFUN (base64_decode, args, , + "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {@var{x} =} base64_decode (@var{s})\n\ @deftypefnx {Built-in Function} {@var{x} =} base64_decode (@var{s}, @var{dims})\n\ -Decode the double matrix or array @var{x} from the base64 format string\n\ +Decode the double matrix or array @var{x} from the base64 encoded string\n\ @var{s}. The optional input parameter @var{dims} should be a vector\n\ containing the dimensions of the decoded array.\n\ @seealso{base64_encode}\n\ @end deftypefn") { - octave_value_list retval; + octave_value retval; int nargin = args.length (); @@ -7359,54 +7346,32 @@ print_usage (); else { - dim_vector new_dims; - Array res; + dim_vector dims; if (nargin > 1) { - const Array new_size = + const Array size = args(1).octave_idx_type_vector_value (); + if (! error_state) { - new_dims = dim_vector::alloc (new_size.length ()); - for (octave_idx_type i = 0; i < new_size.length (); i++) - new_dims(i) = new_size(i); + dims = dim_vector::alloc (size.length ()); + for (octave_idx_type i = 0; i < size.length (); i++) + dims(i) = size(i); } } - const std::string in = args(0).string_value (); + const std::string str = args(0).string_value (); if (! error_state) { - const char *inc = &(in[0]); - char *out; - size_t inlen = in.length (), outlen; - - bool ok = base64_decode_alloc (inc, inlen, &out, &outlen); - - if (! ok) - error ("base64_decode: input was not valid base64"); - else if (! out) - error ("base64_decode: memory allocation error"); - else - { - if ((outlen % (sizeof (double) / sizeof (char))) != 0) - error ("base64_decode: incorrect input size"); - else - { - octave_idx_type l; - l = (outlen * sizeof (char)) / sizeof (double); - res.resize1 (l); - double *dout = reinterpret_cast (out); - std::copy (dout, dout + l, res.fortran_vec ()); - - if (nargin > 1) - retval(0) = octave_value (res).reshape (new_dims); - else - retval(0) = octave_value (res); - } - } - } + Array res = octave_base64_decode (str); + + if (nargin > 1) + res = res.reshape (dims); + + retval = res; + } } return retval; diff -r 50156b22f87c -r fa0118cb67d9 liboctave/Makefile.am --- a/liboctave/Makefile.am Wed Aug 29 16:31:39 2012 -0400 +++ b/liboctave/Makefile.am Wed Aug 29 16:43:25 2012 -0400 @@ -214,6 +214,7 @@ lo-utils.h \ mach-info.h \ oct-alloc.h \ + oct-base64.h \ oct-binmap.h \ oct-cmplx.h \ oct-convn.h \ @@ -437,6 +438,7 @@ lo-utils.cc \ mach-info.cc \ oct-alloc.cc \ + oct-base64.cc \ oct-convn.cc \ oct-env.cc \ oct-fftw.cc \ diff -r 50156b22f87c -r fa0118cb67d9 liboctave/oct-base64.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-base64.cc Wed Aug 29 16:43:25 2012 -0400 @@ -0,0 +1,89 @@ +/* + +Copyright (C) 2012 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include + +#include + +bool +octave_base64_encode (const char *inc, const size_t inlen, char **out) +{ + bool ret = false; + + size_t outlen = base64_encode_alloc (inc, inlen, out); + + if (! *out) + { + if (outlen == 0 && inlen != 0) + (*current_liboctave_error_handler) + ("base64_encode: input array too large"); + else + (*current_liboctave_error_handler) + ("base64_encode: memory allocation error"); + } + else + ret = true; + + return ret; +} + +Array +octave_base64_decode (const std::string& str) +{ + Array retval; + + const char *inc = &(str[0]); + + char *out; + size_t outlen; + + bool ok = base64_decode_alloc (inc, str.length (), &out, &outlen); + + if (! ok) + (*current_liboctave_error_handler) + ("base64_decode: input was not valid base64"); + else if (! out) + (*current_liboctave_error_handler) + ("base64_decode: memory allocation error"); + else + { + if ((outlen % (sizeof (double) / sizeof (char))) != 0) + (*current_liboctave_error_handler) + ("base64_decode: incorrect input size"); + else + { + octave_idx_type len = (outlen * sizeof (char)) / sizeof (double); + retval.resize (dim_vector (1, len)); + double *dout = reinterpret_cast (out); + std::copy (dout, dout + len, retval.fortran_vec ()); + } + } + + return retval; +} + diff -r 50156b22f87c -r fa0118cb67d9 liboctave/oct-base64.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-base64.h Wed Aug 29 16:43:25 2012 -0400 @@ -0,0 +1,37 @@ +/* + +Copyright (C) 2012 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#if !defined (octave_base64_h) +#define octave_base64_h 1 + +#include + +template class Array; + +extern OCTAVE_API bool +octave_base64_encode (const char *inc, const size_t inlen, char **out); + +extern OCTAVE_API Array +octave_base64_decode (const std::string& str); + +#endif +