changeset 15253: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 50156b22f87c
children 701532350420
files libinterp/interpfcn/data.cc liboctave/Makefile.am liboctave/oct-base64.cc liboctave/oct-base64.h
diffstat 4 files changed, 152 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/interpfcn/data.cc
+++ b/libinterp/interpfcn/data.cc
@@ -38,10 +38,10 @@
 #include <ctime>
 
 #include <string>
-#include <base64.h>
 
 #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<const char*> (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<const char*> (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<const char*> (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<double> res;
+      dim_vector dims;
 
       if (nargin > 1)
         {
-          const Array<octave_idx_type> new_size =
+          const Array<octave_idx_type> 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<double*> (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<double> res = octave_base64_decode (str);
+
+          if (nargin > 1)
+            res = res.reshape (dims);
+
+          retval = res;
+        }        
     }
 
   return retval; 
--- a/liboctave/Makefile.am
+++ b/liboctave/Makefile.am
@@ -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 \
new file mode 100644
--- /dev/null
+++ b/liboctave/oct-base64.cc
@@ -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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <algorithm>
+
+#include <base64.h>
+
+#include <Array.h>
+
+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<double>
+octave_base64_decode (const std::string& str)
+{
+  Array<double> 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<double*> (out);
+          std::copy (dout, dout + len, retval.fortran_vec ());
+        }
+    }
+
+  return retval;
+}
+
new file mode 100644
--- /dev/null
+++ b/liboctave/oct-base64.h
@@ -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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if !defined (octave_base64_h)
+#define octave_base64_h 1
+
+#include <string>
+
+template<class T> class Array;
+
+extern OCTAVE_API bool
+octave_base64_encode (const char *inc, const size_t inlen, char **out);
+
+extern OCTAVE_API Array<double>
+octave_base64_decode (const std::string& str);
+
+#endif
+