Wrappers: Use GLib base64 functions
authorAthanasios Oikonomou <athoik@gmail.com>
Sun, 9 Aug 2015 13:49:55 +0000 (16:49 +0300)
committerErik Slagter <erik@openpli.org>
Sun, 6 Sep 2015 14:10:47 +0000 (16:10 +0200)
GLib provides base64 encode and decode functions,
since we are already depend on GLib because of servicemp3
we can use them and get rid of OpenSSL base64 functions

This commit introduces base64encode and base64decode wrapped
GLib functions and uses them in httpstream and streamserver.

Signed-off-by: Erik Slagter <erik@openpli.org>

lib/base/httpstream.cpp
lib/base/wrappers.cpp
lib/base/wrappers.h
lib/dvb/streamserver.cpp

index f638532..00152ac 100644 (file)
@@ -1,5 +1,4 @@
 #include <cstdio>
-#include <openssl/evp.h>
 
 #include <lib/base/httpstream.h>
 #include <lib/base/eerror.h>
@@ -57,24 +56,8 @@ int eHttpStream::openUrl(const std::string &url, std::string &newurl)
        int authenticationindex = hostname.find("@");
        if (authenticationindex > 0)
        {
-               BIO *mbio, *b64bio, *bio;
-               char *p = (char*)NULL;
-               int length = 0;
-               authorizationData = hostname.substr(0, authenticationindex);
+               authorizationData =  base64encode(hostname.substr(0, authenticationindex));
                hostname = hostname.substr(authenticationindex + 1);
-               mbio = BIO_new(BIO_s_mem());
-               b64bio = BIO_new(BIO_f_base64());
-               bio = BIO_push(b64bio, mbio);
-               BIO_write(bio, authorizationData.c_str(), authorizationData.length());
-               BIO_flush(bio);
-               length = BIO_ctrl(mbio, BIO_CTRL_INFO, 0, (char*)&p);
-               authorizationData = "";
-               if (p && length > 0)
-               {
-                       /* base64 output contains a linefeed, which we ignore */
-                       authorizationData.append(p, length - 1);
-               }
-               BIO_free_all(bio);
        }
        int customportindex = hostname.find(":");
        if (customportindex > 0)
index 1c0dc0e..ed3d3bb 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/select.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#include <glib.h>
 
 #include <vector>
 #include <string>
@@ -259,3 +260,46 @@ ssize_t writeAll(int fd, const void *buf, size_t count)
        }
        return handledcount;
 }
+
+std::string base64encode(const std::string str)
+{
+       if(str.empty())
+       {
+               return std::string();
+       }
+
+       char *out = g_base64_encode((const guchar*)str.c_str(), (gsize)str.length());
+
+       if(!out)
+       {
+               g_free(out);
+               return std::string();
+       }
+
+       std::string ret((const char*)out);
+       g_free(out);
+
+       return ret;
+}
+
+std::string base64decode(const std::string hash)
+{
+       if(hash.empty())
+       {
+               return std::string();
+       }
+
+       gsize len;
+       guchar *out = g_base64_decode((const gchar*)hash.c_str(), &len);
+
+       if(!out || !len)
+       {
+               g_free(out);
+               return std::string();
+       }
+
+       std::string ret((const char*)out, len);
+       g_free(out);
+
+       return ret;
+}
index da43cce..d53ac29 100644 (file)
@@ -7,5 +7,7 @@ ssize_t readLine(int fd, char** buffer, size_t* bufsize);
 ssize_t writeAll(int fd, const void *buf, size_t count);
 int Select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
 int Connect(const char *hostname, int port, int timeoutsec);
+std::string base64encode(const std::string str);
+std::string base64decode(const std::string hash);
 
 #endif
index 8bb489e..d3b5025 100644 (file)
@@ -1,7 +1,6 @@
 #include <sys/select.h>
 #include <unistd.h>
 #include <string.h>
-#include <openssl/evp.h>
 #include <sys/types.h>
 #include <pwd.h>
 #include <shadow.h>
@@ -82,24 +81,7 @@ void eStreamClient::notifier(int what)
                                std::string hash = request.substr(pos + 21);
                                pos = hash.find('\r');
                                hash = hash.substr(0, pos);
-                               hash += "\n";
-                               {
-                                       char *in, *out;
-                                       in = strdup(hash.c_str());
-                                       out = (char*)calloc(1, hash.size());
-                                       if (in && out)
-                                       {
-                                               BIO *b64, *bmem;
-                                               b64 = BIO_new(BIO_f_base64());
-                                               bmem = BIO_new_mem_buf(in, hash.size());
-                                               bmem = BIO_push(b64, bmem);
-                                               BIO_read(bmem, out, hash.size());
-                                               BIO_free_all(bmem);
-                                               authentication.append(out, hash.size());
-                                       }
-                                       free(in);
-                                       free(out);
-                               }
+                               authentication = base64decode(hash);
                                pos = authentication.find(':');
                                if (pos != std::string::npos)
                                {