changeset 21889:69afa5dce885

hide gnulib hash function headers * liboctave/wrappers/hash-wrappers.c, liboctave/wrappers/hash-wrappers.h: New files. Wrap gnulib hash functions and hide gnulib headers. * liboctave/util/module.mk, liboctave/wrappers/module.mk: Update. * liboctave/util/lo-hash.cc, liboctave/util/lo-hash.h: New files. Provide simple C++ interface to hashing functions. * hash.cc: Use new hash function.
author John W. Eaton <jwe@octave.org>
date Mon, 13 Jun 2016 18:36:16 -0400
parents 00f1249f2483
children 5dc59e7af536
files libinterp/corefcn/hash.cc liboctave/util/lo-hash.cc liboctave/util/lo-hash.h liboctave/util/module.mk liboctave/wrappers/hash-wrappers.c liboctave/wrappers/hash-wrappers.h liboctave/wrappers/module.mk
diffstat 7 files changed, 378 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/hash.cc	Mon Jun 13 16:01:26 2016 -0400
+++ b/libinterp/corefcn/hash.cc	Mon Jun 13 18:36:16 2016 -0400
@@ -38,17 +38,14 @@
 #  include "config.h"
 #endif
 
-#include <algorithm>
-#include <iomanip>
-#include <sstream>
+#include <string>
+
+#include "lo-hash.h"
 
 #include "defun.h"
-#include "md2.h"
-#include "md4.h"
-#include "md5.h"
-#include "sha1.h"
-#include "sha256.h"
-#include "sha512.h"
+#include "error.h"
+#include "ov.h"
+#include "ovl.h"
 
 DEFUN (hash, args, ,
        "-*- texinfo -*-\n\
@@ -117,69 +114,10 @@
   if (args.length () != 2)
     print_usage ();
 
-  std::string hfun = args(0).string_value ();
+  std::string hash_type = args(0).string_value ();
   std::string str = args(1).string_value ();
 
-  // Determine hash function used
-  std::transform(hfun.begin(), hfun.end(), hfun.begin(), ::toupper);
-
-  // Buffer with maximum size for all hash function
-  char result_buffer[SHA512_DIGEST_SIZE];
-
-  int result_buffer_len = 0;
-  if (hfun == "MD2")
-    {
-      md2_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = MD2_DIGEST_SIZE;
-    }
-  else if (hfun == "MD4")
-    {
-      md4_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = MD4_DIGEST_SIZE;
-    }
-  else if (hfun == "MD5")
-    {
-      md5_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = MD5_DIGEST_SIZE;
-    }
-  else if (hfun == "SHA1")
-    {
-      sha1_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = SHA1_DIGEST_SIZE;
-    }
-  else if (hfun == "SHA224")
-    {
-      sha224_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = SHA224_DIGEST_SIZE;
-    }
-  else if (hfun == "SHA256")
-    {
-      sha256_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = SHA256_DIGEST_SIZE;
-    }
-  else if (hfun == "SHA384")
-    {
-      sha384_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = SHA384_DIGEST_SIZE;
-    }
-  else if (hfun == "SHA512")
-    {
-      sha512_buffer (str.data (), str.length (), result_buffer);
-      result_buffer_len = SHA512_DIGEST_SIZE;
-    }
-  else
-    error ("hash function not supported");
-
-  // Everything went fine, return formatted hash string
-  std::ostringstream os;
-  for (int i = 0; i < result_buffer_len; i++)
-    {
-      // Assure hex representation of one byte is written
-      os << std::hex << std::setw (2) << std::setfill ('0')
-         << (static_cast<int> (result_buffer[i]) & 0xFF);
-    }
-
-  return ovl (os.str ());
+  return ovl (octave::crypto::hash (hash_type, str));
 }
 
 /*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/util/lo-hash.cc	Mon Jun 13 18:36:16 2016 -0400
@@ -0,0 +1,142 @@
+/*
+
+Copyright (C) 2016 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 (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include <algorithm>
+#include <iomanip>
+#include <sstream>
+#include <string>
+
+#include "hash-wrappers.h"
+#include "lo-error.h"
+#include "lo-hash.h"
+#include "oct-locbuf.h"
+
+namespace octave
+{
+  namespace crypto
+  {
+    std::string
+    hash (hash_fptr hash_fcn, const std::string& str, int result_buf_len)
+    {
+      OCTAVE_LOCAL_BUFFER (unsigned char, result_buf, result_buf_len);
+
+      hash_fcn (str.data (), str.length (), result_buf);
+
+      std::ostringstream buf;
+
+      for (int i = 0; i < result_buf_len; i++)
+        buf << std::hex << std::setw (2) << std::setfill ('0')
+            << (result_buf[i] & 0xFF);
+
+      return buf.str ();
+    }
+
+    int md2_digest_size (void) { return octave_md2_digest_size (); }
+    int md4_digest_size (void) { return octave_md4_digest_size (); }
+    int md5_digest_size (void) { return octave_md5_digest_size (); }
+    int sha1_digest_size (void) { return octave_sha1_digest_size (); }
+    int sha224_digest_size (void) { return octave_sha224_digest_size (); }
+    int sha256_digest_size (void) { return octave_sha256_digest_size (); }
+    int sha384_digest_size (void) { return octave_sha384_digest_size (); }
+    int sha512_digest_size (void) { return octave_sha512_digest_size (); }
+
+    std::string
+    md2_hash (const std::string& str)
+    {
+      return hash (octave_md2_buffer_wrapper, str, md2_digest_size ());
+    }
+
+    std::string
+    md4_hash (const std::string& str)
+    {
+      return hash (octave_md4_buffer_wrapper, str, md4_digest_size ());
+    }
+
+    std::string
+    md5_hash (const std::string& str)
+    {
+      return hash (octave_md5_buffer_wrapper, str, md5_digest_size ());
+    }
+
+    std::string
+    sha1_hash (const std::string& str)
+    {
+      return hash (octave_sha1_buffer_wrapper, str, sha1_digest_size ());
+    }
+
+    std::string
+    sha224_hash (const std::string& str)
+    {
+      return hash (octave_sha224_buffer_wrapper, str, sha224_digest_size ());
+    }
+
+    std::string
+    sha256_hash (const std::string& str)
+    {
+      return hash (octave_sha256_buffer_wrapper, str, sha256_digest_size ());
+    }
+
+    std::string
+    sha384_hash (const std::string& str)
+    {
+      return hash (octave_sha384_buffer_wrapper, str, sha384_digest_size ());
+    }
+
+    std::string
+    sha512_hash (const std::string& str)
+    {
+      return hash (octave_sha512_buffer_wrapper, str, sha512_digest_size ());
+    }
+
+    std::string
+    hash (const std::string& hash_type, const std::string& str)
+    {
+      std::string ht = hash_type;
+
+      std::transform (ht.begin (), ht.end (), ht.begin (), ::toupper);
+
+      if (ht == "MD2")
+        return md2_hash (str);
+      else if (ht == "MD4")
+        return md4_hash (str);
+      else if (ht == "MD5")
+        return md5_hash (str);
+      else if (ht == "SHA1")
+        return sha1_hash (str);
+      else if (ht == "SHA224")
+        return sha224_hash (str);
+      else if (ht == "SHA256")
+        return sha256_hash (str);
+      else if (ht == "SHA384")
+        return sha384_hash (str);
+      else if (ht == "SHA512")
+        return sha512_hash (str);
+      else
+        (*current_liboctave_error_handler)
+          ("hash function '%s' not supported", hash_type.c_str ());
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/util/lo-hash.h	Mon Jun 13 18:36:16 2016 -0400
@@ -0,0 +1,61 @@
+/*
+
+Copyright (C) 2016 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_lo_hash_h)
+#define octave_lo_hash_h 1
+
+#include "octave-config.h"
+
+#include <string>
+
+namespace octave
+{
+  namespace crypto
+  {
+    typedef void * (hash_fptr) (const char *buffer, size_t len, void *res);
+
+    std::string
+    hash (hash_fptr hash_fcn, const std::string& str, int result_buf_len);
+
+    int md2_digest_size (void);
+    int md4_digest_size (void);
+    int md5_digest_size (void);
+    int sha1_digest_size (void);
+    int sha224_digest_size (void);
+    int sha256_digest_size (void);
+    int sha384_digest_size (void);
+    int sha512_digest_size (void);
+
+    std::string md2_hash (const std::string& str);
+    std::string md4_hash (const std::string& str);
+    std::string md5_hash (const std::string& str);
+    std::string sha1_hash (const std::string& str);
+    std::string sha224_hash (const std::string& str);
+    std::string sha256_hash (const std::string& str);
+    std::string sha384_hash (const std::string& str);
+    std::string sha512_hash (const std::string& str);
+
+    std::string hash (const std::string& hash_type, const std::string& str);
+  }
+}
+
+#endif
--- a/liboctave/util/module.mk	Mon Jun 13 16:01:26 2016 -0400
+++ b/liboctave/util/module.mk	Mon Jun 13 18:36:16 2016 -0400
@@ -11,6 +11,7 @@
   liboctave/util/lo-array-errwarn.h \
   liboctave/util/lo-array-gripes.h \
   liboctave/util/lo-cutils.h \
+  liboctave/util/lo-hash.h \
   liboctave/util/lo-ieee.h \
   liboctave/util/lo-macros.h \
   liboctave/util/lo-math.h \
@@ -59,6 +60,7 @@
   liboctave/util/kpse.cc \
   liboctave/util/lo-array-errwarn.cc \
   liboctave/util/lo-array-gripes.cc \
+  liboctave/util/lo-hash.cc \
   liboctave/util/lo-ieee.cc \
   liboctave/util/lo-utils.cc \
   liboctave/util/oct-base64.cc \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/hash-wrappers.c	Mon Jun 13 18:36:16 2016 -0400
@@ -0,0 +1,96 @@
+/*
+
+Copyright (C) 2016 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/>.
+
+*/
+
+// The hash functions are provided by gnulib.  We don't include gnulib
+// headers directly in Octave's C++ source files to avoid problems that
+// may be caused by the way that gnulib overrides standard library
+// functions.
+
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include "md2.h"
+#include "md4.h"
+#include "md5.h"
+#include "sha1.h"
+#include "sha256.h"
+#include "sha512.h"
+
+#include "hash-wrappers.h"
+
+int octave_md2_digest_size (void) { return MD2_DIGEST_SIZE; }
+int octave_md4_digest_size (void) { return MD4_DIGEST_SIZE; }
+int octave_md5_digest_size (void) { return MD5_DIGEST_SIZE; }
+int octave_sha1_digest_size (void) { return SHA1_DIGEST_SIZE; }
+int octave_sha224_digest_size (void) { return SHA224_DIGEST_SIZE; }
+int octave_sha256_digest_size (void) { return SHA256_DIGEST_SIZE; }
+int octave_sha384_digest_size (void) { return SHA384_DIGEST_SIZE; }
+int octave_sha512_digest_size (void) { return SHA512_DIGEST_SIZE; }
+
+void *
+octave_md2_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return md2_buffer (buf, len, res);
+}
+
+void *
+octave_md4_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return md4_buffer (buf, len, res);
+}
+
+void *
+octave_md5_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return md5_buffer (buf, len, res);
+}
+
+void *
+octave_sha1_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return sha1_buffer (buf, len, res);
+}
+
+void *
+octave_sha224_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return sha224_buffer (buf, len, res);
+}
+
+void *
+octave_sha256_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return sha256_buffer (buf, len, res);
+}
+
+void *
+octave_sha384_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return sha384_buffer (buf, len, res);
+}
+
+void *
+octave_sha512_buffer_wrapper (const char *buf, size_t len, void *res)
+{
+  return sha512_buffer (buf, len, res);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/hash-wrappers.h	Mon Jun 13 18:36:16 2016 -0400
@@ -0,0 +1,67 @@
+/*
+
+Copyright (C) 2016 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_hash_wrappers_h)
+#define octave_hash_wrappers_h 1
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+extern int octave_md2_digest_size (void);
+extern int octave_md4_digest_size (void);
+extern int octave_md5_digest_size (void);
+extern int octave_sha1_digest_size (void);
+extern int octave_sha224_digest_size (void);
+extern int octave_sha256_digest_size (void);
+extern int octave_sha384_digest_size (void);
+extern int octave_sha512_digest_size (void);
+
+extern void *
+octave_md2_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_md4_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_md5_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_sha1_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_sha224_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_sha256_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_sha384_buffer_wrapper (const char *buf, size_t len, void *res);
+
+extern void *
+octave_sha512_buffer_wrapper (const char *buf, size_t len, void *res);
+
+#if defined __cplusplus
+}
+#endif
+
+#endif
--- a/liboctave/wrappers/module.mk	Mon Jun 13 16:01:26 2016 -0400
+++ b/liboctave/wrappers/module.mk	Mon Jun 13 18:36:16 2016 -0400
@@ -1,6 +1,7 @@
 NOINSTALL_WRAPPERS_INC = \
   liboctave/wrappers/canonicalize-file-name-wrapper.h \
   liboctave/wrappers/gen-tempname-wrapper.h \
+  liboctave/wrappers/hash-wrappers.h \
   liboctave/wrappers/mkostemp-wrapper.h \
   liboctave/wrappers/nanosleep-wrapper.h \
   liboctave/wrappers/nproc-wrapper.h \
@@ -13,6 +14,7 @@
 WRAPPERS_SRC = \
   liboctave/wrappers/canonicalize-file-name-wrapper.c \
   liboctave/wrappers/gen-tempname-wrapper.c \
+  liboctave/wrappers/hash-wrappers.c \
   liboctave/wrappers/mkostemp-wrapper.c \
   liboctave/wrappers/nanosleep-wrapper.c \
   liboctave/wrappers/nproc-wrapper.c \