summaryrefslogtreecommitdiff
path: root/glib
diff options
context:
space:
mode:
authorMichael Kruglos <space3000@gmail.com>2012-11-14 00:32:45 +0200
committerMurray Cumming <murrayc@murrayc.com>2013-05-07 12:04:28 +0200
commitf485de5dff5ae69eb724b05474cd3659344375e4 (patch)
treea737410e8b204e476268364d493205812f3f84af /glib
parentac21511f5308e92277095cab923a00b2839b012e (diff)
downloadglibmm-f485de5dff5ae69eb724b05474cd3659344375e4.tar.gz
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.
Diffstat (limited to 'glib')
-rw-r--r--glib/glibmm.h1
-rw-r--r--glib/glibmm/base64.cc52
-rw-r--r--glib/glibmm/base64.h60
-rw-r--r--glib/glibmm/filelist.am2
4 files changed, 115 insertions, 0 deletions
diff --git a/glib/glibmm.h b/glib/glibmm.h
index 825b2099..235545e2 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 00000000..31d913c3
--- /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 00000000..96ad3847
--- /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 80b242e8..dcc3294a 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 \