diff options
author | jgeboski <jgeboski@gmail.com> | 2015-12-15 23:41:53 -0500 |
---|---|---|
committer | jgeboski <jgeboski@gmail.com> | 2015-12-20 13:08:39 -0500 |
commit | 2018aa1a288d5204d5d066bff672df8cb46065b9 (patch) | |
tree | cc051528a9ce8896cbc6d2b9d2bab0190cba3a35 | |
parent | b9f3d252fc662e3ff3a216f1ea42e0d85cd34d69 (diff) | |
download | bitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.gz bitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.bz2 bitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.xz |
facebook-util: use the GConverter interface instead of zlib
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | debian/control | 4 | ||||
-rw-r--r-- | facebook/Makefile.am | 6 | ||||
-rw-r--r-- | facebook/facebook-api.c | 25 | ||||
-rw-r--r-- | facebook/facebook-util.c | 125 | ||||
-rw-r--r-- | facebook/facebook-util.h | 6 |
6 files changed, 74 insertions, 95 deletions
diff --git a/configure.ac b/configure.ac index a492ced..31b534d 100644 --- a/configure.ac +++ b/configure.ac @@ -77,9 +77,8 @@ AC_ARG_WITH( ) PKG_CHECK_MODULES([BITLBEE], [bitlbee >= 3.4]) -PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.32.0 gobject-2.0]) +PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.32.0 gio-2.0 gobject-2.0]) PKG_CHECK_MODULES([JSON], [json-glib-1.0 >= 0.14.0]) -PKG_CHECK_MODULES([ZLIB], [zlib]) PKG_CHECK_VAR([GLIB_GENMARSHAL], [glib-2.0], [glib_genmarshal]) AS_IF( diff --git a/debian/control b/debian/control index 2bd6fa5..007f86e 100644 --- a/debian/control +++ b/debian/control @@ -3,14 +3,14 @@ Maintainer: jgeboski <jgeboski@gmail.com> Section: misc Priority: optional Standards-Version: 3.9.6 -Build-Depends: bitlbee-dev (>= 3.2.2), debhelper (>= 9), dh-autoreconf, libglib2.0-dev (>= 2.32), libjson-glib-dev (>= 0.14), zlib1g-dev +Build-Depends: bitlbee-dev (>= 3.2.2), debhelper (>= 9), dh-autoreconf, libglib2.0-dev (>= 2.32), libjson-glib-dev (>= 0.14) Homepage: https://github.com/jgeboski/bitlbee-facebook Package: bitlbee-facebook Architecture: any Section: misc Priority: optional -Depends: ${shlibs:Depends}, ${misc:Depends}, bitlbee (>= 3.2.2) | bitlbee-libpurple (>= 3.2.2), libglib2.0-0 (>= 2.32), libjson-glib-1.0-0 (>= 0.14), zlib1g +Depends: ${shlibs:Depends}, ${misc:Depends}, bitlbee (>= 3.2.2) | bitlbee-libpurple (>= 3.2.2), libglib2.0-0 (>= 2.32), libjson-glib-1.0-0 (>= 0.14) Homepage: https://github.com/jgeboski/bitlbee-facebook Description: Facebook protocol plugin for BitlBee BitlBee Facebook implements the Facebook Messenger protocol into diff --git a/facebook/Makefile.am b/facebook/Makefile.am index 0e77b91..bb154e5 100644 --- a/facebook/Makefile.am +++ b/facebook/Makefile.am @@ -4,14 +4,12 @@ lib_LTLIBRARIES = facebook.la facebook_la_CFLAGS = \ $(BITLBEE_CFLAGS) \ $(JSON_CFLAGS) \ - $(GLIB_CFLAGS) \ - $(ZLIB_CFLAGS) + $(GLIB_CFLAGS) facebook_la_LDFLAGS = \ $(BITLBEE_LIBS) \ $(JSON_LIBS) \ - $(GLIB_LIBS) \ - $(ZLIB_LIBS) + $(GLIB_LIBS) facebook_la_SOURCES = \ facebook-marshal.c \ diff --git a/facebook/facebook-api.c b/facebook/facebook-api.c index 512bd3f..57a22b0 100644 --- a/facebook/facebook-api.c +++ b/facebook/facebook-api.c @@ -799,6 +799,7 @@ fb_api_cb_mqtt_open(FbMqtt *mqtt, gpointer data) FbApiPrivate *priv = api->priv; FbThrift *thft; GByteArray *cytes; + GError *err = NULL; static guint8 flags = FB_MQTT_CONNECT_FLAG_USER | FB_MQTT_CONNECT_FLAG_PASS | @@ -873,7 +874,12 @@ fb_api_cb_mqtt_open(FbMqtt *mqtt, gpointer data) fb_thrift_write_stop(thft); bytes = fb_thrift_get_bytes(thft); - cytes = fb_util_zcompress(bytes); + cytes = fb_util_zcompress(bytes, &err); + + FB_API_ERROR_EMIT(api, err, + g_object_unref(thft); + return; + ); fb_util_debug_hexdump(FB_UTIL_DEBUG_LEVEL_INFO, bytes, "Writing connect"); fb_mqtt_connect(mqtt, flags, cytes); @@ -1547,6 +1553,7 @@ fb_api_cb_mqtt_publish(FbMqtt *mqtt, const gchar *topic, GByteArray *pload, FbApi *api = data; gboolean comp; GByteArray *bytes; + GError *err = NULL; guint i; static const struct { @@ -1563,12 +1570,8 @@ fb_api_cb_mqtt_publish(FbMqtt *mqtt, const gchar *topic, GByteArray *pload, comp = fb_util_zcompressed(pload); if (G_LIKELY(comp)) { - bytes = fb_util_zuncompress(pload); - - if (G_UNLIKELY(bytes == NULL)) { - fb_api_error(api, FB_API_ERROR, "Failed to decompress"); - return; - } + bytes = fb_util_zuncompress(pload, &err); + FB_API_ERROR_EMIT(api, err, return); } else { bytes = (GByteArray*) pload; } @@ -2054,6 +2057,7 @@ fb_api_publish(FbApi *api, const gchar *topic, const gchar *format, ...) GByteArray *bytes; GByteArray *cytes; gchar *msg; + GError *err = NULL; va_list ap; g_return_if_fail(FB_IS_API(api)); @@ -2066,7 +2070,12 @@ fb_api_publish(FbApi *api, const gchar *topic, const gchar *format, ...) va_end(ap); bytes = g_byte_array_new_take((guint8*) msg, strlen(msg)); - cytes = fb_util_zcompress(bytes); + cytes = fb_util_zcompress(bytes, &err); + + FB_API_ERROR_EMIT(api, err, + g_byte_array_free(bytes, TRUE); + return; + ); fb_util_debug_hexdump(FB_UTIL_DEBUG_LEVEL_INFO, bytes, "Writing message (topic: %s)", diff --git a/facebook/facebook-util.c b/facebook/facebook-util.c index dba7b82..a436a46 100644 --- a/facebook/facebook-util.c +++ b/facebook/facebook-util.c @@ -16,10 +16,10 @@ */ #include <bitlbee.h> +#include <gio/gio.h> #include <sha1.h> #include <stdarg.h> #include <string.h> -#include <zlib.h> #include "facebook-util.h" @@ -295,18 +295,6 @@ fb_util_uuid(void) return sha1_random_uuid(&sha); } -static voidpf -fb_util_zalloc(voidpf opaque, uInt items, uInt size) -{ - return g_malloc(size * items); -} - -static void -fb_util_zfree(voidpf opaque, voidpf address) -{ - g_free(address); -} - gboolean fb_util_zcompressed(const GByteArray *bytes) { @@ -322,90 +310,73 @@ fb_util_zcompressed(const GByteArray *bytes) b0 = *(bytes->data + 0); b1 = *(bytes->data + 1); - return ((((b0 << 8) | b1) % 31) == 0) && /* Check the header */ - ((b0 & 0x0F) == Z_DEFLATED); /* Check the method */ + return ((((b0 << 8) | b1) % 31) == 0) && /* Check the header */ + ((b0 & 0x0F) == 8 /* Z_DEFLATED */); /* Check the method */ } -GByteArray * -fb_util_zcompress(const GByteArray *bytes) +static GByteArray * +fb_util_zconv(GConverter *conv, const GByteArray *bytes, GError **error) { GByteArray *ret; - gint res; - gsize size; - z_stream zs; - - g_return_val_if_fail(bytes != NULL, NULL); + GConverterResult res; + gsize cize = 0; + gsize rize; + gsize wize; + guint8 data[1024]; - memset(&zs, 0, sizeof zs); - zs.zalloc = fb_util_zalloc; - zs.zfree = fb_util_zfree; - zs.next_in = bytes->data; - zs.avail_in = bytes->len; + ret = g_byte_array_new(); - if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) { - return NULL; - } + while (TRUE) { + rize = 0; + wize = 0; - size = compressBound(bytes->len); - ret = g_byte_array_new(); + res = g_converter_convert(conv, + bytes->data + cize, + bytes->len - cize, + data, sizeof data, + G_CONVERTER_INPUT_AT_END, + &rize, &wize, error); - g_byte_array_set_size(ret, size); + switch (res) { + case G_CONVERTER_CONVERTED: + g_byte_array_append(ret, data, wize); + cize += rize; + break; - zs.next_out = ret->data; - zs.avail_out = size; + case G_CONVERTER_ERROR: + g_byte_array_free(ret, TRUE); + return NULL; - res = deflate(&zs, Z_FINISH); + case G_CONVERTER_FINISHED: + g_byte_array_append(ret, data, wize); + return ret; - if (res != Z_STREAM_END) { - deflateEnd(&zs); - g_byte_array_free(ret, TRUE); - return NULL; + default: + break; + } } +} - size -= zs.avail_out; - g_byte_array_remove_range(ret, size, ret->len - size); +GByteArray * +fb_util_zcompress(const GByteArray *bytes, GError **error) +{ + GByteArray *ret; + GZlibCompressor *conv; - deflateEnd(&zs); + conv = g_zlib_compressor_new(G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1); + ret = fb_util_zconv(G_CONVERTER(conv), bytes, error); + g_object_unref(conv); return ret; } GByteArray * -fb_util_zuncompress(const GByteArray *bytes) +fb_util_zuncompress(const GByteArray *bytes, GError **error) { GByteArray *ret; - gint res; - guint8 out[1024]; - z_stream zs; - - g_return_val_if_fail(bytes != NULL, NULL); - - memset(&zs, 0, sizeof zs); - zs.zalloc = fb_util_zalloc; - zs.zfree = fb_util_zfree; - zs.next_in = bytes->data; - zs.avail_in = bytes->len; - - if (inflateInit(&zs) != Z_OK) { - return NULL; - } - - ret = g_byte_array_new(); - - do { - zs.next_out = out; - zs.avail_out = sizeof out; - - res = inflate(&zs, Z_NO_FLUSH); - - if ((res != Z_OK) && (res != Z_STREAM_END)) { - inflateEnd(&zs); - g_byte_array_free(ret, TRUE); - return NULL; - } - - g_byte_array_append(ret, out, sizeof out - zs.avail_out); - } while (res != Z_STREAM_END); + GZlibDecompressor *conv; - inflateEnd(&zs); + conv = g_zlib_decompressor_new(G_ZLIB_COMPRESSOR_FORMAT_ZLIB); + ret = fb_util_zconv(G_CONVERTER(conv), bytes, error); + g_object_unref(conv); return ret; } diff --git a/facebook/facebook-util.h b/facebook/facebook-util.h index f1eb84a..6c71dce 100644 --- a/facebook/facebook-util.h +++ b/facebook/facebook-util.h @@ -224,6 +224,7 @@ fb_util_zcompressed(const GByteArray *bytes); /** * fb_util_zcompress: * @bytes: The #GByteArray. + * @error: The return location for the #GError, or #NULL. * * Compresses a #GByteArray with zlib. The returned #GByteArray should * be freed with #g_byte_array_free() when no longer needed. @@ -231,11 +232,12 @@ fb_util_zcompressed(const GByteArray *bytes); * Returns: The compressed #GByteArray. */ GByteArray * -fb_util_zcompress(const GByteArray *bytes); +fb_util_zcompress(const GByteArray *bytes, GError **error); /** * fb_util_zuncompress: * @bytes: The #GByteArray. + * @error: The return location for the #GError, or #NULL. * * Uncompresses a #GByteArray with zlib. The returned #GByteArray * should be freed with #g_byte_array_free() when no longer needed. @@ -243,6 +245,6 @@ fb_util_zcompress(const GByteArray *bytes); * Returns: The uncompressed #GByteArray, or #NULL on error. */ GByteArray * -fb_util_zuncompress(const GByteArray *bytes); +fb_util_zuncompress(const GByteArray *bytes, GError **error); #endif /* _FACEBOOK_UTIL_H_ */ |