diff libinterp/interpfcn/data.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 336f42406671
children 4db96357fec9
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;