[glibmm] glibmm: Wrapped Base64 functionality from glib



commit f485de5dff5ae69eb724b05474cd3659344375e4
Author: Michael Kruglos <space3000 gmail com>
Date:   Wed Nov 14 00:32:45 2012 +0200

    glibmm: Wrapped Base64 functionality from glib
    
        encoding and decoding are wrapped.
        step by step and in-place decoding are not wrapped.
        (they're too low level for C++, and they're available from the C library.)
    
        Bug #611589.

 ChangeLog                       |   10 ++++++
 glib/glibmm.h                   |    1 +
 glib/glibmm/base64.cc           |   52 +++++++++++++++++++++++++++++++++
 glib/glibmm/base64.h            |   60 +++++++++++++++++++++++++++++++++++++++
 glib/glibmm/filelist.am         |    2 +
 tests/Makefile.am               |    2 +
 tests/glibmm_base64/Makefile.am |    7 ++++
 tests/glibmm_base64/main.cc     |   30 +++++++++++++++++++
 8 files changed, 164 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9c2884b..53cc809 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2013-05-07  Michael Kruglos <space3000 gmail com>
+
+       glibmm: Wrapped Base64 functionality from glib.
+    
+        glib/encoding and decoding are wrapped.
+        step by step and in-place decoding are not wrapped.
+        (they're too low level for C++, and they're available from the C library.)
+    
+        Bug #611589.
+
 2013-04-29  José Alburquerque  <jaalburquerque gmail com>
 
        gmmproc: Output.pm: Use a better name for the c param mappings hash.
diff --git a/glib/glibmm.h b/glib/glibmm.h
index 825b209..235545e 100644
--- a/glib/glibmm.h
+++ b/glib/glibmm.h
@@ -90,6 +90,7 @@
 
 #include <glibmm/arrayhandle.h>
 #include <glibmm/balancedtree.h>
+#include <glibmm/base64.h>
 #include <glibmm/bytearray.h>
 #include <glibmm/bytes.h>
 #include <glibmm/checksum.h>
diff --git a/glib/glibmm/base64.cc b/glib/glibmm/base64.cc
new file mode 100644
index 0000000..31d913c
--- /dev/null
+++ b/glib/glibmm/base64.cc
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2012-13 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <glibmm/base64.h>
+#include <glibmm/utility.h>
+
+namespace Glib
+{
+
+std::string Base64::encode(const std::string& source, bool break_lines)
+{
+  /* The output buffer must be large enough to fit all the data that will be
+     written to it. Due to the way base64 encodes you will need at least:
+     (len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of non-zero state).
+     If you enable line-breaking you will need at least:
+     ((len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space.
+  */
+  gsize length = (source.length() / 3 + 1) * 4 + 1; // + 1 for the terminating zero
+  length += ((length / 72) + 1); // in case break_lines = true
+  const Glib::ScopedPtr<char> buf ((char*) g_malloc(length));
+  gint state = 0, save = 0;
+  const guchar* src = reinterpret_cast<const unsigned char*>(source.data());
+  gsize out = g_base64_encode_step (src, source.length(), break_lines,
+    buf.get(), &state, &save);
+  out += g_base64_encode_close(break_lines, buf.get()+out, &state, &save);
+  return std::string(buf.get(), buf.get() + out);
+}
+
+std::string Base64::decode(const std::string& source)
+{
+  gsize size;
+  const Glib::ScopedPtr<char> buf((char*)g_base64_decode(source.c_str(), &size));
+  return std::string(buf.get(), buf.get() + size);
+}
+
+} // namespace Glib
+
diff --git a/glib/glibmm/base64.h b/glib/glibmm/base64.h
new file mode 100644
index 0000000..96ad384
--- /dev/null
+++ b/glib/glibmm/base64.h
@@ -0,0 +1,60 @@
+#ifndef _GLIBMM_BASE64_H
+#define _GLIBMM_BASE64_H
+
+/*
+ * Copyright (C) 2012-2013 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <string>
+#include <glib.h>
+
+namespace Glib
+{
+
+/** @defgroup Base64 Base64 routines
+ * Base64 encoding/decoding routines.
+ * @{
+ */
+
+namespace Base64
+{
+
+/** Encode the given string in base64 encoding.
+ *  You can pass <tt>true</tt> as the @a break_lines parameter to enable
+ *  line breaking, that is usually used in mail systems. The new line
+ *  character will appear for every 72 bytes.
+ * @param source A string to encode.
+ * @param line_break Enables/disables line breaking.
+ * @return The string encoded in Base-64.
+ */
+std::string encode(const std::string& source, bool break_lines = false);
+
+/** Decode the given base64 encoded string.
+ * @param source A string to decode.
+ * @return The resulting decode string
+ */
+std::string decode(const std::string& source);
+
+}
+
+/** @} group Base64 */
+
+} // namespace Glib
+
+
+#endif /* _GLIBMM_BASE64_H */
+
diff --git a/glib/glibmm/filelist.am b/glib/glibmm/filelist.am
index 80b242e..dcc3294 100644
--- a/glib/glibmm/filelist.am
+++ b/glib/glibmm/filelist.am
@@ -6,6 +6,7 @@ glibmm_files_built_ph = $(patsubst %.hg,private/%_p.h,$(glibmm_files_hg))
 
 glibmm_files_extra_cc =                        \
        arrayhandle.cc                  \
+       base64.cc                       \
        class.cc                        \
        containers.cc                   \
        debug.cc                        \
@@ -42,6 +43,7 @@ glibmm_files_extra_cc =                       \
 
 glibmm_files_extra_h =                 \
        arrayhandle.h                   \
+       base64.h                        \
        class.h                         \
        containerhandle_shared.h        \
        containers.h                    \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 615f00a..7e39e65 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -24,6 +24,7 @@ check_PROGRAMS =                              \
        giomm_asyncresult_sourceobject/test     \
        giomm_tls_client/test                   \
        glibmm_btree/test                       \
+       glibmm_base64/test                      \
        glibmm_date/test                        \
        glibmm_buildfilename/test               \
        glibmm_interface_implementation/test    \
@@ -71,6 +72,7 @@ giomm_asyncresult_sourceobject_test_LDADD    = $(giomm_ldadd)
 giomm_tls_client_test_SOURCES                = giomm_tls_client/main.cc
 giomm_tls_client_test_LDADD                  = $(giomm_ldadd)
 
+glibmm_base64_test_SOURCES               = glibmm_base64/main.cc
 glibmm_btree_test_SOURCES                = glibmm_btree/main.cc
 glibmm_buildfilename_test_SOURCES        = glibmm_buildfilename/main.cc
 glibmm_date_test_SOURCES                 = glibmm_date/main.cc
diff --git a/tests/glibmm_base64/Makefile.am b/tests/glibmm_base64/Makefile.am
new file mode 100644
index 0000000..d0e58bd
--- /dev/null
+++ b/tests/glibmm_base64/Makefile.am
@@ -0,0 +1,7 @@
+include $(top_srcdir)/tests/Makefile.am_fragment
+
+noinst_PROGRAMS = test
+test_SOURCES = main.cc
+
+
+
diff --git a/tests/glibmm_base64/main.cc b/tests/glibmm_base64/main.cc
new file mode 100644
index 0000000..dc4dd62
--- /dev/null
+++ b/tests/glibmm_base64/main.cc
@@ -0,0 +1,30 @@
+#include <glibmm.h>
+#include <iostream>
+#include <string>
+
+int
+main()
+{
+  std::string glib_base64 = "R2xpYg==";
+  std::string glibmm_base64 = "R2xpYm1t";
+  std::string stallman_quote_base64 = 
"VmFsdWUgeW91ciBmcmVlZG9tIG9yIHlvdSB3aWxsIGxvc2UgaXQsIHRlYWNoZXMgaGlzdG9yeS4K\n"
+    "J0Rvbid0IGJvdGhlciB1cyB3aXRoIHBvbGl0aWNzJywgcmVzcG9uZCB0aG9zZSB3aG8gZG9uJ3Qg\n"
+    "d2FudCB0byBsZWFybi4KCi0tIFJpY2hhcmQgU3RhbGxtYW4=\n";
+
+  // test that encodes the string "Glib" into base64
+  std::cerr << Glib::Base64::encode("Glib") << std::endl;
+  g_assert(Glib::Base64::encode("Glib") == glib_base64);
+
+  // test that encodes the quote by Richard Stallman into base64 with linebreaks (the output has line breaks)
+  std::cerr << Glib::Base64::encode("Value your freedom or you will lose it, teaches history.\n"
+        "'Don't bother us with politics', respond those who don't want to learn.\n\n-- Richard Stallman", 
true) << std::endl;
+  std::cerr << stallman_quote_base64 << std::endl;
+  g_assert(Glib::Base64::encode("Value your freedom or you will lose it, teaches history.\n"
+        "'Don't bother us with politics', respond those who don't want to learn.\n\n-- Richard Stallman", 
true) == stallman_quote_base64);
+
+  // test that decodes the string "Glibmm" from base64
+  std::cerr << Glib::Base64::decode(glibmm_base64) << std::endl;
+  g_assert(Glib::Base64::decode(glibmm_base64) == "Glibmm");
+
+  return EXIT_SUCCESS;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]