aboutsummaryrefslogtreecommitdiffstats
path: root/facebook/facebook-util.c
diff options
context:
space:
mode:
authorjgeboski <jgeboski@gmail.com>2015-12-15 23:41:53 -0500
committerjgeboski <jgeboski@gmail.com>2015-12-20 13:08:39 -0500
commit2018aa1a288d5204d5d066bff672df8cb46065b9 (patch)
treecc051528a9ce8896cbc6d2b9d2bab0190cba3a35 /facebook/facebook-util.c
parentb9f3d252fc662e3ff3a216f1ea42e0d85cd34d69 (diff)
downloadbitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.gz
bitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.bz2
bitlbee-facebook-2018aa1a288d5204d5d066bff672df8cb46065b9.tar.xz
facebook-util: use the GConverter interface instead of zlib
Diffstat (limited to 'facebook/facebook-util.c')
-rw-r--r--facebook/facebook-util.c125
1 files changed, 48 insertions, 77 deletions
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;
}