/pidgin/main: 252bb96afbe0: facebook: use the GConverter interfa...
James Geboski <[email protected]>
| Newsgroups | gmane.comp.gnome.gaim.cvs |
|---|---|
| Message-ID | <[email protected]> |
Changeset: 252bb96afbe0a352047e616e2dcfcc298234563c Author: James Geboski <[email protected]> Date: 2015-12-15 22:35 -0500 Branch: default URL: https://hg.pidgin.im/pidgin/main/rev/252bb96afbe0 Description: facebook: use the GConverter interface instead of zlib diffstat: configure.ac | 2 +- libpurple/protocols/facebook/api.c | 26 ++++-- libpurple/protocols/facebook/util.c | 141 ++++++++++++++--------------------- libpurple/protocols/facebook/util.h | 6 +- 4 files changed, 78 insertions(+), 97 deletions(-) diffs (295 lines): diff --git a/configure.ac b/configure.ac --- a/configure.ac +++ b/configure.ac @@ -479,7 +479,7 @@ AM_CONDITIONAL(INSTALL_I18N, test "x$ena dnl ####################################################################### dnl # Check for GLib 2.20 (required) dnl ####################################################################### -PKG_CHECK_MODULES(GLIB, [glib-2.0 >= 2.28.0 gobject-2.0 gthread-2.0], , [ +PKG_CHECK_MODULES(GLIB, [glib-2.0 >= 2.28.0 gio-2.0 gobject-2.0 gthread-2.0], , [ AC_MSG_RESULT(no) AC_MSG_ERROR([ diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c --- a/libpurple/protocols/facebook/api.c +++ b/libpurple/protocols/facebook/api.c @@ -844,6 +844,7 @@ fb_api_cb_mqtt_open(FbMqtt *mqtt, gpoint FbApiPrivate *priv = api->priv; FbThrift *thft; GByteArray *cytes; + GError *err = NULL; static guint8 flags = FB_MQTT_CONNECT_FLAG_USER | FB_MQTT_CONNECT_FLAG_PASS | @@ -918,7 +919,12 @@ fb_api_cb_mqtt_open(FbMqtt *mqtt, gpoint 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_INFO, bytes, "Writing connect"); fb_mqtt_connect(mqtt, flags, cytes); @@ -1599,6 +1605,7 @@ fb_api_cb_mqtt_publish(FbMqtt *mqtt, con FbApi *api = data; gboolean comp; GByteArray *bytes; + GError *err = NULL; guint i; static const struct { @@ -1615,13 +1622,8 @@ fb_api_cb_mqtt_publish(FbMqtt *mqtt, con 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; } @@ -2112,6 +2114,7 @@ fb_api_publish(FbApi *api, const gchar * GByteArray *bytes; GByteArray *cytes; gchar *msg; + GError *err = NULL; va_list ap; g_return_if_fail(FB_IS_API(api)); @@ -2124,7 +2127,12 @@ fb_api_publish(FbApi *api, const gchar * 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_INFO, bytes, "Writing message (topic: %s)", diff --git a/libpurple/protocols/facebook/util.c b/libpurple/protocols/facebook/util.c --- a/libpurple/protocols/facebook/util.c +++ b/libpurple/protocols/facebook/util.c @@ -21,9 +21,9 @@ #include "internal.h" +#include <gio/gio.h> #include <stdarg.h> #include <string.h> -#include <zlib.h> #include "blistnodetypes.h" #include "buddylist.h" @@ -483,18 +483,6 @@ fb_util_str_is(const gchar *str, GAsciiT return TRUE; } -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) { @@ -510,90 +498,73 @@ fb_util_zcompressed(const GByteArray *by 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 */ +} + +static GByteArray * +fb_util_zconv(GConverter *conv, const GByteArray *bytes, GError **error) +{ + GByteArray *ret; + GConverterResult res; + gsize cize = 0; + gsize rize; + gsize wize; + guint8 data[1024]; + + ret = g_byte_array_new(); + + while (TRUE) { + rize = 0; + wize = 0; + + res = g_converter_convert(conv, + bytes->data + cize, + bytes->len - cize, + data, sizeof data, + G_CONVERTER_INPUT_AT_END, + &rize, &wize, error); + + switch (res) { + case G_CONVERTER_CONVERTED: + g_byte_array_append(ret, data, wize); + cize += rize; + break; + + case G_CONVERTER_ERROR: + g_byte_array_free(ret, TRUE); + return NULL; + + case G_CONVERTER_FINISHED: + g_byte_array_append(ret, data, wize); + return ret; + + default: + break; + } + } } GByteArray * -fb_util_zcompress(const GByteArray *bytes) +fb_util_zcompress(const GByteArray *bytes, GError **error) { GByteArray *ret; - gint res; - gsize size; - z_stream zs; + GZlibCompressor *conv; - 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 (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) { - return NULL; - } - - size = compressBound(bytes->len); - ret = g_byte_array_new(); - - g_byte_array_set_size(ret, size); - - zs.next_out = ret->data; - zs.avail_out = size; - - res = deflate(&zs, Z_FINISH); - - if (res != Z_STREAM_END) { - deflateEnd(&zs); - g_byte_array_free(ret, TRUE); - return NULL; - } - - size -= zs.avail_out; - g_byte_array_remove_range(ret, size, ret->len - size); - - 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; + GZlibDecompressor *conv; - 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); - - 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/libpurple/protocols/facebook/util.h b/libpurple/protocols/facebook/util.h --- a/libpurple/protocols/facebook/util.h +++ b/libpurple/protocols/facebook/util.h @@ -320,6 +320,7 @@ fb_util_zcompressed(const GByteArray *by /** * 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. @@ -327,11 +328,12 @@ fb_util_zcompressed(const GByteArray *by * 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. @@ -339,6 +341,6 @@ fb_util_zcompress(const GByteArray *byte * 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_ */ _______________________________________________ Commits mailing list [email protected] https://pidgin.im/cgi-bin/mailman/listinfo/commits