# HG changeset patch
# User Carlo de Falco
# Date 1343288769 -7200
# Node ID 37467312793fca6c63eb374ff904b678925ae734
# Parent 094bc0a145a180637b5b78a377c2f90f947acb1b
Add functions to encode/decode double arrays to/from base64.
* data.cc (base64_encode): new function.
* data.cc (base64_decode): new function.
diff --git a/src/data.cc b/src/data.cc
--- a/src/data.cc
+++ b/src/data.cc
@@ -3,6 +3,7 @@
Copyright (C) 1994-2012 John W. Eaton
Copyright (C) 2009 Jaroslav Hajek
Copyright (C) 2009-2010 VZLU Prague
+Copyright (C) 2012 Carlo de Falco
This file is part of Octave.
@@ -37,6 +38,10 @@
#include
#include
+extern "C"
+{
+#include
+}
#include "lo-ieee.h"
#include "lo-math.h"
@@ -7207,3 +7212,104 @@
return retval;
}
+
+DEFUN (base64_encode, args, nargout,
+ "-*- texinfo -*-\n\
address@hidden {Loadable Function} address@hidden =} base64_encode (@var{x})\n\
+Encode a double matrix or array @var{x} into the base64 format string @var{s}.\n\
+Encoding different numeric types is currently not supported, variables of such types \
+will be converted to double before encoding.\n\
address@hidden
address@hidden deftypefn")
+{
+ octave_value_list retval;
+
+ int nargin = args.length ();
+ if (nargin != 1)
+ print_usage ();
+ else
+ {
+ Array in = args(0).array_value ();
+ if (! error_state)
+ {
+ char* inc = (char*) in.fortran_vec ();
+ size_t inlen = in.numel () * sizeof (double) / sizeof (char);
+
+ char* out;
+
+ size_t outlen = base64_encode_alloc (inc, inlen, &out);
+ if (out == NULL && outlen == 0 && inlen != 0)
+ error ("base64_encode: input array too large.");
+ else if (out == NULL)
+ error ("base64_encode: memory allocation error.");
+
+ std::string s (out);
+ retval(0) = octave_value (s);
+ }
+ }
+ return retval;
+}
+
+DEFUN (base64_decode, args, nargout,
+ "-*- texinfo -*-\n\
address@hidden {Loadable Function} address@hidden =} base64_decode (@var{s}, @var{dims})\n\
+Decode the double matrix or array @var{x} from the base64 format string @var{s}.\n\
+The optional input parameter @var{dims} should be a vector containing the dimensions \
+of the decoded array.\n\
address@hidden
address@hidden deftypefn")
+{
+ octave_value_list retval;
+ dim_vector new_dims;
+ Array res;
+
+ int nargin = args.length ();
+ if (nargin < 1 || nargin > 2)
+ print_usage ();
+ else
+ {
+
+ if (nargin > 1)
+ {
+ Array new_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);
+ }
+ }
+
+ std::string in = args(0).string_value ();
+
+ if (! error_state)
+ {
+ char *inc = &(in[0]),
+ *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 == NULL)
+ 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 = (outlen * sizeof (char)) / sizeof (double);
+ res.resize1 (l);
+ for (octave_idx_type i = 0; i < l; i++)
+ res.fortran_vec ()[i] = ((double*) out)[i];
+
+ res.reshape (new_dims);
+ retval(0) = octave_value (res);
+ }
+ }
+ }
+ }
+ return retval;
+}